blob: fcbc11b0306c75cbcf4e73e10ca511fd75c5702a [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();
65 ANDROID_API void reset();
66 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.
71 ANDROID_API 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; }
John Reck119907c2014-08-14 09:02:01 -070076 void pushStaging(AnimationContext& context);
77 bool animate(AnimationContext& context);
John Reckff941dc2014-05-14 16:34:14 -070078
Doris Liuc4bb1852016-02-19 21:39:21 +000079 bool isRunning() { return mPlayState == PlayState::Running
80 || mPlayState == PlayState::Reversing; }
Chris Craikb9ce116d2015-08-20 15:14:06 -070081 bool isFinished() { return mPlayState == PlayState::Finished; }
John Reckff941dc2014-05-14 16:34:14 -070082 float finalValue() { return mFinalValue; }
John Reck52244ff2014-05-01 21:27:37 -070083
John Recka7c2ea22014-08-08 13:21:00 -070084 ANDROID_API virtual uint32_t dirtyMask() = 0;
John Reck22184722014-06-20 07:19:30 -070085
John Recke2478d42014-09-03 16:46:05 -070086 void forceEndNow(AnimationContext& context);
Doris Liuc4bb1852016-02-19 21:39:21 +000087 RenderNode* target() { return mTarget; }
John Recke2478d42014-09-03 16:46:05 -070088
John Reck52244ff2014-05-01 21:27:37 -070089protected:
Doris Liu766431a2016-02-04 22:17:11 +000090 // PlayState is used by mStagingPlayState and mPlayState to track the state initiated from UI
91 // thread and Render Thread animation state, respectively.
92 // From the UI thread, mStagingPlayState transition looks like
Doris Liuc4bb1852016-02-19 21:39:21 +000093 // NotStarted -> Running/Reversing -> Finished
94 // ^ |
95 // | |
96 // ----------------------
Doris Liu766431a2016-02-04 22:17:11 +000097 // Note: For mStagingState, the Finished state (optional) is only set when the animation is
98 // terminated by user.
99 //
100 // On Render Thread, mPlayState transition:
Doris Liuc4bb1852016-02-19 21:39:21 +0000101 // NotStart -> Running/Reversing-> Finished
102 // ^ |
103 // | |
104 // ------------------
105 // Note that if the animation is in Running/Reversing state, calling start or reverse again
106 // would do nothing if the animation has the same play direction as the request; otherwise,
107 // the animation would start from where it is and change direction (i.e. Reversing <-> Running)
Doris Liu766431a2016-02-04 22:17:11 +0000108
Chris Craikb9ce116d2015-08-20 15:14:06 -0700109 enum class PlayState {
110 NotStarted,
111 Running,
Doris Liuc4bb1852016-02-19 21:39:21 +0000112 Reversing,
Chris Craikb9ce116d2015-08-20 15:14:06 -0700113 Finished,
114 };
115
John Reckff941dc2014-05-14 16:34:14 -0700116 BaseRenderNodeAnimator(float finalValue);
117 virtual ~BaseRenderNodeAnimator();
John Reck52244ff2014-05-01 21:27:37 -0700118
John Reckff941dc2014-05-14 16:34:14 -0700119 virtual float getValue(RenderNode* target) const = 0;
120 virtual void setValue(RenderNode* target, float value) = 0;
John Reck52244ff2014-05-01 21:27:37 -0700121
John Reck119907c2014-08-14 09:02:01 -0700122 void callOnFinishedListener(AnimationContext& context);
John Reck52244ff2014-05-01 21:27:37 -0700123
John Reck8d8af3c2014-07-01 15:23:45 -0700124 virtual void onStagingPlayStateChanged() {}
Doris Liu766431a2016-02-04 22:17:11 +0000125 virtual void onPlayTimeChanged(nsecs_t playTime) {}
John Reck8d8af3c2014-07-01 15:23:45 -0700126
John Reck8d8af3c2014-07-01 15:23:45 -0700127 RenderNode* mTarget;
128
John Reckff941dc2014-05-14 16:34:14 -0700129 float mFinalValue;
130 float mDeltaValue;
131 float mFromValue;
132
Chris Craik51d6a3d2014-12-22 17:16:56 -0800133 std::unique_ptr<Interpolator> mInterpolator;
John Reck68bfe0a2014-06-24 15:34:58 -0700134 PlayState mStagingPlayState;
John Reck52244ff2014-05-01 21:27:37 -0700135 PlayState mPlayState;
John Reck68bfe0a2014-06-24 15:34:58 -0700136 bool mHasStartValue;
Alan Viverettead2f8e32014-05-16 13:28:33 -0700137 nsecs_t mStartTime;
Alan Viverettead2f8e32014-05-16 13:28:33 -0700138 nsecs_t mDuration;
139 nsecs_t mStartDelay;
John Reckf5945a02014-09-05 15:57:47 -0700140 bool mMayRunAsync;
Doris Liuc4bb1852016-02-19 21:39:21 +0000141 // Play Time tracks the progress of animation, it should always be [0, mDuration], 0 being
142 // the beginning of the animation, will reach mDuration at the end of an animation.
143 nsecs_t mPlayTime;
John Reck52244ff2014-05-01 21:27:37 -0700144
Alan Viverettead2f8e32014-05-16 13:28:33 -0700145 sp<AnimationListener> mListener;
John Reck68bfe0a2014-06-24 15:34:58 -0700146
147private:
Doris Liuc4bb1852016-02-19 21:39:21 +0000148 enum class Request {
149 Start,
150 Reverse,
151 Reset,
152 Cancel,
153 End
154 };
John Reck68bfe0a2014-06-24 15:34:58 -0700155 inline void checkMutable();
John Reck119907c2014-08-14 09:02:01 -0700156 virtual void transitionToRunning(AnimationContext& context);
John Reck8d8af3c2014-07-01 15:23:45 -0700157 void doSetStartValue(float value);
Doris Liuc4bb1852016-02-19 21:39:21 +0000158 bool updatePlayTime(nsecs_t playTime);
159 void resolveStagingRequest(Request request);
160
161 std::vector<Request> mStagingRequests;
162
John Reck52244ff2014-05-01 21:27:37 -0700163};
164
John Reck52244ff2014-05-01 21:27:37 -0700165class RenderPropertyAnimator : public BaseRenderNodeAnimator {
166public:
John Recke45b1fd2014-04-15 09:50:16 -0700167 enum RenderProperty {
168 TRANSLATION_X = 0,
169 TRANSLATION_Y,
170 TRANSLATION_Z,
171 SCALE_X,
172 SCALE_Y,
173 ROTATION,
174 ROTATION_X,
175 ROTATION_Y,
176 X,
177 Y,
178 Z,
179 ALPHA,
180 };
181
John Reckff941dc2014-05-14 16:34:14 -0700182 ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue);
183
John Reck22184722014-06-20 07:19:30 -0700184 ANDROID_API virtual uint32_t dirtyMask();
185
John Recke45b1fd2014-04-15 09:50:16 -0700186protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800187 virtual float getValue(RenderNode* target) const override;
188 virtual void setValue(RenderNode* target, float value) override;
189 virtual void onAttached() override;
190 virtual void onStagingPlayStateChanged() override;
John Recke45b1fd2014-04-15 09:50:16 -0700191
192private:
John Reck79c7de72014-05-23 10:33:31 -0700193 typedef bool (RenderProperties::*SetFloatProperty)(float value);
John Reck52244ff2014-05-01 21:27:37 -0700194 typedef float (RenderProperties::*GetFloatProperty)() const;
195
John Reckff941dc2014-05-14 16:34:14 -0700196 struct PropertyAccessors;
197 const PropertyAccessors* mPropertyAccess;
John Reck52244ff2014-05-01 21:27:37 -0700198
199 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[];
200};
201
202class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator {
203public:
204 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property,
John Reckff941dc2014-05-14 16:34:14 -0700205 float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700206
207 ANDROID_API virtual uint32_t dirtyMask();
208
John Reck52244ff2014-05-01 21:27:37 -0700209protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800210 virtual float getValue(RenderNode* target) const override;
211 virtual void setValue(RenderNode* target, float value) override;
John Reck52244ff2014-05-01 21:27:37 -0700212private:
213 sp<CanvasPropertyPrimitive> mProperty;
214};
215
216class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator {
217public:
218 enum PaintField {
219 STROKE_WIDTH = 0,
220 ALPHA,
221 };
222
223 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property,
John Reckff941dc2014-05-14 16:34:14 -0700224 PaintField field, float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700225
226 ANDROID_API virtual uint32_t dirtyMask();
227
John Reck52244ff2014-05-01 21:27:37 -0700228protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800229 virtual float getValue(RenderNode* target) const override;
230 virtual void setValue(RenderNode* target, float value) override;
John Reck52244ff2014-05-01 21:27:37 -0700231private:
232 sp<CanvasPropertyPaint> mProperty;
233 PaintField mField;
John Recke45b1fd2014-04-15 09:50:16 -0700234};
235
John Reckd3de42c2014-07-15 14:29:33 -0700236class RevealAnimator : public BaseRenderNodeAnimator {
237public:
Chris Craikaf4d04c2014-07-29 12:50:14 -0700238 ANDROID_API RevealAnimator(int centerX, int centerY,
John Reckd3de42c2014-07-15 14:29:33 -0700239 float startValue, float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700240
241 ANDROID_API virtual uint32_t dirtyMask();
242
John Reckd3de42c2014-07-15 14:29:33 -0700243protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800244 virtual float getValue(RenderNode* target) const override;
245 virtual void setValue(RenderNode* target, float value) override;
John Reckd3de42c2014-07-15 14:29:33 -0700246
247private:
248 int mCenterX, mCenterY;
John Reckd3de42c2014-07-15 14:29:33 -0700249};
250
John Recke45b1fd2014-04-15 09:50:16 -0700251} /* namespace uirenderer */
252} /* namespace android */
253
254#endif /* ANIMATOR_H */