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