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