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 auto transactionProxy = RSTransactionProxy::GetInstance();
77 if (transactionProxy == nullptr) {
78 ROSEN_LOGE("Failed to start transition animation, transaction proxy is null!");
79 return;
80 }
81
82 std::vector<std::shared_ptr<RSRenderTransitionEffect>> transitionEffects;
83 if (isTransitionIn_) {
84 transitionEffects = effect_->transitionInEffects_;
85 } else {
86 transitionEffects = effect_->transitionOutEffects_;
87 }
88 auto transition = std::make_shared<RSRenderTransition>(GetId(), transitionEffects, isTransitionIn_);
89 auto interpolator = timingCurve_.GetInterpolator(GetDuration());
90 transition->SetInterpolator(interpolator);
91 UpdateParamToRenderAnimation(transition);
92
93 std::unique_ptr<RSCommand> command = std::make_unique<RSAnimationCreateTransition>(target->GetId(), transition);
94 transactionProxy->AddCommand(command, target->IsRenderServiceNode(), target->GetFollowType(), target->GetId());
95 if (target->NeedForcedSendToRemote()) {
96 std::unique_ptr<RSCommand> commandForRemote =
97 std::make_unique<RSAnimationCreateTransition>(target->GetId(), transition);
98 transactionProxy->AddCommand(commandForRemote, true, target->GetFollowType(), target->GetId());
99 }
100 }
101 } // namespace Rosen
102 } // namespace OHOS
103