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
GetAnimationFraction(int64_t time)108 std::tuple<float, bool, bool, bool> RSAnimationFraction::GetAnimationFraction(int64_t time)
109 {
110 int64_t durationNs = duration_ * MS_TO_NS;
111 int64_t startDelayNs = startDelay_ * MS_TO_NS;
112 int64_t deltaTime = time - lastFrameTime_;
113 bool isInStartDelay = false;
114 bool isRepeatFinished = false;
115 bool isFinished = true;
116
117 // When the UI animation and spring animation are inherited, time will be passed the default value of -1 for
118 // lastFrameTime_, which is a normal situation.
119 if (deltaTime < 0 && time != -1) {
120 ROSEN_LOGE("RSAnimationFraction::GetAnimationFraction, "
121 "current time: %{public}lld is earlier than last frame time: %{public}lld",
122 static_cast<long long>(time), static_cast<long long>(lastFrameTime_));
123 }
124 lastFrameTime_ = time;
125
126 if (durationNs <= 0 || (repeatCount_ <= 0 && repeatCount_ != INFINITE)) {
127 isFinished = true;
128 return { GetEndFraction(), isInStartDelay, isFinished, isRepeatFinished};
129 }
130 // 1. Calculates the total running fraction of animation
131 if (!IsStartRunning(deltaTime, startDelayNs)) {
132 isFinished = IsFinished();
133 isInStartDelay = isFinished ? false : true;
134 return { GetStartFraction(), isInStartDelay, isFinished, isRepeatFinished };
135 }
136
137 // 2. Calculate the running time of the current cycle animation.
138 int64_t realPlayTime = runningTime_ - startDelayNs - (currentRepeatCount_ * durationNs);
139
140 // 3. Update the number of cycles and the corresponding animation fraction.
141 if (direction_ == ForwardDirection::NORMAL) {
142 currentRepeatCount_ += realPlayTime / durationNs;
143 } else {
144 while (currentRepeatCount_ > 0 && realPlayTime < 0) {
145 currentRepeatCount_--;
146 realPlayTime += durationNs;
147 }
148 }
149
150 playTime_ = realPlayTime % durationNs;
151 if (IsInRepeat()) {
152 isRepeatFinished = ((playTime_ + deltaTime) >= durationNs);
153 }
154
155 // 4. update status for auto reverse
156 isFinished = IsFinished();
157 UpdateReverseState(isFinished);
158
159 // 5. get final animation fraction
160 if (isFinished) {
161 return { GetEndFraction(), isInStartDelay, isFinished, isRepeatCallbackEnable_};
162 }
163 currentTimeFraction_ = static_cast<float>(playTime_) / durationNs;
164 currentTimeFraction_ = currentIsReverseCycle_ ? (1.0f - currentTimeFraction_) : currentTimeFraction_;
165 currentTimeFraction_ = std::clamp(currentTimeFraction_, 0.0f, 1.0f);
166 return { currentTimeFraction_, isInStartDelay, isFinished, isRepeatFinished };
167 }
168
IsInRepeat() const169 bool RSAnimationFraction::IsInRepeat() const
170 {
171 if (isRepeatCallbackEnable_ && ((repeatCount_ == INFINITE) || ((currentRepeatCount_ + 1) < repeatCount_))) {
172 return true;
173 }
174 return false;
175 }
176
IsFinished() const177 bool RSAnimationFraction::IsFinished() const
178 {
179 if (direction_ == ForwardDirection::NORMAL) {
180 if (repeatCount_ == INFINITE) {
181 // When the animation scale is zero, the infinitely looping animation is considered to be finished
182 return ROSEN_EQ(RSAnimationFraction::GetAnimationScale(), 0.0f);
183 }
184 int64_t totalDuration = (static_cast<int64_t>(duration_) * repeatCount_ + startDelay_) * MS_TO_NS;
185 return runningTime_ >= totalDuration;
186 } else {
187 return runningTime_ <= 0;
188 }
189 }
190
GetStartFraction() const191 float RSAnimationFraction::GetStartFraction() const
192 {
193 return isForward_ ? 0.0f : 1.0f;
194 }
195
GetEndFraction() const196 float RSAnimationFraction::GetEndFraction() const
197 {
198 float endFraction = 1.0f;
199 if ((autoReverse_ && repeatCount_ % REVERSE_COUNT == 0) || direction_ == ForwardDirection::REVERSE) {
200 endFraction = 0.0f;
201 }
202 endFraction = isForward_ ? endFraction : 1.0 - endFraction;
203 return endFraction;
204 }
205
UpdateReverseState(bool finish)206 void RSAnimationFraction::UpdateReverseState(bool finish)
207 {
208 if (isForward_) {
209 if (!autoReverse_) {
210 currentIsReverseCycle_ = false;
211 return;
212 }
213 currentIsReverseCycle_ =
214 finish ? (currentRepeatCount_ % REVERSE_COUNT == 0) : (currentRepeatCount_ % REVERSE_COUNT == 1);
215 } else {
216 if (!autoReverse_) {
217 currentIsReverseCycle_ = true;
218 return;
219 }
220 currentIsReverseCycle_ =
221 finish ? (currentRepeatCount_ % REVERSE_COUNT == 1) : (currentRepeatCount_ % REVERSE_COUNT == 0);
222 }
223 }
224
UpdateRemainTimeFraction(float fraction,int remainTime)225 void RSAnimationFraction::UpdateRemainTimeFraction(float fraction, int remainTime)
226 {
227 int64_t remainTimeNS = remainTime * MS_TO_NS;
228 int64_t durationNs = duration_ * MS_TO_NS;
229 int64_t startDelayNs = startDelay_ * MS_TO_NS;
230 float curRemainProgress = currentIsReverseCycle_ ? currentTimeFraction_ : (1.0f - currentTimeFraction_);
231 float ratio = 1.0f;
232 if (remainTime != 0) {
233 ratio = curRemainProgress * durationNs / remainTimeNS;
234 }
235
236 if (runningTime_ > startDelayNs || fabs(fraction) > 1e-6) {
237 if (currentIsReverseCycle_) {
238 runningTime_ =
239 static_cast<int64_t>(durationNs * (1.0f - fraction)) + startDelayNs + currentRepeatCount_ * durationNs;
240 } else {
241 runningTime_ =
242 static_cast<int64_t>(durationNs * fraction) + startDelayNs + currentRepeatCount_ * durationNs;
243 }
244 }
245
246 speed_ = speed_ * ratio;
247 currentTimeFraction_ = fraction;
248 }
249
ResetFraction()250 void RSAnimationFraction::ResetFraction()
251 {
252 runningTime_ = 0;
253 playTime_ = 0;
254 currentTimeFraction_ = 0.0f;
255 currentRepeatCount_ = 0;
256 currentIsReverseCycle_ = false;
257 }
258
GetRemainingRepeatCount() const259 int RSAnimationFraction::GetRemainingRepeatCount() const
260 {
261 if (repeatCount_ == INFINITE) {
262 return INFINITE;
263 }
264 if (currentRepeatCount_ >= repeatCount_) {
265 return 0;
266 } else {
267 return repeatCount_ - currentRepeatCount_;
268 }
269 }
270
GetCurrentIsReverseCycle() const271 bool RSAnimationFraction::GetCurrentIsReverseCycle() const
272 {
273 return currentIsReverseCycle_;
274 }
275 } // namespace Rosen
276 } // namespace OHOS
277