From b1334c63d7b86e5253737b2e8f38808b33f0c8dc Mon Sep 17 00:00:00 2001 From: ShootingStarDragons Date: Fri, 16 Feb 2024 12:44:51 +0800 Subject: [PATCH] fix: scale problem --- Cargo.lock | 25 +++++++-------------- libwayshot/Cargo.toml | 1 + libwayshot/src/lib.rs | 51 +++++++++++++++++++++++++++++++++++++------ 3 files changed, 53 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9244fa4d..fe984974 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -306,9 +306,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.151" +version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "libloading" @@ -327,6 +327,7 @@ dependencies = [ "image", "memmap2", "nix 0.27.1", + "tempfile", "thiserror", "tracing", "wayland-client", @@ -516,20 +517,11 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "redox_syscall" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" -dependencies = [ - "bitflags 1.3.2", -] - [[package]] name = "rustix" -version = "0.38.28" +version = "0.38.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72e572a5e8ca657d7366229cdde4bd14c4eb5499a9573d4d366fe1b599daa316" +checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949" dependencies = [ "bitflags 2.4.1", "errno", @@ -590,15 +582,14 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.8.1" +version = "3.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ef1adac450ad7f4b3c28589471ade84f25f731a7a0fe30d71dfa9f60fd808e5" +checksum = "a365e8cd18e44762ef95d87f284f4b5cd04107fec2ff3052bd6a3e6069669e67" dependencies = [ "cfg-if", "fastrand", - "redox_syscall", "rustix", - "windows-sys 0.48.0", + "windows-sys 0.52.0", ] [[package]] diff --git a/libwayshot/Cargo.toml b/libwayshot/Cargo.toml index 8a9719b0..c6547705 100644 --- a/libwayshot/Cargo.toml +++ b/libwayshot/Cargo.toml @@ -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.0" diff --git a/libwayshot/src/lib.rs b/libwayshot/src/lib.rs index beda48fe..b6403c53 100644 --- a/libwayshot/src/lib.rs +++ b/libwayshot/src/lib.rs @@ -14,6 +14,7 @@ mod screencopy; use std::{ collections::HashSet, fs::File, + io::Write, os::fd::AsFd, sync::atomic::{AtomicBool, Ordering}, thread, @@ -457,8 +458,9 @@ impl WayshotConnection { )); } }; + let shm = self.globals.bind::(&qh, 1..=1, ())?; - for (frame_copy, frame_guard, output_info) in frames { + for (frame_copy, _frame_guard, output_info) in frames { tracing::span!( tracing::Level::DEBUG, "overlay_frames::surface", @@ -476,11 +478,37 @@ impl WayshotConnection { output_info.wl_output.clone(), ); + let file = tempfile::tempfile().unwrap(); + let image: DynamicImage = frame_copy.try_into().unwrap(); + let image = image::imageops::resize( + &image, + output_info.region.size.width, + output_info.region.size.height, + image::imageops::FilterType::Nearest, + ); + init_overlay(&image, &file); + let pool = shm.create_pool( + file.as_fd(), + (output_info.region.size.width * output_info.region.size.height * 4) as i32, + &qh, + (), + ); + + let buffer = pool.create_buffer( + 0, + output_info.region.size.width as i32, + output_info.region.size.height as i32, + (output_info.region.size.height * 4) as i32, + wl_shm::Format::Argb8888, + &qh, + (), + ); + layer_surface.set_exclusive_zone(-1); layer_surface.set_anchor(Anchor::Top | Anchor::Left); layer_surface.set_size( - frame_copy.frame_format.size.width, - frame_copy.frame_format.size.height, + output_info.region.size.width, + output_info.region.size.height, ); debug!("Committing surface creation changes."); @@ -493,7 +521,7 @@ 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.attach(Some(&buffer), 0, 0); debug!("Committing surface with attached buffer."); surface.commit(); @@ -565,15 +593,15 @@ impl WayshotConnection { thread::scope(|scope| { let rotate_join_handles = frames .into_iter() - .map(|(frame_copy, _, _)| { + .map(|(frame_copy, _, output_info)| { scope.spawn(move || { let image = (&frame_copy).try_into()?; Ok(( image_util::rotate_image_buffer( image, frame_copy.transform, - frame_copy.frame_format.size.width, - frame_copy.frame_format.size.height, + output_info.region.size.width, + output_info.region.size.height, ), frame_copy, )) @@ -675,3 +703,12 @@ impl WayshotConnection { self.screenshot_outputs(self.get_all_outputs(), cursor_overlay) } } + +fn init_overlay(origin_image: &image::ImageBuffer, Vec>, 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(); +}