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 ATTRACTION_EFFECT, 67 MAGNIFIER, 68 WATER_RIPPLE, 69 COMPOUND_EFFECT, 70 FLY_OUT, 71 DISTORT, 72 }; GetFilterType()73 FilterType GetFilterType() const 74 { 75 return type_; 76 } 77 SetFilterType(FilterType type)78 void SetFilterType(FilterType type) 79 { 80 type_ = type; 81 hash_ = SkOpts::hash(&type_, sizeof(type_), hash_); 82 } 83 IsValid()84 virtual bool IsValid() const 85 { 86 return type_ != FilterType::NONE; 87 } 88 DecreasePrecision(float value)89 float DecreasePrecision(float value) 90 { 91 // preserve two digital precision when calculating hash, this can reuse filterCache as much as possible. 92 return 0.01 * round(value * 100); 93 } 94 Hash()95 virtual uint32_t Hash() const 96 { 97 return hash_; 98 } 99 100 virtual bool IsNearEqual( 101 const std::shared_ptr<RSFilter>& other, float threshold = std::numeric_limits<float>::epsilon()) const 102 { 103 return true; 104 } 105 106 virtual bool IsNearZero(float threshold = std::numeric_limits<float>::epsilon()) const 107 { 108 return true; 109 } 110 IsEqual(const std::shared_ptr<RSFilter> & other)111 virtual bool IsEqual(const std::shared_ptr<RSFilter>& other) const 112 { 113 return true; 114 } 115 IsEqualZero()116 virtual bool IsEqualZero() const 117 { 118 return true; 119 } 120 NeedSnapshotOutset()121 bool NeedSnapshotOutset() const 122 { 123 return needSnapshotOutset_; 124 } 125 SetSnapshotOutset(bool needSnapshotOutset)126 void SetSnapshotOutset(bool needSnapshotOutset) 127 { 128 needSnapshotOutset_ = needSnapshotOutset; 129 } 130 131 protected: 132 FilterType type_; 133 uint32_t hash_ = 0; 134 bool needSnapshotOutset_ = true; 135 RSFilter(); Add(const std::shared_ptr<RSFilter> & rhs)136 virtual std::shared_ptr<RSFilter> Add(const std::shared_ptr<RSFilter>& rhs) { return nullptr; } Sub(const std::shared_ptr<RSFilter> & rhs)137 virtual std::shared_ptr<RSFilter> Sub(const std::shared_ptr<RSFilter>& rhs) { return nullptr; } Multiply(float rhs)138 virtual std::shared_ptr<RSFilter> Multiply(float rhs) { return nullptr; } Negate()139 virtual std::shared_ptr<RSFilter> Negate() { return nullptr; } 140 friend RSB_EXPORT std::shared_ptr<RSFilter> operator+(const std::shared_ptr<RSFilter>& lhs, 141 const std::shared_ptr<RSFilter>& rhs); 142 friend RSB_EXPORT std::shared_ptr<RSFilter> operator-(const std::shared_ptr<RSFilter>& lhs, 143 const std::shared_ptr<RSFilter>& rhs); 144 friend RSB_EXPORT std::shared_ptr<RSFilter> operator*(const std::shared_ptr<RSFilter>& lhs, float rhs); 145 }; 146 } // namespace Rosen 147 } // namespace OHOS 148 149 #endif // ROSEN_ENGINE_CORE_RENDER_RS_FILTER_H 150