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 
16 #ifndef RENDER_SERVICE_CLIENT_CORE_RENDER_RS_MOTION_BLUR_FILTER_H
17 #define RENDER_SERVICE_CLIENT_CORE_RENDER_RS_MOTION_BLUR_FILTER_H
18 
19 #include "common/rs_vector2.h"
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 
27 class RSB_EXPORT MotionBlurParam {
28 public:
29     float radius = 0.f;
30     Vector2f scaleAnchor = Vector2f(0.f, 0.f);
31 
MotionBlurParam(float r,Vector2f & s)32     explicit MotionBlurParam(float r, Vector2f& s) : radius(r), scaleAnchor(s) {}
33     ~MotionBlurParam() = default;
34 
35     void Dump(std::string& out) const;
36 };
37 
38 class RSB_EXPORT RSMotionBlurFilter : public RSDrawingFilterOriginal {
39 public:
40     RSMotionBlurFilter(const std::shared_ptr<MotionBlurParam>& para);
41     RSMotionBlurFilter(const RSMotionBlurFilter&) = delete;
42     RSMotionBlurFilter operator=(const RSMotionBlurFilter&) = delete;
43     ~RSMotionBlurFilter() override;
44 
PostProcess(Drawing::Canvas & canvas)45     void PostProcess(Drawing::Canvas& canvas) override {};
46     std::string GetDescription() override;
47     void DrawImageRect(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image>& image,
48         const Drawing::Rect& src, const Drawing::Rect& dst) const override;
PreProcess(std::shared_ptr<Drawing::Image> image)49     void PreProcess(std::shared_ptr<Drawing::Image> image) override {};
Compose(const std::shared_ptr<RSDrawingFilterOriginal> & other)50     std::shared_ptr<RSDrawingFilterOriginal> Compose(
51         const std::shared_ptr<RSDrawingFilterOriginal>& other) const override
52     {
53         return nullptr;
54     }
55 
DisableMotionBlur(bool isDisableMotionBlur)56     void DisableMotionBlur(bool isDisableMotionBlur) override
57     {
58         disableMotionBlur_ = isDisableMotionBlur;
59     }
60 
61 private:
62     void DrawMotionBlur(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image>& image,
63         const Drawing::Rect& src, const Drawing::Rect& dst) const;
64     std::shared_ptr<Drawing::RuntimeShaderBuilder> MakeMotionBlurShader(
65         std::shared_ptr<Drawing::ShaderEffect> srcImageShader, Vector2f& scaleAnchor, Vector2f& scaleSize,
66         Vector2f& rectOffset, float radius) const;
67     bool RectValid(const Drawing::Rect& rect1, const Drawing::Rect& rect2) const;
68     void CaculateRect(Vector2f& rectOffset, Vector2f& scaleSize, Vector2f& scaleAnchorCoord) const;
69     void OutputOriginalImage(Drawing::Canvas& canvas, const std::shared_ptr<Drawing::Image>& image,
70         const Drawing::Rect& src, const Drawing::Rect& dst) const;
71 
72     friend class RSMarshallingHelper;
73     mutable Drawing::Rect lastRect_ = Drawing::Rect(0.f, 0.f, 0.f, 0.f);
74     mutable Drawing::Rect curRect_ = Drawing::Rect(0.f, 0.f, 0.f, 0.f);
75     std::shared_ptr<MotionBlurParam> motionBlurPara_ = nullptr;
76     bool disableMotionBlur_ = false;
77 
78     static std::shared_ptr<Drawing::RuntimeEffect> motionBlurShaderEffect_;
79 };
80 } // namespace Rosen
81 } // namespace OHOS
82 
83 #endif // RENDER_SERVICE_CLIENT_CORE_RENDER_RS_MOTION_BLUR_FILTER_H