|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 DuckDuckGo |
| 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 | + |
| 17 | +package com.duckduckgo.duckchat.impl.inputscreen.ui.view |
| 18 | + |
| 19 | +import android.content.Context |
| 20 | +import android.graphics.Canvas |
| 21 | +import android.graphics.RenderEffect |
| 22 | +import android.graphics.RenderNode |
| 23 | +import android.graphics.RuntimeShader |
| 24 | +import android.util.AttributeSet |
| 25 | +import android.view.View |
| 26 | +import androidx.annotation.RequiresApi |
| 27 | +import com.duckduckgo.common.ui.view.toPx |
| 28 | + |
| 29 | +@RequiresApi(33) |
| 30 | +class BottomBlurView @JvmOverloads constructor( |
| 31 | + context: Context, |
| 32 | + attrs: AttributeSet? = null, |
| 33 | + defStyleAttr: Int = 0, |
| 34 | +) : View(context, attrs, defStyleAttr) { |
| 35 | + |
| 36 | + private val renderNode = RenderNode(null) |
| 37 | + private var targetView: View? = null |
| 38 | + private var maxBlurRadiusPx = MAX_BLUR_RADIUS_DP.toPx().toFloat() |
| 39 | + private val selfLocationOnScreen = IntArray(2) |
| 40 | + private val targetLocationOnScreen = IntArray(2) |
| 41 | + |
| 42 | + private val blurShader = RuntimeShader( |
| 43 | + """ |
| 44 | + uniform shader inputShader; |
| 45 | + uniform vec2 size; |
| 46 | + uniform float maxBlurRadius; |
| 47 | + |
| 48 | + const float TAU = 6.28318530718; |
| 49 | + const int DIRECTIONS = 16; |
| 50 | + const int QUALITY = 3; |
| 51 | + const float SAMPLES = float(DIRECTIONS * QUALITY + 1); |
| 52 | + |
| 53 | + vec2 mirrorCoord(vec2 coord) { |
| 54 | + if (coord.x < 0.0) coord.x = -coord.x; |
| 55 | + else if (coord.x > size.x) coord.x = 2.0 * size.x - coord.x; |
| 56 | + |
| 57 | + if (coord.y < 0.0) coord.y = -coord.y; |
| 58 | + else if (coord.y > size.y) coord.y = 2.0 * size.y - coord.y; |
| 59 | + |
| 60 | + return coord; |
| 61 | + } |
| 62 | + |
| 63 | + vec4 main(vec2 fragCoord) { |
| 64 | + vec2 uv = fragCoord.xy / size.xy; |
| 65 | + float radius = maxBlurRadius * uv.y; |
| 66 | + |
| 67 | + vec4 pixel = inputShader.eval(mirrorCoord(fragCoord)); |
| 68 | + |
| 69 | + for (int i = 0; i < DIRECTIONS; i++) { |
| 70 | + float angle = (TAU * float(i)) / float(DIRECTIONS); |
| 71 | + vec2 dir = vec2(cos(angle), sin(angle)); |
| 72 | + |
| 73 | + for (int j = 1; j <= QUALITY; j++) { |
| 74 | + float frac = float(j) / float(QUALITY); |
| 75 | + vec2 offset = dir * radius * frac; |
| 76 | + vec2 sampleCoord = mirrorCoord(fragCoord + offset); |
| 77 | + pixel += inputShader.eval(sampleCoord); |
| 78 | + } |
| 79 | + } |
| 80 | + return pixel / SAMPLES; |
| 81 | + } |
| 82 | + """.trimIndent(), |
| 83 | + ) |
| 84 | + |
| 85 | + private val shaderEffect: RenderEffect |
| 86 | + get() = RenderEffect.createRuntimeShaderEffect(blurShader, "inputShader") |
| 87 | + |
| 88 | + init { |
| 89 | + setLayerType(LAYER_TYPE_HARDWARE, null) |
| 90 | + background = null |
| 91 | + } |
| 92 | + |
| 93 | + fun setTargetView(view: View) { |
| 94 | + targetView = view |
| 95 | + invalidate() |
| 96 | + } |
| 97 | + |
| 98 | + override fun onDraw(canvas: Canvas) { |
| 99 | + val viewToBlur = targetView ?: return |
| 100 | + |
| 101 | + val w = width |
| 102 | + val h = height |
| 103 | + if (w == 0 || h == 0) return |
| 104 | + |
| 105 | + getLocationOnScreen(selfLocationOnScreen) |
| 106 | + viewToBlur.getLocationOnScreen(targetLocationOnScreen) |
| 107 | + val dx = (selfLocationOnScreen[0] - targetLocationOnScreen[0]).toFloat() |
| 108 | + val dy = (selfLocationOnScreen[1] - targetLocationOnScreen[1]).toFloat() |
| 109 | + |
| 110 | + renderNode.setPosition(0, 0, w, h) |
| 111 | + renderNode.beginRecording().apply { |
| 112 | + translate(-dx, -dy) |
| 113 | + viewToBlur.draw(this) |
| 114 | + renderNode.endRecording() |
| 115 | + } |
| 116 | + |
| 117 | + blurShader.setFloatUniform("size", w.toFloat(), h.toFloat()) |
| 118 | + blurShader.setFloatUniform("maxBlurRadius", maxBlurRadiusPx) |
| 119 | + |
| 120 | + renderNode.setRenderEffect(shaderEffect) |
| 121 | + canvas.drawRenderNode(renderNode) |
| 122 | + } |
| 123 | + |
| 124 | + companion object { |
| 125 | + const val MAX_BLUR_RADIUS_DP = 6 |
| 126 | + } |
| 127 | +} |
0 commit comments