PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(compose): hash surface defaults in effect cache identity (#105)
MikeeI4af95fa807de
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rsindex 54e2b6aa1..f450d6ee1 100644--- a/crates/compose/src/lib.rs+++ b/crates/compose/src/lib.rs@@ -968,11 +968,19 @@ fn shape_parts(layer: &Layer, clipped: &[Layer], rect: Rect, cx: &Ctx) -> Optionfn layer_identity(layer: &Layer, h: &mut std::collections::hash_map::DefaultHasher) {use std::hash::{Hash, Hasher};fn surface_fp(s: &Surface) -> u64 {- s.tiles().fold(s.tile_count() as u64, |acc, (c, t)| {+ let mut h = std::collections::hash_map::DefaultHasher::new();+ s.format().hash(&mut h);+ // Missing tiles read the default pixel, so tile identity alone is not pixel identity.+ for sample in s.default_pixel() {+ sample.to_bits().hash(&mut h);+ }+ let tiles = s.tiles().fold(s.tile_count() as u64, |acc, (c, t)| {let mut x = (std::sync::Arc::as_ptr(t) as usize as u64) ^ ((c.tx as u64) << 40) ^ ((c.ty as u32 as u64) << 8);x = x.wrapping_mul(0x9e37_79b9_7f4a_7c15);acc.wrapping_add(x ^ (x >> 29))- })+ });+ tiles.hash(&mut h);+ h.finish()}layer.id.0.hash(h);layer.visible.hash(h);diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rsindex e003e21ee..24c3e2591 100644--- a/crates/compose/src/tests.rs+++ b/crates/compose/src/tests.rs@@ -684,6 +684,17 @@ fn effect_maps_are_cached_and_invalidated_by_pixel_changes() {assert!(close4(*p, *q));}assert_ne!(first.px, again.px);++ // Tileless masks differ only in their defaults; reusing their effect maps leaves a ghost shadow.+ let mut masked = fx_doc(vec![Effect::DropShadow(shadow(5.0, 90.0))]);+ masked.layers[1].mask = Some(photocraft_doc::LayerMask::reveal_all());+ assert!(close4(px(&masked, 20, 32), [0.0, 0.0, 0.0, 1.0]));+ masked.layers[1].mask = Some(photocraft_doc::LayerMask::hide_all());+ let warm = flatten(&masked);+ let mut cold = masked.clone();+ cold.layers[1].id = Layer::raster("uncached", cold.pixel_format()).id;+ assert!(warm.px == flatten(&cold).px, "default-only mask changes must invalidate cached effects");+ assert!(close4(px(&masked, 20, 32), [1.0; 4]));}// ---------- PSD-fidelity effect semantics (fitted on Photoshop composites) ----------