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_BASE_DRAWABLE_RS_MISC_DRAWABLE_H
17 #define RENDER_SERVICE_BASE_DRAWABLE_RS_MISC_DRAWABLE_H
18 
19 #include <bitset>
20 #include <cstdint>
21 #include <functional>
22 #include <memory>
23 #include <set>
24 #include <unordered_set>
25 
26 #include "drawable/rs_drawable.h"
27 #include "modifier/rs_modifier_type.h"
28 #include "pipeline/rs_paint_filter_canvas.h"
29 #include "property/rs_properties_def.h"
30 
31 namespace OHOS::Rosen {
32 enum class RSModifierType : int16_t;
33 namespace Drawing {
34 class DrawCmdList;
35 }
36 
37 namespace DrawableV2 {
38 class RSRenderNodeDrawableAdapter;
39 // RSChildrenDrawable, for drawing children of RSRenderNode, updates on child add/remove
40 class RSChildrenDrawable : public RSDrawable {
41 public:
42     RSChildrenDrawable() = default;
43     ~RSChildrenDrawable() override = default;
44 
45     static RSDrawable::Ptr OnGenerate(const RSRenderNode& node);
46     bool OnUpdate(const RSRenderNode& content) override;
47     void OnSync() override;
48     Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override;
49 
50 private:
51     bool needSync_ = false;
52     std::vector<std::shared_ptr<RSRenderNodeDrawableAdapter>> childrenDrawableVec_;
53     std::vector<std::shared_ptr<RSRenderNodeDrawableAdapter>> stagingChildrenDrawableVec_;
54 
55     // Shared Transition related
56     bool childrenHasSharedTransition_ = false;
57     bool OnSharedTransition(const std::shared_ptr<RSRenderNode>& node);
58     friend class RSRenderNode;
59     friend class RSRenderNodeDrawableAdapter;
60 };
61 
62 // RSCustomModifierDrawable, for drawing custom modifiers
63 class RSCustomModifierDrawable : public RSDrawable {
64 public:
RSCustomModifierDrawable(RSModifierType type)65     RSCustomModifierDrawable(RSModifierType type) : type_(type) {}
66     static RSDrawable::Ptr OnGenerate(const RSRenderNode& content, RSModifierType type);
67     bool OnUpdate(const RSRenderNode& node) override;
68     void OnSync() override;
69     void OnPurge() override;
70     Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override;
71 
72 private:
73     RSModifierType type_;
74     bool needClearOp_ = false;
75     bool needSync_ = false;
76     Gravity gravity_ = Gravity::DEFAULT;
77     Gravity stagingGravity_ = Gravity::DEFAULT;
78     bool isCanvasNode_ = false;
79     bool stagingIsCanvasNode_ = false;
80     std::vector<std::shared_ptr<Drawing::DrawCmdList>> drawCmdListVec_;
81     std::vector<std::shared_ptr<Drawing::DrawCmdList>> stagingDrawCmdListVec_;
82 };
83 
84 // ============================================================================
85 // Save and Restore
86 class RSSaveDrawable : public RSDrawable {
87 public:
88     explicit RSSaveDrawable(std::shared_ptr<uint32_t> content);
89     ~RSSaveDrawable() override = default;
90 
91     // no need to sync, content_ only used in render thread
OnSync()92     void OnSync() override {};
93     Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override;
94 
95 private:
96     std::shared_ptr<uint32_t> content_;
97 };
98 
99 class RSRestoreDrawable : public RSDrawable {
100 public:
101     explicit RSRestoreDrawable(std::shared_ptr<uint32_t> content);
102     ~RSRestoreDrawable() override = default;
103 
104     // no need to sync, content_ only used in render thread
OnSync()105     void OnSync() override {};
106     Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override;
107 
108 private:
109     std::shared_ptr<uint32_t> content_;
110 };
111 
112 class RSCustomSaveDrawable : public RSDrawable {
113 public:
114     explicit RSCustomSaveDrawable(
115         std::shared_ptr<RSPaintFilterCanvas::SaveStatus> content, RSPaintFilterCanvas::SaveType type);
116     ~RSCustomSaveDrawable() override = default;
117 
118     // no need to sync, content_ only used in render thread
OnSync()119     void OnSync() override {};
120     Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override;
121 
122 private:
123     std::shared_ptr<RSPaintFilterCanvas::SaveStatus> content_;
124     RSPaintFilterCanvas::SaveType type_;
125 };
126 
127 class RSCustomRestoreDrawable : public RSDrawable {
128 public:
129     explicit RSCustomRestoreDrawable(std::shared_ptr<RSPaintFilterCanvas::SaveStatus> content);
130     ~RSCustomRestoreDrawable() override = default;
131 
132     // no need to sync, content_ only used in render thread
OnSync()133     void OnSync() override {};
134     Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override;
135 
136 private:
137     std::shared_ptr<RSPaintFilterCanvas::SaveStatus> content_;
138 };
139 
140 // ============================================================================
141 // EnvFGColor
142 class RSEnvFGColorDrawable : public RSDrawable {
143 public:
144     explicit RSEnvFGColorDrawable() = default;
145     ~RSEnvFGColorDrawable() override = default;
146 
147     static RSDrawable::Ptr OnGenerate(const RSRenderNode& node);
148     bool OnUpdate(const RSRenderNode& node) override;
149     void OnSync() override;
150 
151     Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override;
152 
153 protected:
154     bool needSync_ = false;
155     Color envFGColor_;
156     Color stagingEnvFGColor_;
157 };
158 
159 // ============================================================================
160 // EnvFGColorStrategy
161 class RSEnvFGColorStrategyDrawable : public RSDrawable {
162 public:
163     explicit RSEnvFGColorStrategyDrawable() = default;
164     ~RSEnvFGColorStrategyDrawable() override = default;
165 
166     static RSDrawable::Ptr OnGenerate(const RSRenderNode& node);
167     bool OnUpdate(const RSRenderNode& node) override;
168     void OnSync() override;
169 
170     Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override;
171 
172 protected:
173     bool needSync_ = false;
174     ForegroundColorStrategyType envFGColorStrategy_;
175     bool needClipToBounds_;
176     Color backgroundColor_;
177     Vector4f boundsRect_;
178 
179     ForegroundColorStrategyType stagingEnvFGColorStrategy_;
180     bool stagingNeedClipToBounds_;
181     Color stagingBackgroundColor_;
182     Vector4f stagingBoundsRect_;
183 };
184 
185 class RSCustomClipToFrameDrawable : public RSDrawable {
186 public:
187     explicit RSCustomClipToFrameDrawable() = default;
188     ~RSCustomClipToFrameDrawable() override = default;
189 
190     static RSDrawable::Ptr OnGenerate(const RSRenderNode& node);
191     bool OnUpdate(const RSRenderNode& node) override;
192     void OnSync() override;
193 
194     Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override;
195 
196 protected:
197     bool needSync_ = false;
198     Drawing::Rect stagingCustomClipRect_;
199     Drawing::Rect customClipRect_;
200 };
201 
202 // ============================================================================
203 // Blender & BlendMode
204 class RSBeginBlenderDrawable : public RSDrawable {
205 public:
206     RSBeginBlenderDrawable() = default;
207     ~RSBeginBlenderDrawable() override = default;
208 
209     static RSDrawable::Ptr OnGenerate(const RSRenderNode& node);
210     bool OnUpdate(const RSRenderNode& node) override;
211     void OnSync() override;
212     Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override;
213 
214 private:
215     bool needSync_ = false;
216     std::shared_ptr<Drawing::Blender> blender_ = nullptr;
217     int blendApplyType_ = 0;
218     bool isDangerous_ = false;
219     std::string propertyDescription_;
220 
221     std::shared_ptr<Drawing::Blender> stagingBlender_ = nullptr;
222     int stagingBlendApplyType_ = 0;
223     bool stagingIsDangerous_ = false;
224     std::string stagingPropertyDescription_;
225 };
226 
227 class RSEndBlenderDrawable : public RSDrawable {
228 public:
229     RSEndBlenderDrawable() = default;
230     ~RSEndBlenderDrawable() override = default;
231 
232     static RSDrawable::Ptr OnGenerate(const RSRenderNode& node);
233     bool OnUpdate(const RSRenderNode& node) override;
234     void OnSync() override;
235     Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override;
236 
237 private:
238     bool needSync_ = false;
239     int blendApplyType_ = 0;
240     int stagingBlendApplyType_ = 0;
241 };
242 } // namespace DrawableV2
243 } // namespace OHOS::Rosen
244 #endif // RENDER_SERVICE_BASE_DRAWABLE_RS_MISC_DRAWABLE_H
245