Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(layer-shell-scaling): frozen buffers must scale to output scaling. #112

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions libwayshot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ thiserror = "1"
wayland-client = "0.31.1"
wayland-protocols = { version = "0.31.0", features = ["client", "unstable"] }
wayland-protocols-wlr = { version = "0.2.0", features = ["client"] }
tempfile = "3.10.1"
54 changes: 52 additions & 2 deletions libwayshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod screencopy;
use std::{
collections::HashSet,
fs::File,
io::Write,
os::fd::AsFd,
sync::atomic::{AtomicBool, Ordering},
thread,
Expand Down Expand Up @@ -446,13 +447,53 @@ impl WayshotConnection {
}
};

let shm = self.globals.bind::<WlShm, _, _>(&qh, 1..=1, ())?;
for (frame_copy, frame_guard, output_info) in frames {
tracing::span!(
tracing::Level::DEBUG,
"overlay_frames::surface",
output = format!("{output_info}")
)
.in_scope(|| -> Result<()> {
let (scale, buffer) = {
let output_size = output_info.logical_region.inner.size;
if output_info.scale() % 1.0 >= 0.1 {
let file = tempfile::tempfile()?;
let image: DynamicImage = frame_copy.try_into()?;
let image = image::imageops::resize(
&image,
output_size.width,
output_size.height,
image::imageops::FilterType::Triangle,
);
init_overlay(&image, &file);
let pool = shm.create_pool(
file.as_fd(),
frame_copy
.frame_format
.byte_size()
.try_into()
.map_err(|_| Error::BufferTooSmall)?,
&qh,
(),
);

(
1,
pool.create_buffer(
0,
output_size.width as i32,
output_size.height as i32,
(output_size.width * 4) as i32,
frame_copy.frame_format.format,
&qh,
(),
),
)
} else {
(output_info.scale() as i32, frame_guard.buffer.clone())
}
};
let surface = compositor.create_surface(&qh, ());

let layer_surface = layer_shell.get_layer_surface(
Expand Down Expand Up @@ -480,8 +521,8 @@ impl WayshotConnection {
}

surface.set_buffer_transform(output_info.transform);
// surface.set_buffer_scale(output_info.scale());
surface.attach(Some(&frame_guard.buffer), 0, 0);
surface.set_buffer_scale(scale);
surface.attach(Some(&buffer), 0, 0);

debug!("Committing surface with attached buffer.");
surface.commit();
Expand Down Expand Up @@ -675,3 +716,12 @@ impl WayshotConnection {
self.screenshot_outputs(self.get_all_outputs(), cursor_overlay)
}
}

fn init_overlay(origin_image: &image::ImageBuffer<image::Rgba<u8>, Vec<u8>>, tmp: &File) {
let mut buf = std::io::BufWriter::new(tmp);

for index in origin_image.pixels() {
buf.write_all(&index.0).unwrap();
}
buf.flush().unwrap();
}
4 changes: 2 additions & 2 deletions wayshot/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ pub struct Cli {
pub log_level: tracing::Level,

/// Arguments to call slurp with for selecting a region
#[arg(short, long, value_name = "SLURP_ARGS")]
pub slurp: Option<String>,
#[arg(short, long)]
pub slurp: bool,

/// Enable cursor in screenshots
#[arg(short, long)]
Expand Down
8 changes: 2 additions & 6 deletions wayshot/src/wayshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,11 @@ fn main() -> Result<()> {
return Ok(());
}

let image_buffer = if let Some(slurp_region) = cli.slurp {
let slurp_region = slurp_region.clone();
let image_buffer = if cli.slurp {
wayshot_conn.screenshot_freeze(
Box::new(move || {
|| -> Result<LogicalRegion> {
let slurp_output = Command::new("slurp")
.args(slurp_region.split(' '))
.output()?
.stdout;
Comment on lines -69 to -77
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change this behaviour? This is removing the ability to pass arguments to slurp.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slurp only accept '-d' to select screen, but -d cannot be passed into, and it is useless

Copy link
Collaborator Author

@Decodetalkers Decodetalkers Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
all the argument is like -d -w, how can you pass the argument into it? I have try send it , but it can never be received

image

like such kind of error

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cargo run -- --slurp '\-d'
this command will work

but '-d' will never work

let slurp_output = Command::new("slurp").arg("-d").output()?.stdout;

utils::parse_geometry(&String::from_utf8(slurp_output)?)
}()
Expand Down
Loading