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