From c616c77002bf8990160ecce991bc1f4e614a847a Mon Sep 17 00:00:00 2001 From: Khawar Zamman Wani Date: Tue, 10 Nov 2026 20:56:38 +0530 Subject: [PATCH] Speed up adjustment previews for unstyled clipped raster layers (#12356) * Preview adjustments on clipped raster layers in isolation * Measure clipped adjustment previews and fix test lint * Match styled preview benchmark to canvas proxy eligibility * Keep styled clipped previews off the UI thread * Address preview benchmark reporting and regression coverage review * Document benchmark reporting regressions * Document benchmark setup and isolated fixture execution --- crates/ui-egui/examples/interactive_bench.rs | 528 +++++++++++++----- crates/ui-egui/src/dialogs/adjust_preview.rs | 55 +- .../src/dialogs/adjust_preview/tests.rs | 287 +++++++++- crates/ui-egui/tests/it/adjust_preview_gpu.rs | 106 +++- crates/ui-egui/tests/it/main.rs | 5 + 5 files changed, 809 insertions(+), 172 deletions(-) diff --git a/crates/ui-egui/examples/interactive_bench.rs b/crates/ui-egui/examples/interactive_bench.rs index 0b4cc5c952..8ee3abcb18 100644 --- a/crates/ui-egui/examples/interactive_bench.rs +++ b/crates/ui-egui/examples/interactive_bench.rs @@ -12,12 +12,17 @@ //! real latency. Without a GPU adapter the CPU compositor stands in. Zoom and pan don't //! recomposite (the canvas samples the composited texture's mip levels); "viewport (CPU)" times //! what the CPU fallback pays for one 1920×1080 view. Each row is the median of `--reps` runs -//! (min / max shown); the first run of an operation is reported separately as "cold". +//! (min / max shown); the first run of an operation is reported separately as "cold", and +//! path-reported rows keep its compositing path (`cold_path` in `--json`) beside the warm row's +//! `warm_path`. Adjustment preview rows cover a plain pixel target and a clipped one. A styled +//! clipped target keeps the CPU proxy (its preview group would rebuild effect shape maps on the +//! UI thread), so only its CPU reference rows run and the reason is printed. use std::time::Instant; use eframe::wgpu; -use photocraft_doc::{Document, LayerContent}; +use photocraft_color::{BlendMode, Color, ColorMode, SampleType}; +use photocraft_doc::{Document, Effect, FxCommon, Layer, LayerContent, LayerId, Size}; use photocraft_engine::Session; use photocraft_geom::Rect; use serde_json::{Value, json}; @@ -38,22 +43,25 @@ fn block_on(f: F) -> F::Output { } struct Gpu { + adapter_name: String, device: wgpu::Device, queue: wgpu::Queue, comp: photocraft_gpu::Compositor, } +/// Request the app's GPU limits, retaining adapter identity for reports; absence selects the CPU canvas. fn gpu() -> Option { let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle_from_env()); let adapter = block_on(instance.request_adapter(&wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::HighPerformance, ..Default::default() })) .ok()?; - eprintln!("adapter: {}", adapter.get_info().name); + let adapter_name = adapter.get_info().name; + eprintln!("adapter: {adapter_name}"); // The limits the app requests (see `gpu_canvas::use_adapter_limits`). let limits = photocraft_ui_egui::gpu_canvas::device_limits(&adapter); let (device, queue) = block_on(adapter.request_device(&wgpu::DeviceDescriptor { required_limits: limits, ..Default::default() })).ok()?; let comp = photocraft_gpu::Compositor::new(&device); - Some(Gpu { device, queue, comp }) + Some(Gpu { adapter_name, device, queue, comp }) } /// A photo-like RGB image: smooth gradients, soft blobs, fine noise (so JPEG has real work). @@ -76,6 +84,16 @@ fn photo(w: u32, h: u32) -> photocraft_codecs::Image { photocraft_codecs::Image::from_raw(w, h, photocraft_codecs::ChannelLayout::Rgb, photocraft_codecs::SampleType::U8, px).expect("image") } +/// JSON metadata for one benchmark row, in the same order as `Bench::rows`. +struct RowExtra { + samples: Vec, + peak_rss: Option, + faults: Option, + /// Any fallback among the warm samples takes precedence. + warm_path: Option<&'static str>, + cold_path: Option<&'static str>, +} + struct Bench { gpu: Option, reps: usize, @@ -83,9 +101,8 @@ struct Bench { /// state later rows need). only: Option, rows: Vec<(String, f64, f64, f64, f64)>, - /// Per row (same order as `rows`): the timed samples, the peak RSS while they ran and the - /// minor page faults per warm run (all of it, untimed setup included), for `--json` (`cargo xtask perf`). - extra: Vec<(Vec, Option, Option)>, + /// Per row (same order as `rows`): the metadata for `--json` (`cargo xtask perf`). + extra: Vec, rss: photocraft_testkit::perf::RssSampler, } @@ -101,44 +118,75 @@ impl Bench { /// GPU (or CPU fallback) composite of `region` of `doc`, waited on. fn render(&mut self, doc: &Document, region: Rect) -> f64 { - if region.is_empty() { - return 0.0; - } - let t = Instant::now(); - match &mut self.gpu { - Some(g) => { - if g.comp.render(&g.device, &g.queue, doc, region, |_, _| {}).is_err() { - std::hint::black_box(photocraft_compose::render(doc, region)); - } - let _ = g.device.poll(wgpu::PollType::Wait { submission_index: None, timeout: None }); - } - None => { - std::hint::black_box(photocraft_compose::render(doc, region)); - } - } - t.elapsed().as_secs_f64() * 1000.0 + self.render_preview(doc, region).0 } /// Times `f` (`reps` runs after a cold one); `f` returns its own breakdown-free total in ms. fn time(&mut self, name: &str, mut f: impl FnMut(&mut Self) -> f64) { + self.time_path(name, false, |b| (f(b), "")); + } + + /// [`Self::time`] for a row that also reports the compositing path that drew it (`GPU`, + /// `CPU fallback` or `CPU canvas`), so a fallback is never presented as a GPU result. The cold + /// sample's path is kept beside the warm row's (see [`RowExtra`]). + fn time_path(&mut self, name: &str, report_path: bool, mut f: impl FnMut(&mut Self) -> (f64, &'static str)) { if self.only.as_ref().is_some_and(|o| !name.contains(o.as_str())) { f(self); return; } self.rss.reset(); - let cold = f(self); + let (cold, cold_path) = f(self); let faults0 = photocraft_testkit::perf::minor_faults(); - let mut v: Vec = (0..self.reps).map(|_| f(self)).collect(); + let mut v = Vec::with_capacity(self.reps); + let mut path = ""; + let mut fallback = false; + for _ in 0..self.reps { + let (t, p) = f(self); + fallback |= p == "CPU fallback"; + path = p; + v.push(t); + } + if fallback { + path = "CPU fallback"; + } // Minor page faults per warm run: memory mapped fresh from the kernel each time (#10479). // Counted over the whole of `f`, so it includes untimed setup and cleanup (an undo, an // extra refresh), not only the work the milliseconds measure. let faults = faults0.zip(photocraft_testkit::perf::minor_faults()).map(|(a, b)| b.saturating_sub(a) / self.reps as u64); - self.extra.push((v.clone(), self.rss.peak(), faults)); + let report_paths = report_path && !path.is_empty(); + let warm_path = report_paths.then_some(path); + let cold_path = (report_path && !cold_path.is_empty()).then_some(cold_path); + self.extra.push(RowExtra { samples: v.clone(), peak_rss: self.rss.peak(), faults, warm_path, cold_path }); v.sort_by(f64::total_cmp); let med = v[v.len() / 2]; let fl = faults.map_or(String::new(), |f| format!(", {f} faults/run incl. setup")); - println!("{name:<44} {med:>9.1} ms (min {:>8.1}, max {:>8.1}, cold {:>8.1}{fl})", v[0], v[v.len() - 1], cold); - self.rows.push((name.to_string(), med, v[0], v[v.len() - 1], cold)); + let cold_tag = cold_path.map_or(String::new(), |p| format!(" ({p})")); + let shown = if report_paths { format!("{name} ({path})") } else { name.to_string() }; + println!("{shown:<44} {med:>9.1} ms (min {:>8.1}, max {:>8.1}, cold {:>8.1}{cold_tag}{fl})", v[0], v[v.len() - 1], cold); + self.rows.push((shown, med, v[0], v[v.len() - 1], cold)); + } + + /// Composite `region` of `doc`, wait for the GPU, and report which path drew it. + fn render_preview(&mut self, doc: &Document, region: Rect) -> (f64, &'static str) { + if region.is_empty() { + return (0.0, "empty region"); + } + let t = Instant::now(); + let path = match &mut self.gpu { + Some(g) => { + let ok = g.comp.render(&g.device, &g.queue, doc, region, |_, _| {}).is_ok(); + if !ok { + std::hint::black_box(photocraft_compose::render(doc, region)); + } + let _ = g.device.poll(wgpu::PollType::Wait { submission_index: None, timeout: None }); + if ok { "GPU" } else { "CPU fallback" } + } + None => { + std::hint::black_box(photocraft_compose::render(doc, region)); + "CPU canvas" + } + }; + (ms(t), path) } } @@ -173,6 +221,203 @@ fn exec(s: &mut Session, id: &str, p: Value) { } } +/// A 36 MP RGB/8 document with a clipped raster target in a real clipping stack: a base, a +/// clipped sibling below, the clipped target and a clipped sibling above. `styled` gives the +/// target a Color Overlay: that makes it ineligible for the layer preview (the preview's styled +/// group would rebuild its effect shape maps on the UI thread), so the benchmark reports only +/// its CPU reference rows (#7487). +fn clipped_document(w: u32, h: u32, styled: bool) -> (Document, LayerId) { + let mut doc = Document::new("clipped preview", Size::new(w, h), ColorMode::Rgb, SampleType::U8); + let fmt = doc.pixel_format(); + let (wi, hi) = (w as i32, h as i32); + let region = Rect::from_xywh(wi / 4, hi / 4, w / 2, h / 2); + let band = Rect::from_xywh(region.x0, region.y0, region.width(), region.height() / 3); + let mut base = Layer::raster("clip base", fmt); + if let Some(s) = base.surface_mut() { + s.fill_rect(doc.bounds(), &[0.42, 0.5, 0.58, 1.0]); + s.fill_rect(Rect::from_xywh(0, 0, w / 2, h), &[0.3, 0.36, 0.44, 1.0]); + } + base.opacity = 0.9; + let mut below = Layer::raster("clip sibling below", fmt); + if let Some(s) = below.surface_mut() { + s.fill_rect(band, &[0.2, 0.7, 0.4, 0.6]); + } + below.clipped = true; + below.opacity = 0.5; + let mut target = Layer::raster("clipped target", fmt); + if let Some(s) = target.surface_mut() { + s.fill_rect(region, &[0.8, 0.3, 0.2, 0.9]); + s.fill_rect(Rect::from_xywh(region.x0, region.y0, region.width() / 3, region.height() / 2), &[0.1, 0.2, 0.9, 1.0]); + } + target.clipped = true; + target.opacity = 0.8; + target.blend = BlendMode::Multiply; + if styled { + target.effects.items.push(Effect::ColorOverlay { common: FxCommon::new(BlendMode::Normal, 0.5), color: Color::rgba(0.2, 0.8, 0.4, 1.0) }); + } + let id = target.id; + let mut above = Layer::raster("clip sibling above", fmt); + if let Some(s) = above.surface_mut() { + s.fill_rect(band, &[0.5, 0.5, 0.9, 0.4]); + } + above.clipped = true; + above.opacity = 0.6; + doc.layers = vec![base, below, target, above]; + (doc, id) +} + +/// The old CPU path builds an adjusted proxy and flattens the whole document for each change. +fn cpu_adjust_preview_case(b: &mut Bench, doc: &Document, target: LayerId, name: &str, k: u32) { + let mut v = 0; + b.time(name, |_| { + v = (v + 7) % 120; + let t = Instant::now(); + let p = photocraft_ui_egui::filter_dialog::preview_document( + doc, + Some(target), + "image.adjustments.curves", + &json!({"points": [[0, 0], [100, 100 + v], [255, 255]]}), + k, + ) + .expect("preview"); + std::hint::black_box(photocraft_compose::flatten(&p)); + ms(t) + }); +} + +/// The CPU reference rows: the old proxy at the desktop factor, and (for path-reported rows) at +/// the 100 % and zoomed-out resolutions the canvas would use, so the layer path compares fairly. +fn cpu_preview_rows(b: &mut Bench, doc: &Document, target: LayerId, name: &str, sel: &str, report_path: bool) { + cpu_adjust_preview_case(b, doc, target, &format!("{name}, CPU proxy preview{sel}"), photocraft_ui_egui::proxy::factor(doc)); + if report_path { + let zoom = 1600.0 / doc.size.width as f32 * 2.0; + cpu_adjust_preview_case(b, doc, target, &format!("{name}, CPU proxy preview 100 % view{sel}"), 1); + cpu_adjust_preview_case( + b, + doc, + target, + &format!("{name}, CPU proxy preview zoomed out{sel}"), + photocraft_ui_egui::proxy::view_factor(photocraft_ui_egui::proxy::factor(doc), zoom), + ); + } +} + +/// Compare CPU and layer previews, keeping session setup outside the settings-change samples. +fn adjust_preview_case(b: &mut Bench, s: &Session, target: LayerId, name: &str, sel: &str, report_path: bool) { + let doc = &s.active().expect("doc").doc; + let (w, h) = (doc.size.width, doc.size.height); + let zoom = 1600.0 / w as f32 * 2.0; + let k = photocraft_ui_egui::adjust_preview::proxy_factor(zoom); + if let Some(why) = photocraft_ui_egui::adjust_preview::unsupported(doc, target) { + println!(" ({name}{sel}: no layer preview: {why}; CPU reference rows only)"); + cpu_preview_rows(b, doc, target, name, sel, report_path); + return; + } + cpu_preview_rows(b, doc, target, name, sel, report_path); + // Once per dialog session (the app caches both per document revision). + let (base, region) = { + let st = s.active().expect("doc"); + let t = Instant::now(); + let base = photocraft_ui_egui::adjust_preview::base_document(&st.doc, target).expect("base"); + let region = photocraft_ui_egui::adjust_preview::region(&st.doc, target); + println!(" ({name}{sel}: layer preview session setup: {:.1} ms, region {region:?})", ms(t)); + (base, region) + }; + let mut v = 0; + b.time_path(&format!("{name}, layer preview{sel}"), report_path, |b| { + v = (v + 7) % 120; + let t = Instant::now(); + let p = photocraft_ui_egui::adjust_preview::with_settings(&base, "curves", &json!({"points": [[0, 0], [100, 100 + v], [255, 255]]})).expect("preview"); + let build = ms(t); + let (render, path) = b.render_preview(&p, region); + (build + render, path) + }); + // Zoomed in, the canvas composites the region's visible part (here a 2560×1440 view at + // 100 %) and catches up elsewhere when the view moves. + let view = Rect::from_xywh(w as i32 / 3, h as i32 / 3, 2560, 1440).intersect(®ion); + let mut v = 0; + b.time_path(&format!("{name}, layer preview 100 % view{sel}"), report_path, |b| { + v = (v + 7) % 120; + let t = Instant::now(); + let p = photocraft_ui_egui::adjust_preview::with_settings(&base, "curves", &json!({"points": [[0, 0], [100, 100 + v], [255, 255]]})).expect("preview"); + let build = ms(t); + let (render, path) = b.render_preview(&p, view); + (build + render, path) + }); + // Zoomed out (fit to a 1600-pixel-wide view on a 2× display: k = 2 for 36 MP), the canvas + // previews on a reduced copy built once per session, unless effects or vector masks make + // that proxy inaccurate (the same eligibility check as adjust_preview::gpu_proxy). + let k = if photocraft_compose::proxy::proxy_faithful(&base) { k } else { 1 }; + let t = Instant::now(); + let proxy = photocraft_ui_egui::adjust_preview::proxy_base(&base, k); + println!(" ({name}{sel}: zoomed-out proxy k {k} setup: {:.1} ms)", ms(t)); + let kk = k as i32; + let pregion = Rect::new(region.x0 / kk, region.y0 / kk, region.x1 / kk + 1, region.y1 / kk + 1).intersect(&proxy.bounds()); + let mut v = 0; + b.time_path(&format!("{name}, layer preview zoomed out{sel}"), report_path, |b| { + v = (v + 7) % 120; + let t = Instant::now(); + let p = photocraft_ui_egui::adjust_preview::proxy_with_settings(&proxy, "curves", &json!({"points": [[0, 0], [100, 100 + v], [255, 255]]})) + .expect("preview"); + let build = ms(t); + let (render, path) = b.render_preview(&p, pregion); + (build + render, path) + }); +} + +/// The independent clipped-preview fixtures: unstyled (layer/GPU path) and styled (CPU fallback). +/// Both use their own session, so `--only clipped` never touches the big photo, PSD or gradient +/// setup and the full run keeps its existing row order. +fn clipped_preview_group(b: &mut Bench, w: u32, h: u32) { + for (styled, label) in [(false, "clipped target"), (true, "clipped styled target")] { + let t = Instant::now(); + let (doc, target) = clipped_document(w, h, styled); + let mut cs = Session::new(); + cs.open_document(doc, None); + exec(&mut cs, "layer.select", json!({"layer": target.0})); + println!(" ({label} fixture setup: {:.1} ms)", ms(t)); + for selection in [false, true] { + if selection { + exec(&mut cs, "select.rect", json!({"x": w / 4, "y": h / 4, "width": w / 2, "height": h / 2, "ellipse": true})); + } + let sel = if selection { ", selection" } else { "" }; + adjust_preview_case(b, &cs, target, &format!("Curves dialog change, {label}"), sel, true); + if selection { + exec(&mut cs, "select.deselect", json!({})); + } + } + } +} + +/// Write the `--json` report of the rows measured so far (shared by the full run and the early +/// `--only clipped` path). +fn write_report(b: &Bench, args: &[String], w: u32, h: u32) { + let Some(out) = arg(args, "--json") else { return }; + let rows: Vec = b + .rows + .iter() + .zip(&b.extra) + .map(|((n, med, min, max, cold), extra)| { + let mut r = json!({"name": n, "median_ms": med, "min_ms": min, "max_ms": max, "cold_ms": cold}); + // For `cargo xtask perf`: raw samples (it computes p50 / p95) and peak RSS. + r["samples_ms"] = json!(&extra.samples); + r["peak_rss_bytes"] = json!(extra.peak_rss); + r["minor_faults_per_run"] = json!(extra.faults); + if let Some(p) = extra.warm_path { + r["warm_path"] = json!(p); + } + if let Some(p) = extra.cold_path { + r["cold_path"] = json!(p); + } + r + }) + .collect(); + let context = json!({"gpu_adapter": b.gpu.as_ref().map(|g| g.adapter_name.as_str())}); + let report = json!({"width": w, "height": h, "gpu": b.gpu.is_some(), "context": context, "rows": rows}); + std::fs::write(&out, serde_json::to_string_pretty(&report).unwrap()).expect("write json"); +} + +/// Run the selected benchmarks, handling exact `--only clipped` before unrelated fixture setup. fn main() { let args: Vec = std::env::args().collect(); let (w, h) = @@ -188,6 +433,15 @@ fn main() { }; println!("document {w}×{h} ({:.1} MP), 8-bit RGB, {} reps, {}", (w * h) as f64 / 1e6, reps, if b.gpu.is_some() { "GPU canvas" } else { "CPU canvas" }); + // `--only clipped` measures just the independent clipped fixtures, without building the + // 36 MP photo, encoding JPEG, or opening/exporting PSD. That keeps the run's retained + // memory lower and avoids unrelated rows that `--only` would otherwise execute once. + if arg(&args, "--only").as_deref() == Some("clipped") { + clipped_preview_group(&mut b, w, h); + write_report(&b, &args, w, h); + return; + } + // ---- open / save / export ---------------------------------------------------------------- let img = photo(w, h); let mut jpeg = Vec::new(); @@ -273,105 +527,15 @@ fn main() { exec(&mut s, "select.rect", json!({"x": w / 4, "y": h / 4, "width": w / 2, "height": h / 2, "ellipse": true})); } let sel = if selection { ", selection" } else { "" }; - let mut v = 0; - b.time(&format!("Curves dialog change, CPU proxy preview{sel}"), |_| { - v = (v + 7) % 120; - let st = s.active().expect("doc"); - let k = photocraft_ui_egui::proxy::factor(&st.doc); - let t = Instant::now(); - let p = photocraft_ui_egui::filter_dialog::preview_document( - &st.doc, - st.active_layer, - "image.adjustments.curves", - &json!({"points": [[0, 0], [100, 100 + v], [255, 255]]}), - k, - ) - .expect("preview"); - std::hint::black_box(photocraft_compose::flatten(&p)); - ms(t) - }); - // Once per dialog session (the app caches both per document revision). - let (base, region) = { - let st = s.active().expect("doc"); - let target = st.active_layer.expect("layer"); - let t = Instant::now(); - let base = photocraft_ui_egui::adjust_preview::base_document(&st.doc, target).expect("base"); - let region = photocraft_ui_egui::adjust_preview::region(&st.doc, target); - println!(" (layer preview session setup: {:.1} ms, region {region:?})", ms(t)); - (base, region) - }; - b.time(&format!("Curves dialog change, layer preview{sel}"), |b| { - v = (v + 7) % 120; - let t = Instant::now(); - let p = - photocraft_ui_egui::adjust_preview::with_settings(&base, "curves", &json!({"points": [[0, 0], [100, 100 + v], [255, 255]]})).expect("preview"); - let build = ms(t); - let t = Instant::now(); - match &mut b.gpu { - Some(g) => { - if g.comp.render(&g.device, &g.queue, &p, region, |_, _| {}).is_err() { - std::hint::black_box(photocraft_compose::render(&p, region)); - } - let _ = g.device.poll(wgpu::PollType::Wait { submission_index: None, timeout: None }); - } - None => { - std::hint::black_box(photocraft_compose::render(&p, region)); - } - } - build + ms(t) - }); - // Zoomed in, the canvas composites the region's visible part (here a 2560×1440 view at - // 100 %) and catches up elsewhere when the view moves. - let view = Rect::from_xywh(w as i32 / 3, h as i32 / 3, 2560, 1440).intersect(®ion); - b.time(&format!("Curves dialog change, layer preview 100 % view{sel}"), |b| { - v = (v + 7) % 120; - let t = Instant::now(); - let p = - photocraft_ui_egui::adjust_preview::with_settings(&base, "curves", &json!({"points": [[0, 0], [100, 100 + v], [255, 255]]})).expect("preview"); - match &mut b.gpu { - Some(g) => { - if g.comp.render(&g.device, &g.queue, &p, view, |_, _| {}).is_err() { - std::hint::black_box(photocraft_compose::render(&p, view)); - } - let _ = g.device.poll(wgpu::PollType::Wait { submission_index: None, timeout: None }); - } - None => { - std::hint::black_box(photocraft_compose::render(&p, view)); - } - } - ms(t) - }); - // Zoomed out (fit to a 1600-pixel-wide view on a 2× display: k = 2 for 36 MP), the canvas - // previews on a reduced copy built once per session. - let zoom = 1600.0 / w as f32 * 2.0; - let k = photocraft_ui_egui::adjust_preview::proxy_factor(zoom); - let t = Instant::now(); - let proxy = photocraft_ui_egui::adjust_preview::proxy_base(&base, k); - println!(" (zoomed-out proxy k {k} setup: {:.1} ms)", ms(t)); - let kk = k as i32; - let pregion = Rect::new(region.x0 / kk, region.y0 / kk, region.x1 / kk + 1, region.y1 / kk + 1).intersect(&proxy.bounds()); - b.time(&format!("Curves dialog change, layer preview zoomed out{sel}"), |b| { - v = (v + 7) % 120; - let t = Instant::now(); - let p = photocraft_ui_egui::adjust_preview::proxy_with_settings(&proxy, "curves", &json!({"points": [[0, 0], [100, 100 + v], [255, 255]]})) - .expect("preview"); - match &mut b.gpu { - Some(g) => { - if g.comp.render(&g.device, &g.queue, &p, pregion, |_, _| {}).is_err() { - std::hint::black_box(photocraft_compose::render(&p, pregion)); - } - let _ = g.device.poll(wgpu::PollType::Wait { submission_index: None, timeout: None }); - } - None => { - std::hint::black_box(photocraft_compose::render(&p, pregion)); - } - } - ms(t) - }); + let target = s.active().expect("doc").active_layer.expect("layer"); + adjust_preview_case(&mut b, &s, target, "Curves dialog change", sel, false); if selection { exec(&mut s, "select.deselect", json!({})); } } + // The same case on a clipped target (#7487): the preview isolates it in a group. A styled + // target keeps the CPU proxy (see `adjust_preview_case`), so only its CPU rows are measured. + clipped_preview_group(&mut b, w, h); exec(&mut s, "layer.newAdjustmentLayer.levels", json!({"inBlack": 10, "inWhite": 240, "gamma": 1.1})); let mut g = 1.0; b.time("Levels layer tweak (gamma) + full refresh", |b| { @@ -474,21 +638,107 @@ fn main() { ms(t) + b.refresh(&s, false) }); - if let Some(out) = arg(&args, "--json") { - let rows: Vec = b - .rows - .iter() - .zip(&b.extra) - .map(|((n, med, min, max, cold), (samples, rss, faults))| { - let mut r = json!({"name": n, "median_ms": med, "min_ms": min, "max_ms": max, "cold_ms": cold}); - // For `cargo xtask perf`: raw samples (it computes p50 / p95) and peak RSS. - r["samples_ms"] = json!(samples); - r["peak_rss_bytes"] = json!(rss); - r["minor_faults_per_run"] = json!(faults); - r - }) - .collect(); - std::fs::write(&out, serde_json::to_string_pretty(&json!({"width": w, "height": h, "gpu": b.gpu.is_some(), "rows": rows})).unwrap()) - .expect("write json"); + write_report(&b, &args, w, h); +} + +/// The `--json` reporting path, exercised with fake timings: CI runs integration tests, not +/// examples, so these live here and are included by `tests/it/main.rs`. +#[cfg(test)] +mod reporting_tests { + use super::*; + + fn bench(reps: usize) -> Bench { + Bench { + gpu: None, + reps, + only: None, + rows: Vec::new(), + extra: Vec::new(), + rss: photocraft_testkit::perf::RssSampler::start(std::time::Duration::from_millis(2)), + } + } + + /// Write `b`'s report to a temporary file and return its rows. + fn report_rows(b: &Bench) -> Vec { + let dir = tempfile::tempdir().expect("tempdir"); + let path = dir.path().join("report.json"); + let args = ["--json".to_string(), path.to_string_lossy().into_owned()]; + write_report(b, &args, 40, 32); + let text = std::fs::read_to_string(&path).expect("read report"); + serde_json::from_str::(&text).expect("report json")["rows"].as_array().expect("report rows").clone() + } + + /// A cold CPU fallback must not relabel GPU warm samples or sort their raw timings. + #[test] + fn a_cold_cpu_fallback_keeps_the_warm_gpu_path() { + let mut b = bench(3); + let mut call = 0; + b.time_path("row", true, |_| { + call += 1; + match call { + 1 => (10.0, "CPU fallback"), + 2 => (24.0, "GPU"), + 3 => (22.0, "GPU"), + _ => (23.0, "GPU"), + } + }); + let rows = report_rows(&b); + let row = &rows[0]; + assert_eq!(row["name"], "row (GPU)"); + assert_eq!(row["median_ms"], 23.0); + assert_eq!(row["min_ms"], 22.0); + assert_eq!(row["max_ms"], 24.0); + assert_eq!(row["cold_ms"], 10.0); + assert_eq!(row["cold_path"], "CPU fallback"); + assert_eq!(row["warm_path"], "GPU"); + assert_eq!(row["samples_ms"], json!([24.0, 22.0, 23.0])); + } + + /// Retain a warm fallback after recovery without relabeling the cold sample. + #[test] + fn any_warm_fallback_labels_the_warm_row_and_keeps_the_cold_path() { + let mut b = bench(3); + let mut call = 0; + b.time_path("row", true, |_| { + call += 1; + match call { + 1 => (5.0, "GPU"), + 2 => (20.0, "GPU"), + 3 => (21.0, "CPU fallback"), + _ => (22.0, "GPU"), + } + }); + let rows = report_rows(&b); + let row = &rows[0]; + assert_eq!(row["name"], "row (CPU fallback)"); + assert_eq!(row["median_ms"], 21.0); + assert_eq!(row["cold_ms"], 5.0); + assert_eq!(row["cold_path"], "GPU"); + assert_eq!(row["warm_path"], "CPU fallback"); + assert_eq!(row["samples_ms"], json!([20.0, 21.0, 22.0])); + } + + /// Preserve the legacy row schema and sample alignment when rendering paths are not requested. + #[test] + fn ordinary_rows_keep_their_names_and_have_no_path_metadata() { + let mut b = bench(2); + b.time("plain row", |_| 4.0); + b.time("other row", |_| 6.0); + let rows = report_rows(&b); + assert_eq!(rows.len(), 2); + assert_eq!(rows[0]["name"], "plain row"); + assert_eq!(rows[0]["median_ms"], 4.0); + assert_eq!(rows[0]["cold_ms"], 4.0); + assert_eq!(rows[0]["samples_ms"], json!([4.0, 4.0])); + for key in ["median_ms", "min_ms", "max_ms", "cold_ms", "samples_ms", "peak_rss_bytes", "minor_faults_per_run"] { + assert!(rows[0].get(key).is_some(), "{key} missing: {}", rows[0]); + } + assert!(rows[0].get("warm_path").is_none(), "{}", rows[0]); + assert!(rows[0].get("cold_path").is_none(), "{}", rows[0]); + assert_eq!(rows[1]["name"], "other row"); + assert_eq!(rows[1]["min_ms"], 6.0); + assert_eq!(rows[1]["cold_ms"], 6.0); + assert!(rows[1].get("warm_path").is_none(), "{}", rows[1]); + assert!(rows[1].get("cold_path").is_none(), "{}", rows[1]); } } diff --git a/crates/ui-egui/src/dialogs/adjust_preview.rs b/crates/ui-egui/src/dialogs/adjust_preview.rs index 49cecdc51b..c2ffeb1b1a 100644 --- a/crates/ui-egui/src/dialogs/adjust_preview.rs +++ b/crates/ui-egui/src/dialogs/adjust_preview.rs @@ -16,12 +16,18 @@ //! document (with the preview layer) and the region are built once per dialog session; a change //! only swaps the layer's adjustment in a shallow copy. //! +//! An already-clipped, unstyled target becomes a group in the preview document only: its raw +//! raster and the temporary adjustment are children, while its external compositing properties +//! stay on the group. This applies the adjustment before the target blends into its clipping +//! group, as the destructive command does. +//! //! Cases where an adjustment-layer composite differs from the destructive result, or where it //! would not be cheap, keep the CPU proxy preview (`canvas::ensure_filter_preview`), see //! [`unsupported`]: //! - the target is not a plain pixel layer (smart object, type, shape, group: the command edits -//! a smart object's filters or refuses), or is itself clipped (a clipped adjustment would act on -//! the whole clipping group, not just the target); +//! a smart object's filters or refuses); +//! - a clipped target has enabled effects on itself or an enclosing group (their effect shape +//! maps would rebuild on the UI thread for every settings change); //! - the target uses Blend If (its "Current Layer" ranges test the layer's adjusted colour); //! - the target's untouched area is visible (a surface without alpha whose content doesn't cover //! the canvas: the command only edits content); @@ -38,7 +44,7 @@ use std::sync::Arc; use photocraft_color::ColorMode; -use photocraft_doc::{Adjustment, DocId, Document, Layer, LayerContent, LayerId, LayerMask}; +use photocraft_doc::{Adjustment, DocId, Document, Group, Layer, LayerContent, LayerId, LayerMask}; use photocraft_geom::Rect; use serde_json::Value; @@ -47,6 +53,9 @@ use crate::PhotocraftApp; /// Id of the temporary preview layer. Fixed so the GPU compositor keeps its mask (the selection) /// resident from one change to the next. Never reaches a committed document. pub const PREVIEW_LAYER: LayerId = LayerId(u64::MAX - 77); +/// Id of the raw-raster carrier inside a clipped target's preview group (see [`base_document`]). +/// Fixed like [`PREVIEW_LAYER`] so its GPU textures stay resident across settings changes. +const CARRIER_LAYER: LayerId = LayerId(u64::MAX - 76); /// Tag of the preview keys handed to the canvas (`canvas::display_doc`). const TAG: u64 = 1 << 59; @@ -54,15 +63,19 @@ const TAG: u64 = 1 << 59; /// settings hash. const SESSION_SHIFT: u32 = 40; -/// Why `target` in `doc` can't preview as a clipped adjustment layer, or None when it can. +/// Why `target` in `doc` can't preview through the temporary layer path, or None when it can. pub fn unsupported(doc: &Document, target: LayerId) -> Option<&'static str> { if !matches!(doc.mode, ColorMode::Rgb | ColorMode::Grayscale) { return Some("colour mode composites through display RGB"); } let Some(l) = doc.layer(target) else { return Some("no target layer") }; let LayerContent::Raster(s) = &l.content else { return Some("not a pixel layer") }; - if l.clipped { - return Some("the target is clipped"); + // A clipped target is previewed inside a new group. If the target or a group around it is + // styled, every slider change invalidates that group's effect shape maps, and rebuilding + // them is CPU work on the UI thread. The CPU proxy has its own worker, so styled clips keep + // it; an unrelated styled sibling doesn't affect this target's clip. + if l.clipped && (photocraft_compose::effects::has_effects(l) || styled_ancestor(doc, target)) { + return Some("clipped target or enclosing group has layer effects"); } if photocraft_compose::blend_if_active(l, doc.mode) { return Some(tl!("Blend If")); @@ -74,6 +87,12 @@ pub fn unsupported(doc: &Document, target: LayerId) -> Option<&'static str> { None } +/// Whether a group enclosing `target` has enabled layer effects. +fn styled_ancestor(doc: &Document, target: LayerId) -> bool { + let Some(path) = doc.path_of(target) else { return false }; + (1..path.len()).any(|n| path.get(..n).and_then(|p| doc.layer_at(p)).is_some_and(photocraft_compose::effects::has_effects)) +} + /// Whether `adj` maps gray to gray. Others make colour in a grayscale document, which the command /// folds back to gray per pixel while the composite carries it to the end. fn keeps_gray(adj: &Adjustment) -> bool { @@ -93,18 +112,32 @@ fn keeps_gray(adj: &Adjustment) -> bool { /// `doc` with a placeholder temporary adjustment layer clipped to `target` (the selection as its /// mask): built once per dialog session, then [`with_settings`] per change. Errors when the target /// isn't eligible ([`unsupported`]). +/// +/// An already-clipped target becomes a group containing its raw raster and the adjustment, so +/// only its own pixels are adjusted. Its id and external compositing properties stay on the group. pub fn base_document(doc: &Document, target: LayerId) -> Result { if let Some(why) = unsupported(doc, target) { return Err(why.into()); } - let mut l = Layer::new(tl!("Adjustment Preview"), LayerContent::Adjustment(Adjustment::Invert)); - l.id = PREVIEW_LAYER; - l.clipped = true; + let mut preview = Layer::new(tl!("Adjustment Preview"), LayerContent::Adjustment(Adjustment::Invert)); + preview.id = PREVIEW_LAYER; + preview.clipped = true; if let Some(sel) = &doc.selection { - l.mask = Some(LayerMask { surface: sel.clone(), enabled: true, linked: false, density: 1.0, feather: 0.0 }); + preview.mask = Some(LayerMask { surface: sel.clone(), enabled: true, linked: false, density: 1.0, feather: 0.0 }); } let mut out = doc.clone(); - out.insert_above(Some(target), l); + if !out.layer(target).is_some_and(|l| l.clipped) { + out.insert_above(Some(target), preview); + return Ok(out); + } + let l = out.layer_mut(target).ok_or("no target layer")?; + let content = std::mem::replace(&mut l.content, LayerContent::Group(Group { children: Vec::new(), expanded: true, artboard: None, frame_stroke: None })); + let LayerContent::Raster(surface) = content else { return Err("not a pixel layer".into()) }; + let mut carrier = Layer::new(l.name.clone(), LayerContent::Raster(surface)); + carrier.id = CARRIER_LAYER; + if let LayerContent::Group(g) = &mut l.content { + g.children = vec![carrier, preview]; + } Ok(out) } diff --git a/crates/ui-egui/src/dialogs/adjust_preview/tests.rs b/crates/ui-egui/src/dialogs/adjust_preview/tests.rs index 40418c202b..6f8137f802 100644 --- a/crates/ui-egui/src/dialogs/adjust_preview/tests.rs +++ b/crates/ui-egui/src/dialogs/adjust_preview/tests.rs @@ -1,6 +1,6 @@ use super::*; use photocraft_color::{BlendMode, Color, PixelFormat, SampleType}; -use photocraft_doc::Size; +use photocraft_doc::{BlendIf, BlendRange, Effect, FxCommon, Size}; use serde_json::json; const KINDS: [&str; 14] = crate::adjust_editors::KINDS; @@ -89,6 +89,63 @@ pub(crate) fn document(mode: ColorMode, depth: SampleType, selection: bool) -> ( (doc, id) } +/// A target in the middle of a clipping group, nested two groups deep: a base, a clipped layer +/// below the target, the target (semi-transparent noise, 80 % Multiply, a mask) and a clipped +/// layer above it. The target is clipped, so its preview must isolate it from the group. It is +/// unstyled, which is the path the preview accelerates. +pub(crate) fn clipped_document(mode: ColorMode, depth: SampleType, selection: bool) -> (Document, LayerId) { + let (w, h) = (40u32, 32u32); + let mut doc = Document::with_background("t", Size::new(w, h), mode, depth, Color::rgba(0.7, 0.6, 0.5, 1.0)); + let fmt = doc.pixel_format(); + if let Some(s) = doc.layers[0].surface_mut() { + fill_noise(s, Rect::from_xywh(0, 0, w, h / 2), 3, 1.0); + } + let mut base = Layer::raster("clip base", fmt); + if let Some(s) = base.surface_mut() { + fill_noise(s, Rect::from_xywh(0, 0, w, h), 5, 0.35); + } + base.opacity = 0.9; + let mut below = Layer::raster("below", fmt); + if let Some(s) = below.surface_mut() { + fill_noise(s, Rect::from_xywh(2, 18, 30, 10), 7, 0.4); + } + below.clipped = true; + below.opacity = 0.5; + let mut target = Layer::raster("target", fmt); + if let Some(s) = target.surface_mut() { + fill_noise(s, Rect::from_xywh(4, 2, 30, 26), 11, 0.2); + } + target.clipped = true; + target.opacity = 0.8; + target.blend = BlendMode::Multiply; + let mut m = LayerMask::reveal_all(); + m.surface.fill_rect(Rect::from_xywh(0, 0, 10, 10), &[0.3]); + target.mask = Some(m); + let id = target.id; + let mut above = Layer::raster("above", fmt); + if let Some(s) = above.surface_mut() { + fill_noise(s, Rect::from_xywh(20, 10, 12, 12), 17, 0.5); + } + above.clipped = true; + above.opacity = 0.6; + let mut inner = Layer::group("inner", vec![base, below, target, above]); + inner.opacity = 0.95; + let outer = Layer::group("outer", vec![inner]); + let mut top = Layer::raster("top", fmt); + if let Some(s) = top.surface_mut() { + fill_noise(s, Rect::from_xywh(0, 20, 40, 12), 23, 0.0); + } + top.blend = BlendMode::Screen; + doc.layers.extend([outer, top]); + if selection { + let mut sel = photocraft_raster::Surface::new(PixelFormat::GRAY8); + sel.fill_rect(Rect::from_xywh(8, 4, 24, 20), &[1.0]); + sel.fill_rect(Rect::from_xywh(8, 4, 6, 20), &[0.5]); + doc.selection = Some(sel); + } + (doc, id) +} + /// The document after running the real destructive command on `target`. pub(crate) fn destructive(doc: &Document, target: LayerId, kind: &str, params: &Value) -> Option { let mut s = photocraft_engine::Session::new(); @@ -144,6 +201,121 @@ fn preview_composite_matches_the_destructive_command() { assert!(fails.is_empty(), "{}", fails.join("\n")); } +/// A clipped target is adjusted through an isolating group in the preview only: its composite +/// matches the destructive command for every kind, depth, mode and selection. +#[test] +fn a_clipped_target_previews_like_the_destructive_command() { + let tol = 1.0 / 255.0 + 1e-4; + let mut worst = (0.0f32, String::new()); + let mut fails = Vec::new(); + for mode in [ColorMode::Rgb, ColorMode::Grayscale] { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + for selection in [false, true] { + let (doc, target) = clipped_document(mode, depth, selection); + assert!(unsupported(&doc, target).is_none(), "a clipped raster target is supported"); + for kind in KINDS { + for params in [sample(kind), json!({})] { + let what = format!("{kind} {mode:?} {depth:?} selection {selection} {params}"); + let want = destructive(&doc, target, kind, ¶ms); + let got = preview_document(&doc, target, kind, ¶ms); + let gray_colour = mode == ColorMode::Grayscale && GRAY_COLOUR.contains(&kind); + let (Some(want), Ok(got)) = (want, got) else { + assert!(gray_colour, "{what}: command and preview disagree on validity"); + continue; + }; + assert!(!gray_colour, "{what}: should use the proxy preview"); + let d = max_diff(&got, &want); + if d > worst.0 { + worst = (d, what.clone()); + } + if d > tol { + fails.push(format!("{what}: preview differs from the command by {:.2}/255", d * 255.0)); + } + } + } + } + } + } + eprintln!("worst clipped-target preview difference {:.3}/255 ({})", worst.0 * 255.0, worst.1); + assert!(fails.is_empty(), "{}", fails.join("\n")); +} + +/// The isolating preview keeps the target's place and compositing properties, puts its raw raster +/// and the adjustment inside its group, and leaves the committed document untouched. +#[test] +fn a_clipped_target_preview_keeps_its_place_and_properties() { + let (mut doc, target) = clipped_document(ColorMode::Rgb, SampleType::U8, true); + // Nondefaults the transformed group must keep, not just Photoshop's defaults. + let l = doc.layer_mut(target).unwrap(); + l.fill_opacity = 0.4; + l.excluded_channels = 0b101; + l.advanced.blend_clipped = false; + let before = doc.layer(target).unwrap().clone(); + let path = doc.path_of(target).unwrap(); + let layer_count = doc.layer_count(); + let base = base_document(&doc, target).unwrap(); + let group = base.layer(target).unwrap(); + assert!(group.is_group(), "the target becomes an isolating group"); + assert_eq!(base.path_of(target), Some(path.clone()), "same place in the stack"); + assert_eq!(group.clipped, before.clipped); + assert_eq!((group.blend, group.opacity, group.fill_opacity), (before.blend, before.opacity, before.fill_opacity)); + assert_eq!(group.mask, before.mask); + assert_eq!(group.effects, before.effects); + assert_eq!(group.excluded_channels, before.excluded_channels); + assert_eq!(group.advanced, before.advanced); + let children = group.children().unwrap(); + assert_eq!(children.len(), 2); + assert_eq!(children[0].id, CARRIER_LAYER); + assert_eq!(children[0].surface(), before.surface(), "the raw raster is unchanged"); + assert_eq!(children[1].id, PREVIEW_LAYER); + assert!(children[1].clipped, "the adjustment clips to the raw raster"); + let mut preview_path = path; + preview_path.push(1); + assert_eq!(base.path_of(PREVIEW_LAYER), Some(preview_path), "inside the target's group"); + // The committed document is untouched. + assert_eq!(doc.layer_count(), layer_count); + assert!(doc.layer(PREVIEW_LAYER).is_none()); + assert!(doc.layer(CARRIER_LAYER).is_none()); + assert_eq!(doc.layer(target).unwrap().surface(), before.surface()); + // The reduced-resolution preview flips every id, including the carrier and the nested + // adjustment, so its textures stay separate and stable. + let proxy = proxy_base(&base, 2); + assert_ne!(proxy.id, base.id); + assert!(proxy.walk().iter().all(|(_, _, l)| base.layer(l.id).is_none() && l.id != PREVIEW_LAYER)); + assert!(proxy.layer(LayerId(PREVIEW_LAYER.0 ^ PROXY_LAYER_BIT)).is_some()); + assert!(proxy.layer(LayerId(CARRIER_LAYER.0 ^ PROXY_LAYER_BIT)).is_some()); +} + +/// The canvas damage region still bounds every change when the target is clipped: outside it the +/// preview composites like the committed document, within a tight premultiplied tolerance. +#[test] +fn a_clipped_target_preview_changes_nothing_outside_its_region() { + // The extra group's straight-alpha normalization introduces floating-point rounding. + // Allow 1e-6 outside the damage region, well below the 1/255 command-parity tolerance; + // the committed document itself must remain identical. + const OUTSIDE: f32 = 1e-6; + let pm = |p: &[f32; 4]| [p[0] * p[3], p[1] * p[3], p[2] * p[3], p[3]]; + for selection in [false, true] { + let (doc, target) = clipped_document(ColorMode::Rgb, SampleType::U8, selection); + let committed = doc.clone(); + let r = region(&doc, target); + assert!(!r.is_empty(), "the target can change something"); + let before = photocraft_compose::render(&doc, doc.bounds()); + for kind in KINDS { + let p = preview_document(&doc, target, kind, &sample(kind)).unwrap(); + let after = photocraft_compose::render(&p, doc.bounds()); + for (i, (a, b)) in before.px.iter().zip(&after.px).enumerate() { + let (x, y) = ((i % 40) as i32, (i / 40) as i32); + if !r.contains(x, y) { + let (pa, pb) = (pm(a), pm(b)); + assert!((0..4).all(|c| (pa[c] - pb[c]).abs() <= OUTSIDE), "{kind} selection {selection}: ({x}, {y}) outside {r:?}: {pa:?} vs {pb:?}"); + } + } + } + assert_eq!(doc, committed, "the committed document is unchanged"); + } +} + #[test] fn preview_leaves_the_document_alone_and_marks_its_layer() { let (doc, target) = document(ColorMode::Rgb, SampleType::U8, true); @@ -163,7 +335,14 @@ fn preview_leaves_the_document_alone_and_marks_its_layer() { fn ineligible_targets_use_the_proxy_preview() { let (mut doc, target) = document(ColorMode::Rgb, SampleType::U8, false); let clipped = doc.layers[2].id; - assert_eq!(unsupported(&doc, clipped), Some("the target is clipped")); + assert_eq!(unsupported(&doc, clipped), None, "a clipped raster target previews by isolating it"); + assert!(base_document(&doc, clipped).unwrap().layer(clipped).unwrap().is_group()); + // Blend If still needs the CPU proxy: its "This Layer" ranges test the adjusted colour. + let (mut blend_doc, blend_target) = clipped_document(ColorMode::Rgb, SampleType::U8, false); + let mut blend_if = BlendIf::default(); + blend_if.set(0, [BlendRange { black: [30, 30], white: [220, 220] }, BlendRange::FULL]); + blend_doc.layer_mut(blend_target).unwrap().blend_if = blend_if; + assert_eq!(unsupported(&blend_doc, blend_target), Some(tl!("Blend If"))); assert_eq!(unsupported(&doc, LayerId(u64::MAX - 1)), Some("no target layer")); doc.layers.push(Layer::new("adj", LayerContent::Adjustment(photocraft_doc::Adjustment::Invert))); let adj = doc.layers[4].id; @@ -183,6 +362,47 @@ fn ineligible_targets_use_the_proxy_preview() { assert!(preview_document(&gray, t, "hueSaturation", &json!({"lightness": 20})).is_ok()); } +/// A clipped target with enabled effects on itself or an enclosing group keeps the CPU proxy: +/// the preview's temporary group would rebuild the styled group's effect shape maps on the UI +/// thread for every settings change. Disabled effects and styled siblings elsewhere don't count. +#[test] +fn a_styled_clipped_target_keeps_the_proxy_preview() { + let overlay = || Effect::ColorOverlay { common: FxCommon::new(BlendMode::Normal, 0.5), color: Color::rgba(0.2, 0.8, 0.4, 1.0) }; + let (mut doc, target) = clipped_document(ColorMode::Rgb, SampleType::U8, false); + assert_eq!(unsupported(&doc, target), None, "the fixture target is unstyled"); + // An enabled effect on the target itself. + doc.layer_mut(target).unwrap().effects.items.push(overlay()); + let why = unsupported(&doc, target).expect("a styled clipped target keeps the proxy"); + assert!(why.contains("layer effects"), "{why}"); + assert!(base_document(&doc, target).is_err(), "no preview group for a styled clip"); + doc.layer_mut(target).unwrap().clipped = false; + assert_eq!(unsupported(&doc, target), None, "unclipped styled targets keep their existing path"); + doc.layer_mut(target).unwrap().clipped = true; + doc.layer_mut(target).unwrap().effects.enabled = false; + assert_eq!(unsupported(&doc, target), None, "the disabled master switch doesn't count"); + doc.layer_mut(target).unwrap().effects.enabled = true; + // Disabled effects cost nothing: still eligible. + if let Effect::ColorOverlay { common, .. } = &mut doc.layer_mut(target).unwrap().effects.items[0] { + common.enabled = false; + } + assert_eq!(unsupported(&doc, target), None, "disabled effects don't count"); + doc.layer_mut(target).unwrap().effects.items.clear(); + // An enabled effect on an enclosing group. + let inner = doc.layers[1].children().unwrap()[0].id; + doc.layer_mut(inner).unwrap().effects.items.push(overlay()); + assert!(unsupported(&doc, target).is_some(), "a styled enclosing group keeps the proxy"); + if let Effect::ColorOverlay { common, .. } = &mut doc.layer_mut(inner).unwrap().effects.items[0] { + common.enabled = false; + } + assert_eq!(unsupported(&doc, target), None, "a disabled group effect doesn't count"); + doc.layer_mut(inner).unwrap().effects.items.clear(); + // A styled sibling inside the same clipping group is unrelated. + let above = doc.layer(inner).unwrap().children().unwrap()[3].id; + doc.layer_mut(above).unwrap().effects.items.push(overlay()); + assert_eq!(unsupported(&doc, target), None, "an unrelated styled sibling doesn't reject the target"); + assert!(base_document(&doc, target).is_ok()); +} + /// The canvas recomposites only `region` between previews: nothing outside it may change. #[test] fn preview_changes_nothing_outside_its_region() { @@ -280,13 +500,42 @@ fn dialog_previews_through_the_layer() { } } +/// A clipped target still previews through the layer: the session keys, damage region and +/// settings swap work on its isolating group, and closing the dialog drops the preview. +#[test] +fn a_clipped_target_dialog_previews_through_the_layer() { + let (doc, target) = clipped_document(ColorMode::Rgb, SampleType::U16, true); + let mut app = app_with(doc.clone(), target); + let id = crate::adjust_dialog::open(&mut app, "image.adjustments.levels").unwrap(); + assert!(on_layer(&mut app, 0)); + let (first, k1) = display_doc(&mut app, 0).unwrap(); + assert!(first.layer(target).unwrap().is_group()); + assert!(first.layer(PREVIEW_LAYER).is_some()); + let (again, k1b) = display_doc(&mut app, 0).unwrap(); + assert!(Arc::ptr_eq(&first, &again) && k1 == k1b, "unchanged settings reuse the cached preview"); + app.ui.dialog_mut(id).unwrap().fields.insert("gamma".into(), json!(1.6)); + let (second, k2) = display_doc(&mut app, 0).unwrap(); + assert_ne!(k1, k2); + let st = app.session.active().unwrap(); + assert_eq!(switch_region(&app, st.doc.id, st.revision, k1, k2), Some(region(&doc, target))); + let params = crate::filter_dialog::params_of(&app.ui.dialog_mut(id).unwrap().fields); + let want = destructive(&doc, target, "levels", ¶ms).unwrap(); + assert!(max_diff(&second, &want) <= 1.0 / 255.0); + app.ui.dialog_mut(id).unwrap().fields.insert("__preview".into(), json!(false)); + assert!(display_doc(&mut app, 0).is_none()); + assert!(on_layer(&mut app, 0), "still the layer path, just hidden"); + app.ui.close_dialog(id); + assert!(display_doc(&mut app, 0).is_none() && !on_layer(&mut app, 0)); +} + /// Targets the layer path can't reproduce keep the CPU proxy preview. #[test] fn ineligible_dialogs_keep_the_proxy_preview() { - // A clipped target. - let (doc, _) = document(ColorMode::Rgb, SampleType::U8, false); - let clipped = doc.layers[2].id; - let mut app = app_with(doc, clipped); + // An adjustment-layer target. + let (mut doc, _) = document(ColorMode::Rgb, SampleType::U8, false); + doc.layers.push(Layer::new("adj", LayerContent::Adjustment(photocraft_doc::Adjustment::Invert))); + let adj = doc.layers[4].id; + let mut app = app_with(doc, adj); crate::adjust_dialog::open(&mut app, "image.adjustments.curves").unwrap(); assert!(!on_layer(&mut app, 0) && display_doc(&mut app, 0).is_none()); // An alpha-channel target. @@ -301,27 +550,28 @@ fn ineligible_dialogs_keep_the_proxy_preview() { assert!(!on_layer(&mut app, 0)); } -/// The proxy preview of a clipped target records why it doesn't preview through the layer +/// The proxy preview of an ineligible target records why it doesn't preview through the layer /// instead of dropping the reason (#7487). #[test] -fn a_clipped_adjustment_preview_records_why_it_uses_the_proxy() { - let (doc, _) = document(ColorMode::Rgb, SampleType::U8, false); - let clipped = doc.layers[2].id; - let mut app = app_with(doc, clipped); +fn an_ineligible_adjustment_preview_records_why_it_uses_the_proxy() { + let (mut doc, _) = document(ColorMode::Rgb, SampleType::U8, false); + doc.layers.push(Layer::new("adj", LayerContent::Adjustment(photocraft_doc::Adjustment::Invert))); + let adj = doc.layers[4].id; + let mut app = app_with(doc, adj); app.render_path.assume_gpu = true; let dialog = crate::adjust_dialog::open(&mut app, "image.adjustments.curves").unwrap(); assert!(!on_layer(&mut app, 0)); let doc = app.session.active().unwrap().doc.id.0; - assert_eq!(app.render_path.current(doc), Some(&crate::render_path::CpuReason::AdjustPreview("the target is clipped"))); + assert_eq!(app.render_path.current(doc), Some(&crate::render_path::CpuReason::AdjustPreview("not a pixel layer"))); let path = crate::render_path::inspect(&app); assert_eq!(path["current"]["kind"], "adjustPreview"); - assert!(path["current"]["detail"].as_str().is_some_and(|d| d.contains("the target is clipped")), "{path}"); + assert!(path["current"]["detail"].as_str().is_some_and(|d| d.contains("not a pixel layer")), "{path}"); // Asked again with nothing changed: no new entry. assert!(!on_layer(&mut app, 0)); assert_eq!(app.render_path.history().count(), 1); // A GPU refresh of the canvas while the dialog is open doesn't end the preview's reason. crate::render_path::note(&mut app, doc, crate::render_path::Path::Gpu { full: true }); - assert_eq!(app.render_path.current(doc), Some(&crate::render_path::CpuReason::AdjustPreview("the target is clipped"))); + assert_eq!(app.render_path.current(doc), Some(&crate::render_path::CpuReason::AdjustPreview("not a pixel layer"))); assert_eq!(app.render_path.history().count(), 1); // Closing the dialog ends the reason, though the canvas is still current (no refresh). app.ui.close_dialog(dialog); @@ -338,10 +588,11 @@ fn a_clipped_adjustment_preview_records_why_it_uses_the_proxy() { #[test] fn an_adjustment_preview_keeps_the_canvas_reason() { use crate::render_path::{CpuReason, Path}; - let (doc, _) = document(ColorMode::Rgb, SampleType::U8, false); - let clipped = doc.layers[2].id; + let (mut doc, _) = document(ColorMode::Rgb, SampleType::U8, false); + doc.layers.push(Layer::new("adj", LayerContent::Adjustment(photocraft_doc::Adjustment::Invert))); + let adj = doc.layers[4].id; let canvas_back_on_gpu = |app: &crate::PhotocraftApp| app.render_path.history().any(|c| !c.preview && c.reason.is_none()); - let mut app = app_with(doc.clone(), clipped); + let mut app = app_with(doc.clone(), adj); app.render_path.assume_gpu = true; let doc_id = app.session.active().unwrap().doc.id.0; let canvas = CpuReason::Unsupported("Advanced Blending on Layer 1".into()); @@ -357,7 +608,7 @@ fn an_adjustment_preview_keeps_the_canvas_reason() { assert!(!canvas_back_on_gpu(&app)); // A window without the GPU (`--safe-gpu`): the preview adds nothing, before or after closing. - let mut app = app_with(doc, clipped); + let mut app = app_with(doc, adj); app.render_path.assume_gpu = true; let doc_id = app.session.active().unwrap().doc.id.0; let cpu = CpuReason::CpuMode("--safe-gpu".into()); diff --git a/crates/ui-egui/tests/it/adjust_preview_gpu.rs b/crates/ui-egui/tests/it/adjust_preview_gpu.rs index 4e8aa8552c..59dc67c550 100644 --- a/crates/ui-egui/tests/it/adjust_preview_gpu.rs +++ b/crates/ui-egui/tests/it/adjust_preview_gpu.rs @@ -92,6 +92,47 @@ fn document(mode: ColorMode, depth: SampleType, selection: bool) -> (Document, L (doc, id) } +/// A target clipped in the middle of a clipping group inside two nested groups: the preview wraps +/// it in an isolating group so the adjustment runs on its raw raster, not the whole clip group. +fn clipped_document(mode: ColorMode, depth: SampleType, selection: bool) -> (Document, LayerId) { + let mut doc = Document::with_background("t", Size::new(48, 40), mode, depth, Color::rgba(0.7, 0.6, 0.5, 1.0)); + let fmt = doc.pixel_format(); + let mut base = Layer::raster("clip base", fmt); + noise(&mut base, Rect::from_xywh(0, 0, 48, 40), 5, 0.35); + base.opacity = 0.9; + let mut below = Layer::raster("below", fmt); + noise(&mut below, Rect::from_xywh(2, 22, 38, 12), 7, 0.4); + below.clipped = true; + below.opacity = 0.5; + let mut target = Layer::raster("target", fmt); + noise(&mut target, Rect::from_xywh(4, 2, 38, 34), 11, 0.2); + target.clipped = true; + target.opacity = 0.8; + target.blend = BlendMode::Multiply; + let mut m = LayerMask::reveal_all(); + m.surface.fill_rect(Rect::from_xywh(0, 0, 12, 12), &[0.3]); + target.mask = Some(m); + let id = target.id; + let mut above = Layer::raster("above", fmt); + noise(&mut above, Rect::from_xywh(24, 12, 16, 16), 17, 0.5); + above.clipped = true; + above.opacity = 0.6; + let mut inner = Layer::group("inner", vec![base, below, target, above]); + inner.opacity = 0.95; + let outer = Layer::group("outer", vec![inner]); + let mut top = Layer::raster("top", fmt); + noise(&mut top, Rect::from_xywh(0, 28, 48, 12), 23, 0.0); + top.blend = BlendMode::Screen; + doc.layers.extend([outer, top]); + if selection { + let mut sel = photocraft_raster::Surface::new(PixelFormat::GRAY8); + sel.fill_rect(Rect::from_xywh(8, 4, 30, 26), &[1.0]); + sel.fill_rect(Rect::from_xywh(8, 4, 8, 26), &[0.5]); + doc.selection = Some(sel); + } + (doc, id) +} + fn sample(kind: &str) -> Value { match kind { "brightnessContrast" => json!({"brightness": 40, "contrast": 30}), @@ -107,10 +148,16 @@ fn sample(kind: &str) -> Value { "posterize" => json!({"levels": 3}), "threshold" => json!({"level": 90}), "gradientMap" => json!({"stops": [[0, "#200040"], [1, "#ffd080"]]}), + "selectiveColor" => json!({"method": "absolute", "reds": [10, -20, 30, 5], "blues": [-15, 0, 25, 0]}), _ => json!({}), } } +/// Grayscale kinds that make colour from gray: only these preview on the CPU proxy, so a +/// Grayscale preview error is expected only for them. The Hue/Saturation sample leaves +/// `colorize` off, so it stays gray. +const GRAY_COLOUR: [&str; 7] = ["vibrance", "colorBalance", "blackWhite", "photoFilter", "channelMixer", "gradientMap", "selectiveColor"]; + fn pm(p: &[f32; 4]) -> [f32; 4] { [p[0] * p[3], p[1] * p[3], p[2] * p[3], p[3]] } @@ -140,10 +187,14 @@ fn preview_layer_composites_on_the_gpu_like_the_command() { let (doc, target) = document(mode, depth, selection); for kind in photocraft_ui_egui::adjust_editors::KINDS { let params = sample(kind); - let Ok(preview) = preview_document(&doc, target, kind, ¶ms) else { - // Grayscale kinds that make colour preview on the CPU proxy. - assert_eq!(mode, ColorMode::Grayscale, "{kind}"); - continue; + let preview = match preview_document(&doc, target, kind, ¶ms) { + Ok(p) => p, + Err(e) => { + // Only grayscale kinds that make colour keep the CPU proxy preview. + assert_eq!(mode, ColorMode::Grayscale, "{kind}: {e}"); + assert!(GRAY_COLOUR.contains(&kind), "{kind} {mode:?}: unexpected preview error: {e}"); + continue; + } }; assert!(preview.layer(PREVIEW_LAYER).is_some()); let what = format!("{kind} {mode:?} {depth:?} selection {selection}"); @@ -170,6 +221,53 @@ fn preview_layer_composites_on_the_gpu_like_the_command() { eprintln!("worst GPU preview vs command: {:.3}/255", worst * 255.0); } +/// A clipped target's isolating-group preview composites on the GPU like the CPU reference and +/// like the destructive command's result. +#[test] +fn clipped_target_preview_composites_on_the_gpu_like_the_command() { + let _gpu = photocraft_testkit::gpu_lock(); + let Some((device, queue, mut comp)) = gpu() else { return }; + let (tol_gpu, tol_cmd) = (2.0 / 255.0, 2.5 / 255.0); + let mut worst = 0.0f32; + for mode in [ColorMode::Rgb, ColorMode::Grayscale] { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + for selection in [false, true] { + let (doc, target) = clipped_document(mode, depth, selection); + for kind in photocraft_ui_egui::adjust_editors::KINDS { + let params = sample(kind); + let preview = match preview_document(&doc, target, kind, ¶ms) { + Ok(p) => p, + Err(e) => { + // Only grayscale kinds that make colour keep the CPU proxy preview. + assert_eq!(mode, ColorMode::Grayscale, "{kind}: {e}"); + assert!(GRAY_COLOUR.contains(&kind), "{kind} {mode:?}: unexpected preview error: {e}"); + continue; + } + }; + assert!(preview.layer(PREVIEW_LAYER).is_some()); + assert!(preview.layer(target).unwrap().is_group(), "the clipped target is isolated"); + let what = format!("{kind} {mode:?} {depth:?} selection {selection}"); + let b = preview.bounds(); + let skip = if matches!(kind, "threshold" | "posterize") && depth != SampleType::U8 { (b.width() * b.height()) as usize / 100 } else { 0 }; + let on_gpu = render_to_vec(&mut comp, &device, &queue, &preview, b).unwrap_or_else(|e| panic!("{what}: {e:?}")); + let on_cpu = photocraft_compose::render(&preview, b).into_px(); + let d = max_diff(&on_gpu, &on_cpu, skip); + assert!(d <= tol_gpu, "{what}: GPU vs CPU {:.2}/255", d * 255.0); + let mut s = photocraft_engine::Session::new(); + s.add_document(doc.clone(), None); + s.select_layer(target).unwrap(); + s.execute(&format!("image.adjustments.{kind}"), params.clone()).unwrap(); + let cmd = photocraft_compose::render(&s.active().unwrap().doc, b).into_px(); + let d = max_diff(&on_gpu, &cmd, skip); + worst = worst.max(d); + assert!(d <= tol_cmd, "{what}: GPU preview vs command {:.2}/255", d * 255.0); + } + } + } + } + eprintln!("worst clipped-target GPU preview vs command: {:.3}/255", worst * 255.0); +} + /// The zoomed-out proxy preview (own document and layer ids) composites on the GPU like the CPU. #[test] fn proxy_preview_composites_on_the_gpu() { diff --git a/crates/ui-egui/tests/it/main.rs b/crates/ui-egui/tests/it/main.rs index c48cf51dc5..cc7eab2fc9 100644 --- a/crates/ui-egui/tests/it/main.rs +++ b/crates/ui-egui/tests/it/main.rs @@ -36,6 +36,11 @@ mod eyedropper_ring; mod full_res_preview; mod gpu_device_loss; mod healing_reset_tip; +// The interactive bench's `#[cfg(test)]` reporting tests: CI runs `--tests`, never examples. +// Embedding the example leaves its other entrypoints unused in this binary. +#[path = "../../examples/interactive_bench.rs"] +#[allow(dead_code)] +mod interactive_bench_reporting; mod layout_perf; mod live_stroke_canvas; mod magnetic_lasso_right_click;