Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Adjustment layers: a live preview is never coarser than the screen (#5764) (#5778)

Michael Higgins3aaffdde681b
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index d9414d857..cb7227d6e 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -1924,14 +1924,22 @@ fn ensure_adjust_proxy(app: &mut PhotocraftApp, idx: usize, zoom: f32) -> Option
/// If a live adjustment preview is active on a large document, composite it on the proxy and upload
/// it under its own GPU key. Returns (factor, gpu key, dimensions) when the proxy should be drawn.
-fn ensure_proxy_preview(app: &mut PhotocraftApp, idx: usize) -> Option<(u32, u64, [u32; 2])> {
+/// `zoom` is the view's (physical screen pixels per document pixel).
+fn ensure_proxy_preview(app: &mut PhotocraftApp, idx: usize, zoom: f32) -> Option<(u32, u64, [u32; 2])> {
let (layer, params) = app.live_adjust.clone()?;
let (doc_id, revision, doc) = {
let st = app.session.documents().get(idx)?;
(st.doc.id, st.revision, st.doc.clone())
};
// Low-resolution previews off: k is 1 and the canvas shows `display_doc` (full size).
- let k = crate::proxy::preview_factor(&doc, crate::proxy::reduced_previews(app));
+ let mut k = crate::proxy::preview_factor(&doc, crate::proxy::reduced_previews(app));
+ // Zoomed in, a proxy coarser than the screen showed the drag in k-pixel blocks (#5764): when
+ // the wgpu compositor draws the full-size document, use a proxy only as coarse as the screen
+ // (none at 50 % and closer). Otherwise the full size would be a CPU composite per change.
+ let fine = crate::proxy::view_factor(k, zoom);
+ if fine < k && app.gpu.as_ref().is_some_and(|g| g.supports(&doc)) {
+ k = fine;
+ }
if k <= 1 {
return None;
}
@@ -2962,7 +2970,7 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
if app.gpu.is_some()
&& gpu_ok
&& let Some((k, key, preview_size)) =
- ensure_adjust_proxy(app, idx, view.zoom).or_else(|| ensure_filter_preview(app, idx, &ctx)).or_else(|| ensure_proxy_preview(app, idx))
+ ensure_adjust_proxy(app, idx, view.zoom).or_else(|| ensure_filter_preview(app, idx, &ctx)).or_else(|| ensure_proxy_preview(app, idx, view.zoom))
{
on_gpu = true;
app.navigator_gpu.remove(&doc.id);
diff --git a/crates/ui-egui/src/proxy.rs b/crates/ui-egui/src/proxy.rs
index dc30cc314..a3a73c754 100644
--- a/crates/ui-egui/src/proxy.rs
+++ b/crates/ui-egui/src/proxy.rs
@@ -27,6 +27,13 @@ pub fn preview_factor(doc: &Document, reduced: bool) -> u32 {
if reduced { factor(doc) } else { 1 }
}
+/// A live preview's factor `k` capped for a view at `zoom` (physical screen pixels per document
+/// pixel): never coarser than the screen ([`crate::adjust_preview::proxy_factor`]), so a view at
+/// 100 % shows full resolution instead of k-pixel blocks while a control moves (#5764).
+pub fn view_factor(k: u32, zoom: f32) -> u32 {
+ k.min(crate::adjust_preview::proxy_factor(zoom)).max(1)
+}
+
pub use photocraft_compose::proxy::{downsample, proxy_document};
#[cfg(test)]
@@ -48,6 +55,23 @@ mod tests {
assert_eq!(preview_factor(&small, true), 1);
}
+ /// #5764: dragging a Curves point on a large document showed the canvas in k-pixel blocks at
+ /// any zoom. The proxy may only be as coarse as the screen.
+ #[test]
+ fn view_factor_never_coarser_than_the_screen() {
+ let big = Document::new("b", Size::new(6016, 6016), ColorMode::Rgb, SampleType::U8);
+ let k = preview_factor(&big, true);
+ assert!(k > 2, "{k}");
+ assert_eq!(view_factor(k, 1.0), 1, "100 %: full resolution");
+ assert_eq!(view_factor(k, 2.0), 1, "zoomed in: full resolution");
+ assert_eq!(view_factor(k, 0.6), 1);
+ assert_eq!(view_factor(k, 0.5), 2);
+ assert_eq!(view_factor(k, 0.1), k, "far out: the document's proxy");
+ assert_eq!(view_factor(1, 0.1), 1, "small documents stay full size");
+ assert_eq!(view_factor(k, f32::NAN), 1);
+ assert_eq!(view_factor(0, 0.1), 1);
+ }
+
#[test]
fn proxy_keeps_structure_and_ids() {
let doc = Document::with_background("d", Size::new(4000, 3000), ColorMode::Rgb, SampleType::U8, Color::WHITE);