blob: 42f4cf8828bd98a257641f36be829287559d1ae9 [file] [log] [blame]
John Recke45b1fd2014-04-15 09:50:16 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef ANIMATOR_H
17#define ANIMATOR_H
18
19#include <cutils/compiler.h>
John Reck9fa40712014-05-09 15:26:59 -070020#include <utils/RefBase.h>
John Reck52244ff2014-05-01 21:27:37 -070021#include <utils/StrongPointer.h>
Tom Hudson2dc236b2014-10-15 15:46:42 -040022#include <utils/Timers.h>
John Reck1bcacfd2017-11-03 10:12:19 -070023#include <memory>
John Recke45b1fd2014-04-15 09:50:16 -070024
John Reck52244ff2014-05-01 21:27:37 -070025#include "utils/Macros.h"
John Recke45b1fd2014-04-15 09:50:16 -070026
Doris Liuc4bb1852016-02-19 21:39:21 +000027#include <vector>
28
John Recke45b1fd2014-04-15 09:50:16 -070029namespace android {
30namespace uirenderer {
31
John Reck119907c2014-08-14 09:02:01 -070032class AnimationContext;
33class BaseRenderNodeAnimator;
Tom Hudson2dc236b2014-10-15 15:46:42 -040034class CanvasPropertyPrimitive;
35class CanvasPropertyPaint;
36class Interpolator;
John Reck52244ff2014-05-01 21:27:37 -070037class RenderNode;
John Recke45b1fd2014-04-15 09:50:16 -070038class RenderProperties;
John Recke45b1fd2014-04-15 09:50:16 -070039
John Reck52244ff2014-05-01 21:27:37 -070040class AnimationListener : public VirtualLightRefBase {
41public:
John Reckff941dc2014-05-14 16:34:14 -070042 ANDROID_API virtual void onAnimationFinished(BaseRenderNodeAnimator*) = 0;
John Reck1bcacfd2017-11-03 10:12:19 -070043
John Reck52244ff2014-05-01 21:27:37 -070044protected:
45 ANDROID_API virtual ~AnimationListener() {}
46};
47
Doris Liuf7167e82016-08-03 17:54:28 -070048enum class RepeatMode {
49 // These are the same values as the RESTART and REVERSE in ValueAnimator.java.
50 Restart = 1,
51 Reverse = 2
52};
53
John Reckff941dc2014-05-14 16:34:14 -070054class BaseRenderNodeAnimator : public VirtualLightRefBase {
55 PREVENT_COPY_AND_ASSIGN(BaseRenderNodeAnimator);
John Reck1bcacfd2017-11-03 10:12:19 -070056
John Reck52244ff2014-05-01 21:27:37 -070057public:
John Reckc6b32642014-06-02 11:00:09 -070058 ANDROID_API void setStartValue(float value);
John Reck52244ff2014-05-01 21:27:37 -070059 ANDROID_API void setInterpolator(Interpolator* interpolator);
60 ANDROID_API void setDuration(nsecs_t durationInMs);
John Reck315c3292014-05-09 19:21:04 -070061 ANDROID_API nsecs_t duration() { return mDuration; }
Alan Viverettead2f8e32014-05-16 13:28:33 -070062 ANDROID_API void setStartDelay(nsecs_t startDelayInMs);
63 ANDROID_API nsecs_t startDelay() { return mStartDelay; }
John Reck1bcacfd2017-11-03 10:12:19 -070064 ANDROID_API void setListener(AnimationListener* listener) { mListener = listener; }
John Reck119907c2014-08-14 09:02:01 -070065 AnimationListener* listener() { return mListener.get(); }
John Reck1bcacfd2017-11-03 10:12:19 -070066 ANDROID_API void setAllowRunningAsync(bool mayRunAsync) { mMayRunAsync = mayRunAsync; }
John Reckf5945a02014-09-05 15:57:47 -070067 bool mayRunAsync() { return mMayRunAsync; }
Doris Liuc4bb1852016-02-19 21:39:21 +000068 ANDROID_API void start();
Doris Liu718cd3e2016-05-17 16:50:31 -070069 ANDROID_API virtual void reset();
Doris Liuc4bb1852016-02-19 21:39:21 +000070 ANDROID_API void reverse();
71 // Terminates the animation at its current progress.
72 ANDROID_API void cancel();
73
74 // Terminates the animation and skip to the end of the animation.
Doris Liu718cd3e2016-05-17 16:50:31 -070075 ANDROID_API virtual void end();
John Reck52244ff2014-05-01 21:27:37 -070076
John Reck8d8af3c2014-07-01 15:23:45 -070077 void attach(RenderNode* target);
78 virtual void onAttached() {}
Chris Craikd41c4d82015-01-05 15:51:13 -080079 void detach() { mTarget = nullptr; }
Doris Liu718cd3e2016-05-17 16:50:31 -070080 ANDROID_API void pushStaging(AnimationContext& context);
81 ANDROID_API bool animate(AnimationContext& context);
82
83 // Returns the remaining time in ms for the animation. Note this should only be called during
84 // an animation on RenderThread.
85 ANDROID_API nsecs_t getRemainingPlayTime();
John Reckff941dc2014-05-14 16:34:14 -070086
John Reck1bcacfd2017-11-03 10:12:19 -070087 bool isRunning() {
88 return mPlayState == PlayState::Running || mPlayState == PlayState::Reversing;
89 }
Chris Craikb9ce116d2015-08-20 15:14:06 -070090 bool isFinished() { return mPlayState == PlayState::Finished; }
John Reckff941dc2014-05-14 16:34:14 -070091 float finalValue() { return mFinalValue; }
John Reck52244ff2014-05-01 21:27:37 -070092
John Recka7c2ea22014-08-08 13:21:00 -070093 ANDROID_API virtual uint32_t dirtyMask() = 0;
John Reck22184722014-06-20 07:19:30 -070094
John Recke2478d42014-09-03 16:46:05 -070095 void forceEndNow(AnimationContext& context);
Doris Liuc4bb1852016-02-19 21:39:21 +000096 RenderNode* target() { return mTarget; }
Doris Liu8b083202016-02-19 21:46:06 +000097 RenderNode* stagingTarget() { return mStagingTarget; }
John Recke2478d42014-09-03 16:46:05 -070098
John Reck52244ff2014-05-01 21:27:37 -070099protected:
Doris Liu766431a2016-02-04 22:17:11 +0000100 // PlayState is used by mStagingPlayState and mPlayState to track the state initiated from UI
101 // thread and Render Thread animation state, respectively.
102 // From the UI thread, mStagingPlayState transition looks like
Doris Liuc4bb1852016-02-19 21:39:21 +0000103 // NotStarted -> Running/Reversing -> Finished
104 // ^ |
105 // | |
106 // ----------------------
Doris Liu766431a2016-02-04 22:17:11 +0000107 // Note: For mStagingState, the Finished state (optional) is only set when the animation is
108 // terminated by user.
109 //
110 // On Render Thread, mPlayState transition:
Doris Liuc4bb1852016-02-19 21:39:21 +0000111 // NotStart -> Running/Reversing-> Finished
112 // ^ |
113 // | |
114 // ------------------
115 // Note that if the animation is in Running/Reversing state, calling start or reverse again
116 // would do nothing if the animation has the same play direction as the request; otherwise,
117 // the animation would start from where it is and change direction (i.e. Reversing <-> Running)
Doris Liu766431a2016-02-04 22:17:11 +0000118
Chris Craikb9ce116d2015-08-20 15:14:06 -0700119 enum class PlayState {
120 NotStarted,
121 Running,
Doris Liuc4bb1852016-02-19 21:39:21 +0000122 Reversing,
Chris Craikb9ce116d2015-08-20 15:14:06 -0700123 Finished,
124 };
125
Chih-Hung Hsieh49796452016-08-10 14:08:35 -0700126 explicit BaseRenderNodeAnimator(float finalValue);
John Reckff941dc2014-05-14 16:34:14 -0700127 virtual ~BaseRenderNodeAnimator();
John Reck52244ff2014-05-01 21:27:37 -0700128
John Reckff941dc2014-05-14 16:34:14 -0700129 virtual float getValue(RenderNode* target) const = 0;
130 virtual void setValue(RenderNode* target, float value) = 0;
John Reck52244ff2014-05-01 21:27:37 -0700131
John Reck119907c2014-08-14 09:02:01 -0700132 void callOnFinishedListener(AnimationContext& context);
John Reck52244ff2014-05-01 21:27:37 -0700133
John Reck8d8af3c2014-07-01 15:23:45 -0700134 virtual void onStagingPlayStateChanged() {}
Doris Liu766431a2016-02-04 22:17:11 +0000135 virtual void onPlayTimeChanged(nsecs_t playTime) {}
Doris Liu8b083202016-02-19 21:46:06 +0000136 virtual void onPushStaging() {}
John Reck8d8af3c2014-07-01 15:23:45 -0700137
John Reck8d8af3c2014-07-01 15:23:45 -0700138 RenderNode* mTarget;
Doris Liu8b083202016-02-19 21:46:06 +0000139 RenderNode* mStagingTarget;
John Reck8d8af3c2014-07-01 15:23:45 -0700140
John Reckff941dc2014-05-14 16:34:14 -0700141 float mFinalValue;
142 float mDeltaValue;
143 float mFromValue;
144
Chris Craik51d6a3d2014-12-22 17:16:56 -0800145 std::unique_ptr<Interpolator> mInterpolator;
John Reck68bfe0a2014-06-24 15:34:58 -0700146 PlayState mStagingPlayState;
John Reck52244ff2014-05-01 21:27:37 -0700147 PlayState mPlayState;
John Reck68bfe0a2014-06-24 15:34:58 -0700148 bool mHasStartValue;
Alan Viverettead2f8e32014-05-16 13:28:33 -0700149 nsecs_t mStartTime;
Alan Viverettead2f8e32014-05-16 13:28:33 -0700150 nsecs_t mDuration;
151 nsecs_t mStartDelay;
John Reckf5945a02014-09-05 15:57:47 -0700152 bool mMayRunAsync;
Doris Liuc4bb1852016-02-19 21:39:21 +0000153 // Play Time tracks the progress of animation, it should always be [0, mDuration], 0 being
154 // the beginning of the animation, will reach mDuration at the end of an animation.
155 nsecs_t mPlayTime;
John Reck52244ff2014-05-01 21:27:37 -0700156
Alan Viverettead2f8e32014-05-16 13:28:33 -0700157 sp<AnimationListener> mListener;
John Reck68bfe0a2014-06-24 15:34:58 -0700158
159private:
John Reck1bcacfd2017-11-03 10:12:19 -0700160 enum class Request { Start, Reverse, Reset, Cancel, End };
Doris Liu6725d582016-08-04 13:20:17 -0700161
162 // Defines different actions upon finish.
163 enum class Action {
164 // For animations that got canceled or finished normally. no more action needs to be done.
165 None,
166 // For animations that get reset, the reset will happen in the next animation pulse.
167 Reset,
168 // For animations being ended, in the next animation pulse the animation will skip to end.
169 End
170 };
171
John Reck68bfe0a2014-06-24 15:34:58 -0700172 inline void checkMutable();
John Reck119907c2014-08-14 09:02:01 -0700173 virtual void transitionToRunning(AnimationContext& context);
John Reck8d8af3c2014-07-01 15:23:45 -0700174 void doSetStartValue(float value);
Doris Liuc4bb1852016-02-19 21:39:21 +0000175 bool updatePlayTime(nsecs_t playTime);
176 void resolveStagingRequest(Request request);
177
178 std::vector<Request> mStagingRequests;
Doris Liu6725d582016-08-04 13:20:17 -0700179 Action mPendingActionUponFinish = Action::None;
John Reck52244ff2014-05-01 21:27:37 -0700180};
181
John Reck52244ff2014-05-01 21:27:37 -0700182class RenderPropertyAnimator : public BaseRenderNodeAnimator {
183public:
John Recke45b1fd2014-04-15 09:50:16 -0700184 enum RenderProperty {
185 TRANSLATION_X = 0,
186 TRANSLATION_Y,
187 TRANSLATION_Z,
188 SCALE_X,
189 SCALE_Y,
190 ROTATION,
191 ROTATION_X,
192 ROTATION_Y,
193 X,
194 Y,
195 Z,
196 ALPHA,
197 };
198
John Reckff941dc2014-05-14 16:34:14 -0700199 ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue);
200
John Reck22184722014-06-20 07:19:30 -0700201 ANDROID_API virtual uint32_t dirtyMask();
202
John Recke45b1fd2014-04-15 09:50:16 -0700203protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800204 virtual float getValue(RenderNode* target) const override;
205 virtual void setValue(RenderNode* target, float value) override;
206 virtual void onAttached() override;
207 virtual void onStagingPlayStateChanged() override;
Doris Liu8b083202016-02-19 21:46:06 +0000208 virtual void onPushStaging() override;
John Recke45b1fd2014-04-15 09:50:16 -0700209
210private:
John Reck79c7de72014-05-23 10:33:31 -0700211 typedef bool (RenderProperties::*SetFloatProperty)(float value);
John Reck52244ff2014-05-01 21:27:37 -0700212 typedef float (RenderProperties::*GetFloatProperty)() const;
213
John Reckff941dc2014-05-14 16:34:14 -0700214 struct PropertyAccessors;
215 const PropertyAccessors* mPropertyAccess;
John Reck52244ff2014-05-01 21:27:37 -0700216
217 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[];
Doris Liu8b083202016-02-19 21:46:06 +0000218 bool mShouldSyncPropertyFields = false;
219 bool mShouldUpdateStagingProperties = false;
John Reck52244ff2014-05-01 21:27:37 -0700220};
221
222class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator {
223public:
224 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property,
John Reck1bcacfd2017-11-03 10:12:19 -0700225 float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700226
227 ANDROID_API virtual uint32_t dirtyMask();
228
John Reck52244ff2014-05-01 21:27:37 -0700229protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800230 virtual float getValue(RenderNode* target) const override;
231 virtual void setValue(RenderNode* target, float value) override;
John Reck1bcacfd2017-11-03 10:12:19 -0700232
John Reck52244ff2014-05-01 21:27:37 -0700233private:
234 sp<CanvasPropertyPrimitive> mProperty;
235};
236
237class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator {
238public:
239 enum PaintField {
240 STROKE_WIDTH = 0,
241 ALPHA,
242 };
243
John Reck1bcacfd2017-11-03 10:12:19 -0700244 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property, PaintField field,
245 float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700246
247 ANDROID_API virtual uint32_t dirtyMask();
248
John Reck52244ff2014-05-01 21:27:37 -0700249protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800250 virtual float getValue(RenderNode* target) const override;
251 virtual void setValue(RenderNode* target, float value) override;
John Reck1bcacfd2017-11-03 10:12:19 -0700252
John Reck52244ff2014-05-01 21:27:37 -0700253private:
254 sp<CanvasPropertyPaint> mProperty;
255 PaintField mField;
John Recke45b1fd2014-04-15 09:50:16 -0700256};
257
John Reckd3de42c2014-07-15 14:29:33 -0700258class RevealAnimator : public BaseRenderNodeAnimator {
259public:
John Reck1bcacfd2017-11-03 10:12:19 -0700260 ANDROID_API RevealAnimator(int centerX, int centerY, float startValue, float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700261
262 ANDROID_API virtual uint32_t dirtyMask();
263
John Reckd3de42c2014-07-15 14:29:33 -0700264protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800265 virtual float getValue(RenderNode* target) const override;
266 virtual void setValue(RenderNode* target, float value) override;
John Reckd3de42c2014-07-15 14:29:33 -0700267
268private:
269 int mCenterX, mCenterY;
John Reckd3de42c2014-07-15 14:29:33 -0700270};
271
John Recke45b1fd2014-04-15 09:50:16 -0700272} /* namespace uirenderer */
273} /* namespace android */
274
275#endif /* ANIMATOR_H */