• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 CMD_LIST_H
17 #define CMD_LIST_H
18 
19 #include <map>
20 #include <optional>
21 #include <vector>
22 
23 #include "draw/canvas.h"
24 #include "recording/op_item.h"
25 #include "recording/mem_allocator.h"
26 #include "recording/recording_handle.h"
27 #include "utils/drawing_macros.h"
28 #include "utils/extend_object.h"
29 #include "utils/object.h"
30 #ifdef ROSEN_OHOS
31 #include "surface_buffer.h"
32 #include "sync_fence.h"
33 #endif
34 
35 namespace OHOS {
36 namespace Rosen {
37 namespace Drawing {
38 using CmdListData = std::pair<const void*, size_t>;
39 using NodeId = uint64_t;
40 constexpr size_t MAX_OPITEMSIZE = 170000;
41 constexpr int32_t RECORD_CMD_MAX_DEPTH = 800;
42 constexpr size_t RECORD_CMD_MAX_SIZE = 100000;
43 
44 class DRAWING_API ExtendImageObject {
45 public:
46     virtual ~ExtendImageObject() = default;
47     virtual void Playback(Canvas& canvas, const Rect& rect,
48         const SamplingOptions& sampling, bool isBackground = false) = 0;
SetNodeId(NodeId id)49     virtual void SetNodeId(NodeId id) {};
SetPaint(Paint paint)50     virtual void SetPaint(Paint paint) {};
Purge()51     virtual void Purge() {};
IsValid()52     virtual bool IsValid() {return false;};
Dump(std::string & dump)53     virtual void Dump(std::string& dump) {};
GetNodeId()54     virtual NodeId GetNodeId() const
55     {
56         return 0;
57     }
58 };
59 
60 class DRAWING_API ExtendImageBaseObj {
61 public:
62     virtual ~ExtendImageBaseObj() = default;
63     virtual void Playback(Canvas& canvas, const Rect& rect, const SamplingOptions& sampling,
64         SrcRectConstraint constraint = SrcRectConstraint::STRICT_SRC_RECT_CONSTRAINT) = 0;
SetNodeId(NodeId id)65     virtual void SetNodeId(NodeId id) {};
Purge()66     virtual void Purge() {};
67 };
68 
69 class DRAWING_API ExtendImageNineObject {
70 public:
71     virtual ~ExtendImageNineObject() = default;
72     virtual void Playback(Canvas& canvas, const RectI& center, const Rect& dst,
73         FilterMode filterMode = FilterMode::NEAREST) = 0;
SetNodeId(NodeId id)74     virtual void SetNodeId(NodeId id) {};
Purge()75     virtual void Purge() {};
76 };
77 
78 class DRAWING_API ExtendImageLatticeObject {
79 public:
80     virtual ~ExtendImageLatticeObject() = default;
81     virtual void Playback(Canvas& canvas, const Lattice& lattice, const Rect& dst,
82         FilterMode filterMode = FilterMode::NEAREST) = 0;
SetNodeId(NodeId id)83     virtual void SetNodeId(NodeId id) {};
Purge()84     virtual void Purge() {};
85 };
86 
87 class DRAWING_API ExtendDrawFuncObj {
88 public:
89     virtual ~ExtendDrawFuncObj () = default;
90     virtual void Playback(Canvas* canvas, const Rect* rect) = 0;
91 };
92 
93 #ifdef ROSEN_OHOS
94 struct SurfaceBufferEntry {
95     SurfaceBufferEntry() = default;
SurfaceBufferEntrySurfaceBufferEntry96     SurfaceBufferEntry(const sptr<SurfaceBuffer> surfaceBuffer, const sptr<SyncFence> acquireFence)
97         : surfaceBuffer_(surfaceBuffer), acquireFence_(acquireFence)
98     {}
99     sptr<SurfaceBuffer> surfaceBuffer_ = nullptr;
100     sptr<SyncFence> acquireFence_ = nullptr;
101 };
102 #endif
103 
104 class DRAWING_API CmdList {
105 public:
106     enum Type : uint32_t {
107         CMD_LIST = 0,
108         DRAW_CMD_LIST,
109         MASK_CMD_LIST,
110     };
111 
112     CmdList() = default;
113     explicit CmdList(const CmdListData& cmdListData);
114     virtual ~CmdList();
115 
GetType()116     virtual uint32_t GetType() const
117     {
118         return Type::CMD_LIST;
119     }
120 
121     /**
122      * @brief       Add OpItem to CmdList.
123      * @param T     The name of OpItem class.
124      * @param Args  Constructs arguments to the OpItem.
125      */
126     template<typename T, typename... Args>
AddOp(Args &&...args)127     void AddOp(Args&&... args)
128     {
129         std::lock_guard<std::recursive_mutex> lock(mutex_);
130         T* op = opAllocator_.Allocate<T>(std::forward<Args>(args)...);
131         if (op == nullptr) {
132             return;
133         }
134 
135         size_t offset = opAllocator_.AddrToOffset(op);
136         if (lastOpItemOffset_.has_value()) {
137 #ifdef CROSS_PLATFORM
138             auto* lastOpItem = static_cast<OpItem*>(
139                 opAllocator_.OffsetToAddr(lastOpItemOffset_.__get(), sizeof(OpItem)));
140 #else
141             auto* lastOpItem = static_cast<OpItem*>(
142                 opAllocator_.OffsetToAddr(lastOpItemOffset_.value(), sizeof(OpItem)));
143 #endif
144             if (lastOpItem != nullptr) {
145                 lastOpItem->SetNextOpItemOffset(offset);
146             }
147         }
148         lastOpItemOffset_.emplace(offset);
149         opCnt_++;
150     }
151 
152     /**
153      * @brief       Add a contiguous buffers to the CmdList.
154      * @param data  A contiguous buffers.
155      * @return      Returns the offset of the contiguous buffers and CmdList head point.
156      */
157     size_t AddCmdListData(const CmdListData& data);
158 
159     const void* GetCmdListData(size_t offset, size_t size) const;
160 
161     /**
162      * @brief   Gets the contiguous buffers of CmdList.
163      */
164     CmdListData GetData() const;
165 
166     // using for recording, should to remove after using shared memory
167     bool SetUpImageData(const void* data, size_t size);
168     size_t AddImageData(const void* data, size_t size);
169     const void* GetImageData(size_t offset, size_t size) const;
170     CmdListData GetAllImageData() const;
171 
172     OpDataHandle AddImage(const Image& image);
173     std::shared_ptr<Image> GetImage(const OpDataHandle& imageHandle);
174 
175     size_t AddBitmapData(const void* data, size_t size);
176     const void* GetBitmapData(size_t offset, size_t size) const;
177     bool SetUpBitmapData(const void* data, size_t size);
178     CmdListData GetAllBitmapData() const;
179 
180     /*
181      * @brief  return ExtendObject index. UINT32_MAX is error.
182      */
183     uint32_t AddExtendObject(const std::shared_ptr<ExtendObject>& object);
184 
185     /*
186      * @brief  get ExtendObject by index.
187      */
188     std::shared_ptr<ExtendObject> GetExtendObject(uint32_t index);
189 
190     /*
191      * @brief  return ExtendObject size, 0 is no ExtendObject.
192      */
193     uint32_t GetAllExtendObject(std::vector<std::shared_ptr<ExtendObject>>& objectList);
194 
195     /*
196      * @brief  return real setup ExtendObject size.
197      */
198     uint32_t SetupExtendObject(const std::vector<std::shared_ptr<ExtendObject>>& objectList);
199 
200     /*
201      * @brief  return DrawingObject index. UINT32_MAX is error.
202      */
203     uint32_t AddDrawingObject(const std::shared_ptr<Object>& object);
204 
205     /*
206      * @brief  get DrawingObject by index.
207      */
208     std::shared_ptr<Object> GetDrawingObject(uint32_t index);
209 
210     /*
211      * @brief  return DrawingObject size, 0 is no DrawingObject.
212      */
213     uint32_t GetAllDrawingObject(std::vector<std::shared_ptr<Object>>& objectList);
214 
215     /*
216      * @brief  return real setup DrawingObject size.
217      */
218     uint32_t SetupDrawingObject(const std::vector<std::shared_ptr<Object>>& objectList);
219 
220     /*
221      * @brief  return RecordCmd index. UINT32_MAX is error.
222      */
223     uint32_t AddRecordCmd(const std::shared_ptr<RecordCmd>& recordCmd);
224 
225     /*
226      * @brief  get RecordCmd by index.
227      */
228     std::shared_ptr<RecordCmd> GetRecordCmd(uint32_t index);
229 
230     /*
231      * @brief  return real setup RecordCmd size.
232      */
233     uint32_t SetupRecordCmd(std::vector<std::shared_ptr<RecordCmd>>& recordCmdList);
234 
235     /*
236      * @brief  return RecordCmd size, 0 is no RecordCmd.
237      */
238     uint32_t GetAllRecordCmd(std::vector<std::shared_ptr<RecordCmd>>& recordCmdList);
239 
240     /*
241      * @brief  return imageObject index, negative is error.
242      */
243     uint32_t AddImageObject(const std::shared_ptr<ExtendImageObject>& object);
244 
245     /*
246      * @brief  get imageObject by index.
247      */
248     std::shared_ptr<ExtendImageObject> GetImageObject(uint32_t id);
249 
250     /*
251      * @brief  return imageObject size, 0 is no imageObject.
252      */
253     uint32_t GetAllObject(std::vector<std::shared_ptr<ExtendImageObject>>& objectList);
254 
255     /*
256      * @brief  return real setup imageObject size.
257      */
258     uint32_t SetupObject(const std::vector<std::shared_ptr<ExtendImageObject>>& objectList);
259 
260      /*
261      * @brief  return imageBaseObj index, negative is error.
262      */
263     uint32_t AddImageBaseObj(const std::shared_ptr<ExtendImageBaseObj>& object);
264 
265     /*
266      * @brief  get imageBaseObj by index.
267      */
268     std::shared_ptr<ExtendImageBaseObj> GetImageBaseObj(uint32_t id);
269 
270     /*
271      * @brief  return imageBaseObj size, 0 is no imageBaseObj.
272      */
273     uint32_t GetAllBaseObj(std::vector<std::shared_ptr<ExtendImageBaseObj>>& objectList);
274 
275     /*
276      * @brief  return real setup imageBaseObj size.
277      */
278     uint32_t SetupBaseObj(const std::vector<std::shared_ptr<ExtendImageBaseObj>>& objectList);
279 
280      /*
281      * @brief  return imageNineObject index, negative is error.
282      */
283     uint32_t AddImageNineObject(const std::shared_ptr<ExtendImageNineObject>& object);
284 
285     /*
286      * @brief  get imageNineObject by index.
287      */
288     std::shared_ptr<ExtendImageNineObject> GetImageNineObject(uint32_t id);
289 
290     /*
291      * @brief  return imageNineObject size, 0 is no imageNineObject.
292      */
293     uint32_t GetAllImageNineObject(std::vector<std::shared_ptr<ExtendImageNineObject>>& objectList);
294 
295     /*
296      * @brief  return real setup imageNineObject size.
297      */
298     uint32_t SetupImageNineObject(const std::vector<std::shared_ptr<ExtendImageNineObject>>& objectList);
299 
300      /*
301      * @brief  return imageLatticeObject index, negative is error.
302      */
303     uint32_t AddImageLatticeObject(const std::shared_ptr<ExtendImageLatticeObject>& object);
304 
305     /*
306      * @brief  get imageLatticeObject by index.
307      */
308     std::shared_ptr<ExtendImageLatticeObject> GetImageLatticeObject(uint32_t id);
309 
310     /*
311      * @brief  return imageLatticeObject size, 0 is no imageLatticeObject.
312      */
313     uint32_t GetAllImageLatticeObject(std::vector<std::shared_ptr<ExtendImageLatticeObject>>& objectList);
314 
315     /*
316      * @brief  return real setup imageLatticeObject size.
317      */
318     uint32_t SetupImageLatticeObject(const std::vector<std::shared_ptr<ExtendImageLatticeObject>>& objectList);
319 
320      /*
321      * @brief  return DrawFuncObj index, negative is error.
322      */
323     uint32_t AddDrawFuncOjb(const std::shared_ptr<ExtendDrawFuncObj>& object);
324 
325     /*
326      * @brief  get DrawFuncObj by index.
327      */
328     std::shared_ptr<ExtendDrawFuncObj> GetDrawFuncObj(uint32_t id);
329 
330     /*
331      * @brief  copy object vec to another CmdList.
332      */
333     void CopyObjectTo(CmdList& other) const;
334 
335     /*
336      * @brief  return recording op count.
337      */
338     uint32_t GetOpCnt() const;
339 
340     CmdList(CmdList&&) = delete;
341     CmdList(const CmdList&) = delete;
342     CmdList& operator=(CmdList&&) = delete;
343     CmdList& operator=(const CmdList&) = delete;
344 
345 #ifdef ROSEN_OHOS
346     /*
347      * @brief  return surfaceBufferEntry index, negative is error.
348      */
349     uint32_t AddSurfaceBufferEntry(const std::shared_ptr<SurfaceBufferEntry>& surfaceBufferEntry);
350 
351     /*
352      * @brief  get surfaceBufferEntry by index.
353      */
354     std::shared_ptr<SurfaceBufferEntry> GetSurfaceBufferEntry(uint32_t id);
355 
356     /*
357      * @brief  return surfaceBufferEntry size, 0 is no surfaceBuffer.
358      */
359     uint32_t GetAllSurfaceBufferEntry(std::vector<std::shared_ptr<SurfaceBufferEntry>>& objectList);
360 
361     /*
362      * @brief  return real setup surfaceBufferEntry size.
363      */
364     uint32_t SetupSurfaceBufferEntry(const std::vector<std::shared_ptr<SurfaceBufferEntry>>& objectList);
365 #endif
366 
367 protected:
368     void ProfilerPushObjects(std::stringstream& stream, size_t size);
369     void ProfilerPopObjects(std::stringstream& stream, size_t size);
370 
371     MemAllocator opAllocator_;
372     MemAllocator imageAllocator_;
373     MemAllocator bitmapAllocator_;
374     std::optional<size_t> lastOpItemOffset_ = std::nullopt;
375     std::recursive_mutex mutex_;
376     std::map<size_t, std::shared_ptr<Image>> imageMap_;
377     std::vector<std::pair<size_t, OpDataHandle>> imageHandleVec_;
378     uint32_t opCnt_ = 0;
379 
380     std::vector<std::shared_ptr<RecordCmd>> recordCmdVec_;
381     std::mutex recordCmdMutex_;
382     std::vector<std::shared_ptr<ExtendImageObject>> imageObjectVec_;
383     std::mutex imageObjectMutex_;
384     std::vector<std::shared_ptr<ExtendImageBaseObj>> imageBaseObjVec_;
385     std::mutex imageBaseObjMutex_;
386     std::vector<std::shared_ptr<ExtendImageNineObject>> imageNineObjectVec_;
387     std::mutex imageNineObjectMutex_;
388     std::vector<std::shared_ptr<ExtendImageLatticeObject>> imageLatticeObjectVec_;
389     std::mutex imageLatticeObjectMutex_;
390     std::vector<std::shared_ptr<ExtendObject>> extendObjectVec_;
391     std::mutex extendObjectMutex_;
392     std::vector<std::shared_ptr<Object>> drawingObjectVec_;
393     std::mutex drawingObjectMutex_;
394 #ifdef ROSEN_OHOS
395     std::vector<std::shared_ptr<SurfaceBufferEntry>> surfaceBufferEntryVec_;
396     std::mutex surfaceBufferEntryMutex_;
397 #endif
398     std::vector<std::shared_ptr<ExtendDrawFuncObj>> drawFuncObjVec_;
399     std::mutex drawFuncObjMutex_;
400 };
401 } // namespace Drawing
402 } // namespace Rosen
403 } // namespace OHOS
404 #endif