Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions crates/recording/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,13 @@ fn macos_window_crop(
.raw_handle()
.logical_bounds()?;
let display_position = display.raw_handle().logical_position();
// The video reads the window inside its rounded corners; the cursor
// is placed against the same trimmed frame.
Some(CursorCropBounds::new_macos(
crate::sources::screen_capture::trim_window_corners(LogicalBounds::new(
LogicalPosition::new(
window_bounds.position().x() - display_position.x(),
window_bounds.position().y() - display_position.y(),
),
window_bounds.size(),
)),
))
Some(CursorCropBounds::new_macos(LogicalBounds::new(
LogicalPosition::new(
window_bounds.position().x() - display_position.x(),
window_bounds.position().y() - display_position.y(),
),
window_bounds.size(),
)))
}

#[cfg(windows)]
Expand Down
63 changes: 59 additions & 4 deletions crates/recording/src/sources/screen_capture/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ fn configure_window_capture(settings: &mut sc::StreamCfg) {
}
}

/// What the stream reads. A display capture crops to `crop_bounds`; a window
/// capture reads the window itself, inside its rounded corners, where
/// `crop_bounds` is the window already trimmed by `WINDOW_CORNER_TRIM`.
/// What the stream reads. A display capture crops to `crop_bounds` on the
/// display; a window capture reads the same size from the window's own
/// top-left corner, so Focus records every pixel a screen region would.
fn source_rect(captures_window: bool, crop_bounds: super::CropBounds) -> cg::Rect {
let (x, y) = if captures_window {
(super::WINDOW_CORNER_TRIM, super::WINDOW_CORNER_TRIM)
(0.0, 0.0)
} else {
(crop_bounds.position().x(), crop_bounds.position().y())
};
Expand Down Expand Up @@ -1488,6 +1488,61 @@ mod tests {
}
}

fn window_crop(
x: f64,
y: f64,
width: f64,
height: f64,
scale: f64,
) -> super::super::CropBounds {
super::super::even_pixel_crop(
LogicalBounds::new(LogicalPosition::new(x, y), LogicalSize::new(width, height)),
scale,
)
}

fn rect(rect: cg::Rect) -> (f64, f64, f64, f64) {
(
rect.origin.x,
rect.origin.y,
rect.size.width,
rect.size.height,
)
}

#[test]
fn focus_reads_the_whole_window_from_its_top_left() {
let crop = window_crop(100., 50., 920., 436., 1.0);
assert_eq!(rect(source_rect(true, crop)), (0., 0., 920., 436.));
assert_eq!(rect(source_rect(false, crop)), (100., 50., 920., 436.));
}

#[test]
fn focus_reads_as_many_pixels_as_a_screen_region_at_every_scale() {
for (scale, width, height) in [
(1.0, 701., 461.),
(2.0, 845., 753.5),
(1.5, 1280., 800.),
(2.0, 1512., 949.),
] {
let crop = window_crop(37., 25., width, height, scale);
let (_, _, focus_width, focus_height) = rect(source_rect(true, crop));
let (_, _, region_width, region_height) = rect(source_rect(false, crop));
assert_eq!((focus_width, focus_height), (region_width, region_height));
assert!(width - focus_width < 2. / scale, "{scale}x {width} pt wide");
assert!(
height - focus_height < 2. / scale,
"{scale}x {height} pt tall"
);
}
}

#[test]
fn focus_reads_a_partly_offscreen_window_from_its_top_left() {
let crop = window_crop(-1440., -120., 1440., 900., 2.0);
assert_eq!(rect(source_rect(true, crop)), (0., 0., 1440., 900.));
}

