• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.google.android.wallpaper.weathereffects.graphics.sun
18 
19 import android.graphics.Bitmap
20 import android.graphics.BitmapShader
21 import android.graphics.Canvas
22 import android.graphics.Paint
23 import android.graphics.RuntimeShader
24 import android.graphics.Shader
25 import android.util.SizeF
26 import com.google.android.wallpaper.weathereffects.graphics.WeatherEffect
27 import com.google.android.wallpaper.weathereffects.graphics.WeatherEffectBase
28 import com.google.android.wallpaper.weathereffects.graphics.utils.TimeUtils
29 
30 /** Defines and generates the sunny weather animation. */
31 class SunEffect(
32     /** The config of the sunny effect. */
33     private val sunConfig: SunEffectConfig,
34     foreground: Bitmap,
35     background: Bitmap,
36     intensity: Float = WeatherEffect.DEFAULT_INTENSITY,
37     /** The initial size of the surface where the effect will be shown. */
38     surfaceSize: SizeF,
39 ) : WeatherEffectBase(foreground = foreground, background = background, surfaceSize = surfaceSize) {
40 
<lambda>null41     private val sunnyPaint = Paint().also { it.shader = sunConfig.colorGradingShader }
42 
43     init {
44         updateTextureUniforms()
45         adjustCropping(surfaceSize)
46         prepareColorGrading()
47         setIntensity(intensity)
48     }
49 
50     override val shader: RuntimeShader
51         get() = sunConfig.shader
52 
53     override val colorGradingShader: RuntimeShader
54         get() = sunConfig.colorGradingShader
55 
56     override val lut: Bitmap?
57         get() = sunConfig.lut
58 
59     override val colorGradingIntensity: Float
60         get() = sunConfig.colorGradingIntensity
61 
updatenull62     override fun update(deltaMillis: Long, frameTimeNanos: Long) {
63         elapsedTime += TimeUtils.millisToSeconds(deltaMillis)
64         sunConfig.shader.setFloatUniform("time", elapsedTime)
65         sunConfig.colorGradingShader.setInputShader("texture", sunConfig.shader)
66     }
67 
drawnull68     override fun draw(canvas: Canvas) {
69         canvas.drawPaint(sunnyPaint)
70     }
71 
prepareColorGradingnull72     private fun prepareColorGrading() {
73         sunConfig.colorGradingShader.setInputShader("texture", sunConfig.shader)
74         sunConfig.lut?.let {
75             sunConfig.colorGradingShader.setInputShader(
76                 "lut",
77                 BitmapShader(it, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR),
78             )
79         }
80         sunConfig.colorGradingShader.setFloatUniform("intensity", sunConfig.colorGradingIntensity)
81     }
82 }
83