mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
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 <[email protected]>
This commit is contained in:
1 parent
2f3e1c0842
commit
864d9ce991
2 files changed
+35
-3
No files matched your search
@@ -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)));
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user