Compose: a clipped adjustment inside a pass-through clip base damages the whole group (#6535)

A pass-through group that is the base of a clipping group renders isolated while it holds a
visible adjustment (`clip_base_adjusts`), so showing, hiding, adding or removing a clipped
adjustment inside it can switch the group's other children (a Multiply layer beside the base)
between isolated and pass-through rendering. `change_bounds_in` now takes the list holding the
parent group and reports that group's change bounds in this case; `layers_damage`, `step_damage`
and `one_sided_bounds` pass it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
David MannandClaude Opus 5.5 committed 2026-10-19 14:14:06 +02:00
1 parent 8534f0c935
commit de2cb1f0c0
3 files changed
+123 -27

No files matched your search

+14 -2
View File
@@ -1177,14 +1177,26 @@ pub fn change_bounds(layer: &Layer, canvas: Rect) -> Option<Rect> {
/// adjustment layer: it changes only its clip base's pixels, so it reaches as far as showing,
/// hiding or restyling that base does (#6535). Still `None` when there is no base below it (the
/// compositor then draws it as an ordinary adjustment) or the base is an adjustment itself
/// (layers clipped to one composite atop the adjusted backdrop, everywhere).
pub fn change_bounds_in(layers: &[Layer], k: usize, canvas: Rect) -> Option<Rect> {
/// (layers clipped to one composite atop the adjusted backdrop, everywhere). `outer` is the list
/// holding the group whose children `layers` are, and that group's index there (`None` at the
/// top level): a pass-through group that is a clip base renders isolated while it holds a visible
/// adjustment, so showing or hiding one inside can change the whole group.
pub fn change_bounds_in(layers: &[Layer], k: usize, outer: Option<(&[Layer], usize)>, canvas: Rect) -> Option<Rect> {
let layer = layers.get(k)?;
if layer.clipped && matches!(layer.content, LayerContent::Adjustment(_)) {
let base = layers.get(..k)?.iter().rev().find(|l| !l.clipped)?;
if matches!(base.content, LayerContent::Adjustment(_)) {
return None;
}
if let Some((outer, g)) = outer
&& let Some(group) = outer.get(g)
&& group.blend == BlendMode::PassThrough
&& !group.clipped
&& outer.get(g + 1).is_some_and(|above| above.clipped)
{
// The group bounds its base too.
return change_bounds(group, canvas);
}
return change_bounds(base, canvas);
}
change_bounds(layer, canvas)
+40 -9
View File
@@ -464,7 +464,7 @@ fn clipped_adjustment_changes_only_its_base() {
adj.clipped = true;
d.layers.push(adj);
assert_eq!(change_bounds(&d.layers[3], canvas), None);
assert_eq!(change_bounds_in(&d.layers, 3, canvas), Some(base_rect));
assert_eq!(change_bounds_in(&d.layers, 3, None, canvas), Some(base_rect));
// Every pixel the toggle changes lies inside those bounds.
let shown = render(&d, canvas);
d.layers[3].visible = false;
@@ -480,24 +480,24 @@ fn clipped_adjustment_changes_only_its_base() {
}
assert_eq!(changed, base_rect);
// Other layers keep their own bounds.
assert_eq!(change_bounds_in(&d.layers, 2, canvas), Some(Rect::new(3, 2, 4, 3)));
assert_eq!(change_bounds_in(&d.layers, 9, canvas), None);
assert_eq!(change_bounds_in(&d.layers, 2, None, canvas), Some(Rect::new(3, 2, 4, 3)));
assert_eq!(change_bounds_in(&d.layers, 9, None, canvas), None);
// Unclipped, or with no base beneath it, it reaches everything.
let mut free = d.layers.clone();
free[3].clipped = false;
assert_eq!(change_bounds_in(&free, 3, canvas), None);
assert_eq!(change_bounds_in(&free, 3, None, canvas), None);
let orphan = vec![d.layers[3].clone()];
assert_eq!(change_bounds_in(&orphan, 0, canvas), None);
assert_eq!(change_bounds_in(&orphan, 0, None, canvas), None);
// Clipped layers on an adjustment composite atop the adjusted backdrop.
let on_adj = vec![d.layers[0].clone(), Layer::new("lv", LayerContent::Adjustment(Adjustment::Invert)), d.layers[3].clone()];
assert_eq!(change_bounds_in(&on_adj, 2, canvas), None);
assert_eq!(change_bounds_in(&on_adj, 2, None, canvas), None);
// A pass-through group holding an adjustment is isolated only while it has visible clipped
// layers, so they change whether its adjustment reaches the layers beneath.
let mut g = Layer::group("g", vec![solid_layer("in", base_rect, [0.2, 0.4, 0.6, 1.0]), Layer::new("inv", LayerContent::Adjustment(Adjustment::Invert))]);
g.blend = BlendMode::PassThrough;
let on_group = vec![d.layers[0].clone(), g, d.layers[3].clone()];
assert_eq!(change_bounds_in(&on_group, 2, canvas), None);
assert_eq!(change_bounds_in(&on_group, 2, None, canvas), None);
}
/// #6535: a clipped adjustment with no base beneath it, inside a pass-through group, composites
@@ -518,12 +518,43 @@ fn orphan_clipped_adjustment_in_pass_through_base_reaches_everything() {
let shown = px(&d, 0, 0);
d.layers[2].visible = false;
assert!(!close4(shown, px(&d, 0, 0)), "the toggle changes pixels outside the group");
assert_eq!(change_bounds_in(&d.layers, 2, canvas), None);
assert_eq!(change_bounds_in(&d.layers, 2, None, canvas), None);
assert_eq!(change_bounds(&d.layers[1], canvas), None);
// With a base beneath it the clipped adjustment stays inside the group.
let LayerContent::Group(g) = &mut d.layers[1].content else { panic!("group") };
g.children.swap(0, 1);
assert_eq!(change_bounds_in(&d.layers, 2, canvas), Some(Rect::new(2, 1, 4, 3)));
assert_eq!(change_bounds_in(&d.layers, 2, None, canvas), Some(Rect::new(2, 1, 4, 3)));
}
/// A pass-through group that is a clip base renders isolated while it holds a visible
/// adjustment, so hiding a clipped adjustment inside it changes the group's other children too:
/// its damage is the group's, not just its base's.
#[test]
fn clipped_adjustment_in_pass_through_clip_base_reaches_its_group() {
let canvas = Rect::new(0, 0, 10, 8);
let mut d = doc_white(10, 8);
d.layers.push(solid_layer("bg", canvas, [0.5, 0.5, 0.5, 1.0]));
let mut a = Layer::new("A", LayerContent::Adjustment(Adjustment::Invert));
a.clipped = true;
let mut q = solid_layer("Q", Rect::new(6, 4, 8, 6), [0.5, 0.5, 0.5, 1.0]);
q.blend = BlendMode::Multiply;
let mut g = Layer::group("G", vec![solid_layer("P", Rect::new(2, 1, 4, 3), [0.2, 0.4, 0.6, 1.0]), a, q]);
g.blend = BlendMode::PassThrough;
d.layers.push(g);
let mut x = solid_layer("X", Rect::new(2, 1, 3, 2), [1.0, 0.0, 0.0, 1.0]);
x.clipped = true;
d.layers.push(x);
let shown = px(&d, 7, 5);
let LayerContent::Group(g) = &mut d.layers[2].content else { panic!("group") };
g.children[1].visible = false;
assert!(!close4(shown, px(&d, 7, 5)), "hiding A changes Q, outside A's base");
let group = Rect::new(2, 1, 8, 6);
let LayerContent::Group(g) = &d.layers[2].content else { panic!("group") };
assert_eq!(change_bounds_in(&g.children, 1, Some((&d.layers, 2)), canvas), Some(group));
// Not a clip base: the group stays pass-through either way, and A changes only its base.
d.layers.truncate(3);
let LayerContent::Group(g) = &d.layers[2].content else { panic!("group") };
assert_eq!(change_bounds_in(&g.children, 1, Some((&d.layers, 2)), canvas), Some(Rect::new(2, 1, 4, 3)));
}
#[test]
+69 -16
View File
@@ -502,8 +502,8 @@ pub fn layers_damage(before: &Document, after: &Document, ids: &[LayerId]) -> Op
let canvas = d.bounds();
for id in ids {
// A layer gone from one side (never for a move) changes who knows what.
let (siblings, k) = siblings_of(d, *id)?;
let b = photocraft_compose::change_bounds_in(siblings, k, canvas)?;
let (siblings, k, outer) = siblings_of(d, *id)?;
let b = photocraft_compose::change_bounds_in(siblings, k, outer, canvas)?;
if !b.is_empty() {
out = if out.is_empty() { b } else { out.union(&b) };
}
@@ -512,12 +512,15 @@ pub fn layers_damage(before: &Document, after: &Document, ids: &[LayerId]) -> Op
Some(out)
}
/// The sibling list holding layer `id` in `doc` (bottom→top) and its index there.
fn siblings_of(doc: &Document, id: LayerId) -> Option<(&[Layer], usize)> {
/// The sibling list holding layer `id` in `doc` (bottom→top), its index there, and the list
/// holding its parent group with the group's index (`None` at the top level).
type Siblings<'d> = (&'d [Layer], usize, Option<(&'d [Layer], usize)>);
fn siblings_of(doc: &Document, id: LayerId) -> Option<Siblings<'_>> {
let path = doc.path_of(id)?;
let (&k, parent) = path.split_last()?;
let siblings = if parent.is_empty() { doc.layers.as_slice() } else { doc.layer_at(parent)?.children()? };
Some((siblings, k))
let Some((&g, grand)) = parent.split_last() else { return Some((doc.layers.as_slice(), k, None)) };
let outer = if grand.is_empty() { doc.layers.as_slice() } else { doc.layer_at(grand)?.children()? };
Some((outer.get(g)?.children()?, k, Some((outer, g))))
}
/// Report [`layers_damage`] as the active document's last change, so the canvas recomposites
@@ -770,11 +773,13 @@ pub fn step_damage(a: &Document, b: &Document) -> Option<Rect> {
if !doc_pixels_equal(a, b) {
return None;
}
fn walk(xs: &[Layer], ys: &[Layer], a: &Document, b: &Document, out: &mut Rect) -> bool {
/// The list holding a sibling list's parent group, and the group's index there.
type Outer<'d> = Option<(&'d [Layer], usize)>;
fn walk(xs: &[Layer], ys: &[Layer], (ox, oy): (Outer, Outer), a: &Document, b: &Document, out: &mut Rect) -> bool {
// Pair the layers by id, stepping over layers only one side has: one that draws nothing
// changes no pixels (a new empty layer or group, #1771), any other only where it draws.
let one_sided = |layers: &[Layer], k: usize, canvas: Rect, out: &mut Rect| {
let Some(r) = one_sided_bounds(layers, k, canvas) else { return false };
let one_sided = |layers: &[Layer], k: usize, outer: Outer, canvas: Rect, out: &mut Rect| {
let Some(r) = one_sided_bounds(layers, k, outer, canvas) else { return false };
if !r.is_empty() {
*out = if out.is_empty() { r } else { out.union(&r) };
}
@@ -794,14 +799,14 @@ pub fn step_damage(a: &Document, b: &Document) -> Option<Rect> {
continue;
}
(_, Some(y)) if xs.iter().all(|x| x.id != y.id) => {
if !one_sided(ys, j, b.bounds(), out) {
if !one_sided(ys, j, oy, b.bounds(), out) {
return false;
}
j += 1;
continue;
}
(Some(x), _) if ys.iter().all(|y| y.id != x.id) => {
if !one_sided(xs, i, a.bounds(), out) {
if !one_sided(xs, i, ox, a.bounds(), out) {
return false;
}
i += 1;
@@ -824,7 +829,8 @@ pub fn step_damage(a: &Document, b: &Document) -> Option<Rect> {
if !layer_pixels_equal(x, y) {
// Both sides' extents: a move changes pixels where it left and where it landed.
// A clipped adjustment reaches as far as its base (#6535).
let (Some(bx), Some(by)) = (photocraft_compose::change_bounds_in(xs, xk, a.bounds()), photocraft_compose::change_bounds_in(ys, yk, b.bounds()))
let (Some(bx), Some(by)) =
(photocraft_compose::change_bounds_in(xs, xk, ox, a.bounds()), photocraft_compose::change_bounds_in(ys, yk, oy, b.bounds()))
else {
return false;
};
@@ -832,7 +838,7 @@ pub fn step_damage(a: &Document, b: &Document) -> Option<Rect> {
}
match (x.children(), y.children()) {
(Some(xc), Some(yc)) => {
if !walk(xc, yc, a, b, out) {
if !walk(xc, yc, (Some((xs, xk)), Some((ys, yk))), a, b, out) {
return false;
}
}
@@ -842,7 +848,7 @@ pub fn step_damage(a: &Document, b: &Document) -> Option<Rect> {
}
}
let mut out = Rect::EMPTY;
if walk(&a.layers, &b.layers, a, b, &mut out) { Some(out) } else { None }
if walk(&a.layers, &b.layers, (None, None), a, b, &mut out) { Some(out) } else { None }
}
/// The pixels adding or removing `layers[k]` can change, when that is bounded: its change bounds
@@ -851,7 +857,7 @@ pub fn step_damage(a: &Document, b: &Document) -> Option<Rect> {
/// of clipped layers above it (they would change base), and neither it nor anything inside it is
/// a video layer or excludes channels (both reach outside the layer's bounds, see
/// [`step_damage`]).
fn one_sided_bounds(layers: &[Layer], k: usize, canvas: Rect) -> Option<Rect> {
fn one_sided_bounds(layers: &[Layer], k: usize, outer: Option<(&[Layer], usize)>, canvas: Rect) -> Option<Rect> {
fn contained(l: &Layer, depth: usize) -> bool {
depth < 256 && l.video.is_none() && l.excluded_channels == 0 && l.children().is_none_or(|c| c.iter().all(|c| contained(c, depth + 1)))
}
@@ -860,7 +866,7 @@ fn one_sided_bounds(layers: &[Layer], k: usize, canvas: Rect) -> Option<Rect> {
if new_base || !contained(l, 0) {
return None;
}
photocraft_compose::change_bounds_in(layers, k, canvas)
photocraft_compose::change_bounds_in(layers, k, outer, canvas)
}
/// Whether `layers[k]` draws nothing wherever it sits, so adding or removing it leaves the
@@ -1858,6 +1864,53 @@ mod tests {
}
}
/// #6535: hiding a clipped adjustment inside a pass-through group that is itself a clip base
/// can switch the group between isolated and pass-through rendering, so the step damages the
/// whole group, on undo, redo and when the adjustment is deleted too.
#[test]
fn clipped_adjustment_in_pass_through_clip_base_damages_its_group() {
use photocraft_doc::{Adjustment, BlendMode};
let solid = |name: &str, r: Rect, blend: BlendMode| {
let mut l = Layer::raster(name, PixelFormat::RGBA8);
l.surface_mut().unwrap().fill_rect(r, &[0.5, 0.5, 0.5, 1.0]);
l.blend = blend;
l
};
let mut before = Document::with_background(
"t",
photocraft_geom::Size::new(10, 8),
photocraft_doc::ColorMode::Rgb,
photocraft_doc::SampleType::U8,
photocraft_color::Color::WHITE,
);
before.layers.push(solid("bg", before.bounds(), BlendMode::Normal));
let mut a = Layer::new("A", LayerContent::Adjustment(Adjustment::Invert));
a.clipped = true;
let mut g = Layer::group("G", vec![solid("P", Rect::new(2, 1, 4, 3), BlendMode::Normal), a, solid("Q", Rect::new(6, 4, 8, 6), BlendMode::Multiply)]);
g.blend = BlendMode::PassThrough;
before.layers.push(g);
let mut x = solid("X", Rect::new(2, 1, 3, 2), BlendMode::Normal);
x.clipped = true;
before.layers.push(x);
let mut after = before.clone();
let LayerContent::Group(g) = &mut after.layers[2].content else { panic!("group") };
g.children[1].visible = false;
let group = Some(Rect::new(2, 1, 8, 6));
assert_eq!(step_damage(&before, &after), group);
assert_eq!(step_damage(&after, &before), group);
let mut deleted = before.clone();
let LayerContent::Group(g) = &mut deleted.layers[2].content else { panic!("group") };
g.children.remove(1);
assert_eq!(step_damage(&before, &deleted), group);
let LayerContent::Group(g) = &before.layers[2].content else { panic!("group") };
let ids = [g.children[1].id];
assert_eq!(layers_damage(&before, &after, &ids), group);
// Without the layer clipped to the group it stays pass-through: just A's base.
before.layers.truncate(3);
after.layers.truncate(3);
assert_eq!(step_damage(&before, &after), Some(Rect::new(2, 1, 4, 3)));
}
/// #2883 (P8/P9): a duplicate shares the source's tiles (no pixel copy) and recomposites only
/// where it draws; undo brings back exactly the old pixels. A copy that becomes the clipping
/// base of the layers above still recomposites everything.