• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 PATH_CMD_LIST_H
17 #define PATH_CMD_LIST_H
18 
19 #include <unordered_map>
20 
21 #include "draw/path.h"
22 #include "recording/cmd_list.h"
23 
24 namespace OHOS {
25 namespace Rosen {
26 namespace Drawing {
27 class PathCmdList : public CmdList {
28 public:
29     PathCmdList() = default;
30     ~PathCmdList() override = default;
31 
GetType()32     uint32_t GetType() const override
33     {
34         return Type::PATH_CMD_LIST;
35     }
36 
37     /*
38      * @brief       Creates a PathCmdList with contiguous buffers.
39      * @param data  A contiguous buffers.
40      */
41     static std::shared_ptr<PathCmdList> CreateFromData(const CmdListData& data, bool isCopy = false);
42 
43     /*
44      * @brief  Calls the corresponding operations of all opitems in PathCmdList to the path.
45      */
46     std::shared_ptr<Path> Playback() const;
47 };
48 
49 /* OpItem */
50 /*
51  * @brief  Helper class for path playback.
52  *         Contains the playback context and a static mapping table: { OpItemType, OpItemPlaybackFunc }.
53  */
54 class PathPlayer {
55 public:
56     PathPlayer(Path& path, const CmdList& cmdList);
57     ~PathPlayer() = default;
58 
59     /*
60      * @brief  Obtain the corresponding func according to the type lookup mapping table
61      *         and then invoke the func to plays opItem back to path which in context.
62      */
63     bool Playback(uint32_t type, const void* opItem);
64 
65     Path& path_;
66     const CmdList& cmdList_;
67 
68     using PathPlaybackFunc = void(*)(PathPlayer& palyer, const void* opItem);
69 private:
70     static std::unordered_map<uint32_t, PathPlaybackFunc> opPlaybackFuncLUT_;
71 };
72 
73 class PathOpItem : public OpItem {
74 public:
PathOpItem(uint32_t type)75     PathOpItem(uint32_t type) : OpItem(type) {}
76     ~PathOpItem() = default;
77 
78     enum Type : uint32_t {
79         OPITEM_HEAD,
80         BUILDFROMSVG_OPITEM,
81         MOVETO_OPITEM,
82         LINETO_OPITEM,
83         ARCTO_OPITEM,
84         CUBICTO_OPITEM,
85         QUADTO_OPITEM,
86         ADDRECT_OPITEM,
87         ADDOVAL_OPITEM,
88         ADDARC_OPITEM,
89         ADDPOLY_OPITEM,
90         ADDCIRCLE_OPITEM,
91         ADDRRECT_OPITEM,
92         ADDPATH_OPITEM,
93         ADDPATHWITHMATRIX_OPITEM,
94         REVERSEADDPATH_OPITEM,
95         SETFILLSTYLE_OPITEM,
96         BUILDFROMINTERPOLATE_OPITEM,
97         TRANSFORM_OPITEM,
98         OFFSET_OPITEM,
99         PATHOPWITH_OPITEM,
100         RESET_OPITEM,
101         CLOSE_OPITEM,
102     };
103 };
104 
105 class BuildFromSVGOpItem : public PathOpItem {
106 public:
107     explicit BuildFromSVGOpItem(const uint32_t offset, const size_t size);
108     ~BuildFromSVGOpItem() = default;
109 
110     /*
111      * @brief         Plays the opItem back into path which hold by Player.
112      * @param opItem  opItem static_cast to this class.
113      */
114     static void Playback(PathPlayer& player, const void* opItem);
115 
116     /*
117      * @brief  Plays OpItem back into path.
118      */
119     void Playback(Path& path, const CmdList& cmdList) const;
120 private:
121     uint32_t offset_;
122     size_t size_;
123 };
124 
125 class MoveToOpItem : public PathOpItem {
126 public:
127     MoveToOpItem(const scalar x, const scalar y);
128     ~MoveToOpItem() = default;
129     static void Playback(PathPlayer& player, const void* opItem);
130     void Playback(Path& path) const;
131 private:
132     scalar x_;
133     scalar y_;
134 };
135 
136 class LineToOpItem : public PathOpItem {
137 public:
138     LineToOpItem(const scalar x, const scalar y);
139     ~LineToOpItem() = default;
140     static void Playback(PathPlayer& player, const void* opItem);
141     void Playback(Path& path) const;
142 private:
143     scalar x_;
144     scalar y_;
145 };
146 
147 class ArcToOpItem : public PathOpItem {
148 public:
149     ArcToOpItem(const Point& pt1, const Point& pt2, const scalar startAngle, const scalar sweepAngle);
150     ArcToOpItem(const scalar rx, const scalar ry, const scalar angle, const PathDirection direction, const scalar endX,
151                 const scalar endY);
152     ~ArcToOpItem() = default;
153     static void Playback(PathPlayer& player, const void* opItem);
154     void Playback(Path& path) const;
155 private:
156     Point pt1_;
157     Point pt2_;
158     scalar startAngle_;
159     scalar sweepAngle_;
160     PathDirection direction_;
161     const int32_t methodIndex_;
162 };
163 
164 class CubicToOpItem : public PathOpItem {
165 public:
166     CubicToOpItem(const Point& ctrlPt1, const Point& ctrlPt2, const Point& endPt);
167     ~CubicToOpItem() = default;
168     static void Playback(PathPlayer& player, const void* opItem);
169     void Playback(Path& path) const;
170 private:
171     Point ctrlPt1_;
172     Point ctrlPt2_;
173     Point endPt_;
174 };
175 
176 class QuadToOpItem : public PathOpItem {
177 public:
178     QuadToOpItem(const Point& ctrlPt, const Point& endPt);
179     ~QuadToOpItem() = default;
180     static void Playback(PathPlayer& player, const void* opItem);
181     void Playback(Path& path) const;
182 private:
183     Point ctrlPt_;
184     Point endPt_;
185 };
186 
187 class AddRectOpItem : public PathOpItem {
188 public:
189     AddRectOpItem(const Rect& rect, PathDirection dir);
190     ~AddRectOpItem() = default;
191     static void Playback(PathPlayer& player, const void* opItem);
192     void Playback(Path& path) const;
193 private:
194     Rect rect_;
195     PathDirection dir_;
196 };
197 
198 class AddOvalOpItem : public PathOpItem {
199 public:
200     AddOvalOpItem(const Rect& oval, PathDirection dir);
201     ~AddOvalOpItem() = default;
202     static void Playback(PathPlayer& player, const void* opItem);
203     void Playback(Path& path) const;
204 private:
205     Rect rect_;
206     PathDirection dir_;
207 };
208 
209 class AddArcOpItem : public PathOpItem {
210 public:
211     AddArcOpItem(const Rect& oval, const scalar startAngle, const scalar sweepAngle);
212     ~AddArcOpItem() = default;
213     static void Playback(PathPlayer& player, const void* opItem);
214     void Playback(Path& path) const;
215 private:
216     Rect rect_;
217     scalar startAngle_;
218     scalar sweepAngle_;
219 };
220 
221 class AddPolyOpItem : public PathOpItem {
222 public:
223     AddPolyOpItem(const std::pair<uint32_t, size_t>& points, int32_t count, bool close);
224     ~AddPolyOpItem() = default;
225     static void Playback(PathPlayer& player, const void* opItem);
226     void Playback(Path& path, const CmdList& menAllocator) const;
227 private:
228     std::pair<uint32_t, size_t> points_;
229     int32_t count_;
230     bool close_;
231 };
232 
233 class AddCircleOpItem : public PathOpItem {
234 public:
235     AddCircleOpItem(const scalar x, const scalar y, const scalar radius, PathDirection dir);
236     ~AddCircleOpItem() = default;
237     static void Playback(PathPlayer& player, const void* opItem);
238     void Playback(Path& path) const;
239 private:
240     scalar x_;
241     scalar y_;
242     scalar radius_;
243     PathDirection dir_;
244 };
245 
246 class AddRoundRectOpItem : public PathOpItem {
247 public:
248     AddRoundRectOpItem(const std::pair<uint32_t, size_t>& radiusXYData, const Rect& rect, PathDirection dir);
249     ~AddRoundRectOpItem() = default;
250     static void Playback(PathPlayer& player, const void* opItem);
251     void Playback(Path& path, const CmdList& cmdList) const;
252 private:
253     std::pair<uint32_t, size_t> radiusXYData_;
254     Rect rect_;
255     PathDirection dir_;
256 };
257 
258 class AddPathOpItem : public PathOpItem {
259 public:
260     AddPathOpItem(const CmdListHandle& src, const scalar x, const scalar y);
261     AddPathOpItem(const CmdListHandle& src);
262     ~AddPathOpItem() = default;
263     static void Playback(PathPlayer& player, const void* opItem);
264     void Playback(Path& path, const CmdList& cmdList) const;
265 private:
266     CmdListHandle src_;
267     scalar x_;
268     scalar y_;
269     const int32_t methodIndex_;
270 };
271 
272 class AddPathWithMatrixOpItem : public PathOpItem {
273 public:
274     AddPathWithMatrixOpItem(const CmdListHandle& src, const Matrix& matrix);
275     ~AddPathWithMatrixOpItem() = default;
276     static void Playback(PathPlayer& player, const void* opItem);
277     void Playback(Path& path, const CmdList& cmdList) const;
278 private:
279     CmdListHandle src_;
280     Matrix::Buffer matrixBuffer_;
281 };
282 
283 class ReverseAddPathOpItem : public PathOpItem {
284 public:
285     explicit ReverseAddPathOpItem(const CmdListHandle& src);
286     ~ReverseAddPathOpItem() = default;
287     static void Playback(PathPlayer& player, const void* opItem);
288     void Playback(Path& path, const CmdList& cmdList) const;
289 private:
290     CmdListHandle src_;
291 };
292 
293 class SetFillStyleOpItem : public PathOpItem {
294 public:
295     explicit SetFillStyleOpItem(PathFillType fillstyle);
296     ~SetFillStyleOpItem() = default;
297     static void Playback(PathPlayer& player, const void* opItem);
298     void Playback(Path& path) const;
299 private:
300     PathFillType fillstyle_;
301 };
302 
303 class BuildFromInterpolateOpItem : public PathOpItem {
304 public:
305     BuildFromInterpolateOpItem(const CmdListHandle& src, const CmdListHandle& ending, const scalar weight);
306     ~BuildFromInterpolateOpItem() = default;
307     static void Playback(PathPlayer& player, const void* opItem);
308     void Playback(Path& path, const CmdList& cmdList) const;
309 private:
310     CmdListHandle src_;
311     CmdListHandle ending_;
312     scalar weight_;
313 };
314 
315 class TransformOpItem : public PathOpItem {
316 public:
317     explicit TransformOpItem(const Matrix& matrix);
318     ~TransformOpItem() = default;
319     static void Playback(PathPlayer& player, const void* opItem);
320     void Playback(Path& path) const;
321 private:
322     Matrix::Buffer matrixBuffer_;
323 };
324 
325 class OffsetOpItem : public PathOpItem {
326 public:
327     OffsetOpItem(const scalar dx, const scalar dy);
328     ~OffsetOpItem() = default;
329     static void Playback(PathPlayer& player, const void* opItem);
330     void Playback(Path& path) const;
331 private:
332     scalar x_;
333     scalar y_;
334 };
335 
336 class PathOpWithOpItem : public PathOpItem {
337 public:
338     PathOpWithOpItem(const CmdListHandle& path1, const CmdListHandle& path2, PathOp op);
339     ~PathOpWithOpItem() = default;
340     static void Playback(PathPlayer& player, const void* opItem);
341     void Playback(Path& path, const CmdList& cmdList) const;
342 private:
343     CmdListHandle path1_;
344     CmdListHandle path2_;
345     PathOp op_;
346 };
347 
348 class ResetOpItem : public PathOpItem {
349 public:
350     ResetOpItem();
351     ~ResetOpItem();
352     static void Playback(PathPlayer& player, const void* opItem);
353     void Playback(Path& path) const;
354 };
355 
356 class CloseOpItem : public PathOpItem {
357 public:
358     CloseOpItem();
359     ~CloseOpItem();
360     static void Playback(PathPlayer& player, const void* opItem);
361     void Playback(Path& path) const;
362 };
363 } // namespace Drawing
364 } // namespace Rosen
365 } // namespace OHOS
366 #endif
367