Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(enc-gif): hand frames to gifski zero-copy via strided ImgVec

Wraientb6efa12d7964
diff --git a/crates/enc-gif/examples/frame-ingest-benchmark.rs b/crates/enc-gif/examples/frame-ingest-benchmark.rs
new file mode 100644
index 000000000..4e914226e
--- /dev/null
+++ b/crates/enc-gif/examples/frame-ingest-benchmark.rs
@@ -0,0 +1,131 @@
+//! Measures the per-frame ingest preparation in cap-enc-gif: turning a padded
+//! RGBA frame buffer (256-byte-aligned rows, as produced by GPU readback) into
+//! the `ImgVec` handed to gifski, via the slice-based preparation that copies
+//! the frame and via the owned strided preparation that reinterprets it. Both
+//! preparations live in this file so they share compiler flags and inputs, and
+//! their outputs are asserted equal before timing.
+//!
+//! Run with: `cargo run --release -p cap-enc-gif --example frame-ingest-benchmark`
+
+use imgref::ImgVec;
+use rgb::RGBA8;
+use std::{hint::black_box, time::Instant};
+
+fn main() {
+ println!("workload,sample,before_ns,after_ns");
+ for (name, width, height) in [
+ ("768p-padded", 1366usize, 768usize),
+ ("1080p", 1920, 1080),
+ ("4k", 3840, 2160),
+ ] {
+ let bytes_per_row = (width * 4).next_multiple_of(256);
+ let src: Vec<u8> = (0..bytes_per_row * height).map(|i| i as u8).collect();
+
+ let before = baseline::prepare(&src, width, height, bytes_per_row);
+ let after = candidate::prepare(src.clone(), width, height, bytes_per_row);
+ assert_images_equal(&before, &after, width);
+
+ for sample in 0..35 {
+ let run_before = |src: &[u8]| {
+ let start = Instant::now();
+ let img = baseline::prepare(
+ black_box(src),
+ black_box(width),
+ black_box(height),
+ black_box(bytes_per_row),
+ );
+ let elapsed = start.elapsed().as_nanos();
+ black_box(&img);
+ elapsed
+ };
+ let run_after = |src: &[u8]| {
+ let owned = src.to_vec();
+ let start = Instant::now();
+ let img = candidate::prepare(
+ black_box(owned),
+ black_box(width),
+ black_box(height),
+ black_box(bytes_per_row),
+ );
+ let elapsed = start.elapsed().as_nanos();
+ black_box(&img);
+ elapsed
+ };
+ let (old, new) = if sample % 2 == 0 {
+ let old = run_before(&src);
+ (old, run_after(&src))
+ } else {
+ let new = run_after(&src);
+ (run_before(&src), new)
+ };
+ if sample >= 5 {
+ println!("{name},{},{old},{new}", sample - 5);
+ }
+ }
+ }
+}
+
+fn assert_images_equal(before: &ImgVec<RGBA8>, after: &ImgVec<RGBA8>, width: usize) {
+ assert_eq!(before.width(), after.width());
+ assert_eq!(before.height(), after.height());
+ for (before_row, after_row) in before.rows().zip(after.rows()) {
+ assert_eq!(&before_row[..width], &after_row[..width]);
+ }
+}
+
+mod baseline {
+ use imgref::ImgVec;
+ use rgb::RGBA8;
+
+ pub fn prepare(
+ frame_data: &[u8],
+ width: usize,
+ height: usize,
+ bytes_per_row: usize,
+ ) -> ImgVec<RGBA8> {
+ let expected_bytes_per_row = width * 4;
+ if bytes_per_row == expected_bytes_per_row {
+ let pixel_count = width * height;
+ let byte_slice = &frame_data[..pixel_count * 4];
+ let pixels: &[RGBA8] = unsafe {
+ std::slice::from_raw_parts(byte_slice.as_ptr().cast::<RGBA8>(), pixel_count)
+ };
+ imgref::Img::new(pixels.to_vec(), width, height)
+ } else {
+ let mut rgba_pixels = Vec::with_capacity(width * height);
+ for y in 0..height {
+ let row_start = y * bytes_per_row;
+ let row_bytes = &frame_data[row_start..row_start + expected_bytes_per_row];
+ let row_pixels: &[RGBA8] = unsafe {
+ std::slice::from_raw_parts(row_bytes.as_ptr().cast::<RGBA8>(), width)
+ };
+ rgba_pixels.extend_from_slice(row_pixels);
+ }
+ imgref::Img::new(rgba_pixels, width, height)
+ }
+ }
+}
+
+mod candidate {
+ use imgref::ImgVec;
+ use rgb::RGBA8;
+
+ pub fn prepare(
+ frame_data: Vec<u8>,
+ width: usize,
+ height: usize,
+ bytes_per_row: usize,
+ ) -> ImgVec<RGBA8> {
+ let mut frame_data = frame_data;
+ let (ptr, len, capacity) = (
+ frame_data.as_mut_ptr(),
+ frame_data.len(),
+ frame_data.capacity(),
+ );
+ std::mem::forget(frame_data);
+ // SAFETY: RGBA8 is #[repr(C)] with size 4 and alignment 1, and the
+ // caller guarantees the byte length and capacity are multiples of 4.
+ let pixels = unsafe { Vec::from_raw_parts(ptr.cast::<RGBA8>(), len / 4, capacity / 4) };
+ imgref::Img::new_stride(pixels, width, height, bytes_per_row / 4)
+ }
+}
diff --git a/crates/enc-gif/src/lib.rs b/crates/enc-gif/src/lib.rs
index 005738434..932305f4c 100644
--- a/crates/enc-gif/src/lib.rs
+++ b/crates/enc-gif/src/lib.rs
@@ -108,21 +108,12 @@ impl GifEncoderWrapper {
return Err(GifEncodingError::EncoderFinished);
}
- let collector = self
- .collector
- .as_mut()
- .ok_or(GifEncodingError::EncoderFinished)?;
+ self.validate_frame(frame_data.len(), bytes_per_row)?;
let w = self.width as usize;
let h = self.height as usize;
let expected_bytes_per_row = w * 4;
- if bytes_per_row < expected_bytes_per_row
- || frame_data.len() < bytes_per_row * h.saturating_sub(1) + expected_bytes_per_row
- {
- return Err(GifEncodingError::InvalidFrameData);
- }
-
let img = if bytes_per_row == expected_bytes_per_row {
let pixel_count = w * h;
let byte_slice = &frame_data[..pixel_count * 4];
@@ -142,6 +133,66 @@ impl GifEncoderWrapper {
imgref::Img::new(rgba_pixels, w, h)
};
+ self.submit_frame(img)
+ }
+
+ pub fn add_frame_owned(
+ &mut self,
+ frame_data: Vec<u8>,
+ bytes_per_row: usize,
+ ) -> Result<(), GifEncodingError> {
+ if self.finished {
+ return Err(GifEncodingError::EncoderFinished);
+ }
+
+ self.validate_frame(frame_data.len(), bytes_per_row)?;
+
+ if bytes_per_row % 4 != 0 || frame_data.len() % 4 != 0 || frame_data.capacity() % 4 != 0 {
+ return self.add_frame(&frame_data, bytes_per_row);
+ }
+
+ let w = self.width as usize;
+ let h = self.height as usize;
+
+ let mut frame_data = frame_data;
+ let (ptr, len, capacity) = (
+ frame_data.as_mut_ptr(),
+ frame_data.len(),
+ frame_data.capacity(),
+ );
+ std::mem::forget(frame_data);
+ // SAFETY: RGBA8 is #[repr(C)] with size 4 and alignment 1, the checks
+ // above guarantee the byte length and capacity are multiples of 4, and
+ // validate_frame guarantees the buffer covers every row at this stride.
+ let pixels = unsafe { Vec::from_raw_parts(ptr.cast::<RGBA8>(), len / 4, capacity / 4) };
+
+ self.submit_frame(imgref::Img::new_stride(pixels, w, h, bytes_per_row / 4))
+ }
+
+ fn validate_frame(
+ &self,
+ frame_len: usize,
+ bytes_per_row: usize,
+ ) -> Result<(), GifEncodingError> {
+ let w = self.width as usize;
+ let h = self.height as usize;
+ let expected_bytes_per_row = w * 4;
+
+ if bytes_per_row < expected_bytes_per_row
+ || frame_len < bytes_per_row * h.saturating_sub(1) + expected_bytes_per_row
+ {
+ return Err(GifEncodingError::InvalidFrameData);
+ }
+
+ Ok(())
+ }
+
+ fn submit_frame(&mut self, img: imgref::ImgVec<RGBA8>) -> Result<(), GifEncodingError> {
+ let collector = self
+ .collector
+ .as_mut()
+ .ok_or(GifEncodingError::EncoderFinished)?;
+
let pts = (self.frame_index as f64) / (self.fps as f64);
collector
@@ -418,4 +469,70 @@ mod tests {
assert!(output.first_error.is_none());
assert_eq!(output.bytes, expected);
}
+
+ fn patterned_frame(width: usize, height: usize, bytes_per_row: usize) -> Vec<u8> {
+ let mut frame = vec![0xEE; bytes_per_row * height];
+ let mut hash = 7919u32;
+ for y in 0..height {
+ for x in 0..width {
+ hash ^= hash << 13;
+ hash ^= hash >> 17;
+ hash ^= hash << 5;
+ let value = hash % 64;
+ let start = y * bytes_per_row + x * 4;
+ frame[start] = ((value & 3) * 85) as u8;
+ frame[start + 1] = (((value >> 2) & 3) * 85) as u8;
+ frame[start + 2] = (((value >> 4) & 3) * 85) as u8;
+ frame[start + 3] = 255;
+ }
+ }
+ frame
+ }
+
+ fn encode_via_wrapper(owned: bool, bytes_per_row: usize) -> Vec<u8> {
+ let (width, height) = (100usize, 77usize);
+ let path = std::env::temp_dir().join(format!(
+ "cap-enc-gif-test-{}-{owned}-{bytes_per_row}.gif",
+ std::process::id()
+ ));
+ let mut encoder = GifEncoderWrapper::new(&path, width as u32, height as u32, 30).unwrap();
+ for _ in 0..3 {
+ let frame = patterned_frame(width, height, bytes_per_row);
+ if owned {
+ encoder.add_frame_owned(frame, bytes_per_row).unwrap();
+ } else {
+ encoder.add_frame(&frame, bytes_per_row).unwrap();
+ }
+ }
+ encoder.finish().unwrap();
+ let bytes = std::fs::read(&path).unwrap();
+ std::fs::remove_file(&path).unwrap();
+ bytes
+ }
+
+ #[test]
+ fn owned_frames_match_slice_frames_for_padded_rows() {
+ let bytes_per_row = (100 * 4usize).next_multiple_of(256);
+ assert_eq!(
+ encode_via_wrapper(true, bytes_per_row),
+ encode_via_wrapper(false, bytes_per_row)
+ );
+ }
+
+ #[test]
+ fn owned_frames_match_slice_frames_for_tight_rows() {
+ assert_eq!(
+ encode_via_wrapper(true, 100 * 4),
+ encode_via_wrapper(false, 100 * 4)
+ );
+ }
+
+ #[test]
+ fn owned_frames_match_slice_frames_for_unaligned_stride() {
+ let bytes_per_row = 100 * 4 + 2;
+ assert_eq!(
+ encode_via_wrapper(true, bytes_per_row),
+ encode_via_wrapper(false, bytes_per_row)
+ );
+ }
}
diff --git a/crates/export/src/gif.rs b/crates/export/src/gif.rs
index 27439e6c2..85669be9e 100644
--- a/crates/export/src/gif.rs
+++ b/crates/export/src/gif.rs
@@ -3,7 +3,7 @@ use cap_rendering::{ProjectUniforms, RenderSegment, RenderedFrame};
use futures::FutureExt;
use serde::{Deserialize, Serialize};
use specta::Type;
-use std::path::PathBuf;
+use std::{path::PathBuf, sync::Arc};
use tracing::trace;
use crate::{ExportError, ExporterBase, prepare_new_output};
@@ -130,9 +130,15 @@ impl GifExportSettings {
return Err(ExportError::Other("Export cancelled".to_string()));
}
- if let Err(e) =
- gif_encoder.add_frame(&frame.data, frame.padded_bytes_per_row as usize)
- {
+ let add_result = match Arc::try_unwrap(frame.data) {
+ Ok(data) => {
+ gif_encoder.add_frame_owned(data, frame.padded_bytes_per_row as usize)
+ }
+ Err(shared) => {
+ gif_encoder.add_frame(&shared, frame.padded_bytes_per_row as usize)
+ }
+ };
+ if let Err(e) = add_result {
return Err(ExportError::Other(format!(
"Failed to add frame to GIF: {e}"
)));