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 {
25 namespace {
26 static constexpr int MAX_KEYFRAME_SIZE_NUMBER = 100000;
27 }
RSRenderKeyframeAnimation(AnimationId id,const PropertyId & propertyId,const std::shared_ptr<RSRenderPropertyBase> & originValue)28 RSRenderKeyframeAnimation::RSRenderKeyframeAnimation(AnimationId id, const PropertyId& propertyId,
29 const std::shared_ptr<RSRenderPropertyBase>& originValue)
30 : RSRenderPropertyAnimation(id, propertyId, originValue)
31 {}
32
DumpAnimationType(std::string & out) const33 void RSRenderKeyframeAnimation::DumpAnimationType(std::string& out) const
34 {
35 out += "Type:RSRenderKeyframeAnimation";
36 }
37
AddKeyframe(float fraction,const std::shared_ptr<RSRenderPropertyBase> & value,const std::shared_ptr<RSInterpolator> & interpolator)38 void RSRenderKeyframeAnimation::AddKeyframe(float fraction, const std::shared_ptr<RSRenderPropertyBase>& value,
39 const std::shared_ptr<RSInterpolator>& interpolator)
40 {
41 if (fraction < FRACTION_MIN || fraction > FRACTION_MAX) {
42 ROSEN_LOGE("Failed to add key frame, fraction is invalid!");
43 return;
44 }
45
46 if (IsStarted()) {
47 ROSEN_LOGE("Failed to add key frame, animation has started!");
48 return;
49 }
50
51 keyframes_.push_back({ fraction, value, interpolator });
52 }
53
AddKeyframes(const std::vector<std::tuple<float,std::shared_ptr<RSRenderPropertyBase>,std::shared_ptr<RSInterpolator>>> & keyframes)54 void RSRenderKeyframeAnimation::AddKeyframes(const std::vector<std::tuple<float, std::shared_ptr<RSRenderPropertyBase>,
55 std::shared_ptr<RSInterpolator>>>& keyframes)
56 {
57 if (IsStarted()) {
58 ROSEN_LOGE("Failed to add key frame, animation has started!");
59 return;
60 }
61
62 keyframes_ = keyframes;
63 }
64
AddKeyframe(int startDuration,int endDuration,const std::shared_ptr<RSRenderPropertyBase> & value,const std::shared_ptr<RSInterpolator> & interpolator)65 void RSRenderKeyframeAnimation::AddKeyframe(int startDuration, int endDuration,
66 const std::shared_ptr<RSRenderPropertyBase>& value, const std::shared_ptr<RSInterpolator>& interpolator)
67 {
68 if (startDuration > endDuration) {
69 ROSEN_LOGE("Failed to add key frame, startDuration larger than endDuration!");
70 return;
71 }
72
73 if (IsStarted()) {
74 ROSEN_LOGE("Failed to add key frame, animation has started!");
75 return;
76 }
77
78 if (GetDuration() <= 0) {
79 ROSEN_LOGE("Failed to add key frame, total duration is 0!");
80 return;
81 }
82
83 durationKeyframes_.push_back(
84 { startDuration / (float)GetDuration(), endDuration / (float)GetDuration(), value, interpolator });
85 }
86
SetDurationKeyframe(bool isDuration)87 void RSRenderKeyframeAnimation::SetDurationKeyframe(bool isDuration)
88 {
89 isDurationKeyframe_ = isDuration;
90 }
91
Marshalling(Parcel & parcel) const92 bool RSRenderKeyframeAnimation::Marshalling(Parcel& parcel) const
93 {
94 if (!RSRenderPropertyAnimation::Marshalling(parcel)) {
95 ROSEN_LOGE("RSRenderKeyframeAnimation::Marshalling, RenderPropertyAnimation failed");
96 return false;
97 }
98 if (!parcel.WriteBool(isDurationKeyframe_)) {
99 ROSEN_LOGE("RSRenderKeyframeAnimation::Marshalling, isDurationKeyframe_ failed");
100 return false;
101 }
102 if (isDurationKeyframe_) {
103 uint32_t size = durationKeyframes_.size();
104 if (!parcel.WriteUint32(size)) {
105 ROSEN_LOGE("RSRenderKeyframeAnimation::Marshalling, Write durationkeyframe size failed");
106 return false;
107 }
108 for (const auto& [startFraction, endFraction, property, interpolator] : durationKeyframes_) {
109 if (!(parcel.WriteFloat(startFraction) && parcel.WriteFloat(endFraction) &&
110 RSRenderPropertyBase::Marshalling(parcel, property) &&
111 interpolator != nullptr && interpolator->Marshalling(parcel))) {
112 ROSEN_LOGE("RSRenderKeyframeAnimation::Marshalling, Write durationkeyframe value failed");
113 return false;
114 }
115 }
116 return true;
117 }
118 uint32_t size = keyframes_.size();
119 if (!parcel.WriteUint32(size)) {
120 ROSEN_LOGE("RSRenderKeyframeAnimation::Marshalling, Write size failed");
121 return false;
122 }
123 for (const auto& [value, property, interpolator] : keyframes_) {
124 if (!(parcel.WriteFloat(value) && RSRenderPropertyBase::Marshalling(parcel, property) &&
125 interpolator != nullptr && interpolator->Marshalling(parcel))) {
126 ROSEN_LOGE("RSRenderKeyframeAnimation::Marshalling, Write value failed");
127 return false;
128 }
129 }
130 return true;
131 }
132
Unmarshalling(Parcel & parcel)133 RSRenderKeyframeAnimation* RSRenderKeyframeAnimation::Unmarshalling(Parcel& parcel)
134 {
135 RSRenderKeyframeAnimation* renderKeyframeAnimation = new RSRenderKeyframeAnimation();
136 if (!renderKeyframeAnimation->ParseParam(parcel)) {
137 ROSEN_LOGE("RSRenderKeyframeAnimation::Unmarshalling, ParseParam failed");
138 delete renderKeyframeAnimation;
139 return nullptr;
140 }
141 return renderKeyframeAnimation;
142 }
143
ParseParam(Parcel & parcel)144 bool RSRenderKeyframeAnimation::ParseParam(Parcel& parcel)
145 {
146 if (!RSRenderPropertyAnimation::ParseParam(parcel)) {
147 ROSEN_LOGE("RSRenderKeyframeAnimation::ParseParam, RenderPropertyAnimation fail");
148 return false;
149 }
150 if (!parcel.ReadBool(isDurationKeyframe_)) {
151 ROSEN_LOGE("RSRenderKeyframeAnimation::ParseParam, Parse isDurationKeyframe fail");
152 return false;
153 }
154 uint32_t size = 0;
155 if (!parcel.ReadUint32(size)) {
156 ROSEN_LOGE("RSRenderKeyframeAnimation::ParseParam, Parse Keyframes size fail");
157 return false;
158 }
159 if (size > MAX_KEYFRAME_SIZE_NUMBER) {
160 ROSEN_LOGE("RSRenderKeyframeAnimation::ParseParam, Keyframes size number is too large.");
161 return false;
162 }
163 keyframes_.clear();
164 durationKeyframes_.clear();
165 if (isDurationKeyframe_) {
166 return ParseDurationKeyframesParam(parcel, size);
167 }
168 float tupValue0 = 0.0;
169 for (uint32_t i = 0; i < size; i++) {
170 if (!(parcel.ReadFloat(tupValue0))) {
171 ROSEN_LOGE("RSRenderKeyframeAnimation::ParseParam, Unmarshalling value failed");
172 return false;
173 }
174 std::shared_ptr<RSRenderPropertyBase> tupValue1;
175 if (!RSRenderPropertyBase::Unmarshalling(parcel, tupValue1)) {
176 return false;
177 }
178 std::shared_ptr<RSInterpolator> interpolator(RSInterpolator::Unmarshalling(parcel));
179 if (interpolator == nullptr) {
180 ROSEN_LOGE("RSRenderKeyframeAnimation::ParseParam, Unmarshalling interpolator failed");
181 return false;
182 }
183 keyframes_.emplace_back(std::make_tuple(tupValue0, tupValue1, interpolator));
184 }
185 return true;
186 }
187
ParseDurationKeyframesParam(Parcel & parcel,int keyframeSize)188 bool RSRenderKeyframeAnimation::ParseDurationKeyframesParam(Parcel& parcel, int keyframeSize)
189 {
190 float startFraction = 0.0;
191 float endFraction = 0.0;
192 for (int i = 0; i < keyframeSize; i++) {
193 if (!(parcel.ReadFloat(startFraction)) || !(parcel.ReadFloat(endFraction))) {
194 ROSEN_LOGE("RSRenderKeyframeAnimation::ParseParam, Unmarshalling duration value failed");
195 return false;
196 }
197 std::shared_ptr<RSRenderPropertyBase> tupValue1;
198 if (!RSRenderPropertyBase::Unmarshalling(parcel, tupValue1)) {
199 return false;
200 }
201 std::shared_ptr<RSInterpolator> interpolator(RSInterpolator::Unmarshalling(parcel));
202 if (interpolator == nullptr) {
203 ROSEN_LOGE("RSRenderKeyframeAnimation::ParseDurationParam, Unmarshalling interpolator failed");
204 return false;
205 }
206 durationKeyframes_.emplace_back(std::make_tuple(startFraction, endFraction, tupValue1, interpolator));
207 }
208 return true;
209 }
210
OnAnimate(float fraction)211 void RSRenderKeyframeAnimation::OnAnimate(float fraction)
212 {
213 if (keyframes_.empty() && durationKeyframes_.empty()) {
214 ROSEN_LOGE("Failed to animate key frame, keyframes is empty!");
215 return;
216 }
217
218 if (valueEstimator_ == nullptr) {
219 return;
220 }
221 valueEstimator_->UpdateAnimationValue(fraction, GetAdditive());
222 }
223
InitValueEstimator()224 void RSRenderKeyframeAnimation::InitValueEstimator()
225 {
226 if (valueEstimator_ == nullptr) {
227 valueEstimator_ = property_->CreateRSValueEstimator(RSValueEstimatorType::KEYFRAME_VALUE_ESTIMATOR);
228 }
229 if (valueEstimator_ == nullptr) {
230 ROSEN_LOGE("RSRenderKeyframeAnimation::InitValueEstimator, valueEstimator_ is nullptr.");
231 return;
232 }
233
234 if (isDurationKeyframe_) {
235 valueEstimator_->InitDurationKeyframeAnimationValue(property_, durationKeyframes_, lastValue_);
236 } else {
237 valueEstimator_->InitKeyframeAnimationValue(property_, keyframes_, lastValue_);
238 }
239 }
240 } // namespace Rosen
241 } // namespace OHOS
242