Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

refactor(export): construct zoom timelines in export and screenshot paths

Richie McIlroy316416f06c96
diff --git a/apps/desktop/src-tauri/src/export.rs b/apps/desktop/src-tauri/src/export.rs
index e01cadd02..a9dd08a15 100644
--- a/apps/desktop/src-tauri/src/export.rs
+++ b/apps/desktop/src-tauri/src/export.rs
@@ -4,7 +4,7 @@ use cap_export::{ExporterBase, make_cursor_only_project};
use cap_project::{RecordingMeta, XY};
use cap_rendering::{
FrameRenderer, ProjectRecordingsMeta, ProjectUniforms, RenderSegment, RenderVideoConstants,
- RendererLayers, ZoomFocusInterpolator, spring_mass_damper::SpringMassDamperSimulationConfig,
+ RendererLayers, ZoomTransformTimeline,
};
use futures::FutureExt;
use image::codecs::jpeg::JpegEncoder;
@@ -1543,25 +1543,12 @@ async fn generate_export_preview_inner(
.map(|t| t.duration())
.unwrap_or(0.0);
- let cursor_smoothing =
- (!project_config.cursor.raw).then_some(SpringMassDamperSimulationConfig {
- tension: project_config.cursor.tension,
- mass: project_config.cursor.mass,
- friction: project_config.cursor.friction,
- });
-
- let zoom_focus_interpolator = ZoomFocusInterpolator::new(
+ let mut zoom_timeline = ZoomTransformTimeline::from_project(
+ &project_config,
&render_segment.cursor,
- cursor_smoothing,
- project_config.cursor.click_spring_config(),
- project_config.screen_movement_spring,
total_duration,
- project_config
- .timeline
- .as_ref()
- .map(|t| t.zoom_segments.as_slice())
- .unwrap_or(&[]),
);
+ zoom_timeline.ensure_precomputed_until((frame_number as f32 + 1.0) / settings.fps as f32);
let uniforms = ProjectUniforms::new(
&render_constants,
@@ -1572,7 +1559,7 @@ async fn generate_export_preview_inner(
&render_segment.cursor,
&segment_frames,
total_duration,
- &zoom_focus_interpolator,
+ &zoom_timeline,
);
let mut frame_renderer = FrameRenderer::new(&render_constants);
@@ -1804,25 +1791,9 @@ async fn generate_export_preview_fast_inner(
.map(|t| t.duration())
.unwrap_or(0.0);
- let cursor_smoothing =
- (!project_config.cursor.raw).then_some(SpringMassDamperSimulationConfig {
- tension: project_config.cursor.tension,
- mass: project_config.cursor.mass,
- friction: project_config.cursor.friction,
- });
-
- let zoom_focus_interpolator = ZoomFocusInterpolator::new(
- &segment_media.cursor,
- cursor_smoothing,
- project_config.cursor.click_spring_config(),
- project_config.screen_movement_spring,
- total_duration,
- project_config
- .timeline
- .as_ref()
- .map(|t| t.zoom_segments.as_slice())
- .unwrap_or(&[]),
- );
+ let mut zoom_timeline =
+ ZoomTransformTimeline::from_project(&project_config, &segment_media.cursor, total_duration);
+ zoom_timeline.ensure_precomputed_until((frame_number as f32 + 1.0) / settings.fps as f32);
let uniforms = ProjectUniforms::new(
&editor.render_constants,
@@ -1833,7 +1804,7 @@ async fn generate_export_preview_fast_inner(
&segment_media.cursor,
&segment_frames,
total_duration,
- &zoom_focus_interpolator,
+ &zoom_timeline,
);
let mut frame_renderer = FrameRenderer::new(&editor.render_constants);
diff --git a/apps/desktop/src-tauri/src/screenshot_editor.rs b/apps/desktop/src-tauri/src/screenshot_editor.rs
index 19e24b7b8..2b75c8d7b 100644
--- a/apps/desktop/src-tauri/src/screenshot_editor.rs
+++ b/apps/desktop/src-tauri/src/screenshot_editor.rs
@@ -8,7 +8,7 @@ use cap_project::{
};
use cap_rendering::{
DecodedFrame, DecodedSegmentFrames, FrameRenderer, ProjectUniforms, RenderVideoConstants,
- RendererLayers, ZoomFocusInterpolator,
+ RendererLayers, ZoomTransformTimeline,
};
use image::{
GenericImageView, ImageEncoder, RgbImage, buffer::ConvertBuffer, codecs::png::PngEncoder,
@@ -398,18 +398,9 @@ impl ScreenshotEditorInstances {
ProjectUniforms::get_base_size(&constants.options, &current_config);
let cursor_events = cap_project::CursorEvents::default();
- let zoom_focus_interpolator = ZoomFocusInterpolator::new(
- &cursor_events,
- None,
- current_config.cursor.click_spring_config(),
- current_config.screen_movement_spring,
- 0.0,
- current_config
- .timeline
- .as_ref()
- .map(|t| t.zoom_segments.as_slice())
- .unwrap_or(&[]),
- );
+ let mut zoom_timeline =
+ ZoomTransformTimeline::from_project(&current_config, &cursor_events, 0.0);
+ zoom_timeline.ensure_precomputed_until(1.0 / 30.0);
let uniforms = ProjectUniforms::new(
&constants,
@@ -420,7 +411,7 @@ impl ScreenshotEditorInstances {
&cursor_events,
&segment_frames,
0.0,
- &zoom_focus_interpolator,
+ &zoom_timeline,
);
let render_started = Instant::now();
@@ -868,14 +859,14 @@ pub async fn prewarm_screenshot_renderer() {
let (base_w, base_h) = ProjectUniforms::get_base_size(&constants.options, &config);
let cursor_events = cap_project::CursorEvents::default();
- let zoom_focus_interpolator = ZoomFocusInterpolator::new(
- &cursor_events,
+ let mut zoom_timeline = ZoomTransformTimeline::new(
+ &[],
None,
- config.cursor.click_spring_config(),
+ &cursor_events,
config.screen_movement_spring,
0.0,
- &[],
);
+ zoom_timeline.ensure_precomputed_until(1.0 / 30.0);
let uniforms = ProjectUniforms::new(
&constants,
&config,
@@ -885,7 +876,7 @@ pub async fn prewarm_screenshot_renderer() {
&cursor_events,
&segment_frames,
0.0,
- &zoom_focus_interpolator,
+ &zoom_timeline,
);
match frame_renderer
@@ -1666,18 +1657,8 @@ pub async fn render_screenshot_png(instance: &ScreenshotEditorInstance) -> Resul
segment_has_camera: false,
};
let cursor_events = cap_project::CursorEvents::default();
- let zoom_focus_interpolator = ZoomFocusInterpolator::new(
- &cursor_events,
- None,
- config.cursor.click_spring_config(),
- config.screen_movement_spring,
- 0.0,
- config
- .timeline
- .as_ref()
- .map(|timeline| timeline.zoom_segments.as_slice())
- .unwrap_or(&[]),
- );
+ let mut zoom_timeline = ZoomTransformTimeline::from_project(&config, &cursor_events, 0.0);
+ zoom_timeline.ensure_precomputed_until(1.0 / 30.0);
let uniforms = ProjectUniforms::new(
&constants,
&config,
@@ -1687,7 +1668,7 @@ pub async fn render_screenshot_png(instance: &ScreenshotEditorInstance) -> Resul
&cursor_events,
&segment_frames,
0.0,
- &zoom_focus_interpolator,
+ &zoom_timeline,
);
let rendered_frame = frame_renderer
.render_immediate(
diff --git a/crates/cap-test/src/suites/performance.rs b/crates/cap-test/src/suites/performance.rs
index 5dad8669e..543846f41 100644
--- a/crates/cap-test/src/suites/performance.rs
+++ b/crates/cap-test/src/suites/performance.rs
@@ -6,7 +6,7 @@ use cap_export::{
use cap_project::{ProjectConfiguration, RecordingMeta, RecordingMetaInner, XY};
use cap_rendering::{
FrameRenderer, ProjectRecordingsMeta, ProjectUniforms, RenderVideoConstants, RendererLayers,
- ZoomFocusInterpolator, spring_mass_damper::SpringMassDamperSimulationConfig,
+ ZoomTransformTimeline,
};
use chrono::Utc;
use std::{
@@ -414,12 +414,13 @@ async fn benchmark_playback(
let mut frame_renderer = FrameRenderer::new(&context.render_constants);
let mut layers = create_layers(&context)?;
- let cursor_smoothing =
- (!context.project.cursor.raw).then_some(SpringMassDamperSimulationConfig {
- tension: context.project.cursor.tension,
- mass: context.project.cursor.mass,
- friction: context.project.cursor.friction,
- });
+ let mut zoom_timelines: Vec<ZoomTransformTimeline> = context
+ .segments
+ .iter()
+ .map(|segment_media| {
+ ZoomTransformTimeline::from_project(&context.project, &segment_media.cursor, duration)
+ })
+ .collect();
let mut metrics = PlaybackMetrics {
requested_frames,
@@ -479,19 +480,9 @@ async fn benchmark_playback(
continue;
};
- let zoom_focus_interpolator = ZoomFocusInterpolator::new(
- &segment_media.cursor,
- cursor_smoothing,
- context.project.cursor.click_spring_config(),
- context.project.screen_movement_spring,
- duration,
- context
- .project
- .timeline
- .as_ref()
- .map(|t| t.zoom_segments.as_slice())
- .unwrap_or(&[]),
- );
+ let clip_index = segment.recording_clip as usize;
+ let zoom_timeline = &mut zoom_timelines[clip_index];
+ zoom_timeline.ensure_precomputed_until((frame_number as f32 + 1.0) / PLAYBACK_FPS as f32);
let uniforms = ProjectUniforms::new(
&context.render_constants,
@@ -502,7 +493,7 @@ async fn benchmark_playback(
&segment_media.cursor,
&segment_frames,
duration,
- &zoom_focus_interpolator,
+ zoom_timeline,
);
let render_start = Instant::now();
@@ -676,25 +667,9 @@ async fn render_single_frame(context: &FixtureContext) -> Result<()> {
.get_frames_initial(0.0, !context.project.camera.hide, true, clip_offsets)
.await
.ok_or_else(|| anyhow::anyhow!("Initial frame decode returned no frame"))?;
- let cursor_smoothing =
- (!context.project.cursor.raw).then_some(SpringMassDamperSimulationConfig {
- tension: context.project.cursor.tension,
- mass: context.project.cursor.mass,
- friction: context.project.cursor.friction,
- });
- let zoom_focus_interpolator = ZoomFocusInterpolator::new(
- &segment_media.cursor,
- cursor_smoothing,
- context.project.cursor.click_spring_config(),
- context.project.screen_movement_spring,
- duration,
- context
- .project
- .timeline
- .as_ref()
- .map(|t| t.zoom_segments.as_slice())
- .unwrap_or(&[]),
- );
+ let mut zoom_timeline =
+ ZoomTransformTimeline::from_project(&context.project, &segment_media.cursor, duration);
+ zoom_timeline.ensure_precomputed_until(1.0 / PLAYBACK_FPS as f32);
let uniforms = ProjectUniforms::new(
&context.render_constants,
&context.project,
@@ -704,7 +679,7 @@ async fn render_single_frame(context: &FixtureContext) -> Result<()> {
&segment_media.cursor,
&frames,
duration,
- &zoom_focus_interpolator,
+ &zoom_timeline,
);
frame_renderer
diff --git a/crates/editor/examples/playback-pipeline-benchmark.rs b/crates/editor/examples/playback-pipeline-benchmark.rs
index d93e80b6f..6d0c58d5e 100644
--- a/crates/editor/examples/playback-pipeline-benchmark.rs
+++ b/crates/editor/examples/playback-pipeline-benchmark.rs
@@ -4,8 +4,7 @@ use cap_project::{
};
use cap_rendering::{
FrameRenderer, ProjectRecordingsMeta, ProjectUniforms, RenderVideoConstants, RendererLayers,
- ZoomFocusInterpolator, decoder::spawn_decoder,
- spring_mass_damper::SpringMassDamperSimulationConfig,
+ ZoomTransformTimeline, decoder::spawn_decoder,
};
use std::{
path::{Path, PathBuf},
@@ -485,11 +484,12 @@ async fn run_full_pipeline_benchmark(
resolution_base.x, resolution_base.y
);
- let cursor_smoothing = (!project.cursor.raw).then_some(SpringMassDamperSimulationConfig {
- tension: project.cursor.tension,
- mass: project.cursor.mass,
- friction: project.cursor.friction,
- });
+ let mut zoom_timelines: Vec<ZoomTransformTimeline> = segments
+ .iter()
+ .map(|segment_media| {
+ ZoomTransformTimeline::from_project(project, &segment_media.cursor, duration)
+ })
+ .collect();
for i in 0..max_frames {
let frame_time = i as f64 / fps as f64;
@@ -544,18 +544,9 @@ async fn run_full_pipeline_benchmark(
timings.decode_ms.push(decode_elapsed_ms);
- let zoom_focus_interpolator = ZoomFocusInterpolator::new(
- &segment_media.cursor,
- cursor_smoothing,
- project.cursor.click_spring_config(),
- project.screen_movement_spring,
- duration,
- project
- .timeline
- .as_ref()
- .map(|t| t.zoom_segments.as_slice())
- .unwrap_or(&[]),
- );
+ let clip_index = segment.recording_clip as usize;
+ let zoom_timeline = &mut zoom_timelines[clip_index];
+ zoom_timeline.ensure_precomputed_until((i as f32 + 1.0) / fps as f32);
let uniforms = ProjectUniforms::new(
&render_constants,
@@ -566,7 +557,7 @@ async fn run_full_pipeline_benchmark(
&segment_media.cursor,
&segment_frames,
duration,
- &zoom_focus_interpolator,
+ zoom_timeline,
);
let render_start = Instant::now();
@@ -689,11 +680,12 @@ async fn run_scrubbing_benchmark(
.map(|t| t.duration())
.unwrap_or(10.0);
- let cursor_smoothing = (!project.cursor.raw).then_some(SpringMassDamperSimulationConfig {
- tension: project.cursor.tension,
- mass: project.cursor.mass,
- friction: project.cursor.friction,
- });
+ let mut zoom_timelines: Vec<ZoomTransformTimeline> = segments
+ .iter()
+ .map(|segment_media| {
+ ZoomTransformTimeline::from_project(project, &segment_media.cursor, duration)
+ })
+ .collect();
let scrub_positions: Vec<f64> = {
let golden_ratio = 1.618_034;
@@ -758,18 +750,9 @@ async fn run_scrubbing_benchmark(
let frame_number = (scrub_time * fps as f64).round() as u32;
- let zoom_focus_interpolator = ZoomFocusInterpolator::new(
- &segment_media.cursor,
- cursor_smoothing,
- project.cursor.click_spring_config(),
- project.screen_movement_spring,
- duration,
- project
- .timeline
- .as_ref()
- .map(|t| t.zoom_segments.as_slice())
- .unwrap_or(&[]),
- );
+ let clip_index = segment.recording_clip as usize;
+ let zoom_timeline = &mut zoom_timelines[clip_index];
+ zoom_timeline.ensure_precomputed_until((frame_number as f32 + 1.0) / fps as f32);
let uniforms = ProjectUniforms::new(
&render_constants,
@@ -780,7 +763,7 @@ async fn run_scrubbing_benchmark(
&segment_media.cursor,
&segment_frames,
duration,
- &zoom_focus_interpolator,
+ zoom_timeline,
);
let render_start = Instant::now();
diff --git a/crates/export/src/preview.rs b/crates/export/src/preview.rs
index 729864124..a9327b638 100644
--- a/crates/export/src/preview.rs
+++ b/crates/export/src/preview.rs
@@ -2,10 +2,7 @@ use std::path::PathBuf;
use base64::{Engine, engine::general_purpose::STANDARD};
use cap_project::{RecordingMeta, XY};
-use cap_rendering::{
- FrameRenderer, ProjectUniforms, RendererLayers, ZoomFocusInterpolator,
- spring_mass_damper::SpringMassDamperSimulationConfig,
-};
+use cap_rendering::{FrameRenderer, ProjectUniforms, RendererLayers, ZoomTransformTimeline};
use image::codecs::jpeg::JpegEncoder;
use serde::{Deserialize, Serialize};
@@ -97,26 +94,12 @@ async fn render_preview_with_base(
&exporter_base.project_config,
);
- let cursor_smoothing =
- (!exporter_base.project_config.cursor.raw).then_some(SpringMassDamperSimulationConfig {
- tension: exporter_base.project_config.cursor.tension,
- mass: exporter_base.project_config.cursor.mass,
- friction: exporter_base.project_config.cursor.friction,
- });
-
- let zoom_focus_interpolator = ZoomFocusInterpolator::new(
+ let mut zoom_timeline = ZoomTransformTimeline::from_project(
+ &exporter_base.project_config,
&segment_media.cursor,
- cursor_smoothing,
- exporter_base.project_config.cursor.click_spring_config(),
- exporter_base.project_config.screen_movement_spring,
total_duration,
- exporter_base
- .project_config
- .timeline
- .as_ref()
- .map(|t| t.zoom_segments.as_slice())
- .unwrap_or(&[]),
);
+ zoom_timeline.ensure_precomputed_until((frame_number as f32 + 1.0) / settings.fps as f32);
let uniforms = ProjectUniforms::new(
&exporter_base.render_constants,
@@ -127,7 +110,7 @@ async fn render_preview_with_base(
&segment_media.cursor,
&segment_frames,
total_duration,
- &zoom_focus_interpolator,
+ &zoom_timeline,
);
let mut frame_renderer = FrameRenderer::new(&exporter_base.render_constants);
diff --git a/crates/export/tests/real_recording_regression.rs b/crates/export/tests/real_recording_regression.rs
index 1790b2e59..e0c8fd2ef 100644
--- a/crates/export/tests/real_recording_regression.rs
+++ b/crates/export/tests/real_recording_regression.rs
@@ -8,7 +8,7 @@ use cap_editor::create_segments;
use cap_project::{ProjectConfiguration, RecordingMeta, XY};
use cap_rendering::{
FrameRenderer, PrecomputedCursorTimeline, ProjectRecordingsMeta, ProjectUniforms,
- RenderSegment, RenderVideoConstants, RenderedFrame, RendererLayers, ZoomFocusInterpolator,
+ RenderSegment, RenderVideoConstants, RenderedFrame, RendererLayers, ZoomTransformTimeline,
render_video_to_channel, spring_mass_damper::SpringMassDamperSimulationConfig,
};
@@ -198,22 +198,13 @@ async fn export_sequence_render_matches_editor_reference_for_real_recording()
})
.collect();
- let mut zoom_focus_interpolators: Vec<ZoomFocusInterpolator> = reference_render_segments
+ let mut zoom_timelines: Vec<ZoomTransformTimeline> = reference_render_segments
.iter()
- .zip(precomputed_cursor_timelines.iter())
- .map(|(segment, precomputed_cursor)| {
- ZoomFocusInterpolator::new_with_precomputed_cursor(
+ .map(|segment| {
+ ZoomTransformTimeline::from_project(
+ &project_config,
&segment.cursor,
- cursor_smoothing,
- click_spring,
- project_config.screen_movement_spring,
total_duration,
- project_config
- .timeline
- .as_ref()
- .map(|timeline| timeline.zoom_segments.as_slice())
- .unwrap_or(&[]),
- Some(precomputed_cursor.clone()),
)
})
.collect();
@@ -253,7 +244,7 @@ async fn export_sequence_render_matches_editor_reference_for_real_recording()
.find(|clip| clip.index == timeline_segment.recording_clip);
let zoom_until = (frame_number as f32 + 1.0) / 60.0;
- zoom_focus_interpolators[clip_index].ensure_precomputed_until(zoom_until);
+ zoom_timelines[clip_index].ensure_precomputed_until(zoom_until);
let decoded = render_segment
.decoders
@@ -275,7 +266,7 @@ async fn export_sequence_render_matches_editor_reference_for_real_recording()
&render_segment.cursor,
&decoded,
total_duration,
- &zoom_focus_interpolators[clip_index],
+ &zoom_timelines[clip_index],
&precomputed_cursor_timelines[clip_index],
);