PhotoCraft: An open-source, photo editor written by scratch in pure Rust
perf: the Navigator redraws only the area an edit changed (#3845)
Jakub05c4cd685eac
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rsindex c059d9c8b..08dcdeaf6 100644--- a/crates/compose/src/lib.rs+++ b/crates/compose/src/lib.rs@@ -313,13 +313,19 @@ pub fn thumbnail(doc: &Document, max_side: u32) -> Rgba8Image {thumbnail_buffer(doc, max_side).to_rgba8()}+/// Width and height of [`thumbnail`]`(doc, max_side)`.+pub fn thumbnail_size(doc: &Document, max_side: u32) -> (u32, u32) {+ let b = doc.bounds();+ let longest = b.width().max(b.height()).max(1);+ let scale = (max_side as f32 / longest as f32).min(1.0);+ (((b.width() as f32 * scale).round() as u32).max(1), ((b.height() as f32 * scale).round() as u32).max(1))+}+/// [`thumbnail`] as straight-alpha floats (for callers that colour-convert it first).pub fn thumbnail_buffer(doc: &Document, max_side: u32) -> Buffer {let b = doc.bounds();let longest = b.width().max(b.height()).max(1);- let scale = (max_side as f32 / longest as f32).min(1.0);- let w = ((b.width() as f32 * scale).round() as u32).max(1);- let h = ((b.height() as f32 * scale).round() as u32).max(1);+ let (w, h) = thumbnail_size(doc, max_side);let k = longest / max_side.max(1).saturating_mul(2);if k >= 2 && doc.size.area() > PROXY_THUMBNAIL_PIXELS && proxy::proxy_faithful(doc) {render_reduced(&proxy::proxy_document(doc, k), w, h)diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex c9e4c6ce9..bd3b64f32 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -1080,7 +1080,8 @@ pub(crate) fn display_doc(app: &mut PhotocraftApp, idx: usize) -> (std::sync::Ar/// Longest side of the Navigator panel's image (about twice the panel's width, for HiDPI).pub const NAVIGATOR_SIDE: u32 = 512;-pub(crate) type NavigatorCache = (std::sync::Weak<Document>, u64, egui::TextureHandle);+/// The navigator image of a document: its snapshot, preview key and revision, and the texture.+pub(crate) type NavigatorCache = (std::sync::Weak<Document>, u64, u64, egui::TextureHandle);/// How long adjustment-dialog settings must stay unchanged before the navigator shows them.const NAVIGATOR_SETTLE_MS: f64 = 200.0;@@ -1099,25 +1100,26 @@ pub fn navigator_texture(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usizreturn ensure_texture(app, ctx, idx, app.session.color.main_display).map(|(t, _)| t);}// Keyed by the document snapshot, not its revision: selecting a layer changes no pixels.- let (snapshot, id) = app.session.documents().get(idx).map(|st| (std::sync::Arc::downgrade(&st.doc), st.doc.id))?;+ let (snapshot, id, revision, last_damage) =+ app.session.documents().get(idx).map(|st| (std::sync::Arc::downgrade(&st.doc), st.doc.id, st.revision, st.last_damage))?;let (doc, preview_key) = display_doc(app, idx);let (display, display_key) = canvas_display(app, &doc, None);let preview_key = preview_key ^ display_key;let cached = app.navigator_textures.get(&id).cloned();- if let Some((r, p, t)) = &cached+ if let Some((r, p, _, t)) = &cached&& r.ptr_eq(&snapshot)&& *p == preview_key{return Some(t.id());}- if let Some((_, _, t)) = &cached+ if let Some((_, _, _, t)) = &cached&& navigator_waits_for_drag(app, idx){return Some(t.id());}// While an adjustment dialog's settings are changing, the navigator keeps its image and// catches up once they settle (a thumbnail composite per change would cost more than the canvas).- if let Some((r, _, t)) = &cached+ if let Some((r, _, _, t)) = &cached&& r.ptr_eq(&snapshot)&& crate::adjust_preview::settling(app, NAVIGATOR_SETTLE_MS){@@ -1125,16 +1127,38 @@ pub fn navigator_texture(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usizreturn Some(t.id());}let t0 = crate::gpu_canvas::now_ms();+ // One edit since the cached image, with the area it changed: update just that part (nothing+ // for a rename, a selection or a new empty layer). A thumbnail composite of the whole+ // document on every edit cost a full-resolution CPU composite on the UI thread.+ let size = photocraft_compose::thumbnail_size(&doc, NAVIGATOR_SIDE);+ if let Some((_, p, rev, t)) = &cached+ && *p == preview_key+ && rev.checked_add(1) == Some(revision)+ && let Some(d) = last_damage+ && t.size() == [size.0 as usize, size.1 as usize]+ {+ let mut t = t.clone();+ if !d.is_empty() {+ let buf = photocraft_compose::render_reduced_damage(&doc, size.0, size.1, d.inflate(effect_reach(&doc.layers)));+ let r = buf.rect;+ if !r.is_empty() {+ t.set_partial([r.x0 as usize, r.y0 as usize], display_image(display.as_deref(), &buf), TextureOptions::LINEAR);+ }+ }+ app.perf.span("navigator", crate::gpu_canvas::now_ms() - t0);+ app.navigator_textures.insert(id, (snapshot, preview_key, revision, t.clone()));+ return Some(t.id());+ }let image = display_image(display.as_deref(), &photocraft_compose::thumbnail_buffer(&doc, NAVIGATOR_SIDE));let tex = match cached {- Some((_, _, mut t)) => {+ Some((_, _, _, mut t)) => {t.set(image, TextureOptions::LINEAR);t}None => ctx.load_texture(format!("navigator-{}", id.0), image, TextureOptions::LINEAR),};app.perf.span("navigator", crate::gpu_canvas::now_ms() - t0);- app.navigator_textures.insert(id, (snapshot, preview_key, tex.clone()));+ app.navigator_textures.insert(id, (snapshot, preview_key, revision, tex.clone()));Some(tex.id())}@@ -5499,7 +5523,7 @@ mod tests {// The same handle ownership as navigator_texture, without requiring a GPU adapter.let tex = ctx.load_texture("navigator-test", egui::ColorImage::filled([64, 64], egui::Color32::WHITE), TextureOptions::LINEAR);let nav = tex.id();- app.navigator_textures.insert(first.id, (std::sync::Arc::downgrade(&first), 0, tex));+ app.navigator_textures.insert(first.id, (std::sync::Arc::downgrade(&first), 0, 0, tex));app.run("file.new", json!({"width":32,"height":32})).unwrap();let other = app.session.active().unwrap().doc.id;let b = ensure_texture(&mut app, &ctx, 1, Some(1)).unwrap().0;@@ -5591,6 +5615,45 @@ mod tests {assert_eq!(texture_key(canvas_display(&app, &doc, Some(1)).0.as_deref()), texture_key(canvas_display(&app, &doc, Some(4)).0.as_deref()));}+ /// The navigator's update after an edit (its image with the edit's damage, grown by the+ /// effect reach, rendered over it) equals a new thumbnail of the edited document.+ #[test]+ fn navigator_damage_updates_match_a_full_thumbnail() {+ use photocraft_compose::{render_reduced_damage, thumbnail_buffer, thumbnail_size};+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());+ app.run("file.new", json!({"width": 1500, "height": 900, "background": "white"})).unwrap();+ app.run("layer.new.layer", json!({})).unwrap();+ app.run("select.rect", json!({"x": 200, "y": 150, "width": 300, "height": 200})).unwrap();+ app.run("edit.fill", json!({"color": "#3070a0"})).unwrap();+ app.run("select.deselect", json!({})).unwrap();+ app.run("layer.layerStyle.dropShadow", json!({"distance": 30, "size": 20})).unwrap();+ let steps = [+ ("layer.translate", json!({"dx": 37, "dy": -11})),+ ("layer.setProps", json!({"opacity": 0.4})),+ ("paint.stroke", json!({"points": [[700, 500], [900, 520]], "size": 25})),+ ("layer.setProps", json!({"blend": "Multiply"})),+ ("layer.new.layer", json!({})),+ ];+ for (id, p) in steps {+ let before = app.session.active().unwrap().doc.clone();+ app.run(id, p).unwrap();+ let st = app.session.active().unwrap();+ let d = st.last_damage.unwrap_or_else(|| panic!("{id}: a bounded damage"));+ let (w, h) = thumbnail_size(&st.doc, NAVIGATOR_SIDE);+ let mut shown = thumbnail_buffer(&before, NAVIGATOR_SIDE);+ assert_eq!((shown.rect.width(), shown.rect.height()), (w, h));+ if !d.is_empty() {+ let patch = render_reduced_damage(&st.doc, w, h, d.inflate(effect_reach(&st.doc.layers)));+ let r = patch.rect;+ for y in 0..r.height() as usize {+ let at = (r.y0 as usize + y) * w as usize + r.x0 as usize;+ shown.px[at..at + r.width() as usize].copy_from_slice(&patch.px[y * r.width() as usize..][..r.width() as usize]);+ }+ }+ assert!(shown.px == thumbnail_buffer(&st.doc, NAVIGATOR_SIDE).px, "{id}: the updated navigator differs from a new thumbnail");+ }+ }+#[test]fn tab_context_uses_clicked_document_and_existing_close_commands() {let items = tab_context_items(2, 3, true);