#[test]
fn initial_capture_uses_window_filter_for_non_excluded_window() {
let config = window_config(Some(window_id("247")));
Expand Down
54 changes: 1 addition & 53 deletions crates/recording/src/sources/screen_capture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,6 @@ pub struct Config {
#[cfg(target_os = "macos")]
pub type CropBounds = LogicalBounds;

/// macOS hands a single window over with its rounded corners transparent,
/// which the encoder fills with black. Window-only capture reads this many
/// points in from every edge: a corner's curve clears 0.29 of its radius in,
/// and this covers radii up to 27 pt.
#[cfg(target_os = "macos")]
pub const WINDOW_CORNER_TRIM: f64 = 8.0;

/// `bounds` shrunk to whole, even pixel dimensions at `scale`. The encoder
/// needs even frames; a region an odd number of pixels across would be read
/// whole and squeezed a pixel smaller, resampling, and so softening, every
Expand All @@ -414,22 +407,6 @@ pub fn even_pixel_crop(bounds: CropBounds, scale: f64) -> CropBounds {
)
}

/// `bounds` with [`WINDOW_CORNER_TRIM`] taken off every side, or as it was
/// when the window is too small to trim.
#[cfg(target_os = "macos")]
pub fn trim_window_corners(bounds: CropBounds) -> CropBounds {
let size = bounds.size();
let trim = WINDOW_CORNER_TRIM;
if size.width() <= trim * 4. || size.height() <= trim * 4. {
return bounds;
}
let position = bounds.position();
LogicalBounds::new(
LogicalPosition::new(position.x() + trim, position.y() + trim),
LogicalSize::new(size.width() - 2. * trim, size.height() - 2. * trim),
)
}

#[cfg(windows)]
pub type CropBounds = PhysicalBounds;

Expand Down Expand Up @@ -559,13 +536,6 @@ impl<TCaptureFormat: ScreenCaptureFormat> ScreenCaptureConfig<TCaptureFormat> {
let target_refresh = validated_refresh_rate(display.refresh_rate());
let fps = std::cmp::max(1, std::cmp::min(max_fps, target_refresh));

// The capture reads the window inside its corners (see
// `WINDOW_CORNER_TRIM`), so the output and the cursor crop match that.
#[cfg(target_os = "macos")]
let crop_bounds = match window {
Some(_) => crop_bounds.map(trim_window_corners),
None => crop_bounds,
};
// Read exactly as many pixels as the frame holds (see `even_pixel_crop`).
#[cfg(target_os = "macos")]
let crop_bounds = crop_bounds
Expand Down Expand Up @@ -1240,22 +1210,9 @@ mod tests {
}

#[cfg(all(test, target_os = "macos"))]
mod window_corner_tests {
mod even_pixel_crop_tests {
use super::*;

#[test]
fn window_capture_reads_inside_the_rounded_corners() {
let window = LogicalBounds::new(
LogicalPosition::new(100., 50.),
LogicalSize::new(920., 436.),
);
let trimmed = trim_window_corners(window);
assert_eq!(trimmed.position().x(), 108.);
assert_eq!(trimmed.position().y(), 58.);
assert_eq!(trimmed.size().width(), 904.);
assert_eq!(trimmed.size().height(), 420.);
}

#[test]
fn crops_read_whole_even_pixels() {
let odd = LogicalBounds::new(LogicalPosition::new(8., 8.), LogicalSize::new(1690., 753.));
Expand All @@ -1271,13 +1228,4 @@ mod window_corner_tests {
assert_eq!(retina.size().width() * 2.0, 1690.);
assert_eq!(retina.size().height() * 2.0, 1506.);
}

#[test]
fn a_tiny_window_is_left_whole() {
let window = LogicalBounds::new(LogicalPosition::new(0., 0.), LogicalSize::new(30., 200.));
let kept = trim_window_corners(window);
assert_eq!(kept.position().x(), 0.);
assert_eq!(kept.size().width(), 30.);
assert_eq!(kept.size().height(), 200.);
}
}
14 changes: 3 additions & 11 deletions crates/verify/tests/recording.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3204,7 +3204,7 @@ const START_COVERAGE_SECS: f64 = 0.3;
/// no magenta from the occluder in front of it (the screen key is blue, the
/// noise grey, so strong positive Cr is the occluder alone), no black column
/// or row along an edge (the window drawn smaller than its frame), and the
/// window's own size, less the rounded corners macOS trims off.
/// whole window, every edge, at its own size.
fn check_focus(case: &mut Case, track: &Track, stimulus: &[StimulusEvent]) -> anyhow::Result<()> {
let Some((width, height, scale)) = stimulus.iter().find_map(|event| match event {
StimulusEvent::Ready {
Expand All @@ -3218,15 +3218,7 @@ fn check_focus(case: &mut Case, track: &Track, stimulus: &[StimulusEvent]) -> an
case.problem("missing", "focus: the stimulus never reported its size");
return Ok(());
};
#[cfg(target_os = "macos")]
let trim =
2 * (cap_recording::sources::screen_capture::WINDOW_CORNER_TRIM * scale).round() as u32;
#[cfg(not(target_os = "macos"))]
let trim = {
let _ = scale;
0
};
let expected = (width.saturating_sub(trim), height.saturating_sub(trim));
let expected = (width, height);

let mut frames = 0usize;
let mut sizes = BTreeMap::new();
Expand Down Expand Up @@ -3288,7 +3280,7 @@ fn check_focus(case: &mut Case, track: &Track, stimulus: &[StimulusEvent]) -> an
case.problem(
"focus-size",
format!(
"expected {}x{} (the window less {trim} px of corners), recorded {sizes:?}",
"expected {}x{} (the whole window), recorded {sizes:?}",
expected.0, expected.1
),
);
Expand Down
Loading