Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(rendering): preserve per-clip camera size and display aspect ratio

Richie McIlroyb7b74b2d31ab
diff --git a/apps/desktop/src-tauri/src/screenshot_editor.rs b/apps/desktop/src-tauri/src/screenshot_editor.rs
index d3e39fe8f..5a1777a9b 100644
--- a/apps/desktop/src-tauri/src/screenshot_editor.rs
+++ b/apps/desktop/src-tauri/src/screenshot_editor.rs
@@ -375,6 +375,7 @@ impl ScreenshotEditorInstances {
camera_frame: None,
segment_time: 0.0,
recording_time: 0.0,
+ segment_has_camera: false,
};
let (base_w, base_h) =
@@ -837,6 +838,7 @@ pub async fn prewarm_screenshot_renderer() {
camera_frame: None,
segment_time: 0.0,
recording_time: 0.0,
+ segment_has_camera: false,
};
let (base_w, base_h) = ProjectUniforms::get_base_size(&constants.options, &config);
@@ -1596,6 +1598,7 @@ pub async fn render_screenshot_png(instance: &ScreenshotEditorInstance) -> Resul
camera_frame: None,
segment_time: 0.0,
recording_time: 0.0,
+ segment_has_camera: false,
};
let cursor_events = cap_project::CursorEvents::default();
let zoom_focus_interpolator = ZoomFocusInterpolator::new(
diff --git a/crates/rendering/src/layers/display.rs b/crates/rendering/src/layers/display.rs
index 0e970b277..883b2b591 100644
--- a/crates/rendering/src/layers/display.rs
+++ b/crates/rendering/src/layers/display.rs
@@ -29,6 +29,34 @@ fn uniforms_for_source_frame(
uniforms.crop_bounds[3] * scale_y,
];
uniforms.frame_size = [source_size.x as f32, source_size.y as f32];
+
+ // The shader stretches the cropped source across the whole target rect. When a
+ // clip's aspect differs from the target rect (e.g. an imported clip recorded at
+ // a different resolution than the project's first clip), shrink the target rect
+ // to the clip's aspect and centre it ("contain") so the clip is never stretched;
+ // the leftover margins stay transparent and the project background shows through.
+ // Same-sized clips keep an identical aspect, so this is a no-op for them.
+ let crop_w = uniforms.crop_bounds[2] - uniforms.crop_bounds[0];
+ let crop_h = uniforms.crop_bounds[3] - uniforms.crop_bounds[1];
+ let target_w = uniforms.target_bounds[2] - uniforms.target_bounds[0];
+ let target_h = uniforms.target_bounds[3] - uniforms.target_bounds[1];
+
+ if crop_w > 0.0 && crop_h > 0.0 && target_w > 0.0 && target_h > 0.0 {
+ let source_aspect = crop_w / crop_h;
+ let target_aspect = target_w / target_h;
+
+ if (source_aspect - target_aspect).abs() > 0.001 {
+ let scale = (target_w / crop_w).min(target_h / crop_h);
+ let fitted_w = crop_w * scale;
+ let fitted_h = crop_h * scale;
+ let new_x0 = uniforms.target_bounds[0] + (target_w - fitted_w) * 0.5;
+ let new_y0 = uniforms.target_bounds[1] + (target_h - fitted_h) * 0.5;
+
+ uniforms.target_bounds = [new_x0, new_y0, new_x0 + fitted_w, new_y0 + fitted_h];
+ uniforms.target_size = [fitted_w, fitted_h];
+ }
+ }
+
uniforms
}
diff --git a/crates/rendering/src/lib.rs b/crates/rendering/src/lib.rs
index d901544bf..f762f6b23 100644
--- a/crates/rendering/src/lib.rs
+++ b/crates/rendering/src/lib.rs
@@ -394,6 +394,7 @@ impl RecordingSegmentDecoders {
camera_frame,
segment_time,
recording_time: segment_time + self.segment_offset as f32,
+ segment_has_camera: self.camera.is_some(),
})
} else {
let camera_frame = OptionFuture::from(
@@ -418,6 +419,7 @@ impl RecordingSegmentDecoders {
camera_frame,
segment_time,
recording_time: segment_time + self.segment_offset as f32,
+ segment_has_camera: self.camera.is_some(),
})
}
}
@@ -451,6 +453,7 @@ impl RecordingSegmentDecoders {
camera_frame,
segment_time,
recording_time: segment_time + self.segment_offset as f32,
+ segment_has_camera: self.camera.is_some(),
})
} else {
let camera_frame = OptionFuture::from(
@@ -475,6 +478,7 @@ impl RecordingSegmentDecoders {
camera_frame,
segment_time,
recording_time: segment_time + self.segment_offset as f32,
+ segment_has_camera: self.camera.is_some(),
})
}
}
@@ -3618,6 +3622,7 @@ pub struct DecodedSegmentFrames {
pub camera_frame: Option<DecodedFrame>,
pub segment_time: f32,
pub recording_time: f32,
+ pub segment_has_camera: bool,
}
#[derive(Clone, Copy, Debug, Default)]
@@ -4256,28 +4261,45 @@ impl RendererLayers {
constants,
);
+ let camera_frame_data = if segment_frames.segment_has_camera {
+ constants.options.camera_size.and_then(|_| {
+ segment_frames.camera_frame.as_ref().map(|frame| {
+ // Use the decoded frame's own dimensions rather than the project's
+ // configured `camera_size` (which is taken from the first recording).
+ // An imported clip can carry a camera recorded at a different
+ // resolution; uploading it with the first clip's size makes the YUV
+ // upload fail and leaves the previous clip's camera on screen.
+ (
+ XY::new(frame.width(), frame.height()),
+ frame,
+ segment_frames.recording_time,
+ )
+ })
+ })
+ } else {
+ None
+ };
+
self.camera.prepare(
&constants.device,
&constants.queue,
- uniforms.camera,
- constants.options.camera_size.and_then(|size| {
- segment_frames
- .camera_frame
- .as_ref()
- .map(|frame| (size, frame, segment_frames.recording_time))
- }),
+ if segment_frames.segment_has_camera {
+ uniforms.camera
+ } else {
+ None
+ },
+ camera_frame_data,
);
self.camera_only.prepare(
&constants.device,
&constants.queue,
- uniforms.camera_only,
- constants.options.camera_size.and_then(|size| {
- segment_frames
- .camera_frame
- .as_ref()
- .map(|frame| (size, frame, segment_frames.recording_time))
- }),
+ if segment_frames.segment_has_camera {
+ uniforms.camera_only
+ } else {
+ None
+ },
+ camera_frame_data,
);
if let Some(mode) = blur_mode_from_config(&uniforms.project.camera.background_blur) {
@@ -4384,17 +4406,35 @@ impl RendererLayers {
);
timings.cursor_prepare_duration = start.elapsed();
+ let camera_frame_data = if segment_frames.segment_has_camera {
+ constants.options.camera_size.and_then(|_| {
+ segment_frames.camera_frame.as_ref().map(|frame| {
+ // Use the decoded frame's own dimensions rather than the project's
+ // configured `camera_size` (which is taken from the first recording).
+ // An imported clip can carry a camera recorded at a different
+ // resolution; uploading it with the first clip's size makes the YUV
+ // upload fail and leaves the previous clip's camera on screen.
+ (
+ XY::new(frame.width(), frame.height()),
+ frame,
+ segment_frames.recording_time,
+ )
+ })
+ })
+ } else {
+ None
+ };
+
let start = Instant::now();
self.camera.prepare_with_encoder(
&constants.device,
&constants.queue,
- uniforms.camera,
- constants.options.camera_size.and_then(|size| {
- segment_frames
- .camera_frame
- .as_ref()
- .map(|frame| (size, frame, segment_frames.recording_time))
- }),
+ if segment_frames.segment_has_camera {
+ uniforms.camera
+ } else {
+ None
+ },
+ camera_frame_data,
encoder,
);
timings.camera_prepare_duration = start.elapsed();
@@ -4403,13 +4443,12 @@ impl RendererLayers {
self.camera_only.prepare_with_encoder(
&constants.device,
&constants.queue,
- uniforms.camera_only,
- constants.options.camera_size.and_then(|size| {
- segment_frames
- .camera_frame
- .as_ref()
- .map(|frame| (size, frame, segment_frames.recording_time))
- }),
+ if segment_frames.segment_has_camera {
+ uniforms.camera_only
+ } else {
+ None
+ },
+ camera_frame_data,
encoder,
);
timings.camera_only_prepare_duration = start.elapsed();