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

Commit 23a23265 authored by Shan Huang's avatar Shan Huang
Browse files

Avoid reallocating FrameBuffer when bitmaps don't need updating.

Close the old FrameBuffer before allocating a new one.

Bug: 384121863
Fixes: 384121863
Flag: build.RELEASE_PACKAGE_MAGIC_PORTRAIT_WALLPAPERS
Test: Drag weather slider. Dump meminfo. Make sure memory doesn't grow.
Repeatly scrube the slider and make sure the app doesn't crash.
Test: atest MagicPortraitWallpapersTests

Change-Id: Ie4f1d61243fed27e3a58c7f2e9fc2d58b46f4b1e
parent 5a60b4c9
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -68,8 +68,9 @@ interface WeatherEffect {
     * @param foreground A bitmap containing the foreground of the image, will be null when
     *   segmentation hasn't finished.
     * @param background A bitmap containing the background of the image
     * @return True if the bitmaps have been updated. False otherwise.
     */
    fun setBitmaps(foreground: Bitmap?, background: Bitmap)
    fun setBitmaps(foreground: Bitmap?, background: Bitmap): Boolean

    /**
     * Apply matrix to transform coordinates in shaders. In Editor and preview, it's a center crop
+3 −2
Original line number Diff line number Diff line
@@ -93,9 +93,9 @@ abstract class WeatherEffectBase(
        colorGradingShader.setFloatUniform("intensity", colorGradingIntensity * intensity)
    }

    override fun setBitmaps(foreground: Bitmap?, background: Bitmap) {
    override fun setBitmaps(foreground: Bitmap?, background: Bitmap): Boolean {
        if (this.foreground == foreground && this.background == background) {
            return
            return false
        }
        // Only when background changes, we can infer the bitmap set changes
        if (this.background != background) {
@@ -120,6 +120,7 @@ abstract class WeatherEffectBase(
            BitmapShader(this.foreground, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR),
        )
        adjustCropping(surfaceSize)
        return true
    }

    open fun updateTextureUniforms() {
+3 −1
Original line number Diff line number Diff line
@@ -59,13 +59,15 @@ class NoEffect(

    override fun setIntensity(intensity: Float) {}

    override fun setBitmaps(foreground: Bitmap?, background: Bitmap) {
    override fun setBitmaps(foreground: Bitmap?, background: Bitmap): Boolean {
        // Only when background changes, we can infer the bitmap set changes
        if (this.background != background) {
            this.background.recycle()
            this.foreground.recycle()
            return false
        }
        this.background = background
        this.foreground = foreground ?: background
        return true
    }
}
+6 −2
Original line number Diff line number Diff line
@@ -92,8 +92,11 @@ class RainEffect(
        createOutlineBuffer()
    }

    override fun setBitmaps(foreground: Bitmap?, background: Bitmap) {
        super.setBitmaps(foreground, background)
    override fun setBitmaps(foreground: Bitmap?, background: Bitmap): Boolean {
        if (!super.setBitmaps(foreground, background)) {
            return false
        }
        outlineBuffer.close()
        outlineBuffer =
            FrameBuffer(background.width, background.height).apply {
                setRenderEffect(RenderEffect.createBlurEffect(2f, 2f, Shader.TileMode.CLAMP))
@@ -102,6 +105,7 @@ class RainEffect(

        // Need to recreate the outline buffer as the outlineBuffer has changed due to background
        createOutlineBuffer()
        return true
    }

    override val shader: RuntimeShader
+6 −2
Original line number Diff line number Diff line
@@ -97,9 +97,12 @@ class SnowEffect(
        generateAccumulatedSnow()
    }

    override fun setBitmaps(foreground: Bitmap?, background: Bitmap) {
        super.setBitmaps(foreground, background)
    override fun setBitmaps(foreground: Bitmap?, background: Bitmap): Boolean {
        if (!super.setBitmaps(foreground, background)) {
            return false
        }
        scale = getScale(parallaxMatrix)
        frameBuffer.close()
        frameBuffer =
            FrameBuffer(background.width, background.height).apply {
                setRenderEffect(
@@ -109,6 +112,7 @@ class SnowEffect(
        // GenerateAccumulatedSnow needs foreground for accumulatedSnowShader, and needs frameBuffer
        // which is also changed with background
        generateAccumulatedSnow()
        return true
    }

    override val shader: RuntimeShader