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 #include "recording/cmd_list.h"
17
18 #include <algorithm>
19
20 #ifdef SUPPORT_OHOS_PIXMAP
21 #include "pixel_map.h"
22 #endif
23 #include "utils/log.h"
24
25 namespace OHOS {
26 namespace Rosen {
27 namespace Drawing {
28 static constexpr uint32_t OPITEM_HEAD = 0;
29
CmdList(const CmdListData & cmdListData)30 CmdList::CmdList(const CmdListData& cmdListData)
31 {
32 opAllocator_.BuildFromDataWithCopy(cmdListData.first, cmdListData.second);
33 }
34
~CmdList()35 CmdList::~CmdList()
36 {
37 #ifdef SUPPORT_OHOS_PIXMAP
38 pixelMapVec_.clear();
39 #endif
40 #ifdef ROSEN_OHOS
41 surfaceBufferVec_.clear();
42 #endif
43 }
44
AddCmdListData(const CmdListData & data)45 uint32_t CmdList::AddCmdListData(const CmdListData& data)
46 {
47 std::lock_guard<std::recursive_mutex> lock(mutex_);
48 if (!lastOpItemOffset_.has_value()) {
49 void* op = opAllocator_.Allocate<OpItem>(OPITEM_HEAD);
50 if (op == nullptr) {
51 LOGD("add OpItem head failed!");
52 return 0;
53 }
54 lastOpItemOffset_.emplace(opAllocator_.AddrToOffset(op));
55 }
56 void* addr = opAllocator_.Add(data.first, data.second);
57 if (addr == nullptr) {
58 LOGD("CmdList AddCmdListData failed!");
59 return 0;
60 }
61 return opAllocator_.AddrToOffset(addr);
62 }
63
GetCmdListData(uint32_t offset) const64 const void* CmdList::GetCmdListData(uint32_t offset) const
65 {
66 return opAllocator_.OffsetToAddr(offset);
67 }
68
GetData() const69 CmdListData CmdList::GetData() const
70 {
71 return std::make_pair(opAllocator_.GetData(), opAllocator_.GetSize());
72 }
73
SetUpImageData(const void * data,size_t size)74 bool CmdList::SetUpImageData(const void* data, size_t size)
75 {
76 return imageAllocator_.BuildFromDataWithCopy(data, size);
77 }
78
AddImageData(const void * data,size_t size)79 uint32_t CmdList::AddImageData(const void* data, size_t size)
80 {
81 std::lock_guard<std::recursive_mutex> lock(mutex_);
82 void* addr = imageAllocator_.Add(data, size);
83 if (addr == nullptr) {
84 LOGD("CmdList AddImageData failed!");
85 return 0;
86 }
87 return imageAllocator_.AddrToOffset(addr);
88 }
89
GetImageData(uint32_t offset) const90 const void* CmdList::GetImageData(uint32_t offset) const
91 {
92 return imageAllocator_.OffsetToAddr(offset);
93 }
94
GetAllImageData() const95 CmdListData CmdList::GetAllImageData() const
96 {
97 return std::make_pair(imageAllocator_.GetData(), imageAllocator_.GetSize());
98 }
99
AddImage(const Image & image)100 OpDataHandle CmdList::AddImage(const Image& image)
101 {
102 std::lock_guard<std::recursive_mutex> lock(mutex_);
103 OpDataHandle ret = {0, 0};
104 uint32_t uniqueId = image.GetUniqueID();
105
106 for (auto iter = imageHandleVec_.begin(); iter != imageHandleVec_.end(); iter++) {
107 if (iter->first == uniqueId) {
108 return iter->second;
109 }
110 }
111
112 auto data = image.Serialize();
113 if (data == nullptr || data->GetSize() == 0) {
114 LOGD("image is vaild");
115 return ret;
116 }
117 void* addr = imageAllocator_.Add(data->GetData(), data->GetSize());
118 if (addr == nullptr) {
119 LOGD("CmdList AddImageData failed!");
120 return ret;
121 }
122 uint32_t offset = imageAllocator_.AddrToOffset(addr);
123 imageHandleVec_.push_back(std::pair<uint32_t, OpDataHandle>(uniqueId, {offset, data->GetSize()}));
124
125 return {offset, data->GetSize()};
126 }
127
GetImage(const OpDataHandle & imageHandle)128 std::shared_ptr<Image> CmdList::GetImage(const OpDataHandle& imageHandle)
129 {
130 std::lock_guard<std::recursive_mutex> lock(mutex_);
131 auto imageIt = imageMap_.find(imageHandle.offset);
132 if (imageIt != imageMap_.end()) {
133 return imageMap_[imageHandle.offset];
134 }
135
136 if (imageHandle.size == 0) {
137 LOGD("image is vaild");
138 return nullptr;
139 }
140
141 const void* ptr = imageAllocator_.OffsetToAddr(imageHandle.offset);
142 if (ptr == nullptr) {
143 LOGD("get image data failed");
144 return nullptr;
145 }
146
147 auto imageData = std::make_shared<Data>();
148 imageData->BuildWithoutCopy(ptr, imageHandle.size);
149 auto image = std::make_shared<Image>();
150 if (image->Deserialize(imageData) == false) {
151 LOGD("image deserialize failed!");
152 return nullptr;
153 }
154 imageMap_[imageHandle.offset] = image;
155 return image;
156 }
157
AddBitmapData(const void * data,size_t size)158 uint32_t CmdList::AddBitmapData(const void* data, size_t size)
159 {
160 std::lock_guard<std::recursive_mutex> lock(mutex_);
161 void* addr = bitmapAllocator_.Add(data, size);
162 if (addr == nullptr) {
163 LOGD("CmdList AddImageData failed!");
164 return 0;
165 }
166 return bitmapAllocator_.AddrToOffset(addr);
167 }
168
GetBitmapData(uint32_t offset) const169 const void* CmdList::GetBitmapData(uint32_t offset) const
170 {
171 return bitmapAllocator_.OffsetToAddr(offset);
172 }
173
SetUpBitmapData(const void * data,size_t size)174 bool CmdList::SetUpBitmapData(const void* data, size_t size)
175 {
176 return bitmapAllocator_.BuildFromDataWithCopy(data, size);
177 }
178
GetAllBitmapData() const179 CmdListData CmdList::GetAllBitmapData() const
180 {
181 return std::make_pair(bitmapAllocator_.GetData(), bitmapAllocator_.GetSize());
182 }
183
AddPixelMap(const std::shared_ptr<Media::PixelMap> & pixelMap)184 uint32_t CmdList::AddPixelMap(const std::shared_ptr<Media::PixelMap>& pixelMap)
185 {
186 #ifdef SUPPORT_OHOS_PIXMAP
187 std::lock_guard<std::mutex> lock(pixelMapMutex_);
188 pixelMapVec_.emplace_back(pixelMap);
189 return static_cast<uint32_t>(pixelMapVec_.size()) - 1;
190 #else
191 return 0;
192 #endif
193 }
194
GetPixelMap(uint32_t id)195 std::shared_ptr<Media::PixelMap> CmdList::GetPixelMap(uint32_t id)
196 {
197 #ifdef SUPPORT_OHOS_PIXMAP
198 std::lock_guard<std::mutex> lock(pixelMapMutex_);
199 if (id >= pixelMapVec_.size()) {
200 return nullptr;
201 }
202 return pixelMapVec_[id];
203 #else
204 return nullptr;
205 #endif
206 }
207
GetAllPixelMap(std::vector<std::shared_ptr<Media::PixelMap>> & pixelMapList)208 uint32_t CmdList::GetAllPixelMap(std::vector<std::shared_ptr<Media::PixelMap>>& pixelMapList)
209 {
210 #ifdef SUPPORT_OHOS_PIXMAP
211 std::lock_guard<std::mutex> lock(pixelMapMutex_);
212 for (const auto &pixelMap : pixelMapVec_) {
213 pixelMapList.emplace_back(pixelMap);
214 }
215 return pixelMapList.size();
216 #else
217 return 0;
218 #endif
219 }
220
SetupPixelMap(const std::vector<std::shared_ptr<Media::PixelMap>> & pixelMapList)221 uint32_t CmdList::SetupPixelMap(const std::vector<std::shared_ptr<Media::PixelMap>>& pixelMapList)
222 {
223 #ifdef SUPPORT_OHOS_PIXMAP
224 std::lock_guard<std::mutex> lock(pixelMapMutex_);
225 for (const auto &pixelMap : pixelMapList) {
226 pixelMapVec_.emplace_back(pixelMap);
227 }
228 return pixelMapVec_.size();
229 #else
230 return 0;
231 #endif
232 }
233
AddImageObject(const std::shared_ptr<ExtendImageObject> & object)234 uint32_t CmdList::AddImageObject(const std::shared_ptr<ExtendImageObject>& object)
235 {
236 std::lock_guard<std::mutex> lock(imageObjectMutex_);
237 imageObjectVec_.emplace_back(object);
238 return static_cast<uint32_t>(imageObjectVec_.size()) - 1;
239 }
240
GetImageObject(uint32_t id)241 std::shared_ptr<ExtendImageObject> CmdList::GetImageObject(uint32_t id)
242 {
243 std::lock_guard<std::mutex> lock(imageObjectMutex_);
244 if (id >= imageObjectVec_.size()) {
245 return nullptr;
246 }
247 return imageObjectVec_[id];
248 }
249
GetAllObject(std::vector<std::shared_ptr<ExtendImageObject>> & objectList)250 uint32_t CmdList::GetAllObject(std::vector<std::shared_ptr<ExtendImageObject>>& objectList)
251 {
252 std::lock_guard<std::mutex> lock(imageObjectMutex_);
253 for (const auto &object : imageObjectVec_) {
254 objectList.emplace_back(object);
255 }
256 return objectList.size();
257 }
258
SetupObject(const std::vector<std::shared_ptr<ExtendImageObject>> & objectList)259 uint32_t CmdList::SetupObject(const std::vector<std::shared_ptr<ExtendImageObject>>& objectList)
260 {
261 std::lock_guard<std::mutex> lock(imageObjectMutex_);
262 for (const auto &object : objectList) {
263 imageObjectVec_.emplace_back(object);
264 }
265 return imageObjectVec_.size();
266 }
267
AddImageBaseObj(const std::shared_ptr<ExtendImageBaseObj> & object)268 uint32_t CmdList::AddImageBaseObj(const std::shared_ptr<ExtendImageBaseObj>& object)
269 {
270 std::lock_guard<std::mutex> lock(imageBaseObjMutex_);
271 imageBaseObjVec_.emplace_back(object);
272 return static_cast<uint32_t>(imageBaseObjVec_.size()) - 1;
273 }
274
GetImageBaseObj(uint32_t id)275 std::shared_ptr<ExtendImageBaseObj> CmdList::GetImageBaseObj(uint32_t id)
276 {
277 std::lock_guard<std::mutex> lock(imageBaseObjMutex_);
278 if (id >= imageBaseObjVec_.size()) {
279 return nullptr;
280 }
281 return imageBaseObjVec_[id];
282 }
283
GetAllBaseObj(std::vector<std::shared_ptr<ExtendImageBaseObj>> & objectList)284 uint32_t CmdList::GetAllBaseObj(std::vector<std::shared_ptr<ExtendImageBaseObj>>& objectList)
285 {
286 std::lock_guard<std::mutex> lock(imageBaseObjMutex_);
287 for (const auto &object : imageBaseObjVec_) {
288 objectList.emplace_back(object);
289 }
290 return objectList.size();
291 }
292
SetupBaseObj(const std::vector<std::shared_ptr<ExtendImageBaseObj>> & objectList)293 uint32_t CmdList::SetupBaseObj(const std::vector<std::shared_ptr<ExtendImageBaseObj>>& objectList)
294 {
295 std::lock_guard<std::mutex> lock(imageBaseObjMutex_);
296 for (const auto &object : objectList) {
297 imageBaseObjVec_.emplace_back(object);
298 }
299 return imageBaseObjVec_.size();
300 }
301
CopyObjectTo(CmdList & other) const302 void CmdList::CopyObjectTo(CmdList& other) const
303 {
304 #ifdef SUPPORT_OHOS_PIXMAP
305 other.imageObjectVec_ = imageObjectVec_;
306 #endif
307 other.imageBaseObjVec_ = imageBaseObjVec_;
308 }
309
GetOpCnt() const310 uint32_t CmdList::GetOpCnt() const
311 {
312 return opCnt_;
313 }
314
315 #ifdef ROSEN_OHOS
AddSurfaceBuffer(const sptr<SurfaceBuffer> & surfaceBuffer)316 uint32_t CmdList::AddSurfaceBuffer(const sptr<SurfaceBuffer>& surfaceBuffer)
317 {
318 std::lock_guard<std::mutex> lock(surfaceBufferMutex_);
319 surfaceBufferVec_.emplace_back(surfaceBuffer);
320 return static_cast<uint32_t>(surfaceBufferVec_.size()) - 1;
321 }
322
GetSurfaceBuffer(uint32_t id)323 sptr<SurfaceBuffer> CmdList::GetSurfaceBuffer(uint32_t id)
324 {
325 std::lock_guard<std::mutex> lock(surfaceBufferMutex_);
326 if (id >= surfaceBufferVec_.size()) {
327 return nullptr;
328 }
329 return surfaceBufferVec_[id];
330 }
331
GetAllSurfaceBuffer(std::vector<sptr<SurfaceBuffer>> & objectList)332 uint32_t CmdList::GetAllSurfaceBuffer(std::vector<sptr<SurfaceBuffer>>& objectList)
333 {
334 std::lock_guard<std::mutex> lock(surfaceBufferMutex_);
335 for (const auto &object : surfaceBufferVec_) {
336 objectList.emplace_back(object);
337 }
338 return static_cast<uint32_t>(objectList.size());
339 }
340
SetupSurfaceBuffer(const std::vector<sptr<SurfaceBuffer>> & objectList)341 uint32_t CmdList::SetupSurfaceBuffer(const std::vector<sptr<SurfaceBuffer>>& objectList)
342 {
343 std::lock_guard<std::mutex> lock(surfaceBufferMutex_);
344 for (const auto &object : objectList) {
345 surfaceBufferVec_.emplace_back(object);
346 }
347 return static_cast<uint32_t>(surfaceBufferVec_.size());
348 }
349 #endif
350
AddDrawFuncOjb(const std::shared_ptr<ExtendDrawFuncObj> & object)351 uint32_t CmdList::AddDrawFuncOjb(const std::shared_ptr<ExtendDrawFuncObj> &object)
352 {
353 std::lock_guard<std::mutex> lock(drawFuncObjMutex_);
354 drawFuncObjVec_.emplace_back(object);
355 return static_cast<uint32_t>(drawFuncObjVec_.size()) - 1;
356 }
357
GetDrawFuncObj(uint32_t id)358 std::shared_ptr<ExtendDrawFuncObj> CmdList::GetDrawFuncObj(uint32_t id)
359 {
360 std::lock_guard<std::mutex> lock(drawFuncObjMutex_);
361 if (id >= drawFuncObjVec_.size()) {
362 return nullptr;
363 }
364 return drawFuncObjVec_[id];
365 }
366 } // namespace Drawing
367 } // namespace Rosen
368 } // namespace OHOS
369