1 /*
2  * Copyright (c) 2021-2022 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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_DRAW_DELEGATE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_DRAW_DELEGATE_H
18 
19 #include "base/geometry/rect.h"
20 #include "core/pipeline/layers/layer.h"
21 
22 namespace OHOS::Rosen {
23 class RSNode;
24 }
25 
26 namespace OHOS::Ace {
27 
28 namespace NG {
29 class RenderContext;
30 }
31 
32 class DrawDelegate {
33 public:
34     using DoDrawFrame = std::function<void(RefPtr<Flutter::Layer>&, const Rect&)>;
35     using DoDrawRSFrame = std::function<void(std::shared_ptr<Rosen::RSNode>&, const Rect&)>;
36     using DoDrawRSFrameByRenderContext = std::function<void(RefPtr<NG::RenderContext>&)>;
37     using DoDrawLastFrame = std::function<void(const Rect&)>;
38 
39     DrawDelegate() = default;
40     ~DrawDelegate() = default;
41 
DrawFrame(RefPtr<Flutter::Layer> & rootLayer,const Rect & dirty)42     void DrawFrame(RefPtr<Flutter::Layer>& rootLayer, const Rect& dirty)
43     {
44         if (doDrawFrameCallback_) {
45             doDrawFrameCallback_(rootLayer, dirty);
46         }
47     }
48 
DrawRSFrame(std::shared_ptr<Rosen::RSNode> & node,const Rect & dirty)49     void DrawRSFrame(std::shared_ptr<Rosen::RSNode>& node, const Rect& dirty)
50     {
51         if (doDrawRSFrameCallback_) {
52             doDrawRSFrameCallback_(node, dirty);
53         }
54     }
55 
DrawRSFrame(RefPtr<NG::RenderContext> & renderContext)56     void DrawRSFrame(RefPtr<NG::RenderContext>& renderContext)
57     {
58         if (doDrawRSFrameByRenderContextCallback_) {
59             doDrawRSFrameByRenderContextCallback_(renderContext);
60         }
61     }
62 
DrawLastFrame(const Rect & dirty)63     void DrawLastFrame(const Rect& dirty)
64     {
65         if (doDrawLastFrameCallback_) {
66             doDrawLastFrameCallback_(dirty);
67         }
68     }
69 
SetDrawFrameCallback(DoDrawFrame && doFrameCallback)70     void SetDrawFrameCallback(DoDrawFrame&& doFrameCallback)
71     {
72         doDrawFrameCallback_ = doFrameCallback;
73     }
74 
SetDrawRSFrameCallback(DoDrawRSFrame && doRSFrameCallback)75     void SetDrawRSFrameCallback(DoDrawRSFrame&& doRSFrameCallback)
76     {
77         doDrawRSFrameCallback_ = doRSFrameCallback;
78     }
79 
SetDrawRSFrameByRenderContextCallback(DoDrawRSFrameByRenderContext && doRSFrameByRenderContextCallback)80     void SetDrawRSFrameByRenderContextCallback(DoDrawRSFrameByRenderContext&& doRSFrameByRenderContextCallback)
81     {
82         doDrawRSFrameByRenderContextCallback_ = doRSFrameByRenderContextCallback;
83     }
84 
SetDrawFrameRepeatCallback(DoDrawLastFrame && doFrameCallback)85     void SetDrawFrameRepeatCallback(DoDrawLastFrame&& doFrameCallback)
86     {
87         doDrawLastFrameCallback_ = doFrameCallback;
88     }
89 
90 private:
91     DoDrawFrame doDrawFrameCallback_;
92     DoDrawRSFrame doDrawRSFrameCallback_;
93     DoDrawRSFrameByRenderContext doDrawRSFrameByRenderContextCallback_;
94     DoDrawLastFrame doDrawLastFrameCallback_;
95 };
96 
97 } // namespace OHOS::Ace
98 
99 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_DRAW_DELEGATE_H
100