1 /*
2  * Copyright 2020 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 #include "LinearEffect.h"
18 
19 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
20 
21 #include <SkString.h>
22 #include <log/log.h>
23 #include <shaders/shaders.h>
24 #include <utils/Trace.h>
25 
26 #include <math/mat4.h>
27 
28 namespace android {
29 namespace renderengine {
30 namespace skia {
31 
buildRuntimeEffect(const shaders::LinearEffect & linearEffect)32 sk_sp<SkRuntimeEffect> buildRuntimeEffect(const shaders::LinearEffect& linearEffect) {
33     ATRACE_CALL();
34     SkString shaderString = SkString(shaders::buildLinearEffectSkSL(linearEffect));
35 
36     auto [shader, error] = SkRuntimeEffect::MakeForShader(shaderString);
37     if (!shader) {
38         LOG_ALWAYS_FATAL("LinearColorFilter construction error: %s", error.c_str());
39     }
40     return shader;
41 }
42 
createLinearEffectShader(sk_sp<SkShader> shader,const shaders::LinearEffect & linearEffect,sk_sp<SkRuntimeEffect> runtimeEffect,const mat4 & colorTransform,float maxDisplayLuminance,float currentDisplayLuminanceNits,float maxLuminance,AHardwareBuffer * buffer,aidl::android::hardware::graphics::composer3::RenderIntent renderIntent)43 sk_sp<SkShader> createLinearEffectShader(
44         sk_sp<SkShader> shader, const shaders::LinearEffect& linearEffect,
45         sk_sp<SkRuntimeEffect> runtimeEffect, const mat4& colorTransform, float maxDisplayLuminance,
46         float currentDisplayLuminanceNits, float maxLuminance, AHardwareBuffer* buffer,
47         aidl::android::hardware::graphics::composer3::RenderIntent renderIntent) {
48     ATRACE_CALL();
49     SkRuntimeShaderBuilder effectBuilder(runtimeEffect);
50 
51     effectBuilder.child("child") = shader;
52 
53     const auto uniforms =
54             shaders::buildLinearEffectUniforms(linearEffect, colorTransform, maxDisplayLuminance,
55                                                currentDisplayLuminanceNits, maxLuminance, buffer,
56                                                renderIntent);
57 
58     for (const auto& uniform : uniforms) {
59         effectBuilder.uniform(uniform.name.c_str()).set(uniform.value.data(), uniform.value.size());
60     }
61 
62     return effectBuilder.makeShader();
63 }
64 
65 } // namespace skia
66 } // namespace renderengine
67 } // namespace android
68