1 /*
2 * Copyright (c) 2021 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_render_animation.h"
17
18 #include "pipeline/rs_canvas_render_node.h"
19 #include "platform/common/rs_log.h"
20
21 namespace OHOS {
22 namespace Rosen {
RSRenderAnimation(AnimationId id)23 RSRenderAnimation::RSRenderAnimation(AnimationId id) : id_(id) {}
24 #ifdef ROSEN_OHOS
Marshalling(Parcel & parcel) const25 bool RSRenderAnimation::Marshalling(Parcel& parcel) const
26 {
27 // animationId, targetId
28 if (!(parcel.WriteUint64(id_))) {
29 ROSEN_LOGE("RSRenderAnimation::Marshalling, write id failed");
30 return false;
31 }
32 // RSAnimationTimingProtocal
33 if (!(parcel.WriteInt32(animationFraction_.GetDuration()) &&
34 parcel.WriteInt32(animationFraction_.GetStartDelay()) &&
35 parcel.WriteFloat(animationFraction_.GetSpeed()) &&
36 parcel.WriteInt32(animationFraction_.GetRepeatCount()) &&
37 parcel.WriteBool(animationFraction_.GetAutoReverse()) &&
38 parcel.WriteBool(animationFraction_.GetDirection()) &&
39 parcel.WriteInt32(static_cast<std::underlying_type<FillMode>::type>(animationFraction_.GetFillMode())))) {
40 ROSEN_LOGE("RSRenderAnimation::Marshalling, write param failed");
41 return false;
42 }
43 return true;
44 }
45
ParseParam(Parcel & parcel)46 bool RSRenderAnimation::ParseParam(Parcel& parcel)
47 {
48 int32_t duration = 0;
49 int32_t startDelay = 0;
50 int32_t repeatCount = 0;
51 int32_t fillMode = 0;
52 float speed = 0.0;
53 bool autoReverse = false;
54 bool direction = false;
55 if (!(parcel.ReadUint64(id_) && parcel.ReadInt32(duration) && parcel.ReadInt32(startDelay) &&
56 parcel.ReadFloat(speed) && parcel.ReadInt32(repeatCount) && parcel.ReadBool(autoReverse) &&
57 parcel.ReadBool(direction) && parcel.ReadInt32(fillMode))) {
58 ROSEN_LOGE("RSRenderAnimation::ParseParam, read param failed");
59 return false;
60 }
61 SetDuration(duration);
62 SetStartDelay(startDelay);
63 SetRepeatCount(repeatCount);
64 SetAutoReverse(autoReverse);
65 SetSpeed(speed);
66 SetDirection(direction);
67 SetFillMode(static_cast<FillMode>(fillMode));
68 return true;
69 }
70 #endif
GetAnimationId() const71 AnimationId RSRenderAnimation::GetAnimationId() const
72 {
73 return id_;
74 }
75
IsStarted() const76 bool RSRenderAnimation::IsStarted() const
77 {
78 return state_ != AnimationState::INITIALIZED;
79 }
80
IsRunning() const81 bool RSRenderAnimation::IsRunning() const
82 {
83 return state_ == AnimationState::RUNNING;
84 }
85
IsPaused() const86 bool RSRenderAnimation::IsPaused() const
87 {
88 return state_ == AnimationState::PAUSED;
89 }
90
IsFinished() const91 bool RSRenderAnimation::IsFinished() const
92 {
93 return state_ == AnimationState::FINISHED;
94 }
95
GetProperty() const96 RSAnimatableProperty RSRenderAnimation::GetProperty() const
97 {
98 return RSAnimatableProperty::INVALID;
99 }
100
Attach(RSRenderNode * renderNode)101 void RSRenderAnimation::Attach(RSRenderNode* renderNode)
102 {
103 target_ = renderNode;
104 OnAttach();
105 }
106
Detach()107 void RSRenderAnimation::Detach()
108 {
109 OnDetach();
110 target_ = nullptr;
111 }
112
Start()113 void RSRenderAnimation::Start()
114 {
115 if (IsStarted()) {
116 ROSEN_LOGE("Failed to start animation, animation has started!");
117 return;
118 }
119
120 state_ = AnimationState::RUNNING;
121 firstToRunning_ = true;
122 ProcessFillModeOnStart(animationFraction_.GetStartFraction());
123 }
124
Finish()125 void RSRenderAnimation::Finish()
126 {
127 if (!IsPaused() && !IsRunning()) {
128 ROSEN_LOGE("Failed to finish animation, animation is not running!");
129 return;
130 }
131
132 state_ = AnimationState::FINISHED;
133 ProcessFillModeOnFinish(animationFraction_.GetEndFraction());
134 }
135
Pause()136 void RSRenderAnimation::Pause()
137 {
138 if (!IsRunning()) {
139 ROSEN_LOGE("Failed to pause animation, animation is not running!");
140 return;
141 }
142
143 state_ = AnimationState::PAUSED;
144 }
145
Resume()146 void RSRenderAnimation::Resume()
147 {
148 if (!IsPaused()) {
149 ROSEN_LOGE("Failed to resume animation, animation is not paused!");
150 return;
151 }
152
153 state_ = AnimationState::RUNNING;
154 firstToRunning_ = true;
155 }
156
SetFraction(float fraction)157 void RSRenderAnimation::SetFraction(float fraction)
158 {
159 if (!IsPaused()) {
160 ROSEN_LOGE("Failed to set fraction, animation is not paused!");
161 return;
162 }
163
164 fraction = std::min(std::max(fraction, 0.0f), 1.0f);
165 OnSetFraction(fraction);
166 }
167
SetReversed(bool isReversed)168 void RSRenderAnimation::SetReversed(bool isReversed)
169 {
170 if (!IsPaused() && !IsRunning()) {
171 ROSEN_LOGE("Failed to reverse animation, animation is not running!");
172 return;
173 }
174
175 animationFraction_.SetDirectionAfterStart(isReversed ? ForwardDirection::REVERSE : ForwardDirection::NORMAL);
176 }
177
GetTarget() const178 RSRenderNode* RSRenderAnimation::GetTarget() const
179 {
180 return target_;
181 }
182
SetFractionInner(float fraction)183 void RSRenderAnimation::SetFractionInner(float fraction)
184 {
185 animationFraction_.UpdateRemainTimeFraction(fraction);
186 }
187
OnSetFraction(float fraction)188 void RSRenderAnimation::OnSetFraction(float fraction)
189 {
190 SetFractionInner(fraction);
191 }
192
ProcessFillModeOnStart(float startFraction)193 void RSRenderAnimation::ProcessFillModeOnStart(float startFraction)
194 {
195 auto fillMode = GetFillMode();
196 if (fillMode == FillMode::BACKWARDS || fillMode == FillMode::BOTH) {
197 OnAnimate(startFraction);
198 }
199 }
200
ProcessFillModeOnFinish(float endFraction)201 void RSRenderAnimation::ProcessFillModeOnFinish(float endFraction)
202 {
203 auto fillMode = GetFillMode();
204 if (fillMode == FillMode::FORWARDS || fillMode == FillMode::BOTH) {
205 OnAnimate(endFraction);
206 } else {
207 OnRemoveOnCompletion();
208 }
209 }
210
Animate(int64_t time)211 bool RSRenderAnimation::Animate(int64_t time)
212 {
213 if (!IsRunning()) {
214 ROSEN_LOGI("RSRenderAnimation::Animate, IsRunning is false!");
215 return state_ == AnimationState::FINISHED;
216 }
217
218 if (firstToRunning_) {
219 animationFraction_.SetLastFrameTime(time);
220 firstToRunning_ = false;
221 }
222
223 bool isInStartDelay = false;
224 bool isFinished = false;
225 float fraction = animationFraction_.GetAnimationFraction(time, isInStartDelay, isFinished);
226 if (isInStartDelay) {
227 ProcessFillModeOnStart(fraction);
228 ROSEN_LOGI("RSRenderAnimation::Animate, isInStartDelay is true");
229 return false;
230 }
231
232 OnAnimate(fraction);
233 if (isFinished) {
234 ProcessFillModeOnFinish(fraction);
235 ROSEN_LOGI("RSRenderAnimation::Animate, isFinished is true");
236 return true;
237 }
238 return isFinished;
239 }
240 } // namespace Rosen
241 } // namespace OHOS
242