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_curve_animation.h"
17
18 #include "animation/rs_animation_common.h"
19 #include "animation/rs_render_curve_animation.h"
20 #include "command/rs_animation_command.h"
21 #include "platform/common/rs_log.h"
22 #include "transaction/rs_transaction_proxy.h"
23 #include "ui/rs_node.h"
24 #include "modifier/rs_property.h"
25
26 namespace OHOS {
27 namespace Rosen {
RSCurveAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & byValue)28 RSCurveAnimation::RSCurveAnimation(std::shared_ptr<RSPropertyBase> property,
29 const std::shared_ptr<RSPropertyBase>& byValue) : RSPropertyAnimation(property)
30 {
31 isDelta_ = true;
32 byValue_ = byValue;
33 }
34
RSCurveAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue)35 RSCurveAnimation::RSCurveAnimation(std::shared_ptr<RSPropertyBase> property,
36 const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue)
37 : RSPropertyAnimation(property)
38 {
39 isDelta_ = false;
40 startValue_ = startValue;
41 endValue_ = endValue;
42 }
43
SetTimingCurve(const RSAnimationTimingCurve & timingCurve)44 void RSCurveAnimation::SetTimingCurve(const RSAnimationTimingCurve& timingCurve)
45 {
46 if (timingCurve.type_ != RSAnimationTimingCurve::CurveType::INTERPOLATING) {
47 ROSEN_LOGE("RSCurveAnimation::SetTimingCurve: invalid timing curve type");
48 return;
49 }
50 timingCurve_ = timingCurve;
51 }
52
GetTimingCurve() const53 const RSAnimationTimingCurve& RSCurveAnimation::GetTimingCurve() const
54 {
55 return timingCurve_;
56 }
57
StartRenderAnimation(const std::shared_ptr<RSRenderCurveAnimation> & animation)58 void RSCurveAnimation::StartRenderAnimation(const std::shared_ptr<RSRenderCurveAnimation>& animation)
59 {
60 auto target = GetTarget().lock();
61 if (target == nullptr) {
62 ROSEN_LOGE("Failed to start curve animation, target is null!");
63 return;
64 }
65
66 std::unique_ptr<RSCommand> command = std::make_unique<RSAnimationCreateCurve>(target->GetId(), animation);
67 auto transactionProxy = RSTransactionProxy::GetInstance();
68 if (transactionProxy != nullptr) {
69 transactionProxy->AddCommand(command, target->IsRenderServiceNode(), target->GetFollowType(), target->GetId());
70 if (target->NeedSendExtraCommand()) {
71 std::unique_ptr<RSCommand> extraCommand =
72 std::make_unique<RSAnimationCreateCurve>(target->GetId(), animation);
73 transactionProxy->AddCommand(extraCommand,
74 !target->IsRenderServiceNode(), target->GetFollowType(), target->GetId());
75 }
76 }
77 }
78
StartUIAnimation(const std::shared_ptr<RSRenderCurveAnimation> & animation)79 void RSCurveAnimation::StartUIAnimation(const std::shared_ptr<RSRenderCurveAnimation>& animation)
80 {
81 StartCustomPropertyAnimation(animation);
82 }
83
OnStart()84 void RSCurveAnimation::OnStart()
85 {
86 RSPropertyAnimation::OnStart();
87 auto interpolator = timingCurve_.GetInterpolator(GetDuration());
88 auto animation = std::make_shared<RSRenderCurveAnimation>(GetId(), GetPropertyId(),
89 originValue_->CreateRenderProperty(), startValue_->CreateRenderProperty(), endValue_->CreateRenderProperty());
90 animation->SetInterpolator(interpolator);
91 animation->SetAdditive(GetAdditive());
92 UpdateParamToRenderAnimation(animation);
93 if (isCustom_) {
94 StartUIAnimation(animation);
95 } else {
96 StartRenderAnimation(animation);
97 }
98 }
99 } // namespace Rosen
100 } // namespace OHOS
101