• 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 #include "recording/cmd_list.h"
17 
18 #include <algorithm>
19 #include <sstream>
20 
21 #include "utils/log.h"
22 
23 namespace OHOS {
24 namespace Rosen {
25 namespace Drawing {
26 static constexpr uint32_t OPITEM_HEAD = 0;
27 
CmdList(const CmdListData & cmdListData)28 CmdList::CmdList(const CmdListData& cmdListData)
29 {
30     opAllocator_.BuildFromDataWithCopy(cmdListData.first, cmdListData.second);
31 }
32 
~CmdList()33 CmdList::~CmdList()
34 {
35 #ifdef ROSEN_OHOS
36     surfaceBufferEntryVec_.clear();
37 #endif
38 }
39 
AddCmdListData(const CmdListData & data)40 size_t CmdList::AddCmdListData(const CmdListData& data)
41 {
42     std::lock_guard<std::recursive_mutex> lock(mutex_);
43     if (!lastOpItemOffset_.has_value()) {
44         void* op = opAllocator_.Allocate<OpItem>(OPITEM_HEAD);
45         if (op == nullptr) {
46             LOGD("add OpItem head failed!");
47             return 0;
48         }
49         lastOpItemOffset_.emplace(opAllocator_.AddrToOffset(op));
50     }
51     void* addr = opAllocator_.Add(data.first, data.second);
52     if (addr == nullptr) {
53         LOGD("CmdList AddCmdListData failed!");
54         return 0;
55     }
56     return opAllocator_.AddrToOffset(addr);
57 }
58 
GetCmdListData(size_t offset,size_t size) const59 const void* CmdList::GetCmdListData(size_t offset, size_t size) const
60 {
61     return opAllocator_.OffsetToAddr(offset, size);
62 }
63 
GetData() const64 CmdListData CmdList::GetData() const
65 {
66     return std::make_pair(opAllocator_.GetData(), opAllocator_.GetSize());
67 }
68 
SetUpImageData(const void * data,size_t size)69 bool CmdList::SetUpImageData(const void* data, size_t size)
70 {
71     return imageAllocator_.BuildFromDataWithCopy(data, size);
72 }
73 
AddImageData(const void * data,size_t size)74 size_t CmdList::AddImageData(const void* data, size_t size)
75 {
76     std::lock_guard<std::recursive_mutex> lock(mutex_);
77     void* addr = imageAllocator_.Add(data, size);
78     if (addr == nullptr) {
79         LOGD("CmdList AddImageData failed!");
80         return 0;
81     }
82     return imageAllocator_.AddrToOffset(addr);
83 }
84 
GetImageData(size_t offset,size_t size) const85 const void* CmdList::GetImageData(size_t offset, size_t size) const
86 {
87     return imageAllocator_.OffsetToAddr(offset, size);
88 }
89 
GetAllImageData() const90 CmdListData CmdList::GetAllImageData() const
91 {
92     return std::make_pair(imageAllocator_.GetData(), imageAllocator_.GetSize());
93 }
94 
AddImage(const Image & image)95 OpDataHandle CmdList::AddImage(const Image& image)
96 {
97     std::lock_guard<std::recursive_mutex> lock(mutex_);
98     OpDataHandle ret = {0, 0};
99     uint32_t uniqueId = image.GetUniqueID();
100 
101     for (auto iter = imageHandleVec_.begin(); iter != imageHandleVec_.end(); iter++) {
102         if (iter->first == uniqueId) {
103             return iter->second;
104         }
105     }
106 
107     auto data = image.Serialize();
108     if (data == nullptr || data->GetSize() == 0) {
109         LOGD("image is vaild");
110         return ret;
111     }
112     void* addr = imageAllocator_.Add(data->GetData(), data->GetSize());
113     if (addr == nullptr) {
114         LOGD("CmdList AddImageData failed!");
115         return ret;
116     }
117     size_t offset = imageAllocator_.AddrToOffset(addr);
118     imageHandleVec_.push_back(std::pair<size_t, OpDataHandle>(uniqueId, {offset, data->GetSize()}));
119 
120     return {offset, data->GetSize()};
121 }
122 
GetImage(const OpDataHandle & imageHandle)123 std::shared_ptr<Image> CmdList::GetImage(const OpDataHandle& imageHandle)
124 {
125     std::lock_guard<std::recursive_mutex> lock(mutex_);
126     auto imageIt = imageMap_.find(imageHandle.offset);
127     if (imageIt != imageMap_.end()) {
128         return imageMap_[imageHandle.offset];
129     }
130 
131     if (imageHandle.size == 0) {
132         LOGD("image is vaild");
133         return nullptr;
134     }
135 
136     const void* ptr = imageAllocator_.OffsetToAddr(imageHandle.offset, imageHandle.size);
137     if (ptr == nullptr) {
138         LOGD("get image data failed");
139         return nullptr;
140     }
141 
142     auto imageData = std::make_shared<Data>();
143     imageData->BuildWithoutCopy(ptr, imageHandle.size);
144     auto image = std::make_shared<Image>();
145     if (image->Deserialize(imageData) == false) {
146         LOGD("image deserialize failed!");
147         return nullptr;
148     }
149     imageMap_[imageHandle.offset] = image;
150     return image;
151 }
152 
AddBitmapData(const void * data,size_t size)153 size_t CmdList::AddBitmapData(const void* data, size_t size)
154 {
155     std::lock_guard<std::recursive_mutex> lock(mutex_);
156     void* addr = bitmapAllocator_.Add(data, size);
157     if (addr == nullptr) {
158         LOGD("CmdList AddImageData failed!");
159         return 0;
160     }
161     return bitmapAllocator_.AddrToOffset(addr);
162 }
163 
GetBitmapData(size_t offset,size_t size) const164 const void* CmdList::GetBitmapData(size_t offset, size_t size) const
165 {
166     return bitmapAllocator_.OffsetToAddr(offset, size);
167 }
168 
SetUpBitmapData(const void * data,size_t size)169 bool CmdList::SetUpBitmapData(const void* data, size_t size)
170 {
171     return bitmapAllocator_.BuildFromDataWithCopy(data, size);
172 }
173 
GetAllBitmapData() const174 CmdListData CmdList::GetAllBitmapData() const
175 {
176     return std::make_pair(bitmapAllocator_.GetData(), bitmapAllocator_.GetSize());
177 }
178 
AddExtendObject(const std::shared_ptr<ExtendObject> & object)179 uint32_t CmdList::AddExtendObject(const std::shared_ptr<ExtendObject>& object)
180 {
181     std::lock_guard<std::mutex> lock(extendObjectMutex_);
182     extendObjectVec_.emplace_back(object);
183     return static_cast<uint32_t>(extendObjectVec_.size()) - 1;
184 }
185 
GetExtendObject(uint32_t index)186 std::shared_ptr<ExtendObject> CmdList::GetExtendObject(uint32_t index)
187 {
188     std::lock_guard<std::mutex> lock(extendObjectMutex_);
189     if (index >= extendObjectVec_.size()) {
190         return nullptr;
191     }
192     return extendObjectVec_[index];
193 }
194 
GetAllExtendObject(std::vector<std::shared_ptr<ExtendObject>> & objectList)195 uint32_t CmdList::GetAllExtendObject(std::vector<std::shared_ptr<ExtendObject>>& objectList)
196 {
197     std::lock_guard<std::mutex> lock(extendObjectMutex_);
198     for (const auto &object : extendObjectVec_) {
199         objectList.emplace_back(object);
200     }
201     return objectList.size();
202 }
203 
SetupExtendObject(const std::vector<std::shared_ptr<ExtendObject>> & objectList)204 uint32_t CmdList::SetupExtendObject(const std::vector<std::shared_ptr<ExtendObject>>& objectList)
205 {
206     std::lock_guard<std::mutex> lock(extendObjectMutex_);
207     for (const auto &object : objectList) {
208         extendObjectVec_.emplace_back(object);
209     }
210     return extendObjectVec_.size();
211 }
212 
AddDrawingObject(const std::shared_ptr<Object> & object)213 uint32_t CmdList::AddDrawingObject(const std::shared_ptr<Object>& object)
214 {
215     std::lock_guard<std::mutex> lock(drawingObjectMutex_);
216     drawingObjectVec_.emplace_back(object);
217     return static_cast<uint32_t>(drawingObjectVec_.size()) - 1;
218 }
219 
GetDrawingObject(uint32_t index)220 std::shared_ptr<Object> CmdList::GetDrawingObject(uint32_t index)
221 {
222     std::lock_guard<std::mutex> lock(drawingObjectMutex_);
223     if (index >= drawingObjectVec_.size()) {
224         return nullptr;
225     }
226     return drawingObjectVec_[index];
227 }
228 
GetAllDrawingObject(std::vector<std::shared_ptr<Object>> & objectList)229 uint32_t CmdList::GetAllDrawingObject(std::vector<std::shared_ptr<Object>>& objectList)
230 {
231     std::lock_guard<std::mutex> lock(drawingObjectMutex_);
232     for (const auto &object : drawingObjectVec_) {
233         objectList.emplace_back(object);
234     }
235     return objectList.size();
236 }
237 
SetupDrawingObject(const std::vector<std::shared_ptr<Object>> & objectList)238 uint32_t CmdList::SetupDrawingObject(const std::vector<std::shared_ptr<Object>>& objectList)
239 {
240     std::lock_guard<std::mutex> lock(drawingObjectMutex_);
241     for (const auto &object : objectList) {
242         drawingObjectVec_.emplace_back(object);
243     }
244     return drawingObjectVec_.size();
245 }
246 
AddRecordCmd(const std::shared_ptr<RecordCmd> & recordCmd)247 uint32_t CmdList::AddRecordCmd(const std::shared_ptr<RecordCmd>& recordCmd)
248 {
249     std::lock_guard<std::mutex> lock(recordCmdMutex_);
250     recordCmdVec_.emplace_back(recordCmd);
251     return static_cast<uint32_t>(recordCmdVec_.size()) - 1;
252 }
253 
GetRecordCmd(uint32_t index)254 std::shared_ptr<RecordCmd> CmdList::GetRecordCmd(uint32_t index)
255 {
256     std::lock_guard<std::mutex> lock(recordCmdMutex_);
257     if (index >= recordCmdVec_.size()) {
258         return nullptr;
259     }
260     return recordCmdVec_[index];
261 }
262 
SetupRecordCmd(std::vector<std::shared_ptr<RecordCmd>> & recordCmdList)263 uint32_t CmdList::SetupRecordCmd(std::vector<std::shared_ptr<RecordCmd>>& recordCmdList)
264 {
265     std::lock_guard<std::mutex> lock(recordCmdMutex_);
266     for (const auto &recordCmd : recordCmdList) {
267         recordCmdVec_.emplace_back(recordCmd);
268     }
269     return recordCmdVec_.size();
270 }
271 
GetAllRecordCmd(std::vector<std::shared_ptr<RecordCmd>> & recordCmdList)272 uint32_t CmdList::GetAllRecordCmd(std::vector<std::shared_ptr<RecordCmd>>& recordCmdList)
273 {
274     std::lock_guard<std::mutex> lock(recordCmdMutex_);
275     for (const auto &recordCmd : recordCmdVec_) {
276         recordCmdList.emplace_back(recordCmd);
277     }
278     return recordCmdList.size();
279 }
280 
AddImageObject(const std::shared_ptr<ExtendImageObject> & object)281 uint32_t CmdList::AddImageObject(const std::shared_ptr<ExtendImageObject>& object)
282 {
283     std::lock_guard<std::mutex> lock(imageObjectMutex_);
284     imageObjectVec_.emplace_back(object);
285     return static_cast<uint32_t>(imageObjectVec_.size()) - 1;
286 }
287 
GetImageObject(uint32_t id)288 std::shared_ptr<ExtendImageObject> CmdList::GetImageObject(uint32_t id)
289 {
290     std::lock_guard<std::mutex> lock(imageObjectMutex_);
291     if (id >= imageObjectVec_.size()) {
292         return nullptr;
293     }
294     return imageObjectVec_[id];
295 }
296 
GetAllObject(std::vector<std::shared_ptr<ExtendImageObject>> & objectList)297 uint32_t CmdList::GetAllObject(std::vector<std::shared_ptr<ExtendImageObject>>& objectList)
298 {
299     std::lock_guard<std::mutex> lock(imageObjectMutex_);
300     for (const auto &object : imageObjectVec_) {
301         objectList.emplace_back(object);
302     }
303     return objectList.size();
304 }
305 
SetupObject(const std::vector<std::shared_ptr<ExtendImageObject>> & objectList)306 uint32_t CmdList::SetupObject(const std::vector<std::shared_ptr<ExtendImageObject>>& objectList)
307 {
308     std::lock_guard<std::mutex> lock(imageObjectMutex_);
309     for (const auto &object : objectList) {
310         imageObjectVec_.emplace_back(object);
311     }
312     return imageObjectVec_.size();
313 }
314 
AddImageBaseObj(const std::shared_ptr<ExtendImageBaseObj> & object)315 uint32_t CmdList::AddImageBaseObj(const std::shared_ptr<ExtendImageBaseObj>& object)
316 {
317     std::lock_guard<std::mutex> lock(imageBaseObjMutex_);
318     imageBaseObjVec_.emplace_back(object);
319     return static_cast<uint32_t>(imageBaseObjVec_.size()) - 1;
320 }
321 
GetImageBaseObj(uint32_t id)322 std::shared_ptr<ExtendImageBaseObj> CmdList::GetImageBaseObj(uint32_t id)
323 {
324     std::lock_guard<std::mutex> lock(imageBaseObjMutex_);
325     if (id >= imageBaseObjVec_.size()) {
326         return nullptr;
327     }
328     return imageBaseObjVec_[id];
329 }
330 
GetAllBaseObj(std::vector<std::shared_ptr<ExtendImageBaseObj>> & objectList)331 uint32_t CmdList::GetAllBaseObj(std::vector<std::shared_ptr<ExtendImageBaseObj>>& objectList)
332 {
333     std::lock_guard<std::mutex> lock(imageBaseObjMutex_);
334     for (const auto &object : imageBaseObjVec_) {
335         objectList.emplace_back(object);
336     }
337     return objectList.size();
338 }
339 
SetupBaseObj(const std::vector<std::shared_ptr<ExtendImageBaseObj>> & objectList)340 uint32_t CmdList::SetupBaseObj(const std::vector<std::shared_ptr<ExtendImageBaseObj>>& objectList)
341 {
342     std::lock_guard<std::mutex> lock(imageBaseObjMutex_);
343     for (const auto &object : objectList) {
344         imageBaseObjVec_.emplace_back(object);
345     }
346     return imageBaseObjVec_.size();
347 }
348 
AddImageNineObject(const std::shared_ptr<ExtendImageNineObject> & object)349 uint32_t CmdList::AddImageNineObject(const std::shared_ptr<ExtendImageNineObject>& object)
350 {
351     std::lock_guard<std::mutex> lock(imageNineObjectMutex_);
352     imageNineObjectVec_.emplace_back(object);
353     return static_cast<uint32_t>(imageNineObjectVec_.size()) - 1;
354 }
355 
GetImageNineObject(uint32_t id)356 std::shared_ptr<ExtendImageNineObject> CmdList::GetImageNineObject(uint32_t id)
357 {
358     std::lock_guard<std::mutex> lock(imageNineObjectMutex_);
359     if (id >= imageNineObjectVec_.size()) {
360         return nullptr;
361     }
362     return imageNineObjectVec_[id];
363 }
364 
GetAllImageNineObject(std::vector<std::shared_ptr<ExtendImageNineObject>> & objectList)365 uint32_t CmdList::GetAllImageNineObject(std::vector<std::shared_ptr<ExtendImageNineObject>>& objectList)
366 {
367     std::lock_guard<std::mutex> lock(imageNineObjectMutex_);
368     for (const auto &object : imageNineObjectVec_) {
369         objectList.emplace_back(object);
370     }
371     return objectList.size();
372 }
373 
SetupImageNineObject(const std::vector<std::shared_ptr<ExtendImageNineObject>> & objectList)374 uint32_t CmdList::SetupImageNineObject(const std::vector<std::shared_ptr<ExtendImageNineObject>>& objectList)
375 {
376     std::lock_guard<std::mutex> lock(imageNineObjectMutex_);
377     for (const auto &object : objectList) {
378         imageNineObjectVec_.emplace_back(object);
379     }
380     return imageNineObjectVec_.size();
381 }
382 
AddImageLatticeObject(const std::shared_ptr<ExtendImageLatticeObject> & object)383 uint32_t CmdList::AddImageLatticeObject(const std::shared_ptr<ExtendImageLatticeObject>& object)
384 {
385     std::lock_guard<std::mutex> lock(imageLatticeObjectMutex_);
386     imageLatticeObjectVec_.emplace_back(object);
387     return static_cast<uint32_t>(imageLatticeObjectVec_.size()) - 1;
388 }
389 
GetImageLatticeObject(uint32_t id)390 std::shared_ptr<ExtendImageLatticeObject> CmdList::GetImageLatticeObject(uint32_t id)
391 {
392     std::lock_guard<std::mutex> lock(imageLatticeObjectMutex_);
393     if (id >= imageLatticeObjectVec_.size()) {
394         return nullptr;
395     }
396     return imageLatticeObjectVec_[id];
397 }
398 
GetAllImageLatticeObject(std::vector<std::shared_ptr<ExtendImageLatticeObject>> & objectList)399 uint32_t CmdList::GetAllImageLatticeObject(std::vector<std::shared_ptr<ExtendImageLatticeObject>>& objectList)
400 {
401     std::lock_guard<std::mutex> lock(imageLatticeObjectMutex_);
402     for (const auto &object : imageLatticeObjectVec_) {
403         objectList.emplace_back(object);
404     }
405     return objectList.size();
406 }
407 
SetupImageLatticeObject(const std::vector<std::shared_ptr<ExtendImageLatticeObject>> & objectList)408 uint32_t CmdList::SetupImageLatticeObject(const std::vector<std::shared_ptr<ExtendImageLatticeObject>>& objectList)
409 {
410     std::lock_guard<std::mutex> lock(imageLatticeObjectMutex_);
411     for (const auto &object : objectList) {
412         imageLatticeObjectVec_.emplace_back(object);
413     }
414     return imageLatticeObjectVec_.size();
415 }
416 
CopyObjectTo(CmdList & other) const417 void CmdList::CopyObjectTo(CmdList& other) const
418 {
419 #ifdef SUPPORT_OHOS_PIXMAP
420     other.imageObjectVec_ = imageObjectVec_;
421 #endif
422     other.imageBaseObjVec_ = imageBaseObjVec_;
423 }
424 
GetOpCnt() const425 uint32_t CmdList::GetOpCnt() const
426 {
427     return opCnt_;
428 }
429 
430 #ifdef ROSEN_OHOS
AddSurfaceBufferEntry(const std::shared_ptr<SurfaceBufferEntry> & surfaceBufferEntry)431 uint32_t CmdList::AddSurfaceBufferEntry(const std::shared_ptr<SurfaceBufferEntry>& surfaceBufferEntry)
432 {
433     std::lock_guard<std::mutex> lock(surfaceBufferEntryMutex_);
434     surfaceBufferEntryVec_.emplace_back(surfaceBufferEntry);
435     return static_cast<uint32_t>(surfaceBufferEntryVec_.size()) - 1;
436 }
437 
GetSurfaceBufferEntry(uint32_t id)438 std::shared_ptr<SurfaceBufferEntry> CmdList::GetSurfaceBufferEntry(uint32_t id)
439 {
440     std::lock_guard<std::mutex> lock(surfaceBufferEntryMutex_);
441     if (id >= surfaceBufferEntryVec_.size()) {
442         return nullptr;
443     }
444     return surfaceBufferEntryVec_[id];
445 }
446 
GetAllSurfaceBufferEntry(std::vector<std::shared_ptr<SurfaceBufferEntry>> & objectList)447 uint32_t CmdList::GetAllSurfaceBufferEntry(std::vector<std::shared_ptr<SurfaceBufferEntry>>& objectList)
448 {
449     std::lock_guard<std::mutex> lock(surfaceBufferEntryMutex_);
450     for (const auto &object : surfaceBufferEntryVec_) {
451         objectList.emplace_back(object);
452     }
453     return static_cast<uint32_t>(objectList.size());
454 }
455 
SetupSurfaceBufferEntry(const std::vector<std::shared_ptr<SurfaceBufferEntry>> & objectList)456 uint32_t CmdList::SetupSurfaceBufferEntry(const std::vector<std::shared_ptr<SurfaceBufferEntry>>& objectList)
457 {
458     std::lock_guard<std::mutex> lock(surfaceBufferEntryMutex_);
459     for (const auto &object : objectList) {
460         surfaceBufferEntryVec_.emplace_back(object);
461     }
462     return static_cast<uint32_t>(surfaceBufferEntryVec_.size());
463 }
464 #endif
465 
AddDrawFuncOjb(const std::shared_ptr<ExtendDrawFuncObj> & object)466 uint32_t CmdList::AddDrawFuncOjb(const std::shared_ptr<ExtendDrawFuncObj> &object)
467 {
468     std::lock_guard<std::mutex> lock(drawFuncObjMutex_);
469     drawFuncObjVec_.emplace_back(object);
470     return static_cast<uint32_t>(drawFuncObjVec_.size()) - 1;
471 }
472 
GetDrawFuncObj(uint32_t id)473 std::shared_ptr<ExtendDrawFuncObj> CmdList::GetDrawFuncObj(uint32_t id)
474 {
475     std::lock_guard<std::mutex> lock(drawFuncObjMutex_);
476     if (id >= drawFuncObjVec_.size()) {
477         return nullptr;
478     }
479     return drawFuncObjVec_[id];
480 }
481 
482 } // namespace Drawing
483 } // namespace Rosen
484 } // namespace OHOS
485