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 "animation/rs_render_transition.h"
17
18 #include "animation/rs_transition_effect.h"
19 #include "pipeline/rs_render_node.h"
20 #include "platform/common/rs_log.h"
21 #include "transaction/rs_marshalling_helper.h"
22
23 namespace OHOS {
24 namespace Rosen {
RSRenderTransition(AnimationId id,const std::shared_ptr<const RSTransitionEffect> & effect,bool isTransitionIn)25 RSRenderTransition::RSRenderTransition(
26 AnimationId id, const std::shared_ptr<const RSTransitionEffect>& effect, bool isTransitionIn)
27 : RSRenderAnimation(id), isTransitionIn_(isTransitionIn)
28 {
29 if (isTransitionIn) {
30 effects_ = effect->GetTransitionInEffects();
31 } else {
32 effects_ = effect->GetTransitionOutEffects();
33 }
34 }
35
Marshalling(Parcel & parcel) const36 bool RSRenderTransition::Marshalling(Parcel& parcel) const
37 {
38 if (!RSRenderAnimation::Marshalling(parcel)) {
39 ROSEN_LOGE("RSRenderTransition::Marshalling, step1 failed");
40 return false;
41 }
42 if (!RSMarshallingHelper::Marshalling(parcel, effects_) ||
43 !RSMarshallingHelper::Marshalling(parcel, isTransitionIn_) || interpolator_ == nullptr ||
44 !interpolator_->Marshalling(parcel)) {
45 ROSEN_LOGE("RSRenderTransition::Marshalling, step2 failed");
46 return false;
47 }
48 return true;
49 }
50
Unmarshalling(Parcel & parcel)51 RSRenderTransition* RSRenderTransition::Unmarshalling(Parcel& parcel)
52 {
53 RSRenderTransition* renderTransition = new RSRenderTransition();
54 if (!renderTransition->ParseParam(parcel)) {
55 ROSEN_LOGE("RSRenderTransition::Unmarshalling, ParseParam Failed");
56 delete renderTransition;
57 return nullptr;
58 }
59 return renderTransition;
60 }
61
ParseParam(Parcel & parcel)62 bool RSRenderTransition::ParseParam(Parcel& parcel)
63 {
64 if (!RSRenderAnimation::ParseParam(parcel)) {
65 ROSEN_LOGE("RSRenderTransition::ParseParam, RenderAnimation failed");
66 return false;
67 }
68 if (!RSMarshallingHelper::Unmarshalling(parcel, effects_)) {
69 ROSEN_LOGE("RSRenderTransition::ParseParam, effect failed");
70 return false;
71 }
72 if (!RSMarshallingHelper::Unmarshalling(parcel, isTransitionIn_)) {
73 ROSEN_LOGE("RSRenderTransition::ParseParam, transition direction failed");
74 return false;
75 }
76 std::shared_ptr<RSInterpolator> interpolator(RSInterpolator::Unmarshalling(parcel));
77 if (interpolator == nullptr) {
78 ROSEN_LOGE("RSRenderTransition::ParseParam, interpolator is nullptr");
79 return false;
80 }
81 SetInterpolator(interpolator);
82 return true;
83 }
OnAnimate(float fraction)84 void RSRenderTransition::OnAnimate(float fraction)
85 {
86 float valueFraction = interpolator_->Interpolate(fraction);
87 if (isTransitionIn_) {
88 valueFraction = 1 - valueFraction;
89 }
90 for (auto& effect : effects_) {
91 effect->UpdateFraction(valueFraction);
92 }
93 }
94
OnAttach()95 void RSRenderTransition::OnAttach()
96 {
97 auto target = GetTarget();
98 if (target == nullptr) {
99 ROSEN_LOGE("RSRenderTransition::OnAttach, target is nullptr");
100 return;
101 }
102 // create "transition" modifier and add it to target
103 for (auto& effect : effects_) {
104 target->AddModifier(effect->GetModifier());
105 }
106 // update number of disappearing transition animation
107 if (!isTransitionIn_) {
108 target->disappearingTransitionCount_++;
109 ROSEN_LOGD("RSRenderTransition::OnAttach, target have %u disappearing Transitions",
110 target->disappearingTransitionCount_);
111 }
112 }
113
OnDetach()114 void RSRenderTransition::OnDetach()
115 {
116 auto target = GetTarget();
117 if (target == nullptr) {
118 ROSEN_LOGE("RSRenderTransition::OnDetach, target is nullptr");
119 return;
120 }
121 // remove "transition" modifier from target
122 for (auto& effect : effects_) {
123 target->RemoveModifier(effect->GetModifier()->GetPropertyId());
124 }
125 // update number of disappearing transition animation
126 if (!isTransitionIn_) {
127 target->disappearingTransitionCount_--;
128 ROSEN_LOGD("RSRenderTransition::OnDetach, target have %u disappearing Transitions",
129 target->disappearingTransitionCount_);
130 if (target->disappearingTransitionCount_ == 0) {
131 target->InternalRemoveSelfFromDisappearingChildren();
132 }
133 }
134 }
135 } // namespace Rosen
136 } // namespace OHOS
137