1 /* 2 * Copyright (c) 2024 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_DRAWING_RS_DRAWING_FILTER_H 16 #define RENDER_SERVICE_CLIENT_CORE_RENDER_DRAWING_RS_DRAWING_FILTER_H 17 18 #include "common/rs_color.h" 19 #include "draw/brush.h" 20 #include "draw/canvas.h" 21 #include "draw/color.h" 22 #include "effect/color_filter.h" 23 #include "effect/color_matrix.h" 24 #include "effect/image_filter.h" 25 #include "effect/runtime_shader_builder.h" 26 #include "render/rs_filter.h" 27 #include "render/rs_kawase_blur.h" 28 #include "render/rs_shader_filter.h" 29 #include "render/rs_hps_blur.h" 30 31 namespace OHOS { 32 namespace Rosen { 33 class RSPaintFilterCanvas; 34 class RSDrawingFilter : public RSFilter { 35 public: 36 RSDrawingFilter(std::shared_ptr<Drawing::ImageFilter> imageFilter, uint32_t hash); 37 RSDrawingFilter(std::shared_ptr<RSShaderFilter> shaderFilter); 38 RSDrawingFilter(std::shared_ptr<Drawing::ImageFilter> imageFilter, 39 std::shared_ptr<RSShaderFilter> shaderFilter, uint32_t hash); 40 RSDrawingFilter(std::shared_ptr<Drawing::ImageFilter> imageFilter, 41 std::vector<std::shared_ptr<RSShaderFilter>> shaderFilters, uint32_t hash); 42 ~RSDrawingFilter() override; 43 44 std::string GetDescription() override; 45 std::string GetDetailedDescription() override; 46 Drawing::Brush GetBrush(float brushAlpha = 1.0f) const; 47 48 struct DrawImageRectParams { 49 bool discardCanvas; 50 bool offscreenDraw; 51 }; 52 53 void DrawImageRect(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image> image, 54 const Drawing::Rect& src, const Drawing::Rect& dst, const DrawImageRectParams params = { false, false }); 55 std::vector<std::shared_ptr<RSShaderFilter>> GetShaderFilters() const; 56 void InsertShaderFilter(std::shared_ptr<RSShaderFilter> shaderFilter); 57 std::shared_ptr<Drawing::ImageFilter> GetImageFilter() const; 58 void SetImageFilter(std::shared_ptr<Drawing::ImageFilter> imageFilter); GetShaderFilterWithType(RSShaderFilter::ShaderFilterType type)59 std::shared_ptr<RSShaderFilter> GetShaderFilterWithType(RSShaderFilter::ShaderFilterType type) 60 { 61 for (const auto& shaderFilter : shaderFilters_) { 62 if (shaderFilter->GetShaderFilterType() == type) { 63 return shaderFilter; 64 } 65 } 66 return nullptr; 67 } 68 69 uint32_t Hash() const override; 70 uint32_t ShaderHash() const; 71 uint32_t ImageHash() const; 72 std::shared_ptr<RSDrawingFilter> Compose(const std::shared_ptr<RSDrawingFilter> other) const; 73 std::shared_ptr<RSDrawingFilter> Compose(const std::shared_ptr<Drawing::ImageFilter> other, uint32_t hash) const; 74 std::shared_ptr<RSDrawingFilter> Compose(const std::shared_ptr<RSShaderFilter> other) const; CanSkipFrame()75 bool CanSkipFrame() const 76 { 77 return canSkipFrame_; 78 } 79 SetSkipFrame(bool canSkipFrame)80 void SetSkipFrame(bool canSkipFrame) 81 { 82 canSkipFrame_ = canSkipFrame; 83 } 84 85 static bool CanSkipFrame(float radius); SetSaturationForHPS(float saturationForHPS)86 void SetSaturationForHPS(float saturationForHPS) 87 { 88 saturationForHPS_ = saturationForHPS; 89 } SetBrightnessForHPS(float brightnessForHPS)90 void SetBrightnessForHPS(float brightnessForHPS) 91 { 92 brightnessForHPS_ = brightnessForHPS; 93 } 94 void PreProcess(std::shared_ptr<Drawing::Image>& image); 95 void PostProcess(Drawing::Canvas& canvas); 96 97 void ApplyColorFilter(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image>& image, 98 const Drawing::Rect& src, const Drawing::Rect& dst, float brushAlpha); 99 100 private: 101 struct DrawImageRectAttributes { 102 Drawing::Rect src; 103 Drawing::Rect dst; 104 bool discardCanvas; 105 float brushAlpha; 106 }; 107 void DrawImageRectInternal(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image> image, 108 const DrawImageRectAttributes& attr); 109 float PrepareAlphaForOnScreenDraw(RSPaintFilterCanvas& paintFilterCanvas); 110 std::shared_ptr<Drawing::ImageFilter> ProcessImageFilter(float brushAlpha) const; 111 112 void ApplyImageEffect(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image>& image, 113 const std::shared_ptr<Drawing::GEVisualEffectContainer>& visualEffectContainer, 114 const DrawImageRectAttributes& attr); 115 std::string GetFilterTypeString() const; 116 std::shared_ptr<Drawing::ImageFilter> imageFilter_ = nullptr; 117 std::vector<std::shared_ptr<RSShaderFilter>> shaderFilters_; 118 uint32_t imageFilterHash_ = 0; 119 bool canSkipFrame_ = false; 120 float saturationForHPS_ = 1.f; 121 float brightnessForHPS_ = 1.f; 122 friend class RSMarshallingHelper; 123 }; 124 } // namespace Rosen 125 } // namespace OHOS 126 127 #endif // RENDER_SERVICE_CLIENT_CORE_RENDER_DRAWING_RS_DRAWING_FILTER_H