• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "animation/rs_cubic_bezier_interpolator.h"
17 #include "animation/rs_interpolator.h"
18 #include "animation/rs_render_animation.h"
19 #include "animation/rs_render_curve_animation.h"
20 #include "animation/rs_render_interpolating_spring_animation.h"
21 #include "animation/rs_render_keyframe_animation.h"
22 #include "animation/rs_render_path_animation.h"
23 #include "animation/rs_render_property_animation.h"
24 #include "animation/rs_render_spring_animation.h"
25 #include "animation/rs_render_transition_effect.h"
26 #include "animation/rs_render_transition.h"
27 #include "animation/rs_spring_interpolator.h"
28 #include "animation/rs_steps_interpolator.h"
29 #include "platform/common/rs_log.h"
30 
31 #include "transaction/rs_marshalling_helper.h"
32 
33 namespace OHOS {
34 namespace Rosen {
35 namespace {
36 static constexpr int MAX_KEYFRAME_SIZE_NUMBER = 100000;
37 
38 enum RSTransitionEffectType : uint16_t {
39     FADE = 1,
40     SCALE,
41     TRANSLATE,
42     ROTATE,
43     UNDEFINED,
44 };
45 } // namespace
46 
Marshalling(Parcel & parcel) const47 bool RSCubicBezierInterpolator::Marshalling(Parcel& parcel) const
48 {
49     if (!parcel.WriteUint16(InterpolatorType::CUBIC_BEZIER)) {
50         ROSEN_LOGE("Marshalling RSCubicBezierInterpolator type failed");
51         return false;
52     }
53     if (!parcel.WriteUint64(id_)) {
54         ROSEN_LOGE("Marshalling RSCubicBezierInterpolator id failed");
55         return false;
56     }
57     if (!(parcel.WriteFloat(controlX1_) && parcel.WriteFloat(controlY1_) && parcel.WriteFloat(controlX2_) &&
58             parcel.WriteFloat(controlY2_))) {
59         ROSEN_LOGE("Marshalling RSCubicBezierInterpolator parameter failed");
60         return false;
61     }
62     return true;
63 }
64 
Unmarshalling(Parcel & parcel)65 RSCubicBezierInterpolator* RSCubicBezierInterpolator::Unmarshalling(Parcel& parcel)
66 {
67     uint64_t id{0};
68     if (!RSMarshallingHelper::UnmarshallingPidPlusIdNoChangeIfZero(parcel, id)) {
69         ROSEN_LOGE("Unmarshalling RSCubicBezierInterpolator id failed");
70         return nullptr;
71     }
72     if (id == 0) {
73         ROSEN_LOGE("Unmarshalling RSCubicBezierInterpolator id == 0");
74         return nullptr;
75     }
76     float x1 = 0;
77     float y1 = 0;
78     float x2 = 0;
79     float y2 = 0;
80     if (!(parcel.ReadFloat(x1) && parcel.ReadFloat(y1) && parcel.ReadFloat(x2) && parcel.ReadFloat(y2))) {
81         ROSEN_LOGE("Unmarshalling RSCubicBezierInterpolator parameter failed");
82         return nullptr;
83     }
84     return new RSCubicBezierInterpolator(id, x1, y1, x2, y2);
85 }
86 
UnmarshallingFromParcel(Parcel & parcel)87 RSInterpolator* RSInterpolator::UnmarshallingFromParcel(Parcel& parcel)
88 {
89     uint16_t interpolatorType = 0;
90     if (!parcel.ReadUint16(interpolatorType)) {
91         ROSEN_LOGE("RSInterpolator::Unmarshalling read type failed");
92         return nullptr;
93     }
94     RSInterpolator* ret = nullptr;
95     switch (interpolatorType) {
96         case InterpolatorType::LINEAR:
97             ret = LinearInterpolator::Unmarshalling(parcel);
98             break;
99         case InterpolatorType::CUSTOM:
100             ret = RSCustomInterpolator::Unmarshalling(parcel);
101             break;
102         case InterpolatorType::CUBIC_BEZIER:
103             ret = RSCubicBezierInterpolator::Unmarshalling(parcel);
104             break;
105         case InterpolatorType::SPRING:
106             ret = RSSpringInterpolator::Unmarshalling(parcel);
107             break;
108         case InterpolatorType::STEPS:
109             ret = RSStepsInterpolator::Unmarshalling(parcel);
110             break;
111         default:
112             break;
113     }
114     return ret;
115 }
116 
Unmarshalling(Parcel & parcel)117 std::shared_ptr<RSInterpolator> RSInterpolator::Unmarshalling(Parcel& parcel)
118 {
119     RSInterpolator* rawInterpolator = UnmarshallingFromParcel(parcel);
120     if (rawInterpolator == nullptr) {
121         ROSEN_LOGE("RSInterpolator::Unmarshalling rawInterpolator is nullptr");
122         return nullptr;
123     }
124 
125     static std::mutex cachedInterpolatorsMutex_;
126     static std::unordered_map<uint64_t, std::weak_ptr<RSInterpolator>> cachedInterpolators_;
127     static const auto Destructor = [](RSInterpolator* ptr) {
128         if (ptr == nullptr) {
129             ROSEN_LOGE("RSInterpolator::Unmarshalling, sharePtr is nullptr.");
130             return;
131         }
132         std::unique_lock<std::mutex> lock(cachedInterpolatorsMutex_);
133         cachedInterpolators_.erase(ptr->id_); // Unregister interpolator from cache before destruction.
134         delete ptr;
135         ptr = nullptr;
136     };
137 
138     {
139         // All cache operations should be performed under lock to prevent race conditions
140         std::unique_lock<std::mutex> lock(cachedInterpolatorsMutex_);
141         // Check if there is an existing valid interpolator in the cache, return it if found.
142         if (auto it = cachedInterpolators_.find(rawInterpolator->id_); it != cachedInterpolators_.end()) {
143             if (auto sharedPtr = it->second.lock()) { // Check if weak pointer is valid
144                 delete rawInterpolator;               // Destroy the newly created object
145                 rawInterpolator = nullptr;
146                 return sharedPtr;
147             } else { // If the weak pointer has expired, erase it from the cache. This should never happen.
148                 ROSEN_LOGE("RSInterpolator::Unmarshalling, cached weak pointer expired.");
149                 cachedInterpolators_.erase(it);
150             }
151         }
152         // No existing interpolator in the cache, create a new one and register it in the cache.
153         std::shared_ptr<RSInterpolator> sharedPtr(rawInterpolator, Destructor);
154         cachedInterpolators_.emplace(rawInterpolator->id_, sharedPtr);
155         return sharedPtr;
156     }
157 }
158 
Marshalling(Parcel & parcel) const159 bool LinearInterpolator::Marshalling(Parcel& parcel) const
160 {
161     if (!parcel.WriteUint16(InterpolatorType::LINEAR)) {
162         ROSEN_LOGE("Marshalling LinearInterpolator type failed");
163         return false;
164     }
165     if (!parcel.WriteUint64(id_)) {
166         ROSEN_LOGE("Marshalling LinearInterpolator id failed");
167         return false;
168     }
169     return true;
170 }
171 
Unmarshalling(Parcel & parcel)172 LinearInterpolator* LinearInterpolator::Unmarshalling(Parcel& parcel)
173 {
174     uint64_t id{0};
175     if (!RSMarshallingHelper::UnmarshallingPidPlusIdNoChangeIfZero(parcel, id)) {
176         ROSEN_LOGE("Unmarshalling RSCubicBezierInterpolator id failed");
177         return nullptr;
178     }
179     if (id == 0) {
180         ROSEN_LOGE("Unmarshalling RSCubicBezierInterpolator id == 0");
181         return nullptr;
182     }
183     return new LinearInterpolator(id);
184 }
185 
Marshalling(Parcel & parcel) const186 bool RSCustomInterpolator::Marshalling(Parcel& parcel) const
187 {
188     if (!parcel.WriteUint16(InterpolatorType::CUSTOM)) {
189         ROSEN_LOGE("RSCustomInterpolator::Marshalling, Write type failed");
190         return false;
191     }
192     if (!parcel.WriteUint64(id_)) {
193         ROSEN_LOGE("RSCustomInterpolator::Marshalling, Write id failed");
194         return false;
195     }
196     if (!RSMarshallingHelper::Marshalling(parcel, times_) || !RSMarshallingHelper::Marshalling(parcel, values_)) {
197         ROSEN_LOGE("RSCustomInterpolator::Marshalling, Write value failed");
198         return false;
199     }
200     return true;
201 }
202 
Unmarshalling(Parcel & parcel)203 RSCustomInterpolator* RSCustomInterpolator::Unmarshalling(Parcel& parcel)
204 {
205     uint64_t id{0};
206     if (!RSMarshallingHelper::UnmarshallingPidPlusIdNoChangeIfZero(parcel, id)) {
207         ROSEN_LOGE("Unmarshalling RSCustomInterpolator id failed");
208         return nullptr;
209     }
210     std::vector<float> times;
211     std::vector<float> values;
212     if (!(RSMarshallingHelper::Unmarshalling(parcel, times, MAX_SAMPLE_POINTS) &&
213         RSMarshallingHelper::Unmarshalling(parcel, values, MAX_SAMPLE_POINTS))) {
214         ROSEN_LOGE("Unmarshalling CustomInterpolator failed");
215         return nullptr;
216     }
217     return new RSCustomInterpolator(id, std::move(times), std::move(values));
218 }
219 
Marshalling(Parcel & parcel) const220 bool RSRenderAnimation::Marshalling(Parcel& parcel) const
221 {
222     // animationId, targetId
223     if (!(parcel.WriteUint64(id_))) {
224         ROSEN_LOGE("RSRenderAnimation::Marshalling, write id failed");
225         return false;
226     }
227     // RSAnimationTimingProtocol
228     if (!(parcel.WriteInt32(animationFraction_.GetDuration()) &&
229         parcel.WriteInt32(animationFraction_.GetStartDelay()) &&
230         parcel.WriteFloat(animationFraction_.GetSpeed()) &&
231         parcel.WriteInt32(animationFraction_.GetRepeatCount()) &&
232         parcel.WriteBool(animationFraction_.GetAutoReverse()) &&
233         parcel.WriteBool(animationFraction_.GetDirection()) &&
234         parcel.WriteInt32(static_cast<std::underlying_type<FillMode>::type>(animationFraction_.GetFillMode())) &&
235         parcel.WriteBool(animationFraction_.GetRepeatCallbackEnable()) &&
236         parcel.WriteInt32(animationFraction_.GetFrameRateRange().min_) &&
237         parcel.WriteInt32(animationFraction_.GetFrameRateRange().max_) &&
238         parcel.WriteInt32(animationFraction_.GetFrameRateRange().preferred_) &&
239         parcel.WriteInt32(static_cast<int32_t>(animationFraction_.GetFrameRateRange().componentScene_)))) {
240         ROSEN_LOGE("RSRenderAnimation::Marshalling, write param failed");
241         return false;
242     }
243     // token for finding ui instance when executing animation callback in client
244     if (!parcel.WriteUint64(token_)) {
245         ROSEN_LOGE("multi-instance, RSRenderAnimation::Marshalling, write token failed");
246         return false;
247     }
248     return true;
249 }
250 
ParseParam(Parcel & parcel)251 bool RSRenderAnimation::ParseParam(Parcel& parcel)
252 {
253     int32_t duration = 0;
254     int32_t startDelay = 0;
255     int32_t repeatCount = 0;
256     int32_t fillMode = 0;
257     float speed = 0.0;
258     bool autoReverse = false;
259     bool direction = false;
260     bool isRepeatCallbackEnable = false;
261     int fpsMin = 0;
262     int fpsMax = 0;
263     int fpsPreferred = 0;
264     int componentScene = 0;
265     uint64_t token = 0;
266     if (!(RSMarshallingHelper::UnmarshallingPidPlusId(parcel, id_) && parcel.ReadInt32(duration) &&
267             parcel.ReadInt32(startDelay) && parcel.ReadFloat(speed) && parcel.ReadInt32(repeatCount) &&
268             parcel.ReadBool(autoReverse) && parcel.ReadBool(direction) && parcel.ReadInt32(fillMode) &&
269             parcel.ReadBool(isRepeatCallbackEnable) && parcel.ReadInt32(fpsMin) && parcel.ReadInt32(fpsMax) &&
270             parcel.ReadInt32(fpsPreferred) && parcel.ReadInt32(componentScene) && parcel.ReadUint64(token))) {
271         ROSEN_LOGE("RSRenderAnimation::ParseParam, read param failed");
272         return false;
273     }
274     SetDuration(duration);
275     SetStartDelay(startDelay);
276     SetRepeatCount(repeatCount);
277     SetAutoReverse(autoReverse);
278     SetSpeed(speed);
279     SetDirection(direction);
280     SetFillMode(static_cast<FillMode>(fillMode));
281     SetRepeatCallbackEnable(isRepeatCallbackEnable);
282     SetFrameRateRange({fpsMin, fpsMax, fpsPreferred, 0, static_cast<ComponentScene>(componentScene)});
283     SetToken(token);
284     return true;
285 }
286 
Marshalling(Parcel & parcel) const287 bool RSRenderCurveAnimation::Marshalling(Parcel& parcel) const
288 {
289     if (!RSRenderPropertyAnimation::Marshalling(parcel)) {
290         ROSEN_LOGE("RSRenderCurveAnimation::Marshalling, RenderPropertyAnimation failed");
291         return false;
292     }
293     if (!(RSMarshallingHelper::Marshalling(parcel, startValue_) &&
294             RSMarshallingHelper::Marshalling(parcel, endValue_) && interpolator_ != nullptr &&
295             interpolator_->Marshalling(parcel))) {
296         ROSEN_LOGE("RSRenderCurveAnimation::Marshalling, MarshallingHelper failed");
297         return false;
298     }
299     return true;
300 }
301 
Unmarshalling(Parcel & parcel)302 RSRenderCurveAnimation* RSRenderCurveAnimation::Unmarshalling(Parcel& parcel)
303 {
304     RSRenderCurveAnimation* renderCurveAnimation = new RSRenderCurveAnimation();
305     if (!renderCurveAnimation->ParseParam(parcel)) {
306         ROSEN_LOGE("RSRenderCurveAnimation::Unmarshalling, failed");
307         delete renderCurveAnimation;
308         renderCurveAnimation = nullptr;
309         return nullptr;
310     }
311     return renderCurveAnimation;
312 }
313 
ParseParam(Parcel & parcel)314 bool RSRenderCurveAnimation::ParseParam(Parcel& parcel)
315 {
316     if (!RSRenderPropertyAnimation::ParseParam(parcel)) {
317         ROSEN_LOGE("RSRenderCurveAnimation::ParseParam, ParseParam Fail");
318         return false;
319     }
320 
321     if (!(RSMarshallingHelper::Unmarshalling(parcel, startValue_) &&
322             RSMarshallingHelper::Unmarshalling(parcel, endValue_))) {
323         ROSEN_LOGE("RSRenderCurveAnimation::ParseParam, Unmarshalling Fail");
324         return false;
325     }
326 
327     std::shared_ptr<RSInterpolator> interpolator(RSInterpolator::Unmarshalling(parcel));
328     if (interpolator == nullptr) {
329         ROSEN_LOGE("RSRenderCurveAnimation::ParseParam, Unmarshalling interpolator failed");
330         return false;
331     }
332     SetInterpolator(interpolator);
333     return true;
334 }
335 
336 #ifdef ROSEN_OHOS
Marshalling(Parcel & parcel) const337 bool RSRenderInterpolatingSpringAnimation::Marshalling(Parcel& parcel) const
338 {
339     if (!RSRenderPropertyAnimation::Marshalling(parcel)) {
340         ROSEN_LOGE("RSRenderInterpolatingSpringAnimation::Marshalling, RenderPropertyAnimation failed");
341         return false;
342     }
343     if (!(RSMarshallingHelper::Marshalling(parcel, startValue_) &&
344             RSMarshallingHelper::Marshalling(parcel, endValue_))) {
345         ROSEN_LOGE("RSRenderInterpolatingSpringAnimation::Marshalling, MarshallingHelper failed");
346         return false;
347     }
348 
349     if (!(RSMarshallingHelper::Marshalling(parcel, response_) &&
350             RSMarshallingHelper::Marshalling(parcel, dampingRatio_) &&
351             RSMarshallingHelper::Marshalling(parcel, normalizedInitialVelocity_) &&
352             RSMarshallingHelper::Marshalling(parcel, minimumAmplitudeRatio_) &&
353             RSMarshallingHelper::Marshalling(parcel, needLogicallyFinishCallback_) &&
354             RSMarshallingHelper::Marshalling(parcel, zeroThreshold_))) {
355         ROSEN_LOGE("RSRenderInterpolatingSpringAnimation::Marshalling, invalid parametter failed");
356         return false;
357     }
358 
359     return true;
360 }
361 
Unmarshalling(Parcel & parcel)362 RSRenderInterpolatingSpringAnimation* RSRenderInterpolatingSpringAnimation::Unmarshalling(Parcel& parcel)
363 {
364     auto* renderInterpolatingSpringAnimation = new RSRenderInterpolatingSpringAnimation();
365     if (!renderInterpolatingSpringAnimation->ParseParam(parcel)) {
366         ROSEN_LOGE("RenderInterpolatingSpringAnimation::Unmarshalling, failed");
367         delete renderInterpolatingSpringAnimation;
368         renderInterpolatingSpringAnimation = nullptr;
369         return nullptr;
370     }
371     return renderInterpolatingSpringAnimation;
372 }
373 
ParseParam(Parcel & parcel)374 bool RSRenderInterpolatingSpringAnimation::ParseParam(Parcel& parcel)
375 {
376     if (!RSRenderPropertyAnimation::ParseParam(parcel)) {
377         ROSEN_LOGE("RSRenderInterpolatingSpringAnimation::ParseParam, ParseParam Fail");
378         return false;
379     }
380 
381     if (!(RSMarshallingHelper::Unmarshalling(parcel, startValue_) &&
382             RSMarshallingHelper::Unmarshalling(parcel, endValue_))) {
383         ROSEN_LOGE("RSRenderInterpolatingSpringAnimation::ParseParam, RSRenderPropertyBase Fail");
384         return false;
385     }
386 
387     if (!(RSMarshallingHelper::Unmarshalling(parcel, response_) &&
388             RSMarshallingHelper::Unmarshalling(parcel, dampingRatio_) &&
389             RSMarshallingHelper::Unmarshalling(parcel, normalizedInitialVelocity_) &&
390             RSMarshallingHelper::Unmarshalling(parcel, minimumAmplitudeRatio_) &&
391             RSMarshallingHelper::Unmarshalling(parcel, needLogicallyFinishCallback_) &&
392             RSMarshallingHelper::Unmarshalling(parcel, zeroThreshold_))) {
393         ROSEN_LOGE("RSRenderInterpolatingSpringAnimation::ParseParam, MarshallingHelper Fail");
394         return false;
395     }
396 
397     return true;
398 }
399 #endif
400 
Marshalling(Parcel & parcel) const401 bool RSRenderKeyframeAnimation::Marshalling(Parcel& parcel) const
402 {
403     if (!RSRenderPropertyAnimation::Marshalling(parcel)) {
404         ROSEN_LOGE("RSRenderKeyframeAnimation::Marshalling, RenderPropertyAnimation failed");
405         return false;
406     }
407     if (!parcel.WriteBool(isDurationKeyframe_)) {
408         ROSEN_LOGE("RSRenderKeyframeAnimation::Marshalling, isDurationKeyframe_ failed");
409         return false;
410     }
411     if (isDurationKeyframe_) {
412         uint32_t size = durationKeyframes_.size();
413         if (!parcel.WriteUint32(size)) {
414             ROSEN_LOGE("RSRenderKeyframeAnimation::Marshalling, Write durationkeyframe size failed");
415             return false;
416         }
417         for (const auto& [startFraction, endFraction, property, interpolator] : durationKeyframes_) {
418             if (!(parcel.WriteFloat(startFraction) && parcel.WriteFloat(endFraction) &&
419                     RSMarshallingHelper::Marshalling(parcel, property) &&
420                     interpolator != nullptr && interpolator->Marshalling(parcel))) {
421                 ROSEN_LOGE("RSRenderKeyframeAnimation::Marshalling, Write durationkeyframe value failed");
422                 return false;
423             }
424         }
425         return true;
426     }
427     uint32_t size = keyframes_.size();
428     if (!parcel.WriteUint32(size)) {
429         ROSEN_LOGE("RSRenderKeyframeAnimation::Marshalling, Write size failed");
430         return false;
431     }
432     for (const auto& [value, property, interpolator] : keyframes_) {
433         if (!(parcel.WriteFloat(value) && RSMarshallingHelper::Marshalling(parcel, property) &&
434                 interpolator != nullptr && interpolator->Marshalling(parcel))) {
435             ROSEN_LOGE("RSRenderKeyframeAnimation::Marshalling, Write value failed");
436             return false;
437         }
438     }
439     return true;
440 }
441 
Unmarshalling(Parcel & parcel)442 RSRenderKeyframeAnimation* RSRenderKeyframeAnimation::Unmarshalling(Parcel& parcel)
443 {
444     RSRenderKeyframeAnimation* renderKeyframeAnimation = new RSRenderKeyframeAnimation();
445     if (!renderKeyframeAnimation->ParseParam(parcel)) {
446         ROSEN_LOGE("RSRenderKeyframeAnimation::Unmarshalling, ParseParam failed");
447         delete renderKeyframeAnimation;
448         renderKeyframeAnimation = nullptr;
449         return nullptr;
450     }
451     return renderKeyframeAnimation;
452 }
453 
ParseParam(Parcel & parcel)454 bool RSRenderKeyframeAnimation::ParseParam(Parcel& parcel)
455 {
456     if (!RSRenderPropertyAnimation::ParseParam(parcel)) {
457         ROSEN_LOGE("RSRenderKeyframeAnimation::ParseParam, RenderPropertyAnimation fail");
458         return false;
459     }
460     if (!parcel.ReadBool(isDurationKeyframe_)) {
461         ROSEN_LOGE("RSRenderKeyframeAnimation::ParseParam, Parse isDurationKeyframe fail");
462         return false;
463     }
464     uint32_t size = 0;
465     if (!parcel.ReadUint32(size)) {
466         ROSEN_LOGE("RSRenderKeyframeAnimation::ParseParam, Parse Keyframes size fail");
467         return false;
468     }
469     if (size > MAX_KEYFRAME_SIZE_NUMBER) {
470         ROSEN_LOGE("RSRenderKeyframeAnimation::ParseParam, Keyframes size number is too large.");
471         return false;
472     }
473     keyframes_.clear();
474     durationKeyframes_.clear();
475     if (isDurationKeyframe_) {
476         return ParseDurationKeyframesParam(parcel, size);
477     }
478     float tupValue0 = 0.0;
479     for (uint32_t i = 0; i < size; i++) {
480         if (!(parcel.ReadFloat(tupValue0))) {
481             ROSEN_LOGE("RSRenderKeyframeAnimation::ParseParam, Unmarshalling value failed");
482             return false;
483         }
484         std::shared_ptr<RSRenderPropertyBase> tupValue1;
485         if (!RSMarshallingHelper::Unmarshalling(parcel, tupValue1)) {
486             return false;
487         }
488         std::shared_ptr<RSInterpolator> interpolator(RSInterpolator::Unmarshalling(parcel));
489         if (interpolator == nullptr) {
490             ROSEN_LOGE("RSRenderKeyframeAnimation::ParseParam, Unmarshalling interpolator failed");
491             return false;
492         }
493         keyframes_.emplace_back(std::make_tuple(tupValue0, tupValue1, interpolator));
494     }
495     return true;
496 }
497 
ParseDurationKeyframesParam(Parcel & parcel,int keyframeSize)498 bool RSRenderKeyframeAnimation::ParseDurationKeyframesParam(Parcel& parcel, int keyframeSize)
499 {
500     float startFraction = 0.0;
501     float endFraction = 0.0;
502     for (int i = 0; i < keyframeSize; i++) {
503         if (!(parcel.ReadFloat(startFraction)) || !(parcel.ReadFloat(endFraction))) {
504             ROSEN_LOGE("RSRenderKeyframeAnimation::ParseParam, Unmarshalling duration value failed");
505             return false;
506         }
507         std::shared_ptr<RSRenderPropertyBase> tupValue1;
508         if (!RSMarshallingHelper::Unmarshalling(parcel, tupValue1)) {
509             return false;
510         }
511         std::shared_ptr<RSInterpolator> interpolator(RSInterpolator::Unmarshalling(parcel));
512         if (interpolator == nullptr) {
513             ROSEN_LOGE("RSRenderKeyframeAnimation::ParseDurationParam, Unmarshalling interpolator failed");
514             return false;
515         }
516         durationKeyframes_.emplace_back(std::make_tuple(startFraction, endFraction, tupValue1, interpolator));
517     }
518     return true;
519 }
520 
Marshalling(Parcel & parcel) const521 bool RSRenderPathAnimation::Marshalling(Parcel& parcel) const
522 {
523     if (!RSRenderPropertyAnimation::Marshalling(parcel)) {
524         ROSEN_LOGE("RSRenderPathAnimation::Marshalling, RenderPropertyAnimation failed");
525         return false;
526     }
527     if (!(parcel.WriteFloat(originRotation_) && parcel.WriteFloat(beginFraction_) && parcel.WriteFloat(endFraction_) &&
528             RSMarshallingHelper::Marshalling(parcel, animationPath_) &&
529             parcel.WriteInt32(static_cast<std::underlying_type<RotationMode>::type>(rotationMode_)) &&
530             parcel.WriteBool(isNeedPath_) && parcel.WriteBool(needAddOrigin_) && interpolator_ != nullptr &&
531             interpolator_->Marshalling(parcel) && RSMarshallingHelper::Marshalling(parcel, startValue_) &&
532             RSMarshallingHelper::Marshalling(parcel, endValue_) && parcel.WriteUint64(rotationId_))) {
533         ROSEN_LOGE("RSRenderPathAnimation::Marshalling, write failed");
534         return false;
535     }
536     return true;
537 }
538 
Unmarshalling(Parcel & parcel)539 RSRenderPathAnimation* RSRenderPathAnimation::Unmarshalling(Parcel& parcel)
540 {
541     RSRenderPathAnimation* renderPathAnimation = new RSRenderPathAnimation();
542 
543     if (!renderPathAnimation->ParseParam(parcel)) {
544         ROSEN_LOGE("RSRenderPathAnimation::Unmarshalling, Parse RenderProperty Fail");
545         delete renderPathAnimation;
546         renderPathAnimation = nullptr;
547         return nullptr;
548     }
549     return renderPathAnimation;
550 }
551 
ParseParam(Parcel & parcel)552 bool RSRenderPathAnimation::ParseParam(Parcel& parcel)
553 {
554     if (!RSRenderPropertyAnimation::ParseParam(parcel)) {
555         ROSEN_LOGE("RSRenderPathAnimation::ParseParam, Parse RenderProperty Fail");
556         return false;
557     }
558 
559     int32_t rotationMode = 0;
560     bool isNeedPath = true;
561     if (!(parcel.ReadFloat(originRotation_) && parcel.ReadFloat(beginFraction_) &&
562             parcel.ReadFloat(endFraction_) && RSMarshallingHelper::Unmarshalling(parcel, animationPath_) &&
563             parcel.ReadInt32(rotationMode) && parcel.ReadBool(isNeedPath) && parcel.ReadBool(needAddOrigin_))) {
564         ROSEN_LOGE("RSRenderPathAnimation::ParseParam, Parse PathAnimation Failed");
565         return false;
566     }
567 
568     std::shared_ptr<RSInterpolator> interpolator(RSInterpolator::Unmarshalling(parcel));
569     if (interpolator == nullptr) {
570         ROSEN_LOGE("RSRenderPathAnimation::ParseParam, Unmarshalling interpolator failed");
571         return false;
572     }
573 
574     if (!(RSMarshallingHelper::Unmarshalling(parcel, startValue_) &&
575             RSMarshallingHelper::Unmarshalling(parcel, endValue_) &&
576             RSMarshallingHelper::UnmarshallingPidPlusId(parcel, rotationId_))) {
577         ROSEN_LOGE("RSRenderPathAnimation::ParseParam, Parse values failed");
578         return false;
579     }
580     SetInterpolator(interpolator);
581     SetRotationMode(static_cast<RotationMode>(rotationMode));
582     SetIsNeedPath(isNeedPath);
583     return true;
584 }
585 
Marshalling(Parcel & parcel) const586 bool RSRenderPropertyAnimation::Marshalling(Parcel& parcel) const
587 {
588     if (!RSRenderAnimation::Marshalling(parcel)) {
589         ROSEN_LOGE("RSRenderPropertyAnimation::Marshalling, RenderAnimation failed");
590         return false;
591     }
592     if (!parcel.WriteUint64(propertyId_)) {
593         ROSEN_LOGE("RSRenderPropertyAnimation::Marshalling, write PropertyId failed");
594         return false;
595     }
596     if (!(RSMarshallingHelper::Marshalling(parcel, isAdditive_) &&
597             RSMarshallingHelper::Marshalling(parcel, originValue_))) {
598         ROSEN_LOGE("RSRenderPropertyAnimation::Marshalling, write value failed");
599         return false;
600     }
601     return true;
602 }
603 
ParseParam(Parcel & parcel)604 bool RSRenderPropertyAnimation::ParseParam(Parcel& parcel)
605 {
606     if (!RSRenderAnimation::ParseParam(parcel)) {
607         ROSEN_LOGE("RSRenderPropertyAnimation::ParseParam, RenderAnimation failed");
608         return false;
609     }
610 
611     if (!(RSMarshallingHelper::UnmarshallingPidPlusId(parcel, propertyId_) &&
612             RSMarshallingHelper::Unmarshalling(parcel, isAdditive_))) {
613         ROSEN_LOGE("RSRenderPropertyAnimation::ParseParam, Unmarshalling failed");
614         return false;
615     }
616     if (!RSMarshallingHelper::Unmarshalling(parcel, originValue_)) {
617         return false;
618     }
619     if (originValue_ == nullptr) {
620         ROSEN_LOGE("RSRenderPropertyAnimation::ParseParam, originValue_ is nullptr!");
621         return false;
622     }
623     lastValue_ = originValue_->Clone();
624 
625     return true;
626 }
627 
628 #ifdef ROSEN_OHOS
Marshalling(Parcel & parcel) const629 bool RSRenderSpringAnimation::Marshalling(Parcel& parcel) const
630 {
631     if (!RSRenderPropertyAnimation::Marshalling(parcel)) {
632         ROSEN_LOGE("RSRenderSpringAnimation::Marshalling, RenderPropertyAnimation failed");
633         return false;
634     }
635     if (!(RSMarshallingHelper::Marshalling(parcel, startValue_) &&
636             RSMarshallingHelper::Marshalling(parcel, endValue_))) {
637         ROSEN_LOGE("RSRenderSpringAnimation::Marshalling, RSRenderPropertyBase failed");
638         return false;
639     }
640 
641     if (!(RSMarshallingHelper::Marshalling(parcel, response_) &&
642             RSMarshallingHelper::Marshalling(parcel, dampingRatio_) &&
643             RSMarshallingHelper::Marshalling(parcel, blendDuration_) &&
644             RSMarshallingHelper::Marshalling(parcel, minimumAmplitudeRatio_) &&
645             RSMarshallingHelper::Marshalling(parcel, needLogicallyFinishCallback_) &&
646             RSMarshallingHelper::Marshalling(parcel, zeroThreshold_))) {
647         ROSEN_LOGE("RSRenderSpringAnimation::Marshalling, MarshallingHelper failed");
648         return false;
649     }
650 
651     if (initialVelocity_) {
652         return RSMarshallingHelper::Marshalling(parcel, true) &&
653                RSMarshallingHelper::Marshalling(parcel, initialVelocity_);
654     }
655 
656     return RSMarshallingHelper::Marshalling(parcel, false);
657 }
658 
Unmarshalling(Parcel & parcel)659 RSRenderSpringAnimation* RSRenderSpringAnimation::Unmarshalling(Parcel& parcel)
660 {
661     auto* renderSpringAnimation = new RSRenderSpringAnimation();
662     if (!renderSpringAnimation->ParseParam(parcel)) {
663         ROSEN_LOGE("RSRenderSpringAnimation::Unmarshalling, failed");
664         delete renderSpringAnimation;
665         renderSpringAnimation = nullptr;
666         return nullptr;
667     }
668     return renderSpringAnimation;
669 }
670 
ParseParam(Parcel & parcel)671 bool RSRenderSpringAnimation::ParseParam(Parcel& parcel)
672 {
673     if (!RSRenderPropertyAnimation::ParseParam(parcel)) {
674         ROSEN_LOGE("RSRenderSpringAnimation::ParseParam, ParseParam Fail");
675         return false;
676     }
677 
678     if (!(RSMarshallingHelper::Unmarshalling(parcel, startValue_) &&
679             RSMarshallingHelper::Unmarshalling(parcel, endValue_))) {
680         ROSEN_LOGE("RSRenderSpringAnimation::ParseParam, RSRenderPropertyBase Fail");
681         return false;
682     }
683 
684     auto haveInitialVelocity = false;
685     if (!(RSMarshallingHelper::Unmarshalling(parcel, response_) &&
686             RSMarshallingHelper::Unmarshalling(parcel, dampingRatio_) &&
687             RSMarshallingHelper::Unmarshalling(parcel, blendDuration_) &&
688             RSMarshallingHelper::Unmarshalling(parcel, minimumAmplitudeRatio_) &&
689             RSMarshallingHelper::Unmarshalling(parcel, needLogicallyFinishCallback_) &&
690             RSMarshallingHelper::Unmarshalling(parcel, zeroThreshold_) &&
691             RSMarshallingHelper::Unmarshalling(parcel, haveInitialVelocity))) {
692         ROSEN_LOGE("RSRenderSpringAnimation::ParseParam, MarshallingHelper Fail");
693         return false;
694     }
695 
696     if (haveInitialVelocity) {
697         return RSMarshallingHelper::Unmarshalling(parcel, initialVelocity_);
698     }
699     return true;
700 }
701 #endif
702 
Unmarshalling(Parcel & parcel)703 RSRenderTransitionEffect* RSRenderTransitionEffect::Unmarshalling(Parcel& parcel)
704 {
705     uint16_t transitionType = 0;
706     if (!parcel.ReadUint16(transitionType)) {
707         ROSEN_LOGE("RSRenderTransitionEffect::Unmarshalling, ParseParam Failed");
708         return nullptr;
709     }
710     switch (transitionType) {
711         case RSTransitionEffectType::FADE:
712             return RSTransitionFade::Unmarshalling(parcel);
713         case RSTransitionEffectType::SCALE:
714             return RSTransitionScale::Unmarshalling(parcel);
715         case RSTransitionEffectType::TRANSLATE:
716             return RSTransitionTranslate::Unmarshalling(parcel);
717         case RSTransitionEffectType::ROTATE:
718             return RSTransitionRotate::Unmarshalling(parcel);
719         default:
720             return nullptr;
721     }
722 }
723 
Marshalling(Parcel & parcel) const724 bool RSTransitionFade::Marshalling(Parcel& parcel) const
725 {
726     bool flag = parcel.WriteUint16(RSTransitionEffectType::FADE) && parcel.WriteFloat(alpha_);
727     if (!flag) {
728         ROSEN_LOGE("RSTransitionFade::Marshalling, ParseParam Failed");
729     }
730     return flag;
731 }
732 
Unmarshalling(Parcel & parcel)733 RSRenderTransitionEffect* RSTransitionFade::Unmarshalling(Parcel& parcel)
734 {
735     float alpha;
736     if (!RSMarshallingHelper::Unmarshalling(parcel, alpha)) {
737         ROSEN_LOGE("RSTransitionFade::Unmarshalling, unmarshalling alpha failed");
738         return nullptr;
739     }
740     return new RSTransitionFade(alpha);
741 }
742 
Marshalling(Parcel & parcel) const743 bool RSTransitionScale::Marshalling(Parcel& parcel) const
744 {
745     bool flag = parcel.WriteUint16(RSTransitionEffectType::SCALE) && parcel.WriteFloat(scaleX_) &&
746            parcel.WriteFloat(scaleY_) && parcel.WriteFloat(scaleZ_);
747     if (!flag) {
748         ROSEN_LOGE("RSTransitionScale::Marshalling, ParseParam Failed");
749     }
750     return flag;
751 }
752 
Unmarshalling(Parcel & parcel)753 RSRenderTransitionEffect* RSTransitionScale::Unmarshalling(Parcel& parcel)
754 {
755     float scaleX = 0.0;
756     float scaleY = 0.0;
757     float scaleZ = 0.0;
758     if (!parcel.ReadFloat(scaleX) || !parcel.ReadFloat(scaleY) || !parcel.ReadFloat(scaleZ)) {
759         ROSEN_LOGE("RSTransitionScale::Unmarshalling, unmarshalling failed");
760         return nullptr;
761     }
762     return new RSTransitionScale(scaleX, scaleY, scaleZ);
763 }
764 
Marshalling(Parcel & parcel) const765 bool RSTransitionTranslate::Marshalling(Parcel& parcel) const
766 {
767     bool flag = parcel.WriteUint16(RSTransitionEffectType::TRANSLATE) && parcel.WriteFloat(translateX_) &&
768            parcel.WriteFloat(translateY_) && parcel.WriteFloat(translateZ_);
769     if (!flag) {
770         ROSEN_LOGE("RSTransitionTranslate::Marshalling, ParseParam failed");
771     }
772     return flag;
773 }
774 
Unmarshalling(Parcel & parcel)775 RSRenderTransitionEffect* RSTransitionTranslate::Unmarshalling(Parcel& parcel)
776 {
777     float translateX = 0.0;
778     float translateY = 0.0;
779     float translateZ = 0.0;
780     if (!parcel.ReadFloat(translateX) || !parcel.ReadFloat(translateY) || !parcel.ReadFloat(translateZ)) {
781         ROSEN_LOGE("RSTransitionTranslate::Unmarshalling, unmarshalling failed");
782         return nullptr;
783     }
784     return new RSTransitionTranslate(translateX, translateY, translateZ);
785 }
786 
Marshalling(Parcel & parcel) const787 bool RSTransitionRotate::Marshalling(Parcel& parcel) const
788 {
789     bool flag = parcel.WriteUint16(RSTransitionEffectType::ROTATE) && parcel.WriteFloat(dx_) &&
790                 parcel.WriteFloat(dy_) && parcel.WriteFloat(dz_) && parcel.WriteFloat(radian_);
791     if (!flag) {
792         ROSEN_LOGE("RSTransitionRotate::Marshalling, ParseParam failed");
793     }
794     return flag;
795 }
796 
Unmarshalling(Parcel & parcel)797 RSRenderTransitionEffect* RSTransitionRotate::Unmarshalling(Parcel& parcel)
798 {
799     Quaternion quaternion;
800     float dx = 0.0;
801     float dy = 0.0;
802     float dz = 0.0;
803     float radian = 0.0;
804     if (!parcel.ReadFloat(dx) || !parcel.ReadFloat(dy) || !parcel.ReadFloat(dz) || !parcel.ReadFloat(radian)) {
805         ROSEN_LOGE("RSTransitionRotate::Unmarshalling, unmarshalling failed");
806         return nullptr;
807     }
808     return new RSTransitionRotate(dx, dy, dz, radian);
809 }
810 
Marshalling(Parcel & parcel) const811 bool RSRenderTransition::Marshalling(Parcel& parcel) const
812 {
813     if (!RSRenderAnimation::Marshalling(parcel)) {
814         ROSEN_LOGE("RSRenderTransition::Marshalling, step1 failed");
815         return false;
816     }
817     if (!RSMarshallingHelper::Marshalling(parcel, effects_) ||
818         !RSMarshallingHelper::Marshalling(parcel, isTransitionIn_) || interpolator_ == nullptr ||
819         !interpolator_->Marshalling(parcel)) {
820         ROSEN_LOGE("RSRenderTransition::Marshalling, step2 failed");
821         return false;
822     }
823     return true;
824 }
825 
Unmarshalling(Parcel & parcel)826 RSRenderTransition* RSRenderTransition::Unmarshalling(Parcel& parcel)
827 {
828     RSRenderTransition* renderTransition = new RSRenderTransition();
829     if (!renderTransition->ParseParam(parcel)) {
830         ROSEN_LOGE("RSRenderTransition::Unmarshalling, ParseParam Failed");
831         delete renderTransition;
832         renderTransition = nullptr;
833         return nullptr;
834     }
835     return renderTransition;
836 }
837 
ParseParam(Parcel & parcel)838 bool RSRenderTransition::ParseParam(Parcel& parcel)
839 {
840     if (!RSRenderAnimation::ParseParam(parcel)) {
841         ROSEN_LOGE("RSRenderTransition::ParseParam, RenderAnimation failed");
842         return false;
843     }
844     if (!RSMarshallingHelper::Unmarshalling(parcel, effects_)) {
845         ROSEN_LOGE("RSRenderTransition::ParseParam, effect failed");
846         return false;
847     }
848     if (!RSMarshallingHelper::Unmarshalling(parcel, isTransitionIn_)) {
849         ROSEN_LOGE("RSRenderTransition::ParseParam, transition direction failed");
850         return false;
851     }
852     std::shared_ptr<RSInterpolator> interpolator(RSInterpolator::Unmarshalling(parcel));
853     if (interpolator == nullptr) {
854         ROSEN_LOGE("RSRenderTransition::ParseParam, interpolator is nullptr");
855         return false;
856     }
857     SetInterpolator(interpolator);
858     return true;
859 }
860 
Marshalling(Parcel & parcel) const861 bool RSSpringInterpolator::Marshalling(Parcel& parcel) const
862 {
863     if (!parcel.WriteUint16(InterpolatorType::SPRING)) {
864         ROSEN_LOGE("RSSpringInterpolator::Marshalling, Write type failed");
865         return false;
866     }
867     if (!parcel.WriteUint64(id_)) {
868         ROSEN_LOGE("RSSpringInterpolator::Marshalling, Write id failed");
869         return false;
870     }
871     if (!(parcel.WriteFloat(response_) && parcel.WriteFloat(dampingRatio_) && parcel.WriteFloat(initialVelocity_))) {
872         ROSEN_LOGE("RSSpringInterpolator::Marshalling, Write value failed");
873         return false;
874     }
875     return true;
876 }
877 
Unmarshalling(Parcel & parcel)878 RSSpringInterpolator* RSSpringInterpolator::Unmarshalling(Parcel& parcel)
879 {
880     uint64_t id{0};
881     if (!RSMarshallingHelper::UnmarshallingPidPlusIdNoChangeIfZero(parcel, id)) {
882         ROSEN_LOGE("RSSpringInterpolator::Unmarshalling Read id failed");
883         return nullptr;
884     }
885     float response = 0.0;
886     float dampingRatio = 0.0;
887     float initialVelocity = 0.0;
888     if (!(parcel.ReadFloat(response) && parcel.ReadFloat(dampingRatio) && parcel.ReadFloat(initialVelocity))) {
889         ROSEN_LOGE("RSSpringInterpolator::Unmarshalling, SpringInterpolator failed");
890         return nullptr;
891     }
892     auto ret = new RSSpringInterpolator(id, response, dampingRatio, initialVelocity);
893     return ret;
894 }
895 
Marshalling(Parcel & parcel) const896 bool RSStepsInterpolator::Marshalling(Parcel& parcel) const
897 {
898     if (!parcel.WriteUint16(InterpolatorType::STEPS)) {
899         ROSEN_LOGE("StepsInterpolator marshalling write type failed.");
900         return false;
901     }
902     if (!parcel.WriteUint64(id_)) {
903         ROSEN_LOGE("StepsInterpolator marshalling write id failed.");
904         return false;
905     }
906     if (!(parcel.WriteInt32(steps_) && parcel.WriteInt32(static_cast<int32_t>(position_)))) {
907         ROSEN_LOGE("StepsInterpolator marshalling write value failed.");
908         return false;
909     }
910     return true;
911 }
912 
Unmarshalling(Parcel & parcel)913 RSStepsInterpolator* RSStepsInterpolator::Unmarshalling(Parcel& parcel)
914 {
915     uint64_t id{0};
916     if (!RSMarshallingHelper::UnmarshallingPidPlusIdNoChangeIfZero(parcel, id)) {
917         ROSEN_LOGE("RSStepsInterpolator::Unmarshalling Read id failed");
918         return nullptr;
919     }
920     int32_t steps = 0;
921     int32_t position = 0;
922     if (!(parcel.ReadInt32(steps) && parcel.ReadInt32(position))) {
923         ROSEN_LOGE("StepsInterpolator unmarshalling failed.");
924         return nullptr;
925     }
926     return new RSStepsInterpolator(id, steps, static_cast<StepsCurvePosition>(position));
927 }
928 } // namespace Rosen
929 } // namespace OHOS
930