From 99af18b6ed6e449df0b28777717fcdac31307170 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kostrubiec?= Date: Sat, 5 Oct 2024 20:57:17 +0200 Subject: [PATCH 1/2] Added a multithreading test for Wayland. Fixed a bug which caused screnshots to fail when two threads tried to take them at once. --- src/linux/wayland_capture.rs | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/linux/wayland_capture.rs b/src/linux/wayland_capture.rs index 7522fa3..4c98a66 100644 --- a/src/linux/wayland_capture.rs +++ b/src/linux/wayland_capture.rs @@ -43,7 +43,7 @@ impl SignalArgs for OrgFreedesktopPortalRequestResponse { const NAME: &'static str = "Response"; const INTERFACE: &'static str = "org.freedesktop.portal.Request"; } - +static DBUS_LOCK: Mutex<()> = Mutex::new(()); fn org_gnome_shell_screenshot( conn: &Connection, x: i32, @@ -174,7 +174,31 @@ pub fn wayland_capture(impl_monitor: &ImplMonitor) -> XCapResult { let height = ((impl_monitor.height as f32) * impl_monitor.scale_factor) as i32; let conn = Connection::new_session()?; - - org_gnome_shell_screenshot(&conn, x, y, width, height) - .or_else(|_| org_freedesktop_portal_screenshot(&conn, x, y, width, height)) + let lock = DBUS_LOCK.lock(); + let res = org_gnome_shell_screenshot(&conn, x, y, width, height) + .or_else(|_| org_freedesktop_portal_screenshot(&conn, x, y, width, height)); + drop(lock); + res +} +#[test] +fn screnshot_multithreaded() { + fn make_screenshots() { + let monitors = crate::monitor::Monitor::all().unwrap(); + for monitor in monitors { + monitor.capture_image().unwrap(); + } + } + // Try making screenshots in paralel. If this times out, then this means that there is a threading issue. + const PARALELISM: usize = 1; + let handles: Vec<_> = (0..PARALELISM) + .map(|_| { + std::thread::spawn(|| { + make_screenshots(); + }) + }) + .collect(); + make_screenshots(); + handles + .into_iter() + .for_each(|handle| handle.join().unwrap()); } From 05186b191d800cd5f8d31b4723fca9b263eb5695 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kostrubiec?= Date: Mon, 7 Oct 2024 22:27:57 +0200 Subject: [PATCH 2/2] Fixed a segfault in lbdbus modified: Cargo.toml modified: src/linux/wayland_capture.rs --- Cargo.toml | 2 +- src/linux/wayland_capture.rs | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b32a718..94fabdd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,7 @@ windows = { version = "0.58", features = [ [target.'cfg(target_os="linux")'.dependencies] percent-encoding = "2.3" xcb = { version = "1.4", features = ["randr"] } -dbus = { version = "0.9", features = ["vendored"] } +dbus = { version = "0.9.7" } [dev-dependencies] fs_extra = "1.3" diff --git a/src/linux/wayland_capture.rs b/src/linux/wayland_capture.rs index 4c98a66..6b5823c 100644 --- a/src/linux/wayland_capture.rs +++ b/src/linux/wayland_capture.rs @@ -172,9 +172,8 @@ pub fn wayland_capture(impl_monitor: &ImplMonitor) -> XCapResult { let y = ((impl_monitor.y as f32) * impl_monitor.scale_factor) as i32; let width = ((impl_monitor.width as f32) * impl_monitor.scale_factor) as i32; let height = ((impl_monitor.height as f32) * impl_monitor.scale_factor) as i32; - - let conn = Connection::new_session()?; let lock = DBUS_LOCK.lock(); + let conn = Connection::new_session()?; let res = org_gnome_shell_screenshot(&conn, x, y, width, height) .or_else(|_| org_freedesktop_portal_screenshot(&conn, x, y, width, height)); drop(lock); @@ -189,7 +188,7 @@ fn screnshot_multithreaded() { } } // Try making screenshots in paralel. If this times out, then this means that there is a threading issue. - const PARALELISM: usize = 1; + const PARALELISM: usize = 10; let handles: Vec<_> = (0..PARALELISM) .map(|_| { std::thread::spawn(|| {