1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.launcher3.taskbar
17 
18 import android.graphics.Bitmap
19 import android.graphics.Canvas
20 import android.graphics.PixelFormat
21 import android.graphics.RenderEffect
22 import android.graphics.RenderNode
23 import android.graphics.Shader
24 import android.graphics.drawable.BitmapDrawable
25 import android.graphics.drawable.DrawableWrapper
26 
27 /* BitmapDrawable that can blur the given bitmap. */
28 class BlurredBitmapDrawable(bitmap: Bitmap?, radiusX: Float, radiusY: Float) :
29     DrawableWrapper(BitmapDrawable(bitmap)) {
30     private val mBlurRenderNode: RenderNode = RenderNode("BlurredConstraintLayoutBlurNode")
31 
32     constructor(bitmap: Bitmap?, radius: Float) : this(bitmap, radius, radius)
33 
34     init {
35         mBlurRenderNode.setRenderEffect(
36             RenderEffect.createBlurEffect(radiusX, radiusY, Shader.TileMode.CLAMP)
37         )
38     }
39 
drawnull40     override fun draw(canvas: Canvas) {
41         if (!canvas.isHardwareAccelerated) {
42             super.draw(canvas)
43             return
44         }
45         mBlurRenderNode.setPosition(bounds)
46         if (!mBlurRenderNode.hasDisplayList()) {
47             // Record render node if its display list is not recorded or discarded
48             // (which happens when it's no longer drawn by anything).
49             val recordingCanvas = mBlurRenderNode.beginRecording()
50             super.draw(recordingCanvas)
51             mBlurRenderNode.endRecording()
52         }
53         canvas.drawRenderNode(mBlurRenderNode)
54     }
55 
getOpacitynull56     override fun getOpacity(): Int {
57         return PixelFormat.OPAQUE
58     }
59 }
60