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() const;
47     void DrawImageRect(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image> image,
48         const Drawing::Rect& src, const Drawing::Rect& dst);
49     std::vector<std::shared_ptr<RSShaderFilter>> GetShaderFilters() const;
50     void InsertShaderFilter(std::shared_ptr<RSShaderFilter> shaderFilter);
51     std::shared_ptr<Drawing::ImageFilter> GetImageFilter() const;
52     void SetImageFilter(std::shared_ptr<Drawing::ImageFilter> imageFilter);
GetShaderFilterWithType(RSShaderFilter::ShaderFilterType type)53     std::shared_ptr<RSShaderFilter> GetShaderFilterWithType(RSShaderFilter::ShaderFilterType type)
54     {
55         for (const auto& shaderFilter : shaderFilters_) {
56             if (shaderFilter->GetShaderFilterType() == type) {
57                 return shaderFilter;
58             }
59         }
60         return nullptr;
61     }
62 
63     uint32_t Hash() const override;
64     uint32_t ShaderHash() const;
65     uint32_t ImageHash() const;
66     std::shared_ptr<RSDrawingFilter> Compose(const std::shared_ptr<RSDrawingFilter> other) const;
67     std::shared_ptr<RSDrawingFilter> Compose(const std::shared_ptr<Drawing::ImageFilter> other, uint32_t hash) const;
68     std::shared_ptr<RSDrawingFilter> Compose(const std::shared_ptr<RSShaderFilter> other) const;
CanSkipFrame()69     bool CanSkipFrame() const
70     {
71         return canSkipFrame_;
72     }
73 
SetSkipFrame(bool canSkipFrame)74     void SetSkipFrame(bool canSkipFrame)
75     {
76         canSkipFrame_ = canSkipFrame;
77     }
78 
79     static bool CanSkipFrame(float radius);
SetSaturationForHPS(float saturationForHPS)80     void SetSaturationForHPS(float saturationForHPS)
81     {
82         saturationForHPS_ = saturationForHPS;
83     }
SetBrightnessForHPS(float brightnessForHPS)84     void SetBrightnessForHPS(float brightnessForHPS)
85     {
86         brightnessForHPS_ = brightnessForHPS;
87     }
88     void PreProcess(std::shared_ptr<Drawing::Image>& image);
89     void PostProcess(Drawing::Canvas& canvas);
90 
91     void ApplyColorFilter(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image>& image,
92         const Drawing::Rect& src, const Drawing::Rect& dst);
93 
94 private:
95     std::shared_ptr<Drawing::ImageFilter> imageFilter_ = nullptr;
96     std::vector<std::shared_ptr<RSShaderFilter>> shaderFilters_;
97     uint32_t imageFilterHash_;
98     bool canSkipFrame_ = false;
99     float saturationForHPS_ = 1.f;
100     float brightnessForHPS_ = 1.f;
101     friend class RSMarshallingHelper;
102 };
103 } // namespace Rosen
104 } // namespace OHOS
105 
106 #endif // RENDER_SERVICE_CLIENT_CORE_RENDER_DRAWING_RS_DRAWING_FILTER_H