1 /*
2  * Copyright (c) 2021-2023 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 
16 #include "render/rs_filter.h"
17 
18 #include "platform/common/rs_log.h"
19 #include "render/rs_blur_filter.h"
20 #include "render/rs_material_filter.h"
21 #include "render/rs_light_up_effect_filter.h"
22 
23 namespace OHOS {
24 namespace Rosen {
RSFilter()25 RSFilter::RSFilter()
26     : type_(FilterType::NONE)
27 {}
28 
~RSFilter()29 RSFilter::~RSFilter() {}
30 
GetDescription()31 std::string RSFilter::GetDescription()
32 {
33     return "RSFilter " + std::to_string(type_);
34 }
35 
GetDetailedDescription()36 std::string RSFilter::GetDetailedDescription()
37 {
38     return "RSFilter " + std::to_string(type_);
39 }
40 
CreateBlurFilter(float blurRadiusX,float blurRadiusY)41 std::shared_ptr<RSFilter> RSFilter::CreateBlurFilter(float blurRadiusX, float blurRadiusY)
42 {
43     return std::make_shared<RSBlurFilter>(blurRadiusX, blurRadiusY);
44 }
45 
CreateMaterialFilter(int style,float dipScale,BLUR_COLOR_MODE mode,float ratio)46 std::shared_ptr<RSFilter> RSFilter::CreateMaterialFilter(int style, float dipScale, BLUR_COLOR_MODE mode, float ratio)
47 {
48     return std::make_shared<RSMaterialFilter>(style, dipScale, mode, ratio);
49 }
50 
CreateMaterialFilter(float radius,float saturation,float brightness,uint32_t colorValue,BLUR_COLOR_MODE mode)51 std::shared_ptr<RSFilter> RSFilter::CreateMaterialFilter(float radius, float saturation,
52     float brightness, uint32_t colorValue, BLUR_COLOR_MODE mode)
53 {
54     MaterialParam materialParam = {radius, saturation, brightness, Color::FromArgbInt(colorValue)};
55     return std::make_shared<RSMaterialFilter>(materialParam, mode);
56 }
57 
CreateLightUpEffectFilter(float lightUpDegree)58 std::shared_ptr<RSFilter> RSFilter::CreateLightUpEffectFilter(float lightUpDegree)
59 {
60     return std::make_shared<RSLightUpEffectFilter>(lightUpDegree);
61 }
62 
RadiusVp2Sigma(float radiusVp,float dipScale)63 float RSFilter::RadiusVp2Sigma(float radiusVp, float dipScale)
64 {
65     float radiusPx = radiusVp * dipScale;
66     return radiusPx > 0.0f ? BLUR_SIGMA_SCALE * radiusPx + 0.5f : 0.0f;
67 }
68 
operator +(const std::shared_ptr<RSFilter> & lhs,const std::shared_ptr<RSFilter> & rhs)69 std::shared_ptr<RSFilter> operator+(const std::shared_ptr<RSFilter>& lhs, const std::shared_ptr<RSFilter>& rhs)
70 {
71     if (lhs == nullptr) {
72         return rhs;
73     }
74     if (rhs == nullptr) {
75         return lhs;
76     }
77     return lhs->Add(rhs);
78 }
79 
operator -(const std::shared_ptr<RSFilter> & lhs,const std::shared_ptr<RSFilter> & rhs)80 std::shared_ptr<RSFilter> operator-(const std::shared_ptr<RSFilter>& lhs, const std::shared_ptr<RSFilter>& rhs)
81 {
82     if (rhs == nullptr) {
83         return lhs;
84     }
85     if (lhs == nullptr) {
86         return rhs->Negate();
87     }
88     return lhs->Sub(rhs);
89 }
90 
operator *(const std::shared_ptr<RSFilter> & lhs,float rhs)91 std::shared_ptr<RSFilter> operator*(const std::shared_ptr<RSFilter>& lhs, float rhs)
92 {
93     if (lhs == nullptr) {
94         return nullptr;
95     }
96     return lhs->Multiply(rhs);
97 }
98 } // namespace Rosen
99 } // namespace OHOS
100