Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(desktop-gpui): preview camera effects at 720p with zero-copy segmentation

Richie McIlroyd4a2981dbb57
diff --git a/apps/desktop-gpui/src/camera_blur.rs b/apps/desktop-gpui/src/camera_blur.rs
index 1ff061685..bae5fc835 100644
--- a/apps/desktop-gpui/src/camera_blur.rs
+++ b/apps/desktop-gpui/src/camera_blur.rs
@@ -15,17 +15,19 @@
//! the Tauri `NativeFrameConverter` uses (`camera_native.rs:301-310`).
//! Imports are cached per ring slot (keyed by pixel-buffer pointer +
//! ring generation), so the steady state creates no textures.
-//! 3. `BlurProcessor::process_into_encoder` runs the segmentation mask +
-//! separable blur + composite, with the preview's 33ms inference interval
-//! (`CAMERA_PREVIEW_BLUR_INFERENCE_INTERVAL`, `camera.rs:50`).
+//! 3. The same pixel buffer goes to Apple Vision person segmentation on its
+//! own thread, and `BlurProcessor::process_into_encoder` composites with
+//! the newest finished mask, refined against this frame by the guided
+//! filter. Segmentation never blocks a preview frame.
//! 4. `cap_rendering::RgbaToBgraSurfaceConverter` blits the RGBA output into
//! a BGRA IOSurface-backed CVPixelBuffer from its own ring, and
//! `PendingSurface::wait` blocks until the GPU has finished writing it, so
//! the buffer handed back to the window is always complete when painted.
//!
//! Everything runs on one dedicated `camera-blur` thread (the Tauri preview
-//! renders on a dedicated thread too, `camera.rs:466-490`), so the ~10ms
-//! CoreML/CPU segmentation inference every 33ms never blocks the UI thread.
+//! renders on a dedicated thread too, `camera.rs:466-490`), with the ~10ms
+//! Neural Engine segmentation on a second one, so neither touches the UI
+//! thread.
//! The channel back to the window is how "what you see is what records" holds:
//! this is the same `BlurProcessor` the editor/export camera layer runs
//! (`cap-rendering/src/lib.rs:5742`), fed by the same `BackgroundBlurMode`
@@ -49,12 +51,12 @@ use cap_rendering::{
};
use cidre::{arc, cv};
-/// `CAMERA_PREVIEW_BLUR_MAX_TEXTURE_WIDTH/HEIGHT` (`camera.rs:46-47`): with
-/// blur on, the Tauri preview processes at most 640x360 -- the blur passes
-/// and the 256x256 segmentation downsample get a bounded input no matter what
-/// the camera delivers. The `FrameConverter` applies this cap during its
-/// hardware conversion, so the scale costs nothing extra.
-pub const BLUR_MAX_DIMS: (usize, usize) = (640, 360);
+/// With an effect on, the preview processes at most 720p: enough for a crisp
+/// cutout edge in the largest bubble on a Retina display, while the guided
+/// filter and composite stay around a millisecond of GPU time. The
+/// `FrameConverter` applies this cap during its hardware conversion, so the
+/// scale costs nothing extra.
+pub const BLUR_MAX_DIMS: (usize, usize) = (1280, 720);
/// `CAMERA_PREVIEW_BLUR_INFERENCE_INTERVAL` (`camera.rs:50`).
const BLUR_INFERENCE_INTERVAL: Duration = Duration::from_millis(33);
@@ -259,6 +261,9 @@ impl Worker {
let (width, height) = (job.width, job.height);
let texture = self.input_texture(&job)?.clone();
+ // Vision segments the converted IOSurface itself: no GPU readback or
+ // copy, and segmentation runs beside rendering instead of blocking it.
+ self.processor.set_segmentation_source(job.buffer.0.clone());
let mut encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
@@ -299,7 +304,7 @@ impl Worker {
let raw = &*frame.pixel_buffer as *const cv::PixelBuf as CVPixelBufferRef;
let buffer = unsafe { CVPixelBuffer::wrap_under_get_rule(raw) };
Some(
- crate::camera_window::snapshot_preview(&buffer)
+ crate::camera_window::snapshot_cutout(&buffer)
.context("cutout preview pixels unavailable")?,
)
} else {
@@ -360,7 +365,7 @@ mod tests {
use std::time::Instant;
#[test]
- #[ignore = "requires CAP_CAMERA_EFFECT_TEST_FRAME containing a 640x360 RGBA webcam frame"]
+ #[ignore = "requires CAP_CAMERA_EFFECT_TEST_FRAME containing a 1280x720 RGBA webcam frame"]
fn native_cutout_preserves_alpha_through_iosurface_and_preview_image() {
let pixels = std::fs::read(std::env::var("CAP_CAMERA_EFFECT_TEST_FRAME").unwrap()).unwrap();
let (width, height) = BLUR_MAX_DIMS;
diff --git a/apps/desktop-gpui/src/camera_window.rs b/apps/desktop-gpui/src/camera_window.rs
index 3a7f5497e..5e9b1a035 100644
--- a/apps/desktop-gpui/src/camera_window.rs
+++ b/apps/desktop-gpui/src/camera_window.rs
@@ -394,8 +394,8 @@ mod frame {
/// (`camera-preview-convert-benchmark`: 196us -> 137us per 720p frame on
/// the conversion alone, byte-identical output, before atlas savings).
/// The same transfer also downscales when a maximum size is given (blur
- /// caps its input at 640x360, `camera.rs:46-47`), so the blur path's
- /// scaling costs nothing extra.
+ /// caps its input at 720p, [`crate::camera_blur::BLUR_MAX_DIMS`]), so
+ /// the blur path's scaling costs nothing extra.
///
/// Mirroring is a second hardware pass: a `VTPixelRotationSession` with
/// `FlipHorizontalOrientation` flips the converted BGRA buffer into a
@@ -867,6 +867,23 @@ pub(crate) fn clear_parked_camera_preview(cx: &mut gpui::App) {
#[cfg(target_os = "macos")]
pub(crate) fn snapshot_preview(
buffer: &core_video::pixel_buffer::CVPixelBuffer,
+) -> Option<Arc<gpui::RenderImage>> {
+ snapshot_bgra(buffer, (960., 540.))
+}
+
+/// The background-removal cutout, at the full 720p it was composited at: the
+/// alpha edge is the whole point, so it is never nearest-neighbour shrunk.
+#[cfg(target_os = "macos")]
+pub(crate) fn snapshot_cutout(
+ buffer: &core_video::pixel_buffer::CVPixelBuffer,
+) -> Option<Arc<gpui::RenderImage>> {
+ snapshot_bgra(buffer, (1280., 720.))
+}
+
+#[cfg(target_os = "macos")]
+fn snapshot_bgra(
+ buffer: &core_video::pixel_buffer::CVPixelBuffer,
+ (max_width, max_height): (f64, f64),
) -> Option<Arc<gpui::RenderImage>> {
use core_video::pixel_buffer::{kCVPixelBufferLock_ReadOnly, kCVPixelFormatType_32BGRA};
@@ -892,19 +909,33 @@ pub(crate) fn snapshot_preview(
if base.is_null() {
return None;
}
- let scale = (960. / width as f64).min(540. / height as f64).min(1.);
+ let scale = (max_width / width as f64)
+ .min(max_height / height as f64)
+ .min(1.);
let target_width = (width as f64 * scale).round().max(1.) as u32;
let target_height = (height as f64 * scale).round().max(1.) as u32;
let data = unsafe { std::slice::from_raw_parts(base, required_bytes) };
// RenderImage consumes BGRA bytes even though image::Frame wraps RgbaImage.
- let pixels = cap_gpui_kernels::frame::downsample_nearest(
- data,
- width,
- height,
- stride,
- target_width,
- target_height,
- );
+ let pixels = if (target_width as usize, target_height as usize) == (width, height) {
+ if stride == row_bytes {
+ data[..row_bytes * height].to_vec()
+ } else {
+ let mut pixels = Vec::with_capacity(row_bytes * height);
+ for row in data.chunks(stride).take(height) {
+ pixels.extend_from_slice(&row[..row_bytes]);
+ }
+ pixels
+ }
+ } else {
+ cap_gpui_kernels::frame::downsample_nearest(
+ data,
+ width,
+ height,
+ stride,
+ target_width,
+ target_height,
+ )
+ };
let image = image::RgbaImage::from_raw(target_width, target_height, pixels)?;
Some(Arc::new(gpui::RenderImage::new(smallvec::smallvec![
image::Frame::new(image)