blob: aac355c11ac901165235e101a9f25d0c4f882403 [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
Doris Liuf7167e82016-08-03 17:54:28 -070047enum class RepeatMode {
48 // These are the same values as the RESTART and REVERSE in ValueAnimator.java.
49 Restart = 1,
50 Reverse = 2
51};
52
John Reckff941dc2014-05-14 16:34:14 -070053class BaseRenderNodeAnimator : public VirtualLightRefBase {
54 PREVENT_COPY_AND_ASSIGN(BaseRenderNodeAnimator);
John Reck52244ff2014-05-01 21:27:37 -070055public:
John Reckc6b32642014-06-02 11:00:09 -070056 ANDROID_API void setStartValue(float value);
John Reck52244ff2014-05-01 21:27:37 -070057 ANDROID_API void setInterpolator(Interpolator* interpolator);
58 ANDROID_API void setDuration(nsecs_t durationInMs);
John Reck315c3292014-05-09 19:21:04 -070059 ANDROID_API nsecs_t duration() { return mDuration; }
Alan Viverettead2f8e32014-05-16 13:28:33 -070060 ANDROID_API void setStartDelay(nsecs_t startDelayInMs);
61 ANDROID_API nsecs_t startDelay() { return mStartDelay; }
John Reck52244ff2014-05-01 21:27:37 -070062 ANDROID_API void setListener(AnimationListener* listener) {
63 mListener = listener;
64 }
John Reck119907c2014-08-14 09:02:01 -070065 AnimationListener* listener() { return mListener.get(); }
John Reckf5945a02014-09-05 15:57:47 -070066 ANDROID_API void setAllowRunningAsync(bool mayRunAsync) {
67 mMayRunAsync = mayRunAsync;
68 }
69 bool mayRunAsync() { return mMayRunAsync; }
Doris Liuc4bb1852016-02-19 21:39:21 +000070 ANDROID_API void start();
Doris Liu718cd3e2016-05-17 16:50:31 -070071 ANDROID_API virtual void reset();
Doris Liuc4bb1852016-02-19 21:39:21 +000072 ANDROID_API void reverse();
73 // Terminates the animation at its current progress.
74 ANDROID_API void cancel();
75
76 // Terminates the animation and skip to the end of the animation.
Doris Liu718cd3e2016-05-17 16:50:31 -070077 ANDROID_API virtual void end();
John Reck52244ff2014-05-01 21:27:37 -070078
John Reck8d8af3c2014-07-01 15:23:45 -070079 void attach(RenderNode* target);
80 virtual void onAttached() {}
Chris Craikd41c4d82015-01-05 15:51:13 -080081 void detach() { mTarget = nullptr; }
Doris Liu718cd3e2016-05-17 16:50:31 -070082 ANDROID_API void pushStaging(AnimationContext& context);
83 ANDROID_API bool animate(AnimationContext& context);
84
85 // Returns the remaining time in ms for the animation. Note this should only be called during
86 // an animation on RenderThread.
87 ANDROID_API nsecs_t getRemainingPlayTime();
John Reckff941dc2014-05-14 16:34:14 -070088
Doris Liuc4bb1852016-02-19 21:39:21 +000089 bool isRunning() { return mPlayState == PlayState::Running
90 || mPlayState == PlayState::Reversing; }
Chris Craikb9ce116d2015-08-20 15:14:06 -070091 bool isFinished() { return mPlayState == PlayState::Finished; }
John Reckff941dc2014-05-14 16:34:14 -070092 float finalValue() { return mFinalValue; }
John Reck52244ff2014-05-01 21:27:37 -070093
John Recka7c2ea22014-08-08 13:21:00 -070094 ANDROID_API virtual uint32_t dirtyMask() = 0;
John Reck22184722014-06-20 07:19:30 -070095
John Recke2478d42014-09-03 16:46:05 -070096 void forceEndNow(AnimationContext& context);
Doris Liuc4bb1852016-02-19 21:39:21 +000097 RenderNode* target() { return mTarget; }
Doris Liu8b083202016-02-19 21:46:06 +000098 RenderNode* stagingTarget() { return mStagingTarget; }
John Recke2478d42014-09-03 16:46:05 -070099
John Reck52244ff2014-05-01 21:27:37 -0700100protected:
Doris Liu766431a2016-02-04 22:17:11 +0000101 // PlayState is used by mStagingPlayState and mPlayState to track the state initiated from UI
102 // thread and Render Thread animation state, respectively.
103 // From the UI thread, mStagingPlayState transition looks like
Doris Liuc4bb1852016-02-19 21:39:21 +0000104 // NotStarted -> Running/Reversing -> Finished
105 // ^ |
106 // | |
107 // ----------------------
Doris Liu766431a2016-02-04 22:17:11 +0000108 // Note: For mStagingState, the Finished state (optional) is only set when the animation is
109 // terminated by user.
110 //
111 // On Render Thread, mPlayState transition:
Doris Liuc4bb1852016-02-19 21:39:21 +0000112 // NotStart -> Running/Reversing-> Finished
113 // ^ |
114 // | |
115 // ------------------
116 // Note that if the animation is in Running/Reversing state, calling start or reverse again
117 // would do nothing if the animation has the same play direction as the request; otherwise,
118 // the animation would start from where it is and change direction (i.e. Reversing <-> Running)
Doris Liu766431a2016-02-04 22:17:11 +0000119
Chris Craikb9ce116d2015-08-20 15:14:06 -0700120 enum class PlayState {
121 NotStarted,
122 Running,
Doris Liuc4bb1852016-02-19 21:39:21 +0000123 Reversing,
Chris Craikb9ce116d2015-08-20 15:14:06 -0700124 Finished,
125 };
126
Chih-Hung Hsiehf35c9392016-08-10 14:08:35 -0700127 explicit BaseRenderNodeAnimator(float finalValue);
John Reckff941dc2014-05-14 16:34:14 -0700128 virtual ~BaseRenderNodeAnimator();
John Reck52244ff2014-05-01 21:27:37 -0700129
John Reckff941dc2014-05-14 16:34:14 -0700130 virtual float getValue(RenderNode* target) const = 0;
131 virtual void setValue(RenderNode* target, float value) = 0;
John Reck52244ff2014-05-01 21:27:37 -0700132
John Reck119907c2014-08-14 09:02:01 -0700133 void callOnFinishedListener(AnimationContext& context);
John Reck52244ff2014-05-01 21:27:37 -0700134
John Reck8d8af3c2014-07-01 15:23:45 -0700135 virtual void onStagingPlayStateChanged() {}
Doris Liu766431a2016-02-04 22:17:11 +0000136 virtual void onPlayTimeChanged(nsecs_t playTime) {}
Doris Liu8b083202016-02-19 21:46:06 +0000137 virtual void onPushStaging() {}
John Reck8d8af3c2014-07-01 15:23:45 -0700138
John Reck8d8af3c2014-07-01 15:23:45 -0700139 RenderNode* mTarget;
Doris Liu8b083202016-02-19 21:46:06 +0000140 RenderNode* mStagingTarget;
John Reck8d8af3c2014-07-01 15:23:45 -0700141
John Reckff941dc2014-05-14 16:34:14 -0700142 float mFinalValue;
143 float mDeltaValue;
144 float mFromValue;
145
Chris Craik51d6a3d2014-12-22 17:16:56 -0800146 std::unique_ptr<Interpolator> mInterpolator;
John Reck68bfe0a2014-06-24 15:34:58 -0700147 PlayState mStagingPlayState;
John Reck52244ff2014-05-01 21:27:37 -0700148 PlayState mPlayState;
John Reck68bfe0a2014-06-24 15:34:58 -0700149 bool mHasStartValue;
Alan Viverettead2f8e32014-05-16 13:28:33 -0700150 nsecs_t mStartTime;
Alan Viverettead2f8e32014-05-16 13:28:33 -0700151 nsecs_t mDuration;
152 nsecs_t mStartDelay;
John Reckf5945a02014-09-05 15:57:47 -0700153 bool mMayRunAsync;
Doris Liuc4bb1852016-02-19 21:39:21 +0000154 // Play Time tracks the progress of animation, it should always be [0, mDuration], 0 being
155 // the beginning of the animation, will reach mDuration at the end of an animation.
156 nsecs_t mPlayTime;
John Reck52244ff2014-05-01 21:27:37 -0700157
Alan Viverettead2f8e32014-05-16 13:28:33 -0700158 sp<AnimationListener> mListener;
John Reck68bfe0a2014-06-24 15:34:58 -0700159
160private:
Doris Liuc4bb1852016-02-19 21:39:21 +0000161 enum class Request {
162 Start,
163 Reverse,
164 Reset,
165 Cancel,
166 End
167 };
Doris Liu6725d582016-08-04 13:20:17 -0700168
169 // Defines different actions upon finish.
170 enum class Action {
171 // For animations that got canceled or finished normally. no more action needs to be done.
172 None,
173 // For animations that get reset, the reset will happen in the next animation pulse.
174 Reset,
175 // For animations being ended, in the next animation pulse the animation will skip to end.
176 End
177 };
178
John Reck68bfe0a2014-06-24 15:34:58 -0700179 inline void checkMutable();
John Reck119907c2014-08-14 09:02:01 -0700180 virtual void transitionToRunning(AnimationContext& context);
John Reck8d8af3c2014-07-01 15:23:45 -0700181 void doSetStartValue(float value);
Doris Liuc4bb1852016-02-19 21:39:21 +0000182 bool updatePlayTime(nsecs_t playTime);
183 void resolveStagingRequest(Request request);
184
185 std::vector<Request> mStagingRequests;
Doris Liu6725d582016-08-04 13:20:17 -0700186 Action mPendingActionUponFinish = Action::None;
John Reck52244ff2014-05-01 21:27:37 -0700187};
188
John Reck52244ff2014-05-01 21:27:37 -0700189class RenderPropertyAnimator : public BaseRenderNodeAnimator {
190public:
John Recke45b1fd2014-04-15 09:50:16 -0700191 enum RenderProperty {
192 TRANSLATION_X = 0,
193 TRANSLATION_Y,
194 TRANSLATION_Z,
195 SCALE_X,
196 SCALE_Y,
197 ROTATION,
198 ROTATION_X,
199 ROTATION_Y,
200 X,
201 Y,
202 Z,
203 ALPHA,
204 };
205
John Reckff941dc2014-05-14 16:34:14 -0700206 ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue);
207
John Reck22184722014-06-20 07:19:30 -0700208 ANDROID_API virtual uint32_t dirtyMask();
209
John Recke45b1fd2014-04-15 09:50:16 -0700210protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800211 virtual float getValue(RenderNode* target) const override;
212 virtual void setValue(RenderNode* target, float value) override;
213 virtual void onAttached() override;
214 virtual void onStagingPlayStateChanged() override;
Doris Liu8b083202016-02-19 21:46:06 +0000215 virtual void onPushStaging() override;
John Recke45b1fd2014-04-15 09:50:16 -0700216
217private:
John Reck79c7de72014-05-23 10:33:31 -0700218 typedef bool (RenderProperties::*SetFloatProperty)(float value);
John Reck52244ff2014-05-01 21:27:37 -0700219 typedef float (RenderProperties::*GetFloatProperty)() const;
220
John Reckff941dc2014-05-14 16:34:14 -0700221 struct PropertyAccessors;
222 const PropertyAccessors* mPropertyAccess;
John Reck52244ff2014-05-01 21:27:37 -0700223
224 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[];
Doris Liu8b083202016-02-19 21:46:06 +0000225 bool mShouldSyncPropertyFields = false;
226 bool mShouldUpdateStagingProperties = false;
John Reck52244ff2014-05-01 21:27:37 -0700227};
228
229class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator {
230public:
231 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property,
John Reckff941dc2014-05-14 16:34:14 -0700232 float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700233
234 ANDROID_API virtual uint32_t dirtyMask();
235
John Reck52244ff2014-05-01 21:27:37 -0700236protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800237 virtual float getValue(RenderNode* target) const override;
238 virtual void setValue(RenderNode* target, float value) override;
John Reck52244ff2014-05-01 21:27:37 -0700239private:
240 sp<CanvasPropertyPrimitive> mProperty;
241};
242
243class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator {
244public:
245 enum PaintField {
246 STROKE_WIDTH = 0,
247 ALPHA,
248 };
249
250 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property,
John Reckff941dc2014-05-14 16:34:14 -0700251 PaintField field, float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700252
253 ANDROID_API virtual uint32_t dirtyMask();
254
John Reck52244ff2014-05-01 21:27:37 -0700255protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800256 virtual float getValue(RenderNode* target) const override;
257 virtual void setValue(RenderNode* target, float value) override;
John Reck52244ff2014-05-01 21:27:37 -0700258private:
259 sp<CanvasPropertyPaint> mProperty;
260 PaintField mField;
John Recke45b1fd2014-04-15 09:50:16 -0700261};
262
John Reckd3de42c2014-07-15 14:29:33 -0700263class RevealAnimator : public BaseRenderNodeAnimator {
264public:
Chris Craikaf4d04c2014-07-29 12:50:14 -0700265 ANDROID_API RevealAnimator(int centerX, int centerY,
John Reckd3de42c2014-07-15 14:29:33 -0700266 float startValue, float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700267
268 ANDROID_API virtual uint32_t dirtyMask();
269
John Reckd3de42c2014-07-15 14:29:33 -0700270protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800271 virtual float getValue(RenderNode* target) const override;
272 virtual void setValue(RenderNode* target, float value) override;
John Reckd3de42c2014-07-15 14:29:33 -0700273
274private:
275 int mCenterX, mCenterY;
John Reckd3de42c2014-07-15 14:29:33 -0700276};
277
John Recke45b1fd2014-04-15 09:50:16 -0700278} /* namespace uirenderer */
279} /* namespace android */
280
281#endif /* ANIMATOR_H */