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

Commit 9ea072ab authored by Yein Jo's avatar Yein Jo
Browse files

Polish turbulence noise to be more cloudy and softer.

Please find a recording in the bug

Bug: 281032715
Test: Manual, MediaControlPanelTest TurbulenceNoiseViewTest TurbulenceNoiseShaderTest TurbulenceNoiseControllerTest
Change-Id: Ic90b087fb3935bc77d85aadf05033057a44e3084
parent 83efc2a1
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
@@ -56,7 +56,19 @@ data class TurbulenceNoiseAnimationConfig(
    val easeOutDuration: Float = DEFAULT_EASING_DURATION_IN_MILLIS,
    val pixelDensity: Float = 1f,
    val blendMode: BlendMode = DEFAULT_BLEND_MODE,
    val onAnimationEnd: Runnable? = null
    val onAnimationEnd: Runnable? = null,
    /**
     * Variants in noise. Higher number means more contrast; lower number means less contrast but
     * make the noise dimmed. You may want to increase the [lumaMatteBlendFactor] to compensate.
     * Expected range [0, 1].
     */
    val lumaMatteBlendFactor: Float = DEFAULT_LUMA_MATTE_BLEND_FACTOR,
    /**
     * Offset for the overall brightness in noise. Higher number makes the noise brighter. You may
     * want to use this if you have made the noise softer using [lumaMatteBlendFactor]. Expected
     * range [0, 1].
     */
    val lumaMatteOverallBrightness: Float = DEFAULT_LUMA_MATTE_OVERALL_BRIGHTNESS
) {
    companion object {
        const val DEFAULT_MAX_DURATION_IN_MILLIS = 30_000f // Max 30 sec
@@ -66,6 +78,8 @@ data class TurbulenceNoiseAnimationConfig(
        const val DEFAULT_NOISE_SPEED_Z = 0.3f
        const val DEFAULT_OPACITY = 150 // full opacity is 255.
        const val DEFAULT_COLOR = Color.WHITE
        const val DEFAULT_LUMA_MATTE_BLEND_FACTOR = 1f
        const val DEFAULT_LUMA_MATTE_OVERALL_BRIGHTNESS = 0f
        const val DEFAULT_BACKGROUND_COLOR = Color.BLACK
        val DEFAULT_BLEND_MODE = BlendMode.SRC_OVER
    }
+37 −7
Original line number Diff line number Diff line
@@ -37,6 +37,8 @@ class TurbulenceNoiseShader(useFractal: Boolean = false) :
            uniform float in_opacity;
            uniform float in_pixelDensity;
            uniform float in_inverseLuma;
            uniform half in_lumaMatteBlendFactor;
            uniform half in_lumaMatteOverallBrightness;
            layout(color) uniform vec4 in_color;
            layout(color) uniform vec4 in_backgroundColor;
        """
@@ -48,18 +50,21 @@ class TurbulenceNoiseShader(useFractal: Boolean = false) :
                uv.x *= in_aspectRatio;

                vec3 noiseP = vec3(uv + in_noiseMove.xy, in_noiseMove.z) * in_gridNum;
                float luma = abs(in_inverseLuma - simplex3d(noiseP)) * in_opacity;
                // Bring it to [0, 1] range.
                float luma = (simplex3d(noiseP) * in_inverseLuma) * 0.5 + 0.5;
                luma = saturate(luma * in_lumaMatteBlendFactor + in_lumaMatteOverallBrightness)
                        * in_opacity;
                vec3 mask = maskLuminosity(in_color.rgb, luma);
                vec3 color = in_backgroundColor.rgb + mask * 0.6;

                // Add dither with triangle distribution to avoid color banding. Ok to dither in the
                // Add dither with triangle distribution to avoid color banding. Dither in the
                // shader here as we are in gamma space.
                float dither = triangleNoise(p * in_pixelDensity) / 255.;

                // The result color should be pre-multiplied, i.e. [R*A, G*A, B*A, A], thus need to
                // multiply rgb with a to get the correct result.
                color = (color + dither.rrr) * in_color.a;
                return vec4(color, in_color.a);
                color = (color + dither.rrr) * in_opacity;
                return vec4(color, in_opacity);
            }
        """

@@ -70,12 +75,15 @@ class TurbulenceNoiseShader(useFractal: Boolean = false) :
                uv.x *= in_aspectRatio;

                vec3 noiseP = vec3(uv + in_noiseMove.xy, in_noiseMove.z) * in_gridNum;
                float luma = abs(in_inverseLuma - simplex3d_fractal(noiseP)) * in_opacity;
                // Bring it to [0, 1] range.
                float luma = (simplex3d_fractal(noiseP) * in_inverseLuma) * 0.5 + 0.5;
                luma = saturate(luma * in_lumaMatteBlendFactor + in_lumaMatteOverallBrightness)
                        * in_opacity;
                vec3 mask = maskLuminosity(in_color.rgb, luma);
                vec3 color = in_backgroundColor.rgb + mask * 0.6;

                // Skip dithering.
                return vec4(color * in_color.a, in_color.a);
                return vec4(color * in_opacity, in_opacity);
            }
        """

@@ -124,6 +132,28 @@ class TurbulenceNoiseShader(useFractal: Boolean = false) :
        setFloatUniform("in_aspectRatio", width / max(height, 0.001f))
    }

    /**
     * Sets blend and brightness factors of the luma matte.
     *
     * @param lumaMatteBlendFactor increases or decreases the amount of variance in noise. Setting
     *   this a lower number removes variations. I.e. the turbulence noise will look more blended.
     *   Expected input range is [0, 1]. more dimmed.
     * @param lumaMatteOverallBrightness adds the overall brightness of the turbulence noise.
     *   Expected input range is [0, 1].
     *
     * Example usage: You may want to apply a small number to [lumaMatteBlendFactor], such as 0.2,
     * which makes the noise look softer. However it makes the overall noise look dim, so you want
     * offset something like 0.3 for [lumaMatteOverallBrightness] to bring back its overall
     * brightness.
     */
    fun setLumaMatteFactors(
        lumaMatteBlendFactor: Float = 1f,
        lumaMatteOverallBrightness: Float = 0f
    ) {
        setFloatUniform("in_lumaMatteBlendFactor", lumaMatteBlendFactor)
        setFloatUniform("in_lumaMatteOverallBrightness", lumaMatteOverallBrightness)
    }

    /**
     * Sets whether to inverse the luminosity of the noise.
     *
@@ -132,7 +162,7 @@ class TurbulenceNoiseShader(useFractal: Boolean = false) :
     * true.
     */
    fun setInverseNoiseLuminosity(inverse: Boolean) {
        setFloatUniform("in_inverseLuma", if (inverse) 1f else 0f)
        setFloatUniform("in_inverseLuma", if (inverse) -1f else 1f)
    }

    /** Current noise movements in x, y, and z axes. */
+3 −1
Original line number Diff line number Diff line
@@ -215,10 +215,12 @@ class TurbulenceNoiseView(context: Context?, attrs: AttributeSet?) : View(contex
        noiseConfig = config
        with(turbulenceNoiseShader) {
            setGridCount(config.gridCount)
            setColor(ColorUtils.setAlphaComponent(config.color, config.opacity))
            setColor(config.color)
            setBackgroundColor(config.backgroundColor)
            setSize(config.width, config.height)
            setPixelDensity(config.pixelDensity)
            setInverseNoiseLuminosity(inverse = false)
            setLumaMatteFactors(config.lumaMatteBlendFactor, config.lumaMatteOverallBrightness)
        }
        paint.blendMode = config.blendMode
    }
+10 −11
Original line number Diff line number Diff line
@@ -74,7 +74,6 @@ import androidx.constraintlayout.widget.ConstraintSet;

import com.android.app.animation.Interpolators;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.graphics.ColorUtils;
import com.android.internal.jank.InteractionJankMonitor;
import com.android.internal.logging.InstanceId;
import com.android.internal.widget.CachingIconView;
@@ -1185,24 +1184,24 @@ public class MediaControlPanel {

    private TurbulenceNoiseAnimationConfig createTurbulenceNoiseAnimation() {
        return new TurbulenceNoiseAnimationConfig(
                TurbulenceNoiseAnimationConfig.DEFAULT_NOISE_GRID_COUNT,
                /* gridCount= */ 2.14f,
                TurbulenceNoiseAnimationConfig.DEFAULT_LUMINOSITY_MULTIPLIER,
                /* noiseMoveSpeedX= */ 0f,
                /* noiseMoveSpeedX= */ 0.42f,
                /* noiseMoveSpeedY= */ 0f,
                TurbulenceNoiseAnimationConfig.DEFAULT_NOISE_SPEED_Z,
                /* color= */ mColorSchemeTransition.getAccentPrimary().getCurrentColor(),
                // We want to add (BlendMode.PLUS) the turbulence noise on top of the album art.
                // Thus, set the background color with alpha 0.
                /* backgroundColor= */ ColorUtils.setAlphaComponent(Color.BLACK, 0),
                TurbulenceNoiseAnimationConfig.DEFAULT_OPACITY,
                /* backgroundColor= */ Color.BLACK,
                /* opacity= */ 51,
                /* width= */ mMediaViewHolder.getMultiRippleView().getWidth(),
                /* height= */ mMediaViewHolder.getMultiRippleView().getHeight(),
                TurbulenceNoiseAnimationConfig.DEFAULT_MAX_DURATION_IN_MILLIS,
                /* easeInDuration= */ 2500f,
                /* easeOutDuration= */ 2500f,
                /* easeInDuration= */ 1350f,
                /* easeOutDuration= */ 1350f,
                this.getContext().getResources().getDisplayMetrics().density,
                BlendMode.PLUS,
                /* onAnimationEnd= */ null
                BlendMode.SCREEN,
                /* onAnimationEnd= */ null,
                /* lumaMatteBlendFactor= */ 0.26f,
                /* lumaMatteOverallBrightness= */ 0.09f
        );
    }
    private void clearButton(final ImageButton button) {