• 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 += "Type:RSRenderKeyframeAnimation";
33     RSRenderPropertyType type = RSRenderPropertyType::INVALID;
34     if (property_ != nullptr) {
35         type = property_->GetPropertyType();
36         out += ", ModifierType: " + std::to_string(static_cast<int16_t>(property_->GetModifierType()));
37     } else {
38         out += ", ModifierType: INVALID";
39     }
40 }
41 
AddKeyframe(float fraction,const std::shared_ptr<RSRenderPropertyBase> & value,const std::shared_ptr<RSInterpolator> & interpolator)42 void RSRenderKeyframeAnimation::AddKeyframe(float fraction, const std::shared_ptr<RSRenderPropertyBase>& value,
43     const std::shared_ptr<RSInterpolator>& interpolator)
44 {
45     if (fraction < FRACTION_MIN || fraction > FRACTION_MAX) {
46         ROSEN_LOGE("Failed to add key frame, fraction is invalid!");
47         return;
48     }
49 
50     if (IsStarted()) {
51         ROSEN_LOGE("Failed to add key frame, animation has started!");
52         return;
53     }
54 
55     keyframes_.push_back({ fraction, value, interpolator });
56 }
57 
AddKeyframes(const std::vector<std::tuple<float,std::shared_ptr<RSRenderPropertyBase>,std::shared_ptr<RSInterpolator>>> & keyframes)58 void RSRenderKeyframeAnimation::AddKeyframes(const std::vector<std::tuple<float, std::shared_ptr<RSRenderPropertyBase>,
59     std::shared_ptr<RSInterpolator>>>& keyframes)
60 {
61     if (IsStarted()) {
62         ROSEN_LOGE("Failed to add key frame, animation has started!");
63         return;
64     }
65 
66     keyframes_ = keyframes;
67 }
68 
AddKeyframe(int startDuration,int endDuration,const std::shared_ptr<RSRenderPropertyBase> & value,const std::shared_ptr<RSInterpolator> & interpolator)69 void RSRenderKeyframeAnimation::AddKeyframe(int startDuration, int endDuration,
70     const std::shared_ptr<RSRenderPropertyBase>& value, const std::shared_ptr<RSInterpolator>& interpolator)
71 {
72     if (startDuration > endDuration) {
73         ROSEN_LOGE("Failed to add key frame, startDuration larger than endDuration!");
74         return;
75     }
76 
77     if (IsStarted()) {
78         ROSEN_LOGE("Failed to add key frame, animation has started!");
79         return;
80     }
81 
82     if (GetDuration() <= 0) {
83         ROSEN_LOGE("Failed to add key frame, total duration is 0!");
84         return;
85     }
86 
87     durationKeyframes_.push_back(
88         { startDuration / (float)GetDuration(), endDuration / (float)GetDuration(), value, interpolator });
89 }
90 
SetDurationKeyframe(bool isDuration)91 void RSRenderKeyframeAnimation::SetDurationKeyframe(bool isDuration)
92 {
93     isDurationKeyframe_ = isDuration;
94 }
95 
OnAnimate(float fraction)96 void RSRenderKeyframeAnimation::OnAnimate(float fraction)
97 {
98     if (keyframes_.empty() && durationKeyframes_.empty()) {
99         ROSEN_LOGE("Failed to animate key frame, keyframes is empty!");
100         return;
101     }
102 
103     if (valueEstimator_ == nullptr) {
104         return;
105     }
106     valueEstimator_->UpdateAnimationValue(fraction, GetAdditive());
107 }
108 
InitValueEstimator()109 void RSRenderKeyframeAnimation::InitValueEstimator()
110 {
111     if (valueEstimator_ == nullptr) {
112         valueEstimator_ = property_->CreateRSValueEstimator(RSValueEstimatorType::KEYFRAME_VALUE_ESTIMATOR);
113     }
114     if (valueEstimator_ == nullptr) {
115         ROSEN_LOGE("RSRenderKeyframeAnimation::InitValueEstimator, valueEstimator_ is nullptr.");
116         return;
117     }
118 
119     if (isDurationKeyframe_) {
120         valueEstimator_->InitDurationKeyframeAnimationValue(property_, durationKeyframes_, lastValue_);
121     } else {
122         valueEstimator_->InitKeyframeAnimationValue(property_, keyframes_, lastValue_);
123     }
124 }
125 } // namespace Rosen
126 } // namespace OHOS
127