• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "transaction/rs_marshalling_helper.h"
17 
18 #include <cstdint>
19 #include <memory>
20 #include <message_parcel.h>
21 #include <sys/mman.h>
22 #include <unistd.h>
23 
24 #include "ashmem.h"
25 #ifndef USE_ROSEN_DRAWING
26 #include "include/core/SkDrawable.h"
27 #include "include/core/SkImage.h"
28 #include "include/core/SkMatrix.h"
29 #include "include/core/SkPaint.h"
30 #include "include/core/SkPicture.h"
31 #include "include/core/SkSerialProcs.h"
32 #include "include/core/SkStream.h"
33 #include "include/core/SkTextBlob.h"
34 #include "include/core/SkTypeface.h"
35 #include "include/core/SkVertices.h"
36 #ifdef NEW_SKIA
37 #include "include/core/SkSamplingOptions.h"
38 #include "src/core/SkVerticesPriv.h"
39 #endif
40 #endif
41 #include "memory/rs_memory_track.h"
42 #include "pixel_map.h"
43 #include "securec.h"
44 #ifndef USE_ROSEN_DRAWING
45 #include "src/core/SkAutoMalloc.h"
46 #include "src/core/SkPaintPriv.h"
47 #include "src/core/SkReadBuffer.h"
48 #include "src/core/SkWriteBuffer.h"
49 #include "src/image/SkImage_Base.h"
50 #else
51 #include "recording/recording_path.h"
52 #include "recording/recording_shader_effect.h"
53 #endif
54 
55 #include "animation/rs_render_curve_animation.h"
56 #include "animation/rs_render_interpolating_spring_animation.h"
57 #include "animation/rs_render_keyframe_animation.h"
58 #include "animation/rs_render_particle.h"
59 #include "animation/rs_render_particle_animation.h"
60 #include "animation/rs_render_path_animation.h"
61 #include "animation/rs_render_spring_animation.h"
62 #include "animation/rs_render_transition.h"
63 #include "common/rs_color.h"
64 #include "common/rs_common_def.h"
65 #include "common/rs_matrix3.h"
66 #include "common/rs_vector4.h"
67 #include "modifier/rs_render_modifier.h"
68 #ifndef USE_ROSEN_DRAWING
69 #include "pipeline/rs_draw_cmd.h"
70 #include "pipeline/rs_draw_cmd_list.h"
71 #endif
72 #include "platform/common/rs_log.h"
73 #include "render/rs_blur_filter.h"
74 #include "render/rs_filter.h"
75 #include "render/rs_gradient_blur_para.h"
76 #include "render/rs_image.h"
77 #include "render/rs_image_base.h"
78 #include "render/rs_light_up_effect_filter.h"
79 #include "render/rs_material_filter.h"
80 #include "render/rs_path.h"
81 #include "render/rs_shader.h"
82 #include "transaction/rs_ashmem_helper.h"
83 #ifdef RS_ENABLE_RECORDING
84 #include "benchmarks/rs_recording_thread.h"
85 #endif
86 #if defined (ENABLE_DDGR_OPTIMIZE)
87 #include <sys/mman.h>
88 #include "securec.h"
89 #include "platform/common/rs_system_properties.h"
90 #include "ipc_file_descriptor.h"
91 #include "src/core/SkTextBlobPriv.h"
92 #include "ddgr_renderer.h"
93 #endif
94 
95 namespace OHOS {
96 namespace Rosen {
97 
98 #define MARSHALLING_AND_UNMARSHALLING(TYPE, TYPENAME)                      \
99     bool RSMarshallingHelper::Marshalling(Parcel& parcel, const TYPE& val) \
100     {                                                                      \
101         return parcel.Write##TYPENAME(val);                                \
102     }                                                                      \
103     bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, TYPE& val)     \
104     {                                                                      \
105         return parcel.Read##TYPENAME(val);                                 \
106     }
107 
108 // basic types
109 MARSHALLING_AND_UNMARSHALLING(bool, Bool)
110 MARSHALLING_AND_UNMARSHALLING(int8_t, Int8)
111 MARSHALLING_AND_UNMARSHALLING(uint8_t, Uint8)
112 MARSHALLING_AND_UNMARSHALLING(int16_t, Int16)
113 MARSHALLING_AND_UNMARSHALLING(uint16_t, Uint16)
114 MARSHALLING_AND_UNMARSHALLING(int32_t, Int32)
115 MARSHALLING_AND_UNMARSHALLING(uint32_t, Uint32)
116 MARSHALLING_AND_UNMARSHALLING(int64_t, Int64)
117 MARSHALLING_AND_UNMARSHALLING(uint64_t, Uint64)
118 MARSHALLING_AND_UNMARSHALLING(float, Float)
119 MARSHALLING_AND_UNMARSHALLING(double, Double)
120 
121 #undef MARSHALLING_AND_UNMARSHALLING
122 
123 namespace {
124 template<typename T, typename P>
sk_reinterpret_cast(sk_sp<P> ptr)125 static inline sk_sp<T> sk_reinterpret_cast(sk_sp<P> ptr)
126 {
127     return sk_sp<T>(static_cast<T*>(SkSafeRef(ptr.get())));
128 }
129 } // namespace
130 
131 #ifndef USE_ROSEN_DRAWING
132 // SkData
Marshalling(Parcel & parcel,sk_sp<SkData> val)133 bool RSMarshallingHelper::Marshalling(Parcel& parcel, sk_sp<SkData> val)
134 {
135     if (!val) {
136         return parcel.WriteInt32(-1);
137     }
138 
139     bool ret = parcel.WriteInt32(val->size());
140     if (val->size() == 0) {
141         ROSEN_LOGW("unirender: RSMarshallingHelper::Marshalling SkData size is 0");
142         return ret;
143     }
144 
145     ret = ret && RSMarshallingHelper::WriteToParcel(parcel, val->data(), val->size());
146     if (!ret) {
147         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling SkData");
148     }
149     return ret;
150 }
Unmarshalling(Parcel & parcel,sk_sp<SkData> & val)151 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, sk_sp<SkData>& val)
152 {
153     int32_t size = parcel.ReadInt32();
154     if (size == -1) {
155         val = nullptr;
156         return true;
157     }
158     if (size == 0) {
159         ROSEN_LOGW("unirender: RSMarshallingHelper::Unmarshalling SkData size is 0");
160         val = SkData::MakeEmpty();
161         return true;
162     }
163 
164     const void* data = RSMarshallingHelper::ReadFromParcel(parcel, size);
165     if (data == nullptr) {
166         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling SkData");
167         return false;
168     }
169 #ifdef RS_ENABLE_RECORDING
170     if (static_cast<uint32_t>(size) < MIN_DATA_SIZE ||
171         parcel.GetMaxCapacity() == RSRecordingThread::RECORDING_PARCEL_MAX_CAPCITY) {
172 #else
173     if (static_cast<uint32_t>(size) < MIN_DATA_SIZE) {
174 #endif
175         val = SkData::MakeWithoutCopy(data, size);
176     } else {
177         val = SkData::MakeFromMalloc(data, size);
178     }
179     return val != nullptr;
180 }
181 bool RSMarshallingHelper::SkipSkData(Parcel& parcel)
182 {
183     int32_t size = parcel.ReadInt32();
184     if (size <= 0) {
185         return true;
186     }
187     return SkipFromParcel(parcel, size);
188 }
189 
190 bool RSMarshallingHelper::UnmarshallingWithCopy(Parcel& parcel, sk_sp<SkData>& val)
191 {
192     bool success = Unmarshalling(parcel, val);
193     if (success) {
194 #ifdef RS_ENABLE_RECORDING
195         if (val && (val->size() < MIN_DATA_SIZE ||
196                        parcel.GetMaxCapacity() == RSRecordingThread::RECORDING_PARCEL_MAX_CAPCITY)) {
197 #else
198         if (val && val->size() < MIN_DATA_SIZE) {
199 #endif
200             val = SkData::MakeWithCopy(val->data(), val->size());
201         }
202     }
203     return success;
204 }
205 #else
206 // Drawing::Data
207 bool RSMarshallingHelper::Marshalling(Parcel& parcel, std::shared_ptr<Drawing::Data> val)
208 {
209     if (!val) {
210         return parcel.WriteInt32(-1);
211     }
212 
213     bool ret = parcel.WriteInt32(val->GetSize());
214     if (val->GetSize() == 0) {
215         ROSEN_LOGW("unirender: RSMarshallingHelper::Marshalling Data size is 0");
216         return ret;
217     }
218 
219     ret = ret && RSMarshallingHelper::WriteToParcel(parcel, val->GetData(), val->GetSize());
220     if (!ret) {
221         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Data");
222     }
223     return ret;
224 }
225 
226 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<Drawing::Data>& val)
227 {
228     int32_t size = parcel.ReadInt32();
229     if (size == -1) {
230         val = nullptr;
231         return true;
232     }
233     val = std::make_shared<Drawing::Data>();
234     if (size == 0) {
235         ROSEN_LOGW("unirender: RSMarshallingHelper::Unmarshalling Data size is 0");
236         val->BuildUninitialized(0);
237         return true;
238     }
239 
240     const void* data = RSMarshallingHelper::ReadFromParcel(parcel, size);
241     if (data == nullptr) {
242         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling Data");
243         return false;
244     }
245 
246     if (static_cast<uint32_t>(size) < MIN_DATA_SIZE) {
247         val->BuildWithoutCopy(data, size);
248     } else {
249         val->BuildFromMalloc(data, size);
250     }
251     return true;
252 }
253 
254 bool RSMarshallingHelper::SkipData(Parcel& parcel)
255 {
256     int32_t size = parcel.ReadInt32();
257     if (size <= 0) {
258         return true;
259     }
260     return SkipFromParcel(parcel, size);
261 }
262 
263 bool RSMarshallingHelper::UnmarshallingWithCopy(Parcel& parcel, std::shared_ptr<Drawing::Data>& val)
264 {
265     bool success = Unmarshalling(parcel, val);
266     if (success) {
267         if (val && val->GetSize() < MIN_DATA_SIZE) {
268             val->BuildWithCopy(val->GetData(), val->GetSize());
269         }
270     }
271     return success;
272 }
273 #endif
274 
275 #ifndef USE_ROSEN_DRAWING
276 // SkTypeface serial proc
277 sk_sp<SkData> RSMarshallingHelper::SerializeTypeface(SkTypeface* tf, void* ctx)
278 {
279     if (tf == nullptr) {
280         ROSEN_LOGD("unirender: RSMarshallingHelper::SerializeTypeface SkTypeface is nullptr");
281         return nullptr;
282     }
283     return tf->serialize();
284 }
285 
286 // SkTypeface deserial proc
287 sk_sp<SkTypeface> RSMarshallingHelper::DeserializeTypeface(const void* data, size_t length, void* ctx)
288 {
289     SkMemoryStream stream(data, length);
290     return SkTypeface::MakeDeserialize(&stream);
291 }
292 
293 #if defined (ENABLE_DDGR_OPTIMIZE)
294 int RSMarshallingHelper::IntegrateReadDescriptor(Parcel& parcel)
295 {
296     sptr<IPCFileDescriptor> descriptor = parcel.ReadObject<IPCFileDescriptor>();
297     if (descriptor == nullptr) {
298         return -1;
299     }
300     return descriptor->GetFd();
301 }
302 
303 bool RSMarshallingHelper::IntegrateWriteDescriptor(Parcel& parcel, int fId)
304 {
305     sptr<IPCFileDescriptor> descriptor = new (std::nothrow) IPCFileDescriptor(fId);
306     if (descriptor == nullptr) {
307         return false;
308     }
309     return parcel.WriteObject<IPCFileDescriptor>(descriptor);
310 }
311 
312 bool RSMarshallingHelper::SerializeInternal(Parcel& parcel, const sk_sp<SkTextBlob>& val, const SkSerialProcs& procs)
313 {
314     SkBinaryWriteBuffer buffer;
315     buffer.setSerialProcs(procs);
316     val->TextBlobFlatten(buffer);
317     size_t total = buffer.bytesWritten();
318     int fId = -1;
319     sk_sp<SkData> data;
320 
321     void* dataPtr = DDGRRenderer::GetInstance().IntegrateMemAlloc(fId, total);
322     if (dataPtr != nullptr) {
323         data = SkData::MakeUninitialized(1);
324         buffer.writeToMemory(dataPtr);
325         int fId2 = ::dup(fId);
326         val->TextBlobSetShareParas(fId2, total, dataPtr);
327     } else {
328         data = SkData::MakeUninitialized(total);
329         buffer.writeToMemory(data->writable_data());
330     }
331     bool ret = Marshalling(parcel, data);
332     IntegrateWriteDescriptor(parcel, fId);
333     return ret;
334 }
335 
336 bool RSMarshallingHelper::DserializeInternal(Parcel& parcel, sk_sp<SkTextBlob>& val,
337     const SkDeserialProcs& procs, sk_sp<SkData>& data)
338 {
339     int sizePtr = 0;
340     int fId = IntegrateReadDescriptor(parcel);
341     fId = ::dup(fId);
342     void* dataPtr = DDGRRenderer::GetInstance().IntegrateGetHandle(fId, sizePtr);
343     if (dataPtr != nullptr && sizePtr > 0) {
344         val = SkTextBlob::Deserialize(dataPtr, sizePtr, procs);
345         val->TextBlobSetShareParas(fId, sizePtr, dataPtr);
346         return val != nullptr;
347     }
348     val = SkTextBlob::Deserialize(data->data(), data->size(), procs);
349     return val != nullptr;
350 }
351 #endif
352 
353 // SkTextBlob
354 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const sk_sp<SkTextBlob>& val)
355 {
356     sk_sp<SkData> data;
357     if (!val) {
358         ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling SkTextBlob is nullptr");
359         return Marshalling(parcel, data);
360     }
361     SkSerialProcs serialProcs;
362     serialProcs.fTypefaceProc = &RSMarshallingHelper::SerializeTypeface;
363 #if defined (ENABLE_DDGR_OPTIMIZE)
364     if (RSSystemProperties::GetDDGRIntegrateEnable()) {
365         ROSEN_LOGD("Marshalling text Integrate");
366         return SerializeInternal(parcel, val, serialProcs);
367     }
368 #endif
369     data = val->serialize(serialProcs);
370     return Marshalling(parcel, data);
371 }
372 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, sk_sp<SkTextBlob>& val)
373 {
374     sk_sp<SkData> data;
375     if (!Unmarshalling(parcel, data)) {
376         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling SkTextBlob");
377         return false;
378     }
379     if (data == nullptr) {
380         val = nullptr;
381         return true;
382     }
383     SkDeserialProcs deserialProcs;
384     deserialProcs.fTypefaceProc = &RSMarshallingHelper::DeserializeTypeface;
385 #if defined (ENABLE_DDGR_OPTIMIZE)
386     if (RSSystemProperties::GetDDGRIntegrateEnable()) {
387         ROSEN_LOGD("Unmarshalling text Integrate");
388         return DserializeInternal(parcel, val, deserialProcs, data);
389     }
390 #endif
391     val = SkTextBlob::Deserialize(data->data(), data->size(), deserialProcs);
392     return val != nullptr;
393 }
394 
395 // SkPaint
396 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const SkPaint& val)
397 {
398     SkBinaryWriteBuffer writer;
399     writer.writePaint(val);
400     size_t length = writer.bytesWritten();
401     sk_sp<SkData> data = SkData::MakeUninitialized(length);
402     writer.writeToMemory(data->writable_data());
403     return Marshalling(parcel, data);
404 }
405 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, SkPaint& val)
406 {
407     sk_sp<SkData> data;
408     if (!Unmarshalling(parcel, data) || !data) {
409         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling SkPaint");
410         return false;
411     }
412     SkReadBuffer reader(data->data(), data->size());
413 #ifdef NEW_SKIA
414     val = reader.readPaint();
415 #else
416     reader.readPaint(&val, nullptr);
417 #endif
418     return true;
419 }
420 #endif
421 
422 #ifndef USE_ROSEN_DRAWING
423 // SkImage
424 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const sk_sp<SkImage>& val)
425 {
426     if (!val) {
427         return parcel.WriteInt32(-1);
428     }
429     int32_t type = val->isLazyGenerated();
430     parcel.WriteInt32(type);
431     if (type == 1) {
432         ROSEN_LOGD("RSMarshallingHelper::Marshalling SkImage isLazyGenerated");
433         SkBinaryWriteBuffer writer;
434         writer.writeImage(val.get());
435         size_t length = writer.bytesWritten();
436         sk_sp<SkData> data = SkData::MakeUninitialized(length);
437         writer.writeToMemory(data->writable_data());
438         return Marshalling(parcel, data);
439     } else {
440         SkBitmap bitmap;
441 #ifdef NEW_SKIA
442         auto context = as_IB(val.get())->directContext();
443         if (!as_IB(val.get())->getROPixels(context, &bitmap)) {
444 #else
445         if (!as_IB(val.get())->getROPixels(&bitmap)) {
446 #endif
447             ROSEN_LOGE("RSMarshallingHelper::Marshalling SkImage getROPixels failed");
448             return false;
449         }
450         SkPixmap pixmap;
451         if (!bitmap.peekPixels(&pixmap)) {
452             ROSEN_LOGE("RSMarshallingHelper::Marshalling SkImage peekPixels failed");
453             return false;
454         }
455         size_t rb = pixmap.rowBytes();
456         int width = pixmap.width();
457         int height = pixmap.height();
458         const void* addr = pixmap.addr();
459         size_t size = rb * static_cast<size_t>(height);
460 
461         parcel.WriteUint32(size);
462         if (!WriteToParcel(parcel, addr, size)) {
463             ROSEN_LOGE("RSMarshallingHelper::Marshalling SkImage WriteToParcel failed");
464             return false;
465         }
466 
467         parcel.WriteUint32(rb);
468         parcel.WriteInt32(width);
469         parcel.WriteInt32(height);
470 
471         parcel.WriteUint32(pixmap.colorType());
472         parcel.WriteUint32(pixmap.alphaType());
473 
474         if (pixmap.colorSpace() == nullptr) {
475             parcel.WriteUint32(0);
476             return true;
477         } else {
478             auto data = pixmap.colorSpace()->serialize();
479             parcel.WriteUint32(data->size());
480             if (!WriteToParcel(parcel, data->data(), data->size())) {
481                 ROSEN_LOGE("RSMarshallingHelper::Marshalling SkImage WriteToParcel colorSpace failed");
482                 return false;
483             }
484         }
485         return true;
486     }
487 }
488 
489 static void sk_free_releaseproc(const void* ptr, void*)
490 {
491     MemoryTrack::Instance().RemovePictureRecord(ptr);
492     free(const_cast<void*>(ptr));
493 }
494 
495 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, sk_sp<SkImage>& val)
496 {
497     void* addr = nullptr;
498     return Unmarshalling(parcel, val, addr);
499 }
500 
501 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, sk_sp<SkImage>& val, void*& imagepixelAddr)
502 {
503     int32_t type = parcel.ReadInt32();
504     if (type == -1) {
505         val = nullptr;
506         return true;
507     }
508     if (type == 1) {
509         sk_sp<SkData> data;
510         ROSEN_LOGD("RSMarshallingHelper::Unmarshalling lazy");
511         if (!Unmarshalling(parcel, data)) {
512             ROSEN_LOGE("failed RSMarshallingHelper::Unmarshalling SkImage");
513             return false;
514         }
515         SkReadBuffer reader(data->data(), data->size());
516         val = reader.readImage();
517         return val != nullptr;
518     } else {
519         size_t pixmapSize = parcel.ReadUint32();
520         const void* addr = RSMarshallingHelper::ReadFromParcel(parcel, pixmapSize);
521         if (addr == nullptr) {
522             ROSEN_LOGE("failed RSMarshallingHelper::Unmarshalling SkData addr");
523             return false;
524         }
525 
526         size_t rb = parcel.ReadUint32();
527         int width = parcel.ReadInt32();
528         int height = parcel.ReadInt32();
529 
530         SkColorType colorType = static_cast<SkColorType>(parcel.ReadUint32());
531         SkAlphaType alphaType = static_cast<SkAlphaType>(parcel.ReadUint32());
532         sk_sp<SkColorSpace> colorSpace;
533 
534         size_t size = parcel.ReadUint32();
535         if (size == 0) {
536             colorSpace = nullptr;
537         } else {
538             const void* data = RSMarshallingHelper::ReadFromParcel(parcel, size);
539             if (data == nullptr) {
540                 ROSEN_LOGE("failed RSMarshallingHelper::Unmarshalling SkData data");
541                 return false;
542             }
543             colorSpace = SkColorSpace::Deserialize(data, size);
544 
545 #ifdef RS_ENABLE_RECORDING
546             if (size >= MIN_DATA_SIZE && parcel.GetMaxCapacity() != RSRecordingThread::RECORDING_PARCEL_MAX_CAPCITY) {
547 #else
548             if (size >= MIN_DATA_SIZE) {
549 #endif
550                 free(const_cast<void*>(data));
551             }
552         }
553 
554         // if pixelmapSize >= MIN_DATA_SIZE(copyFromAshMem). record this memory size
555         // use this proc to follow release step
556         SkImageInfo imageInfo = SkImageInfo::Make(width, height, colorType, alphaType, colorSpace);
557 #ifdef RS_ENABLE_RECORDING
558         auto skData =
559             (pixmapSize < MIN_DATA_SIZE || parcel.GetMaxCapacity() == RSRecordingThread::RECORDING_PARCEL_MAX_CAPCITY)
560                 ? SkData::MakeWithCopy(addr, pixmapSize)
561 #else
562         auto skData = pixmapSize < MIN_DATA_SIZE ? SkData::MakeWithCopy(addr, pixmapSize)
563 #endif
564                 : SkData::MakeWithProc(addr, pixmapSize, sk_free_releaseproc, nullptr);
565         val = SkImage::MakeRasterData(imageInfo, skData, rb);
566         // add to MemoryTrack for memoryManager
567 #ifdef RS_ENABLE_RECORDING
568         if (pixmapSize >= MIN_DATA_SIZE && parcel.GetMaxCapacity() != RSRecordingThread::RECORDING_PARCEL_MAX_CAPCITY) {
569 #else
570         if (pixmapSize >= MIN_DATA_SIZE) {
571 #endif
572             MemoryInfo info = { pixmapSize, 0, 0, MEMORY_TYPE::MEM_SKIMAGE };
573             MemoryTrack::Instance().AddPictureRecord(addr, info);
574             imagepixelAddr = const_cast<void*>(addr);
575         }
576         return val != nullptr;
577     }
578 }
579 #else
580 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<Drawing::Image>& val)
581 {
582     if (!val) {
583         return parcel.WriteInt32(-1);
584     }
585     parcel.WriteInt32(1);
586     auto data = val->Serialize();
587     return Marshalling(parcel, data);
588 }
589 
590 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<Drawing::Image>& val, void*& imagepixelAddr)
591 {
592     (void)imagepixelAddr;
593     int32_t type = parcel.ReadInt32();
594     if (type == -1) {
595         val = nullptr;
596         return true;
597     }
598     std::shared_ptr<Drawing::Data> data;
599     if (!Unmarshalling(parcel, data) || !data) {
600         ROSEN_LOGE("failed RSMarshallingHelper::Unmarshalling Drawing::Image");
601         return false;
602     }
603     val = std::make_shared<Drawing::Image>();
604     if (!val->Deserialize(data)) {
605         ROSEN_LOGE("failed RSMarshallingHelper::Unmarshalling Drawing::Image deserialize");
606         val = nullptr;
607         return false;
608     }
609     return true;
610 }
611 #endif
612 
613 #ifndef USE_ROSEN_DRAWING
614 bool RSMarshallingHelper::SkipSkImage(Parcel& parcel)
615 {
616     int32_t type = parcel.ReadInt32();
617     if (type == -1) {
618         return true;
619     }
620     sk_sp<SkData> data;
621     if (type == 1) {
622         ROSEN_LOGD("RSMarshallingHelper::SkipSkImage lazy");
623         return SkipSkData(parcel);
624     } else {
625         size_t pixmapSize = parcel.ReadUint32();
626         if (!SkipFromParcel(parcel, pixmapSize)) {
627             ROSEN_LOGE("failed RSMarshallingHelper::SkipSkImage SkData addr");
628             return false;
629         }
630 
631         parcel.ReadUint32();
632         parcel.ReadInt32();
633         parcel.ReadInt32();
634         parcel.ReadUint32();
635         parcel.ReadUint32();
636         size_t size = parcel.ReadUint32();
637         return size == 0 ? true : SkipFromParcel(parcel, size);
638     }
639 }
640 #else
641 bool RSMarshallingHelper::SkipImage(Parcel& parcel)
642 {
643     int32_t type = parcel.ReadInt32();
644     if (type == -1) {
645         return true;
646     }
647     std::shared_ptr<Drawing::Data> data;
648     if (type == 1) {
649         ROSEN_LOGD("RSMarshallingHelper::SkipImage lazy");
650         return SkipData(parcel);
651     } else {
652         size_t pixmapSize = parcel.ReadUint32();
653         if (!SkipFromParcel(parcel, pixmapSize)) {
654             ROSEN_LOGE("failed RSMarshallingHelper::SkipImage Data addr");
655             return false;
656         }
657 
658         parcel.ReadUint32();
659         parcel.ReadInt32();
660         parcel.ReadInt32();
661         parcel.ReadUint32();
662         parcel.ReadUint32();
663         size_t size = parcel.ReadUint32();
664         return size == 0 ? true : SkipFromParcel(parcel, size);
665     }
666 }
667 #endif
668 
669 #ifndef USE_ROSEN_DRAWING
670 // SkPicture
671 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const sk_sp<SkPicture>& val)
672 {
673     sk_sp<SkData> data;
674     if (!val) {
675         ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling SkPicture is nullptr");
676         return Marshalling(parcel, data);
677     }
678     data = val->serialize();
679     return Marshalling(parcel, data);
680 }
681 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, sk_sp<SkPicture>& val)
682 {
683     sk_sp<SkData> data;
684     if (!Unmarshalling(parcel, data)) {
685         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling SkPicture");
686         return false;
687     }
688     if (data == nullptr) {
689         val = nullptr;
690         return true;
691     }
692     val = SkPicture::MakeFromData(data->data(), data->size());
693     return val != nullptr;
694 }
695 
696 // SkVertices
697 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const sk_sp<SkVertices>& val)
698 {
699     sk_sp<SkData> data;
700     if (!val) {
701         ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling SkVertices is nullptr");
702         return Marshalling(parcel, data);
703     }
704 #ifdef NEW_SKIA
705     SkBinaryWriteBuffer writer;
706     SkVerticesPriv info(val->priv());
707     info.encode(writer);
708     size_t length = writer.bytesWritten();
709     data = SkData::MakeUninitialized(length);
710     writer.writeToMemory(data->writable_data());
711 #else
712     data = val->encode();
713 #endif
714     return Marshalling(parcel, data);
715 }
716 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, sk_sp<SkVertices>& val)
717 {
718     sk_sp<SkData> data;
719     if (!Unmarshalling(parcel, data)) {
720         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling SkVertices");
721         return false;
722     }
723     if (data == nullptr) {
724         val = nullptr;
725         return true;
726     }
727 #ifdef NEW_SKIA
728     SkReadBuffer reader(data->data(), data->size());
729     val = SkVerticesPriv::Decode(reader);
730 #else
731     val = SkVertices::Decode(data->data(), data->size());
732 #endif
733     return val != nullptr;
734 }
735 
736 // SkRect
737 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const SkRect& rect)
738 {
739     SkBinaryWriteBuffer writer;
740     writer.writeRect(rect);
741     size_t length = writer.bytesWritten();
742     sk_sp<SkData> data = SkData::MakeUninitialized(length);
743     writer.writeToMemory(data->writable_data());
744     return Marshalling(parcel, data);
745 }
746 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, SkRect& rect)
747 {
748     sk_sp<SkData> data;
749     if (!Unmarshalling(parcel, data) || !data) {
750         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling SkRect");
751         return false;
752     }
753     SkReadBuffer reader(data->data(), data->size());
754     reader.readRect(&rect);
755     return true;
756 }
757 
758 // SkRegion
759 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const SkRegion& region)
760 {
761     SkBinaryWriteBuffer writer;
762     writer.writeRegion(region);
763     size_t length = writer.bytesWritten();
764     sk_sp<SkData> data = SkData::MakeUninitialized(length);
765     writer.writeToMemory(data->writable_data());
766     return Marshalling(parcel, data);
767 }
768 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, SkRegion& region)
769 {
770     sk_sp<SkData> data;
771     if (!Unmarshalling(parcel, data) || !data) {
772         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling SkRegion");
773         return false;
774     }
775     SkReadBuffer reader(data->data(), data->size());
776     reader.readRegion(&region);
777     return true;
778 }
779 
780 // SkBitmap
781 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const SkBitmap& val)
782 {
783     size_t rb = val.rowBytes();
784     int width = val.width();
785     int height = val.height();
786     const void* addr = val.pixmap().addr();
787     size_t pixmapSize = rb * static_cast<size_t>(height);
788 
789     parcel.WriteUint32(pixmapSize);
790     if (!WriteToParcel(parcel, addr, pixmapSize)) {
791         ROSEN_LOGE("RSMarshallingHelper::Marshalling write SkBitmap addr failed");
792         return false;
793     }
794 
795     parcel.WriteUint32(rb);
796     parcel.WriteInt32(width);
797     parcel.WriteInt32(height);
798 
799     parcel.WriteUint32(val.colorType());
800     parcel.WriteUint32(val.alphaType());
801 
802     if (val.colorSpace() == nullptr) {
803         parcel.WriteUint32(0);
804         return true;
805     } else {
806         auto data = val.colorSpace()->serialize();
807         parcel.WriteUint32(data->size());
808         if (!WriteToParcel(parcel, data->data(), data->size())) {
809             ROSEN_LOGE("RSMarshallingHelper::Marshalling write SkBitmap colorSpace failed");
810             return false;
811         }
812     }
813     return true;
814 }
815 
816 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, SkBitmap& val)
817 {
818     size_t pixmapSize = parcel.ReadUint32();
819     const void* addr = RSMarshallingHelper::ReadFromParcel(parcel, pixmapSize);
820     if (addr == nullptr) {
821         ROSEN_LOGE("RSMarshallingHelper::Unmarshalling read SkBitmap addr failed");
822         return false;
823     }
824 
825     size_t rb = parcel.ReadUint32();
826     int width = parcel.ReadInt32();
827     int height = parcel.ReadInt32();
828 
829     SkColorType colorType = static_cast<SkColorType>(parcel.ReadUint32());
830     SkAlphaType alphaType = static_cast<SkAlphaType>(parcel.ReadUint32());
831     sk_sp<SkColorSpace> colorSpace;
832 
833     size_t size = parcel.ReadUint32();
834     if (size == 0) {
835         colorSpace = nullptr;
836     } else {
837         const void* data = RSMarshallingHelper::ReadFromParcel(parcel, size);
838         if (data == nullptr) {
839             ROSEN_LOGE("failed RSMarshallingHelper::Unmarshalling read SkBitmap data failed");
840             return false;
841         }
842         colorSpace = SkColorSpace::Deserialize(data, size);
843 
844 #ifdef RS_ENABLE_RECORDING
845         if (size >= MIN_DATA_SIZE && parcel.GetMaxCapacity() != RSRecordingThread::RECORDING_PARCEL_MAX_CAPCITY) {
846 #else
847         if (size >= MIN_DATA_SIZE) {
848 #endif
849             free(const_cast<void*>(data));
850         }
851     }
852 
853     SkImageInfo imageInfo = SkImageInfo::Make(width, height, colorType, alphaType, colorSpace);
854     val.setInfo(imageInfo, rb);
855     val.setPixels(const_cast<void*>(addr));
856     return true;
857 }
858 
859 // SKPath
860 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const SkPath& val)
861 {
862     SkBinaryWriteBuffer writer;
863     writer.writePath(val);
864     size_t length = writer.bytesWritten();
865     sk_sp<SkData> data = SkData::MakeUninitialized(length);
866     writer.writeToMemory(data->writable_data());
867     return Marshalling(parcel, data);
868 }
869 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, SkPath& val)
870 {
871     sk_sp<SkData> data;
872     if (!Unmarshalling(parcel, data) || !data) {
873         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling SKPath");
874         return false;
875     }
876     SkReadBuffer reader(data->data(), data->size());
877     reader.readPath(&val);
878     return true;
879 }
880 
881 // SkFlattenable
882 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const sk_sp<SkFlattenable>& val)
883 {
884     if (!val) {
885         ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling SkFlattenable is nullptr");
886         return parcel.WriteInt32(-1);
887     }
888     sk_sp<SkData> data = val->serialize();
889     return parcel.WriteInt32(val->getFlattenableType()) && Marshalling(parcel, data);
890 }
891 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, sk_sp<SkFlattenable>& val)
892 {
893     int32_t type = parcel.ReadInt32();
894     if (type == -1) {
895         val = nullptr;
896         return true;
897     }
898     sk_sp<SkData> data;
899     if (!Unmarshalling(parcel, data) || !data) {
900         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling SkFlattenable");
901         return false;
902     }
903     val = SkFlattenable::Deserialize(static_cast<SkFlattenable::Type>(type), data->data(), data->size());
904     return val != nullptr;
905 }
906 
907 // SkDrawable
908 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const sk_sp<SkDrawable>& val)
909 {
910     if (!val) {
911         ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling SkDrawable is nullptr");
912     }
913     return Marshalling(parcel, sk_sp<SkFlattenable>(val));
914 }
915 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, sk_sp<SkDrawable>& val)
916 {
917     sk_sp<SkFlattenable> flattenablePtr;
918     if (!Unmarshalling(parcel, flattenablePtr)) {
919         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling SkDrawable");
920         return false;
921     }
922     val = sk_reinterpret_cast<SkDrawable>(flattenablePtr);
923     return true;
924 }
925 
926 // SkImageFilter
927 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const sk_sp<SkImageFilter>& val)
928 {
929     if (!val) {
930         ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling SkImageFilter is nullptr");
931     }
932     return Marshalling(parcel, sk_sp<SkFlattenable>(val));
933 }
934 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, sk_sp<SkImageFilter>& val)
935 {
936     sk_sp<SkFlattenable> flattenablePtr;
937     if (!Unmarshalling(parcel, flattenablePtr)) {
938         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling SkImageFilter");
939         return false;
940     }
941     val = sk_reinterpret_cast<SkImageFilter>(flattenablePtr);
942     return true;
943 }
944 #endif
945 
946 // RSShader
947 #ifndef USE_ROSEN_DRAWING
948 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSShader>& val)
949 {
950     if (!val) {
951         ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling RSShader is nullptr");
952         return parcel.WriteInt32(-1);
953     }
954     return parcel.WriteInt32(1) && Marshalling(parcel, sk_sp<SkFlattenable>(val->GetSkShader()));
955 }
956 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSShader>& val)
957 {
958     if (parcel.ReadInt32() == -1) {
959         val = nullptr;
960         return true;
961     }
962     sk_sp<SkFlattenable> flattenablePtr;
963     if (!Unmarshalling(parcel, flattenablePtr)) {
964         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling RSShader");
965         return false;
966     }
967     auto shaderPtr = sk_reinterpret_cast<SkShader>(flattenablePtr);
968     val = RSShader::CreateRSShader(shaderPtr);
969     return val != nullptr;
970 }
971 #else
972 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSShader>& val)
973 {
974     if (!val || !val->GetDrawingShader()) {
975         ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling RSShader is nullptr");
976         return parcel.WriteInt32(-1);
977     }
978     if (val->GetDrawingShader()->GetDrawingType() != Drawing::DrawingType::RECORDING) {
979         ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling Drawing::ShaderEffect is invalid");
980         return parcel.WriteInt32(-1);
981     }
982     auto recordingShaderEffect = static_cast<Drawing::RecordingShaderEffect*>(val->GetDrawingShader().get());
983     auto cmdListData = recordingShaderEffect->GetCmdList()->GetData();
984     bool ret = parcel.WriteInt32(cmdListData.second);
985     if (cmdListData.second == 0) {
986         ROSEN_LOGW("unirender: RSMarshallingHelper::Marshalling RecordingShaderEffectCmdList size is 0");
987         return ret;
988     }
989 
990     ret &= RSMarshallingHelper::WriteToParcel(parcel, cmdListData.first, cmdListData.second);
991     if (!ret) {
992         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::RecordingShaderEffect");
993     }
994 
995     return ret;
996 }
997 
998 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSShader>& val)
999 {
1000     int32_t size = parcel.ReadInt32();
1001     if (size == -1) {
1002         val = nullptr;
1003         return true;
1004     }
1005     if (size == 0) {
1006         ROSEN_LOGW("unirender: RSMarshallingHelper::Unmarshalling Drawing::ShaderEffectCmdList size is 0");
1007         val = RSShader::CreateRSShader();
1008         return true;
1009     }
1010 
1011     const void* data = RSMarshallingHelper::ReadFromParcel(parcel, size);
1012     if (data == nullptr) {
1013         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling RSShader");
1014         return false;
1015     }
1016     auto shaderEffectCmdList = Drawing::ShaderEffectCmdList::CreateFromData({ data, size }, true);
1017     if (shaderEffectCmdList == nullptr) {
1018         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling RSShader shader effect cmdlist is nullptr");
1019         return false;
1020     }
1021     auto shaderEffect = shaderEffectCmdList->Playback();
1022     val = RSShader::CreateRSShader(shaderEffect);
1023     return val != nullptr;
1024 }
1025 
1026 // Drawing::Matrix
1027 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const Drawing::Matrix& val)
1028 {
1029     Drawing::Matrix::Buffer buffer;
1030     val.GetAll(buffer);
1031     int32_t size = buffer.size() * sizeof(Drawing::scalar);
1032     bool ret = parcel.WriteInt32(size);
1033     ret &= RSMarshallingHelper::WriteToParcel(parcel, buffer.data(), size);
1034     if (!ret) {
1035         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::Matrix");
1036     }
1037     return ret;
1038 }
1039 
1040 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, Drawing::Matrix& val)
1041 {
1042     int32_t size = parcel.ReadInt32();
1043     auto data = static_cast<const Drawing::scalar*>(RSMarshallingHelper::ReadFromParcel(parcel, size));
1044     if (data == nullptr) {
1045         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling Drawing::Matrix");
1046         return false;
1047     }
1048 
1049     val.SetMatrix(data[Drawing::Matrix::SCALE_X], data[Drawing::Matrix::SKEW_X], data[Drawing::Matrix::TRANS_X],
1050         data[Drawing::Matrix::SKEW_Y], data[Drawing::Matrix::SCALE_Y], data[Drawing::Matrix::TRANS_Y],
1051         data[Drawing::Matrix::PERSP_0], data[Drawing::Matrix::PERSP_1], data[Drawing::Matrix::PERSP_2]);
1052     return true;
1053 }
1054 #endif
1055 
1056 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSLinearGradientBlurPara>& val)
1057 {
1058     bool success = Marshalling(parcel, val->blurRadius_);
1059     success = success && parcel.WriteUint32(static_cast<uint32_t>(val->fractionStops_.size()));
1060     for (size_t i = 0; i < val->fractionStops_.size(); i++) {
1061         success = success && Marshalling(parcel, val->fractionStops_[i].first);
1062         success = success && Marshalling(parcel, val->fractionStops_[i].second);
1063     }
1064     success = success && Marshalling(parcel, val->direction_);
1065     return success;
1066 }
1067 
1068 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSLinearGradientBlurPara>& val)
1069 {
1070     float blurRadius;
1071     std::vector<std::pair<float, float>> fractionStops;
1072     GradientDirection direction = GradientDirection::NONE;
1073     bool success = Unmarshalling(parcel, blurRadius);
1074     uint32_t fractionStopsSize = parcel.ReadUint32();
1075     for (size_t i = 0; i < fractionStopsSize; i++) {
1076         std::pair<float, float> fractionStop;
1077         float first = 0.0;
1078         float second = 0.0;
1079         success = success && Unmarshalling(parcel, first);
1080         if (!success) {
1081             return false;
1082         }
1083         fractionStop.first = first;
1084         success = success && Unmarshalling(parcel, second);
1085         if (!success) {
1086             return false;
1087         }
1088         fractionStop.second = second;
1089         fractionStops.push_back(fractionStop);
1090     }
1091     success = success && Unmarshalling(parcel, direction);
1092     if (success) {
1093         val = std::make_shared<RSLinearGradientBlurPara>(blurRadius, fractionStops, direction);
1094     }
1095     return success;
1096 }
1097 
1098 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const EmitterConfig& val)
1099 {
1100     bool success = Marshalling(parcel, val.emitRate_);
1101     success = success && Marshalling(parcel, val.emitShape_);
1102     success = success && Marshalling(parcel, val.position_.x_);
1103     success = success && Marshalling(parcel, val.position_.y_);
1104     success = success && Marshalling(parcel, val.emitSize_.x_);
1105     success = success && Marshalling(parcel, val.emitSize_.y_);
1106     success = success && Marshalling(parcel, val.particleCount_);
1107     success = success && Marshalling(parcel, val.lifeTime_);
1108     success = success && Marshalling(parcel, val.type_);
1109     success = success && Marshalling(parcel, val.radius_);
1110     success = success && Marshalling(parcel, val.image_);
1111     success = success && Marshalling(parcel, val.imageSize_.x_);
1112     success = success && Marshalling(parcel, val.imageSize_.y_);
1113 
1114     return success;
1115 }
1116 
1117 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, EmitterConfig& val)
1118 {
1119     int emitRate = 0;
1120     ShapeType emitShape = ShapeType::RECT;
1121     float positionX = 0.f;
1122     float positionY = 0.f;
1123     float emitSizeWidth = 0.f;
1124     float emitSizeHeight = 0.f;
1125     int particleCount = 0;
1126     int64_t lifeTime = 0;
1127     ParticleType particleType = ParticleType::POINTS;
1128     float radius = 0.f;
1129     std::shared_ptr<RSImage> image = nullptr;
1130     float imageWidth = 0.f;
1131     float imageHeight = 0.f;
1132 
1133     bool success = Unmarshalling(parcel, emitRate);
1134     success = success && Unmarshalling(parcel, emitShape);
1135     success = success && Unmarshalling(parcel, positionX);
1136     success = success && Unmarshalling(parcel, positionY);
1137     Vector2f position(positionX, positionY);
1138     success = success && Unmarshalling(parcel, emitSizeWidth);
1139     success = success && Unmarshalling(parcel, emitSizeHeight);
1140     Vector2f emitSize(emitSizeWidth, emitSizeHeight);
1141     success = success && Unmarshalling(parcel, particleCount);
1142     success = success && Unmarshalling(parcel, lifeTime);
1143     success = success && Unmarshalling(parcel, particleType);
1144     success = success && Unmarshalling(parcel, radius);
1145     Unmarshalling(parcel, image);
1146     success = success && Unmarshalling(parcel, imageWidth);
1147     success = success && Unmarshalling(parcel, imageHeight);
1148     Vector2f imageSize(imageWidth, imageHeight);
1149     if (success) {
1150         val = EmitterConfig(
1151             emitRate, emitShape, position, emitSize, particleCount, lifeTime, particleType, radius, image, imageSize);
1152     }
1153     return success;
1154 }
1155 
1156 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const ParticleVelocity& val)
1157 {
1158     return Marshalling(parcel, val.velocityValue_.start_) && Marshalling(parcel, val.velocityValue_.end_) &&
1159            Marshalling(parcel, val.velocityAngle_.start_) && Marshalling(parcel, val.velocityAngle_.end_);
1160 }
1161 
1162 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, ParticleVelocity& val)
1163 {
1164     float valueStart = 0.f;
1165     float valueEnd = 0.f;
1166     float angleStart = 0.f;
1167     float angleEnd = 0.f;
1168     bool success = Unmarshalling(parcel, valueStart) && Unmarshalling(parcel, valueEnd) &&
1169         Unmarshalling(parcel, angleStart) && Unmarshalling(parcel, angleEnd);
1170     if (success) {
1171         Range<float> velocityValue(valueStart, valueEnd);
1172         Range<float> velocityAngle(angleStart, angleEnd);
1173         val = ParticleVelocity(velocityValue, velocityAngle);
1174     }
1175     return success;
1176 }
1177 
1178 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const RenderParticleParaType<float>& val)
1179 {
1180     bool success = Marshalling(parcel, val.val_.start_) && Marshalling(parcel, val.val_.end_) &&
1181                    Marshalling(parcel, val.updator_);
1182     if (val.updator_ == ParticleUpdator::RANDOM) {
1183         success = success && Marshalling(parcel, val.random_.start_) && Marshalling(parcel, val.random_.end_);
1184     } else if (val.updator_ == ParticleUpdator::CURVE) {
1185         success = success && parcel.WriteUint32(static_cast<uint32_t>(val.valChangeOverLife_.size()));
1186         for (size_t i = 0; i < val.valChangeOverLife_.size(); i++) {
1187             success = success && Marshalling(parcel, val.valChangeOverLife_[i]->fromValue_);
1188             success = success && Marshalling(parcel, val.valChangeOverLife_[i]->toValue_);
1189             success = success && Marshalling(parcel, val.valChangeOverLife_[i]->startMillis_);
1190             success = success && Marshalling(parcel, val.valChangeOverLife_[i]->endMillis_);
1191             success = success && val.valChangeOverLife_[i]->interpolator_->Marshalling(parcel);
1192         }
1193     }
1194     return success;
1195 }
1196 
1197 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, RenderParticleParaType<float>& val)
1198 {
1199     float valueStart = 0.f;
1200     float valueEnd = 0.f;
1201     ParticleUpdator updator = ParticleUpdator::NONE;
1202     float randomStart = 0.f;
1203     float randomEnd = 0.f;
1204     std::vector<std::shared_ptr<ChangeInOverLife<float>>> valChangeOverLife;
1205     bool success =
1206         Unmarshalling(parcel, valueStart) && Unmarshalling(parcel, valueEnd) && Unmarshalling(parcel, updator);
1207     if (updator == ParticleUpdator::RANDOM) {
1208         success = success && Unmarshalling(parcel, randomStart) && Unmarshalling(parcel, randomEnd);
1209     } else if (updator == ParticleUpdator::CURVE) {
1210         uint32_t valChangeOverLifeSize = parcel.ReadUint32();
1211         for (size_t i = 0; i < valChangeOverLifeSize; i++) {
1212             float fromValue = 0.f;
1213             float toValue = 0.f;
1214             int startMillis = 0;
1215             int endMillis = 0;
1216             success = success && Unmarshalling(parcel, fromValue);
1217             success = success && Unmarshalling(parcel, toValue);
1218             success = success && Unmarshalling(parcel, startMillis);
1219             success = success && Unmarshalling(parcel, endMillis);
1220             std::shared_ptr<RSInterpolator> interpolator(RSInterpolator::Unmarshalling(parcel));
1221             auto change = std::make_shared<ChangeInOverLife<float>>(
1222                 fromValue, toValue, startMillis, endMillis, interpolator);
1223             valChangeOverLife.push_back(change);
1224         }
1225     }
1226     if (success) {
1227         Range<float> value(valueStart, valueEnd);
1228         Range<float> random(randomStart, randomEnd);
1229         val = RenderParticleParaType<float>(value, updator, random, valChangeOverLife);
1230     }
1231     return success;
1232 }
1233 
1234 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const RenderParticleColorParaType& val)
1235 {
1236     bool success = Marshalling(parcel, val.colorVal_.start_) && Marshalling(parcel, val.colorVal_.end_) &&
1237                    Marshalling(parcel, val.updator_);
1238     if (val.updator_ == ParticleUpdator::RANDOM) {
1239         success = success && Marshalling(parcel, val.redRandom_.start_) && Marshalling(parcel, val.redRandom_.end_);
1240         success =
1241             success && Marshalling(parcel, val.greenRandom_.start_) && Marshalling(parcel, val.greenRandom_.end_);
1242         success =
1243             success && Marshalling(parcel, val.blueRandom_.start_) && Marshalling(parcel, val.blueRandom_.end_);
1244         success =
1245             success && Marshalling(parcel, val.alphaRandom_.start_) && Marshalling(parcel, val.alphaRandom_.end_);
1246     } else if (val.updator_ == ParticleUpdator::CURVE) {
1247         success = success && parcel.WriteUint32(static_cast<uint32_t>(val.valChangeOverLife_.size()));
1248         for (size_t i = 0; i < val.valChangeOverLife_.size(); i++) {
1249             success = success && Marshalling(parcel, val.valChangeOverLife_[i]->fromValue_);
1250             success = success && Marshalling(parcel, val.valChangeOverLife_[i]->toValue_);
1251             success = success && Marshalling(parcel, val.valChangeOverLife_[i]->startMillis_);
1252             success = success && Marshalling(parcel, val.valChangeOverLife_[i]->endMillis_);
1253             success = success && val.valChangeOverLife_[i]->interpolator_->Marshalling(parcel);
1254         }
1255     }
1256     return success;
1257 }
1258 
1259 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, RenderParticleColorParaType& val)
1260 {
1261     Color colorValStart = RSColor(0, 0, 0);
1262     Color colorValEnd = RSColor(0, 0, 0);
1263     ParticleUpdator updator = ParticleUpdator::NONE;
1264     float redRandomStart = 0.f;
1265     float redRandomEnd = 0.f;
1266     float greenRandomStart = 0.f;
1267     float greenRandomEnd = 0.f;
1268     float blueRandomStart = 0.f;
1269     float blueRandomEnd = 0.f;
1270     float alphaRandomStart = 0.f;
1271     float alphaRandomEnd = 0.f;
1272     std::vector<std::shared_ptr<ChangeInOverLife<Color>>> valChangeOverLife;
1273     bool success = Unmarshalling(parcel, colorValStart) && Unmarshalling(parcel, colorValEnd) &&
1274                    Unmarshalling(parcel, updator);
1275     if (updator == ParticleUpdator::RANDOM) {
1276         success = success && Unmarshalling(parcel, redRandomStart) && Unmarshalling(parcel, redRandomEnd) &&
1277                   Unmarshalling(parcel, greenRandomStart) && Unmarshalling(parcel, greenRandomEnd) &&
1278                   Unmarshalling(parcel, blueRandomStart) && Unmarshalling(parcel, blueRandomEnd) &&
1279                   Unmarshalling(parcel, alphaRandomStart) && Unmarshalling(parcel, alphaRandomEnd);
1280     } else if (updator == ParticleUpdator::CURVE) {
1281         uint32_t valChangeOverLifeSize = parcel.ReadUint32();
1282         for (size_t i = 0; i < valChangeOverLifeSize; i++) {
1283             Color fromValue = RSColor(0, 0, 0);
1284             Color toValue = RSColor(0, 0, 0);
1285             int startMillis = 0;
1286             int endMillis = 0;
1287             success = success && Unmarshalling(parcel, fromValue);
1288             success = success && Unmarshalling(parcel, toValue);
1289             success = success && Unmarshalling(parcel, startMillis);
1290             success = success && Unmarshalling(parcel, endMillis);
1291             std::shared_ptr<RSInterpolator> interpolator(RSInterpolator::Unmarshalling(parcel));
1292             auto change =
1293                 std::make_shared<ChangeInOverLife<Color>>(fromValue, toValue, startMillis, endMillis, interpolator);
1294             valChangeOverLife.push_back(change);
1295         }
1296     }
1297     if (success) {
1298         Range<Color> colorVal(colorValStart, colorValEnd);
1299         Range<float> redRandom(redRandomStart, redRandomEnd);
1300         Range<float> greenRandom(greenRandomStart, greenRandomEnd);
1301         Range<float> blueRandom(blueRandomStart, blueRandomEnd);
1302         Range<float> alphaRandom(alphaRandomStart, alphaRandomEnd);
1303         val = RenderParticleColorParaType(
1304             colorVal, updator, redRandom, greenRandom, blueRandom, alphaRandom, valChangeOverLife);
1305     }
1306     return success;
1307 }
1308 
1309 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<ParticleRenderParams>& val)
1310 {
1311     bool success = Marshalling(parcel, val->emitterConfig_);
1312     success = success && Marshalling(parcel, val->velocity_);
1313     success = success && Marshalling(parcel, val->acceleration_.accelerationValue_);
1314     success = success && Marshalling(parcel, val->acceleration_.accelerationAngle_);
1315     success = success && Marshalling(parcel, val->color_);
1316     success = success && Marshalling(parcel, val->opacity_);
1317     success = success && Marshalling(parcel, val->scale_);
1318     success = success && Marshalling(parcel, val->spin_);
1319     return success;
1320 }
1321 
1322 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<ParticleRenderParams>& val)
1323 {
1324     EmitterConfig emitterConfig;
1325     ParticleVelocity velocity;
1326     RenderParticleParaType<float> accelerationValue;
1327     RenderParticleParaType<float> accelerationAngle;
1328     RenderParticleColorParaType color;
1329     RenderParticleParaType<float> opacity;
1330     RenderParticleParaType<float> scale;
1331     RenderParticleParaType<float> spin;
1332     bool success = Unmarshalling(parcel, emitterConfig);
1333     success = success && Unmarshalling(parcel, velocity);
1334     success = success && Unmarshalling(parcel, accelerationValue);
1335     success = success && Unmarshalling(parcel, accelerationAngle);
1336     RenderParticleAcceleration acceleration = RenderParticleAcceleration(accelerationValue, accelerationAngle);
1337     success = success && Unmarshalling(parcel, color);
1338     success = success && Unmarshalling(parcel, opacity);
1339     success = success && Unmarshalling(parcel, scale);
1340     success = success && Unmarshalling(parcel, spin);
1341     if (success) {
1342         val =
1343             std::make_shared<ParticleRenderParams>(emitterConfig, velocity, acceleration, color, opacity, scale, spin);
1344     }
1345     return success;
1346 }
1347 
1348 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::vector<std::shared_ptr<ParticleRenderParams>>& val)
1349 {
1350     bool success = parcel.WriteUint32(static_cast<uint32_t>(val.size()));
1351     for (size_t i = 0; i < val.size(); i++) {
1352         success = success && Marshalling(parcel, val[i]);
1353     }
1354     return success;
1355 }
1356 
1357 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::vector<std::shared_ptr<ParticleRenderParams>>& val)
1358 {
1359     uint32_t size = parcel.ReadUint32();
1360     bool success = true;
1361     std::vector<std::shared_ptr<ParticleRenderParams>> particlesRenderParams;
1362     for (size_t i = 0; i < size; i++) {
1363         std::shared_ptr<ParticleRenderParams> particleRenderParams;
1364         success = success && Unmarshalling(parcel, particleRenderParams);
1365         particlesRenderParams.push_back(particleRenderParams);
1366     }
1367     if (success) {
1368         val = particlesRenderParams;
1369     }
1370     return success;
1371 }
1372 
1373 // RSPath
1374 #ifndef USE_ROSEN_DRAWING
1375 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSPath>& val)
1376 {
1377     if (!val) {
1378         ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling RSPath is nullptr");
1379         return parcel.WriteInt32(-1);
1380     }
1381     return parcel.WriteInt32(1) && Marshalling(parcel, val->GetSkiaPath());
1382 }
1383 
1384 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSPath>& val)
1385 {
1386     if (parcel.ReadInt32() == -1) {
1387         val = nullptr;
1388         return true;
1389     }
1390     SkPath path;
1391     if (!Unmarshalling(parcel, path)) {
1392         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling RSPath");
1393         return false;
1394     }
1395     val = RSPath::CreateRSPath(path);
1396     return val != nullptr;
1397 }
1398 #else
1399 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSPath>& val)
1400 {
1401     if (!val) {
1402         ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling RSPath is nullptr");
1403         return parcel.WriteInt32(-1);
1404     }
1405     if (val->GetDrawingPath().GetDrawingType() != Drawing::DrawingType::RECORDING) {
1406         ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling Drawing::Path is invalid");
1407         return parcel.WriteInt32(-1);
1408     }
1409     auto recordingPath = static_cast<const Drawing::RecordingPath&>(val->GetDrawingPath());
1410     auto cmdListData = recordingPath.GetCmdList()->GetData();
1411     bool ret = parcel.WriteInt32(cmdListData.second);
1412     if (cmdListData.second == 0) {
1413         ROSEN_LOGW("unirender: RSMarshallingHelper::Marshalling RecordingPathCmdList size is 0");
1414         return ret;
1415     }
1416 
1417     ret &= RSMarshallingHelper::WriteToParcel(parcel, cmdListData.first, cmdListData.second);
1418     if (!ret) {
1419         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::RecordingPathCmdList");
1420     }
1421 
1422     return ret;
1423 }
1424 
1425 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSPath>& val)
1426 {
1427     int32_t size = parcel.ReadInt32();
1428     if (size == -1) {
1429         val = nullptr;
1430         return true;
1431     }
1432     if (size == 0) {
1433         ROSEN_LOGW("unirender: RSMarshallingHelper::Unmarshalling Drawing::PathCmdList size is 0");
1434         val = RSPath::CreateRSPath();
1435         return true;
1436     }
1437     const void* data = RSMarshallingHelper::ReadFromParcel(parcel, size);
1438     if (data == nullptr) {
1439         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling RSPath");
1440         return false;
1441     }
1442     auto pathCmdList = Drawing::PathCmdList::CreateFromData({ data, size }, true);
1443     if (pathCmdList == nullptr) {
1444         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling RSPath path cmdlist is nullptr");
1445         return false;
1446     }
1447     auto path = pathCmdList->Playback();
1448     val = RSPath::CreateRSPath(*path);
1449     return val != nullptr;
1450 }
1451 #endif
1452 
1453 // RSMask
1454 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSMask>& val)
1455 {
1456     if (!val) {
1457         ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling RSMask is nullptr");
1458         return parcel.WriteInt32(-1);
1459     }
1460     return parcel.WriteInt32(1) && val->Marshalling(parcel);
1461 }
1462 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSMask>& val)
1463 {
1464     if (parcel.ReadInt32() == -1) {
1465         val = nullptr;
1466         return true;
1467     }
1468     val.reset(RSMask::Unmarshalling(parcel));
1469     return val != nullptr;
1470 }
1471 
1472 // RSFilter
1473 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSFilter>& val)
1474 {
1475     if (!val) {
1476         return parcel.WriteInt32(RSFilter::NONE);
1477     }
1478     bool success = parcel.WriteInt32(static_cast<int>(val->GetFilterType()));
1479     switch (val->GetFilterType()) {
1480         case RSFilter::BLUR: {
1481             auto blur = std::static_pointer_cast<RSBlurFilter>(val);
1482             success = success && parcel.WriteFloat(blur->GetBlurRadiusX()) && parcel.WriteFloat(blur->GetBlurRadiusY());
1483             break;
1484         }
1485         case RSFilter::MATERIAL: {
1486             auto material = std::static_pointer_cast<RSMaterialFilter>(val);
1487             success = success && parcel.WriteFloat(material->radius_) && parcel.WriteFloat(material->saturation_) &&
1488                       parcel.WriteFloat(material->brightness_) &&
1489                       RSMarshallingHelper::Marshalling(parcel, material->maskColor_) &&
1490                       parcel.WriteInt32(material->colorMode_);
1491             break;
1492         }
1493         case RSFilter::LIGHT_UP_EFFECT: {
1494             auto lightUp = std::static_pointer_cast<RSLightUpEffectFilter>(val);
1495             success = success && parcel.WriteFloat(lightUp->lightUpDegree_);
1496             break;
1497         }
1498         default:
1499             break;
1500     }
1501     return success;
1502 }
1503 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSFilter>& val)
1504 {
1505     int type = 0;
1506     bool success = parcel.ReadInt32(type);
1507     switch (static_cast<RSFilter::FilterType>(type)) {
1508         case RSFilter::BLUR: {
1509             float blurRadiusX;
1510             float blurRadiusY;
1511             success = success && parcel.ReadFloat(blurRadiusX) && parcel.ReadFloat(blurRadiusY);
1512             if (success) {
1513                 val = RSFilter::CreateBlurFilter(blurRadiusX, blurRadiusY);
1514             }
1515             break;
1516         }
1517         case RSFilter::MATERIAL: {
1518             MaterialParam materialParam;
1519             int colorMode;
1520             success = success && parcel.ReadFloat(materialParam.radius) && parcel.ReadFloat(materialParam.saturation) &&
1521                       parcel.ReadFloat(materialParam.brightness) &&
1522                       RSMarshallingHelper::Unmarshalling(parcel, materialParam.maskColor) &&
1523                       parcel.ReadInt32(colorMode);
1524             if (success) {
1525                 val = std::make_shared<RSMaterialFilter>(materialParam, static_cast<BLUR_COLOR_MODE>(colorMode));
1526             }
1527             break;
1528         }
1529         case RSFilter::LIGHT_UP_EFFECT: {
1530             float lightUpDegree;
1531             success = success && parcel.ReadFloat(lightUpDegree);
1532             if (success) {
1533                 val = RSFilter::CreateLightUpEffectFilter(lightUpDegree);
1534             }
1535             break;
1536         }
1537         default: {
1538             val = nullptr;
1539             break;
1540         }
1541     }
1542     return success;
1543 }
1544 
1545 // RSImageBase
1546 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSImageBase>& val)
1547 {
1548     if (!val) {
1549         ROSEN_LOGD("RSMarshallingHelper::Marshalling RSImageBase is nullptr");
1550         return parcel.WriteInt32(-1);
1551     }
1552     return parcel.WriteInt32(1) && val->Marshalling(parcel);
1553 }
1554 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSImageBase>& val)
1555 {
1556     if (parcel.ReadInt32() == -1) {
1557         val = nullptr;
1558         return true;
1559     }
1560     val.reset(RSImageBase::Unmarshalling(parcel));
1561     return val != nullptr;
1562 }
1563 
1564 // RSImage
1565 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSImage>& val)
1566 {
1567     if (!val) {
1568         ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling RSImage is nullptr");
1569         return parcel.WriteInt32(-1);
1570     }
1571     return parcel.WriteInt32(1) && val->Marshalling(parcel);
1572 }
1573 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSImage>& val)
1574 {
1575     if (parcel.ReadInt32() == -1) {
1576         val = nullptr;
1577         return true;
1578     }
1579     val.reset(RSImage::Unmarshalling(parcel));
1580     return val != nullptr;
1581 }
1582 
1583 // Media::PixelMap
1584 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<Media::PixelMap>& val)
1585 {
1586     if (!val) {
1587         return parcel.WriteInt32(-1);
1588     }
1589     auto position = parcel.GetWritePosition();
1590     if (!(parcel.WriteInt32(1) && val->Marshalling(parcel))) {
1591         ROSEN_LOGE("failed RSMarshallingHelper::Marshalling Media::PixelMap");
1592         return false;
1593     }
1594     // correct pixelmap size recorded in Parcel
1595     *reinterpret_cast<int32_t*>(parcel.GetData() + position) =
1596         static_cast<int32_t>(parcel.GetWritePosition() - position - sizeof(int32_t));
1597     return true;
1598 }
1599 
1600 static void CustomFreePixelMap(void* addr, void* context, uint32_t size)
1601 {
1602     MemoryTrack::Instance().RemovePictureRecord(addr);
1603 }
1604 
1605 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<Media::PixelMap>& val)
1606 {
1607     if (parcel.ReadInt32() == -1) {
1608         val = nullptr;
1609         return true;
1610     }
1611     val.reset(Media::PixelMap::Unmarshalling(parcel));
1612     if (val == nullptr) {
1613         ROSEN_LOGE("failed RSMarshallingHelper::Unmarshalling Media::PixelMap");
1614         return false;
1615     }
1616     MemoryInfo info = { val->GetByteCount(), 0, 0, MEMORY_TYPE::MEM_PIXELMAP }; // pid is set to 0 temporarily.
1617     MemoryTrack::Instance().AddPictureRecord(val->GetPixels(), info);
1618     val->SetFreePixelMapProc(CustomFreePixelMap);
1619     return true;
1620 }
1621 bool RSMarshallingHelper::SkipPixelMap(Parcel& parcel)
1622 {
1623     auto size = parcel.ReadInt32();
1624     if (size != -1) {
1625         parcel.SkipBytes(size);
1626     }
1627     return true;
1628 }
1629 
1630 // RectF
1631 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RectT<float>>& val)
1632 {
1633     if (!val) {
1634         ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling RectF is nullptr");
1635         return parcel.WriteInt32(-1);
1636     }
1637     return parcel.WriteInt32(1) && val->Marshalling(parcel);
1638 }
1639 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RectT<float>>& val)
1640 {
1641     if (parcel.ReadInt32() == -1) {
1642         val = nullptr;
1643         return true;
1644     }
1645     val.reset(RectT<float>::Unmarshalling(parcel));
1646     return val != nullptr;
1647 }
1648 
1649 // RRect
1650 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const RRectT<float>& val)
1651 {
1652     return Marshalling(parcel, val.rect_) && Marshalling(parcel, val.radius_[0]) &&
1653            Marshalling(parcel, val.radius_[1]) && Marshalling(parcel, val.radius_[2]) &&
1654            Marshalling(parcel, val.radius_[3]);
1655 }
1656 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, RRectT<float>& val)
1657 {
1658     return Unmarshalling(parcel, val.rect_) && Unmarshalling(parcel, val.radius_[0]) &&
1659            Unmarshalling(parcel, val.radius_[1]) && Unmarshalling(parcel, val.radius_[2]) &&
1660            Unmarshalling(parcel, val.radius_[3]);
1661 }
1662 
1663 #ifndef USE_ROSEN_DRAWING
1664 #ifdef NEW_SKIA
1665 // SkPaint
1666 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const SkSamplingOptions& val)
1667 {
1668     bool success = parcel.WriteBool(val.useCubic) && parcel.WriteFloat(val.cubic.B) && parcel.WriteFloat(val.cubic.C) &&
1669                    parcel.WriteInt32(static_cast<int32_t>(val.filter)) &&
1670                    parcel.WriteInt32(static_cast<int32_t>(val.mipmap));
1671     return success;
1672 }
1673 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, SkSamplingOptions& val)
1674 {
1675     bool useCubic = false;
1676     float b = 0;
1677     float c = 0;
1678     int32_t filter = 0;
1679     int32_t mipmap = 0;
1680     bool success = parcel.ReadBool(useCubic) && parcel.ReadFloat(b) && parcel.ReadFloat(c) &&
1681                    parcel.ReadInt32(filter) && parcel.ReadInt32(mipmap);
1682     if (!success) {
1683         ROSEN_LOGE("failed RSMarshallingHelper::Unmarshalling SkSamplingOptions");
1684         return false;
1685     }
1686     if (useCubic) {
1687         SkCubicResampler cubicResampler;
1688         cubicResampler.B = b;
1689         cubicResampler.C = c;
1690         SkSamplingOptions options(cubicResampler);
1691         val = options;
1692     } else {
1693         SkSamplingOptions options(static_cast<SkFilterMode>(filter), static_cast<SkMipmapMode>(mipmap));
1694         val = options;
1695     }
1696     return true;
1697 }
1698 #endif
1699 #endif
1700 
1701 #ifdef USE_ROSEN_DRAWING
1702 // Drawing::DrawCmdList
1703 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<Drawing::DrawCmdList>& val)
1704 {
1705     if (!val) {
1706         return parcel.WriteInt32(-1);
1707     }
1708     auto cmdListData = val->GetData();
1709     bool ret = parcel.WriteInt32(cmdListData.second);
1710     parcel.WriteInt32(val->GetWidth());
1711     parcel.WriteInt32(val->GetHeight());
1712     if (cmdListData.second == 0) {
1713         ROSEN_LOGW("unirender: RSMarshallingHelper::Marshalling Drawing::DrawCmdList, size is 0");
1714         return ret;
1715     }
1716     ret &= RSMarshallingHelper::WriteToParcel(parcel, cmdListData.first, cmdListData.second);
1717     if (!ret) {
1718         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::DrawCmdList");
1719         return ret;
1720     }
1721 
1722     auto imageData = val->GetAllImageData();
1723     ret &= parcel.WriteInt32(imageData.second);
1724     if (!ret) {
1725         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::DrawCmdList image size");
1726         return ret;
1727     }
1728     if (imageData.second > 0) {
1729         ret &= RSMarshallingHelper::WriteToParcel(parcel, imageData.first, imageData.second);
1730         if (!ret) {
1731             ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::DrawCmdList image");
1732             return ret;
1733         }
1734     }
1735 
1736     std::vector<std::shared_ptr<Media::PixelMap>> pixelMapVec;
1737     uint32_t pixelMapSize = val->GetAllPixelMap(pixelMapVec);
1738     ret &= parcel.WriteUint32(pixelMapSize);
1739     if (pixelMapSize == 0) {
1740         return ret;
1741     }
1742     for (const auto& pixelMap : pixelMapVec) {
1743         ret &= RSMarshallingHelper::Marshalling(parcel, pixelMap);
1744         if (!ret) {
1745             ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::DrawCmdList pixelMap");
1746             return ret;
1747         }
1748     }
1749     return ret;
1750 }
1751 
1752 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<Drawing::DrawCmdList>& val)
1753 {
1754     int32_t size = parcel.ReadInt32();
1755     if (size == -1) {
1756         val = nullptr;
1757         return true;
1758     }
1759     int32_t width = parcel.ReadInt32();
1760     int32_t height = parcel.ReadInt32();
1761     if (size == 0) {
1762         ROSEN_LOGW("unirender: RSMarshallingHelper::Unmarshalling Drawing::DrawCmdList size is 0");
1763         val = std::make_shared<Drawing::DrawCmdList>(width, height);
1764         return true;
1765     }
1766 
1767     const void* data = RSMarshallingHelper::ReadFromParcel(parcel, size);
1768     if (data == nullptr) {
1769         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling Drawing::DrawCmdList");
1770         return false;
1771     }
1772 
1773     val = Drawing::DrawCmdList::CreateFromData({ data, size }, true);
1774     if (val == nullptr) {
1775         ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling Drawing::DrawCmdList is nullptr");
1776         return false;
1777     }
1778     val->SetWidth(width);
1779     val->SetHeight(height);
1780     int32_t imageSize = parcel.ReadInt32();
1781     if (imageSize > 0) {
1782         const void* imageData = RSMarshallingHelper::ReadFromParcel(parcel, imageSize);
1783         if (imageData == nullptr) {
1784             ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling Drawing::DrawCmdList image is nullptr");
1785             return false;
1786         }
1787         val->SetUpImageData(imageData, imageSize);
1788     }
1789 
1790     uint32_t pixelMapSize = parcel.ReadUint32();
1791     if (pixelMapSize == 0) {
1792         return true;
1793     }
1794     bool ret = true;
1795     std::vector<std::shared_ptr<Media::PixelMap>> pixelMapVec;
1796     for (uint32_t i = 0; i < pixelMapSize; i++) {
1797         std::shared_ptr<Media::PixelMap> pixelMap = std::make_shared<Media::PixelMap>();
1798         ret &= RSMarshallingHelper::Unmarshalling(parcel, pixelMap);
1799         if (!ret) {
1800             ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling DrawCmdList pixelMap: %d", i);
1801             return ret;
1802         }
1803         pixelMapVec.emplace_back(pixelMap);
1804     }
1805     val->SetupPixelMap(pixelMapVec);
1806     return ret;
1807 }
1808 #endif
1809 
1810 #define MARSHALLING_AND_UNMARSHALLING(TYPE)                                                 \
1811     bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<TYPE>& val) \
1812     {                                                                                       \
1813         return parcel.WriteParcelable(val.get());                                           \
1814     }                                                                                       \
1815     bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<TYPE>& val)     \
1816     {                                                                                       \
1817         val.reset(parcel.ReadParcelable<TYPE>());                                           \
1818         return val != nullptr;                                                              \
1819     }
1820 MARSHALLING_AND_UNMARSHALLING(RSRenderTransition)
1821 MARSHALLING_AND_UNMARSHALLING(RSRenderTransitionEffect)
1822 #ifndef USE_ROSEN_DRAWING
1823 MARSHALLING_AND_UNMARSHALLING(DrawCmdList)
1824 #endif
1825 #undef MARSHALLING_AND_UNMARSHALLING
1826 
1827 #define MARSHALLING_AND_UNMARSHALLING(TEMPLATE)                                                 \
1828     bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<TEMPLATE>& val) \
1829     {                                                                                           \
1830         return parcel.WriteParcelable(val.get());                                               \
1831     }                                                                                           \
1832     bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<TEMPLATE>& val)     \
1833     {                                                                                           \
1834         val.reset(parcel.ReadParcelable<TEMPLATE>());                                           \
1835         return val != nullptr;                                                                  \
1836     }
1837 
1838 MARSHALLING_AND_UNMARSHALLING(RSRenderCurveAnimation)
1839 MARSHALLING_AND_UNMARSHALLING(RSRenderParticleAnimation)
1840 MARSHALLING_AND_UNMARSHALLING(RSRenderInterpolatingSpringAnimation)
1841 MARSHALLING_AND_UNMARSHALLING(RSRenderKeyframeAnimation)
1842 MARSHALLING_AND_UNMARSHALLING(RSRenderSpringAnimation)
1843 MARSHALLING_AND_UNMARSHALLING(RSRenderPathAnimation)
1844 #undef MARSHALLING_AND_UNMARSHALLING
1845 
1846 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSRenderModifier>& val)
1847 {
1848     return val != nullptr && val->Marshalling(parcel);
1849 }
1850 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSRenderModifier>& val)
1851 {
1852     val.reset(RSRenderModifier::Unmarshalling(parcel));
1853     return val != nullptr;
1854 }
1855 
1856 #define MARSHALLING_AND_UNMARSHALLING(TEMPLATE)                                                                       \
1857     template<typename T>                                                                                              \
1858     bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<TEMPLATE<T>>& val)                    \
1859     {                                                                                                                 \
1860         return parcel.WriteInt16(static_cast<int16_t>(val->GetPropertyType())) && parcel.WriteUint64(val->GetId()) && \
1861                Marshalling(parcel, val->Get());                                                                       \
1862     }                                                                                                                 \
1863     template<typename T>                                                                                              \
1864     bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<TEMPLATE<T>>& val)                        \
1865     {                                                                                                                 \
1866         PropertyId id = 0;                                                                                            \
1867         int16_t typeId = 0;                                                                                           \
1868         if (!parcel.ReadInt16(typeId)) {                                                                              \
1869             return false;                                                                                             \
1870         }                                                                                                             \
1871         RSRenderPropertyType type = static_cast<RSRenderPropertyType>(typeId);                                        \
1872         if (!parcel.ReadUint64(id)) {                                                                                 \
1873             return false;                                                                                             \
1874         }                                                                                                             \
1875         T value;                                                                                                      \
1876         if (!Unmarshalling(parcel, value)) {                                                                          \
1877             return false;                                                                                             \
1878         }                                                                                                             \
1879         val.reset(new TEMPLATE<T>(value, id, type));                                                                  \
1880         return val != nullptr;                                                                                        \
1881     }
1882 
1883 MARSHALLING_AND_UNMARSHALLING(RSRenderProperty)
1884 MARSHALLING_AND_UNMARSHALLING(RSRenderAnimatableProperty)
1885 #undef MARSHALLING_AND_UNMARSHALLING
1886 
1887 #define EXPLICIT_INSTANTIATION(TEMPLATE, TYPE)                                                                  \
1888     template bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<TEMPLATE<TYPE>>& val); \
1889     template bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<TEMPLATE<TYPE>>& val);
1890 
1891 #ifndef USE_ROSEN_DRAWING
1892 #ifdef NEW_SKIA
1893 #define BATCH_EXPLICIT_INSTANTIATION(TEMPLATE)                                       \
1894     EXPLICIT_INSTANTIATION(TEMPLATE, bool)                                           \
1895     EXPLICIT_INSTANTIATION(TEMPLATE, float)                                          \
1896     EXPLICIT_INSTANTIATION(TEMPLATE, int)                                            \
1897     EXPLICIT_INSTANTIATION(TEMPLATE, Color)                                          \
1898     EXPLICIT_INSTANTIATION(TEMPLATE, Gravity)                                        \
1899     EXPLICIT_INSTANTIATION(TEMPLATE, GradientDirection)                              \
1900     EXPLICIT_INSTANTIATION(TEMPLATE, ForegroundColorStrategyType)                    \
1901     EXPLICIT_INSTANTIATION(TEMPLATE, Matrix3f)                                       \
1902     EXPLICIT_INSTANTIATION(TEMPLATE, Quaternion)                                     \
1903     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSFilter>)                      \
1904     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSImage>)                       \
1905     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSMask>)                        \
1906     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSPath>)                        \
1907     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSShader>)                      \
1908     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSLinearGradientBlurPara>)      \
1909     EXPLICIT_INSTANTIATION(TEMPLATE, std::vector<std::shared_ptr<ParticleRenderParams>>)              \
1910     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<ParticleRenderParams>)          \
1911     EXPLICIT_INSTANTIATION(TEMPLATE, RenderParticleColorParaType)                    \
1912     EXPLICIT_INSTANTIATION(TEMPLATE, RenderParticleParaType<float>)                  \
1913     EXPLICIT_INSTANTIATION(TEMPLATE, ParticleVelocity)                               \
1914     EXPLICIT_INSTANTIATION(TEMPLATE, EmitterConfig)                                  \
1915     EXPLICIT_INSTANTIATION(TEMPLATE, Vector2f)                                       \
1916     EXPLICIT_INSTANTIATION(TEMPLATE, Vector4<uint32_t>)                              \
1917     EXPLICIT_INSTANTIATION(TEMPLATE, Vector4<Color>)                                 \
1918     EXPLICIT_INSTANTIATION(TEMPLATE, Vector4f)                                       \
1919     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<DrawCmdList>)                   \
1920     EXPLICIT_INSTANTIATION(TEMPLATE, SkMatrix)                                       \
1921     EXPLICIT_INSTANTIATION(TEMPLATE, SkM44)
1922 #else
1923 #define BATCH_EXPLICIT_INSTANTIATION(TEMPLATE)                                       \
1924     EXPLICIT_INSTANTIATION(TEMPLATE, bool)                                           \
1925     EXPLICIT_INSTANTIATION(TEMPLATE, float)                                          \
1926     EXPLICIT_INSTANTIATION(TEMPLATE, int)                                            \
1927     EXPLICIT_INSTANTIATION(TEMPLATE, Color)                                          \
1928     EXPLICIT_INSTANTIATION(TEMPLATE, Gravity)                                        \
1929     EXPLICIT_INSTANTIATION(TEMPLATE, GradientDirection)                              \
1930     EXPLICIT_INSTANTIATION(TEMPLATE, ForegroundColorStrategyType)                    \
1931     EXPLICIT_INSTANTIATION(TEMPLATE, Matrix3f)                                       \
1932     EXPLICIT_INSTANTIATION(TEMPLATE, Quaternion)                                     \
1933     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSFilter>)                      \
1934     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSImage>)                       \
1935     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSMask>)                        \
1936     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSPath>)                        \
1937     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSShader>)                      \
1938     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSLinearGradientBlurPara>)      \
1939     EXPLICIT_INSTANTIATION(TEMPLATE, std::vector<std::shared_ptr<ParticleRenderParams>>)              \
1940     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<ParticleRenderParams>)          \
1941     EXPLICIT_INSTANTIATION(TEMPLATE, RenderParticleColorParaType)                    \
1942     EXPLICIT_INSTANTIATION(TEMPLATE, RenderParticleParaType<float>)                  \
1943     EXPLICIT_INSTANTIATION(TEMPLATE, ParticleVelocity)                               \
1944     EXPLICIT_INSTANTIATION(TEMPLATE, EmitterConfig)                                  \
1945     EXPLICIT_INSTANTIATION(TEMPLATE, Vector2f)                                       \
1946     EXPLICIT_INSTANTIATION(TEMPLATE, Vector4<uint32_t>)                              \
1947     EXPLICIT_INSTANTIATION(TEMPLATE, Vector4<Color>)                                 \
1948     EXPLICIT_INSTANTIATION(TEMPLATE, Vector4f)                                       \
1949     EXPLICIT_INSTANTIATION(TEMPLATE, RRectT<float>)                                  \
1950     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<DrawCmdList>)                   \
1951     EXPLICIT_INSTANTIATION(TEMPLATE, SkMatrix)
1952 #endif
1953 #else
1954 #define BATCH_EXPLICIT_INSTANTIATION(TEMPLATE)                                     \
1955     EXPLICIT_INSTANTIATION(TEMPLATE, bool)                                         \
1956     EXPLICIT_INSTANTIATION(TEMPLATE, float)                                        \
1957     EXPLICIT_INSTANTIATION(TEMPLATE, int)                                          \
1958     EXPLICIT_INSTANTIATION(TEMPLATE, Color)                                        \
1959     EXPLICIT_INSTANTIATION(TEMPLATE, Gravity)                                      \
1960     EXPLICIT_INSTANTIATION(TEMPLATE, GradientDirection)                            \
1961     EXPLICIT_INSTANTIATION(TEMPLATE, ForegroundColorStrategyType)                  \
1962     EXPLICIT_INSTANTIATION(TEMPLATE, Matrix3f)                                     \
1963     EXPLICIT_INSTANTIATION(TEMPLATE, Quaternion)                                   \
1964     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSFilter>)                    \
1965     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSImage>)                     \
1966     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSMask>)                      \
1967     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSPath>)                      \
1968     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSShader>)                    \
1969     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSLinearGradientBlurPara>)    \
1970     EXPLICIT_INSTANTIATION(TEMPLATE, std::vector<std::shared_ptr<ParticleRenderParams>>)              \
1971     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<ParticleRenderParams>)        \
1972     EXPLICIT_INSTANTIATION(TEMPLATE, RenderParticleColorParaType)                  \
1973     EXPLICIT_INSTANTIATION(TEMPLATE, RenderParticleParaType<float>)                \
1974     EXPLICIT_INSTANTIATION(TEMPLATE, ParticleVelocity)                             \
1975     EXPLICIT_INSTANTIATION(TEMPLATE, EmitterConfig)                                \
1976     EXPLICIT_INSTANTIATION(TEMPLATE, Vector2f)                                     \
1977     EXPLICIT_INSTANTIATION(TEMPLATE, Vector4<uint32_t>)                            \
1978     EXPLICIT_INSTANTIATION(TEMPLATE, Vector4<Color>)                               \
1979     EXPLICIT_INSTANTIATION(TEMPLATE, Vector4f)                                     \
1980     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<Drawing::DrawCmdList>)        \
1981     EXPLICIT_INSTANTIATION(TEMPLATE, Drawing::Matrix)
1982 #endif
1983 
1984 BATCH_EXPLICIT_INSTANTIATION(RSRenderProperty)
1985 
1986 #undef EXPLICIT_INSTANTIATION
1987 #undef BATCH_EXPLICIT_INSTANTIATION
1988 
1989 #define EXPLICIT_INSTANTIATION(TEMPLATE, TYPE)                                                                  \
1990     template bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<TEMPLATE<TYPE>>& val); \
1991     template bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<TEMPLATE<TYPE>>& val);
1992 
1993 #define BATCH_EXPLICIT_INSTANTIATION(TEMPLATE)                  \
1994     EXPLICIT_INSTANTIATION(TEMPLATE, float)                     \
1995     EXPLICIT_INSTANTIATION(TEMPLATE, int)                       \
1996     EXPLICIT_INSTANTIATION(TEMPLATE, Color)                     \
1997     EXPLICIT_INSTANTIATION(TEMPLATE, Matrix3f)                  \
1998     EXPLICIT_INSTANTIATION(TEMPLATE, Quaternion)                \
1999     EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSFilter>) \
2000     EXPLICIT_INSTANTIATION(TEMPLATE, Vector2f)                  \
2001     EXPLICIT_INSTANTIATION(TEMPLATE, Vector4<Color>)            \
2002     EXPLICIT_INSTANTIATION(TEMPLATE, Vector4f)                  \
2003     EXPLICIT_INSTANTIATION(TEMPLATE, RRectT<float>)
2004 
2005 BATCH_EXPLICIT_INSTANTIATION(RSRenderAnimatableProperty)
2006 
2007 #undef EXPLICIT_INSTANTIATION
2008 #undef BATCH_EXPLICIT_INSTANTIATION
2009 
2010 bool RSMarshallingHelper::WriteToParcel(Parcel& parcel, const void* data, size_t size)
2011 {
2012     if (data == nullptr) {
2013         ROSEN_LOGE("RSMarshallingHelper::WriteToParcel data is nullptr");
2014         return false;
2015     }
2016     if (size > MAX_DATA_SIZE) {
2017         ROSEN_LOGD("RSMarshallingHelper::WriteToParcel data exceed MAX_DATA_SIZE, size:%zu", size);
2018     }
2019 
2020     if (!parcel.WriteUint32(size)) {
2021         return false;
2022     }
2023 #ifdef RS_ENABLE_RECORDING
2024     if (size < MIN_DATA_SIZE || parcel.GetMaxCapacity() == RSRecordingThread::RECORDING_PARCEL_MAX_CAPCITY) {
2025 #else
2026     if (size < MIN_DATA_SIZE) {
2027 #endif
2028         return parcel.WriteUnpadBuffer(data, size);
2029     }
2030 
2031     // write to ashmem
2032     auto ashmemAllocator = AshmemAllocator::CreateAshmemAllocator(size, PROT_READ | PROT_WRITE);
2033     if (!ashmemAllocator) {
2034         ROSEN_LOGE("RSMarshallingHelper::WriteToParcel CreateAshmemAllocator fail");
2035         return false;
2036     }
2037     int fd = ashmemAllocator->GetFd();
2038     if (!(static_cast<MessageParcel*>(&parcel)->WriteFileDescriptor(fd))) {
2039         ROSEN_LOGE("RSMarshallingHelper::WriteToParcel WriteFileDescriptor error");
2040         return false;
2041     }
2042     if (!ashmemAllocator->WriteToAshmem(data, size)) {
2043         ROSEN_LOGE("RSMarshallingHelper::WriteToParcel memcpy_s failed");
2044         return false;
2045     }
2046     return true;
2047 }
2048 
2049 const void* RSMarshallingHelper::ReadFromParcel(Parcel& parcel, size_t size)
2050 {
2051     uint32_t bufferSize = parcel.ReadUint32();
2052     if (static_cast<unsigned int>(bufferSize) != size) {
2053         ROSEN_LOGE("RSMarshallingHelper::ReadFromParcel size mismatch");
2054         return nullptr;
2055     }
2056 #ifdef RS_ENABLE_RECORDING
2057     if (static_cast<unsigned int>(bufferSize) < MIN_DATA_SIZE ||
2058         parcel.GetMaxCapacity() == RSRecordingThread::RECORDING_PARCEL_MAX_CAPCITY) {
2059 #else
2060     if (static_cast<unsigned int>(bufferSize) < MIN_DATA_SIZE) {
2061 #endif
2062         return parcel.ReadUnpadBuffer(size);
2063     }
2064     // read from ashmem
2065     int fd = static_cast<MessageParcel*>(&parcel)->ReadFileDescriptor();
2066     auto ashmemAllocator = AshmemAllocator::CreateAshmemAllocatorWithFd(fd, size, PROT_READ);
2067     if (!ashmemAllocator) {
2068         ROSEN_LOGE("RSMarshallingHelper::ReadFromParcel CreateAshmemAllocator fail");
2069         return nullptr;
2070     }
2071     return ashmemAllocator->CopyFromAshmem(size);
2072 }
2073 
2074 bool RSMarshallingHelper::SkipFromParcel(Parcel& parcel, size_t size)
2075 {
2076     int32_t bufferSize = parcel.ReadInt32();
2077     if (static_cast<unsigned int>(bufferSize) != size) {
2078         ROSEN_LOGE("RSMarshallingHelper::SkipFromParcel size mismatch");
2079         return false;
2080     }
2081 #ifdef RS_ENABLE_RECORDING
2082     if (static_cast<unsigned int>(bufferSize) < MIN_DATA_SIZE ||
2083         parcel.GetMaxCapacity() == RSRecordingThread::RECORDING_PARCEL_MAX_CAPCITY) {
2084 #else
2085     if (static_cast<unsigned int>(bufferSize) < MIN_DATA_SIZE) {
2086 #endif
2087         parcel.SkipBytes(size);
2088         return true;
2089     }
2090     // read from ashmem
2091     int fd = static_cast<MessageParcel*>(&parcel)->ReadFileDescriptor();
2092     auto ashmemAllocator = AshmemAllocator::CreateAshmemAllocatorWithFd(fd, size, PROT_READ);
2093     return ashmemAllocator != nullptr;
2094 }
2095 
2096 #ifndef USE_ROSEN_DRAWING
2097 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::unique_ptr<OpItem>& val)
2098 {
2099     return RSMarshallingHelper::Marshalling(parcel, val->GetType()) && val->Marshalling(parcel);
2100 }
2101 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::unique_ptr<OpItem>& val)
2102 {
2103     val = nullptr;
2104     RSOpType type;
2105     if (!RSMarshallingHelper::Unmarshalling(parcel, type)) {
2106         ROSEN_LOGE("DrawCmdList::Unmarshalling failed");
2107         return false;
2108     }
2109     auto func = DrawCmdList::GetOpUnmarshallingFunc(type);
2110     if (!func) {
2111         ROSEN_LOGW("unirender: opItem Unmarshalling func not define, optype = %d", type);
2112         return false;
2113     }
2114 
2115     OpItem* item = (*func)(parcel);
2116     if (!item) {
2117         ROSEN_LOGE("unirender: failed opItem Unmarshalling, optype = %d", type);
2118         return false;
2119     }
2120 
2121     val.reset(item);
2122     return true;
2123 }
2124 #endif
2125 } // namespace Rosen
2126 } // namespace OHOS
2127