Skip to content

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

perf: reuse sampled composite across channel visibility toggles (#4080)

Nigel Ihza Firdausyc8de7870d599
diff --git a/crates/ui-egui/src/channel_view.rs b/crates/ui-egui/src/channel_view.rs
index d7a8b190f..38116e58e 100644
--- a/crates/ui-egui/src/channel_view.rs
+++ b/crates/ui-egui/src/channel_view.rs
@@ -18,6 +18,20 @@ use crate::PhotocraftApp;
/// Largest overlay texture side; bigger documents show a downsampled overlay.
const MAX_SIDE: u32 = 4096;
+/// Limit the sampled float composite to 128 MiB across open documents.
+const MAX_COMPOSITE_CACHE_PIXELS: usize = 8 * 1024 * 1024;
+
+struct SampledComposite {
+ revision: u64,
+ preview_key: u64,
+ factor: u32,
+ pixels: Vec<[f32; 4]>,
+}
+
+struct CompositeSource<'a> {
+ cached: Option<&'a [[f32; 4]]>,
+ fresh: &'a mut Option<Vec<[f32; 4]>>,
+}
pub struct Cache {
revision: u64,
@@ -29,6 +43,7 @@ pub struct Cache {
/// reuses its allocation) but its pixels are stale.
valid: bool,
tex: egui::TextureHandle,
+ source: Option<SampledComposite>,
}
fn sample(s: &Surface, r: Rect, factor: u32) -> Vec<f32> {
@@ -49,18 +64,22 @@ fn sample(s: &Surface, r: Rect, factor: u32) -> Vec<f32> {
}
/// Composite RGBA (over white, like Photoshop's channel views) at `factor` stride.
+/// Render in bands instead of allocating a full-resolution float composite to sample a preview.
fn composite(doc: &Document, r: Rect, factor: u32) -> Vec<[f32; 4]> {
- let buf = photocraft_compose::render(doc, r);
let (w, h) = (r.width().div_ceil(factor), r.height().div_ceil(factor));
- let bw = r.width();
let mut out = Vec::with_capacity((w * h) as usize);
- for y in 0..h {
- for x in 0..w {
- let p = buf.px[((y * factor) * bw + x * factor) as usize];
- let a = p[3].clamp(0.0, 1.0);
- out.push([p[0] * a + 1.0 - a, p[1] * a + 1.0 - a, p[2] * a + 1.0 - a, 1.0]);
+ let f = factor as i32;
+ let _ = photocraft_compose::render_bands(doc, r, 0, |band| -> Result<(), ()> {
+ let first = band.rect.y0 + (r.y0 - band.rect.y0).rem_euclid(f);
+ for y in (first..band.rect.y1).step_by(factor as usize) {
+ for x in (r.x0..r.x1).step_by(factor as usize) {
+ let p = band.get(x, y);
+ let a = p[3].clamp(0.0, 1.0);
+ out.push([p[0] * a + 1.0 - a, p[1] * a + 1.0 - a, p[2] * a + 1.0 - a, 1.0]);
+ }
}
- }
+ Ok(())
+ });
out
}
@@ -68,6 +87,19 @@ fn composite(doc: &Document, r: Rect, factor: u32) -> Vec<[f32; 4]> {
/// or `None` when the plain composite is all there is to show. `in_color` shows single colour
/// channels tinted instead of grayscale (Photoshop's "Show Channels in Color").
pub fn render(doc: &Document, v: &ChannelView, r: Rect, factor: u32, in_color: bool, active: Option<photocraft_doc::LayerId>) -> Option<Vec<Color32>> {
+ let mut unused = None;
+ render_with_source(doc, v, r, factor, in_color, active, CompositeSource { cached: None, fresh: &mut unused })
+}
+
+fn render_with_source(
+ doc: &Document,
+ v: &ChannelView,
+ r: Rect,
+ factor: u32,
+ in_color: bool,
+ active: Option<photocraft_doc::LayerId>,
+ source: CompositeSource<'_>,
+) -> Option<Vec<Color32>> {
if v.is_plain(doc) || r.is_empty() {
return None;
}
@@ -116,8 +148,11 @@ pub fn render(doc: &Document, v: &ChannelView, r: Rect, factor: u32, in_color: b
let fmt = doc.pixel_format();
let mode = fmt.mode;
let shown: Vec<usize> = (0..colors).filter(|k| v.color_visible(*k)).collect();
- composite(doc, r, factor)
- .into_iter()
+ let computed = if source.cached.is_some_and(|px| px.len() == n) { None } else { Some(composite(doc, r, factor)) };
+ let pixels = source.cached.filter(|px| px.len() == n).or(computed.as_deref())?;
+ let converted = pixels
+ .iter()
+ .copied()
.map(|rgba| {
let mut native = from_rgba(&fmt, rgba);
if shown.len() == 1 && !in_color {
@@ -137,7 +172,11 @@ pub fn render(doc: &Document, v: &ChannelView, r: Rect, factor: u32, in_color: b
let c = to_rgba(&fmt, &native);
[c[0], c[1], c[2], 1.0]
})
- .collect()
+ .collect();
+ if n <= MAX_COMPOSITE_CACHE_PIXELS {
+ *source.fresh = computed;
+ }
+ converted
} else {
vec![[0.0; 4]; n]
};
@@ -231,6 +270,13 @@ pub fn ensure(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize) -> Optio
// Never exceed what the GPU accepts (egui panics on oversized textures).
let max_side = (ctx.input(|i| i.max_texture_side) as u32).clamp(256, MAX_SIDE);
let factor = doc.size.width.max(doc.size.height).div_ceil(max_side).max(1);
+ // A live preview can mutate the same Arc in place. Revision and preview_key, not
+ // pointer identity, determine whether these flattened pixels are still current.
+ if let Some(c) = app.channel_views.get_mut(&id)
+ && c.source.as_ref().is_some_and(|s| s.factor != factor || s.revision != revision || s.preview_key != preview_key)
+ {
+ c.source = None;
+ }
if let Some(c) = app.channel_views.get(&id)
&& c.key == key
&& c.factor == factor
@@ -264,7 +310,10 @@ pub fn ensure(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize) -> Optio
return Some(c.tex.id());
}
let b = doc.bounds();
- let px = render(&doc, &view, b, factor, in_color, active)?;
+ let cached = app.channel_views.get(&id).and_then(|c| c.source.as_ref()).map(|s| s.pixels.as_slice());
+ let mut fresh = None;
+ let px = render_with_source(&doc, &view, b, factor, in_color, active, CompositeSource { cached, fresh: &mut fresh })?;
+ let new_source = fresh.is_some();
let size = [b.width().div_ceil(factor) as usize, b.height().div_ceil(factor) as usize];
let img = egui::ColorImage::new(size, px);
match app.channel_views.get_mut(&id) {
@@ -275,10 +324,21 @@ pub fn ensure(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize) -> Optio
c.key = key;
c.factor = factor;
c.valid = true;
+ if let Some(pixels) = fresh.take() {
+ c.source = Some(SampledComposite { revision, preview_key, factor, pixels });
+ }
}
_ => {
let tex = ctx.load_texture(format!("channel-view-{id}"), img, TextureOptions::LINEAR);
- app.channel_views.insert(id, Cache { revision, key, preview_key, factor, valid: true, tex });
+ let source = fresh.map(|pixels| SampledComposite { revision, preview_key, factor, pixels });
+ app.channel_views.insert(id, Cache { revision, key, preview_key, factor, valid: true, tex, source });
+ }
+ }
+ if new_source {
+ for (other_id, c) in &mut app.channel_views {
+ if *other_id != id {
+ c.source = None;
+ }
}
}
app.channel_views.get(&id).map(|c| c.tex.id())
@@ -297,6 +357,78 @@ mod tests {
s
}
+ #[test]
+ fn composite_bands_keep_the_downsample_grid() {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 41, "height": 700})).unwrap();
+ s.execute("edit.fill", json!({"color": "#398db2"})).unwrap();
+ let doc = &s.active().unwrap().doc;
+ let rect = doc.bounds();
+ let full = photocraft_compose::render(doc, rect);
+ let full_ref = &full;
+ for factor in [1, 2, 3, 5] {
+ let expected: Vec<_> = (0..rect.height())
+ .step_by(factor as usize)
+ .flat_map(|y| {
+ (0..rect.width()).step_by(factor as usize).map(move |x| {
+ let p = full_ref.get(rect.x0 + x as i32, rect.y0 + y as i32);
+ let a = p[3].clamp(0.0, 1.0);
+ [p[0] * a + 1.0 - a, p[1] * a + 1.0 - a, p[2] * a + 1.0 - a, 1.0]
+ })
+ })
+ .collect();
+ assert_eq!(composite(doc, rect, factor), expected, "factor {factor}");
+ }
+ }
+
+ #[test]
+ fn channel_eyes_reuse_composite_until_pixels_change() {
+ let mut app = PhotocraftApp::new(Session::new(), crate::Services::default());
+ app.run("file.new", json!({"width": 64, "height": 32})).unwrap();
+ app.run("edit.fill", json!({"color": "#4080c0"})).unwrap();
+ app.sync_views();
+ let ctx = egui::Context::default();
+ app.run("channel.setVisible", json!({"channel": "red", "visible": false})).unwrap();
+ assert!(ensure(&mut app, &ctx, 0).is_some());
+ let id = app.session.active().unwrap().doc.id.0;
+ let first = app.channel_views.get(&id).unwrap().source.as_ref().unwrap();
+ let original_sample = first.pixels[0];
+ let ptr = first.pixels.as_ptr();
+
+ app.run("channel.setVisible", json!({"channel": "green", "visible": false})).unwrap();
+ assert!(ensure(&mut app, &ctx, 0).is_some());
+ let reused = app.channel_views.get(&id).unwrap().source.as_ref().unwrap();
+ assert_eq!(ptr, reused.pixels.as_ptr(), "visibility does not recompose the pixels");
+
+ app.run("edit.fill", json!({"color": "#e02010"})).unwrap();
+ assert!(ensure(&mut app, &ctx, 0).is_some());
+ // A partial damage upload discards the source; a full redraw may replace it.
+ // In either case, cached samples must never refer to the pre-edit pixels.
+ if let Some(source) = app.channel_views.get(&id).unwrap().source.as_ref() {
+ assert_ne!(source.pixels[0], original_sample, "a full redraw replaces stale pixels");
+ }
+ app.run("channel.setVisible", json!({"channel": "red", "visible": true})).unwrap();
+ assert!(ensure(&mut app, &ctx, 0).is_some());
+ let refreshed = app.channel_views.get(&id).unwrap().source.as_ref().unwrap();
+ let st = app.session.active().unwrap();
+ assert_eq!(refreshed.revision, st.revision);
+ assert_ne!(refreshed.pixels[0], original_sample, "new pixels must be recomposited");
+ let expected = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap();
+ let mut unused = None;
+ let actual = render_with_source(
+ &st.doc,
+ &st.channel_view,
+ st.doc.bounds(),
+ 1,
+ false,
+ st.active_layer,
+ CompositeSource { cached: Some(&refreshed.pixels), fresh: &mut unused },
+ )
+ .unwrap();
+ assert_eq!(actual, expected, "reused pixels must preserve channel colours");
+ assert!(unused.is_none(), "reuse must not create a second composite");
+ }
+
#[test]
fn plain_composite_draws_nothing() {
let s = session();