Skip to content

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

Filters: the live preview is never coarser than the screen (#6326) (#6351)

F4LCONa05daee325a1
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 38c3b439b..914d395ac 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -1720,6 +1720,27 @@ fn budget_allowance(app: &PhotocraftApp) -> (u64, u64) {
(u64::from(performance.memory_usage_mb).saturating_mul(1 << 20), u64::from(performance.gpu_memory_mb).saturating_mul(1 << 20))
}
+/// The proxy factor of a live preview of filter `cmd` with `params` on `doc`, the active document:
+/// reduced on a large document (Preferences › Performance › Low Resolution Previews), except for
+/// filters whose features don't scale with a proxy (#2063), and never coarser than the active view,
+/// so at 100 % the preview shows every pixel instead of k-pixel blocks (#6326, as adjustment
+/// previews have since #5764). Zoomed out, the proxy keeps previews of large documents fast.
+///
+/// The in-dialog preview window ([`crate::filter_preview_ui`]) sizes its own textures from this
+/// too, so the canvas and the window never disagree about the factor.
+///
+/// Full resolution costs a whole filter per parameter change, which only the background worker
+/// absorbs. The web build has no worker and computes the preview inline on the UI thread (#1676),
+/// where that would freeze the page: there the document's proxy stands. The test and
+/// `PHOTOCRAFT_INLINE_JOBS` inline paths keep the cap deliberately, because `background_jobs` is
+/// flipped mid-session (a shell save during `actions.play`): a factor that followed it would change
+/// [`crate::filter_dialog::FilterPreviewKey`] under a finished preview and recompute it inline,
+/// the very stall this avoids.
+pub(crate) fn filter_preview_factor(app: &PhotocraftApp, doc: &Document, cmd: &str, params: &serde_json::Value) -> u32 {
+ let k = crate::filter_dialog::preview_factor(cmd, params, crate::proxy::preview_factor(doc, crate::proxy::reduced_previews(app)));
+ if cfg!(target_arch = "wasm32") { k } else { crate::proxy::view_factor(k, app.current_zoom()) }
+}
+
/// Live preview for an open filter dialog: run the filter on the proxy and upload it. With
/// background jobs (the desktop app) it computes on one background worker
/// (`filter_preview_worker`); otherwise (web, tests, `PHOTOCRAFT_INLINE_JOBS`) it keeps the
@@ -1751,8 +1772,7 @@ pub(crate) fn ensure_filter_preview(app: &mut PhotocraftApp, idx: usize, ctx: &e
params = crate::select_mask_view::request_params(&d.fields, params);
}
let st = app.session.documents().get(idx)?;
- // Filters whose features don't scale with a proxy preview at full resolution (#2063).
- let k = crate::filter_dialog::preview_factor(&cmd, &params, crate::proxy::preview_factor(&st.doc, crate::proxy::reduced_previews(app)));
+ let k = filter_preview_factor(app, &st.doc, &cmd, &params);
let request = crate::filter_dialog::FilterPreviewKey {
doc: st.doc.id,
revision: st.revision,
@@ -6108,6 +6128,59 @@ mod tests {
assert_eq!(super::display_image(Some(&d), &buf).pixels, reference(d.to_rgba8(&buf)).pixels);
}
+ /// #6326: a filter's live preview is never coarser than the screen. On a document big enough
+ /// for a reduced proxy, the view at 100 % previews at full resolution, and only a view zoomed
+ /// out far enough uses the proxy. Through `ensure_filter_preview`, so the factor the canvas
+ /// actually requests is the one under test.
+ #[test]
+ fn filter_preview_at_100_percent_is_full_resolution() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.background_jobs = false;
+ app.run("file.new", json!({"width": 2400, "height": 2200})).unwrap();
+ assert_eq!(crate::proxy::preview_factor(&app.session.active().unwrap().doc, true), 2, "big enough for a proxy");
+ crate::filter_dialog::open(&mut app, "filter.stylize.solarize").unwrap();
+ let ctx = egui::Context::default();
+ let preview_at = |app: &mut PhotocraftApp, zoom: f32| {
+ app.ui.views.get_mut(0).expect("the new document's view").zoom = zoom;
+ assert_eq!(app.current_zoom(), zoom);
+ let (k, _, size) = ensure_filter_preview(app, 0, &ctx).expect("a preview");
+ (k, size)
+ };
+ assert_eq!(preview_at(&mut app, 1.0), (1, [2400, 2200]), "100 %: every pixel");
+ assert_eq!(preview_at(&mut app, 0.25), (2, [1200, 1100]), "zoomed out: the document's proxy");
+ // Low Resolution Previews off: full resolution at any zoom, exactly as before #6326.
+ app.run("prefs.set", json!({"path": "performance.lowResolutionPreviews", "value": false})).unwrap();
+ assert_eq!(preview_at(&mut app, 0.25), (1, [2400, 2200]), "the preference still decides");
+ }
+
+ /// The factor itself over the zoom range, and that Preferences › Performance › Low
+ /// Resolution Previews still decides whether there is a proxy at all (#6326).
+ #[test]
+ fn filter_preview_factor_caps_at_the_view_without_overriding_the_preference() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ // 9 MP: reduced previews halve it (empty tiles, so nothing is allocated).
+ app.run("file.new", json!({"width": 3000, "height": 3000, "background": "transparent"})).unwrap();
+ let doc = app.session.active().unwrap().doc.clone();
+ let params = json!({"amount": 100, "radius": 4.0, "threshold": 0});
+ let k = |app: &mut PhotocraftApp, zoom: f32| {
+ app.ui.views.get_mut(0).expect("the new document's view").zoom = zoom;
+ super::filter_preview_factor(app, &doc, "filter.sharpen.unsharpMask", &params)
+ };
+ assert_eq!(k(&mut app, 0.25), 2, "zoomed out the preview stays reduced");
+ assert_eq!(k(&mut app, 0.5), 2);
+ assert_eq!(k(&mut app, 0.75), 1, "past the proxy's resolution it shows every pixel");
+ assert_eq!(k(&mut app, 1.0), 1);
+ assert_eq!(k(&mut app, 4.0), 1);
+ // A corrupt or unset zoom must not pick a coarser proxy than the document's own.
+ assert_eq!(k(&mut app, f32::NAN), 1);
+ assert_eq!(k(&mut app, 0.0), 1);
+ // Low Resolution Previews off: full resolution at any zoom, the cap a no-op.
+ app.run("prefs.set", json!({"path": "performance.lowResolutionPreviews", "value": false})).unwrap();
+ for zoom in [0.1, 0.25, 1.0, 4.0] {
+ assert_eq!(k(&mut app, zoom), 1, "zoom {zoom}");
+ }
+ }
+
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn every_filter_previews_off_the_ui_thread_and_a_newer_value_cancels_the_old() {
diff --git a/crates/ui-egui/src/filter_preview_ui.rs b/crates/ui-egui/src/filter_preview_ui.rs
index 90eaf4f6f..cf290cf19 100644
--- a/crates/ui-egui/src/filter_preview_ui.rs
+++ b/crates/ui-egui/src/filter_preview_ui.rs
@@ -46,9 +46,14 @@ pub(crate) fn window(app: &PhotocraftApp, dialog: u64, ui: &mut egui::Ui, f: &mu
(preview_on && p.key.dialog == dialog && p.key.doc == doc_id && p.key.command == cmd).then(|| p.result.clone().map(|r| (p.key.clone(), r))).flatten()
});
// The proxy factor the preview runs at (the filtered key's when it exists, else the request's).
+ // The fallback must be the factor the canvas will ask for, not the document's own: since #6326
+ // the canvas caps it with the view's zoom, and a window that sized its textures from the
+ // uncapped factor built a proxy the canvas never asked for, then rebuilt it (a full-size copy
+ // and upload on the UI thread at 100 %) the moment the first result arrived, shifting this
+ // window's 100 % mapping and pan clamp for a frame.
let base_k = filtered.as_ref().map(|(k, _)| k.k).unwrap_or_else(|| {
let params = app.with_mask_target(&cmd, crate::filter_dialog::params_of(f));
- crate::filter_dialog::preview_factor(&cmd, &params, crate::proxy::preview_factor(&st.doc, crate::proxy::reduced_previews(app)))
+ crate::canvas::filter_preview_factor(app, &st.doc, &cmd, &params)
});
let t = Tokens::get(ui.ctx());
diff --git a/crates/ui-egui/tests/full_res_preview.rs b/crates/ui-egui/tests/full_res_preview.rs
index 191a071b9..f472dd2ad 100644
--- a/crates/ui-egui/tests/full_res_preview.rs
+++ b/crates/ui-egui/tests/full_res_preview.rs
@@ -145,21 +145,30 @@ fn adjustment_layer_drag_previews_at_full_resolution_when_low_res_is_off() {
assert!((full - committed).abs() < 8.0, "low-res previews off: the drag matches the committed view ({full} vs {committed})");
}
+/// A filter dialog's live preview is never coarser than the screen either (#6326): at 100 % every
+/// column shows with low-resolution previews on; zoomed out to 50 % the default preview runs on
+/// the reduced copy (only the black columns), and with them off it matches the committed view.
#[test]
fn filter_dialog_previews_at_full_resolution_when_low_res_is_off() {
let _gpu = photocraft_testkit::gpu_lock();
let Some(mut h) = harness() else { return };
- setup(&mut h, 1.0);
+ setup(&mut h, 0.5);
if !on_gpu(&mut h) {
return;
}
+ let committed = mean(&row(&mut h));
+ assert!(committed > 60.0, "zoomed out the columns average to grey ({committed})");
+ control(&mut h, "ui.set", json!({"zoom": 1.0, "center": [W / 2, H / 2]}));
// Offset 0, 0 leaves the pixels as they are: any change is the preview's resolution.
photocraft_ui_egui::filter_dialog::open(h.state_mut(), "filter.other.offset").expect("Offset dialog");
- let reduced = row(&mut h);
- assert!(columns(&reduced) < 0.1, "default: the reduced copy loses the columns ({})", columns(&reduced));
+ let at_100 = row(&mut h);
+ assert!(columns(&at_100) > 0.9, "default at 100 %: the preview is never coarser than the screen ({})", columns(&at_100));
+ control(&mut h, "ui.set", json!({"zoom": 0.5, "center": [W / 2, H / 2]}));
+ let reduced = mean(&row(&mut h));
+ assert!(reduced < 30.0, "default at 50 %: the reduced copy keeps only the black columns ({reduced})");
low_res(&mut h, false);
- let full = row(&mut h);
- assert!(columns(&full) > 0.9, "low-res previews off: every column shows ({})", columns(&full));
+ let full = mean(&row(&mut h));
+ assert!((full - committed).abs() < 8.0, "low-res previews off: the preview matches the committed view ({full} vs {committed})");
}
#[test]