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 static std::shared_ptr<RSFilter> CreateMaterialFilter( 48 int style, float dipScale, BLUR_COLOR_MODE mode = DEFAULT, float ratio = 1.0); 49 static std::shared_ptr<RSFilter> CreateMaterialFilter( 50 float radius, float saturation, float brightness, uint32_t colorValue, 51 BLUR_COLOR_MODE mode = BLUR_COLOR_MODE::DEFAULT); 52 static std::shared_ptr<RSFilter> CreateLightUpEffectFilter(float lightUpDegree); 53 static float RadiusVp2Sigma(float radiusVp, float dipScale); 54 55 enum FilterType { 56 NONE = 0, 57 BLUR, 58 MATERIAL, 59 LIGHT_UP_EFFECT, 60 AIBAR, 61 LINEAR_GRADIENT_BLUR, 62 FOREGROUND_EFFECT, 63 MOTION_BLUR, 64 SPHERIZE_EFFECT, 65 COLORFUL_SHADOW, 66 MAGNIFIER, 67 WATER_RIPPLE, 68 COMPOUND_EFFECT, 69 FLY_OUT, 70 DISTORT, 71 }; GetFilterType()72 FilterType GetFilterType() const 73 { 74 return type_; 75 } 76 SetFilterType(FilterType type)77 void SetFilterType(FilterType type) 78 { 79 type_ = type; 80 hash_ = SkOpts::hash(&type_, sizeof(type_), hash_); 81 } 82 IsValid()83 virtual bool IsValid() const 84 { 85 return type_ != FilterType::NONE; 86 } 87 DecreasePrecision(float value)88 float DecreasePrecision(float value) 89 { 90 // preserve two digital precision when calculating hash, this can reuse filterCache as much as possible. 91 return 0.01 * round(value * 100); 92 } 93 Hash()94 virtual uint32_t Hash() const 95 { 96 return hash_; 97 } 98 99 virtual bool IsNearEqual( 100 const std::shared_ptr<RSFilter>& other, float threshold = std::numeric_limits<float>::epsilon()) const 101 { 102 return true; 103 } 104 105 virtual bool IsNearZero(float threshold = std::numeric_limits<float>::epsilon()) const 106 { 107 return true; 108 } 109 IsEqual(const std::shared_ptr<RSFilter> & other)110 virtual bool IsEqual(const std::shared_ptr<RSFilter>& other) const 111 { 112 return true; 113 } 114 IsEqualZero()115 virtual bool IsEqualZero() const 116 { 117 return true; 118 } 119 NeedSnapshotOutset()120 bool NeedSnapshotOutset() const 121 { 122 return needSnapshotOutset_; 123 } 124 SetSnapshotOutset(bool needSnapshotOutset)125 void SetSnapshotOutset(bool needSnapshotOutset) 126 { 127 needSnapshotOutset_ = needSnapshotOutset; 128 } 129 130 protected: 131 FilterType type_; 132 uint32_t hash_ = 0; 133 bool needSnapshotOutset_ = true; 134 RSFilter(); Add(const std::shared_ptr<RSFilter> & rhs)135 virtual std::shared_ptr<RSFilter> Add(const std::shared_ptr<RSFilter>& rhs) { return nullptr; } Sub(const std::shared_ptr<RSFilter> & rhs)136 virtual std::shared_ptr<RSFilter> Sub(const std::shared_ptr<RSFilter>& rhs) { return nullptr; } Multiply(float rhs)137 virtual std::shared_ptr<RSFilter> Multiply(float rhs) { return nullptr; } Negate()138 virtual std::shared_ptr<RSFilter> Negate() { return nullptr; } 139 friend RSB_EXPORT std::shared_ptr<RSFilter> operator+(const std::shared_ptr<RSFilter>& lhs, 140 const std::shared_ptr<RSFilter>& rhs); 141 friend RSB_EXPORT std::shared_ptr<RSFilter> operator-(const std::shared_ptr<RSFilter>& lhs, 142 const std::shared_ptr<RSFilter>& rhs); 143 friend RSB_EXPORT std::shared_ptr<RSFilter> operator*(const std::shared_ptr<RSFilter>& lhs, float rhs); 144 }; 145 } // namespace Rosen 146 } // namespace OHOS 147 148 #endif // ROSEN_ENGINE_CORE_RENDER_RS_FILTER_H 149