Skip to content

Commit

Permalink
Do not use states for layer invalidation without new layers (#1627)
Browse files Browse the repository at this point in the history
Fixes
https://youtrack.jetbrains.com/issue/CMP-6877/Do-not-use-states-for-layer-invalidation-without-new-layers

Disable state tracking for old internal layers if there is no need to do
so (no active new layers).
It's considered as a temporary solution for 1.7.x

It's probably better to wait @igordmn before merging

## Testing
Run [LazyGrid
benchmark](https://github.com/JetBrains/compose-multiplatform/blob/master/benchmarks/ios/jvm-vs-kotlin-native/src/commonMain/kotlin/benchmarks/lazygrid/LazyGrid.kt)
  • Loading branch information
MatkovIvan authored Nov 1, 2024
1 parent 33496e5 commit 619dafa
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions compose/ui/ui-graphics/api/desktop/ui-graphics.api
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,7 @@ public final class androidx/compose/ui/graphics/SkiaGraphicsContext : androidx/c
public fun <init> ()V
public fun createGraphicsLayer ()Landroidx/compose/ui/graphics/layer/GraphicsLayer;
public final fun dispose ()V
public final fun getActiveGraphicsLayersCount ()I
public fun releaseGraphicsLayer (Landroidx/compose/ui/graphics/layer/GraphicsLayer;)V
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class SkiaGraphicsContext() : GraphicsContext {
command()
}

// Temporary workaround to disable state tracking workaround inside old internal layers
var activeGraphicsLayersCount = 0
private set

init {
snapshotObserver.start()
}
Expand All @@ -36,10 +40,14 @@ class SkiaGraphicsContext() : GraphicsContext {
}

override fun createGraphicsLayer(): GraphicsLayer {
activeGraphicsLayersCount++
return GraphicsLayer(snapshotObserver)
}

override fun releaseGraphicsLayer(layer: GraphicsLayer) {
layer.release()
if (!layer.isReleased) {
activeGraphicsLayersCount--
layer.release()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ internal class RootNodeOwner(
density
},
measureDrawBounds = platformContext.measureDrawLayerBounds,
requiresStateWorkaround = { graphicsContext.activeGraphicsLayersCount > 0 },
invalidateParentLayer = {
invalidateParentLayer()
snapshotInvalidationTracker.requestDraw()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import org.jetbrains.skia.ShadowUtils
internal class RenderNodeLayer(
private var density: Density,
measureDrawBounds: Boolean,
private val requiresStateWorkaround: () -> Boolean,
private val invalidateParentLayer: () -> Unit,
private val drawBlock: (canvas: Canvas, parentLayer: GraphicsLayer?) -> Unit,
private val onDestroy: () -> Unit = {}
Expand Down Expand Up @@ -218,12 +219,14 @@ internal class RenderNodeLayer(
picture?.close()
picture = null
}
drawState.value = Unit
if (requiresStateWorkaround()) {
drawState.value = Unit
}
invalidateParentLayer()
}

override fun drawLayer(canvas: Canvas, parentLayer: GraphicsLayer?) {
if (parentLayer != null) {
if (requiresStateWorkaround() && parentLayer != null) {
// Read the state because any changes to the state should trigger re-drawing of [GraphicsLayer].
drawState.value
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ class RenderNodeLayerTest {
) = RenderNodeLayer(
Density(1f, 1f),
measureDrawBounds = false,
requiresStateWorkaround = { false },
invalidateParentLayer = invalidateParentLayer,
drawBlock = drawBlock,
)
Expand Down

0 comments on commit 619dafa

Please sign in to comment.