Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(rendering): stop retaining readback clones for owned frame delivery

Wraient106c64b9757a
diff --git a/crates/export/src/gif.rs b/crates/export/src/gif.rs
index 85669be9e..0a65bab77 100644
--- a/crates/export/src/gif.rs
+++ b/crates/export/src/gif.rs
@@ -186,6 +186,7 @@ impl GifExportSettings {
self.resolution_base,
&base.recordings,
base.sample_windows.clone(),
+ true,
)
.then(|f| async { f.map_err(|v| v.to_string()) });
diff --git a/crates/export/src/mov.rs b/crates/export/src/mov.rs
index c26d17dc3..2e63cacac 100644
--- a/crates/export/src/mov.rs
+++ b/crates/export/src/mov.rs
@@ -161,6 +161,7 @@ impl MovExportSettings {
self.resolution_base,
&base.recordings,
base.sample_windows.clone(),
+ false,
)
.then(|f| async { f.map_err(|v| v.to_string()) });
diff --git a/crates/export/tests/real_recording_regression.rs b/crates/export/tests/real_recording_regression.rs
index b3267eb64..9c1b4b2db 100644
--- a/crates/export/tests/real_recording_regression.rs
+++ b/crates/export/tests/real_recording_regression.rs
@@ -106,6 +106,7 @@ async fn render_sampled_sequence_frames(
resolution_base,
recordings,
None,
+ false,
)
.await
.map_err(std::io::Error::other)?;
diff --git a/crates/rendering/src/frame_pipeline.rs b/crates/rendering/src/frame_pipeline.rs
index def4d9b9e..7660f4dc6 100644
--- a/crates/rendering/src/frame_pipeline.rs
+++ b/crates/rendering/src/frame_pipeline.rs
@@ -1644,6 +1644,7 @@ pub struct PendingReadback {
frame_number: u32,
frame_rate: u32,
export_deadlines: bool,
+ recycle_readback_frames: bool,
}
fn active_readback_byte_len(
@@ -1772,7 +1773,7 @@ impl PendingReadback {
phase.finish("frame_ready");
}
let data = Arc::new(data_vec);
- if self.export_deadlines {
+ if self.export_deadlines && self.recycle_readback_frames {
recycled.keep(&data);
}
Ok(RenderedFrame {
@@ -1827,6 +1828,7 @@ pub struct PipelinedGpuReadback {
pending_resize_size: u64,
readiness_first_output: bool,
export_deadlines: bool,
+ recycle_readback_frames: bool,
}
impl PipelinedGpuReadback {
@@ -1850,6 +1852,7 @@ impl PipelinedGpuReadback {
pending_resize_size: 0,
readiness_first_output: true,
export_deadlines: false,
+ recycle_readback_frames: true,
}
}
@@ -1982,6 +1985,7 @@ impl PipelinedGpuReadback {
frame_number: uniforms.frame_number,
frame_rate: uniforms.frame_rate,
export_deadlines: self.export_deadlines,
+ recycle_readback_frames: self.recycle_readback_frames,
});
Ok(())
@@ -2000,6 +2004,7 @@ pub struct RenderSession {
texture_width: u32,
texture_height: u32,
export_deadlines: bool,
+ recycle_readback_frames: bool,
}
/// The blurred-background cache copies its result into the current session
@@ -2044,6 +2049,7 @@ impl RenderSession {
texture_width: width,
texture_height: height,
export_deadlines: false,
+ recycle_readback_frames: true,
};
phase.finish("returned");
session
@@ -2055,6 +2061,11 @@ impl RenderSession {
self
}
+ pub fn with_recycle_readback_frames(mut self, recycle_readback_frames: bool) -> Self {
+ self.recycle_readback_frames = recycle_readback_frames;
+ self
+ }
+
pub fn update_texture_size(&mut self, device: &wgpu::Device, width: u32, height: u32) {
if self.texture_width == width && self.texture_height == height {
return;
@@ -2223,10 +2234,12 @@ pub async fn finish_encoder_timed(
let initial_buffer_size =
(padded_bytes_per_row(uniforms.output_size) * uniforms.output_size.1) as u64;
let export_deadlines = session.export_deadlines;
+ let recycle_readback_frames = session.recycle_readback_frames;
let readback = session
.pipelined_readback
.get_or_insert_with(|| PipelinedGpuReadback::new(device, initial_buffer_size));
readback.export_deadlines = export_deadlines;
+ readback.recycle_readback_frames = recycle_readback_frames;
let previous = readback.take_pending();
@@ -2448,6 +2461,7 @@ mod readback_output_tests {
frame_number: 7,
frame_rate: 30,
export_deadlines: false,
+ recycle_readback_frames: true,
};
if fail {
readback_allocation_failure::inject(7);
diff --git a/crates/rendering/src/lib.rs b/crates/rendering/src/lib.rs
index 15ddef1bf..d8a0c0bd5 100644
--- a/crates/rendering/src/lib.rs
+++ b/crates/rendering/src/lib.rs
@@ -907,6 +907,7 @@ pub async fn render_video_to_channel(
resolution_base: XY<u32>,
recordings: &ProjectRecordingsMeta,
frame_windows: Option<FrameWindows>,
+ owned_frames: bool,
) -> Result<(), RenderingError> {
ffmpeg::init().unwrap();
validate_timeline(project, render_segments.len())?;
@@ -980,6 +981,9 @@ pub async fn render_video_to_channel(
let mut frame_renderer = FrameRenderer::new(constants);
frame_renderer.use_export_deadlines();
+ if owned_frames {
+ frame_renderer.deliver_owned_frames();
+ }
let mut layers = RendererLayers::new_with_options(
&constants.device,
@@ -6745,6 +6749,7 @@ mod export_order_tests {
XY::new(SIZE, SIZE),
&recordings,
None,
+ false,
);
let collect = async {
let mut order = Vec::new();
@@ -7227,6 +7232,7 @@ pub struct FrameRenderer<'a> {
nv12_buffer_pool: NV12BufferPool,
transition_compositor: Option<TransitionCompositor>,
export_deadlines: bool,
+ owned_frames: bool,
}
impl<'a> FrameRenderer<'a> {
@@ -7245,6 +7251,7 @@ impl<'a> FrameRenderer<'a> {
nv12_buffer_pool: NV12BufferPool::new(6),
transition_compositor: None,
export_deadlines: false,
+ owned_frames: false,
}
}
@@ -7255,6 +7262,13 @@ impl<'a> FrameRenderer<'a> {
self.export_deadlines = true;
}
+ /// Frames are delivered with no other owner: the readback does not keep
+ /// a clone for recycling, so the consumer can take the buffer by value
+ /// instead of copying it out of a shared `Arc`.
+ pub(crate) fn deliver_owned_frames(&mut self) {
+ self.owned_frames = true;
+ }
+
/// NV12 frames come back as IOSurface-backed CVPixelBuffers instead of
/// CPU readbacks, for zero-copy VideoToolbox encoding. Only meaningful on
/// macOS with a hardware adapter; the converter falls back to readback
@@ -7303,6 +7317,7 @@ impl<'a> FrameRenderer<'a> {
let session = self.session.get_or_insert_with(|| {
RenderSession::new(&self.constants.device, width, height)
.with_export_deadlines(self.export_deadlines)
+ .with_recycle_readback_frames(!self.owned_frames)
});
session.update_texture_size(&self.constants.device, width, height);
}
@@ -7351,6 +7366,7 @@ impl<'a> FrameRenderer<'a> {
uniforms.output_size.1,
)
.with_export_deadlines(self.export_deadlines)
+ .with_recycle_readback_frames(!self.owned_frames)
});
session.update_texture_size(
@@ -7429,6 +7445,7 @@ impl<'a> FrameRenderer<'a> {
incoming.uniforms.output_size.1,
)
.with_export_deadlines(self.export_deadlines)
+ .with_recycle_readback_frames(!self.owned_frames)
});
session.update_texture_size(
&self.constants.device,
@@ -7601,6 +7618,7 @@ impl<'a> FrameRenderer<'a> {
uniforms.output_size.1,
)
.with_export_deadlines(self.export_deadlines)
+ .with_recycle_readback_frames(!self.owned_frames)
});
session.update_texture_size(
&self.constants.device,
@@ -7739,6 +7757,7 @@ impl<'a> FrameRenderer<'a> {
incoming.uniforms.output_size.1,
)
.with_export_deadlines(self.export_deadlines)
+ .with_recycle_readback_frames(!self.owned_frames)
});
session.update_texture_size(
&self.constants.device,
@@ -7825,6 +7844,7 @@ impl<'a> FrameRenderer<'a> {
incoming.uniforms.output_size.1,
)
.with_export_deadlines(self.export_deadlines)
+ .with_recycle_readback_frames(!self.owned_frames)
});
session.update_texture_size(
&self.constants.device,
@@ -8018,6 +8038,7 @@ impl<'a> FrameRenderer<'a> {
uniforms.output_size.1,
)
.with_export_deadlines(self.export_deadlines)
+ .with_recycle_readback_frames(!self.owned_frames)
});
session.update_texture_size(
diff --git a/crates/rendering/src/main.rs b/crates/rendering/src/main.rs
index 4918b8da1..c6e3d3480 100644
--- a/crates/rendering/src/main.rs
+++ b/crates/rendering/src/main.rs
@@ -201,6 +201,7 @@ async fn main() -> Result<()> {
XY::new(output_size.0, output_size.1),
&recordings,
None,
+ false,
)
.await
});