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 #include "image_type.h"
18 #include "rs_profiler.h"
19
20 #include <cstddef>
21 #include <cstdint>
22 #include <memory>
23 #include <message_parcel.h>
24 #include <sys/mman.h>
25 #include <unistd.h>
26
27 #include "memory/rs_memory_track.h"
28 #include "pixel_map.h"
29
30 #include "animation/rs_render_curve_animation.h"
31 #include "animation/rs_render_interpolating_spring_animation.h"
32 #include "animation/rs_render_keyframe_animation.h"
33 #include "animation/rs_render_particle.h"
34 #include "animation/rs_particle_noise_field.h"
35 #include "animation/rs_render_particle_animation.h"
36 #include "animation/rs_render_path_animation.h"
37 #include "animation/rs_render_spring_animation.h"
38 #include "animation/rs_render_transition.h"
39 #include "common/rs_color.h"
40 #include "common/rs_common_def.h"
41 #include "common/rs_matrix3.h"
42 #include "common/rs_vector4.h"
43 #include "image/image.h"
44 #include "modifier/rs_render_modifier.h"
45 #include "pipeline/rs_draw_cmd.h"
46 #include "platform/common/rs_log.h"
47 #include "recording/record_cmd.h"
48 #include "render/rs_blur_filter.h"
49 #include "render/rs_filter.h"
50 #include "render/rs_gradient_blur_para.h"
51 #include "render/rs_image.h"
52 #include "render/rs_image_base.h"
53 #include "render/rs_light_up_effect_filter.h"
54 #include "render/rs_magnifier_para.h"
55 #include "render/rs_mask.h"
56 #include "render/rs_material_filter.h"
57 #include "render/rs_motion_blur_filter.h"
58 #include "render/rs_path.h"
59 #include "render/rs_pixel_map_shader.h"
60 #include "render/rs_shader.h"
61 #include "text/hm_symbol.h"
62 #include "transaction/rs_ashmem_helper.h"
63
64 #ifdef ROSEN_OHOS
65 #include "buffer_utils.h"
66 #endif
67 #include "recording/mask_cmd_list.h"
68 #include "property/rs_properties_def.h"
69
70 namespace OHOS {
71 namespace Rosen {
72
73 namespace {
74 bool g_useSharedMem = true;
75 std::thread::id g_tid = std::thread::id();
76 constexpr size_t LARGE_MALLOC = 200000000;
77 constexpr size_t PIXELMAP_UNMARSHALLING_DEBUG_OFFSET = 12;
78 }
79
80 #define MARSHALLING_AND_UNMARSHALLING(TYPE, TYPENAME) \
81 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const TYPE& val) \
82 { \
83 return parcel.Write##TYPENAME(val); \
84 } \
85 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, TYPE& val) \
86 { \
87 return parcel.Read##TYPENAME(val); \
88 }
89
90 // basic types
91 MARSHALLING_AND_UNMARSHALLING(bool, Bool)
92 MARSHALLING_AND_UNMARSHALLING(int8_t, Int8)
93 MARSHALLING_AND_UNMARSHALLING(uint8_t, Uint8)
94 MARSHALLING_AND_UNMARSHALLING(int16_t, Int16)
95 MARSHALLING_AND_UNMARSHALLING(uint16_t, Uint16)
96 MARSHALLING_AND_UNMARSHALLING(int32_t, Int32)
97 MARSHALLING_AND_UNMARSHALLING(uint32_t, Uint32)
98 MARSHALLING_AND_UNMARSHALLING(int64_t, Int64)
99 MARSHALLING_AND_UNMARSHALLING(uint64_t, Uint64)
100 MARSHALLING_AND_UNMARSHALLING(float, Float)
101 MARSHALLING_AND_UNMARSHALLING(double, Double)
102
103 #undef MARSHALLING_AND_UNMARSHALLING
104
105 namespace {
MarshallingRecordCmdFromDrawCmdList(Parcel & parcel,const std::shared_ptr<Drawing::DrawCmdList> & val)106 static bool MarshallingRecordCmdFromDrawCmdList(Parcel& parcel, const std::shared_ptr<Drawing::DrawCmdList>& val)
107 {
108 if (!val) {
109 ROSEN_LOGE("unirender: RSMarshallingHelper::MarshallingRecordCmdFromDrawCmdList failed with null CmdList");
110 return false;
111 }
112 std::vector<std::shared_ptr<Drawing::RecordCmd>> recordCmdVec;
113 uint32_t recordCmdSize = val->GetAllRecordCmd(recordCmdVec);
114 if (!parcel.WriteUint32(recordCmdSize)) {
115 return false;
116 }
117 if (recordCmdSize == 0) {
118 return true;
119 }
120 if (recordCmdSize > USHRT_MAX) {
121 ROSEN_LOGE("unirender: RSMarshallingHelper::MarshallingRecordCmdFromDrawCmdList failed with max limit");
122 return false;
123 }
124 for (const auto& recordCmd : recordCmdVec) {
125 if (!RSMarshallingHelper::Marshalling(parcel, recordCmd)) {
126 return false;
127 }
128 }
129 return true;
130 }
131
UnmarshallingRecordCmdToDrawCmdList(Parcel & parcel,std::shared_ptr<Drawing::DrawCmdList> & val)132 static bool UnmarshallingRecordCmdToDrawCmdList(Parcel& parcel, std::shared_ptr<Drawing::DrawCmdList>& val)
133 {
134 if (!val) {
135 ROSEN_LOGE("unirender: RSMarshallingHelper::UnmarshallingRecordCmdToDrawCmdList failed with null CmdList");
136 return false;
137 }
138 uint32_t recordCmdSize = parcel.ReadUint32();
139 if (recordCmdSize == 0) {
140 return true;
141 }
142 if (recordCmdSize > USHRT_MAX) {
143 ROSEN_LOGE("unirender: RSMarshallingHelper::UnmarshallingRecordCmdToDrawCmdList failed with max limit");
144 return false;
145 }
146 uint32_t opItemCount = 0;
147 std::vector<std::shared_ptr<Drawing::RecordCmd>> recordCmdVec;
148 for (uint32_t i = 0; i < recordCmdSize; i++) {
149 std::shared_ptr<Drawing::RecordCmd> recordCmd = nullptr;
150 if (!RSMarshallingHelper::Unmarshalling(parcel, recordCmd, &opItemCount)) {
151 return false;
152 }
153 if (opItemCount > Drawing::MAX_OPITEMSIZE) {
154 ROSEN_LOGE(
155 "unirender: RSMarshallingHelper::UnmarshallingRecordCmdToDrawCmdList failed with max opItemSize");
156 return false;
157 }
158 recordCmdVec.emplace_back(recordCmd);
159 }
160 return val->SetupRecordCmd(recordCmdVec);
161 }
162
MarshallingExtendObjectFromDrawCmdList(Parcel & parcel,const std::shared_ptr<Drawing::DrawCmdList> & val)163 bool MarshallingExtendObjectFromDrawCmdList(Parcel& parcel, const std::shared_ptr<Drawing::DrawCmdList>& val)
164 {
165 if (!val) {
166 ROSEN_LOGE("unirender: RSMarshallingHelper::MarshallingExtendObjectFromDrawCmdList failed with null CmdList");
167 return false;
168 }
169 std::vector<std::shared_ptr<Drawing::ExtendObject>> objectVec;
170 uint32_t objectSize = val->GetAllExtendObject(objectVec);
171 if (!parcel.WriteUint32(objectSize)) {
172 return false;
173 }
174 if (objectSize == 0) {
175 return true;
176 }
177 if (objectSize > USHRT_MAX) {
178 ROSEN_LOGE("unirender: RSMarshallingHelper::MarshallingExtendObjectFromDrawCmdList failed with max limit");
179 return false;
180 }
181 for (const auto& object : objectVec) {
182 if (!object->Marshalling(parcel)) {
183 return false;
184 }
185 }
186 return true;
187 }
188
UnmarshallingExtendObjectToDrawCmdList(Parcel & parcel,std::shared_ptr<Drawing::DrawCmdList> & val)189 bool UnmarshallingExtendObjectToDrawCmdList(Parcel& parcel, std::shared_ptr<Drawing::DrawCmdList>& val)
190 {
191 if (!val) {
192 ROSEN_LOGE("unirender: RSMarshallingHelper::UnmarshallingExtendObjectToDrawCmdList failed with null CmdList");
193 return false;
194 }
195 uint32_t objectSize = parcel.ReadUint32();
196 if (objectSize == 0) {
197 return true;
198 }
199 if (objectSize > USHRT_MAX) {
200 ROSEN_LOGE("unirender: RSMarshallingHelper::UnmarshallingExtendObjectToDrawCmdList failed with max limit");
201 return false;
202 }
203 std::vector<std::shared_ptr<Drawing::ExtendObject>> objectVec;
204 for (uint32_t i = 0; i < objectSize; i++) {
205 std::shared_ptr<RSPixelMapShader> object = std::make_shared<RSPixelMapShader>();
206 if (!object->Unmarshalling(parcel)) {
207 return false;
208 }
209 objectVec.emplace_back(object);
210 }
211 return val->SetupExtendObject(objectVec);
212 }
213 } // namespace
214
215
216 // Drawing::Data
Marshalling(Parcel & parcel,std::shared_ptr<Drawing::Data> val)217 bool RSMarshallingHelper::Marshalling(Parcel& parcel, std::shared_ptr<Drawing::Data> val)
218 {
219 if (!val) {
220 return parcel.WriteUint32(UINT32_MAX);
221 }
222
223 uint32_t size = val->GetSize();
224 if (size == UINT32_MAX) {
225 ROSEN_LOGE("unirender: RSMarshallingHelper::Marshalling Data failed with max limit");
226 return false;
227 }
228
229 if (size == 0) {
230 ROSEN_LOGW("unirender: RSMarshallingHelper::Marshalling Data size is 0");
231 return parcel.WriteUint32(0);
232 }
233
234 const void* data = val->GetData();
235 if (!data) {
236 ROSEN_LOGE("unirender: RSMarshallingHelper::Marshalling Data failed with max nullptr");
237 return false;
238 }
239
240 if (!parcel.WriteUint32(size) || !RSMarshallingHelper::WriteToParcel(parcel, data, size)) {
241 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Data");
242 return false;
243 }
244 return true;
245 }
246
Unmarshalling(Parcel & parcel,std::shared_ptr<Drawing::Data> & val)247 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<Drawing::Data>& val)
248 {
249 uint32_t size = parcel.ReadUint32();
250 if (size == UINT32_MAX) {
251 val = nullptr;
252 return true;
253 }
254 val = std::make_shared<Drawing::Data>();
255 if (size == 0) {
256 ROSEN_LOGW("unirender: RSMarshallingHelper::Unmarshalling Data size is 0");
257 val->BuildUninitialized(0);
258 return true;
259 }
260
261 bool isMalloc = false;
262 const void* data = RSMarshallingHelper::ReadFromParcel(parcel, size, isMalloc);
263 if (data == nullptr) {
264 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling Data failed with read data");
265 return false;
266 }
267
268 bool ret = false;
269 if (!isMalloc) {
270 ret = val->BuildWithoutCopy(data, size);
271 } else {
272 ret = val->BuildFromMalloc(data, size);
273 }
274 if (!ret) {
275 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling Data failed with Build Data");
276 }
277 return ret;
278 }
279
SkipData(Parcel & parcel)280 bool RSMarshallingHelper::SkipData(Parcel& parcel)
281 {
282 int32_t size = parcel.ReadInt32();
283 if (size <= 0) {
284 return true;
285 }
286 return SkipFromParcel(parcel, size);
287 }
288
UnmarshallingWithCopy(Parcel & parcel,std::shared_ptr<Drawing::Data> & val)289 bool RSMarshallingHelper::UnmarshallingWithCopy(Parcel& parcel, std::shared_ptr<Drawing::Data>& val)
290 {
291 bool success = Unmarshalling(parcel, val);
292 if (success && val && val->GetSize() < MIN_DATA_SIZE) {
293 val->BuildWithCopy(val->GetData(), val->GetSize());
294 }
295 return success;
296 }
297
298
Marshalling(Parcel & parcel,const Drawing::Bitmap & val)299 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const Drawing::Bitmap& val)
300 {
301 Drawing::BitmapFormat bitmapFormat = val.GetFormat();
302 Marshalling(parcel, bitmapFormat);
303
304 std::shared_ptr<Drawing::Data> data = val.Serialize();
305 if (!data) {
306 ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling Bitmap is nullptr");
307 return false;
308 }
309 Marshalling(parcel, data);
310 return true;
311 }
312
Unmarshalling(Parcel & parcel,Drawing::Bitmap & val)313 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, Drawing::Bitmap& val)
314 {
315 Drawing::BitmapFormat bitmapFormat;
316 if (!Unmarshalling(parcel, bitmapFormat)) {
317 ROSEN_LOGE("RSMarshallingHelper::Unmarshalling read BitmapFormat in Bitmap failed");
318 return false;
319 }
320
321 val.SetFormat(bitmapFormat);
322
323 std::shared_ptr<Drawing::Data> data;
324 if (!Unmarshalling(parcel, data) || !data) {
325 ROSEN_LOGE("failed RSMarshallingHelper::Unmarshalling Drawing::Bitmap");
326 return false;
327 }
328
329 if (!val.Deserialize(data)) {
330 ROSEN_LOGE("failed RSMarshallingHelper::Unmarshalling Drawing::Bitmap Deserialize");
331 return false;
332 }
333
334 return true;
335 }
336
SkFreeReleaseproc(const void * ptr,void *)337 static void SkFreeReleaseproc(const void* ptr, void*)
338 {
339 MemoryTrack::Instance().RemovePictureRecord(ptr);
340 free(const_cast<void*>(ptr));
341 ptr = nullptr;
342 }
343
Marshalling(Parcel & parcel,std::shared_ptr<Drawing::Typeface> & typeface)344 bool RSMarshallingHelper::Marshalling(Parcel& parcel, std::shared_ptr<Drawing::Typeface>& typeface)
345 {
346 if (!typeface) {
347 ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling Typeface is nullptr");
348 return false;
349 }
350 std::shared_ptr<Drawing::Data> data = typeface->Serialize();
351 if (!data) {
352 ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling Typeface serialize failed");
353 return false;
354 }
355 Marshalling(parcel, data);
356 return true;
357 }
358
Unmarshalling(Parcel & parcel,std::shared_ptr<Drawing::Typeface> & typeface)359 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<Drawing::Typeface>& typeface)
360 {
361 std::shared_ptr<Drawing::Data> data;
362 if (!Unmarshalling(parcel, data) || !data) {
363 ROSEN_LOGE("failed RSMarshallingHelper::Unmarshalling Drawing::Typeface data");
364 return false;
365 }
366 typeface = Drawing::Typeface::Deserialize(data->GetData(), data->GetSize());
367 if (typeface == nullptr) {
368 ROSEN_LOGE("failed RSMarshallingHelper::Unmarshalling Drawing::Typeface Deserialize");
369 return false;
370 }
371 return true;
372 }
373
Marshalling(Parcel & parcel,const std::shared_ptr<Drawing::Image> & val)374 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<Drawing::Image>& val)
375 {
376 if (!val) {
377 return parcel.WriteInt32(-1);
378 }
379 int32_t type = val->IsLazyGenerated();
380 parcel.WriteInt32(type);
381 if (type == 1) {
382 auto data = val->Serialize();
383 return Marshalling(parcel, data);
384 } else {
385 Drawing::Bitmap bitmap;
386 if (!val->GetROPixels(bitmap)) {
387 ROSEN_LOGE("RSMarshallingHelper::Marshalling get bitmap failed");
388 return false;
389 }
390 Drawing::Pixmap pixmap;
391 if (!bitmap.PeekPixels(pixmap)) {
392 ROSEN_LOGE("RSMarshallingHelper::Marshalling peek pixels failed");
393 return false;
394 }
395 size_t rb = pixmap.GetRowBytes();
396 int width = pixmap.GetWidth();
397 int height = pixmap.GetHeight();
398 const void* addr = pixmap.GetAddr();
399 size_t size = bitmap.ComputeByteSize();
400
401 parcel.WriteUint32(size);
402 if (!WriteToParcel(parcel, addr, size)) {
403 ROSEN_LOGE("RSMarshallingHelper::Marshalling Image write parcel failed");
404 return false;
405 }
406
407 parcel.WriteUint32(rb);
408 parcel.WriteInt32(width);
409 parcel.WriteInt32(height);
410 parcel.WriteUint32(pixmap.GetColorType());
411 parcel.WriteUint32(pixmap.GetAlphaType());
412
413 if (pixmap.GetColorSpace() == nullptr) {
414 parcel.WriteUint32(0);
415 return true;
416 } else {
417 auto data = pixmap.GetColorSpace()->Serialize();
418 if (data == nullptr || data->GetSize() == 0) {
419 parcel.WriteUint32(0);
420 return true;
421 }
422 parcel.WriteUint32(data->GetSize());
423 if (!WriteToParcel(parcel, data->GetData(), data->GetSize())) {
424 ROSEN_LOGE("RSMarshallingHelper::Marshalling data write parcel failed");
425 return false;
426 }
427 }
428 return true;
429 }
430 }
431
ReadColorSpaceFromParcel(Parcel & parcel,std::shared_ptr<Drawing::ColorSpace> & colorSpace)432 bool RSMarshallingHelper::ReadColorSpaceFromParcel(Parcel& parcel, std::shared_ptr<Drawing::ColorSpace>& colorSpace)
433 {
434 size_t size = parcel.ReadUint32();
435 if (size == 0) {
436 colorSpace = nullptr;
437 } else {
438 bool isMal = false;
439 auto data = std::make_shared<Drawing::Data>();
440 const void* dataPtr = RSMarshallingHelper::ReadFromParcel(parcel, size, isMal);
441 if (dataPtr == nullptr) {
442 ROSEN_LOGE("dataPtr is nullptr");
443 return false;
444 }
445 if (data->BuildWithoutCopy(dataPtr, size) == false) {
446 if (isMal) {
447 free(const_cast<void*>(dataPtr));
448 dataPtr = nullptr;
449 }
450 ROSEN_LOGE("data build without copy failed");
451 return false;
452 }
453 if (colorSpace->Deserialize(data) == false) {
454 if (isMal) {
455 free(const_cast<void*>(dataPtr));
456 dataPtr = nullptr;
457 }
458 ROSEN_LOGE("colorSpace deserialize failed");
459 return false;
460 }
461 if (isMal) {
462 free(const_cast<void*>(dataPtr));
463 dataPtr = nullptr;
464 }
465 }
466 return true;
467 }
468
UnmarshallingNoLazyGeneratedImage(Parcel & parcel,std::shared_ptr<Drawing::Image> & val,void * & imagepixelAddr)469 bool RSMarshallingHelper::UnmarshallingNoLazyGeneratedImage(Parcel& parcel,
470 std::shared_ptr<Drawing::Image>& val, void*& imagepixelAddr)
471 {
472 size_t pixmapSize = parcel.ReadUint32();
473 bool isMalloc = false;
474 const void* addr = RSMarshallingHelper::ReadFromParcel(parcel, pixmapSize, isMalloc);
475 if (addr == nullptr) {
476 ROSEN_LOGE("failed RSMarshallingHelper::Unmarshalling Drawing::Image read from parcel");
477 return false;
478 }
479
480 size_t rb = parcel.ReadUint32();
481 int width = parcel.ReadInt32();
482 int height = parcel.ReadInt32();
483
484 size_t ct = parcel.ReadUint32();
485 Drawing::ColorType colorType = Drawing::ColorType::COLORTYPE_UNKNOWN;
486 if (ct >= Drawing::ColorType::COLORTYPE_ALPHA_8 && ct <= Drawing::ColorType::COLORTYPE_RGB_888X) {
487 colorType = static_cast<Drawing::ColorType>(ct);
488 }
489 size_t at = parcel.ReadUint32();
490 Drawing::AlphaType alphaType = Drawing::AlphaType::ALPHATYPE_UNKNOWN;
491 if (at >= Drawing::AlphaType::ALPHATYPE_OPAQUE && at <= Drawing::AlphaType::ALPHATYPE_UNPREMUL) {
492 alphaType = static_cast<Drawing::AlphaType>(at);
493 }
494 auto colorSpace = std::make_shared<Drawing::ColorSpace>(Drawing::ColorSpace::ColorSpaceType::NO_TYPE);
495
496 if (!ReadColorSpaceFromParcel(parcel, colorSpace)) {
497 if (isMalloc) {
498 free(const_cast<void*>(addr));
499 addr = nullptr;
500 }
501 return false;
502 }
503
504 // if pixelmapSize >= MIN_DATA_SIZE(copyFromAshMem). record this memory size
505 // use this proc to follow release step
506 Drawing::ImageInfo imageInfo = Drawing::ImageInfo(width, height, colorType, alphaType, colorSpace);
507 auto skData = std::make_shared<Drawing::Data>();
508 if (pixmapSize < MIN_DATA_SIZE || (!g_useSharedMem && g_tid == std::this_thread::get_id())) {
509 skData->BuildWithCopy(addr, pixmapSize);
510 } else {
511 skData->BuildWithProc(addr, pixmapSize, SkFreeReleaseproc, nullptr);
512 }
513 val = Drawing::Image::MakeRasterData(imageInfo, skData, rb);
514 // add to MemoryTrack for memoryManager
515 if (isMalloc) {
516 MemoryInfo info = {pixmapSize, 0, 0, MEMORY_TYPE::MEM_SKIMAGE};
517 MemoryTrack::Instance().AddPictureRecord(addr, info);
518 imagepixelAddr = const_cast<void*>(addr);
519 }
520 return val != nullptr;
521 }
522
Unmarshalling(Parcel & parcel,std::shared_ptr<Drawing::Image> & val)523 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<Drawing::Image>& val)
524 {
525 void* addr = nullptr;
526 return Unmarshalling(parcel, val, addr);
527 }
528
Unmarshalling(Parcel & parcel,std::shared_ptr<Drawing::Image> & val,void * & imagepixelAddr)529 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<Drawing::Image>& val, void*& imagepixelAddr)
530 {
531 (void)imagepixelAddr;
532 int32_t type = parcel.ReadInt32();
533 if (type == -1) {
534 val = nullptr;
535 return true;
536 }
537 if (type == 1) {
538 std::shared_ptr<Drawing::Data> data;
539 if (!Unmarshalling(parcel, data) || !data) {
540 ROSEN_LOGE("failed RSMarshallingHelper::Unmarshalling Drawing::Image");
541 return false;
542 }
543 val = std::make_shared<Drawing::Image>();
544 if (!val->Deserialize(data)) {
545 ROSEN_LOGE("failed RSMarshallingHelper::Unmarshalling Drawing::Image deserialize");
546 val = nullptr;
547 return false;
548 }
549 return true;
550 }
551 return UnmarshallingNoLazyGeneratedImage(parcel, val, imagepixelAddr);
552 }
553
SkipImage(Parcel & parcel)554 bool RSMarshallingHelper::SkipImage(Parcel& parcel)
555 {
556 int32_t type = parcel.ReadInt32();
557 if (type == -1) {
558 return true;
559 }
560 std::shared_ptr<Drawing::Data> data;
561 if (type == 1) {
562 ROSEN_LOGD("RSMarshallingHelper::SkipImage lazy");
563 return SkipData(parcel);
564 } else {
565 size_t pixmapSize = parcel.ReadUint32();
566 if (!SkipFromParcel(parcel, pixmapSize)) {
567 ROSEN_LOGE("failed RSMarshallingHelper::SkipImage Data addr");
568 return false;
569 }
570
571 parcel.ReadUint32();
572 parcel.ReadInt32();
573 parcel.ReadInt32();
574 parcel.ReadUint32();
575 parcel.ReadUint32();
576 size_t size = parcel.ReadUint32();
577 return size == 0 ? true : SkipFromParcel(parcel, size);
578 }
579 }
580
581
582 // RSShader
Marshalling(Parcel & parcel,const std::shared_ptr<RSShader> & val)583 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSShader>& val)
584 {
585 if (!val || !val->GetDrawingShader()) {
586 ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling RSShader is nullptr");
587 return parcel.WriteInt32(-1);
588 }
589 auto& shaderEffect = val->GetDrawingShader();
590 int32_t type = static_cast<int32_t>(shaderEffect->GetType());
591 std::shared_ptr<Drawing::Data> data = shaderEffect->Serialize();
592 if (!data) {
593 ROSEN_LOGE("unirender: RSMarshallingHelper::Marshalling RSShader, data is nullptr");
594 return parcel.WriteInt32(-1);
595 }
596 return parcel.WriteInt32(type) && Marshalling(parcel, data);
597 }
598
Unmarshalling(Parcel & parcel,std::shared_ptr<RSShader> & val)599 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSShader>& val)
600 {
601 int32_t type = parcel.ReadInt32();
602 if (type == -1) {
603 val = nullptr;
604 return true;
605 }
606 std::shared_ptr<Drawing::Data> data;
607 if (!Unmarshalling(parcel, data) || !data) {
608 ROSEN_LOGE("unirender: RSMarshallingHelper::Unmarshalling RSShader, data is nullptr");
609 return false;
610 }
611 Drawing::ShaderEffect::ShaderEffectType shaderEffectType = Drawing::ShaderEffect::ShaderEffectType::NO_TYPE;
612 if (type >= static_cast<int32_t>(Drawing::ShaderEffect::ShaderEffectType::COLOR_SHADER) &&
613 type <= static_cast<int32_t>(Drawing::ShaderEffect::ShaderEffectType::EXTEND_SHADER)) {
614 shaderEffectType = static_cast<Drawing::ShaderEffect::ShaderEffectType>(type);
615 }
616 auto shaderEffect = std::make_shared<Drawing::ShaderEffect>(shaderEffectType);
617 if (!shaderEffect->Deserialize(data)) {
618 ROSEN_LOGE("unirender: RSMarshallingHelper::Unmarshalling RSShader, Deserialize failed");
619 return false;
620 }
621 val = RSShader::CreateRSShader(shaderEffect);
622 return val != nullptr;
623 }
624
625 // Drawing::Matrix
Marshalling(Parcel & parcel,const Drawing::Matrix & val)626 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const Drawing::Matrix& val)
627 {
628 Drawing::Matrix::Buffer buffer;
629 val.GetAll(buffer);
630 uint32_t size = buffer.size() * sizeof(Drawing::scalar);
631 bool ret = parcel.WriteUint32(size);
632 ret &= RSMarshallingHelper::WriteToParcel(parcel, buffer.data(), size);
633 if (!ret) {
634 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::Matrix");
635 }
636 return ret;
637 }
638
Unmarshalling(Parcel & parcel,Drawing::Matrix & val)639 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, Drawing::Matrix& val)
640 {
641 uint32_t size = parcel.ReadUint32();
642 if (size < sizeof(Drawing::scalar) * Drawing::Matrix::MATRIX_SIZE) {
643 ROSEN_LOGE("RSMarshallingHelper::Unmarshalling Drawing::Matrix failed size %{public}d", size);
644 return false;
645 }
646 bool isMalloc = false;
647 auto data = static_cast<const Drawing::scalar*>(RSMarshallingHelper::ReadFromParcel(parcel, size, isMalloc));
648 if (data == nullptr) {
649 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling Drawing::Matrix");
650 return false;
651 }
652
653 val.SetMatrix(data[Drawing::Matrix::SCALE_X], data[Drawing::Matrix::SKEW_X], data[Drawing::Matrix::TRANS_X],
654 data[Drawing::Matrix::SKEW_Y], data[Drawing::Matrix::SCALE_Y], data[Drawing::Matrix::TRANS_Y],
655 data[Drawing::Matrix::PERSP_0], data[Drawing::Matrix::PERSP_1], data[Drawing::Matrix::PERSP_2]);
656 if (isMalloc) {
657 free(static_cast<void*>(const_cast<Drawing::scalar*>(data)));
658 data = nullptr;
659 }
660 return true;
661 }
662
Marshalling(Parcel & parcel,const std::shared_ptr<RSLinearGradientBlurPara> & val)663 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSLinearGradientBlurPara>& val)
664 {
665 if (!val) {
666 ROSEN_LOGD("RSMarshallingHelper::Marshalling RSLinearGradientBlurPara is nullptr");
667 return parcel.WriteInt32(-1);
668 }
669 bool success = parcel.WriteInt32(1) && Marshalling(parcel, val->blurRadius_);
670 success = success && parcel.WriteUint32(static_cast<uint32_t>(val->fractionStops_.size()));
671 for (size_t i = 0; i < val->fractionStops_.size(); i++) {
672 success = success && Marshalling(parcel, val->fractionStops_[i].first);
673 success = success && Marshalling(parcel, val->fractionStops_[i].second);
674 }
675 success = success && Marshalling(parcel, val->direction_);
676 return success;
677 }
678
Unmarshalling(Parcel & parcel,std::shared_ptr<RSLinearGradientBlurPara> & val)679 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSLinearGradientBlurPara>& val)
680 {
681 if (parcel.ReadInt32() == -1) {
682 val = nullptr;
683 return true;
684 }
685 float blurRadius;
686 std::vector<std::pair<float, float>> fractionStops;
687 GradientDirection direction = GradientDirection::NONE;
688 bool success = Unmarshalling(parcel, blurRadius);
689 uint32_t fractionStopsSize = parcel.ReadUint32();
690 if (fractionStopsSize > SIZE_UPPER_LIMIT) {
691 return false;
692 }
693 for (size_t i = 0; i < fractionStopsSize; i++) {
694 std::pair<float, float> fractionStop;
695 float first = 0.0;
696 float second = 0.0;
697 success = success && Unmarshalling(parcel, first);
698 if (!success) {
699 return false;
700 }
701 fractionStop.first = first;
702 success = success && Unmarshalling(parcel, second);
703 if (!success) {
704 return false;
705 }
706 fractionStop.second = second;
707 fractionStops.push_back(fractionStop);
708 }
709 success = success && Unmarshalling(parcel, direction);
710 if (success) {
711 val = std::make_shared<RSLinearGradientBlurPara>(blurRadius, fractionStops, direction);
712 }
713 return success;
714 }
715
716 // MotionBlurPara
Marshalling(Parcel & parcel,const std::shared_ptr<MotionBlurParam> & val)717 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<MotionBlurParam>& val)
718 {
719 bool success = Marshalling(parcel, val->radius);
720 success = success && Marshalling(parcel, val->scaleAnchor[0]);
721 success = success && Marshalling(parcel, val->scaleAnchor[1]);
722 return success;
723 }
724
Unmarshalling(Parcel & parcel,std::shared_ptr<MotionBlurParam> & val)725 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<MotionBlurParam>& val)
726 {
727 float radius;
728 float anchorX = 0.f;
729 float anchorY = 0.f;
730
731 bool success = Unmarshalling(parcel, radius);
732 success = success && Unmarshalling(parcel, anchorX);
733 success = success && Unmarshalling(parcel, anchorY);
734 Vector2f anchor(anchorX, anchorY);
735
736 if (success) {
737 val = std::make_shared<MotionBlurParam>(radius, anchor);
738 }
739 return success;
740 }
741
742 // MagnifierPara
Marshalling(Parcel & parcel,const std::shared_ptr<RSMagnifierParams> & val)743 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSMagnifierParams>& val)
744 {
745 bool success = Marshalling(parcel, val->factor_);
746 success = success && Marshalling(parcel, val->width_);
747 success = success && Marshalling(parcel, val->height_);
748 success = success && Marshalling(parcel, val->cornerRadius_);
749 success = success && Marshalling(parcel, val->borderWidth_);
750 success = success && Marshalling(parcel, val->offsetX_);
751 success = success && Marshalling(parcel, val->offsetY_);
752
753 success = success && Marshalling(parcel, val->shadowOffsetX_);
754 success = success && Marshalling(parcel, val->shadowOffsetY_);
755 success = success && Marshalling(parcel, val->shadowSize_);
756 success = success && Marshalling(parcel, val->shadowStrength_);
757
758 success = success && Marshalling(parcel, val->gradientMaskColor1_);
759 success = success && Marshalling(parcel, val->gradientMaskColor2_);
760 success = success && Marshalling(parcel, val->outerContourColor1_);
761 success = success && Marshalling(parcel, val->outerContourColor2_);
762
763 return success;
764 }
765
Unmarshalling(Parcel & parcel,std::shared_ptr<RSMagnifierParams> & val)766 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSMagnifierParams>& val)
767 {
768 float factor = 0.f;
769 float width = 0.f;
770 float height = 0.f;
771 float cornerRadius = 0.f;
772 float borderWidth = 0.f;
773 float offsetX = 0.f;
774 float offsetY = 0.f;
775 float shadowOffsetX = 0.f;
776 float shadowOffsetY = 0.f;
777 float shadowSize = 0.f;
778 float shadowStrength = 0.f;
779 uint32_t gradientMaskColor1 = 0x00000000;
780 uint32_t gradientMaskColor2 = 0x00000000;
781 uint32_t outerContourColor1 = 0x00000000;
782 uint32_t outerContourColor2 = 0x00000000;
783
784 val = std::make_shared<RSMagnifierParams>();
785 if (val == nullptr) { return false; }
786
787 bool success = Unmarshalling(parcel, factor);
788 if (success) { val->factor_ = factor; }
789 success = success && Unmarshalling(parcel, width);
790 if (success) { val->width_ = width; }
791 success = success && Unmarshalling(parcel, height);
792 if (success) { val->height_ = height; }
793 success = success && Unmarshalling(parcel, cornerRadius);
794 if (success) { val->cornerRadius_ = cornerRadius; }
795 success = success && Unmarshalling(parcel, borderWidth);
796 if (success) { val->borderWidth_ = borderWidth; }
797 success = success && Unmarshalling(parcel, offsetX);
798 if (success) { val->offsetX_ = offsetX; }
799 success = success && Unmarshalling(parcel, offsetY);
800 if (success) { val->offsetY_ = offsetY; }
801
802 success = success && Unmarshalling(parcel, shadowOffsetX);
803 if (success) { val->shadowOffsetX_ = shadowOffsetX; }
804 success = success && Unmarshalling(parcel, shadowOffsetY);
805 if (success) { val->shadowOffsetY_ = shadowOffsetY; }
806 success = success && Unmarshalling(parcel, shadowSize);
807 if (success) { val->shadowSize_ = shadowSize; }
808 success = success && Unmarshalling(parcel, shadowStrength);
809 if (success) { val->shadowStrength_ = shadowStrength; }
810
811 success = success && Unmarshalling(parcel, gradientMaskColor1);
812 if (success) { val->gradientMaskColor1_ = gradientMaskColor1; }
813 success = success && Unmarshalling(parcel, gradientMaskColor2);
814 if (success) { val->gradientMaskColor2_ = gradientMaskColor2; }
815 success = success && Unmarshalling(parcel, outerContourColor1);
816 if (success) { val->outerContourColor1_ = outerContourColor1; }
817 success = success && Unmarshalling(parcel, outerContourColor2);
818 if (success) { val->outerContourColor2_ = outerContourColor2; }
819
820 return success;
821 }
822
823 // Particle
Marshalling(Parcel & parcel,const std::shared_ptr<EmitterUpdater> & val)824 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<EmitterUpdater>& val)
825 {
826 if (!val) {
827 ROSEN_LOGD("RSMarshallingHelper::Marshalling EmitterUpdater is nullptr");
828 return parcel.WriteInt32(-1);
829 }
830 bool success = parcel.WriteInt32(1) && Marshalling(parcel, val->emitterIndex_);
831 success = success && Marshalling(parcel, val->position_);
832 success = success && Marshalling(parcel, val->emitSize_) ;
833 success = success && Marshalling(parcel, val->emitRate_);
834 return success;
835 }
836
Unmarshalling(Parcel & parcel,std::shared_ptr<EmitterUpdater> & val)837 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<EmitterUpdater>& val)
838 {
839 if (parcel.ReadInt32() == -1) {
840 val = nullptr;
841 return true;
842 }
843 int emitterIndex = 0;
844 std::optional<Vector2f> position = std::nullopt;
845 std::optional<Vector2f> emitSize = std::nullopt;
846 std::optional<int> emitRate = std::nullopt;
847
848 bool success = Unmarshalling(parcel, emitterIndex);
849 success = success && Unmarshalling(parcel, position);
850 success = success && Unmarshalling(parcel, emitSize);
851 success = success && Unmarshalling(parcel, emitRate);
852 if (success) {
853 val = std::make_shared<EmitterUpdater>(emitterIndex, position, emitSize, emitRate);
854 }
855 return success;
856 }
857
Marshalling(Parcel & parcel,const std::vector<std::shared_ptr<EmitterUpdater>> & val)858 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::vector<std::shared_ptr<EmitterUpdater>>& val)
859 {
860 bool success = parcel.WriteUint32(static_cast<uint32_t>(val.size()));
861 for (size_t i = 0; i < val.size(); i++) {
862 success = success && Marshalling(parcel, val[i]);
863 }
864 return success;
865 }
866
Unmarshalling(Parcel & parcel,std::vector<std::shared_ptr<EmitterUpdater>> & val)867 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::vector<std::shared_ptr<EmitterUpdater>>& val)
868 {
869 uint32_t size = parcel.ReadUint32();
870 bool success = true;
871 std::vector<std::shared_ptr<EmitterUpdater>> emitterUpdaters;
872 if (size > PARTICLE_EMMITER_UPPER_LIMIT) {
873 return false;
874 }
875 for (size_t i = 0; i < size; i++) {
876 std::shared_ptr<EmitterUpdater> emitterUpdater;
877 success = success && Unmarshalling(parcel, emitterUpdater);
878 emitterUpdaters.push_back(emitterUpdater);
879 }
880 if (success) {
881 val = emitterUpdaters;
882 }
883 return success;
884 }
885
Marshalling(Parcel & parcel,const std::shared_ptr<ParticleNoiseField> & val)886 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<ParticleNoiseField>& val)
887 {
888 if (!val) {
889 ROSEN_LOGD("RSMarshallingHelper::Marshalling ParticleNoiseField is nullptr");
890 return parcel.WriteInt32(-1);
891 }
892 bool success = parcel.WriteInt32(1) && Marshalling(parcel, val->fieldStrength_);
893 success = success && Marshalling(parcel, val->fieldShape_);
894 success = success && Marshalling(parcel, val->fieldSize_.x_) && Marshalling(parcel, val->fieldSize_.y_);
895 success = success && Marshalling(parcel, val->fieldCenter_.x_) && Marshalling(parcel, val->fieldCenter_.y_);
896 success = success && Marshalling(parcel, val->fieldFeather_) && Marshalling(parcel, val->noiseScale_);
897 success = success && Marshalling(parcel, val->noiseFrequency_) && Marshalling(parcel, val->noiseAmplitude_);
898 return success;
899 }
900
Unmarshalling(Parcel & parcel,std::shared_ptr<ParticleNoiseField> & val)901 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<ParticleNoiseField>& val)
902 {
903 if (parcel.ReadInt32() == -1) {
904 val = nullptr;
905 return true;
906 }
907 int fieldStrength = 0;
908 ShapeType fieldShape = ShapeType::RECT;
909 float fieldSizeX = 0.f;
910 float fieldSizeY = 0.f;
911 float fieldCenterX = 0.f;
912 float fieldCenterY = 0.f;
913 uint16_t fieldFeather = 0;
914 float noiseScale = 0.f;
915 float noiseFrequency = 0.f;
916 float noiseAmplitude = 0.f;
917
918 bool success = Unmarshalling(parcel, fieldStrength);
919 success = success && Unmarshalling(parcel, fieldShape);
920 success = success && Unmarshalling(parcel, fieldSizeX) && Unmarshalling(parcel, fieldSizeY);
921 Vector2f fieldSize(fieldSizeX, fieldSizeY);
922 success = success && Unmarshalling(parcel, fieldCenterX) && Unmarshalling(parcel, fieldCenterY);
923 Vector2f fieldCenter(fieldCenterX, fieldCenterY);
924 success = success && Unmarshalling(parcel, fieldFeather);
925 success = success && Unmarshalling(parcel, noiseScale);
926 success = success && Unmarshalling(parcel, noiseFrequency);
927 success = success && Unmarshalling(parcel, noiseAmplitude);
928 if (success) {
929 val = std::make_shared<ParticleNoiseField>(fieldStrength, fieldShape, fieldSize, fieldCenter, fieldFeather,
930 noiseScale, noiseFrequency, noiseAmplitude);
931 }
932 return success;
933 }
934
Marshalling(Parcel & parcel,const std::shared_ptr<ParticleNoiseFields> & val)935 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<ParticleNoiseFields>& val)
936 {
937 if (!val) {
938 ROSEN_LOGD("RSMarshallingHelper::Marshalling ParticleNoiseFields is nullptr");
939 return parcel.WriteInt32(-1);
940 }
941 bool success = parcel.WriteInt32(1) && parcel.WriteUint32(static_cast<uint32_t>(val->fields_.size()));
942 for (size_t i = 0; i < val->fields_.size(); i++) {
943 success = success && Marshalling(parcel, val->fields_[i]);
944 }
945 return success;
946 }
947
Unmarshalling(Parcel & parcel,std::shared_ptr<ParticleNoiseFields> & val)948 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<ParticleNoiseFields>& val)
949 {
950 if (parcel.ReadInt32() == -1) {
951 val = nullptr;
952 return true;
953 }
954 uint32_t size = parcel.ReadUint32();
955 bool success = true;
956 if (size > PARTICLE_EMMITER_UPPER_LIMIT) {
957 return false;
958 }
959 std::shared_ptr<ParticleNoiseFields> noiseFields = std::make_shared<ParticleNoiseFields>();
960 for (size_t i = 0; i < size; i++) {
961 std::shared_ptr<ParticleNoiseField> ParticleNoiseField;
962 success = success && Unmarshalling(parcel, ParticleNoiseField);
963 noiseFields->AddField(ParticleNoiseField);
964 }
965 if (success) {
966 val = noiseFields;
967 }
968 return success;
969 }
970
Marshalling(Parcel & parcel,const EmitterConfig & val)971 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const EmitterConfig& val)
972 {
973 bool success = Marshalling(parcel, val.emitRate_);
974 success = success && Marshalling(parcel, val.emitShape_);
975 success = success && Marshalling(parcel, val.position_.x_);
976 success = success && Marshalling(parcel, val.position_.y_);
977 success = success && Marshalling(parcel, val.emitSize_.x_);
978 success = success && Marshalling(parcel, val.emitSize_.y_);
979 success = success && Marshalling(parcel, val.particleCount_);
980 success = success && Marshalling(parcel, val.lifeTime_.start_);
981 success = success && Marshalling(parcel, val.lifeTime_.end_);
982 success = success && Marshalling(parcel, val.type_);
983 success = success && Marshalling(parcel, val.radius_);
984 success = success && Marshalling(parcel, val.image_);
985 success = success && Marshalling(parcel, val.imageSize_.x_);
986 success = success && Marshalling(parcel, val.imageSize_.y_);
987
988 return success;
989 }
990
Unmarshalling(Parcel & parcel,EmitterConfig & val)991 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, EmitterConfig& val)
992 {
993 int emitRate = 0;
994 ShapeType emitShape = ShapeType::RECT;
995 float positionX = 0.f;
996 float positionY = 0.f;
997 float emitSizeWidth = 0.f;
998 float emitSizeHeight = 0.f;
999 int particleCount = 0;
1000 int64_t lifeTimeStart = 0;
1001 int64_t lifeTimeEnd = 0;
1002 ParticleType particleType = ParticleType::POINTS;
1003 float radius = 0.f;
1004 std::shared_ptr<RSImage> image = nullptr;
1005 float imageWidth = 0.f;
1006 float imageHeight = 0.f;
1007
1008 bool success = Unmarshalling(parcel, emitRate);
1009 success = success && Unmarshalling(parcel, emitShape);
1010 success = success && Unmarshalling(parcel, positionX);
1011 success = success && Unmarshalling(parcel, positionY);
1012 Vector2f position(positionX, positionY);
1013 success = success && Unmarshalling(parcel, emitSizeWidth);
1014 success = success && Unmarshalling(parcel, emitSizeHeight);
1015 Vector2f emitSize(emitSizeWidth, emitSizeHeight);
1016 success = success && Unmarshalling(parcel, particleCount);
1017 success = success && Unmarshalling(parcel, lifeTimeStart);
1018 success = success && Unmarshalling(parcel, lifeTimeEnd);
1019 success = success && Unmarshalling(parcel, particleType);
1020 success = success && Unmarshalling(parcel, radius);
1021 Unmarshalling(parcel, image);
1022 success = success && Unmarshalling(parcel, imageWidth);
1023 success = success && Unmarshalling(parcel, imageHeight);
1024 Vector2f imageSize(imageWidth, imageHeight);
1025 if (success) {
1026 Range<int64_t> lifeTime(lifeTimeStart, lifeTimeEnd);
1027 val = EmitterConfig(
1028 emitRate, emitShape, position, emitSize, particleCount, lifeTime, particleType, radius, image, imageSize);
1029 }
1030 return success;
1031 }
1032
Marshalling(Parcel & parcel,const ParticleVelocity & val)1033 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const ParticleVelocity& val)
1034 {
1035 return Marshalling(parcel, val.velocityValue_.start_) && Marshalling(parcel, val.velocityValue_.end_) &&
1036 Marshalling(parcel, val.velocityAngle_.start_) && Marshalling(parcel, val.velocityAngle_.end_);
1037 }
1038
Unmarshalling(Parcel & parcel,ParticleVelocity & val)1039 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, ParticleVelocity& val)
1040 {
1041 float valueStart = 0.f;
1042 float valueEnd = 0.f;
1043 float angleStart = 0.f;
1044 float angleEnd = 0.f;
1045 bool success = Unmarshalling(parcel, valueStart) && Unmarshalling(parcel, valueEnd) &&
1046 Unmarshalling(parcel, angleStart) && Unmarshalling(parcel, angleEnd);
1047 if (success) {
1048 Range<float> velocityValue(valueStart, valueEnd);
1049 Range<float> velocityAngle(angleStart, angleEnd);
1050 val = ParticleVelocity(velocityValue, velocityAngle);
1051 }
1052 return success;
1053 }
1054
Marshalling(Parcel & parcel,const RenderParticleParaType<float> & val)1055 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const RenderParticleParaType<float>& val)
1056 {
1057 bool success = Marshalling(parcel, val.val_.start_) && Marshalling(parcel, val.val_.end_) &&
1058 Marshalling(parcel, val.updator_);
1059 if (val.updator_ == ParticleUpdator::RANDOM) {
1060 success = success && Marshalling(parcel, val.random_.start_) && Marshalling(parcel, val.random_.end_);
1061 } else if (val.updator_ == ParticleUpdator::CURVE) {
1062 success = success && parcel.WriteUint32(static_cast<uint32_t>(val.valChangeOverLife_.size()));
1063 for (size_t i = 0; i < val.valChangeOverLife_.size(); i++) {
1064 if (val.valChangeOverLife_[i] == nullptr || val.valChangeOverLife_[i]->interpolator_ == nullptr) {
1065 ROSEN_LOGE("RSMarshallingHelper::Unmarshalling RenderParticleParaType fail cause nullptr");
1066 return false;
1067 }
1068 success = success && Marshalling(parcel, val.valChangeOverLife_[i]->fromValue_);
1069 success = success && Marshalling(parcel, val.valChangeOverLife_[i]->toValue_);
1070 success = success && Marshalling(parcel, val.valChangeOverLife_[i]->startMillis_);
1071 success = success && Marshalling(parcel, val.valChangeOverLife_[i]->endMillis_);
1072 success = success && val.valChangeOverLife_[i]->interpolator_->Marshalling(parcel);
1073 }
1074 }
1075 return success;
1076 }
1077
Unmarshalling(Parcel & parcel,RenderParticleParaType<float> & val)1078 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, RenderParticleParaType<float>& val)
1079 {
1080 float valueStart = 0.f;
1081 float valueEnd = 0.f;
1082 ParticleUpdator updator = ParticleUpdator::NONE;
1083 float randomStart = 0.f;
1084 float randomEnd = 0.f;
1085 std::vector<std::shared_ptr<ChangeInOverLife<float>>> valChangeOverLife;
1086 bool success =
1087 Unmarshalling(parcel, valueStart) && Unmarshalling(parcel, valueEnd) && Unmarshalling(parcel, updator);
1088 if (updator == ParticleUpdator::RANDOM) {
1089 success = success && Unmarshalling(parcel, randomStart) && Unmarshalling(parcel, randomEnd);
1090 } else if (updator == ParticleUpdator::CURVE) {
1091 uint32_t valChangeOverLifeSize = parcel.ReadUint32();
1092 if (valChangeOverLifeSize > SIZE_UPPER_LIMIT) {
1093 return false;
1094 }
1095 for (size_t i = 0; i < valChangeOverLifeSize; i++) {
1096 float fromValue = 0.f;
1097 float toValue = 0.f;
1098 int startMillis = 0;
1099 int endMillis = 0;
1100 success = success && Unmarshalling(parcel, fromValue);
1101 success = success && Unmarshalling(parcel, toValue);
1102 success = success && Unmarshalling(parcel, startMillis);
1103 success = success && Unmarshalling(parcel, endMillis);
1104 std::shared_ptr<RSInterpolator> interpolator(RSInterpolator::Unmarshalling(parcel));
1105 auto change = std::make_shared<ChangeInOverLife<float>>(
1106 fromValue, toValue, startMillis, endMillis, interpolator);
1107 valChangeOverLife.push_back(change);
1108 }
1109 }
1110 if (success) {
1111 Range<float> value(valueStart, valueEnd);
1112 Range<float> random(randomStart, randomEnd);
1113 val = RenderParticleParaType<float>(value, updator, random, std::move(valChangeOverLife));
1114 }
1115 return success;
1116 }
1117
Marshalling(Parcel & parcel,const RenderParticleColorParaType & val)1118 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const RenderParticleColorParaType& val)
1119 {
1120 bool success = Marshalling(parcel, val.colorVal_.start_) && Marshalling(parcel, val.colorVal_.end_) &&
1121 Marshalling(parcel, val.distribution_) && Marshalling(parcel, val.updator_);
1122 if (val.updator_ == ParticleUpdator::RANDOM) {
1123 success = success && Marshalling(parcel, val.redRandom_.start_) && Marshalling(parcel, val.redRandom_.end_);
1124 success =
1125 success && Marshalling(parcel, val.greenRandom_.start_) && Marshalling(parcel, val.greenRandom_.end_);
1126 success =
1127 success && Marshalling(parcel, val.blueRandom_.start_) && Marshalling(parcel, val.blueRandom_.end_);
1128 success =
1129 success && Marshalling(parcel, val.alphaRandom_.start_) && Marshalling(parcel, val.alphaRandom_.end_);
1130 } else if (val.updator_ == ParticleUpdator::CURVE) {
1131 success = success && parcel.WriteUint32(static_cast<uint32_t>(val.valChangeOverLife_.size()));
1132 for (size_t i = 0; i < val.valChangeOverLife_.size(); i++) {
1133 if (val.valChangeOverLife_[i] == nullptr || val.valChangeOverLife_[i]->interpolator_ == nullptr) {
1134 ROSEN_LOGE("RSMarshallingHelper::Unmarshalling RenderParticleColorParaType fail cause nullptr");
1135 return false;
1136 }
1137 success = success && Marshalling(parcel, val.valChangeOverLife_[i]->fromValue_);
1138 success = success && Marshalling(parcel, val.valChangeOverLife_[i]->toValue_);
1139 success = success && Marshalling(parcel, val.valChangeOverLife_[i]->startMillis_);
1140 success = success && Marshalling(parcel, val.valChangeOverLife_[i]->endMillis_);
1141 success = success && val.valChangeOverLife_[i]->interpolator_->Marshalling(parcel);
1142 }
1143 }
1144 return success;
1145 }
1146
Unmarshalling(Parcel & parcel,RenderParticleColorParaType & val)1147 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, RenderParticleColorParaType& val)
1148 {
1149 Color colorValStart = RSColor(0, 0, 0);
1150 Color colorValEnd = RSColor(0, 0, 0);
1151 DistributionType distribution = DistributionType::UNIFORM;
1152 ParticleUpdator updator = ParticleUpdator::NONE;
1153 float redRandomStart = 0.f;
1154 float redRandomEnd = 0.f;
1155 float greenRandomStart = 0.f;
1156 float greenRandomEnd = 0.f;
1157 float blueRandomStart = 0.f;
1158 float blueRandomEnd = 0.f;
1159 float alphaRandomStart = 0.f;
1160 float alphaRandomEnd = 0.f;
1161 std::vector<std::shared_ptr<ChangeInOverLife<Color>>> valChangeOverLife;
1162 bool success = Unmarshalling(parcel, colorValStart) && Unmarshalling(parcel, colorValEnd) &&
1163 Unmarshalling(parcel, distribution) && Unmarshalling(parcel, updator);
1164 if (updator == ParticleUpdator::RANDOM) {
1165 success = success && Unmarshalling(parcel, redRandomStart) && Unmarshalling(parcel, redRandomEnd) &&
1166 Unmarshalling(parcel, greenRandomStart) && Unmarshalling(parcel, greenRandomEnd) &&
1167 Unmarshalling(parcel, blueRandomStart) && Unmarshalling(parcel, blueRandomEnd) &&
1168 Unmarshalling(parcel, alphaRandomStart) && Unmarshalling(parcel, alphaRandomEnd);
1169 } else if (updator == ParticleUpdator::CURVE) {
1170 uint32_t valChangeOverLifeSize = parcel.ReadUint32();
1171 if (valChangeOverLifeSize > SIZE_UPPER_LIMIT) {
1172 return false;
1173 }
1174 for (size_t i = 0; i < valChangeOverLifeSize; i++) {
1175 Color fromValue = RSColor(0, 0, 0);
1176 Color toValue = RSColor(0, 0, 0);
1177 int startMillis = 0;
1178 int endMillis = 0;
1179 success = success && Unmarshalling(parcel, fromValue) && Unmarshalling(parcel, toValue) &&
1180 Unmarshalling(parcel, startMillis) && Unmarshalling(parcel, endMillis);
1181 std::shared_ptr<RSInterpolator> interpolator(RSInterpolator::Unmarshalling(parcel));
1182 auto change = std::make_shared<ChangeInOverLife<Color>>(
1183 fromValue, toValue, startMillis, endMillis, std::move(interpolator));
1184 valChangeOverLife.push_back(change);
1185 }
1186 }
1187 if (success) {
1188 Range<Color> colorVal(colorValStart, colorValEnd);
1189 Range<float> redRandom(redRandomStart, redRandomEnd);
1190 Range<float> greenRandom(greenRandomStart, greenRandomEnd);
1191 Range<float> blueRandom(blueRandomStart, blueRandomEnd);
1192 Range<float> alphaRandom(alphaRandomStart, alphaRandomEnd);
1193 val = RenderParticleColorParaType(colorVal, distribution, updator, redRandom, greenRandom, blueRandom,
1194 alphaRandom, std::move(valChangeOverLife));
1195 }
1196 return success;
1197 }
1198
Marshalling(Parcel & parcel,const std::shared_ptr<ParticleRenderParams> & val)1199 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<ParticleRenderParams>& val)
1200 {
1201 if (!val) {
1202 ROSEN_LOGD("RSMarshallingHelper::Marshalling ParticleRenderParams is nullptr");
1203 return parcel.WriteInt32(-1);
1204 }
1205 bool success = parcel.WriteInt32(1) && Marshalling(parcel, val->emitterConfig_);
1206 success = success && Marshalling(parcel, val->velocity_);
1207 success = success && Marshalling(parcel, val->acceleration_.accelerationValue_);
1208 success = success && Marshalling(parcel, val->acceleration_.accelerationAngle_);
1209 success = success && Marshalling(parcel, val->color_);
1210 success = success && Marshalling(parcel, val->opacity_);
1211 success = success && Marshalling(parcel, val->scale_);
1212 success = success && Marshalling(parcel, val->spin_);
1213 return success;
1214 }
1215
Unmarshalling(Parcel & parcel,std::shared_ptr<ParticleRenderParams> & val)1216 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<ParticleRenderParams>& val)
1217 {
1218 if (parcel.ReadInt32() == -1) {
1219 val = nullptr;
1220 return true;
1221 }
1222 EmitterConfig emitterConfig;
1223 ParticleVelocity velocity;
1224 RenderParticleParaType<float> accelerationValue;
1225 RenderParticleParaType<float> accelerationAngle;
1226 RenderParticleColorParaType color;
1227 RenderParticleParaType<float> opacity;
1228 RenderParticleParaType<float> scale;
1229 RenderParticleParaType<float> spin;
1230 bool success = Unmarshalling(parcel, emitterConfig);
1231 success = success && Unmarshalling(parcel, velocity);
1232 success = success && Unmarshalling(parcel, accelerationValue);
1233 success = success && Unmarshalling(parcel, accelerationAngle);
1234 RenderParticleAcceleration acceleration = RenderParticleAcceleration(accelerationValue, accelerationAngle);
1235 success = success && Unmarshalling(parcel, color);
1236 success = success && Unmarshalling(parcel, opacity);
1237 success = success && Unmarshalling(parcel, scale);
1238 success = success && Unmarshalling(parcel, spin);
1239 if (success) {
1240 val =
1241 std::make_shared<ParticleRenderParams>(emitterConfig, velocity, acceleration, color, opacity, scale, spin);
1242 }
1243 return success;
1244 }
1245
Marshalling(Parcel & parcel,const std::vector<std::shared_ptr<ParticleRenderParams>> & val)1246 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::vector<std::shared_ptr<ParticleRenderParams>>& val)
1247 {
1248 bool success = parcel.WriteUint32(static_cast<uint32_t>(val.size()));
1249 for (size_t i = 0; i < val.size(); i++) {
1250 success = success && Marshalling(parcel, val[i]);
1251 }
1252 return success;
1253 }
1254
Unmarshalling(Parcel & parcel,std::vector<std::shared_ptr<ParticleRenderParams>> & val)1255 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::vector<std::shared_ptr<ParticleRenderParams>>& val)
1256 {
1257 uint32_t size = parcel.ReadUint32();
1258 bool success = true;
1259 std::vector<std::shared_ptr<ParticleRenderParams>> particlesRenderParams;
1260 if (size > PARTICLE_EMMITER_UPPER_LIMIT) {
1261 return false;
1262 }
1263 for (size_t i = 0; i < size; i++) {
1264 std::shared_ptr<ParticleRenderParams> particleRenderParams;
1265 success = success && Unmarshalling(parcel, particleRenderParams);
1266 particlesRenderParams.push_back(particleRenderParams);
1267 }
1268 if (success) {
1269 val = particlesRenderParams;
1270 }
1271 return success;
1272 }
1273
1274 // RSPath
Marshalling(Parcel & parcel,const std::shared_ptr<RSPath> & val)1275 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSPath>& val)
1276 {
1277 if (!val) {
1278 ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling RSPath is nullptr");
1279 return parcel.WriteInt32(-1);
1280 }
1281
1282 std::shared_ptr<Drawing::Data> data = val->GetDrawingPath().Serialize();
1283 if (!data) {
1284 ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling Path is nullptr");
1285 return false;
1286 }
1287
1288 return parcel.WriteInt32(1) && Marshalling(parcel, data);
1289 }
1290
Unmarshalling(Parcel & parcel,std::shared_ptr<RSPath> & val)1291 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSPath>& val)
1292 {
1293 int32_t size = parcel.ReadInt32();
1294 if (size == -1) {
1295 val = nullptr;
1296 return true;
1297 }
1298
1299 std::shared_ptr<Drawing::Data> data;
1300 Drawing::Path path;
1301 if (!Unmarshalling(parcel, data) || !data) {
1302 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling Path");
1303 return false;
1304 }
1305
1306 if (!path.Deserialize(data)) {
1307 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling RSPath");
1308 return false;
1309 }
1310
1311 val = RSPath::CreateRSPath(path);
1312 return val != nullptr;
1313 }
1314
1315 // RSMask
Marshalling(Parcel & parcel,const std::shared_ptr<RSMask> & val)1316 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSMask>& val)
1317 {
1318 if (!val) {
1319 ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling RSMask is nullptr");
1320 return parcel.WriteInt32(-1);
1321 }
1322 return parcel.WriteInt32(1) && val->Marshalling(parcel);
1323 }
Unmarshalling(Parcel & parcel,std::shared_ptr<RSMask> & val)1324 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSMask>& val)
1325 {
1326 if (parcel.ReadInt32() == -1) {
1327 val = nullptr;
1328 return true;
1329 }
1330 val.reset(RSMask::Unmarshalling(parcel));
1331 return val != nullptr;
1332 }
1333
1334 // RSFilter
Marshalling(Parcel & parcel,const std::shared_ptr<RSFilter> & val)1335 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSFilter>& val)
1336 {
1337 if (!val) {
1338 return parcel.WriteInt32(RSFilter::NONE);
1339 }
1340 bool success = parcel.WriteInt32(static_cast<int>(val->GetFilterType()));
1341 switch (val->GetFilterType()) {
1342 case RSFilter::BLUR: {
1343 auto blur = std::static_pointer_cast<RSBlurFilter>(val);
1344 success = success && parcel.WriteFloat(blur->GetBlurRadiusX()) && parcel.WriteFloat(blur->GetBlurRadiusY());
1345 break;
1346 }
1347 case RSFilter::MATERIAL: {
1348 auto material = std::static_pointer_cast<RSMaterialFilter>(val);
1349 success = success && parcel.WriteFloat(material->radius_) && parcel.WriteFloat(material->saturation_) &&
1350 parcel.WriteFloat(material->brightness_) &&
1351 RSMarshallingHelper::Marshalling(parcel, material->maskColor_) &&
1352 parcel.WriteInt32(material->colorMode_);
1353 break;
1354 }
1355 case RSFilter::LIGHT_UP_EFFECT: {
1356 auto lightUp = std::static_pointer_cast<RSLightUpEffectFilter>(val);
1357 success = success && parcel.WriteFloat(lightUp->lightUpDegree_);
1358 break;
1359 }
1360 default:
1361 break;
1362 }
1363 return success;
1364 }
Unmarshalling(Parcel & parcel,std::shared_ptr<RSFilter> & val)1365 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSFilter>& val)
1366 {
1367 int type = 0;
1368 bool success = parcel.ReadInt32(type);
1369 switch (static_cast<RSFilter::FilterType>(type)) {
1370 case RSFilter::BLUR: {
1371 float blurRadiusX;
1372 float blurRadiusY;
1373 success = success && parcel.ReadFloat(blurRadiusX) && parcel.ReadFloat(blurRadiusY);
1374 if (success) {
1375 val = RSFilter::CreateBlurFilter(blurRadiusX, blurRadiusY);
1376 }
1377 break;
1378 }
1379 case RSFilter::MATERIAL: {
1380 MaterialParam materialParam;
1381 int colorMode;
1382 success = success && parcel.ReadFloat(materialParam.radius) && parcel.ReadFloat(materialParam.saturation) &&
1383 parcel.ReadFloat(materialParam.brightness) &&
1384 RSMarshallingHelper::Unmarshalling(parcel, materialParam.maskColor) &&
1385 parcel.ReadInt32(colorMode);
1386 if (success) {
1387 val = std::make_shared<RSMaterialFilter>(materialParam, static_cast<BLUR_COLOR_MODE>(colorMode));
1388 }
1389 break;
1390 }
1391 case RSFilter::LIGHT_UP_EFFECT: {
1392 float lightUpDegree;
1393 success = success && parcel.ReadFloat(lightUpDegree);
1394 if (success) {
1395 val = RSFilter::CreateLightUpEffectFilter(lightUpDegree);
1396 }
1397 break;
1398 }
1399 default: {
1400 val = nullptr;
1401 break;
1402 }
1403 }
1404 return success;
1405 }
1406
1407 // RSImageBase
Marshalling(Parcel & parcel,const std::shared_ptr<RSImageBase> & val)1408 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSImageBase>& val)
1409 {
1410 if (!val) {
1411 ROSEN_LOGD("RSMarshallingHelper::Marshalling RSImageBase is nullptr");
1412 return parcel.WriteInt32(-1);
1413 }
1414 return parcel.WriteInt32(1) && val->Marshalling(parcel);
1415 }
Unmarshalling(Parcel & parcel,std::shared_ptr<RSImageBase> & val)1416 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSImageBase>& val)
1417 {
1418 if (parcel.ReadInt32() == -1) {
1419 val = nullptr;
1420 return true;
1421 }
1422 val.reset(RSImageBase::Unmarshalling(parcel));
1423 return val != nullptr;
1424 }
1425
1426 // RSImage
Marshalling(Parcel & parcel,const std::shared_ptr<RSImage> & val)1427 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSImage>& val)
1428 {
1429 if (!val) {
1430 ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling RSImage is nullptr");
1431 return parcel.WriteInt32(-1);
1432 }
1433 return parcel.WriteInt32(1) && val->Marshalling(parcel);
1434 }
Unmarshalling(Parcel & parcel,std::shared_ptr<RSImage> & val)1435 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSImage>& val)
1436 {
1437 if (parcel.ReadInt32() == -1) {
1438 val = nullptr;
1439 return true;
1440 }
1441 val.reset(RSImage::Unmarshalling(parcel));
1442 return val != nullptr;
1443 }
1444
1445 // Media::PixelMap
Marshalling(Parcel & parcel,const std::shared_ptr<Media::PixelMap> & val)1446 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<Media::PixelMap>& val)
1447 {
1448 if (!val) {
1449 return parcel.WriteInt32(-1);
1450 }
1451 auto position = parcel.GetWritePosition();
1452 if (!(parcel.WriteInt32(1) && RS_PROFILER_MARSHAL_PIXELMAP(parcel, val))) {
1453 ROSEN_LOGE("failed RSMarshallingHelper::Marshalling Media::PixelMap");
1454 return false;
1455 }
1456 // correct pixelmap size recorded in Parcel
1457 *reinterpret_cast<int32_t*>(parcel.GetData() + position) =
1458 static_cast<int32_t>(parcel.GetWritePosition() - position - sizeof(int32_t));
1459 return true;
1460 }
1461
CustomFreePixelMap(void * addr,void * context,uint32_t size)1462 static void CustomFreePixelMap(void* addr, void* context, uint32_t size)
1463 {
1464 #ifdef ROSEN_OHOS
1465 if (RSSystemProperties::GetClosePixelMapFdEnabled()) {
1466 MemoryTrack::Instance().RemovePictureRecord(addr);
1467 } else {
1468 MemoryTrack::Instance().RemovePictureRecord(context);
1469 }
1470 #else
1471 MemoryTrack::Instance().RemovePictureRecord(addr);
1472 #endif
1473 }
1474
Unmarshalling(Parcel & parcel,std::shared_ptr<Media::PixelMap> & val)1475 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<Media::PixelMap>& val)
1476 {
1477 if (parcel.ReadInt32() == -1) {
1478 val = nullptr;
1479 return true;
1480 }
1481 auto readPosition = parcel.GetReadPosition();
1482 val.reset(RS_PROFILER_UNMARSHAL_PIXELMAP(parcel));
1483 if (val == nullptr) {
1484 ROSEN_LOGE("failed RSMarshallingHelper::Unmarshalling Media::PixelMap");
1485 if (readPosition > PIXELMAP_UNMARSHALLING_DEBUG_OFFSET &&
1486 parcel.RewindRead(readPosition - PIXELMAP_UNMARSHALLING_DEBUG_OFFSET)) {
1487 ROSEN_LOGE("RSMarshallingHelper::Unmarshalling PixelMap before"
1488 " [%{public}d, %{public}d, %{public}d] [w, h, format]:[%{public}d, %{public}d, %{public}d]",
1489 parcel.ReadInt32(), parcel.ReadInt32(), parcel.ReadInt32(), parcel.ReadInt32(), parcel.ReadInt32(),
1490 parcel.ReadInt32());
1491 } else {
1492 ROSEN_LOGE("RSMarshallingHelper::Unmarshalling RewindRead failed");
1493 }
1494 return false;
1495 }
1496 if (RSSystemProperties::GetClosePixelMapFdEnabled()) {
1497 val->CloseFd();
1498 }
1499 MemoryInfo info = {
1500 val->GetByteCount(), 0, 0, val->GetUniqueId(), MEMORY_TYPE::MEM_PIXELMAP, val->GetAllocatorType(), val
1501 };
1502
1503 #ifdef ROSEN_OHOS
1504 if (RSSystemProperties::GetClosePixelMapFdEnabled()) {
1505 MemoryTrack::Instance().AddPictureRecord(val->GetPixels(), info);
1506 } else {
1507 MemoryTrack::Instance().AddPictureRecord(val->GetFd(), info);
1508 }
1509 #else
1510 MemoryTrack::Instance().AddPictureRecord(val->GetPixels(), info);
1511 #endif
1512 val->SetFreePixelMapProc(CustomFreePixelMap);
1513 return true;
1514 }
1515
SkipPixelMap(Parcel & parcel)1516 bool RSMarshallingHelper::SkipPixelMap(Parcel& parcel)
1517 {
1518 auto size = parcel.ReadInt32();
1519 if (size != -1) {
1520 parcel.SkipBytes(size);
1521 }
1522 return true;
1523 }
1524
1525 // RectF
Marshalling(Parcel & parcel,const std::shared_ptr<RectT<float>> & val)1526 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RectT<float>>& val)
1527 {
1528 if (!val) {
1529 ROSEN_LOGD("unirender: RSMarshallingHelper::Marshalling RectF is nullptr");
1530 return parcel.WriteInt32(-1);
1531 }
1532 return parcel.WriteInt32(1) && val->Marshalling(parcel);
1533 }
1534
Unmarshalling(Parcel & parcel,std::shared_ptr<RectT<float>> & val)1535 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RectT<float>>& val)
1536 {
1537 if (parcel.ReadInt32() == -1) {
1538 val = nullptr;
1539 return true;
1540 }
1541 val.reset(RectT<float>::Unmarshalling(parcel));
1542 return val != nullptr;
1543 }
1544
1545 // RRect
Marshalling(Parcel & parcel,const RRectT<float> & val)1546 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const RRectT<float>& val)
1547 {
1548 return Marshalling(parcel, val.rect_) && Marshalling(parcel, val.radius_[0]) &&
1549 Marshalling(parcel, val.radius_[1]) && Marshalling(parcel, val.radius_[2]) &&
1550 Marshalling(parcel, val.radius_[3]);
1551 }
Unmarshalling(Parcel & parcel,RRectT<float> & val)1552 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, RRectT<float>& val)
1553 {
1554 return Unmarshalling(parcel, val.rect_) && Unmarshalling(parcel, val.radius_[0]) &&
1555 Unmarshalling(parcel, val.radius_[1]) && Unmarshalling(parcel, val.radius_[2]) &&
1556 Unmarshalling(parcel, val.radius_[3]);
1557 }
1558
1559
1560 // Drawing::DrawCmdList
Marshalling(Parcel & parcel,const std::shared_ptr<Drawing::DrawCmdList> & val,bool isRecordCmd)1561 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<Drawing::DrawCmdList>& val,
1562 bool isRecordCmd)
1563 {
1564 if (!val) {
1565 return parcel.WriteInt32(-1);
1566 }
1567 auto opItemSize = val->GetOpItemSize();
1568 if (opItemSize > Drawing::MAX_OPITEMSIZE) {
1569 ROSEN_LOGE("OpItemSize is too large, OpItemSize is %{public}zu", opItemSize);
1570 return false;
1571 }
1572 auto cmdListData = val->GetData();
1573 bool ret = parcel.WriteInt32(cmdListData.second);
1574 if (cmdListData.second > LARGE_MALLOC) {
1575 ROSEN_LOGW("RSMarshallingHelper::Marshalling this time malloc memory, size:%{public}zu", cmdListData.second);
1576 }
1577 parcel.WriteInt32(val->GetWidth());
1578 parcel.WriteInt32(val->GetHeight());
1579
1580 parcel.WriteBool(val->GetIsCache());
1581 parcel.WriteBool(val->GetCachedHighContrast());
1582 auto replacedOpList = val->GetReplacedOpList();
1583 parcel.WriteUint32(replacedOpList.size());
1584 for (size_t i = 0; i < replacedOpList.size(); ++i) {
1585 parcel.WriteUint32(replacedOpList[i].first);
1586 parcel.WriteUint32(replacedOpList[i].second);
1587 }
1588
1589 if (cmdListData.second == 0) {
1590 ROSEN_LOGW("unirender: RSMarshallingHelper::Marshalling Drawing::DrawCmdList, size is 0");
1591 return ret;
1592 }
1593 ret &= RSMarshallingHelper::WriteToParcel(parcel, cmdListData.first, cmdListData.second);
1594 if (!ret) {
1595 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::DrawCmdList");
1596 return ret;
1597 }
1598
1599 auto imageData = val->GetAllImageData();
1600 ret &= parcel.WriteInt32(imageData.second);
1601 if (!ret) {
1602 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::DrawCmdList image size");
1603 return ret;
1604 }
1605 if (imageData.second > 0) {
1606 ret &= RSMarshallingHelper::WriteToParcel(parcel, imageData.first, imageData.second);
1607 if (!ret) {
1608 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::DrawCmdList image");
1609 return ret;
1610 }
1611 }
1612
1613 auto bitmapData = val->GetAllBitmapData();
1614 ret &= parcel.WriteInt32(bitmapData.second);
1615 if (!ret) {
1616 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::DrawCmdList bitmap size");
1617 return ret;
1618 }
1619 if (bitmapData.second > 0) {
1620 ret &= RSMarshallingHelper::WriteToParcel(parcel, bitmapData.first, bitmapData.second);
1621 if (!ret) {
1622 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::DrawCmdList bitmap");
1623 return ret;
1624 }
1625 }
1626
1627 std::vector<std::shared_ptr<Drawing::ExtendImageObject>> objectVec;
1628 uint32_t objectSize = val->GetAllObject(objectVec);
1629 ret &= parcel.WriteUint32(objectSize);
1630 if (objectSize > 0) {
1631 for (const auto& object : objectVec) {
1632 auto rsObject = std::static_pointer_cast<RSExtendImageObject>(object);
1633 ret &= RSMarshallingHelper::Marshalling(parcel, rsObject);
1634 if (!ret) {
1635 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::DrawCmdList imageObject");
1636 return ret;
1637 }
1638 }
1639 }
1640
1641 std::vector<std::shared_ptr<Drawing::ExtendImageBaseObj>> objectBaseVec;
1642 uint32_t objectBaseSize = val->GetAllBaseObj(objectBaseVec);
1643 ret &= parcel.WriteUint32(objectBaseSize);
1644 if (!ret) {
1645 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::DrawCmdList imageBase");
1646 return ret;
1647 }
1648 if (objectBaseSize > 0) {
1649 for (const auto& objectBase : objectBaseVec) {
1650 auto rsBaseObject = std::static_pointer_cast<RSExtendImageBaseObj>(objectBase);
1651 ret &= RSMarshallingHelper::Marshalling(parcel, rsBaseObject);
1652 if (!ret) {
1653 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::DrawCmdList imageBase");
1654 return ret;
1655 }
1656 }
1657 }
1658
1659 ret &= MarshallingExtendObjectFromDrawCmdList(parcel, val);
1660 if (!ret) {
1661 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::DrawCmdList ExtendObject");
1662 return ret;
1663 }
1664 if (!isRecordCmd) {
1665 ret &= MarshallingRecordCmdFromDrawCmdList(parcel, val);
1666 if (!ret) {
1667 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::DrawCmdList RecordCmd");
1668 return ret;
1669 }
1670 }
1671 #ifdef ROSEN_OHOS
1672 std::vector<std::shared_ptr<Drawing::SurfaceBufferEntry>> surfaceBufferEntryVec;
1673 uint32_t surfaceBufferSize = val->GetAllSurfaceBufferEntry(surfaceBufferEntryVec);
1674 ret = parcel.WriteUint32(surfaceBufferSize);
1675 if (surfaceBufferSize > 0) {
1676 for (const auto& object : surfaceBufferEntryVec) {
1677 if (!object) {
1678 ROSEN_LOGE("RSMarshallingHelper::Marshalling DrawCmdList surfaceBufferVec has null object");
1679 return false;
1680 }
1681 auto surfaceBuffer = object->surfaceBuffer_;
1682 MessageParcel* parcelSurfaceBuffer = static_cast<MessageParcel*>(&parcel);
1683 WriteSurfaceBufferImpl(
1684 *parcelSurfaceBuffer, surfaceBuffer->GetSeqNum(), surfaceBuffer);
1685 auto acquireFence = object->acquireFence_;
1686 if (acquireFence) {
1687 parcel.WriteBool(true);
1688 acquireFence->WriteToMessageParcel(*parcelSurfaceBuffer);
1689 } else {
1690 parcel.WriteBool(false);
1691 }
1692 }
1693 }
1694 #endif
1695 return ret;
1696 }
1697
Unmarshalling(Parcel & parcel,std::shared_ptr<Drawing::DrawCmdList> & val,uint32_t * opItemCount)1698 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<Drawing::DrawCmdList>& val,
1699 uint32_t* opItemCount)
1700 {
1701 int32_t size = parcel.ReadInt32();
1702 if (size == -1) {
1703 val = nullptr;
1704 return true;
1705 }
1706 int32_t width = parcel.ReadInt32();
1707 int32_t height = parcel.ReadInt32();
1708 if (size == 0) {
1709 ROSEN_LOGW("unirender: RSMarshallingHelper::Unmarshalling Drawing::DrawCmdList size is 0");
1710 val = std::make_shared<Drawing::DrawCmdList>(width, height);
1711 return true;
1712 }
1713
1714 bool isCache = parcel.ReadBool();
1715 bool cachedHighContrast = parcel.ReadBool();
1716
1717 uint32_t replacedOpListSize = parcel.ReadUint32();
1718 uint32_t readableSize = parcel.GetReadableBytes() / (sizeof(uint32_t) * 2); // 增加IPC异常保护,读取2个uint_32_t
1719 if (replacedOpListSize > readableSize) {
1720 ROSEN_LOGE("RSMarshallingHelper::Unmarshalling Drawing readableSize %{public}d less than size %{public}d",
1721 readableSize, replacedOpListSize);
1722 val = nullptr;
1723 return false;
1724 }
1725 std::vector<std::pair<size_t, size_t>> replacedOpList;
1726 for (uint32_t i = 0; i < replacedOpListSize; ++i) {
1727 auto regionPos = parcel.ReadUint32();
1728 auto replacePos = parcel.ReadUint32();
1729 replacedOpList.emplace_back(regionPos, replacePos);
1730 }
1731
1732 bool isMalloc = false;
1733 const void* data = RSMarshallingHelper::ReadFromParcel(parcel, size, isMalloc);
1734 if (data == nullptr) {
1735 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling Drawing::DrawCmdList");
1736 return false;
1737 }
1738
1739 val = Drawing::DrawCmdList::CreateFromData({ data, size }, true);
1740 if (isMalloc) {
1741 free(const_cast<void*>(data));
1742 data = nullptr;
1743 }
1744 if (val == nullptr) {
1745 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling Drawing::DrawCmdList is nullptr");
1746 return false;
1747 }
1748 val->SetWidth(width);
1749 val->SetHeight(height);
1750
1751 val->SetIsCache(isCache);
1752 val->SetCachedHighContrast(cachedHighContrast);
1753 val->SetReplacedOpList(replacedOpList);
1754
1755 int32_t imageSize = parcel.ReadInt32();
1756 if (imageSize > 0) {
1757 bool isMal = false;
1758 const void* imageData = RSMarshallingHelper::ReadFromParcel(parcel, imageSize, isMal);
1759 if (imageData == nullptr) {
1760 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling Drawing::DrawCmdList image is nullptr");
1761 return false;
1762 }
1763 val->SetUpImageData(imageData, imageSize);
1764 if (isMal) {
1765 free(const_cast<void*>(imageData));
1766 imageData = nullptr;
1767 }
1768 }
1769
1770 int32_t bitmapSize = parcel.ReadInt32();
1771 if (bitmapSize > 0) {
1772 bool isMal = false;
1773 const void* bitmapData = RSMarshallingHelper::ReadFromParcel(parcel, bitmapSize, isMal);
1774 if (bitmapData == nullptr) {
1775 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling Drawing::DrawCmdList bitmap is nullptr");
1776 return false;
1777 }
1778 val->SetUpBitmapData(bitmapData, bitmapSize);
1779 if (isMal) {
1780 free(const_cast<void*>(bitmapData));
1781 bitmapData = nullptr;
1782 }
1783 }
1784
1785 bool ret = true;
1786 uint32_t objectSize = parcel.ReadUint32();
1787 if (objectSize > 0) {
1788 if (objectSize > Drawing::MAX_OPITEMSIZE) {
1789 return false;
1790 }
1791 std::vector<std::shared_ptr<Drawing::ExtendImageObject>> imageObjectVec;
1792 for (uint32_t i = 0; i < objectSize; i++) {
1793 std::shared_ptr<RSExtendImageObject> object;
1794 ret &= RSMarshallingHelper::Unmarshalling(parcel, object);
1795 if (!ret) {
1796 ROSEN_LOGE(
1797 "unirender: failed RSMarshallingHelper::Unmarshalling DrawCmdList imageObject: %{public}d", i);
1798 return ret;
1799 }
1800 imageObjectVec.emplace_back(object);
1801 }
1802 val->SetupObject(imageObjectVec);
1803 }
1804
1805 uint32_t objectBaseSize = parcel.ReadUint32();
1806 if (objectBaseSize > 0) {
1807 if (objectBaseSize > Drawing::MAX_OPITEMSIZE) {
1808 return false;
1809 }
1810 std::vector<std::shared_ptr<Drawing::ExtendImageBaseObj>> ObjectBaseVec;
1811 for (uint32_t i = 0; i < objectBaseSize; i++) {
1812 std::shared_ptr<RSExtendImageBaseObj> objectBase;
1813 ret &= RSMarshallingHelper::Unmarshalling(parcel, objectBase);
1814 if (!ret) {
1815 ROSEN_LOGE(
1816 "unirender: failed RSMarshallingHelper::Unmarshalling DrawCmdList objectBase: %{public}d", i);
1817 return ret;
1818 }
1819 ObjectBaseVec.emplace_back(objectBase);
1820 }
1821 val->SetupBaseObj(ObjectBaseVec);
1822 }
1823
1824 ret &= UnmarshallingExtendObjectToDrawCmdList(parcel, val);
1825 if (!ret) {
1826 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::DrawCmdList ExtendObject");
1827 return ret;
1828 }
1829
1830 if (!opItemCount) {
1831 ret &= UnmarshallingRecordCmdToDrawCmdList(parcel, val);
1832 if (!ret) {
1833 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::DrawCmdList RecordCmd");
1834 return ret;
1835 }
1836 }
1837 #ifdef ROSEN_OHOS
1838 uint32_t surfaceBufferEntrySize = parcel.ReadUint32();
1839 if (surfaceBufferEntrySize > 0) {
1840 if (surfaceBufferEntrySize > Drawing::MAX_OPITEMSIZE) {
1841 return false;
1842 }
1843 std::vector<std::shared_ptr<Drawing::SurfaceBufferEntry>> surfaceBufferEntryVec;
1844 for (uint32_t i = 0; i < surfaceBufferEntrySize; ++i) {
1845 sptr<SurfaceBuffer> surfaceBuffer;
1846 MessageParcel* parcelSurfaceBuffer = static_cast<MessageParcel*>(&parcel);
1847 uint32_t sequence = 0U;
1848 GSError retCode = ReadSurfaceBufferImpl(*parcelSurfaceBuffer, sequence, surfaceBuffer);
1849 if (retCode != GSERROR_OK) {
1850 ROSEN_LOGE(
1851 "RSMarshallingHelper::Unmarshalling failed read surfaceBuffer: %{public}d %{public}d", i, retCode);
1852 return false;
1853 }
1854 sptr<SyncFence> acquireFence = nullptr;
1855 bool hasAcquireFence = parcel.ReadBool();
1856 if (hasAcquireFence) {
1857 acquireFence = SyncFence::ReadFromMessageParcel(*parcelSurfaceBuffer);
1858 }
1859 std::shared_ptr<Drawing::SurfaceBufferEntry> surfaceBufferEntry =
1860 std::make_shared<Drawing::SurfaceBufferEntry>(surfaceBuffer, acquireFence);
1861 surfaceBufferEntryVec.emplace_back(surfaceBufferEntry);
1862 }
1863 val->SetupSurfaceBufferEntry(surfaceBufferEntryVec);
1864 }
1865 #endif
1866 val->UnmarshallingDrawOps(opItemCount);
1867 return ret;
1868 }
1869
Marshalling(Parcel & parcel,const std::shared_ptr<Drawing::RecordCmd> & val)1870 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<Drawing::RecordCmd>& val)
1871 {
1872 if (!val) {
1873 ROSEN_LOGE("RSMarshallingHelper::Marshalling failed, RecordCmd is nullptr");
1874 return false;
1875 }
1876 const auto& rect = val->GetCullRect();
1877 return parcel.WriteFloat(rect.GetLeft()) && parcel.WriteFloat(rect.GetTop()) &&
1878 parcel.WriteFloat(rect.GetRight()) && parcel.WriteFloat(rect.GetBottom()) &&
1879 RSMarshallingHelper::Marshalling(parcel, val->GetDrawCmdList(), true);
1880 }
1881
Unmarshalling(Parcel & parcel,std::shared_ptr<Drawing::RecordCmd> & val,uint32_t * opItemCount)1882 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<Drawing::RecordCmd>& val,
1883 uint32_t* opItemCount)
1884 {
1885 val = nullptr;
1886 float left = 0;
1887 float top = 0;
1888 float right = 0;
1889 float bottom = 0;
1890 bool success = parcel.ReadFloat(left) && parcel.ReadFloat(top) &&
1891 parcel.ReadFloat(right) && parcel.ReadFloat(bottom);
1892 if (!success) {
1893 ROSEN_LOGE("RSMarshallingHelper::Unmarshalling RecordCmd, read float failed.");
1894 return false;
1895 }
1896 std::shared_ptr<Drawing::DrawCmdList> drawCmdList = nullptr;
1897 success = RSMarshallingHelper::Unmarshalling(parcel, drawCmdList, opItemCount);
1898 if (!success) {
1899 ROSEN_LOGE("RSMarshallingHelper::Unmarshalling RecordCmd, drawCmdList unmarshalling failed.");
1900 return false;
1901 }
1902 Drawing::Rect rect(left, top, right, bottom);
1903 val = std::make_shared<Drawing::RecordCmd>(drawCmdList, rect);
1904 return true;
1905 }
1906
Marshalling(Parcel & parcel,const std::shared_ptr<RSExtendImageObject> & val)1907 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSExtendImageObject>& val)
1908 {
1909 if (!val) {
1910 return parcel.WriteInt32(-1);
1911 }
1912 if (!(parcel.WriteInt32(1) && val->Marshalling(parcel))) {
1913 ROSEN_LOGE("failed RSMarshallingHelper::Marshalling imageObject");
1914 return false;
1915 }
1916
1917 return true;
1918 }
1919
Unmarshalling(Parcel & parcel,std::shared_ptr<RSExtendImageObject> & val)1920 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSExtendImageObject>& val)
1921 {
1922 if (parcel.ReadInt32() == -1) {
1923 val = nullptr;
1924 return true;
1925 }
1926 val.reset(RSExtendImageObject::Unmarshalling(parcel));
1927 if (val == nullptr) {
1928 ROSEN_LOGE("failed RSMarshallingHelper::Unmarshalling imageObject");
1929 return false;
1930 }
1931
1932 return true;
1933 }
1934
Marshalling(Parcel & parcel,const std::shared_ptr<RSExtendImageBaseObj> & val)1935 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSExtendImageBaseObj>& val)
1936 {
1937 if (!val) {
1938 return parcel.WriteInt32(-1);
1939 }
1940 if (!(parcel.WriteInt32(1) && val->Marshalling(parcel))) {
1941 ROSEN_LOGE("failed RSMarshallingHelper::Marshalling ImageBaseObj");
1942 return false;
1943 }
1944
1945 return true;
1946 }
1947
Unmarshalling(Parcel & parcel,std::shared_ptr<RSExtendImageBaseObj> & val)1948 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSExtendImageBaseObj>& val)
1949 {
1950 if (parcel.ReadInt32() == -1) {
1951 val = nullptr;
1952 return true;
1953 }
1954 val.reset(RSExtendImageBaseObj::Unmarshalling(parcel));
1955 if (val == nullptr) {
1956 ROSEN_LOGE("failed RSMarshallingHelper::Unmarshalling ImageBaseObj");
1957 return false;
1958 }
1959
1960 return true;
1961 }
1962
Marshalling(Parcel & parcel,const std::shared_ptr<Drawing::MaskCmdList> & val)1963 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<Drawing::MaskCmdList>& val)
1964 {
1965 if (!val) {
1966 return parcel.WriteInt32(-1);
1967 }
1968 auto cmdListData = val->GetData();
1969 bool ret = parcel.WriteInt32(cmdListData.second);
1970 if (cmdListData.second == 0 || !ret) {
1971 ROSEN_LOGW("unirender: RSMarshallingHelper::Marshalling Drawing::MaskCmdList, size is 0");
1972 return ret;
1973 }
1974
1975 ret = RSMarshallingHelper::WriteToParcel(parcel, cmdListData.first, cmdListData.second);
1976 if (!ret) {
1977 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::MaskCmdList");
1978 return ret;
1979 }
1980
1981 auto imageData = val->GetAllImageData();
1982 ret = parcel.WriteInt32(imageData.second);
1983 if (!ret) {
1984 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::MaskCmdList image size");
1985 return ret;
1986 }
1987 if (imageData.second > 0) {
1988 ret = RSMarshallingHelper::WriteToParcel(parcel, imageData.first, imageData.second);
1989 if (!ret) {
1990 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::MaskCmdList image");
1991 return ret;
1992 }
1993 }
1994
1995 auto bitmapData = val->GetAllBitmapData();
1996 ret &= parcel.WriteInt32(bitmapData.second);
1997 if (!ret) {
1998 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::MaskCmdList bitmap size");
1999 return ret;
2000 }
2001 if (bitmapData.second > 0) {
2002 ret &= RSMarshallingHelper::WriteToParcel(parcel, bitmapData.first, bitmapData.second);
2003 if (!ret) {
2004 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Marshalling Drawing::MaskCmdList bitmap");
2005 return ret;
2006 }
2007 }
2008
2009 return ret;
2010 }
2011
Unmarshalling(Parcel & parcel,std::shared_ptr<Drawing::MaskCmdList> & val)2012 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<Drawing::MaskCmdList>& val)
2013 {
2014 int32_t size = parcel.ReadInt32();
2015 if (size == -1) {
2016 return true;
2017 }
2018 if (size == 0) {
2019 ROSEN_LOGW("unirender: RSMarshallingHelper::Unmarshalling Drawing::MaskCmdList size is 0");
2020 return true;
2021 }
2022 bool isMalloc = false;
2023 const void* data = RSMarshallingHelper::ReadFromParcel(parcel, size, isMalloc);
2024 if (data == nullptr) {
2025 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling Drawing::MaskCmdList");
2026 return false;
2027 }
2028 val = Drawing::MaskCmdList::CreateFromData({ data, size }, true);
2029 if (isMalloc) {
2030 free(const_cast<void*>(data));
2031 data = nullptr;
2032 }
2033 if (val == nullptr) {
2034 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling Drawing::MaskCmdList is nullptr");
2035 return false;
2036 }
2037 int32_t imageSize = parcel.ReadInt32();
2038 if (imageSize > 0) {
2039 bool isMal = false;
2040 const void* imageData = RSMarshallingHelper::ReadFromParcel(parcel, imageSize, isMal);
2041 if (imageData == nullptr) {
2042 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling Drawing::MaskCmdList image is nullptr");
2043 return false;
2044 }
2045 val->SetUpImageData(imageData, imageSize);
2046 if (isMal) {
2047 free(const_cast<void*>(imageData));
2048 imageData = nullptr;
2049 }
2050 }
2051 int32_t bitmapSize = parcel.ReadInt32();
2052 if (bitmapSize > 0) {
2053 bool isMal = false;
2054 const void* bitmapData = RSMarshallingHelper::ReadFromParcel(parcel, bitmapSize, isMal);
2055 if (bitmapData == nullptr) {
2056 ROSEN_LOGE("unirender: failed RSMarshallingHelper::Unmarshalling Drawing::MaskCmdList bitmap is nullptr");
2057 return false;
2058 }
2059 val->SetUpBitmapData(bitmapData, bitmapSize);
2060 if (isMal) {
2061 free(const_cast<void*>(bitmapData));
2062 bitmapData = nullptr;
2063 }
2064 }
2065 return true;
2066 }
2067
2068
2069 #define MARSHALLING_AND_UNMARSHALLING(TYPE) \
2070 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<TYPE>& val) \
2071 { \
2072 return parcel.WriteParcelable(val.get()); \
2073 } \
2074 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<TYPE>& val) \
2075 { \
2076 val.reset(parcel.ReadParcelable<TYPE>()); \
2077 return val != nullptr; \
2078 }
2079 MARSHALLING_AND_UNMARSHALLING(RSRenderTransition)
MARSHALLING_AND_UNMARSHALLING(RSRenderTransitionEffect)2080 MARSHALLING_AND_UNMARSHALLING(RSRenderTransitionEffect)
2081 #undef MARSHALLING_AND_UNMARSHALLING
2082
2083 #define MARSHALLING_AND_UNMARSHALLING(TEMPLATE) \
2084 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<TEMPLATE>& val) \
2085 { \
2086 return parcel.WriteParcelable(val.get()); \
2087 } \
2088 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<TEMPLATE>& val) \
2089 { \
2090 val.reset(parcel.ReadParcelable<TEMPLATE>()); \
2091 return val != nullptr; \
2092 }
2093
2094 MARSHALLING_AND_UNMARSHALLING(RSRenderCurveAnimation)
2095 MARSHALLING_AND_UNMARSHALLING(RSRenderParticleAnimation)
2096 MARSHALLING_AND_UNMARSHALLING(RSRenderInterpolatingSpringAnimation)
2097 MARSHALLING_AND_UNMARSHALLING(RSRenderKeyframeAnimation)
2098 MARSHALLING_AND_UNMARSHALLING(RSRenderSpringAnimation)
2099 MARSHALLING_AND_UNMARSHALLING(RSRenderPathAnimation)
2100 #undef MARSHALLING_AND_UNMARSHALLING
2101
2102 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSRenderModifier>& val)
2103 {
2104 return val != nullptr && val->Marshalling(parcel);
2105 }
Unmarshalling(Parcel & parcel,std::shared_ptr<RSRenderModifier> & val)2106 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSRenderModifier>& val)
2107 {
2108 val.reset(RSRenderModifier::Unmarshalling(parcel));
2109 return val != nullptr;
2110 }
2111
2112 #define MARSHALLING_AND_UNMARSHALLING(TEMPLATE) \
2113 template<typename T> \
2114 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<TEMPLATE<T>>& val) \
2115 { \
2116 return parcel.WriteInt16(static_cast<int16_t>(val->GetPropertyType())) && parcel.WriteUint64(val->GetId()) && \
2117 Marshalling(parcel, val->Get()); \
2118 } \
2119 template<typename T> \
2120 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<TEMPLATE<T>>& val) \
2121 { \
2122 PropertyId id = 0; \
2123 int16_t typeId = 0; \
2124 if (!parcel.ReadInt16(typeId)) { \
2125 return false; \
2126 } \
2127 RSRenderPropertyType type = static_cast<RSRenderPropertyType>(typeId); \
2128 if (!parcel.ReadUint64(id)) { \
2129 return false; \
2130 } \
2131 T value; \
2132 if (!Unmarshalling(parcel, value)) { \
2133 return false; \
2134 } \
2135 RS_PROFILER_PATCH_NODE_ID(parcel, id); \
2136 val.reset(new TEMPLATE<T>(value, id, type)); \
2137 return val != nullptr; \
2138 }
2139
2140 MARSHALLING_AND_UNMARSHALLING(RSRenderProperty)
MARSHALLING_AND_UNMARSHALLING(RSRenderAnimatableProperty)2141 MARSHALLING_AND_UNMARSHALLING(RSRenderAnimatableProperty)
2142 #undef MARSHALLING_AND_UNMARSHALLING
2143
2144 #define EXPLICIT_INSTANTIATION(TEMPLATE, TYPE) \
2145 template bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<TEMPLATE<TYPE>>& val); \
2146 template bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<TEMPLATE<TYPE>>& val);
2147
2148 #define BATCH_EXPLICIT_INSTANTIATION(TEMPLATE) \
2149 EXPLICIT_INSTANTIATION(TEMPLATE, bool) \
2150 EXPLICIT_INSTANTIATION(TEMPLATE, float) \
2151 EXPLICIT_INSTANTIATION(TEMPLATE, int) \
2152 EXPLICIT_INSTANTIATION(TEMPLATE, Color) \
2153 EXPLICIT_INSTANTIATION(TEMPLATE, RSDynamicBrightnessPara) \
2154 EXPLICIT_INSTANTIATION(TEMPLATE, RSWaterRipplePara) \
2155 EXPLICIT_INSTANTIATION(TEMPLATE, RSFlyOutPara) \
2156 EXPLICIT_INSTANTIATION(TEMPLATE, Gravity) \
2157 EXPLICIT_INSTANTIATION(TEMPLATE, GradientDirection) \
2158 EXPLICIT_INSTANTIATION(TEMPLATE, ForegroundColorStrategyType) \
2159 EXPLICIT_INSTANTIATION(TEMPLATE, Matrix3f) \
2160 EXPLICIT_INSTANTIATION(TEMPLATE, Quaternion) \
2161 EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSFilter>) \
2162 EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSImage>) \
2163 EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSMask>) \
2164 EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSPath>) \
2165 EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSShader>) \
2166 EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSLinearGradientBlurPara>) \
2167 EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<MotionBlurParam>) \
2168 EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSMagnifierParams>) \
2169 EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<EmitterUpdater>) \
2170 EXPLICIT_INSTANTIATION(TEMPLATE, std::vector<std::shared_ptr<EmitterUpdater>>) \
2171 EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<ParticleNoiseField>) \
2172 EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<ParticleNoiseFields>) \
2173 EXPLICIT_INSTANTIATION(TEMPLATE, std::vector<std::shared_ptr<ParticleRenderParams>>) \
2174 EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<ParticleRenderParams>) \
2175 EXPLICIT_INSTANTIATION(TEMPLATE, RSRenderParticleVector) \
2176 EXPLICIT_INSTANTIATION(TEMPLATE, RenderParticleColorParaType) \
2177 EXPLICIT_INSTANTIATION(TEMPLATE, RenderParticleParaType<float>) \
2178 EXPLICIT_INSTANTIATION(TEMPLATE, ParticleVelocity) \
2179 EXPLICIT_INSTANTIATION(TEMPLATE, EmitterConfig) \
2180 EXPLICIT_INSTANTIATION(TEMPLATE, Vector2f) \
2181 EXPLICIT_INSTANTIATION(TEMPLATE, Vector4<uint32_t>) \
2182 EXPLICIT_INSTANTIATION(TEMPLATE, Vector4<Color>) \
2183 EXPLICIT_INSTANTIATION(TEMPLATE, Vector4f) \
2184 EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<Drawing::DrawCmdList>) \
2185 EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<Drawing::RecordCmd>) \
2186 EXPLICIT_INSTANTIATION(TEMPLATE, Drawing::Matrix)
2187
2188 BATCH_EXPLICIT_INSTANTIATION(RSRenderProperty)
2189
2190 #undef EXPLICIT_INSTANTIATION
2191 #undef BATCH_EXPLICIT_INSTANTIATION
2192
2193 #define EXPLICIT_INSTANTIATION(TEMPLATE, TYPE) \
2194 template bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<TEMPLATE<TYPE>>& val); \
2195 template bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<TEMPLATE<TYPE>>& val);
2196
2197 #define BATCH_EXPLICIT_INSTANTIATION(TEMPLATE) \
2198 EXPLICIT_INSTANTIATION(TEMPLATE, float) \
2199 EXPLICIT_INSTANTIATION(TEMPLATE, int) \
2200 EXPLICIT_INSTANTIATION(TEMPLATE, Color) \
2201 EXPLICIT_INSTANTIATION(TEMPLATE, Matrix3f) \
2202 EXPLICIT_INSTANTIATION(TEMPLATE, Quaternion) \
2203 EXPLICIT_INSTANTIATION(TEMPLATE, std::shared_ptr<RSFilter>) \
2204 EXPLICIT_INSTANTIATION(TEMPLATE, Vector2f) \
2205 EXPLICIT_INSTANTIATION(TEMPLATE, Vector4<Color>) \
2206 EXPLICIT_INSTANTIATION(TEMPLATE, Vector4f) \
2207 EXPLICIT_INSTANTIATION(TEMPLATE, RRectT<float>)
2208
2209 BATCH_EXPLICIT_INSTANTIATION(RSRenderAnimatableProperty)
2210
2211 #undef EXPLICIT_INSTANTIATION
2212 #undef BATCH_EXPLICIT_INSTANTIATION
2213
2214 bool RSMarshallingHelper::WriteToParcel(Parcel& parcel, const void* data, size_t size)
2215 {
2216 if (data == nullptr) {
2217 ROSEN_LOGE("RSMarshallingHelper::WriteToParcel data is nullptr");
2218 return false;
2219 }
2220 if (size > MAX_DATA_SIZE) {
2221 ROSEN_LOGD("RSMarshallingHelper::WriteToParcel data exceed MAX_DATA_SIZE, size:%{public}zu", size);
2222 }
2223
2224 if (!parcel.WriteUint32(size)) {
2225 return false;
2226 }
2227 if (size < MIN_DATA_SIZE || (!g_useSharedMem && g_tid == std::this_thread::get_id())) {
2228 return parcel.WriteUnpadBuffer(data, size);
2229 }
2230
2231 // write to ashmem
2232 auto ashmemAllocator = AshmemAllocator::CreateAshmemAllocator(size, PROT_READ | PROT_WRITE);
2233 if (!ashmemAllocator) {
2234 ROSEN_LOGE("RSMarshallingHelper::WriteToParcel CreateAshmemAllocator fail");
2235 return false;
2236 }
2237 RS_PROFILER_WRITE_PARCEL_DATA(parcel);
2238 int fd = ashmemAllocator->GetFd();
2239 if (!(static_cast<MessageParcel*>(&parcel)->WriteFileDescriptor(fd))) {
2240 ROSEN_LOGE("RSMarshallingHelper::WriteToParcel WriteFileDescriptor error");
2241 return false;
2242 }
2243 if (!ashmemAllocator->WriteToAshmem(data, size)) {
2244 ROSEN_LOGE("RSMarshallingHelper::WriteToParcel memcpy_s failed");
2245 return false;
2246 }
2247 return true;
2248 }
2249
ReadFromParcel(Parcel & parcel,size_t size,bool & isMalloc)2250 const void* RSMarshallingHelper::ReadFromParcel(Parcel& parcel, size_t size, bool& isMalloc)
2251 {
2252 uint32_t bufferSize = parcel.ReadUint32();
2253 if (static_cast<unsigned int>(bufferSize) != size) {
2254 ROSEN_LOGE("RSMarshallingHelper::ReadFromParcel size mismatch");
2255 return nullptr;
2256 }
2257 if (static_cast<unsigned int>(bufferSize) < MIN_DATA_SIZE ||
2258 (!g_useSharedMem && g_tid == std::this_thread::get_id())) {
2259 isMalloc = false;
2260 return parcel.ReadUnpadBuffer(size);
2261 }
2262 // read from ashmem
2263 return RS_PROFILER_READ_PARCEL_DATA(parcel, size, isMalloc);
2264 }
2265
SkipFromParcel(Parcel & parcel,size_t size)2266 bool RSMarshallingHelper::SkipFromParcel(Parcel& parcel, size_t size)
2267 {
2268 int32_t bufferSize = parcel.ReadInt32();
2269 if (static_cast<unsigned int>(bufferSize) != size) {
2270 ROSEN_LOGE("RSMarshallingHelper::SkipFromParcel size mismatch");
2271 return false;
2272 }
2273 if (static_cast<unsigned int>(bufferSize) < MIN_DATA_SIZE ||
2274 (!g_useSharedMem && g_tid == std::this_thread::get_id())) {
2275 parcel.SkipBytes(size);
2276 return true;
2277 }
2278 // read from ashmem
2279 int fd = static_cast<MessageParcel*>(&parcel)->ReadFileDescriptor();
2280 auto ashmemAllocator = AshmemAllocator::CreateAshmemAllocatorWithFd(fd, size, PROT_READ);
2281 return ashmemAllocator != nullptr;
2282 }
2283
ReadFromAshmem(Parcel & parcel,size_t size,bool & isMalloc)2284 const void* RSMarshallingHelper::ReadFromAshmem(Parcel& parcel, size_t size, bool& isMalloc)
2285 {
2286 isMalloc = false;
2287 int fd = static_cast<MessageParcel*>(&parcel)->ReadFileDescriptor();
2288 auto ashmemAllocator = AshmemAllocator::CreateAshmemAllocatorWithFd(fd, size, PROT_READ);
2289 if (!ashmemAllocator) {
2290 ROSEN_LOGE("RSMarshallingHelper::ReadFromAshmem CreateAshmemAllocator fail");
2291 return nullptr;
2292 }
2293 isMalloc = true;
2294 return ashmemAllocator->CopyFromAshmem(size);
2295 }
2296
BeginNoSharedMem(std::thread::id tid)2297 void RSMarshallingHelper::BeginNoSharedMem(std::thread::id tid)
2298 {
2299 g_useSharedMem = false;
2300 g_tid = tid;
2301 }
EndNoSharedMem()2302 void RSMarshallingHelper::EndNoSharedMem()
2303 {
2304 g_useSharedMem = true;
2305 g_tid.__reset();
2306 }
2307
GetUseSharedMem(std::thread::id tid)2308 bool RSMarshallingHelper::GetUseSharedMem(std::thread::id tid)
2309 {
2310 if (tid == g_tid) {
2311 return g_useSharedMem;
2312 }
2313 return true;
2314 }
2315
Marshalling(Parcel & parcel,const std::shared_ptr<RSRenderPropertyBase> & val)2316 bool RSMarshallingHelper::Marshalling(Parcel& parcel, const std::shared_ptr<RSRenderPropertyBase>& val)
2317 {
2318 return RSRenderPropertyBase::Marshalling(parcel, val);
2319 }
2320
Unmarshalling(Parcel & parcel,std::shared_ptr<RSRenderPropertyBase> & val)2321 bool RSMarshallingHelper::Unmarshalling(Parcel& parcel, std::shared_ptr<RSRenderPropertyBase>& val)
2322 {
2323 return RSRenderPropertyBase::Unmarshalling(parcel, val);
2324 }
2325 } // namespace Rosen
2326 } // namespace OHOS
2327