Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
perf(rendering): precompute cursor timeline and zoom focus upfront for export
Cursor Agent91e95014d7b1
diff --git a/crates/export/src/mp4.rs b/crates/export/src/mp4.rsindex b9a6bd1a9..2847f0762 100644--- a/crates/export/src/mp4.rs+++ b/crates/export/src/mp4.rs@@ -578,7 +578,7 @@ async fn export_render_to_channel(mut on_progress: impl FnMut(u32) -> bool + Send + 'static,project_path: PathBuf,) -> Result<(), cap_rendering::RenderingError> {- let (tx_image_data, mut video_rx) = tokio::sync::mpsc::channel::<(Nv12RenderedFrame, u32)>(4);+ let (tx_image_data, mut video_rx) = tokio::sync::mpsc::channel::<(Nv12RenderedFrame, u32)>(8);let screenshot_project_path = project_path;diff --git a/crates/rendering/src/cursor_interpolation.rs b/crates/rendering/src/cursor_interpolation.rsindex ad0dec8d8..e68d118d4 100644--- a/crates/rendering/src/cursor_interpolation.rs+++ b/crates/rendering/src/cursor_interpolation.rs@@ -236,47 +236,105 @@ pub fn interpolate_cursor_with_click_spring();interpolate_timeline(&timeline, time_ms)} else {- if cursor.moves[0].time_ms > time_ms {- let event = &cursor.moves[0];- return Some(InterpolatedCursorPosition {- position: Coord::new(XY {- x: event.x,- y: event.y,- }),- velocity: XY::new(0.0, 0.0),- cursor_id: event.cursor_id.clone(),- });+ interpolate_raw_cursor(cursor, time_ms)+ }+}++fn interpolate_raw_cursor(+ cursor: &CursorEvents,+ time_ms: f64,+) -> Option<InterpolatedCursorPosition> {+ if cursor.moves.is_empty() {+ return None;+ }++ if cursor.moves[0].time_ms > time_ms {+ let event = &cursor.moves[0];+ return Some(InterpolatedCursorPosition {+ position: Coord::new(XY {+ x: event.x,+ y: event.y,+ }),+ velocity: XY::new(0.0, 0.0),+ cursor_id: event.cursor_id.clone(),+ });+ }++ if let Some(event) = cursor.moves.last()+ && event.time_ms <= time_ms+ {+ return Some(InterpolatedCursorPosition {+ position: Coord::new(XY {+ x: event.x,+ y: event.y,+ }),+ velocity: XY::new(0.0, 0.0),+ cursor_id: event.cursor_id.clone(),+ });+ }++ cursor.moves.windows(2).find_map(|chunk| {+ if time_ms >= chunk[0].time_ms && time_ms < chunk[1].time_ms {+ let c = &chunk[0];+ let next = &chunk[1];+ let delta_ms = (next.time_ms - c.time_ms) as f32;+ let dt = (delta_ms / 1000.0).max(0.000_1);+ let velocity = XY::new(((next.x - c.x) as f32) / dt, ((next.y - c.y) as f32) / dt);+ Some(InterpolatedCursorPosition {+ position: Coord::new(XY { x: c.x, y: c.y }),+ velocity,+ cursor_id: c.cursor_id.clone(),+ })+ } else {+ None}+ })+}- if let Some(event) = cursor.moves.last()- && event.time_ms <= time_ms- {- return Some(InterpolatedCursorPosition {- position: Coord::new(XY {- x: event.x,- y: event.y,- }),- velocity: XY::new(0.0, 0.0),- cursor_id: event.cursor_id.clone(),- });+pub struct PrecomputedCursorTimeline {+ timeline: Vec<SmoothedCursorEvent>,+ raw_cursor: CursorEvents,+ has_smoothing: bool,+}++impl PrecomputedCursorTimeline {+ pub fn new(+ cursor: &CursorEvents,+ smoothing: Option<SpringMassDamperSimulationConfig>,+ click_spring: Option<ClickSpringConfig>,+ ) -> Self {+ if cursor.moves.is_empty() || smoothing.is_none() {+ return Self {+ timeline: vec![],+ raw_cursor: cursor.clone(),+ has_smoothing: false,+ };}- cursor.moves.windows(2).find_map(|chunk| {- if time_ms >= chunk[0].time_ms && time_ms < chunk[1].time_ms {- let c = &chunk[0];- let next = &chunk[1];- let delta_ms = (next.time_ms - c.time_ms) as f32;- let dt = (delta_ms / 1000.0).max(0.000_1);- let velocity = XY::new(((next.x - c.x) as f32) / dt, ((next.y - c.y) as f32) / dt);- Some(InterpolatedCursorPosition {- position: Coord::new(XY { x: c.x, y: c.y }),- velocity,- cursor_id: c.cursor_id.clone(),- })- } else {- None- }- })+ let smoothing_config = smoothing.unwrap();+ let filtered_moves = filter_cursor_shake(&cursor.moves);+ let prepared_moves = decimate_cursor_moves(filtered_moves.as_ref());+ let timeline = build_smoothed_timeline(+ cursor,+ prepared_moves.as_ref(),+ smoothing_config,+ click_spring,+ );++ Self {+ timeline,+ raw_cursor: cursor.clone(),+ has_smoothing: true,+ }+ }++ pub fn interpolate(&self, time_secs: f32) -> Option<InterpolatedCursorPosition> {+ let time_ms = (time_secs * 1000.0) as f64;+ if self.has_smoothing {+ interpolate_timeline(&self.timeline, time_ms)+ } else {+ interpolate_raw_cursor(&self.raw_cursor, time_ms)+ }}}diff --git a/crates/rendering/src/lib.rs b/crates/rendering/src/lib.rsindex 846075e69..a7b0e8caf 100644--- a/crates/rendering/src/lib.rs+++ b/crates/rendering/src/lib.rs@@ -6,7 +6,8 @@ use cap_project::{use composite_frame::CompositeVideoFrameUniforms;use core::f64;use cursor_interpolation::{- InterpolatedCursorPosition, interpolate_cursor, interpolate_cursor_with_click_spring,+ InterpolatedCursorPosition, PrecomputedCursorTimeline, interpolate_cursor,+ interpolate_cursor_with_click_spring,};use decoder::{AsyncVideoDecoderHandle, spawn_decoder};use frame_pipeline::{@@ -783,8 +784,30 @@ pub async fn render_video_to_channel_nv12()}).collect();+ for interp in &mut zoom_focus_interpolators {+ interp.ensure_precomputed_until(duration as f32 + 1.0);+ }let zoom_focus_interpolators_construct_ms = zoom_build_start.elapsed().as_millis() as u64;+ let cursor_smoothing_for_precompute =+ (!project.cursor.raw).then_some(spring_mass_damper::SpringMassDamperSimulationConfig {+ tension: project.cursor.tension,+ mass: project.cursor.mass,+ friction: project.cursor.friction,+ });+ let click_spring_for_precompute = project.cursor.click_spring_config();++ let precomputed_cursor_timelines: Vec<PrecomputedCursorTimeline> = render_segments+ .iter()+ .map(|segment| {+ PrecomputedCursorTimeline::new(+ &segment.cursor,+ cursor_smoothing_for_precompute,+ Some(click_spring_for_precompute),+ )+ })+ .collect();+let mut frame_number = 0;let renderer_setup_start = Instant::now();@@ -885,8 +908,9 @@ pub async fn render_video_to_channel_nv12(consecutive_failures = 0;let zoom_focus_interp = &zoom_focus_interpolators[segment_clip_index];+ let precomputed_cursor = &precomputed_cursor_timelines[segment_clip_index];- let uniforms = ProjectUniforms::new(+ let uniforms = ProjectUniforms::new_with_precomputed_cursor(constants,project,current_frame_number,@@ -896,6 +920,7 @@ pub async fn render_video_to_channel_nv12(&segment_frames,duration,zoom_focus_interp,+ precomputed_cursor,);let next_frame_number = frame_number;@@ -2065,6 +2090,83 @@ impl ProjectUniforms {segment_frames: &DecodedSegmentFrames,total_duration: f64,zoom_focus_interpolator: &ZoomFocusInterpolator,+ ) -> Self {+ let cursor_smoothing = (!project.cursor.raw).then_some(SpringMassDamperSimulationConfig {+ tension: project.cursor.tension,+ mass: project.cursor.mass,+ friction: project.cursor.friction,+ });+ let click_spring_cfg = project.cursor.click_spring_config();++ let cursor_interp_fn = |time: f32| -> Option<InterpolatedCursorPosition> {+ match cursor_smoothing {+ Some(cfg) => interpolate_cursor_with_click_spring(+ cursor_events,+ time,+ Some(cfg),+ Some(click_spring_cfg),+ ),+ None => interpolate_cursor(cursor_events, time, None),+ }+ };++ Self::new_inner(+ constants,+ project,+ frame_number,+ fps,+ resolution_base,+ cursor_events,+ segment_frames,+ total_duration,+ zoom_focus_interpolator,+ &cursor_interp_fn,+ )+ }++ #[allow(clippy::too_many_arguments)]+ pub fn new_with_precomputed_cursor(+ constants: &RenderVideoConstants,+ project: &ProjectConfiguration,+ frame_number: u32,+ fps: u32,+ resolution_base: XY<u32>,+ cursor_events: &CursorEvents,+ segment_frames: &DecodedSegmentFrames,+ total_duration: f64,+ zoom_focus_interpolator: &ZoomFocusInterpolator,+ precomputed_cursor: &PrecomputedCursorTimeline,+ ) -> Self {+ let cursor_interp_fn = |time: f32| -> Option<InterpolatedCursorPosition> {+ precomputed_cursor.interpolate(time)+ };++ Self::new_inner(+ constants,+ project,+ frame_number,+ fps,+ resolution_base,+ cursor_events,+ segment_frames,+ total_duration,+ zoom_focus_interpolator,+ &cursor_interp_fn,+ )+ }++ #[allow(clippy::too_many_arguments)]+ fn new_inner(+ constants: &RenderVideoConstants,+ project: &ProjectConfiguration,+ frame_number: u32,+ fps: u32,+ resolution_base: XY<u32>,+ cursor_events: &CursorEvents,+ segment_frames: &DecodedSegmentFrames,+ total_duration: f64,+ zoom_focus_interpolator: &ZoomFocusInterpolator,+ cursor_interp_fn: &dyn Fn(f32) -> Option<InterpolatedCursorPosition>,) -> Self {let options = &constants.options;let output_size = Self::get_output_size(options, project, resolution_base);@@ -2102,44 +2204,10 @@ impl ProjectUniforms {let crop = Self::get_crop(options, project);- let cursor_smoothing = (!project.cursor.raw).then_some(SpringMassDamperSimulationConfig {- tension: project.cursor.tension,- mass: project.cursor.mass,- friction: project.cursor.friction,- });-- let click_spring_cfg = project.cursor.click_spring_config();-- let interpolated_cursor = match cursor_smoothing {- Some(cfg) => interpolate_cursor_with_click_spring(- cursor_events,- cursor_time_for_interp,- Some(cfg),- Some(click_spring_cfg),- ),- None => interpolate_cursor(cursor_events, cursor_time_for_interp, None),- };-- let prev_interpolated_cursor = match cursor_smoothing {- Some(cfg) => interpolate_cursor_with_click_spring(- cursor_events,- prev_cursor_time_for_interp,- Some(cfg),- Some(click_spring_cfg),- ),- None => interpolate_cursor(cursor_events, prev_cursor_time_for_interp, None),- };-+ let interpolated_cursor = cursor_interp_fn(cursor_time_for_interp);+ let prev_interpolated_cursor = cursor_interp_fn(prev_cursor_time_for_interp);let lookback_t = (cursor_time_for_interp - 0.4).max(0.0);- let past_cursor_for_tilt = match cursor_smoothing {- Some(cfg) => interpolate_cursor_with_click_spring(- cursor_events,- lookback_t,- Some(cfg),- Some(click_spring_cfg),- ),- None => interpolate_cursor(cursor_events, lookback_t, None),- };+ let past_cursor_for_tilt = cursor_interp_fn(lookback_t);let cursor_x_axis_tilt_radians =if let (Some(cur), Some(past)) = (&interpolated_cursor, past_cursor_for_tilt) {@@ -2205,15 +2273,7 @@ impl ProjectUniforms {let boundary_recording_time = (current_recording_time as f64- (frame_time as f64 - prev.end)).clamp(0.0, prev.end) as f32;- match cursor_smoothing {- Some(cfg) => interpolate_cursor_with_click_spring(- cursor_events,- boundary_recording_time,- Some(cfg),- Some(click_spring_cfg),- ),- None => interpolate_cursor(cursor_events, boundary_recording_time, None),- }+ cursor_interp_fn(boundary_recording_time)}).map(|c| Coord::<RawDisplayUVSpace>::new(c.position.coord));@@ -2241,15 +2301,7 @@ impl ProjectUniforms {let boundary_recording_time = (prev_recording_time as f64- (prev_frame_time as f64 - prev.end)).clamp(0.0, prev.end) as f32;- match cursor_smoothing {- Some(cfg) => interpolate_cursor_with_click_spring(- cursor_events,- boundary_recording_time,- Some(cfg),- Some(click_spring_cfg),- ),- None => interpolate_cursor(cursor_events, boundary_recording_time, None),- }+ cursor_interp_fn(boundary_recording_time)}).map(|c| Coord::<RawDisplayUVSpace>::new(c.position.coord));