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_helper.h"
17 
18 #include "recording/draw_cmd.h"
19 #include "recording/draw_cmd_list.h"
20 #include "skia_adapter/skia_vertices.h"
21 #include "skia_adapter/skia_image_filter.h"
22 #include "skia_adapter/skia_mask_filter.h"
23 #include "skia_adapter/skia_color_filter.h"
24 #include "skia_adapter/skia_shader_effect.h"
25 #include "skia_adapter/skia_path_effect.h"
26 
27 #include "draw/color.h"
28 #include "draw/core_canvas.h"
29 #include "utils/log.h"
30 #include "utils/rect.h"
31 
32 #include "skia_adapter/skia_path.h"
33 #include "skia_adapter/skia_picture.h"
34 
35 namespace OHOS {
36 namespace Rosen {
37 namespace Drawing {
ColorTypeToBytesPerPixel(ColorType colorType)38 static int ColorTypeToBytesPerPixel(ColorType colorType)
39 {
40     // returns the number of bytes per pixel: 1byte, 2bytes, 4bytes
41     switch (colorType) {
42         case ColorType::COLORTYPE_ALPHA_8:
43             return 1;
44         case ColorType::COLORTYPE_RGB_565:
45         case ColorType::COLORTYPE_ARGB_4444:
46             return 2;
47         case ColorType::COLORTYPE_RGBA_8888:
48         case ColorType::COLORTYPE_BGRA_8888:
49         case ColorType::COLORTYPE_RGB_888X:
50         case ColorType::COLORTYPE_N32:
51             return 4;
52         case ColorType::COLORTYPE_RGBA_F16:
53             return 8;
54         case ColorType::COLORTYPE_UNKNOWN:
55         default:
56             return 0;
57     }
58 }
59 
AddImageToCmdList(CmdList & cmdList,const Image & image)60 OpDataHandle CmdListHelper::AddImageToCmdList(CmdList& cmdList, const Image& image)
61 {
62     return cmdList.AddImage(image);
63 }
64 
AddImageToCmdList(CmdList & cmdList,const std::shared_ptr<Image> & image)65 OpDataHandle CmdListHelper::AddImageToCmdList(CmdList& cmdList, const std::shared_ptr<Image>& image)
66 {
67     if (image == nullptr) {
68         LOGD("image is nullptr!");
69         return { 0 };
70     }
71     return CmdListHelper::AddImageToCmdList(cmdList, *image);
72 }
73 
GetImageFromCmdList(const CmdList & cmdList,const OpDataHandle & opDataHandle)74 std::shared_ptr<Image> CmdListHelper::GetImageFromCmdList(const CmdList& cmdList, const OpDataHandle& opDataHandle)
75 {
76     return (const_cast<CmdList&>(cmdList)).GetImage(opDataHandle);
77 }
78 
AddVerticesToCmdList(CmdList & cmdList,const Vertices & vertices)79 OpDataHandle CmdListHelper::AddVerticesToCmdList(CmdList& cmdList, const Vertices& vertices)
80 {
81     auto data = vertices.Serialize();
82     if (data == nullptr || data->GetSize() == 0) {
83         LOGD("vertices is valid!");
84         return { 0 };
85     }
86 
87     auto offset = cmdList.AddImageData(data->GetData(), data->GetSize());
88     return { offset, data->GetSize() };
89 }
90 
GetVerticesFromCmdList(const CmdList & cmdList,const OpDataHandle & opDataHandle)91 std::shared_ptr<Vertices> CmdListHelper::GetVerticesFromCmdList(
92     const CmdList& cmdList, const OpDataHandle& opDataHandle)
93 {
94     if (opDataHandle.size == 0) {
95         return nullptr;
96     }
97 
98     const void* ptr = cmdList.GetImageData(opDataHandle.offset, opDataHandle.size);
99     if (ptr == nullptr) {
100         LOGD("get vertices data failed!");
101         return nullptr;
102     }
103 
104     auto verticesData = std::make_shared<Data>();
105     verticesData->BuildWithoutCopy(ptr, opDataHandle.size);
106     auto vertices = std::make_shared<Vertices>();
107     if (vertices->Deserialize(verticesData) == false) {
108         LOGD("vertices deserialize failed!");
109         return nullptr;
110     }
111     return vertices;
112 }
113 
AddBitmapToCmdList(CmdList & cmdList,const Bitmap & bitmap)114 ImageHandle CmdListHelper::AddBitmapToCmdList(CmdList& cmdList, const Bitmap& bitmap)
115 {
116     auto format = bitmap.GetFormat();
117     auto bpp = ColorTypeToBytesPerPixel(format.colorType);
118     auto bitmapSize = bitmap.GetHeight() * bitmap.GetWidth() * bpp;
119     if (bitmapSize == 0) {
120         LOGD("bitmap is valid!");
121         return { 0 };
122     }
123 
124     auto offset = cmdList.AddBitmapData(bitmap.GetPixels(), bitmapSize);
125     return { offset, bitmapSize, bitmap.GetWidth(), bitmap.GetHeight(), format.colorType, format.alphaType };
126 }
127 
GetBitmapFromCmdList(const CmdList & cmdList,const ImageHandle & bitmapHandle)128 std::shared_ptr<Bitmap> CmdListHelper::GetBitmapFromCmdList(const CmdList& cmdList, const ImageHandle& bitmapHandle)
129 {
130     if (bitmapHandle.size == 0) {
131         return nullptr;
132     }
133 
134     const void* ptr = cmdList.GetBitmapData(bitmapHandle.offset, bitmapHandle.size);
135     if (ptr == nullptr) {
136         LOGD("get bitmap data failed!");
137         return nullptr;
138     }
139 
140     BitmapFormat format = { bitmapHandle.colorType, bitmapHandle.alphaType };
141     auto bitmap = std::make_shared<Bitmap>();
142     bitmap->Build(bitmapHandle.width, bitmapHandle.height, format);
143     bitmap->SetPixels(const_cast<void*>(ptr));
144 
145     return bitmap;
146 }
147 
AddRecordCmdToCmdList(CmdList & cmdList,const std::shared_ptr<RecordCmd> & recordCmd)148 OpDataHandle DRAWING_API CmdListHelper::AddRecordCmdToCmdList(
149     CmdList& cmdList, const std::shared_ptr<RecordCmd>& recordCmd)
150 {
151     auto index = cmdList.AddRecordCmd(recordCmd);
152     return { index };
153 }
154 
GetRecordCmdFromCmdList(const CmdList & cmdList,const OpDataHandle & recordCmdHandle)155 std::shared_ptr<RecordCmd> CmdListHelper::GetRecordCmdFromCmdList(
156     const CmdList& cmdList, const OpDataHandle& recordCmdHandle)
157 {
158     return (const_cast<CmdList&>(cmdList)).GetRecordCmd(recordCmdHandle.offset);
159 }
160 
AddImageObjectToCmdList(CmdList & cmdList,const std::shared_ptr<ExtendImageObject> & object)161 OpDataHandle CmdListHelper::AddImageObjectToCmdList(CmdList& cmdList, const std::shared_ptr<ExtendImageObject>& object)
162 {
163     auto index = cmdList.AddImageObject(object);
164     return { index };
165 }
166 
GetImageObjectFromCmdList(const CmdList & cmdList,const OpDataHandle & objectHandle)167 std::shared_ptr<ExtendImageObject> CmdListHelper::GetImageObjectFromCmdList(
168     const CmdList& cmdList, const OpDataHandle& objectHandle)
169 {
170     return (const_cast<CmdList&>(cmdList)).GetImageObject(objectHandle.offset);
171 }
172 
AddImageBaseObjToCmdList(CmdList & cmdList,const std::shared_ptr<ExtendImageBaseObj> & object)173 OpDataHandle CmdListHelper::AddImageBaseObjToCmdList(
174     CmdList& cmdList, const std::shared_ptr<ExtendImageBaseObj>& object)
175 {
176     auto index = cmdList.AddImageBaseObj(object);
177     return { index };
178 }
179 
GetImageBaseObjFromCmdList(const CmdList & cmdList,const OpDataHandle & objectHandle)180 std::shared_ptr<ExtendImageBaseObj> CmdListHelper::GetImageBaseObjFromCmdList(
181     const CmdList& cmdList, const OpDataHandle& objectHandle)
182 {
183     return (const_cast<CmdList&>(cmdList)).GetImageBaseObj(objectHandle.offset);
184 }
185 
AddPictureToCmdList(CmdList & cmdList,const Picture & picture)186 OpDataHandle CmdListHelper::AddPictureToCmdList(CmdList& cmdList, const Picture& picture)
187 {
188     auto data = picture.Serialize();
189     if (data == nullptr || data->GetSize() == 0) {
190         LOGD("picture is valid!");
191         return { 0 };
192     }
193 
194     auto offset = cmdList.AddImageData(data->GetData(), data->GetSize());
195     return { offset, data->GetSize() };
196 }
197 
GetPictureFromCmdList(const CmdList & cmdList,const OpDataHandle & pictureHandle)198 std::shared_ptr<Picture> CmdListHelper::GetPictureFromCmdList(const CmdList& cmdList, const OpDataHandle& pictureHandle)
199 {
200     if (pictureHandle.size == 0) {
201         return nullptr;
202     }
203 
204     const void* ptr = cmdList.GetImageData(pictureHandle.offset, pictureHandle.size);
205     if (ptr == nullptr) {
206         LOGD("get picture data failed!");
207         return nullptr;
208     }
209 
210     auto pictureData = std::make_shared<Data>();
211     pictureData->BuildWithoutCopy(ptr, pictureHandle.size);
212     auto picture = std::make_shared<Picture>();
213     if (picture->Deserialize(pictureData) == false) {
214         LOGD("picture deserialize failed!");
215         return nullptr;
216     }
217     return picture;
218 }
219 
AddCompressDataToCmdList(CmdList & cmdList,const std::shared_ptr<Data> & data)220 OpDataHandle CmdListHelper::AddCompressDataToCmdList(CmdList& cmdList, const std::shared_ptr<Data>& data)
221 {
222     if (data == nullptr || data->GetSize() == 0) {
223         LOGD("data is valid!");
224         return { 0 };
225     }
226 
227     auto offset = cmdList.AddImageData(data->GetData(), data->GetSize());
228     return { offset, data->GetSize() };
229 }
230 
GetCompressDataFromCmdList(const CmdList & cmdList,const OpDataHandle & imageHandle)231 std::shared_ptr<Data> CmdListHelper::GetCompressDataFromCmdList(const CmdList& cmdList, const OpDataHandle& imageHandle)
232 {
233     if (imageHandle.size == 0) {
234         return nullptr;
235     }
236 
237     const void* ptr = cmdList.GetImageData(imageHandle.offset, imageHandle.size);
238     if (ptr == nullptr) {
239         LOGD("get image data failed!");
240         return nullptr;
241     }
242 
243     auto imageData = std::make_shared<Data>();
244     imageData->BuildWithoutCopy(ptr, imageHandle.size);
245     return imageData;
246 }
247 
AddChildToCmdList(CmdList & cmdList,const std::shared_ptr<CmdList> & child)248 CmdListHandle CmdListHelper::AddChildToCmdList(CmdList& cmdList, const std::shared_ptr<CmdList>& child)
249 {
250     CmdListHandle childHandle = { 0 };
251 
252     if (child == nullptr) {
253         LOGD("child is invalid!");
254         return childHandle;
255     }
256 
257     childHandle.type = child->GetType();
258 
259     auto childData = child->GetData();
260     if (childData.first != nullptr && childData.second != 0) {
261         childHandle.offset = cmdList.AddCmdListData(childData);
262         childHandle.size = childData.second;
263     } else {
264         return childHandle;
265     }
266 
267     auto childImageData = child->GetAllImageData();
268     if (childImageData.first != nullptr && childImageData.second != 0) {
269         childHandle.imageOffset = cmdList.AddImageData(childImageData.first, childImageData.second);
270         childHandle.imageSize = childImageData.second;
271     }
272 
273     auto childBitmapData = child->GetAllBitmapData();
274     if (childBitmapData.first != nullptr && childBitmapData.second != 0) {
275         childHandle.bitmapOffset = cmdList.AddBitmapData(childBitmapData.first, childBitmapData.second);
276         childHandle.bitmapSize = childBitmapData.second;
277     }
278 
279     return childHandle;
280 }
281 
AddLatticeToCmdList(CmdList & cmdList,const Lattice & lattice)282 LatticeHandle CmdListHelper::AddLatticeToCmdList(CmdList& cmdList, const Lattice& lattice)
283 {
284     LatticeHandle latticeHandle;
285     latticeHandle.fXDivs = AddVectorToCmdList<int>(cmdList, lattice.fXDivs);
286     latticeHandle.fYDivs = AddVectorToCmdList<int>(cmdList, lattice.fYDivs);
287     latticeHandle.fRectTypes = AddVectorToCmdList<Lattice::RectType>(cmdList, lattice.fRectTypes);
288     latticeHandle.fXCount = lattice.fXCount;
289     latticeHandle.fYCount = lattice.fYCount;
290     latticeHandle.fBounds = AddVectorToCmdList<RectI>(cmdList, lattice.fBounds);
291     latticeHandle.fColors = AddVectorToCmdList<Color>(cmdList, lattice.fColors);
292     return latticeHandle;
293 }
294 
GetLatticeFromCmdList(const CmdList & cmdList,const LatticeHandle & latticeHandle)295 Lattice CmdListHelper::GetLatticeFromCmdList(const CmdList& cmdList, const LatticeHandle& latticeHandle)
296 {
297     Lattice lattice;
298     lattice.fXDivs = GetVectorFromCmdList<int>(cmdList, latticeHandle.fXDivs);
299     lattice.fYDivs = GetVectorFromCmdList<int>(cmdList, latticeHandle.fYDivs);
300     lattice.fRectTypes = GetVectorFromCmdList<Lattice::RectType>(cmdList, latticeHandle.fRectTypes);
301     lattice.fXCount = latticeHandle.fXCount;
302     lattice.fYCount = latticeHandle.fYCount;
303     lattice.fBounds = GetVectorFromCmdList<RectI>(cmdList, latticeHandle.fBounds);
304     lattice.fColors = GetVectorFromCmdList<Color>(cmdList, latticeHandle.fColors);
305     return lattice;
306 }
307 
AddSymbolToCmdList(CmdList & cmdList,const DrawingHMSymbolData & symbol)308 SymbolOpHandle CmdListHelper::AddSymbolToCmdList(CmdList& cmdList, const DrawingHMSymbolData& symbol)
309 {
310     auto symbolLayersHandle = AddSymbolLayersToCmdList(cmdList, symbol.symbolInfo_);
311     auto pathHandle = AddPathToCmdList(cmdList, symbol.path_);
312     return {symbolLayersHandle, pathHandle, symbol.symbolId};
313 }
314 
GetSymbolFromCmdList(const CmdList & cmdList,const SymbolOpHandle & symbolHandle)315 DrawingHMSymbolData CmdListHelper::GetSymbolFromCmdList(const CmdList& cmdList,
316     const SymbolOpHandle& symbolHandle)
317 {
318     DrawingHMSymbolData symbol;
319 
320     symbol.symbolInfo_ = GetSymbolLayersFromCmdList(cmdList, symbolHandle.symbolLayerHandle);
321 
322     auto path = GetPathFromCmdList(cmdList, symbolHandle.pathHandle);
323     if (path != nullptr) {
324         symbol.path_ = *path;
325     }
326     symbol.symbolId = symbolHandle.symbolId;
327     return symbol;
328 }
329 
AddSymbolLayersToCmdList(CmdList & cmdList,const DrawingSymbolLayers & symbolLayers)330 SymbolLayersHandle CmdListHelper::AddSymbolLayersToCmdList(CmdList& cmdList, const DrawingSymbolLayers& symbolLayers)
331 {
332     auto layers = symbolLayers.layers;
333     std::vector<std::pair<size_t, size_t>> handleVector1;
334     for (size_t i = 0; i < layers.size(); i++) {
335         handleVector1.push_back(AddVectorToCmdList(cmdList, layers.at(i)));
336     }
337     std::pair<size_t, size_t> layersHandle = AddVectorToCmdList(cmdList, handleVector1);
338 
339     auto groups = symbolLayers.renderGroups;
340     std::vector<RenderGroupHandle> handleVector2;
341     for (size_t i = 0; i < groups.size(); i++) {
342         handleVector2.push_back(AddRenderGroupToCmdList(cmdList, groups.at(i)));
343     }
344     std::pair<size_t, size_t> groupsHandle = AddVectorToCmdList(cmdList, handleVector2);
345 
346     return { symbolLayers.symbolGlyphId, layersHandle, groupsHandle};
347 }
348 
GetSymbolLayersFromCmdList(const CmdList & cmdList,const SymbolLayersHandle & symbolLayersHandle)349 DrawingSymbolLayers CmdListHelper::GetSymbolLayersFromCmdList(const CmdList& cmdList,
350     const SymbolLayersHandle& symbolLayersHandle)
351 {
352     DrawingSymbolLayers symbolLayers;
353     symbolLayers.symbolGlyphId = symbolLayersHandle.id;
354 
355     auto handleVector1 = GetVectorFromCmdList<std::pair<size_t, size_t>>(cmdList, symbolLayersHandle.layers);
356     std::vector<std::vector<size_t>> layers;
357     for (size_t i = 0; i < handleVector1.size(); i++) {
358         layers.push_back(GetVectorFromCmdList<size_t>(cmdList, handleVector1.at(i)));
359     }
360     symbolLayers.layers = layers;
361 
362     auto handleVector2 = GetVectorFromCmdList<RenderGroupHandle>(cmdList, symbolLayersHandle.groups);
363     std::vector<DrawingRenderGroup> renderGroups;
364     for (size_t i = 0; i < handleVector2.size(); i++) {
365         renderGroups.push_back(GetRenderGroupFromCmdList(cmdList, handleVector2.at(i)));
366     }
367     symbolLayers.renderGroups = renderGroups;
368 
369     return symbolLayers;
370 }
371 
AddRenderGroupToCmdList(CmdList & cmdList,const DrawingRenderGroup & group)372 RenderGroupHandle CmdListHelper::AddRenderGroupToCmdList(CmdList& cmdList, const DrawingRenderGroup& group)
373 {
374     auto infos = group.groupInfos;
375     std::vector<GroupInfoHandle> handleVector;
376     for (size_t i = 0; i < infos.size(); i++) {
377         handleVector.push_back(AddGroupInfoToCmdList(cmdList, infos.at(i)));
378     }
379     std::pair<size_t, size_t> groupInfosHandle = AddVectorToCmdList(cmdList, handleVector);
380     return { groupInfosHandle, group.color };
381 }
382 
GetRenderGroupFromCmdList(const CmdList & cmdList,const RenderGroupHandle & renderGroupHandle)383 DrawingRenderGroup CmdListHelper::GetRenderGroupFromCmdList(const CmdList& cmdList,
384     const RenderGroupHandle& renderGroupHandle)
385 {
386     DrawingRenderGroup group;
387     group.color = renderGroupHandle.color;
388 
389     auto handleVector = GetVectorFromCmdList<GroupInfoHandle>(cmdList, renderGroupHandle.infos);
390     std::vector<DrawingGroupInfo> groupInfos;
391     for (size_t i = 0; i < handleVector.size(); i++) {
392         groupInfos.push_back(GetGroupInfoFromCmdList(cmdList, handleVector.at(i)));
393     }
394     group.groupInfos = groupInfos;
395 
396     return group;
397 }
398 
AddGroupInfoToCmdList(CmdList & cmdList,const DrawingGroupInfo & groupInfo)399 GroupInfoHandle CmdListHelper::AddGroupInfoToCmdList(CmdList& cmdList, const DrawingGroupInfo& groupInfo)
400 {
401     std::pair<size_t, size_t> layerIndexes = AddVectorToCmdList(cmdList, groupInfo.layerIndexes);
402     std::pair<size_t, size_t> maskIndexes = AddVectorToCmdList(cmdList, groupInfo.maskIndexes);
403     return { layerIndexes, maskIndexes };
404 }
405 
GetGroupInfoFromCmdList(const CmdList & cmdList,const GroupInfoHandle & groupInfoHandle)406 DrawingGroupInfo CmdListHelper::GetGroupInfoFromCmdList(const CmdList& cmdList, const GroupInfoHandle& groupInfoHandle)
407 {
408     DrawingGroupInfo groupInfo;
409     groupInfo.layerIndexes = GetVectorFromCmdList<size_t>(cmdList, groupInfoHandle.vec1);
410     groupInfo.maskIndexes = GetVectorFromCmdList<size_t>(cmdList, groupInfoHandle.vec2);
411     return groupInfo;
412 }
413 
AddTextBlobToCmdList(CmdList & cmdList,const TextBlob * textBlob,void * ctx)414 OpDataHandle CmdListHelper::AddTextBlobToCmdList(CmdList& cmdList, const TextBlob* textBlob, void* ctx)
415 {
416     if (!textBlob) {
417         return { 0 };
418     }
419     auto data = textBlob->Serialize(ctx);
420     if (!data || data->GetSize() == 0) {
421         LOGD("textBlob serialize invalid, %{public}s, %{public}d", __FUNCTION__, __LINE__);
422         return { 0 };
423     }
424 
425     auto offset = cmdList.AddImageData(data->GetData(), data->GetSize());
426     return { offset, data->GetSize() };
427 }
428 
GetTextBlobFromCmdList(const CmdList & cmdList,const OpDataHandle & textBlobHandle,uint64_t globalUniqueId)429 std::shared_ptr<TextBlob> CmdListHelper::GetTextBlobFromCmdList(const CmdList& cmdList,
430     const OpDataHandle& textBlobHandle, uint64_t globalUniqueId)
431 {
432     if (textBlobHandle.size == 0) {
433         return nullptr;
434     }
435 
436     std::shared_ptr<Drawing::Typeface> typeface = nullptr;
437     if (DrawOpItem::customTypefaceQueryfunc_) {
438         typeface = DrawOpItem::customTypefaceQueryfunc_(globalUniqueId);
439     }
440     TextBlob::Context customCtx {typeface, false};
441 
442     const void* data = cmdList.GetImageData(textBlobHandle.offset, textBlobHandle.size);
443     if (!data) {
444         LOGD("textBlob data nullptr, %{public}s, %{public}d", __FUNCTION__, __LINE__);
445         return nullptr;
446     }
447 
448     auto textBlobData = std::make_shared<Data>();
449     textBlobData->BuildWithoutCopy(data, textBlobHandle.size);
450     return TextBlob::Deserialize(textBlobData->GetData(), textBlobData->GetSize(), &customCtx);
451 }
452 
AddDataToCmdList(CmdList & cmdList,const Data * srcData)453 OpDataHandle CmdListHelper::AddDataToCmdList(CmdList& cmdList, const Data* srcData)
454 {
455     if (!srcData) {
456         LOGD("data nullptr, %{public}s, %{public}d", __FUNCTION__, __LINE__);
457         return { 0 };
458     }
459 
460     auto data = srcData->Serialize();
461     if (data == nullptr || data->GetSize() == 0) {
462         LOGD("srcData is invalid!");
463         return { 0 };
464     }
465 
466     auto offset = cmdList.AddImageData(data->GetData(), data->GetSize());
467     return { offset, data->GetSize() };
468 }
469 
GetDataFromCmdList(const CmdList & cmdList,const OpDataHandle & imageHandle)470 std::shared_ptr<Data> CmdListHelper::GetDataFromCmdList(const CmdList& cmdList, const OpDataHandle& imageHandle)
471 {
472     if (imageHandle.size == 0) {
473         return nullptr;
474     }
475 
476     const void* ptr = cmdList.GetImageData(imageHandle.offset, imageHandle.size);
477     if (ptr == nullptr) {
478         LOGD("get data failed!");
479         return nullptr;
480     }
481 
482     auto imageData = std::make_shared<Data>();
483     imageData->BuildWithoutCopy(ptr, imageHandle.size);
484     return imageData;
485 }
486 
AddPathToCmdList(CmdList & cmdList,const Path & path)487 OpDataHandle CmdListHelper::AddPathToCmdList(CmdList& cmdList, const Path& path)
488 {
489     auto data = path.Serialize();
490     if (data == nullptr || data->GetSize() == 0) {
491         LOGE("path is invalid, %{public}s, %{public}d", __FUNCTION__, __LINE__);
492         return { 0 };
493     }
494 
495     auto offset = cmdList.AddImageData(data->GetData(), data->GetSize());
496     return { offset, data->GetSize() };
497 }
498 
GetPathFromCmdList(const CmdList & cmdList,const OpDataHandle & pathHandle)499 std::shared_ptr<Path> CmdListHelper::GetPathFromCmdList(const CmdList& cmdList,
500     const OpDataHandle& pathHandle)
501 {
502     if (pathHandle.size == 0) {
503         LOGE("pathHandle is invalid!");
504         return nullptr;
505     }
506 
507     const void* ptr = cmdList.GetImageData(pathHandle.offset, pathHandle.size);
508     if (ptr == nullptr) {
509         LOGE("get path data failed!");
510         return nullptr;
511     }
512 
513     auto pathData = std::make_shared<Data>();
514     pathData->BuildWithoutCopy(ptr, pathHandle.size);
515     auto path = std::make_shared<Path>();
516     if (path->Deserialize(pathData) == false) {
517         LOGE("path deserialize failed!");
518         return nullptr;
519     }
520 
521     return path;
522 }
523 
AddRegionToCmdList(CmdList & cmdList,const Region & region)524 OpDataHandle CmdListHelper::AddRegionToCmdList(CmdList& cmdList, const Region& region)
525 {
526     auto data = region.Serialize();
527     if (data == nullptr || data->GetSize() == 0) {
528         LOGD("region is invalid, %{public}s, %{public}d", __FUNCTION__, __LINE__);
529         return { 0 };
530     }
531 
532     auto offset = cmdList.AddImageData(data->GetData(), data->GetSize());
533     return { offset, data->GetSize() };
534 }
535 
GetRegionFromCmdList(const CmdList & cmdList,const OpDataHandle & regionHandle)536 std::shared_ptr<Region> CmdListHelper::GetRegionFromCmdList(const CmdList& cmdList, const OpDataHandle& regionHandle)
537 {
538     if (regionHandle.size == 0) {
539         return nullptr;
540     }
541 
542     const void* ptr = cmdList.GetImageData(regionHandle.offset, regionHandle.size);
543     if (ptr == nullptr) {
544         LOGD("get region data failed!");
545         return nullptr;
546     }
547 
548     auto regionData = std::make_shared<Data>();
549     regionData->BuildWithoutCopy(ptr, regionHandle.size);
550     auto region = std::make_shared<Region>();
551     if (region->Deserialize(regionData) == false) {
552         LOGD("region deserialize failed!");
553         return nullptr;
554     }
555 
556     return region;
557 }
558 
AddColorSpaceToCmdList(CmdList & cmdList,const std::shared_ptr<ColorSpace> colorSpace)559 OpDataHandle CmdListHelper::AddColorSpaceToCmdList(CmdList& cmdList, const std::shared_ptr<ColorSpace> colorSpace)
560 {
561     if (colorSpace == nullptr) {
562         return { 0 };
563     }
564 
565     auto data = colorSpace->Serialize();
566     if (data == nullptr || data->GetSize() == 0) {
567         LOGD("colorSpace is invalid, %{public}s, %{public}d", __FUNCTION__, __LINE__);
568         return { 0 };
569     }
570 
571     auto offset = cmdList.AddImageData(data->GetData(), data->GetSize());
572     return { offset, data->GetSize() };
573 }
574 
GetColorSpaceFromCmdList(const CmdList & cmdList,const OpDataHandle & imageHandle)575 std::shared_ptr<ColorSpace> CmdListHelper::GetColorSpaceFromCmdList(const CmdList& cmdList,
576     const OpDataHandle& imageHandle)
577 {
578     if (imageHandle.size == 0) {
579         return nullptr;
580     }
581 
582     const void* ptr = cmdList.GetImageData(imageHandle.offset, imageHandle.size);
583     if (ptr == nullptr) {
584         return nullptr;
585     }
586     auto colorSpaceData = std::make_shared<Data>();
587     colorSpaceData->BuildWithoutCopy(ptr, imageHandle.size);
588     auto colorSpace = std::make_shared<ColorSpace>(ColorSpace::ColorSpaceType::REF_IMAGE);
589     if (colorSpace->Deserialize(colorSpaceData) == false) {
590         LOGD("colorSpace deserialize failed!");
591         return nullptr;
592     }
593 
594     return colorSpace;
595 }
596 
AddShaderEffectToCmdList(CmdList & cmdList,std::shared_ptr<ShaderEffect> shaderEffect)597 FlattenableHandle CmdListHelper::AddShaderEffectToCmdList(CmdList& cmdList, std::shared_ptr<ShaderEffect> shaderEffect)
598 {
599     if (shaderEffect == nullptr) {
600         return { 0 };
601     }
602     ShaderEffect::ShaderEffectType type = shaderEffect->GetType();
603     if (type == ShaderEffect::ShaderEffectType::EXTEND_SHADER) {
604         std::shared_ptr<ExtendObject> object = shaderEffect->GetExtendObject();
605         if (!object) {
606             return { 0 };
607         }
608         uint32_t offset = AddExtendObjectToCmdList(cmdList, object);
609         return { offset, 1, static_cast<uint32_t>(type) };
610     }
611     auto data = shaderEffect->Serialize();
612     if (data == nullptr || data->GetSize() == 0) {
613         LOGD("shaderEffect is invalid, %{public}s, %{public}d", __FUNCTION__, __LINE__);
614         return { 0 };
615     }
616     auto offset = cmdList.AddImageData(data->GetData(), data->GetSize());
617     return { offset, data->GetSize(), static_cast<uint32_t>(type) };
618 }
619 
GetShaderEffectFromCmdList(const CmdList & cmdList,const FlattenableHandle & shaderEffectHandle)620 std::shared_ptr<ShaderEffect> CmdListHelper::GetShaderEffectFromCmdList(const CmdList& cmdList,
621     const FlattenableHandle& shaderEffectHandle)
622 {
623     if (shaderEffectHandle.size == 0) {
624         return nullptr;
625     }
626     ShaderEffect::ShaderEffectType type = static_cast<ShaderEffect::ShaderEffectType>(shaderEffectHandle.type);
627     if (type == ShaderEffect::ShaderEffectType::EXTEND_SHADER) {
628         std::shared_ptr<ExtendObject> object = GetExtendObjectFromCmdList(cmdList, shaderEffectHandle.offset);
629         if (!object) {
630             return nullptr;
631         }
632         void* baseObject = object->GenerateBaseObject();
633         if (!baseObject) {
634             return nullptr;
635         }
636         std::shared_ptr<ShaderEffect> shaderEffect;
637         shaderEffect.reset(reinterpret_cast<ShaderEffect*>(baseObject));
638         return shaderEffect;
639     }
640 
641     const void* ptr = cmdList.GetImageData(shaderEffectHandle.offset, shaderEffectHandle.size);
642     if (ptr == nullptr) {
643         return nullptr;
644     }
645 
646     auto shaderEffectData = std::make_shared<Data>();
647     shaderEffectData->BuildWithoutCopy(ptr, shaderEffectHandle.size);
648     auto shaderEffect = std::make_shared<ShaderEffect>(type);
649     if (shaderEffect->Deserialize(shaderEffectData) == false) {
650         LOGD("shaderEffect deserialize failed!");
651         return nullptr;
652     }
653 
654     return shaderEffect;
655 }
656 
AddPathEffectToCmdList(CmdList & cmdList,std::shared_ptr<PathEffect> pathEffect)657 FlattenableHandle CmdListHelper::AddPathEffectToCmdList(CmdList& cmdList, std::shared_ptr<PathEffect> pathEffect)
658 {
659     if (pathEffect == nullptr) {
660         return { 0 };
661     }
662     PathEffect::PathEffectType type = pathEffect->GetType();
663     auto data = pathEffect->Serialize();
664     if (data == nullptr || data->GetSize() == 0) {
665         LOGD("pathEffect is invalid, %{public}s, %{public}d", __FUNCTION__, __LINE__);
666         return { 0 };
667     }
668     auto offset = cmdList.AddImageData(data->GetData(), data->GetSize());
669     return { offset, data->GetSize(), static_cast<uint32_t>(type) };
670 }
671 
GetPathEffectFromCmdList(const CmdList & cmdList,const FlattenableHandle & pathEffectHandle)672 std::shared_ptr<PathEffect> CmdListHelper::GetPathEffectFromCmdList(const CmdList& cmdList,
673     const FlattenableHandle& pathEffectHandle)
674 {
675     if (pathEffectHandle.size == 0) {
676         return nullptr;
677     }
678 
679     const void* ptr = cmdList.GetImageData(pathEffectHandle.offset, pathEffectHandle.size);
680     if (ptr == nullptr) {
681         return nullptr;
682     }
683 
684     auto pathEffectData = std::make_shared<Data>();
685     pathEffectData->BuildWithoutCopy(ptr, pathEffectHandle.size);
686     auto pathEffect = std::make_shared<PathEffect>
687         (static_cast<PathEffect::PathEffectType>(pathEffectHandle.type));
688     if (pathEffect->Deserialize(pathEffectData) == false) {
689         LOGD("pathEffect deserialize failed!");
690         return nullptr;
691     }
692 
693     return pathEffect;
694 }
695 
AddMaskFilterToCmdList(CmdList & cmdList,std::shared_ptr<MaskFilter> maskFilter)696 FlattenableHandle CmdListHelper::AddMaskFilterToCmdList(CmdList& cmdList, std::shared_ptr<MaskFilter> maskFilter)
697 {
698     if (maskFilter == nullptr) {
699         return { 0 };
700     }
701     MaskFilter::FilterType type = maskFilter->GetType();
702     auto data = maskFilter->Serialize();
703     if (data == nullptr || data->GetSize() == 0) {
704         return { 0 };
705     }
706     auto offset = cmdList.AddImageData(data->GetData(), data->GetSize());
707     return { offset, data->GetSize(), static_cast<uint32_t>(type) };
708 }
709 
GetMaskFilterFromCmdList(const CmdList & cmdList,const FlattenableHandle & maskFilterHandle)710 std::shared_ptr<MaskFilter> CmdListHelper::GetMaskFilterFromCmdList(const CmdList& cmdList,
711     const FlattenableHandle& maskFilterHandle)
712 {
713     if (maskFilterHandle.size == 0) {
714         return nullptr;
715     }
716 
717     const void* ptr = cmdList.GetImageData(maskFilterHandle.offset, maskFilterHandle.size);
718     if (ptr == nullptr) {
719         return nullptr;
720     }
721 
722     auto maskFilterData = std::make_shared<Data>();
723     maskFilterData->BuildWithoutCopy(ptr, maskFilterHandle.size);
724     auto maskFilter = std::make_shared<MaskFilter>
725         (static_cast<MaskFilter::FilterType>(maskFilterHandle.type));
726     if (maskFilter->Deserialize(maskFilterData) == false) {
727         LOGD("maskFilter deserialize failed!");
728         return nullptr;
729     }
730 
731     return maskFilter;
732 }
733 
AddColorFilterToCmdList(CmdList & cmdList,std::shared_ptr<ColorFilter> colorFilter)734 FlattenableHandle CmdListHelper::AddColorFilterToCmdList(CmdList& cmdList, std::shared_ptr<ColorFilter> colorFilter)
735 {
736     if (colorFilter == nullptr) {
737         return { 0 };
738     }
739     ColorFilter::FilterType type = colorFilter->GetType();
740     auto data = colorFilter->Serialize();
741     if (data == nullptr || data->GetSize() == 0) {
742         LOGD("colorFilter is invalid, %{public}s, %{public}d", __FUNCTION__, __LINE__);
743         return { 0 };
744     }
745     auto offset = cmdList.AddImageData(data->GetData(), data->GetSize());
746     return { offset, data->GetSize(), static_cast<uint32_t>(type) };
747 }
748 
GetColorFilterFromCmdList(const CmdList & cmdList,const FlattenableHandle & colorFilterHandle)749 std::shared_ptr<ColorFilter> CmdListHelper::GetColorFilterFromCmdList(const CmdList& cmdList,
750     const FlattenableHandle& colorFilterHandle)
751 {
752     if (colorFilterHandle.size == 0) {
753         return nullptr;
754     }
755 
756     const void* ptr = cmdList.GetImageData(colorFilterHandle.offset, colorFilterHandle.size);
757     if (ptr == nullptr) {
758         return nullptr;
759     }
760 
761     auto colorFilterData = std::make_shared<Data>();
762     colorFilterData->BuildWithoutCopy(ptr, colorFilterHandle.size);
763     auto colorFilter = std::make_shared<ColorFilter>
764         (static_cast<ColorFilter::FilterType>(colorFilterHandle.type));
765     if (colorFilter->Deserialize(colorFilterData) == false) {
766         LOGD("colorFilter deserialize failed!");
767         return nullptr;
768     }
769 
770     return colorFilter;
771 }
772 
AddImageFilterToCmdList(CmdList & cmdList,const ImageFilter * imageFilter)773 FlattenableHandle CmdListHelper::AddImageFilterToCmdList(CmdList& cmdList, const ImageFilter* imageFilter)
774 {
775     if (imageFilter == nullptr) {
776         return { 0 };
777     }
778     ImageFilter::FilterType type = imageFilter->GetType();
779     auto data = imageFilter->Serialize();
780     if (data == nullptr || data->GetSize() == 0) {
781         return { 0 };
782     }
783     auto offset = cmdList.AddImageData(data->GetData(), data->GetSize());
784     return { offset, data->GetSize(), static_cast<uint32_t>(type) };
785 }
786 
AddImageFilterToCmdList(CmdList & cmdList,std::shared_ptr<ImageFilter> imageFilter)787 FlattenableHandle CmdListHelper::AddImageFilterToCmdList(CmdList& cmdList,
788     std::shared_ptr<ImageFilter> imageFilter)
789 {
790     return AddImageFilterToCmdList(cmdList, imageFilter.get());
791 }
792 
GetImageFilterFromCmdList(const CmdList & cmdList,const FlattenableHandle & imageFilterHandle)793 std::shared_ptr<ImageFilter> CmdListHelper::GetImageFilterFromCmdList(const CmdList& cmdList,
794     const FlattenableHandle& imageFilterHandle)
795 {
796     if (imageFilterHandle.size == 0) {
797         return nullptr;
798     }
799 
800     const void* ptr = cmdList.GetImageData(imageFilterHandle.offset, imageFilterHandle.size);
801     if (ptr == nullptr) {
802         return nullptr;
803     }
804 
805     auto imageFilterData = std::make_shared<Data>();
806     imageFilterData->BuildWithoutCopy(ptr, imageFilterHandle.size);
807     auto imageFilter = std::make_shared<ImageFilter>
808         (static_cast<ImageFilter::FilterType>(imageFilterHandle.type));
809     if (imageFilter->Deserialize(imageFilterData) == false) {
810         LOGD("imageFilter deserialize failed!");
811         return nullptr;
812     }
813 
814     return imageFilter;
815 }
816 
AddBlurDrawLooperToCmdList(CmdList & cmdList,std::shared_ptr<BlurDrawLooper> blurDrawLooper)817 OpDataHandle CmdListHelper::AddBlurDrawLooperToCmdList(CmdList& cmdList,
818     std::shared_ptr<BlurDrawLooper> blurDrawLooper)
819 {
820     if (blurDrawLooper == nullptr) {
821         LOGD("blurDrawLooper is nullptr");
822         return { 0 };
823     }
824 
825     auto data = blurDrawLooper->Serialize();
826     if (data == nullptr || data->GetSize() == 0) {
827         LOGD("blurDrawLooper serialize failed!");
828         return { 0 };
829     }
830     auto offset = cmdList.AddImageData(data->GetData(), data->GetSize());
831     return { offset, data->GetSize()};
832 }
833 
GetBlurDrawLooperFromCmdList(const CmdList & cmdList,const OpDataHandle & blurDrawLooperHandle)834 std::shared_ptr<BlurDrawLooper> CmdListHelper::GetBlurDrawLooperFromCmdList(const CmdList& cmdList,
835     const OpDataHandle& blurDrawLooperHandle)
836 {
837     if (blurDrawLooperHandle.size == 0) {
838         return nullptr;
839     }
840 
841     const void* ptr = cmdList.GetImageData(blurDrawLooperHandle.offset, blurDrawLooperHandle.size);
842     if (ptr == nullptr) {
843         return nullptr;
844     }
845 
846     auto blurData = std::make_shared<Data>();
847     blurData->BuildWithoutCopy(ptr, blurDrawLooperHandle.size);
848     return BlurDrawLooper::Deserialize(blurData);
849 }
850 
851 #ifdef ROSEN_OHOS
AddSurfaceBufferEntryToCmdList(CmdList & cmdList,const std::shared_ptr<SurfaceBufferEntry> & surfaceBuffer)852 uint32_t CmdListHelper::AddSurfaceBufferEntryToCmdList(
853     CmdList& cmdList, const std::shared_ptr<SurfaceBufferEntry>& surfaceBuffer)
854 {
855     return cmdList.AddSurfaceBufferEntry(surfaceBuffer);
856 }
857 
GetSurfaceBufferEntryFromCmdList(const CmdList & cmdList,uint32_t surfaceBufferHandle)858 std::shared_ptr<SurfaceBufferEntry> CmdListHelper::GetSurfaceBufferEntryFromCmdList(
859     const CmdList& cmdList, uint32_t surfaceBufferHandle)
860 {
861     return (const_cast<CmdList&>(cmdList)).GetSurfaceBufferEntry(surfaceBufferHandle);
862 }
863 #endif
864 
AddDrawFuncObjToCmdList(CmdList & cmdList,const std::shared_ptr<ExtendDrawFuncObj> & object)865 uint32_t CmdListHelper::AddDrawFuncObjToCmdList(CmdList &cmdList, const std::shared_ptr<ExtendDrawFuncObj> &object)
866 {
867     return cmdList.AddDrawFuncOjb(object);
868 }
869 
GetDrawFuncObjFromCmdList(const CmdList & cmdList,uint32_t objectHandle)870 std::shared_ptr<ExtendDrawFuncObj> CmdListHelper::GetDrawFuncObjFromCmdList(
871     const CmdList& cmdList, uint32_t objectHandle)
872 {
873     return (const_cast<CmdList&>(cmdList)).GetDrawFuncObj(objectHandle);
874 }
875 
AddExtendObjectToCmdList(CmdList & cmdList,std::shared_ptr<ExtendObject> object)876 uint32_t CmdListHelper::AddExtendObjectToCmdList(CmdList& cmdList, std::shared_ptr<ExtendObject> object)
877 {
878     return cmdList.AddExtendObject(object);
879 }
880 
GetExtendObjectFromCmdList(const CmdList & cmdList,uint32_t index)881 std::shared_ptr<ExtendObject> CmdListHelper::GetExtendObjectFromCmdList(const CmdList& cmdList, uint32_t index)
882 {
883     return (const_cast<CmdList&>(cmdList)).GetExtendObject(index);
884 }
885 } // namespace Drawing
886 } // namespace Rosen
887 } // namespace OHOS
888