Fix effects are failed to be observed

This commit is contained in:
Kyant0 committed 2025-09-22 22:46:53 +08:00
1 parent 47efb1ec77
commit 98094adf9b
3 files changed
+69 -39

No files matched your search

@@ -4,10 +4,6 @@ import android.graphics.RenderEffect
import android.graphics.RuntimeShader
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.drawscope.DrawScope
@@ -27,10 +23,10 @@ interface BackdropEffectScope : Density, RuntimeShaderCacheScope {
internal abstract class BackdropEffectScopeImpl : BackdropEffectScope {
override var density: Float by mutableFloatStateOf(1f)
override var fontScale: Float by mutableFloatStateOf(1f)
override var size: Size by mutableStateOf(Size.Unspecified)
override var layoutDirection: LayoutDirection by mutableStateOf(LayoutDirection.Ltr)
override var density: Float = 1f
override var fontScale: Float = 1f
override var size: Size = Size.Unspecified
override var layoutDirection: LayoutDirection = LayoutDirection.Ltr
override var renderEffect: RenderEffect? = null
private val runtimeShaders = mutableMapOf<String, RuntimeShader>()
@@ -40,11 +36,32 @@ internal abstract class BackdropEffectScopeImpl : BackdropEffectScope {
return runtimeShaders.getOrPut(key) { RuntimeShader(string) }
}
fun applyDrawScope(scope: DrawScope) {
density = scope.density
fontScale = scope.fontScale
size = scope.size
layoutDirection = scope.layoutDirection
fun update(scope: DrawScope): Boolean {
val newDensity = scope.density
val newFontScale = scope.fontScale
val newSize = scope.size
val newLayoutDirection = scope.layoutDirection
val changed = newDensity != density ||
newFontScale != fontScale ||
newSize != size ||
newLayoutDirection != layoutDirection
if (changed) {
density = newDensity
fontScale = newFontScale
size = newSize
layoutDirection = newLayoutDirection
}
return changed
}
fun reset() {
density = 1f
fontScale = 1f
size = Size.Unspecified
layoutDirection = LayoutDirection.Ltr
renderEffect = null
}
}
@@ -159,15 +159,6 @@ private class ContentBackdropNode(
null
}
private var isCacheValid = false
private val cacheDrawBlock: DrawScope.() -> Unit = {
if (effectScope != null) {
effectScope.applyDrawScope(this)
effects(effectScope)
graphicsLayer?.renderEffect = effectScope.renderEffect?.asComposeRenderEffect()
}
}
private val layoutLayerBlock: GraphicsLayerScope.() -> Unit = {
clip = true
shape = shapeProvider.shape
@@ -188,9 +179,8 @@ private class ContentBackdropNode(
override fun ContentDrawScope.draw() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (!isCacheValid) {
observeReads { cacheDrawBlock() }
isCacheValid = true
if (effectScope != null && effectScope.update(this)) {
updateEffects()
}
}
@@ -227,13 +217,28 @@ private class ContentBackdropNode(
}
fun invalidateDrawCache() {
isCacheValid = false
observeEffects()
invalidateDraw()
}
private fun observeEffects() {
if (effectScope != null) {
observeReads { updateEffects() }
}
}
private fun updateEffects() {
if (effectScope != null) {
effectScope.renderEffect = null
effects(effectScope)
graphicsLayer?.renderEffect = effectScope.renderEffect?.asComposeRenderEffect()
}
}
override fun onAttach() {
val graphicsContext = requireGraphicsContext()
graphicsLayer = graphicsContext.createGraphicsLayer()
observeEffects()
}
override fun onDetach() {
@@ -242,5 +247,7 @@ private class ContentBackdropNode(
graphicsContext.releaseGraphicsLayer(layer)
graphicsLayer = null
}
inverseLayerScope = null
effectScope?.reset()
}
}
@@ -197,15 +197,6 @@ private class DrawBackdropNode(
null
}
private var isCacheValid = false
private val cacheDrawBlock: DrawScope.() -> Unit = {
if (effectScope != null) {
effectScope.applyDrawScope(this)
effects(effectScope)
graphicsLayer?.renderEffect = effectScope.renderEffect?.asComposeRenderEffect()
}
}
private val layoutLayerBlock: GraphicsLayerScope.() -> Unit = {
clip = true
shape = shapeProvider.shape
@@ -231,9 +222,8 @@ private class DrawBackdropNode(
override fun ContentDrawScope.draw() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (!isCacheValid) {
observeReads { cacheDrawBlock() }
isCacheValid = true
if (effectScope != null && effectScope.update(this)) {
updateEffects()
}
}
@@ -283,13 +273,28 @@ private class DrawBackdropNode(
}
fun invalidateDrawCache() {
isCacheValid = false
observeEffects()
invalidateDraw()
}
private fun observeEffects() {
if (effectScope != null) {
observeReads { updateEffects() }
}
}
private fun updateEffects() {
if (effectScope != null) {
effectScope.renderEffect = null
effects(effectScope)
graphicsLayer?.renderEffect = effectScope.renderEffect?.asComposeRenderEffect()
}
}
override fun onAttach() {
val graphicsContext = requireGraphicsContext()
graphicsLayer = graphicsContext.createGraphicsLayer()
observeEffects()
}
override fun onDetach() {
@@ -299,5 +304,6 @@ private class DrawBackdropNode(
graphicsLayer = null
}
recordBlock = null
effectScope?.reset()
}
}