Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 66444b4c authored by Sherry Zhou's avatar Sherry Zhou
Browse files

Fix snow accumulation thickness in different image size

Bug: 375272898
Flag: EXEMPT only change in Magic Portrait
Test: manual test wallpaper of different sizes, the thickness of snow
accumulation stays the same

Change-Id: I5b01e52c9ef5d4910742312178dd2819d64023dd
parent 4d369e3d
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -15,8 +15,9 @@
 */

uniform shader foreground;
uniform half imageWidth;
uniform half snowThickness;
uniform half scale;
uniform half screenWidth;

#include "shaders/simplex2d.agsl"
#include "shaders/utils.agsl"
@@ -27,7 +28,7 @@ float random(vec2 uv) {

vec4 main(float2 fragCoord) {
    // fragCoord should be already the adjusted UVs to have the expected rect of the image.
    vec2 uv = fragCoord / imageWidth;
    vec2 uv = fragCoord * scale / screenWidth;
    float variation = 0.3 + simplex2d(11. * uv);
    float distance = variation * snowThickness;

+32 −18
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import com.google.android.wallpaper.weathereffects.graphics.utils.GraphicsUtils
import com.google.android.wallpaper.weathereffects.graphics.utils.MathUtils
import com.google.android.wallpaper.weathereffects.graphics.utils.MatrixUtils.centerCropMatrix
import com.google.android.wallpaper.weathereffects.graphics.utils.MatrixUtils.extractTranslationMatrix
import com.google.android.wallpaper.weathereffects.graphics.utils.MatrixUtils.getScale
import com.google.android.wallpaper.weathereffects.graphics.utils.MatrixUtils.postprocessParallaxMatrix
import com.google.android.wallpaper.weathereffects.graphics.utils.TimeUtils
import java.util.concurrent.Executor
@@ -56,10 +57,18 @@ class SnowEffect(
    private var frameBuffer = FrameBuffer(background.width, background.height)
    private val frameBufferPaint = Paint().also { it.shader = snowConfig.accumulatedSnowShader }

    private var matrix: Matrix? = null
    private var matrix: Matrix =
        centerCropMatrix(
            surfaceSize,
            SizeF(this.foreground.width.toFloat(), this.foreground.height.toFloat()),
        )

    private var scale = getScale(matrix)

    init {
        frameBuffer.setRenderEffect(RenderEffect.createBlurEffect(4f, 4f, Shader.TileMode.CLAMP))
        frameBuffer.setRenderEffect(
            RenderEffect.createBlurEffect(4f / scale, 4f / scale, Shader.TileMode.CLAMP)
        )
        updateTextureUniforms()
        adjustCropping(surfaceSize)
        prepareColorGrading()
@@ -71,9 +80,9 @@ class SnowEffect(
    }

    override fun resize(newSurfaceSize: SizeF) {
        surfaceSize = newSurfaceSize
        adjustCropping(newSurfaceSize)
        updateSnowGridSize(newSurfaceSize)
        surfaceSize = newSurfaceSize
    }

    override fun update(deltaMillis: Long, frameTimeNanos: Long) {
@@ -108,10 +117,7 @@ class SnowEffect(
            "intensity",
            snowConfig.colorGradingIntensity * intensity,
        )
        snowConfig.accumulatedSnowShader.setFloatUniform(
            "snowThickness",
            snowConfig.maxAccumulatedSnowThickness * intensity,
        )
        this.intensity = intensity
        // Regenerate accumulated snow since the uniform changed.
        generateAccumulatedSnow()
    }
@@ -128,7 +134,19 @@ class SnowEffect(
        this.background = background
        this.foreground = foreground ?: background

        frameBuffer = FrameBuffer(background.width, background.height)
        matrix =
            centerCropMatrix(
                surfaceSize,
                SizeF(this.foreground.width.toFloat(), this.foreground.height.toFloat()),
            )

        scale = getScale(matrix)
        frameBuffer =
            FrameBuffer(background.width, background.height).apply {
                setRenderEffect(
                    RenderEffect.createBlurEffect(4f / scale, 4f / scale, Shader.TileMode.CLAMP)
                )
            }
        snowConfig.shader.setInputBuffer(
            "background",
            BitmapShader(this.background, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR),
@@ -147,17 +165,11 @@ class SnowEffect(
    override fun setMatrix(matrix: Matrix) {
        this.matrix = matrix
        adjustCropping(surfaceSize)
        generateAccumulatedSnow()
    }

    private fun adjustCropping(surfaceSize: SizeF) {
        if (matrix == null) {
            matrix =
                centerCropMatrix(
                    surfaceSize,
                    SizeF(foreground.width.toFloat(), foreground.height.toFloat()),
                )
        }
        val postprocessedMatrix = postprocessParallaxMatrix(matrix!!)
        val postprocessedMatrix = postprocessParallaxMatrix(matrix)
        val weatherMatrix = extractTranslationMatrix(postprocessedMatrix)
        snowConfig.shader.setFloatUniform("transformMatrixFgd", postprocessedMatrix)
        snowConfig.shader.setFloatUniform("transformMatrixBgd", postprocessedMatrix)
@@ -198,10 +210,12 @@ class SnowEffect(

    private fun generateAccumulatedSnow() {
        val renderingCanvas = frameBuffer.beginDrawing()
        snowConfig.accumulatedSnowShader.setFloatUniform("scale", scale)
        snowConfig.accumulatedSnowShader.setFloatUniform(
            "imageWidth",
            renderingCanvas.width.toFloat(),
            "snowThickness",
            snowConfig.maxAccumulatedSnowThickness * intensity / scale,
        )
        snowConfig.accumulatedSnowShader.setFloatUniform("screenWidth", surfaceSize.width)
        snowConfig.accumulatedSnowShader.setInputBuffer(
            "foreground",
            BitmapShader(foreground, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR),
+5 −0
Original line number Diff line number Diff line
@@ -67,4 +67,9 @@ object MatrixUtils {
            set(7, matrix[7])
        }
    }

    fun getScale(matrix: Matrix): Float {
        matrix.getValues(matrixValues)
        return matrixValues[0]
    }
}