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 #ifndef USE_ROSEN_DRAWING 25 #include "include/gpu/GrDirectContext.h" 26 #else 27 #include "image/gpu_context.h" 28 #endif 29 30 namespace OHOS { 31 namespace Rosen { 32 enum BLUR_COLOR_MODE : int { 33 PRE_DEFINED = 0, // use the pre-defined mask color 34 AVERAGE = 1, // use the average color of the blurred area as mask color 35 FASTAVERAGE = 2, 36 DEFAULT = PRE_DEFINED 37 }; 38 39 class RSB_EXPORT RSFilter : public std::enable_shared_from_this<RSFilter> { 40 public: 41 class RSFilterTask { 42 public: 43 #ifndef USE_ROSEN_DRAWING 44 virtual bool InitSurface(GrRecordingContext* grContext); 45 #else 46 virtual bool InitSurface(Drawing::GPUContext* grContext); 47 #endif 48 virtual bool Render(); 49 virtual bool SaveFilteredImage(); 50 virtual void SwapInit(); 51 virtual bool SetDone(); 52 }; 53 static std::function<void(std::weak_ptr<RSFilter::RSFilterTask>)> postTask; 54 static std::function<void()> clearGpuContext; 55 56 virtual ~RSFilter(); 57 RSFilter(const RSFilter&) = delete; 58 RSFilter(const RSFilter&&) = delete; 59 RSFilter& operator=(const RSFilter&) = delete; 60 RSFilter& operator=(const RSFilter&&) = delete; 61 virtual std::string GetDescription(); 62 static std::shared_ptr<RSFilter> CreateBlurFilter(float blurRadiusX, float blurRadiusY); 63 static std::shared_ptr<RSFilter> CreateMaterialFilter( 64 int style, float dipScale, BLUR_COLOR_MODE mode = DEFAULT, float ratio = 1.0); 65 static std::shared_ptr<RSFilter> CreateMaterialFilter( 66 float radius, float saturation, float brightness, uint32_t colorValue, 67 BLUR_COLOR_MODE mode = BLUR_COLOR_MODE::DEFAULT); 68 static std::shared_ptr<RSFilter> CreateLightUpEffectFilter(float lightUpDegree); 69 70 enum FilterType { 71 NONE = 0, 72 BLUR, 73 MATERIAL, 74 LIGHT_UP_EFFECT, 75 AIBAR, 76 LINEAR_GRADIENT_BLUR, 77 }; GetFilterType()78 FilterType GetFilterType() const 79 { 80 return type_; 81 } IsValid()82 virtual bool IsValid() const 83 { 84 return type_ != FilterType::NONE; 85 } 86 Hash()87 uint32_t Hash() const 88 { 89 return hash_; 90 } 91 92 virtual bool IsNearEqual( 93 const std::shared_ptr<RSFilter>& other, float threshold = std::numeric_limits<float>::epsilon()) const 94 { 95 return true; 96 } 97 98 virtual bool IsNearZero(float threshold = std::numeric_limits<float>::epsilon()) const 99 { 100 return true; 101 } 102 103 protected: 104 FilterType type_; 105 uint32_t hash_ = 0; 106 RSFilter(); Add(const std::shared_ptr<RSFilter> & rhs)107 virtual std::shared_ptr<RSFilter> Add(const std::shared_ptr<RSFilter>& rhs) { return nullptr; } Sub(const std::shared_ptr<RSFilter> & rhs)108 virtual std::shared_ptr<RSFilter> Sub(const std::shared_ptr<RSFilter>& rhs) { return nullptr; } Multiply(float rhs)109 virtual std::shared_ptr<RSFilter> Multiply(float rhs) { return nullptr; } Negate()110 virtual std::shared_ptr<RSFilter> Negate() { return nullptr; } 111 friend RSB_EXPORT std::shared_ptr<RSFilter> operator+(const std::shared_ptr<RSFilter>& lhs, 112 const std::shared_ptr<RSFilter>& rhs); 113 friend RSB_EXPORT std::shared_ptr<RSFilter> operator-(const std::shared_ptr<RSFilter>& lhs, 114 const std::shared_ptr<RSFilter>& rhs); 115 friend RSB_EXPORT std::shared_ptr<RSFilter> operator*(const std::shared_ptr<RSFilter>& lhs, float rhs); 116 }; 117 } // namespace Rosen 118 } // namespace OHOS 119 120 #endif // ROSEN_ENGINE_CORE_RENDER_RS_FILTER_H 121