1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "ge_aibar_shader_filter.h"
16 
17 #include <unordered_map>
18 
19 #include "ge_log.h"
20 #include "include/core/SkTileMode.h"
21 #include "include/effects/SkImageFilters.h"
22 #include "src/core/SkOpts.h"
23 
24 namespace OHOS {
25 namespace Rosen {
26 
GEAIBarShaderFilter(const Drawing::GEAIBarShaderFilterParams & params)27 GEAIBarShaderFilter::GEAIBarShaderFilter(const Drawing::GEAIBarShaderFilterParams& params)
28     : aiBarLow_(params.aiBarLow), aiBarHigh_(params.aiBarHigh), aiBarThreshold_(params.aiBarThreshold),
29     aiBarOpacity_(params.aiBarOpacity), aiBarSaturation_(params.aiBarSaturation)
30 {}
31 
GetDescription() const32 const std::string GEAIBarShaderFilter::GetDescription() const
33 {
34     return "GEAIBarShaderFilter";
35 }
36 
ProcessImage(Drawing::Canvas & canvas,const std::shared_ptr<Drawing::Image> image,const Drawing::Rect & src,const Drawing::Rect & dst)37 std::shared_ptr<Drawing::Image> GEAIBarShaderFilter::ProcessImage(Drawing::Canvas& canvas,
38     const std::shared_ptr<Drawing::Image> image, const Drawing::Rect& src, const Drawing::Rect& dst)
39 {
40     if (image == nullptr) {
41         LOGE("GEAIBarShaderFilter::ProcessImage image is null");
42         return image;
43     }
44 
45     Drawing::Matrix matrix;
46     auto imageShader = Drawing::ShaderEffect::CreateImageShader(*image, Drawing::TileMode::CLAMP,
47         Drawing::TileMode::CLAMP, Drawing::SamplingOptions(Drawing::FilterMode::LINEAR), matrix);
48     float imageWidth = image->GetWidth();
49     float imageHeight = image->GetHeight();
50     auto builder = MakeBinarizationShader(imageWidth, imageHeight, imageShader);
51     auto invertedImage = builder->MakeImage(canvas.GetGPUContext().get(), nullptr, image->GetImageInfo(), false);
52     if (invertedImage == nullptr) {
53         LOGE("GEAIBarShaderFilter::ProcessImage invertedImage is null");
54         return image;
55     }
56 
57     return invertedImage;
58 }
59 
MakeBinarizationShader(float imageWidth,float imageHeight,std::shared_ptr<Drawing::ShaderEffect> imageShader)60 std::shared_ptr<Drawing::RuntimeShaderBuilder> GEAIBarShaderFilter::MakeBinarizationShader(
61     float imageWidth, float imageHeight, std::shared_ptr<Drawing::ShaderEffect> imageShader)
62 {
63     static std::shared_ptr<Drawing::RuntimeEffect> binarizationShaderEffect_;
64 
65     // coefficient of saturation borrowed from
66     // the saturate filter in RSProperties::GenerateColorFilter()
67     static constexpr char prog[] = R"(
68         uniform half low;
69         uniform half high;
70         uniform half threshold;
71         uniform half opacity;
72         uniform half saturation;
73         uniform shader imageShader;
74 
75         const vec3 toLuminance = vec3(0.3086, 0.6094, 0.0820);
76 
77         half4 main(float2 coord) {
78             half3 c = imageShader.eval(coord).rgb;
79             float gray = 0.299 * c.r + 0.587 * c.g + 0.114 * c.b;
80             float bin = mix(high, low, step(threshold, gray));
81             float luminance = dot(c, toLuminance);
82             half3 satAdjust = mix(vec3(luminance), c, saturation);
83             half3 res = satAdjust - (opacity + 1.0) * gray + bin;
84             return half4(mix(c, res, 0.42857), 1.0);
85         }
86     )";
87 
88     if (binarizationShaderEffect_ == nullptr) {
89         binarizationShaderEffect_ = Drawing::RuntimeEffect::CreateForShader(prog);
90         if (binarizationShaderEffect_ == nullptr) {
91             LOGE("MakeBinarizationShader::RuntimeShader effect error\n");
92             return nullptr;
93         }
94     }
95     std::shared_ptr<Drawing::RuntimeShaderBuilder> builder =
96         std::make_shared<Drawing::RuntimeShaderBuilder>(binarizationShaderEffect_);
97     builder->SetChild("imageShader", imageShader);
98     builder->SetUniform("low", aiBarLow_);               // aiInvertCoef[0] is low
99     builder->SetUniform("high", aiBarHigh_);             // aiInvertCoef[1] is high
100     builder->SetUniform("threshold", aiBarThreshold_);   // aiInvertCoef[2] is threshold
101     builder->SetUniform("opacity", aiBarOpacity_);       // aiInvertCoef[3] is opacity
102     builder->SetUniform("saturation", aiBarSaturation_); // aiInvertCoef[4] is saturation
103 
104     return builder;
105 }
106 
107 } // namespace Rosen
108 } // namespace OHOS
109