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 #ifndef RENDER_SERVICE_CLIENT_CORE_RENDER_RS_MATERIAL_FILTER_H 16 #define RENDER_SERVICE_CLIENT_CORE_RENDER_RS_MATERIAL_FILTER_H 17 18 #ifdef NEW_SKIA 19 #include "include/effects/SkRuntimeEffect.h" 20 #endif 21 22 #include "common/rs_color.h" 23 #include "render/rs_skia_filter.h" 24 #include "render/rs_kawase_blur.h" 25 26 #ifndef USE_ROSEN_DRAWING 27 #include "include/core/SkColorFilter.h" 28 #include "include/core/SkColor.h" 29 #include "include/effects/SkColorMatrix.h" 30 #include "include/effects/SkImageFilters.h" 31 #else 32 #include "effect/color_filter.h" 33 #include "draw/color.h" 34 #include "effect/color_matrix.h" 35 #include "effect/image_filter.h" 36 #endif 37 #include "property/rs_color_picker_cache_task.h" 38 39 namespace OHOS { 40 namespace Rosen { 41 enum MATERIAL_BLUR_STYLE : int { 42 // card blur style 43 STYLE_CARD_THIN_LIGHT = 1, 44 STYLE_CARD_LIGHT = 2, 45 STYLE_CARD_THICK_LIGHT = 3, 46 STYLE_CARD_THIN_DARK = 4, 47 STYLE_CARD_DARK = 5, 48 STYLE_CARD_THICK_DARK = 6, 49 50 // background blur style 51 STYLE_BACKGROUND_SMALL_LIGHT = 101, 52 STYLE_BACKGROUND_MEDIUM_LIGHT = 102, 53 STYLE_BACKGROUND_LARGE_LIGHT = 103, 54 STYLE_BACKGROUND_XLARGE_LIGHT = 104, 55 STYLE_BACKGROUND_SMALL_DARK = 105, 56 STYLE_BACKGROUND_MEDIUM_DARK = 106, 57 STYLE_BACKGROUND_LARGE_DARK = 107, 58 STYLE_BACKGROUND_XLARGE_DARK = 108 59 }; 60 // material blur style params 61 struct MaterialParam { 62 float radius; 63 float saturation; 64 float brightness; 65 RSColor maskColor; 66 }; 67 #ifndef USE_ROSEN_DRAWING 68 class RSB_EXPORT RSMaterialFilter : public RSSkiaFilter { 69 #else 70 class RSB_EXPORT RSMaterialFilter : public RSDrawingFilter { 71 #endif 72 public: 73 RSMaterialFilter(int style, float dipScale, BLUR_COLOR_MODE mode, float ratio); 74 RSMaterialFilter(MaterialParam materialParam, BLUR_COLOR_MODE mode); 75 RSMaterialFilter(const RSMaterialFilter&) = delete; 76 RSMaterialFilter operator=(const RSMaterialFilter&) = delete; 77 ~RSMaterialFilter() override; 78 std::shared_ptr<RSFilter> TransformFilter(float fraction) const; 79 bool IsValid() const override; 80 #ifndef USE_ROSEN_DRAWING 81 void PreProcess(sk_sp<SkImage> image) override; 82 #else 83 void PreProcess(std::shared_ptr<Drawing::Image> image) override; 84 #endif 85 void PostProcess(RSPaintFilterCanvas& canvas) override; 86 #ifndef USE_ROSEN_DRAWING 87 std::shared_ptr<RSSkiaFilter> Compose(const std::shared_ptr<RSSkiaFilter>& other) const override; 88 #else 89 std::shared_ptr<RSDrawingFilter> Compose(const std::shared_ptr<RSDrawingFilter>& other) const override; 90 #endif 91 std::string GetDescription() override; 92 93 std::shared_ptr<RSFilter> Add(const std::shared_ptr<RSFilter>& rhs) override; 94 std::shared_ptr<RSFilter> Sub(const std::shared_ptr<RSFilter>& rhs) override; 95 std::shared_ptr<RSFilter> Multiply(float rhs) override; 96 std::shared_ptr<RSFilter> Negate() override; 97 98 #ifndef USE_ROSEN_DRAWING 99 void DrawImageRect( 100 SkCanvas& canvas, const sk_sp<SkImage>& image, const SkRect& src, const SkRect& dst) const override; 101 #else 102 void DrawImageRect(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image>& image, 103 const Drawing::Rect& src, const Drawing::Rect& dst) const override; 104 #endif 105 float GetRadius() const; 106 bool CanSkipFrame() const override; 107 108 bool IsNearEqual( 109 const std::shared_ptr<RSFilter>& other, float threshold = std::numeric_limits<float>::epsilon()) const override; 110 bool IsNearZero(float threshold = std::numeric_limits<float>::epsilon()) const override; 111 void SetGreyCoef(float greyCoef1, float greyCoef2, bool isGreyCoefValid) override; 112 113 // color picker subthread 114 const std::shared_ptr<RSColorPickerCacheTask>& GetColorPickerCacheTask() const; 115 void ReleaseColorPickerFilter(); 116 private: 117 BLUR_COLOR_MODE colorMode_; 118 float radius_ {}; 119 float saturation_ = 1.f; 120 float brightness_ = 1.f; 121 float greyCoef1_ = 0.f; 122 float greyCoef2_ = 0.f; 123 bool isGreyCoefValid_ = false; 124 RSColor maskColor_ = RSColor(); 125 126 #ifndef USE_ROSEN_DRAWING 127 sk_sp<SkColorFilter> GetColorFilter(float sat, float brightness); 128 sk_sp<SkImageFilter> CreateMaterialStyle(MATERIAL_BLUR_STYLE style, float dipScale, float ratio); 129 sk_sp<SkImageFilter> CreateMaterialFilter(float radius, float sat, float brightness); 130 #else 131 std::shared_ptr<Drawing::ColorFilter> GetColorFilter(float sat, float brightness); 132 std::shared_ptr<Drawing::ImageFilter> CreateMaterialStyle(MATERIAL_BLUR_STYLE style, float dipScale, float ratio); 133 std::shared_ptr<Drawing::ImageFilter> CreateMaterialFilter(float radius, float sat, float brightness); 134 #endif 135 static float RadiusVp2Sigma(float radiusVp, float dipScale); 136 137 #ifndef USE_ROSEN_DRAWING 138 sk_sp<SkColorFilter> colorFilter_; 139 #else 140 std::shared_ptr<Drawing::ColorFilter> colorFilter_; 141 #endif 142 std::shared_ptr<RSColorPickerCacheTask> colorPickerTask_; 143 friend class RSMarshallingHelper; 144 }; 145 } // namespace Rosen 146 } // namespace OHOS 147 148 #endif // RENDER_SERVICE_CLIENT_CORE_RENDER_RS_BLUR_FILTER_H