perf: the Navigator redraws only the area an edit changed (#3845)

With the Navigator open, every edit re-rendered the whole document into a new thumbnail on the
UI thread: a full-resolution CPU composite, 303 ms after an opacity change and 486 ms after a
brush stroke on a 20 MP layered document.

The navigator cache now records the document revision of its image. When the document is
exactly one edit ahead, with the same preview key, a bounded last_damage and the same thumbnail
size, only the damage (grown by the layers' effect reach) is rendered with
render_reduced_damage and written into the texture with set_partial; an edit with empty damage
(a rename, a selection, a new empty layer) costs nothing. Anything else renders the whole
thumbnail as before. compose gains thumbnail_size, which thumbnail_buffer now uses, so both
paths agree on the size.

On the 20 MP document: 303 -> 36 ms after an opacity change, 486 -> 171 ms after a stroke
(RTX 3090 machine; the navigator renders on the CPU).

Test: navigator_damage_updates_match_a_full_thumbnail patches a thumbnail after a translate,
an opacity change, a stroke, a blend mode change and a new layer on a document with a drop
shadow, and compares it with a full thumbnail; it fails without the effect-reach inflation.
This commit is contained in:
Jakub authored and GitHub committed 2026-10-12 08:46:53 -04:00
1 parent 3e428e7302
commit 05c4cd685e
2 files changed
+80 -11

No files matched your search

+9 -3
View File
@@ -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)
+71 -8
View File
@@ -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: usiz
return 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: usiz
return 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);