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

Commit 9aec2cd7 authored by Sunny Goyal's avatar Sunny Goyal Committed by Android (Google) Code Review
Browse files

Merge "Making the shader part of delegate instead of the drawable" into main

parents bc5e1cb8 143774a9
Loading
Loading
Loading
Loading
+5 −7
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import android.graphics.drawable.LayerDrawable
import android.os.SystemClock
import android.util.Log
import com.android.launcher3.icons.BitmapInfo.Extender
import com.android.launcher3.icons.FastBitmapDrawableDelegate.Companion.drawShaderInBounds
import com.android.launcher3.icons.FastBitmapDrawableDelegate.DelegateFactory
import com.android.launcher3.icons.GraphicsUtils.getColorMultipliedFilter
import com.android.launcher3.icons.GraphicsUtils.resizeToContentSize
@@ -66,7 +67,7 @@ private constructor(base: AdaptiveIconDrawable, private val animationInfo: Clock
            delegateFactory =
                animationInfo.copy(
                    themeFgColor = NO_COLOR,
                    shaderProvider = { BitmapShader(flattenBG, CLAMP, CLAMP) },
                    shader = BitmapShader(flattenBG, CLAMP, CLAMP),
                )
        )
    }
@@ -91,7 +92,7 @@ private constructor(base: AdaptiveIconDrawable, private val animationInfo: Clock
        val defaultSecond: Int,
        val baseDrawableState: ConstantState,
        val themeFgColor: Int = NO_COLOR,
        val shaderProvider: (IconShape) -> Shader? = { null },
        val shader: Shader? = null,
    ) : DelegateFactory {

        fun applyTime(time: Calendar, foregroundDrawable: LayerDrawable): Boolean {
@@ -150,21 +151,18 @@ private constructor(base: AdaptiveIconDrawable, private val animationInfo: Clock
                    colorFilter = getColorMultipliedFilter(themedFgColor, paint.colorFilter)
                }

        override fun createPaintShader(bitmapInfo: BitmapInfo, shape: IconShape): Shader? =
            animInfo.shaderProvider.invoke(shape)

        override fun setAlpha(alpha: Int) {
            foreground.alpha = alpha
        }

        override fun drawContent(
            info: BitmapInfo,
            host: FastBitmapDrawable,
            iconShape: IconShape,
            canvas: Canvas,
            bounds: Rect,
            paint: Paint,
        ) {
            host.drawShaderInBounds(canvas, bounds)
            canvas.drawShaderInBounds(bounds, iconShape, paint, animInfo.shader)

            // prepare and draw the foreground
            animInfo.applyTime(time, foreground)
+1 −18
Original line number Diff line number Diff line
@@ -28,7 +28,6 @@ import android.graphics.Paint.ANTI_ALIAS_FLAG
import android.graphics.Paint.FILTER_BITMAP_FLAG
import android.graphics.PixelFormat
import android.graphics.Rect
import android.graphics.Shader
import android.graphics.drawable.Drawable
import android.graphics.drawable.Drawable.Callback
import android.util.FloatProperty
@@ -41,7 +40,6 @@ import com.android.launcher3.icons.BitmapInfo.Companion.LOW_RES_INFO
import com.android.launcher3.icons.BitmapInfo.DrawableCreationFlags
import com.android.launcher3.icons.FastBitmapDrawableDelegate.DelegateFactory
import com.android.launcher3.icons.FastBitmapDrawableDelegate.SimpleDelegateFactory
import com.android.launcher3.icons.GraphicsUtils.resizeToContentSize
import kotlin.math.min

class FastBitmapDrawable
@@ -61,7 +59,6 @@ constructor(
    @JvmField protected val paint: Paint = Paint(FILTER_BITMAP_FLAG or ANTI_ALIAS_FLAG)

    val delegate = delegateFactory.newDelegate(bitmapInfo, iconShape, paint, this)
    private val shader: Shader? = delegate.createPaintShader(bitmapInfo, iconShape)

    @JvmField @VisibleForTesting var isPressed: Boolean = false
    @JvmField @VisibleForTesting var isHovered: Boolean = false
@@ -123,24 +120,10 @@ constructor(
    }

    private fun drawInternal(canvas: Canvas, bounds: Rect) {
        delegate.drawContent(bitmapInfo, this, canvas, bounds, paint)
        delegate.drawContent(bitmapInfo, iconShape, canvas, bounds, paint)
        badge?.draw(canvas)
    }

    /**
     * Draws the shader created using [FastBitmapDrawableDelegate.createPaintShader] in the provided
     * bounds
     */
    fun drawShaderInBounds(canvas: Canvas, bounds: Rect) {
        canvas.drawBitmap(iconShape.shadowLayer, null, bounds, paint)

        canvas.resizeToContentSize(bounds, iconShape.pathSize.toFloat()) {
            paint.shader = shader
            iconShape.shapeRenderer.render(canvas, paint)
            paint.shader = null
        }
    }

    /** Returns the primary icon color, slightly tinted white */
    fun getIconColor(): Int = delegate.getIconColor(bitmapInfo)

+54 −18
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.graphics.Shader
import android.graphics.Shader.TileMode.CLAMP
import androidx.core.graphics.ColorUtils
import com.android.launcher3.icons.BitmapInfo.Companion.FLAG_FULL_BLEED
import com.android.launcher3.icons.GraphicsUtils.resizeToContentSize

/** A delegate for changing the rendering of [FastBitmapDrawable], to support multi-inheritance */
interface FastBitmapDrawableDelegate {
@@ -36,17 +37,11 @@ interface FastBitmapDrawableDelegate {
    /** [android.graphics.drawable.Drawable.draw] */
    fun drawContent(
        info: BitmapInfo,
        host: FastBitmapDrawable,
        iconShape: IconShape,
        canvas: Canvas,
        bounds: Rect,
        paint: Paint,
    ) {
        if ((info.flags and FLAG_FULL_BLEED) != 0) {
            host.drawShaderInBounds(canvas, bounds)
        } else {
            canvas.drawBitmap(info.icon, null, bounds, paint)
        }
    }
    )

    /** [FastBitmapDrawable.getIconColor] */
    fun getIconColor(info: BitmapInfo): Int =
@@ -70,15 +65,6 @@ interface FastBitmapDrawableDelegate {
    /** [android.graphics.drawable.Drawable.onLevelChange] */
    fun onLevelChange(level: Int): Boolean = false

    /** Creates a default shader to be used for drawing the drawable */
    fun createPaintShader(bitmapInfo: BitmapInfo, shape: IconShape): Shader? {
        return if ((bitmapInfo.flags and FLAG_FULL_BLEED) != 0) {
            BitmapShader(bitmapInfo.icon, CLAMP, CLAMP)
        } else {
            null
        }
    }

    /**
     * Interface for creating new delegates. This should not store any state information and can
     * safely be stored in a [android.graphics.drawable.Drawable.ConstantState]
@@ -93,12 +79,62 @@ interface FastBitmapDrawableDelegate {
        ): FastBitmapDrawableDelegate
    }

    class FullBleedDrawableDelegate(bitmapInfo: BitmapInfo) : FastBitmapDrawableDelegate {
        private val shader = BitmapShader(bitmapInfo.icon, CLAMP, CLAMP)

        override fun drawContent(
            info: BitmapInfo,
            iconShape: IconShape,
            canvas: Canvas,
            bounds: Rect,
            paint: Paint,
        ) {
            canvas.drawShaderInBounds(bounds, iconShape, paint, shader)
        }
    }

    object SimpleDrawableDelegate : FastBitmapDrawableDelegate {

        override fun drawContent(
            info: BitmapInfo,
            iconShape: IconShape,
            canvas: Canvas,
            bounds: Rect,
            paint: Paint,
        ) {
            canvas.drawBitmap(info.icon, null, bounds, paint)
        }
    }

    object SimpleDelegateFactory : DelegateFactory {
        override fun newDelegate(
            bitmapInfo: BitmapInfo,
            iconShape: IconShape,
            paint: Paint,
            host: FastBitmapDrawable,
        ) = object : FastBitmapDrawableDelegate {}
        ) =
            if ((bitmapInfo.flags and FLAG_FULL_BLEED) != 0) FullBleedDrawableDelegate(bitmapInfo)
            else SimpleDrawableDelegate
    }

    companion object {

        /**
         * Draws the shader created using [FastBitmapDrawableDelegate.createPaintShader] in the
         * provided bounds
         */
        fun Canvas.drawShaderInBounds(
            bounds: Rect,
            iconShape: IconShape,
            paint: Paint,
            shader: Shader?,
        ) {
            drawBitmap(iconShape.shadowLayer, null, bounds, paint)
            resizeToContentSize(bounds, iconShape.pathSize.toFloat()) {
                paint.shader = shader
                iconShape.shapeRenderer.render(this, paint)
                paint.shader = null
            }
        }
    }
}
+4 −8
Original line number Diff line number Diff line
@@ -32,12 +32,8 @@ import com.android.launcher3.icons.GraphicsUtils.getAttrColor
import com.android.launcher3.icons.GraphicsUtils.resizeToContentSize

/** Subclass which draws a placeholder icon when the actual icon is not yet loaded */
class PlaceHolderDrawableDelegate(
    info: BitmapInfo,
    private val iconShape: IconShape,
    paint: Paint,
    loadingColor: Int,
) : FastBitmapDrawableDelegate {
class PlaceHolderDrawableDelegate(info: BitmapInfo, paint: Paint, loadingColor: Int) :
    FastBitmapDrawableDelegate {

    private val fillColor = ColorUtils.compositeColors(loadingColor, info.color)

@@ -47,7 +43,7 @@ class PlaceHolderDrawableDelegate(

    override fun drawContent(
        info: BitmapInfo,
        host: FastBitmapDrawable,
        iconShape: IconShape,
        canvas: Canvas,
        bounds: Rect,
        paint: Paint,
@@ -92,7 +88,7 @@ class PlaceHolderDrawableDelegate(
            paint: Paint,
            host: FastBitmapDrawable,
        ): FastBitmapDrawableDelegate {
            return PlaceHolderDrawableDelegate(bitmapInfo, iconShape, paint, loadingColor)
            return PlaceHolderDrawableDelegate(bitmapInfo, paint, loadingColor)
        }
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -83,7 +83,7 @@ class ClockThemedBitmap(
            shape,
            animInfo.copy(
                themeFgColor = colors[1],
                shaderProvider = { LinearGradient(0f, 0f, 1f, 1f, colors[0], colors[0], CLAMP) },
                shader = LinearGradient(0f, 0f, 1f, 1f, colors[0], colors[0], CLAMP),
            ),
        )
    }
Loading