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 #ifndef ROSEN_ENGINE_CORE_RENDER_RS_FILTER_H 17 #define ROSEN_ENGINE_CORE_RENDER_RS_FILTER_H 18 19 #include <memory> 20 #include <stdint.h> 21 22 #include "common/rs_color.h" 23 #include "common/rs_macros.h" 24 #include "image/gpu_context.h" 25 #include "src/core/SkOpts.h" 26 27 namespace OHOS { 28 namespace Rosen { 29 constexpr float BLUR_SIGMA_SCALE = 0.57735f; 30 enum BLUR_COLOR_MODE : int { 31 PRE_DEFINED = 0, // use the pre-defined mask color 32 AVERAGE = 1, // use the average color of the blurred area as mask color 33 FASTAVERAGE = 2, 34 DEFAULT = PRE_DEFINED 35 }; 36 37 class RSB_EXPORT RSFilter : public std::enable_shared_from_this<RSFilter> { 38 public: 39 virtual ~RSFilter(); 40 RSFilter(const RSFilter&) = delete; 41 RSFilter(const RSFilter&&) = delete; 42 RSFilter& operator=(const RSFilter&) = delete; 43 RSFilter& operator=(const RSFilter&&) = delete; 44 virtual std::string GetDescription(); 45 virtual std::string GetDetailedDescription(); 46 static std::shared_ptr<RSFilter> CreateBlurFilter(float blurRadiusX, float blurRadiusY, 47 bool disableSystemAdaptation = true); 48 static std::shared_ptr<RSFilter> CreateMaterialFilter( 49 int style, float dipScale, BLUR_COLOR_MODE mode = DEFAULT, float ratio = 1.0, 50 bool disableSystemAdaptation = true); 51 static std::shared_ptr<RSFilter> CreateMaterialFilter( 52 float radius, float saturation, float brightness, uint32_t colorValue, 53 BLUR_COLOR_MODE mode = BLUR_COLOR_MODE::DEFAULT, 54 bool disableSystemAdaptation = true); 55 static std::shared_ptr<RSFilter> CreateLightUpEffectFilter(float lightUpDegree); 56 static float RadiusVp2Sigma(float radiusVp, float dipScale); 57 58 enum FilterType { 59 NONE = 0, 60 BLUR, 61 MATERIAL, 62 LIGHT_UP_EFFECT, 63 AIBAR, 64 LINEAR_GRADIENT_BLUR, 65 FOREGROUND_EFFECT, 66 MOTION_BLUR, 67 SPHERIZE_EFFECT, 68 COLORFUL_SHADOW, 69 ATTRACTION_EFFECT, 70 WATER_RIPPLE, 71 COMPOUND_EFFECT, 72 MAGNIFIER, 73 FLY_OUT, 74 DISTORT, 75 }; GetFilterType()76 FilterType GetFilterType() const 77 { 78 return type_; 79 } 80 SetFilterType(FilterType type)81 void SetFilterType(FilterType type) 82 { 83 type_ = type; 84 hash_ = SkOpts::hash(&type_, sizeof(type_), hash_); 85 } 86 IsValid()87 virtual bool IsValid() const 88 { 89 return type_ != FilterType::NONE; 90 } 91 DecreasePrecision(float value)92 float DecreasePrecision(float value) 93 { 94 // preserve two digital precision when calculating hash, this can reuse filterCache as much as possible. 95 return 0.01 * round(value * 100); 96 } 97 Hash()98 virtual uint32_t Hash() const 99 { 100 return hash_; 101 } 102 103 virtual bool IsNearEqual( 104 const std::shared_ptr<RSFilter>& other, float threshold = std::numeric_limits<float>::epsilon()) const 105 { 106 return true; 107 } 108 109 virtual bool IsNearZero(float threshold = std::numeric_limits<float>::epsilon()) const 110 { 111 return true; 112 } 113 IsEqual(const std::shared_ptr<RSFilter> & other)114 virtual bool IsEqual(const std::shared_ptr<RSFilter>& other) const 115 { 116 return true; 117 } 118 IsEqualZero()119 virtual bool IsEqualZero() const 120 { 121 return true; 122 } 123 NeedSnapshotOutset()124 bool NeedSnapshotOutset() const 125 { 126 return needSnapshotOutset_; 127 } 128 SetSnapshotOutset(bool needSnapshotOutset)129 void SetSnapshotOutset(bool needSnapshotOutset) 130 { 131 needSnapshotOutset_ = needSnapshotOutset; 132 } 133 134 protected: 135 FilterType type_; 136 uint32_t hash_ = 0; 137 bool needSnapshotOutset_ = true; 138 RSFilter(); Add(const std::shared_ptr<RSFilter> & rhs)139 virtual std::shared_ptr<RSFilter> Add(const std::shared_ptr<RSFilter>& rhs) { return nullptr; } Sub(const std::shared_ptr<RSFilter> & rhs)140 virtual std::shared_ptr<RSFilter> Sub(const std::shared_ptr<RSFilter>& rhs) { return nullptr; } Multiply(float rhs)141 virtual std::shared_ptr<RSFilter> Multiply(float rhs) { return nullptr; } Negate()142 virtual std::shared_ptr<RSFilter> Negate() { return nullptr; } 143 friend RSB_EXPORT std::shared_ptr<RSFilter> operator+(const std::shared_ptr<RSFilter>& lhs, 144 const std::shared_ptr<RSFilter>& rhs); 145 friend RSB_EXPORT std::shared_ptr<RSFilter> operator-(const std::shared_ptr<RSFilter>& lhs, 146 const std::shared_ptr<RSFilter>& rhs); 147 friend RSB_EXPORT std::shared_ptr<RSFilter> operator*(const std::shared_ptr<RSFilter>& lhs, float rhs); 148 }; 149 } // namespace Rosen 150 } // namespace OHOS 151 152 #endif // ROSEN_ENGINE_CORE_RENDER_RS_FILTER_H 153