1 /*
2 * Copyright (c) 2021-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_animation_fraction.h"
17
18 #include <cstdlib>
19 #include <cstring>
20 #include <string>
21
22 #include "common/rs_common_def.h"
23 #include "platform/common/rs_log.h"
24 #include "platform/common/rs_system_properties.h"
25
26 namespace OHOS {
27 namespace Rosen {
28 namespace {
29 static constexpr int INFINITE = -1;
30 static constexpr int64_t MS_TO_NS = 1000000;
31 static constexpr int REVERSE_COUNT = 2;
32 static constexpr int MAX_SPEED = 1000000;
33 constexpr const char* ANIMATION_SCALE_NAME = "persist.sys.graphic.animationscale";
34 } // namespace
35
36 std::atomic<bool> RSAnimationFraction::isInitialized_ = false;
37 std::atomic<float> RSAnimationFraction::animationScale_ = 1.0f;
38
RSAnimationFraction()39 RSAnimationFraction::RSAnimationFraction()
40 {
41 currentIsReverseCycle_ = !isForward_;
42 }
43
Init()44 void RSAnimationFraction::Init()
45 {
46 if (isInitialized_) {
47 return;
48 }
49 SetAnimationScale(RSSystemProperties::GetAnimationScale());
50 RSSystemProperties::WatchSystemProperty(ANIMATION_SCALE_NAME, OnAnimationScaleChangedCallback, nullptr);
51 isInitialized_ = true;
52 }
53
GetAnimationScale()54 float RSAnimationFraction::GetAnimationScale()
55 {
56 return animationScale_.load(std::memory_order_relaxed);
57 }
58
SetAnimationScale(float animationScale)59 void RSAnimationFraction::SetAnimationScale(float animationScale)
60 {
61 animationScale_.store(std::max(0.0f, animationScale), std::memory_order_relaxed);
62 }
63
OnAnimationScaleChangedCallback(const char * key,const char * value,void * context)64 void RSAnimationFraction::OnAnimationScaleChangedCallback(const char* key, const char* value, void* context)
65 {
66 if (strcmp(key, ANIMATION_SCALE_NAME) != 0) {
67 return;
68 }
69 float animationScale = std::atof(value);
70 SetAnimationScale(animationScale);
71 }
72
SetDirectionAfterStart(const ForwardDirection & direction)73 void RSAnimationFraction::SetDirectionAfterStart(const ForwardDirection& direction)
74 {
75 direction_ = direction;
76 }
77
SetLastFrameTime(int64_t lastFrameTime)78 void RSAnimationFraction::SetLastFrameTime(int64_t lastFrameTime)
79 {
80 lastFrameTime_ = lastFrameTime;
81 }
82
GetLastFrameTime() const83 int64_t RSAnimationFraction::GetLastFrameTime() const
84 {
85 return lastFrameTime_;
86 }
87
IsStartRunning(const int64_t deltaTime,const int64_t startDelayNs)88 bool RSAnimationFraction::IsStartRunning(const int64_t deltaTime, const int64_t startDelayNs)
89 {
90 float animationScale = GetAnimationScale();
91 if (direction_ == ForwardDirection::NORMAL) {
92 if (ROSEN_EQ(animationScale, 0.0f)) {
93 runningTime_ += static_cast<int64_t>(deltaTime * MAX_SPEED);
94 } else {
95 runningTime_ += static_cast<int64_t>(deltaTime * speed_ / animationScale);
96 }
97 } else {
98 if (ROSEN_EQ(animationScale, 0.0f)) {
99 runningTime_ -= static_cast<int64_t>(deltaTime * MAX_SPEED);
100 } else {
101 runningTime_ -= static_cast<int64_t>(deltaTime * speed_ / animationScale);
102 }
103 }
104
105 return runningTime_ > startDelayNs;
106 }
107
CalculateLeftDelayTime(const int64_t startDelayNs)108 int64_t RSAnimationFraction::CalculateLeftDelayTime(const int64_t startDelayNs)
109 {
110 if (ROSEN_LE(speed_, 0.0f)) {
111 return 0;
112 }
113 if (runningTime_ > startDelayNs) {
114 return 0;
115 }
116 float animationScale = GetAnimationScale();
117 int64_t leftDelayTimeOrigin;
118 if (direction_ == ForwardDirection::NORMAL) {
119 leftDelayTimeOrigin = startDelayNs - runningTime_;
120 } else {
121 leftDelayTimeOrigin = runningTime_;
122 }
123
124 double ret = static_cast<double>(leftDelayTimeOrigin) / MS_TO_NS / speed_ * animationScale;
125 if (ret > static_cast<double>(INT64_MAX) || ret < static_cast<double>(INT64_MIN)) {
126 return 0;
127 }
128 return static_cast<int64_t>(ret);
129 }
130
GetAnimationFraction(int64_t time,int64_t & minLeftDelayTime)131 std::tuple<float, bool, bool, bool> RSAnimationFraction::GetAnimationFraction(int64_t time, int64_t& minLeftDelayTime)
132 {
133 int64_t durationNs = duration_ * MS_TO_NS;
134 int64_t startDelayNs = startDelay_ * MS_TO_NS;
135 int64_t deltaTime = time - lastFrameTime_;
136 bool isInStartDelay = false;
137 bool isRepeatFinished = false;
138 bool isFinished = true;
139
140 // When the UI animation and spring animation are inherited, time will be passed the default value of -1 for
141 // lastFrameTime_, which is a normal situation.
142 if (deltaTime < 0 && time != -1) {
143 ROSEN_LOGE("RSAnimationFraction::GetAnimationFraction, "
144 "current time: %{public}lld is earlier than last frame time: %{public}lld",
145 static_cast<long long>(time), static_cast<long long>(lastFrameTime_));
146 }
147 lastFrameTime_ = time;
148
149 if (durationNs <= 0 || (repeatCount_ <= 0 && repeatCount_ != INFINITE)) {
150 isFinished = true;
151 minLeftDelayTime = 0;
152 return { GetEndFraction(), isInStartDelay, isFinished, isRepeatFinished };
153 }
154 // 1. Calculates the total running fraction of animation
155 if (!IsStartRunning(deltaTime, startDelayNs)) {
156 if (IsFinished()) {
157 minLeftDelayTime = 0;
158 return { GetStartFraction(), isInStartDelay, true, isRepeatFinished };
159 }
160 isInStartDelay = true;
161 if (minLeftDelayTime > 0) {
162 minLeftDelayTime = std::min(CalculateLeftDelayTime(startDelayNs), minLeftDelayTime);
163 }
164 return { GetStartFraction(), isInStartDelay, false, isRepeatFinished };
165 }
166 minLeftDelayTime = 0;
167
168 // 2. Calculate the running time of the current cycle animation.
169 int64_t realPlayTime = runningTime_ - startDelayNs - (currentRepeatCount_ * durationNs);
170
171 // 3. Update the number of cycles and the corresponding animation fraction.
172 if (direction_ == ForwardDirection::NORMAL) {
173 currentRepeatCount_ += realPlayTime / durationNs;
174 } else {
175 while (currentRepeatCount_ > 0 && realPlayTime < 0) {
176 currentRepeatCount_--;
177 realPlayTime += durationNs;
178 }
179 }
180
181 playTime_ = realPlayTime % durationNs;
182 if (IsInRepeat()) {
183 isRepeatFinished = ((playTime_ + deltaTime) >= durationNs);
184 }
185
186 // 4. update status for auto reverse
187 isFinished = IsFinished();
188 UpdateReverseState(isFinished);
189
190 // 5. get final animation fraction
191 if (isFinished) {
192 return { GetEndFraction(), isInStartDelay, isFinished, isRepeatCallbackEnable_ };
193 }
194 currentTimeFraction_ = static_cast<float>(playTime_) / durationNs;
195 currentTimeFraction_ = currentIsReverseCycle_ ? (1.0f - currentTimeFraction_) : currentTimeFraction_;
196 currentTimeFraction_ = std::clamp(currentTimeFraction_, 0.0f, 1.0f);
197 return { currentTimeFraction_, isInStartDelay, isFinished, isRepeatFinished };
198 }
199
IsInRepeat() const200 bool RSAnimationFraction::IsInRepeat() const
201 {
202 if (isRepeatCallbackEnable_ && ((repeatCount_ == INFINITE) || ((currentRepeatCount_ + 1) < repeatCount_))) {
203 return true;
204 }
205 return false;
206 }
207
IsFinished() const208 bool RSAnimationFraction::IsFinished() const
209 {
210 if (direction_ == ForwardDirection::NORMAL) {
211 if (repeatCount_ == INFINITE) {
212 // When the animation scale is zero, the infinitely looping animation is considered to be finished
213 return ROSEN_EQ(RSAnimationFraction::GetAnimationScale(), 0.0f);
214 }
215 int64_t totalDuration = (static_cast<int64_t>(duration_) * repeatCount_ + startDelay_) * MS_TO_NS;
216 return runningTime_ >= totalDuration;
217 } else {
218 return runningTime_ <= 0;
219 }
220 }
221
GetStartFraction() const222 float RSAnimationFraction::GetStartFraction() const
223 {
224 return isForward_ ? 0.0f : 1.0f;
225 }
226
GetEndFraction() const227 float RSAnimationFraction::GetEndFraction() const
228 {
229 float endFraction = 1.0f;
230 if ((autoReverse_ && repeatCount_ % REVERSE_COUNT == 0) || direction_ == ForwardDirection::REVERSE) {
231 endFraction = 0.0f;
232 }
233 endFraction = isForward_ ? endFraction : 1.0 - endFraction;
234 return endFraction;
235 }
236
UpdateReverseState(bool finish)237 void RSAnimationFraction::UpdateReverseState(bool finish)
238 {
239 if (isForward_) {
240 if (!autoReverse_) {
241 currentIsReverseCycle_ = false;
242 return;
243 }
244 currentIsReverseCycle_ =
245 finish ? (currentRepeatCount_ % REVERSE_COUNT == 0) : (currentRepeatCount_ % REVERSE_COUNT == 1);
246 } else {
247 if (!autoReverse_) {
248 currentIsReverseCycle_ = true;
249 return;
250 }
251 currentIsReverseCycle_ =
252 finish ? (currentRepeatCount_ % REVERSE_COUNT == 1) : (currentRepeatCount_ % REVERSE_COUNT == 0);
253 }
254 }
255
UpdateRemainTimeFraction(float fraction,int remainTime)256 void RSAnimationFraction::UpdateRemainTimeFraction(float fraction, int remainTime)
257 {
258 int64_t remainTimeNS = remainTime * MS_TO_NS;
259 int64_t durationNs = duration_ * MS_TO_NS;
260 int64_t startDelayNs = startDelay_ * MS_TO_NS;
261 float curRemainProgress = currentIsReverseCycle_ ? currentTimeFraction_ : (1.0f - currentTimeFraction_);
262 float ratio = 1.0f;
263 if (remainTime != 0) {
264 ratio = curRemainProgress * durationNs / remainTimeNS;
265 }
266
267 if (runningTime_ > startDelayNs || fabs(fraction) > 1e-6) {
268 if (currentIsReverseCycle_) {
269 runningTime_ =
270 static_cast<int64_t>(durationNs * (1.0f - fraction)) + startDelayNs + currentRepeatCount_ * durationNs;
271 } else {
272 runningTime_ =
273 static_cast<int64_t>(durationNs * fraction) + startDelayNs + currentRepeatCount_ * durationNs;
274 }
275 }
276
277 speed_ = speed_ * ratio;
278 currentTimeFraction_ = fraction;
279 }
280
ResetFraction()281 void RSAnimationFraction::ResetFraction()
282 {
283 runningTime_ = 0;
284 playTime_ = 0;
285 currentTimeFraction_ = 0.0f;
286 currentRepeatCount_ = 0;
287 currentIsReverseCycle_ = false;
288 }
289
GetRemainingRepeatCount() const290 int RSAnimationFraction::GetRemainingRepeatCount() const
291 {
292 if (repeatCount_ == INFINITE) {
293 return INFINITE;
294 }
295 if (currentRepeatCount_ >= repeatCount_) {
296 return 0;
297 } else {
298 return repeatCount_ - currentRepeatCount_;
299 }
300 }
301
GetCurrentIsReverseCycle() const302 bool RSAnimationFraction::GetCurrentIsReverseCycle() const
303 {
304 return currentIsReverseCycle_;
305 }
306 } // namespace Rosen
307 } // namespace OHOS
308