Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

perf(export): every frame is drawn from the plan where it lies, and only a treated one is copied

meraxes89167a7331c45a
diff --git a/src/crates/concat-export/src/lib.rs b/src/crates/concat-export/src/lib.rs
index e9ba186e..6f20bffe 100644
--- a/src/crates/concat-export/src/lib.rs
+++ b/src/crates/concat-export/src/lib.rs
@@ -1845,13 +1845,14 @@ fn render_picture(
};
// The plan is kept for a second drawing: a frame drawn on a device
// that died under it is black, and is drawn again on the device
- // that takes over. The copy is cheap - the pictures are shared.
+ // that takes over. Both drawings borrow it - the layers' pictures
+ // were never copied, and now their passes are not either.
let mut composed = composite_treated(
&mut EffectClock {
inner: &mut compositor,
offset: effect_offset,
},
- frame_plan.clone(),
+ &frame_plan,
&treatments,
&transitions,
);
@@ -1870,7 +1871,7 @@ fn render_picture(
inner: &mut compositor,
offset: effect_offset,
},
- frame_plan.clone(),
+ &frame_plan,
&treatments,
&transitions,
);
@@ -1970,10 +1971,13 @@ impl Compositor for EffectClock<'_> {
/// Draws `plan` with every treatment live at its instant applied to the
/// stack beneath its track: each goes into the plan, and the compositor
-/// applies it where it draws.
+/// applies it where it draws. The plan is borrowed: a frame carries its
+/// treatments in a copy of its own only when one is live, which most frames
+/// have none of - the export and the monitor both draw every frame through
+/// here, and the copy is a whole plan's worth of layers and passes.
fn composite_treated(
compositor: &mut dyn Compositor,
- mut plan: FramePlan,
+ plan: &FramePlan,
treatments: &[Treatment],
transitions: &[TransitionSpan],
) -> Frame {
@@ -1998,10 +2002,14 @@ fn composite_treated(
// stacks being combined. Leaving the pairing to the dissolve turned
// every transition under a filter layer into a plain fade.
if let Some(span) = transitions.iter().find(|span| span.covers(time))
- && let Some(frame) = combine_transition(compositor, &plan, span, &planned)
+ && let Some(frame) = combine_transition(compositor, plan, span, &planned)
{
return frame;
}
+ if planned.is_empty() && plan.treatments.is_empty() {
+ return compositor.render(plan);
+ }
+ let mut plan = plan.clone();
plan.treatments = planned;
compositor.render(&plan)
}
@@ -2176,12 +2184,7 @@ impl PreviewSources {
/// The frame, treatments and transitions included, drawn with `compositor`.
pub fn composite(&self, compositor: &mut dyn Compositor) -> Frame {
- composite_treated(
- compositor,
- self.plan.clone(),
- &self.treatments,
- &self.transitions,
- )
+ composite_treated(compositor, &self.plan, &self.treatments, &self.transitions)
}
/// The frame as raw RGBA, drawn on the shared headless compositor: the
@@ -2580,7 +2583,7 @@ mod tests {
};
let out = composite_treated(
&mut compositor,
- stack(Rational::from_int(1)),
+ &stack(Rational::from_int(1)),
std::slice::from_ref(&mono),
&[],
);
@@ -2598,7 +2601,7 @@ mod tests {
strength: 0.5,
..mono.clone()
};
- let out = composite_treated(&mut compositor, stack(Rational::from_int(1)), &[half], &[]);
+ let out = composite_treated(&mut compositor, &stack(Rational::from_int(1)), &[half], &[]);
let pixel = at_of(&out, 7, 7);
assert!(
pixel[0] > pixel[1] + 60 && pixel[1] > grey[1] / 4 && pixel[0] < 255,
@@ -2606,7 +2609,12 @@ mod tests {
);
// Outside its span the treatment does nothing.
- let out = composite_treated(&mut compositor, stack(Rational::from_int(20)), &[mono], &[]);
+ let out = composite_treated(
+ &mut compositor,
+ &stack(Rational::from_int(20)),
+ &[mono],
+ &[],
+ );
assert_eq!(at_of(&out, 7, 7), [255, 0, 0]);
fn at_of(frame: &Frame, x: usize, y: usize) -> [u8; 3] {
let p = &frame.pixels()[(y * 8 + x) * 4..(y * 8 + x) * 4 + 3];
@@ -2668,10 +2676,10 @@ mod tests {
let Some(mut gpu) = WgpuCompositor::new() else {
return; // no renderer here
};
- let pushed = composite_treated(&mut gpu, plan(), &[], std::slice::from_ref(&span));
+ let pushed = composite_treated(&mut gpu, &plan(), &[], std::slice::from_ref(&span));
let graded = composite_treated(
&mut gpu,
- plan(),
+ &plan(),
std::slice::from_ref(&mono),
std::slice::from_ref(&span),
);