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 = 170000;
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 ExtendDrawFuncObj {
61 public:
62     virtual ~ExtendDrawFuncObj () = default;
63     virtual void Playback(Canvas* canvas, const Rect* rect) = 0;
64 };
65 
66 #ifdef ROSEN_OHOS
67 struct SurfaceBufferEntry {
68     SurfaceBufferEntry() = default;
SurfaceBufferEntrySurfaceBufferEntry69     SurfaceBufferEntry(const sptr<SurfaceBuffer> surfaceBuffer, const sptr<SyncFence> acquireFence)
70         : surfaceBuffer_(surfaceBuffer), acquireFence_(acquireFence)
71     {}
72     sptr<SurfaceBuffer> surfaceBuffer_ = nullptr;
73     sptr<SyncFence> acquireFence_ = nullptr;
74 };
75 #endif
76 
77 class DRAWING_API CmdList {
78 public:
79     enum Type : uint32_t {
80         CMD_LIST = 0,
81         DRAW_CMD_LIST,
82         MASK_CMD_LIST,
83     };
84 
85     CmdList() = default;
86     explicit CmdList(const CmdListData& cmdListData);
87     virtual ~CmdList();
88 
GetType()89     virtual uint32_t GetType() const
90     {
91         return Type::CMD_LIST;
92     }
93 
94     /**
95      * @brief       Add OpItem to CmdList.
96      * @param T     The name of OpItem class.
97      * @param Args  Constructs arguments to the OpItem.
98      */
99     template<typename T, typename... Args>
AddOp(Args &&...args)100     void AddOp(Args&&... args)
101     {
102         std::lock_guard<std::recursive_mutex> lock(mutex_);
103         T* op = opAllocator_.Allocate<T>(std::forward<Args>(args)...);
104         if (op == nullptr) {
105             return;
106         }
107 
108         size_t offset = opAllocator_.AddrToOffset(op);
109         if (lastOpItemOffset_.has_value()) {
110 #ifdef CROSS_PLATFORM
111             auto* lastOpItem = static_cast<OpItem*>(
112                 opAllocator_.OffsetToAddr(lastOpItemOffset_.__get(), sizeof(OpItem)));
113 #else
114             auto* lastOpItem = static_cast<OpItem*>(
115                 opAllocator_.OffsetToAddr(lastOpItemOffset_.value(), sizeof(OpItem)));
116 #endif
117             if (lastOpItem != nullptr) {
118                 lastOpItem->SetNextOpItemOffset(offset);
119             }
120         }
121         lastOpItemOffset_.emplace(offset);
122         opCnt_++;
123     }
124 
125     /**
126      * @brief       Add a contiguous buffers to the CmdList.
127      * @param data  A contiguous buffers.
128      * @return      Returns the offset of the contiguous buffers and CmdList head point.
129      */
130     size_t AddCmdListData(const CmdListData& data);
131 
132     const void* GetCmdListData(size_t offset, size_t size) const;
133 
134     /**
135      * @brief   Gets the contiguous buffers of CmdList.
136      */
137     CmdListData GetData() const;
138 
139     // using for recording, should to remove after using shared memory
140     bool SetUpImageData(const void* data, size_t size);
141     size_t AddImageData(const void* data, size_t size);
142     const void* GetImageData(size_t offset, size_t size) const;
143     CmdListData GetAllImageData() const;
144 
145     OpDataHandle AddImage(const Image& image);
146     std::shared_ptr<Image> GetImage(const OpDataHandle& imageHandle);
147 
148     size_t AddBitmapData(const void* data, size_t size);
149     const void* GetBitmapData(size_t offset, size_t size) const;
150     bool SetUpBitmapData(const void* data, size_t size);
151     CmdListData GetAllBitmapData() const;
152 
153     /*
154      * @brief  return ExtendObject index. UINT32_MAX is error.
155      */
156     uint32_t AddExtendObject(const std::shared_ptr<ExtendObject>& object);
157 
158     /*
159      * @brief  get ExtendObject by index.
160      */
161     std::shared_ptr<ExtendObject> GetExtendObject(uint32_t index);
162 
163     /*
164      * @brief  return ExtendObject size, 0 is no ExtendObject.
165      */
166     uint32_t GetAllExtendObject(std::vector<std::shared_ptr<ExtendObject>>& objectList);
167 
168     /*
169      * @brief  return real setup ExtendObject size.
170      */
171     uint32_t SetupExtendObject(const std::vector<std::shared_ptr<ExtendObject>>& objectList);
172 
173     /*
174      * @brief  return RecordCmd index. UINT32_MAX is error.
175      */
176     uint32_t AddRecordCmd(const std::shared_ptr<RecordCmd>& recordCmd);
177 
178     /*
179      * @brief  get RecordCmd by index.
180      */
181     std::shared_ptr<RecordCmd> GetRecordCmd(uint32_t index);
182 
183     /*
184      * @brief  return real setup RecordCmd size.
185      */
186     uint32_t SetupRecordCmd(std::vector<std::shared_ptr<RecordCmd>>& recordCmdList);
187 
188     /*
189      * @brief  return RecordCmd size, 0 is no RecordCmd.
190      */
191     uint32_t GetAllRecordCmd(std::vector<std::shared_ptr<RecordCmd>>& recordCmdList);
192 
193     /*
194      * @brief  return imageObject index, negative is error.
195      */
196     uint32_t AddImageObject(const std::shared_ptr<ExtendImageObject>& object);
197 
198     /*
199      * @brief  get imageObject by index.
200      */
201     std::shared_ptr<ExtendImageObject> GetImageObject(uint32_t id);
202 
203     /*
204      * @brief  return imageObject size, 0 is no imageObject.
205      */
206     uint32_t GetAllObject(std::vector<std::shared_ptr<ExtendImageObject>>& objectList);
207 
208     /*
209      * @brief  return real setup imageObject size.
210      */
211     uint32_t SetupObject(const std::vector<std::shared_ptr<ExtendImageObject>>& objectList);
212 
213      /*
214      * @brief  return imageBaseObj index, negative is error.
215      */
216     uint32_t AddImageBaseObj(const std::shared_ptr<ExtendImageBaseObj>& object);
217 
218     /*
219      * @brief  get imageBaseObj by index.
220      */
221     std::shared_ptr<ExtendImageBaseObj> GetImageBaseObj(uint32_t id);
222 
223     /*
224      * @brief  return imageBaseObj size, 0 is no imageBaseObj.
225      */
226     uint32_t GetAllBaseObj(std::vector<std::shared_ptr<ExtendImageBaseObj>>& objectList);
227 
228     /*
229      * @brief  return real setup imageBaseObj size.
230      */
231     uint32_t SetupBaseObj(const std::vector<std::shared_ptr<ExtendImageBaseObj>>& objectList);
232 
233      /*
234      * @brief  return DrawFuncObj index, negative is error.
235      */
236     uint32_t AddDrawFuncOjb(const std::shared_ptr<ExtendDrawFuncObj>& object);
237 
238     /*
239      * @brief  get DrawFuncObj by index.
240      */
241     std::shared_ptr<ExtendDrawFuncObj> GetDrawFuncObj(uint32_t id);
242 
243     /*
244      * @brief  copy object vec to another CmdList.
245      */
246     void CopyObjectTo(CmdList& other) const;
247 
248     /*
249      * @brief  return recording op count.
250      */
251     uint32_t GetOpCnt() const;
252 
253     CmdList(CmdList&&) = delete;
254     CmdList(const CmdList&) = delete;
255     CmdList& operator=(CmdList&&) = delete;
256     CmdList& operator=(const CmdList&) = delete;
257 
258 #ifdef ROSEN_OHOS
259     /*
260      * @brief  return surfaceBufferEntry index, negative is error.
261      */
262     uint32_t AddSurfaceBufferEntry(const std::shared_ptr<SurfaceBufferEntry>& surfaceBufferEntry);
263 
264     /*
265      * @brief  get surfaceBufferEntry by index.
266      */
267     std::shared_ptr<SurfaceBufferEntry> GetSurfaceBufferEntry(uint32_t id);
268 
269     /*
270      * @brief  return surfaceBufferEntry size, 0 is no surfaceBuffer.
271      */
272     uint32_t GetAllSurfaceBufferEntry(std::vector<std::shared_ptr<SurfaceBufferEntry>>& objectList);
273 
274     /*
275      * @brief  return real setup surfaceBufferEntry size.
276      */
277     uint32_t SetupSurfaceBufferEntry(const std::vector<std::shared_ptr<SurfaceBufferEntry>>& objectList);
278 #endif
279 
280 protected:
281     MemAllocator opAllocator_;
282     MemAllocator imageAllocator_;
283     MemAllocator bitmapAllocator_;
284     std::optional<size_t> lastOpItemOffset_ = std::nullopt;
285     std::recursive_mutex mutex_;
286     std::map<size_t, std::shared_ptr<Image>> imageMap_;
287     std::vector<std::pair<size_t, OpDataHandle>> imageHandleVec_;
288     uint32_t opCnt_ = 0;
289 
290     std::vector<std::shared_ptr<RecordCmd>> recordCmdVec_;
291     std::mutex recordCmdMutex_;
292     std::vector<std::shared_ptr<ExtendImageObject>> imageObjectVec_;
293     std::mutex imageObjectMutex_;
294     std::vector<std::shared_ptr<ExtendImageBaseObj>> imageBaseObjVec_;
295     std::mutex imageBaseObjMutex_;
296     std::vector<std::shared_ptr<ExtendObject>> extendObjectVec_;
297     std::mutex extendObjectMutex_;
298 #ifdef ROSEN_OHOS
299     std::vector<std::shared_ptr<SurfaceBufferEntry>> surfaceBufferEntryVec_;
300     std::mutex surfaceBufferEntryMutex_;
301 #endif
302     std::vector<std::shared_ptr<ExtendDrawFuncObj>> drawFuncObjVec_;
303     std::mutex drawFuncObjMutex_;
304 };
305 } // namespace Drawing
306 } // namespace Rosen
307 } // namespace OHOS
308 #endif