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