Modern Apps git

← Modern-Apps.git

photos: the editor preview clips the active clipped layer like the export does

Commit
d793f93ca154ce68c8033009b3a9dd3b2fd35683
Author
Vayun Mathur <[email protected]>
When
2026-12-01 11:27 UTC (5 days ago)
Parents
a618aae
compositePreview splits the stack below/at the active layer
and renders the rest standalone, but clip lookup searches
only the list it is handed: when the split fell between a
clipped layer and its clip base, the base was in the other
segment, the lookup returned null, and clipping was silently
dropped. A 16x16 document (red left half base, fully blue
clipped layer, active index 1) composited correctly for
export (right half transparent) while the preview showed it
blue — the user edited against one image and saved another.
The group case was already special-cased for the same
split-inside-a-run reason; clipping had not been.

If the rest segment's first layer is clipped, the preview
now renders the whole stack, matching the export. Two
regression tests (plain clipped-active, clipped chain)
failed pre-fix. Edit-document algebra, undo bounds, blend
modes and crop math were audited and cleared. Verified:
targeted tests pass; the one full-suite failure is an
unrelated sync/favorites test failing on isolated rerun,
reported rather than chased; lint errors are pre-existing.

Diff

photos/src/main/java/com/vayunmathur/photos/platform/LayerCompositor.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/platform/LayerCompositor.kt b/photos/src/main/java/com/vayunmathur/photos/platform/LayerCompositor.ktindex 4606ab01f7..3ef81ddee3 100644--- a/photos/src/main/java/com/vayunmathur/photos/platform/LayerCompositor.kt+++ b/photos/src/main/java/com/vayunmathur/photos/platform/LayerCompositor.kt@@ -63,16 +63,20 @@ class LayerCompositor {         val (w, h) = targetSize(document, maxDimension)         val layers = document.layers +        val active = document.activeLayerIndex.coerceIn(0, layers.size)+         // Groups composite as contiguous runs, so the below/rest split (which can         // fall inside a run) would be wrong — render the whole stack instead.-        if (document.groups.isNotEmpty()) {+        // The same applies when the first layer of the "rest" segment is clipped:+        // its clip base lies in the "below" segment, which renderOneLayer would+        // not see (it only searches the list it is handed), so the layer would+        // render unclipped in preview but clipped in the full composite.+        if (document.groups.isNotEmpty() || layers.getOrNull(active)?.clipped == true) {             val backdrop = IntArray(w * h)             renderLayersInto(backdrop, layers, document, w, h)             return applyDocumentTransforms(bitmapFromInts(backdrop, w, h), document)         } -        val active = document.activeLayerIndex.coerceIn(0, layers.size)-         val below = if (active > 0) layers.subList(0, active) else emptyList()         val key = cacheKey(below, w, h)         if (key != belowKey || belowCache == null) {
photos/src/test/java/com/vayunmathur/photos/util/CompositorPreviewClipTest.kt
diff --git a/photos/src/test/java/com/vayunmathur/photos/util/CompositorPreviewClipTest.kt b/photos/src/test/java/com/vayunmathur/photos/util/CompositorPreviewClipTest.ktnew file mode 100644index 0000000000..4106650f50--- /dev/null+++ b/photos/src/test/java/com/vayunmathur/photos/util/CompositorPreviewClipTest.kt@@ -0,0 +1,83 @@+package com.vayunmathur.photos.util++import android.graphics.Bitmap+import android.graphics.Color+import com.vayunmathur.photos.data.BitmapReference+import com.vayunmathur.photos.data.EditDocument+import com.vayunmathur.photos.data.PixelLayer+import com.vayunmathur.photos.platform.LayerCompositor+import kotlin.test.Test+import kotlin.test.assertEquals+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config+import org.robolectric.annotation.GraphicsMode++/**+ * Regression: a layer clipped to the layer below it must render identically in+ * the cached preview and in the full composite, even when the clipped layer is+ * the active layer (the preview's below/rest split point).+ */+@GraphicsMode(GraphicsMode.Mode.NATIVE)+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class CompositorPreviewClipTest {++    private fun halfOpaqueBase(): Bitmap {+        val bmp = Bitmap.createBitmap(16, 16, Bitmap.Config.ARGB_8888)+        for (y in 0 until 16) {+            for (x in 0 until 16) {+                bmp.setPixel(x, y, if (x < 8) Color.RED else Color.TRANSPARENT)+            }+        }+        return bmp+    }++    private fun solid(color: Int): Bitmap =+        Bitmap.createBitmap(16, 16, Bitmap.Config.ARGB_8888).apply { eraseColor(color) }++    private fun clippedDoc(activeIndex: Int): EditDocument =+        EditDocument(canvasWidth = 16, canvasHeight = 16)+            .addLayer(PixelLayer(bitmapRef = BitmapReference(halfOpaqueBase()), name = "base"))+            .addLayer(+                PixelLayer(+                    bitmapRef = BitmapReference(solid(Color.BLUE)),+                    name = "clipped",+                    clipped = true,+                )+            )+            .copy(activeLayerIndex = activeIndex)++    @Test fun clippedActiveLayerPreviewMatchesComposite() {+        val doc = clippedDoc(activeIndex = 1)+        val compositor = LayerCompositor()+        val full = compositor.composite(doc)+        val preview = compositor.compositePreview(doc, 64)++        // Inside the clip base's opaque half the clipped layer shows.+        assertEquals(Color.BLUE, full.getPixel(4, 8))+        assertEquals(Color.BLUE, preview.getPixel(4, 8))+        // Outside it (transparent base) the clipped layer contributes nothing.+        assertEquals(Color.TRANSPARENT, full.getPixel(12, 8))+        assertEquals(full.getPixel(12, 8), preview.getPixel(12, 8))+    }++    @Test fun clippedChainAtActiveLayerPreviewMatchesComposite() {+        val doc = clippedDoc(activeIndex = 1)+            .addLayer(+                PixelLayer(+                    bitmapRef = BitmapReference(solid(Color.GREEN)),+                    name = "clipped-top",+                    clipped = true,+                )+            )+            .copy(activeLayerIndex = 1)+        val compositor = LayerCompositor()+        val full = compositor.composite(doc)+        val preview = compositor.compositePreview(doc, 64)++        assertEquals(full.getPixel(4, 8), preview.getPixel(4, 8))+        assertEquals(full.getPixel(12, 8), preview.getPixel(12, 8))+        assertEquals(Color.TRANSPARENT, preview.getPixel(12, 8))+    }+}