Skip to content

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

CPU effect maps: key fill layers by their cached pixels, drop opacity/visible (#1059)

Elvis33LEa593b3651c81
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs
index 5ed69499d..d1d44e975 100644
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@ -1234,17 +1234,33 @@ fn layer_identity(layer: &Layer, h: &mut std::collections::hash_map::DefaultHash
h.finish()
}
layer.id.0.hash(h);
- layer.visible.hash(h);
- layer.opacity.to_bits().hash(h);
+ // What the maps depend on: `fill_opacity` shades them; layer `opacity` is applied when the
+ // maps are composited and `visible` only skips the lookup entirely, so neither belongs in
+ // the key (an opacity drag must not rebuild shadows over the whole layer; the GPU's
+ // `fx::shape_key` keeps them out for the same reason).
layer.fill_opacity.to_bits().hash(h);
match &layer.content {
LayerContent::Group(g) => {
for c in &g.children {
+ // A styled group's maps come from its children's composite, so how each child
+ // composites (unlike the group's own visibility and opacity) shapes them.
+ (c.visible, c.opacity.to_bits(), c.clipped).hash(h);
+ format!("{:?}", c.blend).hash(h);
layer_identity(c, h);
}
format!("{:?}", g.artboard).hash(h);
}
- LayerContent::Fill(f) => format!("{f:?}").hash(h),
+ LayerContent::Fill(f) => {
+ format!("{f:?}").hash(h);
+ // The cached pixels are what shapes the maps while they match the fill: a
+ // pixel-only rewrite of the cache (Image › Transform, crop) leaves `f` alone, and
+ // the stale maps would keep shadowing the pre-rewrite shape.
+ if let Some(c) = &layer.fill_cache
+ && c.fill == *f
+ {
+ surface_fp(&c.surface).hash(h);
+ }
+ }
LayerContent::Adjustment(a) => format!("{a:?}").hash(h),
_ => layer.surface().map_or(0, surface_fp).hash(h),
}
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index 0d1460272..889890f06 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -419,6 +419,96 @@ fn fill_layers_and_colors() {
assert!(l < r);
}
+#[test]
+fn fill_layer_pixel_rewrites_refresh_the_effect_maps() {
+ // A pixel-only rewrite of a fill layer's cache (what Image › Transform and friends do)
+ // leaves the Fill spec alone; the CPU effect maps must follow the cache surface, or the
+ // composite keeps the pre-rewrite drop shadow. Warm render vs. purged render must agree.
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 64, "height": 48})).unwrap();
+ s.execute("layer.newFillLayer.gradient", json!({"from": "#000000", "to": "#ffffff", "angle": 0})).unwrap();
+ s.edit("shadow", |doc, active| {
+ doc.layer_mut(active.unwrap()).unwrap().effects.items.push(photocraft_doc::Effect::default_drop_shadow());
+ Ok(())
+ })
+ .unwrap();
+ let render = |s: &Session| {
+ let d = &s.active().unwrap().doc;
+ photocraft_compose::render(d, d.bounds()).px
+ };
+ // Give the layer the cached-pixels state a PSD import produces (the cache is what Image
+ // › Transform & friends rewrite in place, without touching the Fill spec).
+ s.edit("cache", |doc, active| {
+ let fmt = doc.pixel_format();
+ let l = doc.layer_mut(active.unwrap()).unwrap();
+ let photocraft_doc::LayerContent::Fill(f) = &l.content else { return Err(EngineError::Other("not a fill layer".into())) };
+ let fill = f.clone();
+ let mut surface = photocraft_raster::Surface::new(fmt);
+ surface.fill_rect(photocraft_geom::Rect::new(8, 8, 30, 40), &[0.0, 0.0, 1.0, 1.0]);
+ l.fill_cache = Some(photocraft_doc::FillCache { fill, surface });
+ Ok(())
+ })
+ .unwrap();
+ let warm = render(&s); // populate the effect-map cache
+ let warm2 = render(&s);
+ assert_eq!(warm, warm2, "a warm cache does not change the composite");
+ // Rewrite the cached pixels in place, without touching the Fill spec.
+ s.edit("shift", |doc, active| {
+ let l = doc.layer_mut(active.unwrap()).unwrap();
+ let fc = l.fill_cache.as_mut().unwrap();
+ let moved = photocraft_algo::resample::translate_surface(&fc.surface, 12, 0);
+ fc.surface = moved;
+ Ok(())
+ })
+ .unwrap();
+ let after = render(&s);
+ assert_ne!(after, warm, "the rewritten cache changes the composite");
+ photocraft_compose::purge_effect_cache();
+ let fresh = render(&s);
+ assert_eq!(after, fresh, "the effect maps track the cache surface, not the Fill spec alone");
+}
+
+#[test]
+fn group_effect_maps_follow_child_visibility_and_opacity() {
+ // A styled group's maps come from its children's composite: hiding a child or changing its
+ // opacity must refresh the group's shadow. Warm render vs. purged render must agree.
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 64, "height": 48, "background": "transparent"})).unwrap();
+ let a = s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap();
+ s.execute("paint.pencil", json!({"points": [[20, 24]], "size": 12, "color": "#ff0000"})).unwrap();
+ let b = s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap();
+ s.execute("paint.pencil", json!({"points": [[44, 24]], "size": 12, "color": "#0000ff"})).unwrap();
+ let g = s.execute("layer.new.group", json!({})).unwrap()["layer"].as_u64().unwrap();
+ for l in [a, b] {
+ s.execute("layer.moveTo", json!({"layer": l, "target": g, "position": "into"})).unwrap();
+ }
+ s.edit("shadow", |doc, _| {
+ doc.layer_mut(LayerId(g)).unwrap().effects.items.push(photocraft_doc::Effect::default_drop_shadow());
+ Ok(())
+ })
+ .unwrap();
+ let render = |s: &Session| {
+ let d = &s.active().unwrap().doc;
+ photocraft_compose::render(d, d.bounds()).px
+ };
+ let mut prev = render(&s); // populate the effect-map cache
+ type Edit = (&'static str, fn(&mut photocraft_doc::Layer));
+ let edits: [Edit; 2] = [("hide", |l| l.visible = false), ("opacity", |l| l.opacity = 0.3)];
+ for ((name, edit), child) in edits.into_iter().zip([a, b]) {
+ let child = LayerId(child);
+ s.edit(name, |doc, _| {
+ edit(doc.layer_mut(child).unwrap());
+ Ok(())
+ })
+ .unwrap();
+ let warm = render(&s);
+ assert_ne!(warm, prev, "{name}: the composite changes");
+ photocraft_compose::purge_effect_cache();
+ assert_eq!(warm, render(&s), "{name}: the group's maps follow its children");
+ prev = warm;
+ }
+}
+
#[test]
fn journal_records_mutations_only() {
let mut s = session_with_doc();