Keep the overlay on the photograph when the view moves
The overlay is a source-space picture; the canvas beside it shows whatever the crop, the zoom and the pan selected out of that same space. Drawn whole it stayed frame-sized while the photograph moved underneath, so zooming in left a map of the whole picture stretched over a detail of it. It now reports the visible rectangle as a clip, which the compositor applies for nothing. Resampling on the CPU instead would mean rebuilding a megapixel image on every frame of a drag, and putting it on the GPU would add a second texture to keep in step with the view. Pushed from the render path rather than the panel's sync: a pan changes no mask and no row, so nothing else needs to run, and rebuilding the row models on every frame of a drag would be waste. Straightening is handled by rotating the image. A quarter turn or a flip permutes the axes and a clip rectangle cannot say that — noted where it happens rather than left to be discovered. The proper fix is to run the overlay through the same shader prologue the photograph goes through, which is the right answer and a larger one than this. Four tests, and the one that matters asserts the clip *narrows* when zoomed — which is precisely what it failed to do.
This commit is contained in:
+156
-5
@@ -858,14 +858,49 @@ impl DevelopSession {
|
||||
self.show_overlay = on;
|
||||
}
|
||||
|
||||
/// The part of the overlay the view is currently showing, in overlay
|
||||
/// pixels: `(x, y, width, height)`.
|
||||
///
|
||||
/// The overlay is a **source-space** picture, and the canvas beside it
|
||||
/// shows whatever the crop, the zoom and the pan selected out of that same
|
||||
/// space. Drawn whole, it stays the size of the frame while the photograph
|
||||
/// moves underneath — which is exactly the fault this exists to fix.
|
||||
///
|
||||
/// Reported as a clip rectangle rather than resampled here: the compositor
|
||||
/// crops and scales a texture for nothing, where doing it on the CPU would
|
||||
/// mean rebuilding a megapixel image on every frame of a drag.
|
||||
///
|
||||
/// **Known gap.** A quarter turn or a flip permutes the axes, and a clip
|
||||
/// rectangle cannot express that — the straightening angle is handled
|
||||
/// alongside this, but a quarter-turned frame shows the overlay unturned.
|
||||
/// Fixing it properly means running the overlay through the same shader
|
||||
/// prologue the image goes through, which is the right answer and a larger
|
||||
/// one than this.
|
||||
pub fn overlay_clip(&self) -> (i32, i32, i32, i32) {
|
||||
let Some(seg) = self.segmentation.as_ref() else {
|
||||
return (0, 0, 0, 0);
|
||||
};
|
||||
let (w, h) = seg.proxy_size();
|
||||
let rect = self.graph.framing().visible_rect();
|
||||
|
||||
// Rounded outward, so half a pixel of rounding never shows as a strip
|
||||
// of missing overlay along an edge.
|
||||
let x = (rect.x * w as f32).floor().max(0.0) as i32;
|
||||
let y = (rect.y * h as f32).floor().max(0.0) as i32;
|
||||
let right = ((rect.x + rect.width) * w as f32).ceil().min(w as f32) as i32;
|
||||
let bottom = ((rect.y + rect.height) * h as f32).ceil().min(h as f32) as i32;
|
||||
|
||||
(x, y, (right - x).max(1), (bottom - y).max(1))
|
||||
}
|
||||
|
||||
/// TRACES: FR-DEV-3
|
||||
/// A false-coloured picture of the current grouping, for the canvas.
|
||||
/// A false-coloured picture of what a click can select, for the canvas.
|
||||
///
|
||||
/// Returned as a CPU image rather than a texture, and deliberately: it is
|
||||
/// regenerated only when the level changes, it is proxy-sized rather than
|
||||
/// viewport-sized, and Slint scales and composites it for free. Putting it
|
||||
/// on the GPU would buy nothing and add a second texture to keep in step
|
||||
/// with the view.
|
||||
/// regenerated only when the segmentation changes, it is proxy-sized
|
||||
/// rather than viewport-sized, and the compositor scales and clips it for
|
||||
/// free. Putting it on the GPU would buy nothing and add a second texture
|
||||
/// to keep in step with the view.
|
||||
///
|
||||
/// `None` when the overlay is off or nothing has been segmented, so the
|
||||
/// caller can bind this straight to an image source.
|
||||
@@ -1865,6 +1900,122 @@ mod tests {
|
||||
out
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// The overlay's clip rectangle
|
||||
// ----------------------------------------------------------------------
|
||||
//
|
||||
// The overlay is a source-space picture and the canvas shows whatever the
|
||||
// crop, the zoom and the pan selected out of that space. Drawn whole it
|
||||
// stays frame-sized while the photograph moves underneath, which is what
|
||||
// these pin down.
|
||||
|
||||
/// A session with a segmentation, so the clip has a proxy to measure
|
||||
/// against.
|
||||
///
|
||||
/// The model finds nothing in flat grey, and that is fine: the clip is
|
||||
/// computed from the framing and the proxy size, neither of which depends
|
||||
/// on what was detected.
|
||||
fn segmented_session(ctx: &GpuContext) -> Option<DevelopSession> {
|
||||
let rgba: Vec<u8> = (0..100 * 100).flat_map(|_| [128, 128, 128, 255]).collect();
|
||||
let mut session =
|
||||
DevelopSession::open_rgb(ctx, &rgba, 100, 100, dr_types::Orientation::NORMAL)
|
||||
.expect("session");
|
||||
session
|
||||
.segment(ctx, &crate::segmentation::Options::default())
|
||||
.ok()?;
|
||||
Some(session)
|
||||
}
|
||||
|
||||
fn headless() -> Option<GpuContext> {
|
||||
pollster::block_on(dr_gpu::GpuContext::new_headless()).ok()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unzoomed_overlay_shows_the_whole_frame() {
|
||||
let Some(ctx) = headless() else { return };
|
||||
let Some(session) = segmented_session(&ctx) else {
|
||||
eprintln!("no model; skipping");
|
||||
return;
|
||||
};
|
||||
|
||||
let (x, y, w, h) = session.overlay_clip();
|
||||
assert_eq!((x, y), (0, 0));
|
||||
assert!(w > 1 && h > 1, "the whole proxy: {w}x{h}");
|
||||
}
|
||||
|
||||
/// The bug this exists for: zooming must narrow the clip, or the overlay
|
||||
/// keeps showing the whole picture at frame size while the canvas shows a
|
||||
/// detail of it.
|
||||
#[test]
|
||||
fn zooming_narrows_the_overlay_to_what_is_visible() {
|
||||
let Some(ctx) = headless() else { return };
|
||||
let Some(mut session) = segmented_session(&ctx) else {
|
||||
eprintln!("no model; skipping");
|
||||
return;
|
||||
};
|
||||
|
||||
let (_, _, full_w, full_h) = session.overlay_clip();
|
||||
session.zoom_about(4.0, 0.5, 0.5);
|
||||
let (_, _, zoomed_w, zoomed_h) = session.overlay_clip();
|
||||
|
||||
assert!(
|
||||
zoomed_w < full_w && zoomed_h < full_h,
|
||||
"zoomed in, the overlay should show less: {zoomed_w}x{zoomed_h} \
|
||||
against {full_w}x{full_h}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn panning_moves_the_overlay_with_the_photograph() {
|
||||
let Some(ctx) = headless() else { return };
|
||||
let Some(mut session) = segmented_session(&ctx) else {
|
||||
eprintln!("no model; skipping");
|
||||
return;
|
||||
};
|
||||
|
||||
session.zoom_about(4.0, 0.5, 0.5);
|
||||
let (before_x, _, _, _) = session.overlay_clip();
|
||||
session.pan_by(0.3, 0.0);
|
||||
let (after_x, _, _, _) = session.overlay_clip();
|
||||
|
||||
assert!(
|
||||
after_x > before_x,
|
||||
"panning right moves the visible window right: {before_x} then {after_x}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cropping_narrows_the_overlay_too() {
|
||||
let Some(ctx) = headless() else { return };
|
||||
let Some(mut session) = segmented_session(&ctx) else {
|
||||
eprintln!("no model; skipping");
|
||||
return;
|
||||
};
|
||||
|
||||
let (_, _, full_w, _) = session.overlay_clip();
|
||||
session.set_crop(dr_pipeline::CropRect {
|
||||
x: 0.25,
|
||||
y: 0.25,
|
||||
width: 0.5,
|
||||
height: 0.5,
|
||||
});
|
||||
let (x, y, w, _) = session.overlay_clip();
|
||||
|
||||
assert!(w < full_w, "a half-width crop shows half the overlay");
|
||||
assert!(x > 0 && y > 0, "and it starts inside the frame");
|
||||
}
|
||||
|
||||
/// Nothing segmented means no overlay, and no rectangle a caller might
|
||||
/// divide by.
|
||||
#[test]
|
||||
fn no_segmentation_means_no_clip() {
|
||||
let Some(ctx) = headless() else { return };
|
||||
let rgba: Vec<u8> = (0..16 * 16).flat_map(|_| [128, 128, 128, 255]).collect();
|
||||
let session = DevelopSession::open_rgb(&ctx, &rgba, 16, 16, dr_types::Orientation::NORMAL)
|
||||
.expect("session");
|
||||
assert_eq!(session.overlay_clip(), (0, 0, 0, 0));
|
||||
}
|
||||
|
||||
/// TRACES: FR-DSP-1 | AC-8
|
||||
#[test]
|
||||
fn the_displayed_frame_is_a_texture_and_not_a_pixel_buffer() {
|
||||
|
||||
Reference in New Issue
Block a user