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 "render/rs_aibar_shader_filter.h"
16 
17 #include <unordered_map>
18 
19 #include "common/rs_common_def.h"
20 #include "platform/common/rs_system_properties.h"
21 #include "pipeline/rs_paint_filter_canvas.h"
22 #include "src/core/SkOpts.h"
23 
24 
25 namespace OHOS {
26 namespace Rosen {
RSAIBarShaderFilter()27 RSAIBarShaderFilter::RSAIBarShaderFilter()
28 {
29     type_ = ShaderFilterType::AIBAR;
30 }
31 RSAIBarShaderFilter::~RSAIBarShaderFilter() = default;
32 
GetAiInvertCoef()33 std::vector<float> RSAIBarShaderFilter::GetAiInvertCoef()
34 {
35     const auto& aiInvertCoef = RSSystemProperties::GetAiInvertCoef();
36     if (!IsAiInvertCoefValid(aiInvertCoef)) {
37         static std::vector<float> aiInvertCoefDefault {0.0, 1.0, 0.55, 0.4, 1.6, 45.0};
38         return aiInvertCoefDefault;
39     }
40     return aiInvertCoef;
41 }
42 
GenerateGEVisualEffect(std::shared_ptr<Drawing::GEVisualEffectContainer> visualEffectContainer)43 void RSAIBarShaderFilter::GenerateGEVisualEffect(
44     std::shared_ptr<Drawing::GEVisualEffectContainer> visualEffectContainer)
45 {
46     auto aiBarPara = GetAiInvertCoef();
47     auto aiBarFilter = std::make_shared<Drawing::GEVisualEffect>("AIBAR", Drawing::DrawingPaintType::BRUSH);
48     aiBarFilter->SetParam("AIBAR_LOW", aiBarPara[0]); // aiBarPara[0] is low
49     aiBarFilter->SetParam("AIBAR_HIGH", aiBarPara[1]); // aiBarPara[1] is high
50     aiBarFilter->SetParam("AIBAR_THRESHOLD", aiBarPara[2]); // aiBarPara[2] is threshold
51     aiBarFilter->SetParam("AIBAR_OPACITY", aiBarPara[3]); // aiBarPara[3] is opacity
52     aiBarFilter->SetParam("AIBAR_SATURATION", aiBarPara[4]); // aiBarPara[4] is saturation
53 
54     visualEffectContainer->AddToChainedFilter(aiBarFilter);
55 }
56 
IsAiInvertCoefValid(const std::vector<float> & aiInvertCoef)57 bool RSAIBarShaderFilter::IsAiInvertCoefValid(const std::vector<float>& aiInvertCoef)
58 {
59     return ROSEN_LNE(aiInvertCoef[0], aiInvertCoef[1]) &&
60         ROSEN_GE(aiInvertCoef[0], 0.0) && ROSEN_LE(aiInvertCoef[0], 1.0) && // aiInvertCoef[0] is low
61         ROSEN_GE(aiInvertCoef[1], 0.0) && ROSEN_LE(aiInvertCoef[1], 1.0) && // aiInvertCoef[1] is high
62         ROSEN_GE(aiInvertCoef[2], 0.0) && ROSEN_LE(aiInvertCoef[2], 1.0) && // aiInvertCoef[2] is threshold
63         ROSEN_GE(aiInvertCoef[3], 0.0) && ROSEN_LE(aiInvertCoef[3], 1.0) && // aiInvertCoef[3] is opacity
64         ROSEN_GE(aiInvertCoef[4], 0.0) && ROSEN_LE(aiInvertCoef[4], 2.0) && // aiInvertCoef[4] is saturation <= 2.0
65         ROSEN_GNE(aiInvertCoef[5], 0.0); // aiInvertCoef[5] is filter_radius
66 }
67 } // namespace Rosen
68 } // namespace OHOS