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