Improve performance for refractionWithDispersion

This commit is contained in:
Kyant0 committed 2025-09-15 11:22:40 +08:00
1 parent 8f4cbb3aa7
commit 29a733d547
2 files changed
+25 -11

No files matched your search

@@ -20,9 +20,6 @@ interface BackdropEffectScope : Density {
var renderEffect: RenderEffect?
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
fun obtainRuntimeShader(key: String, string: String): RuntimeShader
val cornerRadiusArray: FloatArray
get() {
val shape = shape
@@ -41,4 +38,7 @@ interface BackdropEffectScope : Density {
)
return cornerRadius
}
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
fun obtainRuntimeShader(key: String, string: String): RuntimeShader
}
@@ -50,14 +50,28 @@ fun BackdropEffectScope.refractionWithDispersion(
hasDepthEffect: Boolean = false,
dispersionIntensity: Float = 1f
) {
refraction(
height = height,
amount = amount,
hasDepthEffect = hasDepthEffect
)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return
if (height <= 0f || amount <= 0f) return
dispersion(
height = height * dispersionIntensity,
amount = amount * dispersionIntensity
val cornerRadiusArray = cornerRadiusArray
val refractionShader = obtainRuntimeShader("Refraction", RefractionShaderString).apply {
setFloatUniform("size", size.width, size.height)
setFloatUniform("cornerRadius", cornerRadiusArray)
setFloatUniform("refractionHeight", height)
setFloatUniform("refractionAmount", -amount)
setFloatUniform("depthEffect", if (hasDepthEffect) 1f else 0f)
}
val dispersionShader = obtainRuntimeShader("Dispersion", DispersionShaderString).apply {
setFloatUniform("size", size.width, size.height)
setFloatUniform("cornerRadius", cornerRadiusArray)
setFloatUniform("dispersionHeight", height * dispersionIntensity)
setFloatUniform("dispersionAmount", amount * dispersionIntensity)
}
val effect = RenderEffect.createChainEffect(
RenderEffect.createRuntimeShaderEffect(dispersionShader, "image"),
RenderEffect.createRuntimeShaderEffect(refractionShader, "image")
)
effect(effect)
}