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