Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
perf(recording): pool the CPU frames Windows screen capture copies into
Marcos Daniel Rivera Gonzaleza4dae84fb775
diff --git a/crates/scap-ffmpeg/src/direct3d.rs b/crates/scap-ffmpeg/src/direct3d.rsindex fcfdd34fa..762af9c58 100644--- a/crates/scap-ffmpeg/src/direct3d.rs+++ b/crates/scap-ffmpeg/src/direct3d.rs@@ -46,6 +46,65 @@ fn copy_frame_data(}}+struct FramePool {+ key: (Pixel, u32, u32),+ linesize: usize,+ pool: *mut ffmpeg::ffi::AVBufferPool,+}++impl Drop for FramePool {+ fn drop(&mut self) {+ // Frees the pool once every buffer still referenced by an in-flight frame is returned.+ unsafe { ffmpeg::ffi::av_buffer_pool_uninit(&mut self.pool) };+ }+}++thread_local! {+ static FRAME_POOL: std::cell::RefCell<Option<FramePool>> = const { std::cell::RefCell::new(None) };+}++// A fresh multi-megabyte allocation per frame is served by fresh OS pages on Windows, so every+// capture paid page-fault and zeroing costs several times the copy itself (~2ms per 1080p frame).+fn pooled_frame(format: Pixel, width: u32, height: u32) -> ffmpeg::frame::Video {+ debug_assert!(matches!(format, Pixel::RGBA | Pixel::BGRA));+ FRAME_POOL.with_borrow_mut(|slot| {+ if slot+ .as_ref()+ .is_none_or(|pool| pool.key != (format, width, height))+ {+ *slot = None;+ let linesize = (width as usize).next_multiple_of(32) * 4;+ let size =+ linesize * height as usize + ffmpeg::ffi::AV_INPUT_BUFFER_PADDING_SIZE as usize;+ let pool = unsafe { ffmpeg::ffi::av_buffer_pool_init(size, None) };+ if pool.is_null() {+ return ffmpeg::frame::Video::new(format, width, height);+ }+ *slot = Some(FramePool {+ key: (format, width, height),+ linesize,+ pool,+ });+ }+ let pool = slot.as_ref().expect("frame pool initialized above");+ unsafe {+ let buffer = ffmpeg::ffi::av_buffer_pool_get(pool.pool);+ if buffer.is_null() {+ return ffmpeg::frame::Video::new(format, width, height);+ }+ let mut frame = ffmpeg::frame::Video::empty();+ frame.set_format(format);+ frame.set_width(width);+ frame.set_height(height);+ let raw = frame.as_mut_ptr();+ (*raw).buf[0] = buffer;+ (*raw).data[0] = (*buffer).data;+ (*raw).linesize[0] = pool.linesize as i32;+ frame+ }+ })+}+impl super::AsFFmpeg for scap_direct3d::Frame {fn as_ffmpeg(&self) -> Result<ffmpeg::frame::Video, AsFFmpegError> {let buffer = self.as_buffer()?;@@ -59,11 +118,7 @@ impl super::AsFFmpeg for scap_direct3d::Frame {match self.pixel_format() {PixelFormat::R8G8B8A8Unorm => {- let mut ff_frame = ffmpeg::frame::Video::new(- ffmpeg::format::Pixel::RGBA,- self.width(),- self.height(),- );+ let mut ff_frame = pooled_frame(Pixel::RGBA, self.width(), self.height());let dest_stride = ff_frame.stride(0);let dest_bytes = ff_frame.data_mut(0);@@ -80,11 +135,7 @@ impl super::AsFFmpeg for scap_direct3d::Frame {Ok(ff_frame)}PixelFormat::B8G8R8A8Unorm => {- let mut ff_frame = ffmpeg::frame::Video::new(- ffmpeg::format::Pixel::BGRA,- self.width(),- self.height(),- );+ let mut ff_frame = pooled_frame(Pixel::BGRA, self.width(), self.height());let dest_stride = ff_frame.stride(0);let dest_bytes = ff_frame.data_mut(0);@@ -116,3 +167,31 @@ impl PixelFormatExt for PixelFormat {}}}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn pooled_frames_recycle_released_buffers() {+ let first = pooled_frame(Pixel::BGRA, 1001, 3);+ assert_eq!(+ (first.format(), first.width(), first.height()),+ (Pixel::BGRA, 1001, 3)+ );+ assert!(first.stride(0) >= 1001 * 4);+ assert!(first.data(0).len() >= first.stride(0) * 3);+ let held = first.data(0).as_ptr();++ let second = pooled_frame(Pixel::BGRA, 1001, 3);+ assert_ne!(second.data(0).as_ptr(), held);++ drop(first);+ let third = pooled_frame(Pixel::BGRA, 1001, 3);+ assert_eq!(third.data(0).as_ptr(), held);++ let resized = pooled_frame(Pixel::BGRA, 640, 2);+ assert_eq!((resized.width(), resized.height()), (640, 2));+ drop((second, third));+ }+}