From 864d9ce99189060dc1a1281b63fe9d19ab33eebf Mon Sep 17 00:00:00 2001 From: Nigel Ihza Firdausy <106657905+Ni-zav@users.noreply.github.com> Date: Fri, 9 Oct 2026 20:04:35 +0700 Subject: [PATCH] Selection outline: trace serially while background jobs run (#1690) A long filter could occupy every worker in Rayon's global pool, and the marching-ants trace on the UI thread then waited for those workers, so the job's Cancel control froze. While jobs run the outline is traced serially; a test checks it matches the parallel trace. Fixes #1554. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/canvas.rs | 4 +++- crates/ui-egui/src/outline.rs | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 919c060d1b..bc82102b12 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -2171,7 +2171,9 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect: if !fresh { let t0 = crate::gpu_canvas::now_ms(); let b = app.cached_bounds(u64::MAX - doc.id.0, sel).intersect(&vis); - let segs = crate::outline::outline_scaled(sel, b, step); + // Filters can occupy every worker in Rayon's global pool. A parallel scan here would + // block the UI thread waiting for those workers, preventing job cancellation (#1554). + let segs = if app.session.has_jobs() { crate::outline::outline_scaled_serial(sel, b, step) } else { crate::outline::outline_scaled(sel, b, step) }; app.perf.span("outline", crate::gpu_canvas::now_ms() - t0); app.outline_cache = Some((doc.id, key, std::sync::Arc::new(segs))); } diff --git a/crates/ui-egui/src/outline.rs b/crates/ui-egui/src/outline.rs index 9f12e40774..bdb5c41501 100644 --- a/crates/ui-egui/src/outline.rs +++ b/crates/ui-egui/src/outline.rs @@ -17,6 +17,16 @@ pub fn outline(mask: &Surface, bounds: Rect) -> Vec { /// Like [`outline`], sampling one pixel per `step`×`step` block (segments stay in document space). pub fn outline_scaled(mask: &Surface, bounds: Rect, step: u32) -> Vec { + outline_scaled_impl(mask, bounds, step, true) +} + +/// Trace without waiting for Rayon's shared worker pool. The UI uses this while background jobs +/// are active, because a long-running filter can occupy all the workers (#1554). +pub fn outline_scaled_serial(mask: &Surface, bounds: Rect, step: u32) -> Vec { + outline_scaled_impl(mask, bounds, step, false) +} + +fn outline_scaled_impl(mask: &Surface, bounds: Rect, step: u32, parallel: bool) -> Vec { if bounds.is_empty() { return Vec::new(); } @@ -42,12 +52,17 @@ pub fn outline_scaled(mask: &Surface, bounds: Rect, step: u32) -> Vec { } }; #[cfg(not(target_arch = "wasm32"))] - { + if parallel { use rayon::prelude::*; grid.par_chunks_mut(w).enumerate().for_each(|(gy, row)| sample_row(gy, row)); + } else { + grid.chunks_mut(w).enumerate().for_each(|(gy, row)| sample_row(gy, row)); } #[cfg(target_arch = "wasm32")] - grid.chunks_mut(w).enumerate().for_each(|(gy, row)| sample_row(gy, row)); + { + let _ = parallel; + grid.chunks_mut(w).enumerate().for_each(|(gy, row)| sample_row(gy, row)); + } let inside = |x: usize, y: usize| grid[y * w + x]; // Grid → document coordinates, clamped so coarse cells never draw outside the selection bounds. let r = Rect::new(gx0 * step, gy0 * step, gx1 * step, gy1 * step); @@ -131,6 +146,21 @@ mod tests { } } + #[test] + fn serial_outline_matches_parallel_for_scales_and_clipped_bounds() { + let mut mask = Surface::new(PixelFormat::GRAY8); + mask.fill_rect(Rect::new(3, 5, 110, 90), &[1.0]); + mask.fill_rect(Rect::new(17, 21, 45, 65), &[0.0]); + mask.fill_rect(Rect::new(73, 9, 88, 42), &[0.0]); + for step in [1, 2, 4, 8, 16] { + for bounds in [mask.content_bounds(), Rect::new(13, 19, 91, 77), Rect::new(200, 200, 240, 240)] { + let parallel = outline_scaled(&mask, bounds, step); + let serial = outline_scaled_serial(&mask, bounds, step); + assert_eq!(serial, parallel, "outline differs at step {step}, bounds {bounds:?}"); + } + } + } + #[test] fn empty_mask_has_no_outline() { let m = Surface::new(PixelFormat::GRAY8);