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_RS_FOREGROUND_EFFECT_FILTER_H
16 #define RENDER_SERVICE_CLIENT_CORE_RENDER_RS_FOREGROUND_EFFECT_FILTER_H
17 
18 #include <memory>
19 
20 #include "effect/runtime_effect.h"
21 #include "effect/runtime_shader_builder.h"
22 #include "render/rs_skia_filter.h"
23 
24 namespace OHOS {
25 namespace Rosen {
26 struct ForegroundEffectParam {
27     Drawing::Rect src;
28     Drawing::Rect dst;
29 
ForegroundEffectParamForegroundEffectParam30     ForegroundEffectParam(const Drawing::Rect& src, const Drawing::Rect& dst)
31         : src(src), dst(dst) {}
32 };
33 class RSB_EXPORT RSForegroundEffectFilter : public RSDrawingFilterOriginal {
34 public:
35     RSForegroundEffectFilter(float blurRadius);
36     RSForegroundEffectFilter(const RSForegroundEffectFilter&) = delete;
37     RSForegroundEffectFilter operator=(const RSForegroundEffectFilter&) = delete;
38     ~RSForegroundEffectFilter() override;
39 
40     bool IsValid() const override;
41     std::string GetDescription() override;
42     void DrawImageRect(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image>& image,
43         const Drawing::Rect& src, const Drawing::Rect& dst) const override;
PreProcess(std::shared_ptr<Drawing::Image> image)44     void PreProcess(std::shared_ptr<Drawing::Image> image) override {};
PostProcess(Drawing::Canvas & canvas)45     void PostProcess(Drawing::Canvas& canvas) override {};
46     float GetDirtyExtension() const;
47 
Compose(const std::shared_ptr<RSDrawingFilterOriginal> & other)48     std::shared_ptr<RSDrawingFilterOriginal> Compose(
49         const std::shared_ptr<RSDrawingFilterOriginal>& other) const override
50     {
51         return nullptr;
52     }
53 
54 private:
55     float blurScale_ {};
56     float blurRadius_ {}; //kawase blur's blur radius
57     int numberOfPasses_{};
58     float radiusByPasses_{};
59     float unit_{};
60 
61     static constexpr float BASE_BLUR_SCALE = 0.5f; // base downSample radio
62     static constexpr uint32_t MAX_PASSES_LARGE_RADIUS = 7;
63     static constexpr float DILATED_CONVOLUTION_LARGE_RADIUS = 4.6f;
64     static constexpr int EXPAND_UNIT_NUM = 4;
65     static constexpr int MOVE_UNIT_NUM = 2;
66     static constexpr float BLUR_RADIUS_LIMIT = 10000.0f;
67 
68     static std::shared_ptr<Drawing::RuntimeShaderBuilder> MakeForegroundEffect();
69     void ComputeParamter(int radius);
70     void AdjustRadiusAndScale();
71     void ComputePassesAndUnit();
72     void ApplyForegroundEffect(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image>& image,
73         const ForegroundEffectParam& param) const;
74 
75     friend class RSMarshallingHelper;
76 };
77 } // namespace Rosen
78 } // namespace OHOS
79 
80 #endif // RENDER_SERVICE_CLIENT_CORE_RENDER_RS_FOREGROUND_EFFECT_FILTER_H