Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

perf(canvas): a Properties-panel adjustment drag refreshes only the view (#6839) (#7155)

skylar taylor-barrickceb85009c775
diff --git a/crates/ui-egui/src/adjust_preview.rs b/crates/ui-egui/src/adjust_preview.rs
index d94d58bff..f74afc859 100644
--- a/crates/ui-egui/src/adjust_preview.rs
+++ b/crates/ui-egui/src/adjust_preview.rs
@@ -436,6 +436,9 @@ pub(crate) fn retain_documents(app: &mut PhotocraftApp) {
if app.adjust_preview.as_ref().is_some_and(|p| !app.session.documents().iter().any(|st| st.doc.id == p.doc)) {
app.adjust_preview = None;
}
+ if app.live_adjust_preview.as_ref().is_some_and(|p| !app.session.documents().iter().any(|st| st.doc.id == p.doc)) {
+ app.live_adjust_preview = None;
+ }
}
/// GPU texture keys the preview uses besides the documents' own (keep them alive).
@@ -444,6 +447,108 @@ pub fn gpu_keys(app: &PhotocraftApp) -> Option<u64> {
p.proxy.as_ref().map(|g| g.base.id.0)
}
+// ---------------------------------------------------------------------------------------------
+// The Properties panel's live adjustment-layer preview (`canvas::display_doc`'s `live_adjust`).
+//
+// Dragging a control there shows the document with that layer's adjustment replaced. Unlike the
+// dialog above it needs no temporary layer: the layer is already in the document. What it did
+// lack was a damage region, so every parameter value recomposited the whole document — on the
+// GPU canvas at 100 % zoom that is the full size, measured at about 7 ms per megapixel, which
+// made #5778 a latency regression (#6839). These give that path the same treatment the dialog
+// gets: a session-tagged key, the layer's area as the damage, and of that only what the view
+// shows, catching up as the view moves.
+
+/// Tag of the Properties-panel live-adjust preview keys. Distinct from [`TAG`], so a key is
+/// never mistaken for the dialog's.
+pub const LIVE_TAG: u64 = 1 << 58;
+/// Bits of a live-adjust key that identify the session (document, revision, layer); below them
+/// the settings hash. The session occupies bits 40..58, under [`LIVE_TAG`].
+const LIVE_SESSION_SHIFT: u32 = 40;
+/// Mask of the session part of a live-adjust key (18 bits).
+const LIVE_SESSION_MASK: u64 = (1 << 18) - 1;
+
+/// One Properties-panel drag: which layer of which document revision, the area its adjustment
+/// can change, and what the canvas has composited of it so far.
+pub struct LiveAdjust {
+ doc: DocId,
+ revision: u64,
+ session: u64,
+ region: Rect,
+ /// The main canvas texture shows preview `key` only within this rect; elsewhere in the
+ /// region it is older (see [`live_uncovered`]).
+ covered: Option<(u64, Rect)>,
+}
+
+/// The area an adjustment layer can change: the whole document.
+///
+/// It is deliberately not narrowed by the layer's mask. A mask's coverage can live entirely in
+/// its surface's *default pixel* with no tiles written — [`LayerMask::reveal_all`], which is what
+/// `layer.newAdjustmentLayer` gives every adjustment layer — and such a mask reports an empty
+/// `content_bounds()`. Intersecting with that emptied the region, so the damage was empty and the
+/// compositor drew nothing at all for the whole drag. A mask only bounds the effect when it hides
+/// outside its content (default pixel 0); that narrowing is worth little here, since the damage is
+/// clipped to the view anyway ([`live_switch_damage`]), and is not worth the risk of showing stale
+/// pixels. Too large only costs pixels; too small freezes the preview.
+fn live_region(doc: &Document, layer: LayerId) -> Rect {
+ if doc.layer(layer).is_none() {
+ return Rect::EMPTY;
+ }
+ doc.bounds()
+}
+
+/// The preview key for `params` on `layer` of `doc` at `revision`, starting a session (and
+/// measuring its region) when the document, revision or layer changes.
+pub fn live_key(app: &mut PhotocraftApp, doc: &Document, revision: u64, layer: LayerId, params: &Value) -> u64 {
+ let session = hash(&[&doc.id.0.to_le_bytes(), &revision.to_le_bytes(), &layer.0.to_le_bytes()]);
+ if app.live_adjust_preview.as_ref().is_none_or(|p| p.session != session) {
+ app.live_adjust_preview = Some(LiveAdjust { doc: doc.id, revision, session, region: live_region(doc, layer), covered: None });
+ }
+ let h = hash(&[params.to_string().as_bytes()]);
+ LIVE_TAG | ((session & LIVE_SESSION_MASK) << LIVE_SESSION_SHIFT) | (h & ((1 << LIVE_SESSION_SHIFT) - 1))
+}
+
+/// The canvas area to recomposite when document `doc` (at `revision`) switches between two of
+/// this drag's previews, or between one and the committed document (0).
+pub fn live_switch_region(app: &PhotocraftApp, doc: DocId, revision: u64, from: u64, to: u64) -> Option<Rect> {
+ let p = app.live_adjust_preview.as_ref()?;
+ if p.doc != doc || p.revision != revision {
+ return None;
+ }
+ let prefix = LIVE_TAG | ((p.session & LIVE_SESSION_MASK) << LIVE_SESSION_SHIFT);
+ let ours = |k: u64| k == 0 || k >> LIVE_SESSION_SHIFT == prefix >> LIVE_SESSION_SHIFT;
+ (ours(from) && ours(to) && (from != 0 || to != 0)).then_some(p.region)
+}
+
+/// The part of a switch's region to composite now: to a preview, only what the view shows,
+/// remembered so [`live_uncovered`] catches up as the view moves; back to the committed
+/// document (key 0), the whole region.
+pub fn live_switch_damage(app: &mut PhotocraftApp, to: u64, region: Rect, visible: Rect) -> Rect {
+ let Some(p) = app.live_adjust_preview.as_mut() else { return region };
+ if to == 0 {
+ p.covered = None;
+ return region;
+ }
+ let shown = region.intersect(&visible);
+ p.covered = Some((to, shown));
+ shown
+}
+
+/// Where the main canvas texture (showing preview `key`) is out of date within `visible`: the
+/// region's visible part when the view moved past what was composited. Marks it covered.
+pub fn live_uncovered(app: &mut PhotocraftApp, doc: DocId, key: u64, visible: Rect) -> Option<Rect> {
+ let p = app.live_adjust_preview.as_mut().filter(|p| p.doc == doc)?;
+ let (k, cov) = p.covered?;
+ if k != key {
+ return None;
+ }
+ let want = p.region.intersect(&visible);
+ if want.is_empty() || cov.contains_rect(&want) {
+ return None;
+ }
+ p.covered = Some((k, want));
+ Some(want)
+}
+
#[cfg(test)]
#[path = "adjust_preview_tests.rs"]
mod tests;
diff --git a/crates/ui-egui/src/adjust_preview_tests.rs b/crates/ui-egui/src/adjust_preview_tests.rs
index f3e33a723..0b5337da7 100644
--- a/crates/ui-egui/src/adjust_preview_tests.rs
+++ b/crates/ui-egui/src/adjust_preview_tests.rs
@@ -333,3 +333,60 @@ fn proxy_preview_has_its_own_ids() {
}
assert!(proxy_with_settings(&proxy, "levels", &json!({"gamma": "x"})).is_err());
}
+
+/// A Properties-panel drag damages the layer's area and only the part of it the view shows,
+/// catching up as the view moves; a new document revision ends the session (#6839).
+#[test]
+fn a_live_adjust_drag_damages_only_the_visible_part_of_its_layer() {
+ let (doc, target) = document(ColorMode::Rgb, SampleType::U8, false);
+ let (id, bounds) = (doc.id, doc.bounds());
+ let mut app = app_with(doc.clone(), target);
+
+ let k1 = live_key(&mut app, &doc, 7, target, &json!({"gamma": 1.1}));
+ let k2 = live_key(&mut app, &doc, 7, target, &json!({"gamma": 1.4}));
+ assert_ne!(k1, k2, "a different setting is a different key");
+ assert_eq!(k1 & LIVE_TAG, LIVE_TAG, "keys carry the live tag");
+ assert_eq!(k1 >> LIVE_SESSION_SHIFT, k2 >> LIVE_SESSION_SHIFT, "the same drag is one session");
+ assert_ne!(k1 >> LIVE_SESSION_SHIFT, TAG >> LIVE_SESSION_SHIFT, "never the dialog's tag");
+
+ // Between two of the drag's previews, and between one and the committed document.
+ let region = live_switch_region(&app, id, 7, k1, k2).expect("same session");
+ assert_eq!(region, bounds, "the region is the whole document");
+ assert!(live_switch_region(&app, id, 7, 0, k2).is_some(), "from the committed document");
+ assert!(live_switch_region(&app, id, 7, k1, 0).is_some(), "back to the committed document");
+ assert!(live_switch_region(&app, id, 7, 0, 0).is_none(), "the document to itself is no switch");
+ assert!(live_switch_region(&app, id, 8, k1, k2).is_none(), "another revision is another session");
+
+ // Only the visible part is composited now; moving the view asks for the rest.
+ let mid = region.x0 + region.width() as i32 / 2;
+ let left = Rect::new(region.x0, region.y0, mid, region.y1);
+ assert_eq!(live_switch_damage(&mut app, k2, region, left), region.intersect(&left));
+ assert!(live_uncovered(&mut app, id, k2, left).is_none(), "what was just composited is covered");
+ let right = Rect::new(mid, region.y0, region.x1, region.y1);
+ assert_eq!(live_uncovered(&mut app, id, k2, right), Some(region.intersect(&right)), "the view moved");
+ assert!(live_uncovered(&mut app, id, k2, right).is_none(), "and is covered once composited");
+ assert!(live_uncovered(&mut app, id, k1, right).is_none(), "another preview's coverage is not this one's");
+
+ // Switching back to the document composites the whole region, not just the view.
+ assert_eq!(live_switch_damage(&mut app, 0, region, left), region);
+}
+
+/// A reveal-all mask stores its coverage in the surface's default pixel and reports an empty
+/// `content_bounds()`. Narrowing the region by it emptied the damage and the canvas drew nothing
+/// for a whole drag — every adjustment layer `layer.newAdjustmentLayer` creates has one (#6839).
+#[test]
+fn a_reveal_all_mask_does_not_empty_the_damage_region() {
+ let (mut doc, target) = document(ColorMode::Rgb, SampleType::U8, false);
+ let bounds = doc.bounds();
+ let mask = photocraft_doc::LayerMask::reveal_all();
+ assert!(mask.surface.content_bounds().is_empty(), "a reveal-all mask has no written content");
+ doc.layer_mut(target).expect("target").mask = Some(mask);
+ let id = doc.id;
+ let mut app = app_with(doc.clone(), target);
+
+ let k1 = live_key(&mut app, &doc, 3, target, &json!({"gamma": 1.1}));
+ let k2 = live_key(&mut app, &doc, 3, target, &json!({"gamma": 1.4}));
+ let region = live_switch_region(&app, id, 3, k1, k2).expect("same session");
+ assert_eq!(region, bounds, "the whole document, not the mask's empty content");
+ assert!(!live_switch_damage(&mut app, k2, region, bounds).is_empty(), "something is composited");
+}
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 914d395ac..9ca0bac7c 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -1203,12 +1203,13 @@ pub(crate) fn display_doc(app: &mut PhotocraftApp, idx: usize) -> (std::sync::Ar
return (doc.clone(), key);
}
}
+ let revision = st.revision;
let snapshot = st.doc.clone();
if let Some((layer, params)) = app.live_adjust.clone()
&& let Some(l) = snapshot.layer(layer)
&& let LayerContent::Adjustment(a) = &l.content
{
- let key = 1 + params.to_string().bytes().fold(0u64, |h, b| h.wrapping_mul(31).wrapping_add(b as u64));
+ let key = crate::adjust_preview::live_key(app, &snapshot, revision, layer, &params);
let shown = memo_display(app, &snapshot, key, || {
let kind = photocraft_engine::commands::adjustment_kind(a);
let preview = photocraft_engine::adjust_params::from_params(kind, &params, Some(a), snapshot.mode).unwrap_or_else(|_| a.clone());
@@ -1489,6 +1490,14 @@ pub(crate) fn damage_since(app: &PhotocraftApp, idx: usize, seen: (u64, u64), no
{
return Some(r);
}
+ // Between a Properties-panel adjustment-layer drag's previews (and the document): the
+ // layer's area. Without this every parameter value recomposited the whole document (#6839).
+ if seen.0 == now.0
+ && let Some(st) = app.session.documents().get(idx)
+ && let Some(r) = crate::adjust_preview::live_switch_region(app, st.doc.id, now.0, seen.1 ^ display_key, now.1)
+ {
+ return Some(r);
+ }
// Between a Move drag's offsets: where the moving layers were and are (Auto-Select's pick
// at the press changes no pixels).
if (seen.0 == now.0 || (seen.0 + 1 == now.0 && last_damage.is_some_and(|r| r.is_empty())))
@@ -1592,6 +1601,12 @@ fn ensure_gpu(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize, visible:
{
damage = Some(crate::adjust_preview::switch_damage(app, raw_key, r, visible));
}
+ // The same for a Properties-panel drag's previews.
+ if damage.is_some()
+ && let Some(r) = seen.filter(|s| s.0 == revision).and_then(|s| crate::adjust_preview::live_switch_region(app, id, revision, s.1 ^ display_key, raw_key))
+ {
+ damage = Some(crate::adjust_preview::live_switch_damage(app, raw_key, r, visible));
+ }
let preview_key = raw_key ^ display_key;
// The Navigator may draw this texture only while it holds the plain document (no live
// preview, which can be composited only where the view is).
@@ -1635,6 +1650,10 @@ fn ensure_gpu(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize, visible:
let r = gpu.refresh(id.0, &doc, Some(r), display.as_deref());
app.perf.record(r.kind, r.px, r.composite_ms, r.upload_ms);
}
+ if let Some(r) = crate::adjust_preview::live_uncovered(app, id, raw_key, visible) {
+ let r = gpu.refresh(id.0, &doc, Some(r), display.as_deref());
+ app.perf.record(r.kind, r.px, r.composite_ms, r.upload_ms);
+ }
return true;
}
// Display textures for a canvas this large would not fit the GPU: the reduced CPU canvas
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 5adfd7886..b5c978208 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -709,6 +709,9 @@ pub struct PhotocraftApp {
pub(crate) color_range: Option<color_range_ui::Preview>,
/// Image › Adjustments dialog preview through a temporary clipped adjustment layer.
pub(crate) adjust_preview: Option<adjust_preview::AdjustPreview>,
+ /// The Properties panel's live adjustment-layer drag: its damage region and coverage
+ /// (`adjust_preview::live_key`).
+ pub(crate) live_adjust_preview: Option<adjust_preview::LiveAdjust>,
/// Synthetic input events queued by automation (`ui.click`, `ui.key`, …), injected next frame.
pub(crate) synthetic: Vec<egui::Event>,
/// True only while a frame is processing synthetic automation input. It
@@ -916,6 +919,7 @@ impl PhotocraftApp {
redraws: Default::default(),
color_range: None,
adjust_preview: None,
+ live_adjust_preview: None,
synthetic: Vec::new(),
automation_input: false,
channel_thumbs: None,