PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Selection outline: trace serially while background jobs run (#1690)
Nigel Ihza Firdausy864d9ce99189
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 919c060d1..bc82102b1 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.rsindex 9f12e4077..bdb5c4150 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<Segment> {/// 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<Segment> {+ 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<Segment> {+ outline_scaled_impl(mask, bounds, step, false)+}++fn outline_scaled_impl(mask: &Surface, bounds: Rect, step: u32, parallel: bool) -> Vec<Segment> {if bounds.is_empty() {return Vec::new();}@@ -42,12 +52,17 @@ pub fn outline_scaled(mask: &Surface, bounds: Rect, step: u32) -> Vec<Segment> {}};#[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);