• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_keyframe_animation.h"
17 
18 #include "animation/rs_interpolator.h"
19 #include "animation/rs_value_estimator.h"
20 #include "platform/common/rs_log.h"
21 #include "transaction/rs_marshalling_helper.h"
22 
23 namespace OHOS {
24 namespace Rosen {
RSRenderKeyframeAnimation(AnimationId id,const PropertyId & propertyId,const std::shared_ptr<RSRenderPropertyBase> & originValue)25 RSRenderKeyframeAnimation::RSRenderKeyframeAnimation(AnimationId id, const PropertyId& propertyId,
26     const std::shared_ptr<RSRenderPropertyBase>& originValue)
27     : RSRenderPropertyAnimation(id, propertyId, originValue)
28 {}
29 
DumpAnimationInfo(std::string & out) const30 void RSRenderKeyframeAnimation::DumpAnimationInfo(std::string& out) const
31 {
32     out.append("Type:RSRenderKeyframeAnimation");
33     DumpProperty(out);
34 }
35 
AddKeyframe(float fraction,const std::shared_ptr<RSRenderPropertyBase> & value,const std::shared_ptr<RSInterpolator> & interpolator)36 void RSRenderKeyframeAnimation::AddKeyframe(float fraction, const std::shared_ptr<RSRenderPropertyBase>& value,
37     const std::shared_ptr<RSInterpolator>& interpolator)
38 {
39     if (fraction < FRACTION_MIN || fraction > FRACTION_MAX) {
40         ROSEN_LOGE("Failed to add key frame, fraction is invalid!");
41         return;
42     }
43 
44     if (IsStarted()) {
45         ROSEN_LOGE("Failed to add key frame, animation has started!");
46         return;
47     }
48 
49     keyframes_.push_back({ fraction, value, interpolator });
50 }
51 
AddKeyframes(const std::vector<std::tuple<float,std::shared_ptr<RSRenderPropertyBase>,std::shared_ptr<RSInterpolator>>> & keyframes)52 void RSRenderKeyframeAnimation::AddKeyframes(const std::vector<std::tuple<float, std::shared_ptr<RSRenderPropertyBase>,
53     std::shared_ptr<RSInterpolator>>>& keyframes)
54 {
55     if (IsStarted()) {
56         ROSEN_LOGE("Failed to add key frame, animation has started!");
57         return;
58     }
59 
60     keyframes_ = keyframes;
61 }
62 
AddKeyframe(int startDuration,int endDuration,const std::shared_ptr<RSRenderPropertyBase> & value,const std::shared_ptr<RSInterpolator> & interpolator)63 void RSRenderKeyframeAnimation::AddKeyframe(int startDuration, int endDuration,
64     const std::shared_ptr<RSRenderPropertyBase>& value, const std::shared_ptr<RSInterpolator>& interpolator)
65 {
66     if (startDuration > endDuration) {
67         ROSEN_LOGE("Failed to add key frame, startDuration larger than endDuration!");
68         return;
69     }
70 
71     if (IsStarted()) {
72         ROSEN_LOGE("Failed to add key frame, animation has started!");
73         return;
74     }
75 
76     if (GetDuration() <= 0) {
77         ROSEN_LOGE("Failed to add key frame, total duration is 0!");
78         return;
79     }
80 
81     durationKeyframes_.push_back(
82         { startDuration / (float)GetDuration(), endDuration / (float)GetDuration(), value, interpolator });
83 }
84 
SetDurationKeyframe(bool isDuration)85 void RSRenderKeyframeAnimation::SetDurationKeyframe(bool isDuration)
86 {
87     isDurationKeyframe_ = isDuration;
88 }
89 
OnAnimate(float fraction)90 void RSRenderKeyframeAnimation::OnAnimate(float fraction)
91 {
92     if (keyframes_.empty() && durationKeyframes_.empty()) {
93         ROSEN_LOGE("Failed to animate key frame, keyframes is empty!");
94         return;
95     }
96 
97     if (valueEstimator_ == nullptr) {
98         return;
99     }
100     valueEstimator_->UpdateAnimationValue(fraction, GetAdditive());
101 }
102 
InitValueEstimator()103 void RSRenderKeyframeAnimation::InitValueEstimator()
104 {
105     if (valueEstimator_ == nullptr) {
106         valueEstimator_ = property_->CreateRSValueEstimator(RSValueEstimatorType::KEYFRAME_VALUE_ESTIMATOR);
107     }
108     if (valueEstimator_ == nullptr) {
109         ROSEN_LOGE("RSRenderKeyframeAnimation::InitValueEstimator, valueEstimator_ is nullptr.");
110         return;
111     }
112 
113     if (isDurationKeyframe_) {
114         valueEstimator_->InitDurationKeyframeAnimationValue(property_, durationKeyframes_, lastValue_);
115     } else {
116         valueEstimator_->InitKeyframeAnimationValue(property_, keyframes_, lastValue_);
117     }
118 }
119 } // namespace Rosen
120 } // namespace OHOS
121