Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(recording): window-only capture never falls back to the display, the cursor follows the window on macOS, and trimmed D3D frames stop sharing a texture
Richie McIlroy2d1d32ec86bf
diff --git a/crates/recording/examples/cursor-poll-benchmark.rs b/crates/recording/examples/cursor-poll-benchmark.rsindex b7a6acd70..7786b727d 100644--- a/crates/recording/examples/cursor-poll-benchmark.rs+++ b/crates/recording/examples/cursor-poll-benchmark.rs@@ -52,7 +52,6 @@ fn main() -> anyhow::Result<()> {CursorCaptureTarget {crop_bounds,display,- #[cfg(target_os = "linux")]window: None,},dir.path().join("cursors"),diff --git a/crates/recording/src/cursor.rs b/crates/recording/src/cursor.rsindex f6a1f5f15..a12e0c152 100644--- a/crates/recording/src/cursor.rs+++ b/crates/recording/src/cursor.rs@@ -51,10 +51,29 @@ pub struct IncrementalCaptureOutputs {pub struct CursorCaptureTarget {pub crop_bounds: CursorCropBounds,pub display: scap_targets::Display,- #[cfg(any(target_os = "linux", windows))]pub window: Option<scap_targets::WindowId>,}+#[cfg(target_os = "macos")]+fn macos_window_crop(+ window: &scap_targets::WindowId,+ display: &scap_targets::Display,+) -> Option<CursorCropBounds> {+ use scap_targets::bounds::{LogicalBounds, LogicalPosition};++ let window_bounds = scap_targets::Window::from_id_direct(window)?+ .raw_handle()+ .logical_bounds()?;+ let display_position = display.raw_handle().logical_position();+ Some(CursorCropBounds::new_macos(LogicalBounds::new(+ LogicalPosition::new(+ window_bounds.position().x() - display_position.x(),+ window_bounds.position().y() - display_position.y(),+ ),+ window_bounds.size(),+ )))+}+#[cfg(windows)]fn windows_window_crop(window: &scap_targets::WindowId,@@ -688,7 +707,7 @@ pub fn spawn_cursor_recorder(});#[cfg(target_os = "linux")]let mut last_window_position = None;- #[cfg(windows)]+ #[cfg(any(windows, target_os = "macos"))]let mut last_window_crop: Option<CursorCropBounds> = None;let device_state = DeviceState::new();// Reading the held keys asks the OS about every key it knows (two@@ -777,16 +796,21 @@ pub fn spawn_cursor_recorder(.window.as_ref().and_then(|window| windows_window_crop(window, &display));- #[cfg(windows)]+ #[cfg(target_os = "macos")]+ let window_crop = target+ .window+ .as_ref()+ .and_then(|window| macos_window_crop(window, &display));+ #[cfg(any(windows, target_os = "macos"))]let position_changed =position_changed || (window_crop.is_some() && window_crop != last_window_crop);- #[cfg(windows)]+ #[cfg(any(windows, target_os = "macos"))]{if window_crop.is_some() {last_window_crop = window_crop;}}- #[cfg(windows)]+ #[cfg(any(windows, target_os = "macos"))]let crop_bounds = last_window_crop.unwrap_or(crop_bounds);// The cursor image only labels this tick's move and click events,diff --git a/crates/recording/src/sources/screen_capture/macos.rs b/crates/recording/src/sources/screen_capture/macos.rsindex ad4cd3018..5933d9798 100644--- a/crates/recording/src/sources/screen_capture/macos.rs+++ b/crates/recording/src/sources/screen_capture/macos.rs@@ -64,7 +64,7 @@ fn window_content_filter(warn!(%window_id,capture_phase,- "Capture window missing from ScreenCaptureKit shareable content, falling back to display crop"+ "Capture window missing from ScreenCaptureKit shareable content");return None;};@@ -222,6 +222,10 @@ pub enum SourceError {NoDisplay(DisplayId),#[error("AsContentFilter")]AsContentFilter,+ /// A window-only capture whose window is gone. A display crop would+ /// record whatever now covers the window's rectangle.+ #[error("WindowGone: Id '{0}'")]+ WindowGone(WindowId),}pub struct VideoFrame {@@ -264,7 +268,11 @@ impl ScreenCaptureConfig<CMSampleBufferCapture> {.ok_or_else(|| SourceError::NoDisplay(self.config.display.clone()))?;let window_filter = capturable_window(&self.config, &self.excluded_windows, "initial")- .and_then(|id| window_content_filter(id, &self.shareable_content, "initial"));+ .map(|id| {+ window_content_filter(id, &self.shareable_content, "initial")+ .ok_or_else(|| SourceError::WindowGone(id.clone()))+ })+ .transpose()?;let captures_window = window_filter.is_some();let content_filter = match window_filter {@@ -1005,7 +1013,12 @@ async fn rebuild_capturer(params: &CapturerRebuildParams) -> anyhow::Result<Capt.ok_or_else(|| anyhow!("Display not found during restart: {:?}", params.display_id))?;let window_filter = capturable_window(¶ms.config, ¶ms.excluded_windows, "restart")- .and_then(|id| window_content_filter(id, &shareable_content, "restart"));+ .map(|id| {+ window_content_filter(id, &shareable_content, "restart").ok_or_else(|| {+ anyhow!("Capture window {id} is gone; not falling back to the display")+ })+ })+ .transpose()?;let captures_window = window_filter.is_some();let content_filter = match window_filter {diff --git a/crates/recording/src/studio_recording.rs b/crates/recording/src/studio_recording.rsindex 4967455f6..c868f574c 100644--- a/crates/recording/src/studio_recording.rs+++ b/crates/recording/src/studio_recording.rs@@ -3667,7 +3667,7 @@ async fn create_segment_pipeline(display: cursor_display,#[cfg(target_os = "linux")]window: base_inputs.capture_target.window(),- #[cfg(windows)]+ #[cfg(not(target_os = "linux"))]window: crate::window_only_target(&base_inputs.capture_target,base_inputs.window_only,diff --git a/crates/scap-direct3d/src/lib.rs b/crates/scap-direct3d/src/lib.rsindex 736f9c7ca..92e37f9d3 100644--- a/crates/scap-direct3d/src/lib.rs+++ b/crates/scap-direct3d/src/lib.rs@@ -355,6 +355,50 @@ fn create_copy_texture(texture.ok_or_else(windows::core::Error::from_win32)}+/// Textures for frames trimmed down from their surface. A frame hands its+/// texture back when it is dropped, so a texture is only written again once+/// no queued frame still reads it.+#[derive(Default)]+struct TrimPool {+ free: Mutex<Vec<(ID3D11Texture2D, u32, u32)>>,+}++/// Enough for frames waiting on the encoder; more are dropped when returned.+const TRIM_POOL_SIZE: usize = 8;++impl TrimPool {+ fn take(+ &self,+ d3d_device: &ID3D11Device,+ width: u32,+ height: u32,+ pixel_format: PixelFormat,+ ) -> windows::core::Result<ID3D11Texture2D> {+ let reused = {+ let mut free = self+ .free+ .lock()+ .unwrap_or_else(|poisoned| poisoned.into_inner());+ free.retain(|(_, w, h)| (*w, *h) == (width, height));+ free.pop()+ };+ match reused {+ Some((texture, _, _)) => Ok(texture),+ None => create_copy_texture(d3d_device, width, height, pixel_format),+ }+ }++ fn give(&self, texture: ID3D11Texture2D, width: u32, height: u32) {+ let mut free = self+ .free+ .lock()+ .unwrap_or_else(|poisoned| poisoned.into_inner());+ if free.len() < TRIM_POOL_SIZE {+ free.push((texture, width, height));+ }+ }+}+#[derive(Clone, Default, Debug)]pub struct Settings {pub is_border_required: Option<bool>,@@ -590,11 +634,10 @@ impl Capturer {}).transpose()?;- // Allocated on the first frame whose size differs from its surface- // (odd-sized, or smaller than the pool's surface) and again whenever- // that size changes, so a capture that matches its surface never pays- // for the copy.- let trim = Mutex::new(None::<(ID3D11Texture2D, u32, u32)>);+ // Only used by frames whose size differs from their surface (odd-sized,+ // or smaller than the pool's surface), so a capture that matches its+ // surface never pays for the copy.+ let trim = Arc::new(TrimPool::default());let frame_arrived_token = frame_pool.FrameArrived(@@ -729,6 +772,7 @@ impl Capturer {d3d_context: d3d_context.clone(),d3d_device: d3d_device.clone(),staging_pool: staging_pool.clone(),+ recycle: None,}} else {// The frame is the content bounded by its surface,@@ -759,28 +803,11 @@ impl Capturer {d3d_context: d3d_context.clone(),d3d_device: d3d_device.clone(),staging_pool: staging_pool.clone(),+ recycle: None,}} else {- let trimmed_texture = {- let mut trim = trim- .lock()- .unwrap_or_else(|poisoned| poisoned.into_inner());- match trim.as_ref() {- Some((texture, w, h)) if (*w, *h) == (width, height) => {- texture.clone()- }- _ => {- let texture = create_copy_texture(- &d3d_device,- width,- height,- pixel_format,- )?;- *trim = Some((texture.clone(), width, height));- texture- }- }- };+ let trimmed_texture =+ trim.take(&d3d_device, width, height, pixel_format)?;unsafe {d3d_context.CopySubresourceRegion(@@ -811,6 +838,7 @@ impl Capturer {d3d_context: d3d_context.clone(),d3d_device: d3d_device.clone(),staging_pool: staging_pool.clone(),+ recycle: Some(trim.clone()),}}};@@ -911,6 +939,16 @@ pub struct Frame {d3d_device: ID3D11Device,d3d_context: ID3D11DeviceContext,staging_pool: Arc<StagingTexturePool>,+ /// Where a trimmed frame's texture goes back to once the frame is done.+ recycle: Option<Arc<TrimPool>>,+}++impl Drop for Frame {+ fn drop(&mut self) {+ if let Some(pool) = self.recycle.take() {+ pool.give(self.texture.clone(), self.width, self.height);+ }+ }}impl std::fmt::Debug for Frame {diff --git a/crates/scap-targets/src/lib.rs b/crates/scap-targets/src/lib.rsindex 0ffd4fcc6..1d30106be 100644--- a/crates/scap-targets/src/lib.rs+++ b/crates/scap-targets/src/lib.rs@@ -151,7 +151,7 @@ impl Window {/// searching `list()`. Prefer it when the window was already chosen and/// only needs to still exist: enumeration drops minimized windows, so/// `from_id` reports a minimized target as gone.- #[cfg(windows)]+ #[cfg(any(windows, target_os = "macos"))]pub fn from_id_direct(id: &WindowId) -> Option<Self> {WindowImpl::from_id_direct(&id.0).map(Self)}diff --git a/crates/scap-targets/src/platform/macos.rs b/crates/scap-targets/src/platform/macos.rsindex a31e5a992..77e2202a2 100644--- a/crates/scap-targets/src/platform/macos.rs+++ b/crates/scap-targets/src/platform/macos.rs@@ -349,6 +349,12 @@ impl WindowImpl {windows_with_level.first().map(|(window, _)| *window)}+ /// The window `id`, without enumerating every window. Whether it still+ /// exists shows in what its getters return.+ pub fn from_id_direct(id: &WindowIdImpl) -> Option<Self> {+ Some(Self(id.0))+ }+pub fn id(&self) -> WindowIdImpl {WindowIdImpl(self.0)}