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_BASE_RENDER_RENDER_DRAWING_RS_DRAWING_FILTER_H 16 #define RENDER_SERVICE_BASE_RENDER_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_hps_blur.h" 29 #include "render/rs_render_filter_base.h" 30 31 namespace OHOS { 32 namespace Rosen { 33 class RSPaintFilterCanvas; 34 class RSB_EXPORT RSDrawingFilter : public RSFilter { 35 public: 36 RSDrawingFilter() = default; 37 RSDrawingFilter(std::shared_ptr<Drawing::ImageFilter> imageFilter, uint32_t hash); 38 RSDrawingFilter(std::shared_ptr<RSRenderFilterParaBase> shaderFilter); 39 RSDrawingFilter(std::shared_ptr<Drawing::ImageFilter> imageFilter, 40 std::shared_ptr<RSRenderFilterParaBase> shaderFilter, uint32_t hash); 41 RSDrawingFilter(std::shared_ptr<Drawing::ImageFilter> imageFilter, 42 std::vector<std::shared_ptr<RSRenderFilterParaBase>> shaderFilters, uint32_t hash); 43 ~RSDrawingFilter() override; 44 45 std::string GetDescription() override; 46 std::string GetDetailedDescription() override; 47 Drawing::Brush GetBrush(float brushAlpha = 1.0f) const; 48 49 struct DrawImageRectParams { 50 bool discardCanvas; 51 bool offscreenDraw; 52 }; 53 54 void DrawImageRect(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image> image, 55 const Drawing::Rect& src, const Drawing::Rect& dst, const DrawImageRectParams params = { false, false }); 56 std::vector<std::shared_ptr<RSRenderFilterParaBase>> GetShaderFilters() const; 57 void InsertShaderFilter(std::shared_ptr<RSRenderFilterParaBase> shaderFilter); 58 std::shared_ptr<Drawing::ImageFilter> GetImageFilter() const; 59 void SetImageFilter(std::shared_ptr<Drawing::ImageFilter> imageFilter); GetShaderFilterWithType(RSUIFilterType type)60 std::shared_ptr<RSRenderFilterParaBase> GetShaderFilterWithType(RSUIFilterType type) 61 { 62 for (const auto& shaderFilter : shaderFilters_) { 63 if (shaderFilter->GetType() == type) { 64 return shaderFilter; 65 } 66 } 67 return nullptr; 68 } 69 void OnSync() override; IsDrawingFilter()70 bool IsDrawingFilter() const override 71 { 72 return true; 73 } 74 uint32_t Hash() const override; 75 uint32_t ShaderHash() const; 76 uint32_t ImageHash() const; 77 uint32_t RenderFilterHash() const; 78 std::shared_ptr<RSDrawingFilter> Compose(const std::shared_ptr<RSDrawingFilter> other) const; 79 std::shared_ptr<RSDrawingFilter> Compose(const std::shared_ptr<Drawing::ImageFilter> other, uint32_t hash) const; 80 std::shared_ptr<RSDrawingFilter> Compose(const std::shared_ptr<RSRenderFilterParaBase> other) const; 81 void SetNGRenderFilter(std::shared_ptr<RSNGRenderFilterBase> filter); 82 GetNGRenderFilter()83 inline std::shared_ptr<RSNGRenderFilterBase> GetNGRenderFilter() const 84 { 85 return renderFilter_; 86 } 87 88 void GenerateAndUpdateGEVisualEffect(); 89 90 void SetGeometry(Drawing::Canvas& canvas, float geoWidth, float geoHeight); 91 void SetDisplayHeadroom(float headroom); 92 CanSkipFrame()93 bool CanSkipFrame() const 94 { 95 return canSkipFrame_; 96 } 97 SetSkipFrame(bool canSkipFrame)98 void SetSkipFrame(bool canSkipFrame) 99 { 100 canSkipFrame_ = canSkipFrame; 101 } 102 103 static bool CanSkipFrame(float radius); SetSaturationForHPS(float saturationForHPS)104 void SetSaturationForHPS(float saturationForHPS) 105 { 106 saturationForHPS_ = saturationForHPS; 107 } SetBrightnessForHPS(float brightnessForHPS)108 void SetBrightnessForHPS(float brightnessForHPS) 109 { 110 brightnessForHPS_ = brightnessForHPS; 111 } 112 void PreProcess(std::shared_ptr<Drawing::Image>& image); 113 void PostProcess(Drawing::Canvas& canvas); 114 115 void ApplyColorFilter(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image>& image, 116 const Drawing::Rect& src, const Drawing::Rect& dst, float brushAlpha); 117 118 bool NeedForceSubmit() const override; 119 120 private: 121 struct DrawImageRectAttributes { 122 Drawing::Rect src; 123 Drawing::Rect dst; 124 bool discardCanvas; 125 float brushAlpha; 126 }; 127 void DrawImageRectInternal(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image> image, 128 const DrawImageRectAttributes& attr); 129 float PrepareAlphaForOnScreenDraw(RSPaintFilterCanvas& paintFilterCanvas); 130 std::shared_ptr<Drawing::ImageFilter> ProcessImageFilter(float brushAlpha) const; 131 132 bool IsHpsBlurApplied(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image>& outImage, 133 const DrawImageRectAttributes& attr, const Drawing::Brush& brush, float radius); 134 /** 135 * @brief Apply GE Kawase Blur on image 136 * 137 * KAWASE filter will be excuted by GE Kawase BLur on image 138 * 139 * @param canvas the canvas that draw Kawase filter 140 * @param outImage the image that needed to apply Kawase filter 141 * @param attr the Attributes of canvas 142 * @param brush the brush that will be attached on canvas 143 * @param kawaseShaderFilter the Kawase filter that will be applyed 144 */ 145 void DrawKawaseEffect(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image>& outImage, 146 const DrawImageRectAttributes& attr, const Drawing::Brush& brush, 147 std::shared_ptr<RSRenderFilterParaBase>& kawaseShaderFilter); 148 /** 149 * @brief Apply HPS Effect on image 150 * 151 * Try to apply all shaderFilters_ by HPSEffect on image 152 * When calling ApplyHpsImageEffect(), 153 if outImage == nullptr means: 154 HPS draw shaderFilters_ fail, and func will return false, 155 if outImage != nullptr and return true means: 156 shaderFilters_ contain Kawase or Mesa and HPS draw shaderFilters_ succ, 157 if outImage != nullptr and return false means: 158 shaderFilters_ not contain Kawase and Mesa and HPS draw shaderFilters_ succ. 159 * 160 * @param canvas the canvas that draw all shaderFilters_ 161 * @param image the image that needed to apply all shaderFilters_ 162 * @param outImage the image after applying all shaderFilters_ 163 * @param attr the Attributes of canvas 164 * @param brush the brush that will be attached on canvas 165 * @return true when HPS draw Kawase or Mesa succ, false otherwise. 166 */ 167 bool ApplyHpsImageEffect(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image>& image, 168 std::shared_ptr<Drawing::Image>& outImage, const DrawImageRectAttributes& attr, Drawing::Brush& brush); 169 bool ApplyImageEffectWithLightBlur(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image>& image, 170 const DrawImageRectAttributes& attr, const Drawing::Brush& brush); 171 void ApplyImageEffect(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image>& image, 172 const std::shared_ptr<Drawing::GEVisualEffectContainer>& visualEffectContainer, 173 const DrawImageRectAttributes& attr); 174 void ProfilerLogImageEffect(std::shared_ptr<Drawing::GEVisualEffectContainer> visualEffectContainer, 175 const std::shared_ptr<Drawing::Image>& image, const Drawing::Rect& src, 176 const std::shared_ptr<Drawing::Image>& outImage); 177 std::string GetFilterTypeString() const; 178 std::shared_ptr<Drawing::ImageFilter> imageFilter_ = nullptr; 179 std::vector<std::shared_ptr<RSRenderFilterParaBase>> shaderFilters_; 180 std::shared_ptr<RSNGRenderFilterBase> renderFilter_ = nullptr; 181 std::shared_ptr<Drawing::GEVisualEffectContainer> visualEffectContainer_; 182 uint32_t imageFilterHash_ = 0; 183 uint32_t renderFilterHash_ = 0; 184 bool canSkipFrame_ = false; 185 bool canSkipMaskColor_ = false; 186 float saturationForHPS_ = 1.f; 187 float brightnessForHPS_ = 1.f; 188 friend class RSMarshallingHelper; 189 }; 190 } // namespace Rosen 191 } // namespace OHOS 192 193 #endif // RENDER_SERVICE_BASE_RENDER_RENDER_DRAWING_RS_DRAWING_FILTER_H