1 /*
2 * Copyright (c) 2021-2022 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_transition.h"
17
18 #include "animation/rs_render_transition.h"
19 #include "command/rs_animation_command.h"
20 #include "modifier/rs_property.h"
21 #include "transaction/rs_transaction_proxy.h"
22 #include "ui/rs_node.h"
23 #include "platform/common/rs_log.h"
24
25 namespace OHOS {
26 namespace Rosen {
RSTransition(const std::shared_ptr<const RSTransitionEffect> & effect,bool isTransitionIn)27 RSTransition::RSTransition(const std::shared_ptr<const RSTransitionEffect>& effect, bool isTransitionIn)
28 : isTransitionIn_(isTransitionIn), effect_(effect)
29 {}
30
OnStart()31 void RSTransition::OnStart()
32 {
33 if (isCustom_) {
34 StartCustomTransition();
35 }
36 StartRenderTransition();
37 }
38
OnUpdateStagingValue(bool isFirstStart)39 void RSTransition::OnUpdateStagingValue(bool isFirstStart)
40 {
41 if (!isCustom_) {
42 return;
43 }
44 auto& customEffects = isTransitionIn_ ? effect_->customTransitionInEffects_ : effect_->customTransitionOutEffects_;
45 for (auto& customEffect : customEffects) {
46 for (auto& [property, endValue] : customEffect->properties_) {
47 property->SetValue(endValue);
48 }
49 customEffect->properties_.clear();
50 customEffect->customTransitionEffects_.clear();
51 }
52 }
53
StartCustomTransition()54 void RSTransition::StartCustomTransition()
55 {
56 std::vector<std::shared_ptr<RSRenderTransitionEffect>> transitionEffects;
57 auto& customEffects = isTransitionIn_ ? effect_->customTransitionInEffects_ : effect_->customTransitionOutEffects_;
58 for (auto& customEffect : customEffects) {
59 transitionEffects.insert(transitionEffects.end(), customEffect->customTransitionEffects_.begin(),
60 customEffect->customTransitionEffects_.end());
61 }
62 auto transition = std::make_shared<RSRenderTransition>(GetId(), transitionEffects, isTransitionIn_);
63 auto interpolator = timingCurve_.GetInterpolator(GetDuration());
64 transition->SetInterpolator(interpolator);
65 UpdateParamToRenderAnimation(transition);
66 StartCustomAnimation(transition);
67 }
68
StartRenderTransition()69 void RSTransition::StartRenderTransition()
70 {
71 auto target = GetTarget().lock();
72 if (target == nullptr) {
73 ROSEN_LOGE("Failed to start transition animation, target is null!");
74 return;
75 }
76
77 std::vector<std::shared_ptr<RSRenderTransitionEffect>> transitionEffects;
78 if (isTransitionIn_) {
79 transitionEffects = effect_->transitionInEffects_;
80 } else {
81 transitionEffects = effect_->transitionOutEffects_;
82 }
83 auto transition = std::make_shared<RSRenderTransition>(GetId(), transitionEffects, isTransitionIn_);
84 auto interpolator = timingCurve_.GetInterpolator(GetDuration());
85 transition->SetInterpolator(interpolator);
86 UpdateParamToRenderAnimation(transition);
87
88 std::unique_ptr<RSCommand> command =
89 std::make_unique<RSAnimationCreateTransition>(target->GetId(), transition);
90 target->AddCommand(command, target->IsRenderServiceNode(), target->GetFollowType(), target->GetId());
91 if (target->NeedForcedSendToRemote()) {
92 std::unique_ptr<RSCommand> commandForRemote =
93 std::make_unique<RSAnimationCreateTransition>(target->GetId(), transition);
94 target->AddCommand(commandForRemote, true, target->GetFollowType(), target->GetId());
95 }
96 }
97 } // namespace Rosen
98 } // namespace OHOS
99