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