blob: ef9658250bf0fb4a3506c1d1a9d691d3cf59365e [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
Chris Craik51d6a3d2014-12-22 17:16:56 -080019#include <memory>
John Recke45b1fd2014-04-15 09:50:16 -070020#include <cutils/compiler.h>
John Reck9fa40712014-05-09 15:26:59 -070021#include <utils/RefBase.h>
John Reck52244ff2014-05-01 21:27:37 -070022#include <utils/StrongPointer.h>
Tom Hudson2dc236b2014-10-15 15:46:42 -040023#include <utils/Timers.h>
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 Reck52244ff2014-05-01 21:27:37 -070043protected:
44 ANDROID_API virtual ~AnimationListener() {}
45};
46
John Reckff941dc2014-05-14 16:34:14 -070047class BaseRenderNodeAnimator : public VirtualLightRefBase {
48 PREVENT_COPY_AND_ASSIGN(BaseRenderNodeAnimator);
John Reck52244ff2014-05-01 21:27:37 -070049public:
John Reckc6b32642014-06-02 11:00:09 -070050 ANDROID_API void setStartValue(float value);
John Reck52244ff2014-05-01 21:27:37 -070051 ANDROID_API void setInterpolator(Interpolator* interpolator);
52 ANDROID_API void setDuration(nsecs_t durationInMs);
John Reck315c3292014-05-09 19:21:04 -070053 ANDROID_API nsecs_t duration() { return mDuration; }
Alan Viverettead2f8e32014-05-16 13:28:33 -070054 ANDROID_API void setStartDelay(nsecs_t startDelayInMs);
55 ANDROID_API nsecs_t startDelay() { return mStartDelay; }
John Reck52244ff2014-05-01 21:27:37 -070056 ANDROID_API void setListener(AnimationListener* listener) {
57 mListener = listener;
58 }
John Reck119907c2014-08-14 09:02:01 -070059 AnimationListener* listener() { return mListener.get(); }
John Reckf5945a02014-09-05 15:57:47 -070060 ANDROID_API void setAllowRunningAsync(bool mayRunAsync) {
61 mMayRunAsync = mayRunAsync;
62 }
63 bool mayRunAsync() { return mMayRunAsync; }
Doris Liuc4bb1852016-02-19 21:39:21 +000064 ANDROID_API void start();
Doris Liu718cd3e2016-05-17 16:50:31 -070065 ANDROID_API virtual void reset();
Doris Liuc4bb1852016-02-19 21:39:21 +000066 ANDROID_API void reverse();
67 // Terminates the animation at its current progress.
68 ANDROID_API void cancel();
69
70 // Terminates the animation and skip to the end of the animation.
Doris Liu718cd3e2016-05-17 16:50:31 -070071 ANDROID_API virtual void end();
John Reck52244ff2014-05-01 21:27:37 -070072
John Reck8d8af3c2014-07-01 15:23:45 -070073 void attach(RenderNode* target);
74 virtual void onAttached() {}
Chris Craikd41c4d82015-01-05 15:51:13 -080075 void detach() { mTarget = nullptr; }
Doris Liu718cd3e2016-05-17 16:50:31 -070076 ANDROID_API void pushStaging(AnimationContext& context);
77 ANDROID_API bool animate(AnimationContext& context);
78
79 // Returns the remaining time in ms for the animation. Note this should only be called during
80 // an animation on RenderThread.
81 ANDROID_API nsecs_t getRemainingPlayTime();
John Reckff941dc2014-05-14 16:34:14 -070082
Doris Liuc4bb1852016-02-19 21:39:21 +000083 bool isRunning() { return mPlayState == PlayState::Running
84 || mPlayState == PlayState::Reversing; }
Chris Craikb9ce116d2015-08-20 15:14:06 -070085 bool isFinished() { return mPlayState == PlayState::Finished; }
John Reckff941dc2014-05-14 16:34:14 -070086 float finalValue() { return mFinalValue; }
John Reck52244ff2014-05-01 21:27:37 -070087
John Recka7c2ea22014-08-08 13:21:00 -070088 ANDROID_API virtual uint32_t dirtyMask() = 0;
John Reck22184722014-06-20 07:19:30 -070089
John Recke2478d42014-09-03 16:46:05 -070090 void forceEndNow(AnimationContext& context);
Doris Liuc4bb1852016-02-19 21:39:21 +000091 RenderNode* target() { return mTarget; }
Doris Liu8b083202016-02-19 21:46:06 +000092 RenderNode* stagingTarget() { return mStagingTarget; }
John Recke2478d42014-09-03 16:46:05 -070093
John Reck52244ff2014-05-01 21:27:37 -070094protected:
Doris Liu766431a2016-02-04 22:17:11 +000095 // PlayState is used by mStagingPlayState and mPlayState to track the state initiated from UI
96 // thread and Render Thread animation state, respectively.
97 // From the UI thread, mStagingPlayState transition looks like
Doris Liuc4bb1852016-02-19 21:39:21 +000098 // NotStarted -> Running/Reversing -> Finished
99 // ^ |
100 // | |
101 // ----------------------
Doris Liu766431a2016-02-04 22:17:11 +0000102 // Note: For mStagingState, the Finished state (optional) is only set when the animation is
103 // terminated by user.
104 //
105 // On Render Thread, mPlayState transition:
Doris Liuc4bb1852016-02-19 21:39:21 +0000106 // NotStart -> Running/Reversing-> Finished
107 // ^ |
108 // | |
109 // ------------------
110 // Note that if the animation is in Running/Reversing state, calling start or reverse again
111 // would do nothing if the animation has the same play direction as the request; otherwise,
112 // the animation would start from where it is and change direction (i.e. Reversing <-> Running)
Doris Liu766431a2016-02-04 22:17:11 +0000113
Chris Craikb9ce116d2015-08-20 15:14:06 -0700114 enum class PlayState {
115 NotStarted,
116 Running,
Doris Liuc4bb1852016-02-19 21:39:21 +0000117 Reversing,
Chris Craikb9ce116d2015-08-20 15:14:06 -0700118 Finished,
119 };
120
John Reckff941dc2014-05-14 16:34:14 -0700121 BaseRenderNodeAnimator(float finalValue);
122 virtual ~BaseRenderNodeAnimator();
John Reck52244ff2014-05-01 21:27:37 -0700123
John Reckff941dc2014-05-14 16:34:14 -0700124 virtual float getValue(RenderNode* target) const = 0;
125 virtual void setValue(RenderNode* target, float value) = 0;
John Reck52244ff2014-05-01 21:27:37 -0700126
John Reck119907c2014-08-14 09:02:01 -0700127 void callOnFinishedListener(AnimationContext& context);
John Reck52244ff2014-05-01 21:27:37 -0700128
John Reck8d8af3c2014-07-01 15:23:45 -0700129 virtual void onStagingPlayStateChanged() {}
Doris Liu766431a2016-02-04 22:17:11 +0000130 virtual void onPlayTimeChanged(nsecs_t playTime) {}
Doris Liu8b083202016-02-19 21:46:06 +0000131 virtual void onPushStaging() {}
John Reck8d8af3c2014-07-01 15:23:45 -0700132
John Reck8d8af3c2014-07-01 15:23:45 -0700133 RenderNode* mTarget;
Doris Liu8b083202016-02-19 21:46:06 +0000134 RenderNode* mStagingTarget;
John Reck8d8af3c2014-07-01 15:23:45 -0700135
John Reckff941dc2014-05-14 16:34:14 -0700136 float mFinalValue;
137 float mDeltaValue;
138 float mFromValue;
139
Chris Craik51d6a3d2014-12-22 17:16:56 -0800140 std::unique_ptr<Interpolator> mInterpolator;
John Reck68bfe0a2014-06-24 15:34:58 -0700141 PlayState mStagingPlayState;
John Reck52244ff2014-05-01 21:27:37 -0700142 PlayState mPlayState;
John Reck68bfe0a2014-06-24 15:34:58 -0700143 bool mHasStartValue;
Alan Viverettead2f8e32014-05-16 13:28:33 -0700144 nsecs_t mStartTime;
Alan Viverettead2f8e32014-05-16 13:28:33 -0700145 nsecs_t mDuration;
146 nsecs_t mStartDelay;
John Reckf5945a02014-09-05 15:57:47 -0700147 bool mMayRunAsync;
Doris Liuc4bb1852016-02-19 21:39:21 +0000148 // Play Time tracks the progress of animation, it should always be [0, mDuration], 0 being
149 // the beginning of the animation, will reach mDuration at the end of an animation.
150 nsecs_t mPlayTime;
John Reck52244ff2014-05-01 21:27:37 -0700151
Alan Viverettead2f8e32014-05-16 13:28:33 -0700152 sp<AnimationListener> mListener;
John Reck68bfe0a2014-06-24 15:34:58 -0700153
154private:
Doris Liuc4bb1852016-02-19 21:39:21 +0000155 enum class Request {
156 Start,
157 Reverse,
158 Reset,
159 Cancel,
160 End
161 };
Doris Liu6725d582016-08-04 13:20:17 -0700162
163 // Defines different actions upon finish.
164 enum class Action {
165 // For animations that got canceled or finished normally. no more action needs to be done.
166 None,
167 // For animations that get reset, the reset will happen in the next animation pulse.
168 Reset,
169 // For animations being ended, in the next animation pulse the animation will skip to end.
170 End
171 };
172
John Reck68bfe0a2014-06-24 15:34:58 -0700173 inline void checkMutable();
John Reck119907c2014-08-14 09:02:01 -0700174 virtual void transitionToRunning(AnimationContext& context);
John Reck8d8af3c2014-07-01 15:23:45 -0700175 void doSetStartValue(float value);
Doris Liuc4bb1852016-02-19 21:39:21 +0000176 bool updatePlayTime(nsecs_t playTime);
177 void resolveStagingRequest(Request request);
178
179 std::vector<Request> mStagingRequests;
Doris Liu6725d582016-08-04 13:20:17 -0700180 Action mPendingActionUponFinish = Action::None;
John Reck52244ff2014-05-01 21:27:37 -0700181};
182
John Reck52244ff2014-05-01 21:27:37 -0700183class RenderPropertyAnimator : public BaseRenderNodeAnimator {
184public:
John Recke45b1fd2014-04-15 09:50:16 -0700185 enum RenderProperty {
186 TRANSLATION_X = 0,
187 TRANSLATION_Y,
188 TRANSLATION_Z,
189 SCALE_X,
190 SCALE_Y,
191 ROTATION,
192 ROTATION_X,
193 ROTATION_Y,
194 X,
195 Y,
196 Z,
197 ALPHA,
198 };
199
John Reckff941dc2014-05-14 16:34:14 -0700200 ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue);
201
John Reck22184722014-06-20 07:19:30 -0700202 ANDROID_API virtual uint32_t dirtyMask();
203
John Recke45b1fd2014-04-15 09:50:16 -0700204protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800205 virtual float getValue(RenderNode* target) const override;
206 virtual void setValue(RenderNode* target, float value) override;
207 virtual void onAttached() override;
208 virtual void onStagingPlayStateChanged() override;
Doris Liu8b083202016-02-19 21:46:06 +0000209 virtual void onPushStaging() override;
John Recke45b1fd2014-04-15 09:50:16 -0700210
211private:
John Reck79c7de72014-05-23 10:33:31 -0700212 typedef bool (RenderProperties::*SetFloatProperty)(float value);
John Reck52244ff2014-05-01 21:27:37 -0700213 typedef float (RenderProperties::*GetFloatProperty)() const;
214
John Reckff941dc2014-05-14 16:34:14 -0700215 struct PropertyAccessors;
216 const PropertyAccessors* mPropertyAccess;
John Reck52244ff2014-05-01 21:27:37 -0700217
218 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[];
Doris Liu8b083202016-02-19 21:46:06 +0000219 bool mShouldSyncPropertyFields = false;
220 bool mShouldUpdateStagingProperties = false;
John Reck52244ff2014-05-01 21:27:37 -0700221};
222
223class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator {
224public:
225 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property,
John Reckff941dc2014-05-14 16:34:14 -0700226 float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700227
228 ANDROID_API virtual uint32_t dirtyMask();
229
John Reck52244ff2014-05-01 21:27:37 -0700230protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800231 virtual float getValue(RenderNode* target) const override;
232 virtual void setValue(RenderNode* target, float value) override;
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
244 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property,
John Reckff941dc2014-05-14 16:34:14 -0700245 PaintField field, 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 Reck52244ff2014-05-01 21:27:37 -0700252private:
253 sp<CanvasPropertyPaint> mProperty;
254 PaintField mField;
John Recke45b1fd2014-04-15 09:50:16 -0700255};
256
John Reckd3de42c2014-07-15 14:29:33 -0700257class RevealAnimator : public BaseRenderNodeAnimator {
258public:
Chris Craikaf4d04c2014-07-29 12:50:14 -0700259 ANDROID_API RevealAnimator(int centerX, int centerY,
John Reckd3de42c2014-07-15 14:29:33 -0700260 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 */