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
GetAnimationFraction(int64_t time)88 std::tuple<float, bool, bool, bool> RSAnimationFraction::GetAnimationFraction(int64_t time)
89 {
90 int64_t durationNs = duration_ * MS_TO_NS;
91 int64_t startDelayNs = startDelay_ * MS_TO_NS;
92 int64_t deltaTime = time - lastFrameTime_;
93 lastFrameTime_ = time;
94 bool isInStartDelay = false;
95 bool isRepeatFinished = false;
96 bool isFinished = true;
97
98 if (durationNs <= 0 || (repeatCount_ <= 0 && repeatCount_ != INFINITE)) {
99 isFinished = true;
100 return { GetEndFraction(), isInStartDelay, isFinished, isRepeatFinished};
101 }
102 // 1. Calculates the total running fraction of animation
103 float animationScale = GetAnimationScale();
104 if (direction_ == ForwardDirection::NORMAL) {
105 if (animationScale == 0.0) {
106 runningTime_ += static_cast<int64_t>(deltaTime * MAX_SPEED);
107 } else {
108 runningTime_ += static_cast<int64_t>(deltaTime * speed_ / animationScale);
109 }
110 } else {
111 if (animationScale == 0.0) {
112 runningTime_ -= static_cast<int64_t>(deltaTime * MAX_SPEED);
113 } else {
114 runningTime_ -= static_cast<int64_t>(deltaTime * speed_ / animationScale);
115 }
116 }
117 if (runningTime_ < startDelayNs) {
118 isFinished = IsFinished();
119 isInStartDelay = isFinished ? false : true;
120 return { GetStartFraction(), isInStartDelay, isFinished, isRepeatFinished };
121 }
122
123 // 2. Calculate the running time of the current cycle animation.
124 int64_t realPlayTime = runningTime_ - startDelayNs - (currentRepeatCount_ * durationNs);
125
126 // 3. Update the number of cycles and the corresponding animation fraction.
127 if (direction_ == ForwardDirection::NORMAL) {
128 currentRepeatCount_ += realPlayTime / durationNs;
129 } else {
130 while (currentRepeatCount_ > 0 && realPlayTime < 0) {
131 currentRepeatCount_--;
132 realPlayTime += durationNs;
133 }
134 }
135
136 playTime_ = realPlayTime % durationNs;
137 if (IsInRepeat()) {
138 isRepeatFinished = ((playTime_ + deltaTime) >= durationNs);
139 }
140
141 // 4. update status for auto reverse
142 isFinished = IsFinished();
143 UpdateReverseState(isFinished);
144
145 // 5. get final animation fraction
146 if (isFinished) {
147 return { GetEndFraction(), isInStartDelay, isFinished, isRepeatCallbackEnable_};
148 }
149 currentTimeFraction_ = static_cast<float>(playTime_) / durationNs;
150 currentTimeFraction_ = currentIsReverseCycle_ ? (1.0f - currentTimeFraction_) : currentTimeFraction_;
151 currentTimeFraction_ = std::clamp(currentTimeFraction_, 0.0f, 1.0f);
152 return { currentTimeFraction_, isInStartDelay, isFinished, isRepeatFinished };
153 }
154
IsInRepeat() const155 bool RSAnimationFraction::IsInRepeat() const
156 {
157 if (isRepeatCallbackEnable_ && ((repeatCount_ == INFINITE) || ((currentRepeatCount_ + 1) < repeatCount_))) {
158 return true;
159 }
160 return false;
161 }
162
IsFinished() const163 bool RSAnimationFraction::IsFinished() const
164 {
165 if (direction_ == ForwardDirection::NORMAL) {
166 if (repeatCount_ == INFINITE) {
167 return false;
168 }
169 int64_t totalDuration = (duration_ * repeatCount_ + startDelay_) * MS_TO_NS;
170 return runningTime_ >= totalDuration;
171 } else {
172 return runningTime_ <= 0;
173 }
174 }
175
GetStartFraction() const176 float RSAnimationFraction::GetStartFraction() const
177 {
178 return isForward_ ? 0.0f : 1.0f;
179 }
180
GetEndFraction() const181 float RSAnimationFraction::GetEndFraction() const
182 {
183 float endFraction = 1.0f;
184 if ((autoReverse_ && repeatCount_ % REVERSE_COUNT == 0) || direction_ == ForwardDirection::REVERSE) {
185 endFraction = 0.0f;
186 }
187 endFraction = isForward_ ? endFraction : 1.0 - endFraction;
188 return endFraction;
189 }
190
UpdateReverseState(bool finish)191 void RSAnimationFraction::UpdateReverseState(bool finish)
192 {
193 if (isForward_) {
194 if (!autoReverse_) {
195 currentIsReverseCycle_ = false;
196 return;
197 }
198 currentIsReverseCycle_ =
199 finish ? (currentRepeatCount_ % REVERSE_COUNT == 0) : (currentRepeatCount_ % REVERSE_COUNT == 1);
200 } else {
201 if (!autoReverse_) {
202 currentIsReverseCycle_ = true;
203 return;
204 }
205 currentIsReverseCycle_ =
206 finish ? (currentRepeatCount_ % REVERSE_COUNT == 1) : (currentRepeatCount_ % REVERSE_COUNT == 0);
207 }
208 }
209
UpdateRemainTimeFraction(float fraction,int remainTime)210 void RSAnimationFraction::UpdateRemainTimeFraction(float fraction, int remainTime)
211 {
212 int64_t remainTimeNS = remainTime * MS_TO_NS;
213 int64_t durationNs = duration_ * MS_TO_NS;
214 int64_t startDelayNs = startDelay_ * MS_TO_NS;
215 float curRemainProgress = currentIsReverseCycle_ ? currentTimeFraction_ : (1.0f - currentTimeFraction_);
216 float ratio = 1.0f;
217 if (remainTime != 0) {
218 ratio = curRemainProgress * durationNs / remainTimeNS;
219 }
220
221 if (runningTime_ > startDelayNs || fabs(fraction) > 1e-6) {
222 if (currentIsReverseCycle_) {
223 runningTime_ =
224 static_cast<int64_t>(durationNs * (1.0f - fraction)) + startDelayNs + currentRepeatCount_ * durationNs;
225 } else {
226 runningTime_ =
227 static_cast<int64_t>(durationNs * fraction) + startDelayNs + currentRepeatCount_ * durationNs;
228 }
229 }
230
231 speed_ = speed_ * ratio;
232 currentTimeFraction_ = fraction;
233 }
234
ResetFraction()235 void RSAnimationFraction::ResetFraction()
236 {
237 runningTime_ = 0;
238 playTime_ = 0;
239 currentTimeFraction_ = 0.0f;
240 currentRepeatCount_ = 0;
241 currentIsReverseCycle_ = false;
242 }
243 } // namespace Rosen
244 } // namespace OHOS
245