• 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/color.h"
24 #include "recording/op_item.h"
25 #include "recording/mem_allocator.h"
26 #include "recording/adaptive_image_helper.h"
27 #include "recording/recording_handle.h"
28 #include "utils/drawing_macros.h"
29 #ifdef ROSEN_OHOS
30 #include "surface_buffer.h"
31 #endif
32 
33 namespace OHOS {
34 namespace Media {
35 class PixelMap;
36 }
37 namespace Rosen {
38 namespace Drawing {
39 using CmdListData = std::pair<const void*, size_t>;
40 using NodeId = uint64_t;
41 
42 class DRAWING_API ExtendImageObject {
43 public:
44     virtual ~ExtendImageObject() = default;
45     virtual void Playback(Canvas& canvas, const Rect& rect,
46         const SamplingOptions& sampling, bool isBackground = false) = 0;
SetNodeId(NodeId id)47     virtual void SetNodeId(NodeId id) {};
48 };
49 
50 class DRAWING_API ExtendImageBaseObj {
51 public:
52     virtual ~ExtendImageBaseObj() = default;
53     virtual void Playback(Canvas& canvas, const Rect& rect,
54         const SamplingOptions& sampling) = 0;
SetNodeId(NodeId id)55     virtual void SetNodeId(NodeId id) {};
56 };
57 
58 class DRAWING_API ExtendDrawFuncObj {
59 public:
60     virtual ~ExtendDrawFuncObj () = default;
61     virtual void Playback(Canvas* canvas, const Rect* rect) = 0;
62 };
63 
64 class DRAWING_API CmdList {
65 public:
66     enum Type : uint32_t {
67         CMD_LIST = 0,
68         COLOR_FILTER_CMD_LIST,
69         COLOR_SPACE_CMD_LIST,
70         DRAW_CMD_LIST,
71         IMAGE_FILTER_CMD_LIST,
72         MASK_FILTER_CMD_LIST,
73         PATH_CMD_LIST,
74         PATH_EFFECT_CMD_LIST,
75         REGION_CMD_LIST,
76         SHADER_EFFECT_CMD_LIST,
77         MASK_CMD_LIST,
78     };
79 
80     CmdList() = default;
81     explicit CmdList(const CmdListData& cmdListData);
82     virtual ~CmdList();
83 
GetType()84     virtual uint32_t GetType() const
85     {
86         return Type::CMD_LIST;
87     }
88 
89     /*
90      * @brief       Add OpItem to CmdList.
91      * @param T     The name of OpItem class.
92      * @param Args  Constructs arguments to the OpItem.
93      */
94     template<typename T, typename... Args>
AddOp(Args &&...args)95     void AddOp(Args&&... args)
96     {
97         std::lock_guard<std::recursive_mutex> lock(mutex_);
98         T* op = opAllocator_.Allocate<T>(std::forward<Args>(args)...);
99         if (op == nullptr) {
100             return;
101         }
102 
103         uint32_t offset = opAllocator_.AddrToOffset(op);
104         if (lastOpItemOffset_.has_value()) {
105 #ifdef CROSS_PLATFORM
106             auto* lastOpItem = static_cast<OpItem*>(opAllocator_.OffsetToAddr(lastOpItemOffset_.__get()));
107 #else
108             auto* lastOpItem = static_cast<OpItem*>(opAllocator_.OffsetToAddr(lastOpItemOffset_.value()));
109 #endif
110             if (lastOpItem != nullptr) {
111                 lastOpItem->SetNextOpItemOffset(offset);
112             }
113         }
114         lastOpItemOffset_.emplace(offset);
115         opCnt_++;
116     }
117 
118     /*
119      * @brief       Add a contiguous buffers to the CmdList.
120      * @param src   A contiguous buffers.
121      * @return      Returns the offset of the contiguous buffers and CmdList head point.
122      */
123     uint32_t AddCmdListData(const CmdListData& data);
124 
125     const void* GetCmdListData(uint32_t offset) const;
126 
127     /*
128      * @brief   Gets the contiguous buffers of CmdList.
129      */
130     CmdListData GetData() const;
131 
132     // using for recording, should to remove after using shared memory
133     bool SetUpImageData(const void* data, size_t size);
134     uint32_t AddImageData(const void* data, size_t size);
135     const void* GetImageData(uint32_t offset) const;
136     CmdListData GetAllImageData() const;
137 
138     OpDataHandle AddImage(const Image& image);
139     std::shared_ptr<Image> GetImage(const OpDataHandle& imageHandle);
140 
141     uint32_t AddBitmapData(const void* data, size_t size);
142     const void* GetBitmapData(uint32_t offset) const;
143     bool SetUpBitmapData(const void* data, size_t size);
144     CmdListData GetAllBitmapData() const;
145 
146     /*
147      * @brief  return pixelmap index, negative is error.
148      */
149     uint32_t AddPixelMap(const std::shared_ptr<Media::PixelMap>& pixelMap);
150 
151     /*
152      * @brief  get pixelmap by index.
153      */
154     std::shared_ptr<Media::PixelMap> GetPixelMap(uint32_t id);
155 
156     /*
157      * @brief  return pixelmaplist size, 0 is no pixelmap.
158      */
159     uint32_t GetAllPixelMap(std::vector<std::shared_ptr<Media::PixelMap>>& pixelMapList);
160 
161     /*
162      * @brief  return real setup pixelmap size.
163      */
164     uint32_t SetupPixelMap(const std::vector<std::shared_ptr<Media::PixelMap>>& pixelMapList);
165 
166     /*
167      * @brief  return imageObject index, negative is error.
168      */
169     uint32_t AddImageObject(const std::shared_ptr<ExtendImageObject>& object);
170 
171     /*
172      * @brief  get imageObject by index.
173      */
174     std::shared_ptr<ExtendImageObject> GetImageObject(uint32_t id);
175 
176     /*
177      * @brief  return imageObject size, 0 is no imageObject.
178      */
179     uint32_t GetAllObject(std::vector<std::shared_ptr<ExtendImageObject>>& objectList);
180 
181     /*
182      * @brief  return real setup imageObject size.
183      */
184     uint32_t SetupObject(const std::vector<std::shared_ptr<ExtendImageObject>>& objectList);
185 
186      /*
187      * @brief  return imageBaseObj index, negative is error.
188      */
189     uint32_t AddImageBaseObj(const std::shared_ptr<ExtendImageBaseObj>& object);
190 
191     /*
192      * @brief  get imageBaseObj by index.
193      */
194     std::shared_ptr<ExtendImageBaseObj> GetImageBaseObj(uint32_t id);
195 
196     /*
197      * @brief  return imageBaseObj size, 0 is no imageBaseObj.
198      */
199     uint32_t GetAllBaseObj(std::vector<std::shared_ptr<ExtendImageBaseObj>>& objectList);
200 
201     /*
202      * @brief  return real setup imageBaseObj size.
203      */
204     uint32_t SetupBaseObj(const std::vector<std::shared_ptr<ExtendImageBaseObj>>& objectList);
205 
206      /*
207      * @brief  return DrawFuncObj index, negative is error.
208      */
209     uint32_t AddDrawFuncOjb(const std::shared_ptr<ExtendDrawFuncObj>& object);
210 
211     /*
212      * @brief  get DrawFuncObj by index.
213      */
214     std::shared_ptr<ExtendDrawFuncObj> GetDrawFuncObj(uint32_t id);
215 
216     /*
217      * @brief  copy object vec to another CmdList.
218      */
219     void CopyObjectTo(CmdList& other) const;
220 
221     /*
222      * @brief  return recording op count.
223      */
224     uint32_t GetOpCnt() const;
225 
226     CmdList(CmdList&&) = delete;
227     CmdList(const CmdList&) = delete;
228     CmdList& operator=(CmdList&&) = delete;
229     CmdList& operator=(const CmdList&) = delete;
230 
231 #ifdef ROSEN_OHOS
232     /*
233      * @brief  return surfaceBuffer index, negative is error.
234      */
235     uint32_t AddSurfaceBuffer(const sptr<SurfaceBuffer>& surfaceBuffer);
236 
237     /*
238      * @brief  get surfaceBuffer by index.
239      */
240     sptr<SurfaceBuffer> GetSurfaceBuffer(uint32_t id);
241 
242     /*
243      * @brief  return surfaceBuffer size, 0 is no surfaceBuffer.
244      */
245     uint32_t GetAllSurfaceBuffer(std::vector<sptr<SurfaceBuffer>>& objectList);
246 
247     /*
248      * @brief  return real setup surfaceBuffer size.
249      */
250     uint32_t SetupSurfaceBuffer(const std::vector<sptr<SurfaceBuffer>>& objectList);
251 #endif
252 
253 protected:
254     MemAllocator opAllocator_;
255     MemAllocator imageAllocator_;
256     MemAllocator bitmapAllocator_;
257     std::optional<uint32_t> lastOpItemOffset_ = std::nullopt;
258     std::recursive_mutex mutex_;
259     std::map<uint32_t, std::shared_ptr<Image>> imageMap_;
260     std::vector<std::pair<uint32_t, OpDataHandle>> imageHandleVec_;
261     uint32_t opCnt_ = 0;
262 
263 #ifdef SUPPORT_OHOS_PIXMAP
264     std::vector<std::shared_ptr<Media::PixelMap>> pixelMapVec_;
265     std::mutex pixelMapMutex_;
266 #endif
267     std::vector<std::shared_ptr<ExtendImageObject>> imageObjectVec_;
268     std::mutex imageObjectMutex_;
269     std::vector<std::shared_ptr<ExtendImageBaseObj>> imageBaseObjVec_;
270     std::mutex imageBaseObjMutex_;
271 #ifdef ROSEN_OHOS
272     std::vector<sptr<SurfaceBuffer>> surfaceBufferVec_;
273     std::mutex surfaceBufferMutex_;
274 #endif
275     std::vector<std::shared_ptr<ExtendDrawFuncObj>> drawFuncObjVec_;
276     std::mutex drawFuncObjMutex_;
277 };
278 } // namespace Drawing
279 } // namespace Rosen
280 } // namespace OHOS
281 #endif