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 
17 package com.android.systemui.surfaceeffects.glowboxeffect
18 
19 import android.graphics.RuntimeShader
20 import com.android.systemui.surfaceeffects.shaderutil.SdfShaderLibrary
21 
22 /** Soft box shader. */
23 class GlowBoxShader : RuntimeShader(GLOW_SHADER) {
24     // language=AGSL
25     private companion object {
26         private const val SHADER =
27             """
28             uniform half2 in_center;
29             uniform half2 in_size;
30             uniform half in_blur;
31             layout(color) uniform half4 in_color;
32 
33             float4 main(float2 fragcoord) {
34                 half glow = soften(sdBox(fragcoord - in_center, in_size), in_blur);
35                 return in_color * (1. - glow);
36             }
37         """
38 
39         private const val GLOW_SHADER =
40             SdfShaderLibrary.BOX_SDF + SdfShaderLibrary.SHADER_SDF_OPERATION_LIB + SHADER
41     }
42 
setCenternull43     fun setCenter(x: Float, y: Float) {
44         setFloatUniform("in_center", x, y)
45     }
46 
setSizenull47     fun setSize(width: Float, height: Float) {
48         setFloatUniform("in_size", width, height)
49     }
50 
setBlurnull51     fun setBlur(blurAmount: Float) {
52         setFloatUniform("in_blur", blurAmount)
53     }
54 
setColornull55     fun setColor(color: Int) {
56         setColorUniform("in_color", color)
57     }
58 }
59