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