Skip to content

Commit

Permalink
Separate tracking changes inside record scope (#1574)
Browse files Browse the repository at this point in the history
Fixes https://youtrack.jetbrains.com/issue/CMP-6695

## Testing
Test case from the issue.
This should be tested by QA

## Release Notes
N/A (regression after #1555) wasn't released yet
  • Loading branch information
MatkovIvan authored Sep 18, 2024
1 parent 89fe682 commit 420d0e2
Show file tree
Hide file tree
Showing 9 changed files with 1,106 additions and 41 deletions.
2 changes: 1 addition & 1 deletion compose/ui/ui-graphics/api/desktop/ui-graphics.api
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ public final class androidx/compose/ui/graphics/SkiaColorFilter_skikoKt {
}

public final class androidx/compose/ui/graphics/SkiaGraphicsContext_skikoKt {
public static final fun GraphicsContext ()Landroidx/compose/ui/graphics/GraphicsContext;
public static final fun GraphicsContext (Landroidx/compose/runtime/snapshots/SnapshotStateObserver;)Landroidx/compose/ui/graphics/GraphicsContext;
}

public final class androidx/compose/ui/graphics/SkiaImageAsset_skikoKt {
Expand Down
14 changes: 12 additions & 2 deletions compose/ui/ui-graphics/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ if(AndroidXComposePlugin.isMultiplatformEnabled(project)) {

commonTest {
dependencies {
implementation(kotlin("test"))
implementation(libs.kotlinTest)
}
}

Expand Down Expand Up @@ -163,15 +163,25 @@ if(AndroidXComposePlugin.isMultiplatformEnabled(project)) {
implementation(libs.junit)
}

skikoTest {
dependsOn(commonTest)
dependencies {
implementation(project(":compose:ui:ui-test-junit4"))
}
}

desktopTest {
dependsOn(skikoTest)
resources.srcDirs += "src/desktopTest/res"
dependencies {
implementation(project(":compose:ui:ui-test-junit4"))
implementation(libs.junit)
implementation(libs.truth)
implementation(libs.skikoCurrentOs)
}
}
nativeTest {
dependsOn(skikoTest)
}

configureEach {
languageSettings.optIn("androidx.compose.ui.ExperimentalComposeUiApi")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package androidx.compose.ui.graphics

import androidx.compose.runtime.snapshots.SnapshotStateObserver
import androidx.compose.ui.InternalComposeUiApi
import androidx.compose.ui.graphics.layer.GraphicsLayer
import kotlin.js.JsName
Expand All @@ -25,12 +26,14 @@ import kotlin.js.JsName
*/
@InternalComposeUiApi
@JsName("createGraphicsContext")
fun GraphicsContext(): GraphicsContext =
SkiaGraphicsContext()
fun GraphicsContext(snapshotObserver: SnapshotStateObserver): GraphicsContext =
SkiaGraphicsContext(snapshotObserver)

private class SkiaGraphicsContext : GraphicsContext {
private class SkiaGraphicsContext(
private val snapshotObserver: SnapshotStateObserver
) : GraphicsContext {
override fun createGraphicsLayer(): GraphicsLayer {
return GraphicsLayer()
return GraphicsLayer(snapshotObserver)
}

override fun releaseGraphicsLayer(layer: GraphicsLayer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ package androidx.compose.ui.graphics.layer

import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.neverEqualPolicy
import androidx.compose.ui.InternalComposeUiApi
import androidx.compose.runtime.snapshots.SnapshotStateObserver
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.RoundRect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.geometry.center
import androidx.compose.ui.geometry.isUnspecified
import androidx.compose.ui.geometry.toRect
import androidx.compose.ui.graphics.BlendMode
Expand Down Expand Up @@ -55,12 +54,13 @@ import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.toSize
import kotlin.math.abs
import org.jetbrains.skia.Picture
import org.jetbrains.skia.PictureRecorder
import org.jetbrains.skia.Point3

actual class GraphicsLayer internal constructor() {
actual class GraphicsLayer internal constructor(
private val snapshotObserver: SnapshotStateObserver
) {
private val pictureDrawScope = CanvasDrawScope()
private val pictureRecorder = PictureRecorder()
private var picture: Picture? = null
Expand Down Expand Up @@ -198,9 +198,7 @@ actual class GraphicsLayer internal constructor() {
} else {
1.0f
}
childDependenciesTracker.withTracking(
onDependencyRemoved = { it.onRemovedFromParentLayer() }
) {
trackRecord {
pictureDrawScope.draw(
density,
layoutDirection,
Expand All @@ -213,6 +211,18 @@ actual class GraphicsLayer internal constructor() {
picture = pictureRecorder.finishRecordingAsPicture()
}

private fun trackRecord(block: () -> Unit) {
childDependenciesTracker.withTracking(
onDependencyRemoved = { it.onRemovedFromParentLayer() }
) {
snapshotObserver.observeReads(
scope = this,
onValueChangedForScope = { it.requestDraw() },
block = block
)
}
}

private fun addSubLayer(graphicsLayer: GraphicsLayer) {
if (childDependenciesTracker.onDependencyAdded(graphicsLayer)) {
graphicsLayer.onAddedToParentLayer()
Expand Down
Loading

0 comments on commit 420d0e2

Please sign in to comment.