1 /* 2 * Copyright (c) 2021 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 38 namespace OHOS { 39 namespace Rosen { 40 enum MATERIAL_BLUR_STYLE : int { 41 // card blur style 42 STYLE_CARD_THIN_LIGHT = 1, 43 STYLE_CARD_LIGHT = 2, 44 STYLE_CARD_THICK_LIGHT = 3, 45 STYLE_CARD_THIN_DARK = 4, 46 STYLE_CARD_DARK = 5, 47 STYLE_CARD_THICK_DARK = 6, 48 49 // background blur style 50 STYLE_BACKGROUND_SMALL_LIGHT = 101, 51 STYLE_BACKGROUND_MEDIUM_LIGHT = 102, 52 STYLE_BACKGROUND_LARGE_LIGHT = 103, 53 STYLE_BACKGROUND_XLARGE_LIGHT = 104, 54 STYLE_BACKGROUND_SMALL_DARK = 105, 55 STYLE_BACKGROUND_MEDIUM_DARK = 106, 56 STYLE_BACKGROUND_LARGE_DARK = 107, 57 STYLE_BACKGROUND_XLARGE_DARK = 108 58 }; 59 // material blur style params 60 struct MaterialParam { 61 float radius; 62 float saturation; 63 float brightness; 64 RSColor maskColor; 65 }; 66 #ifndef USE_ROSEN_DRAWING 67 class RSB_EXPORT RSMaterialFilter : public RSSkiaFilter { 68 #else 69 class RSB_EXPORT RSMaterialFilter : public RSDrawingFilter { 70 #endif 71 public: 72 RSMaterialFilter(int style, float dipScale, BLUR_COLOR_MODE mode, float ratio); 73 RSMaterialFilter(MaterialParam materialParam, BLUR_COLOR_MODE mode); 74 RSMaterialFilter(const RSMaterialFilter&) = delete; 75 RSMaterialFilter operator=(const RSMaterialFilter&) = delete; 76 ~RSMaterialFilter() override; 77 std::shared_ptr<RSFilter> TransformFilter(float fraction) const; 78 bool IsValid() const override; 79 #ifndef USE_ROSEN_DRAWING 80 void PreProcess(sk_sp<SkImage> image) override; 81 #else 82 void PreProcess(std::shared_ptr<Drawing::Image> image) override; 83 #endif 84 void PostProcess(RSPaintFilterCanvas& canvas) override; 85 #ifndef USE_ROSEN_DRAWING 86 std::shared_ptr<RSSkiaFilter> Compose(const std::shared_ptr<RSSkiaFilter>& other) const override; 87 #else 88 std::shared_ptr<RSDrawingFilter> Compose(const std::shared_ptr<RSDrawingFilter>& other) const override; 89 #endif 90 std::string GetDescription() override; 91 92 std::shared_ptr<RSFilter> Add(const std::shared_ptr<RSFilter>& rhs) override; 93 std::shared_ptr<RSFilter> Sub(const std::shared_ptr<RSFilter>& rhs) override; 94 std::shared_ptr<RSFilter> Multiply(float rhs) override; 95 std::shared_ptr<RSFilter> Negate() override; 96 97 #ifndef USE_ROSEN_DRAWING 98 void DrawImageRect( 99 SkCanvas& canvas, const sk_sp<SkImage>& image, const SkRect& src, const SkRect& dst) const override; 100 #else 101 void DrawImageRect(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image>& image, 102 const Drawing::Rect& src, const Drawing::Rect& dst) const override; 103 #endif 104 105 bool CanSkipFrame() const override; 106 107 private: 108 BLUR_COLOR_MODE colorMode_; 109 float radius_ {}; 110 float saturation_ = 1.f; 111 float brightness_ = 1.f; 112 RSColor maskColor_ = RSColor(); 113 114 #ifndef USE_ROSEN_DRAWING 115 sk_sp<SkColorFilter> GetColorFilter(float sat, float brightness); 116 sk_sp<SkImageFilter> CreateMaterialStyle(MATERIAL_BLUR_STYLE style, float dipScale, float ratio); 117 sk_sp<SkImageFilter> CreateMaterialFilter(float radius, float sat, float brightness); 118 #else 119 std::shared_ptr<Drawing::ImageFilter> CreateMaterialStyle(MATERIAL_BLUR_STYLE style, float dipScale, float ratio); 120 std::shared_ptr<Drawing::ImageFilter> CreateMaterialFilter(float radius, float sat, float brightness); 121 #endif 122 static float RadiusVp2Sigma(float radiusVp, float dipScale); 123 124 #ifndef USE_ROSEN_DRAWING 125 bool useKawase_ = false; 126 static std::shared_ptr<KawaseBlurFilter> kawaseFunc_; 127 #endif 128 sk_sp<SkColorFilter> colorFilter_; 129 friend class RSMarshallingHelper; 130 }; 131 } // namespace Rosen 132 } // namespace OHOS 133 134 #endif // RENDER_SERVICE_CLIENT_CORE_RENDER_RS_BLUR_FILTER_H