PhotoCraft: An open-source, photo editor written by scratch in pure Rust
perf(ui): Channels thumbnails skip selection-only steps and reuse their textures (#4518) (#4678)
FinnNk0de20b55234a
diff --git a/crates/ui-egui/src/channels_panel.rs b/crates/ui-egui/src/channels_panel.rsindex 8873b5bea..e456cfabc 100644--- a/crates/ui-egui/src/channels_panel.rs+++ b/crates/ui-egui/src/channels_panel.rs@@ -483,6 +483,34 @@ mod tests {assert_eq!(thumbs_gray.len(), thumbs_color.len());}+ /// A selection-only step keeps the thumbnails; a pixel edit rebuilds them into the same+ /// textures rather than new ones (#4518).+ #[test]+ fn channel_thumbs_skip_selection_steps_and_reuse_textures() {+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({"width": 64, "height": 48})).unwrap();+ let ctx = egui::Context::default();+ // Texture uploads one pass makes (new textures and updates both count).+ let pass = |app: &mut PhotocraftApp| {+ ctx.begin_pass(Default::default());+ let ids = app.channel_thumbs(&ctx);+ let mut delta = ctx.end_pass().textures_delta;+ let n = delta.set.len();+ delta.clear();+ (ids, n)+ };+ let (first, uploads) = pass(&mut app);+ assert!(uploads > 0);+ app.run("select.rect", json!({"x": 4, "y": 4, "width": 20, "height": 20})).unwrap();+ assert_eq!(pass(&mut app), (first.clone(), 0), "a marquee changes no pixels");+ app.run("select.deselect", json!({})).unwrap();+ assert_eq!(pass(&mut app), (first.clone(), 0), "neither does Deselect");+ app.run("edit.fill", json!({"color": "#336699"})).unwrap();+ let (after, uploads) = pass(&mut app);+ assert!(uploads > 0, "a fill rebuilds them");+ assert_eq!(after, first, "into the same textures");+ }+/// The Channels tab (the Layers card's second tab) of a 64×48 white document with a layer/// mask on "Masked" and one alpha channel.fn channels_harness() -> (egui_kittest::Harness<'static, PhotocraftApp>, u64) {diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 718b38ea6..c10d18ddb 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -2176,7 +2176,20 @@ impl PhotocraftApp {// identity against address reuse without retaining the document's pixel data.let snapshot = std::sync::Arc::downgrade(&doc);let show_color = self.session.prefs().interface.show_channels_in_color;- if !matches!(&self.channel_thumbs, Some((d, old, in_color, _)) if *d == id && old.ptr_eq(&snapshot) && *in_color == show_color) {+ // A step that changes only the selection (a marquee, Deselect) makes a new snapshot with+ // the same pixels: keep the thumbnails, a full composite at 56 px costs ~0.7 s on a+ // layered 20 MP document (#4518).+ let current = match &mut self.channel_thumbs {+ Some((d, old, in_color, _)) if *d == id && *in_color == show_color => {+ let same = old.ptr_eq(&snapshot) || old.upgrade().is_some_and(|prev| same_but_selection(&prev, &doc));+ if same {+ *old = snapshot.clone();+ }+ same+ }+ _ => false,+ };+ if !current {let comp = photocraft_compose::thumbnail(&doc, 56);let (w, h) = (comp.width as usize, comp.height as usize);let side = w.max(h);@@ -2189,7 +2202,7 @@ impl PhotocraftApp {px[(y + oy) * side + x + ox] = f(&comp.pixels[o..o + 4]);}}- ctx.load_texture(format!("chan-{name}"), egui::ColorImage::new([side, side], px), egui::TextureOptions::LINEAR)+ (format!("chan-{name}"), egui::ColorImage::new([side, side], px))};let fmt = doc.pixel_format();let colors = fmt.mode.color_channels();@@ -2222,14 +2235,37 @@ impl PhotocraftApp {px[(y + oy) * side + x + ox] = egui::Color32::from_gray((v.clamp(0.0, 1.0) * 255.0 + 0.5) as u8);}}- texs.push(ctx.load_texture(format!("chan-a{i}"), egui::ColorImage::new([side, side], px), egui::TextureOptions::LINEAR));+ texs.push((format!("chan-a{i}"), egui::ColorImage::new([side, side], px)));}- self.channel_thumbs = Some((id, snapshot, show_color, texs));+ // Update the textures already uploaded rather than allocating new ones.+ let mut handles = match self.channel_thumbs.take() {+ Some((d, _, _, h)) if d == id => h,+ _ => Vec::new(),+ };+ handles.truncate(texs.len());+ for (i, (name, image)) in texs.into_iter().enumerate() {+ match handles.get_mut(i) {+ Some(h) => h.set(image, egui::TextureOptions::LINEAR),+ None => handles.push(ctx.load_texture(name, image, egui::TextureOptions::LINEAR)),+ }+ }+ self.channel_thumbs = Some((id, snapshot, show_color, handles));}self.channel_thumbs.as_ref().map(|(_, _, _, t)| t.iter().map(|t| t.id()).collect()).unwrap_or_default()}}+/// Whether `new` differs from `old` at most in its selection, so every composite and channel+/// thumbnail of it is unchanged. Tiles compare by pointer first, so this costs little.+fn same_but_selection(old: &Document, new: &Document) -> bool {+ if old.selection == new.selection {+ return old == new;+ }+ let mut o = old.clone();+ o.selection = new.selection.clone();+ o == *new+}+/// Layer › Layer Mask commands that add a layer mask: Photoshop targets the new mask (#2166).const ADDS_LAYER_MASK: [&str; 5] = ["layer.layerMask.revealAll",