Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(recording): reuse the Windows cursor image while its handle is unchanged

Marcos Daniel Rivera Gonzalez75da5484c57d
diff --git a/crates/recording/src/cursor.rs b/crates/recording/src/cursor.rs
index 8dba97665..edfd17639 100644
--- a/crates/recording/src/cursor.rs
+++ b/crates/recording/src/cursor.rs
@@ -573,7 +573,7 @@ pub fn spawn_cursor_recorder(
}
}
-#[derive(Debug)]
+#[derive(Debug, Clone)]
struct CursorData {
image: Vec<u8>,
hotspot: XY<f64>,
@@ -798,6 +798,23 @@ fn get_cursor_data() -> Option<CursorData> {
return None;
}
+ // Rendering, trimming and PNG-encoding the cursor every tick only to hash it is wasted
+ // while the shape is unchanged. Keyed on the handle, so a cursor scheme change that keeps
+ // the system cursor handles shows the old image until the shape changes; key on the
+ // bitmap contents instead if that ever matters.
+ thread_local! {
+ static LAST_CURSOR: std::cell::RefCell<Option<(usize, CursorData)>> =
+ const { std::cell::RefCell::new(None) };
+ }
+ let handle = cursor_info.hCursor.0 as usize;
+ if let Some(cached) = LAST_CURSOR.with_borrow(|last| {
+ last.as_ref()
+ .filter(|(last_handle, _)| *last_handle == handle)
+ .map(|(_, data)| data.clone())
+ }) {
+ return Some(cached);
+ }
+
// Get icon info
let mut icon_info = ICONINFO::default();
if GetIconInfo(cursor_info.hCursor.into(), &mut icon_info).is_err() {
@@ -1071,11 +1088,13 @@ fn get_cursor_data() -> Option<CursorData> {
)
.ok()?;
- Some(CursorData {
+ let data = CursorData {
image: png_data,
hotspot: XY::new(hotspot_x, hotspot_y),
shape: CursorShape::try_from(&cursor_info.hCursor).ok(),
- })
+ };
+ LAST_CURSOR.set(Some((handle, data.clone())));
+ Some(data)
}
}