blob: 2c9c9c3fe0f9d60a766d4341fb26c92482804743 [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
27namespace android {
28namespace uirenderer {
29
John Reck119907c2014-08-14 09:02:01 -070030class AnimationContext;
31class BaseRenderNodeAnimator;
Tom Hudson2dc236b2014-10-15 15:46:42 -040032class CanvasPropertyPrimitive;
33class CanvasPropertyPaint;
34class Interpolator;
John Reck52244ff2014-05-01 21:27:37 -070035class RenderNode;
John Recke45b1fd2014-04-15 09:50:16 -070036class RenderProperties;
John Recke45b1fd2014-04-15 09:50:16 -070037
John Reck52244ff2014-05-01 21:27:37 -070038class AnimationListener : public VirtualLightRefBase {
39public:
John Reckff941dc2014-05-14 16:34:14 -070040 ANDROID_API virtual void onAnimationFinished(BaseRenderNodeAnimator*) = 0;
John Reck52244ff2014-05-01 21:27:37 -070041protected:
42 ANDROID_API virtual ~AnimationListener() {}
43};
44
John Reckff941dc2014-05-14 16:34:14 -070045class BaseRenderNodeAnimator : public VirtualLightRefBase {
46 PREVENT_COPY_AND_ASSIGN(BaseRenderNodeAnimator);
John Reck52244ff2014-05-01 21:27:37 -070047public:
John Reckc6b32642014-06-02 11:00:09 -070048 ANDROID_API void setStartValue(float value);
John Reck52244ff2014-05-01 21:27:37 -070049 ANDROID_API void setInterpolator(Interpolator* interpolator);
50 ANDROID_API void setDuration(nsecs_t durationInMs);
John Reck315c3292014-05-09 19:21:04 -070051 ANDROID_API nsecs_t duration() { return mDuration; }
Alan Viverettead2f8e32014-05-16 13:28:33 -070052 ANDROID_API void setStartDelay(nsecs_t startDelayInMs);
53 ANDROID_API nsecs_t startDelay() { return mStartDelay; }
John Reck52244ff2014-05-01 21:27:37 -070054 ANDROID_API void setListener(AnimationListener* listener) {
55 mListener = listener;
56 }
John Reck119907c2014-08-14 09:02:01 -070057 AnimationListener* listener() { return mListener.get(); }
John Reckf5945a02014-09-05 15:57:47 -070058 ANDROID_API void setAllowRunningAsync(bool mayRunAsync) {
59 mMayRunAsync = mayRunAsync;
60 }
61 bool mayRunAsync() { return mMayRunAsync; }
Doris Liuf276acd2016-01-07 13:49:26 -080062 ANDROID_API void start() {
63 if (mStagingPlayState == PlayState::NotStarted) {
64 mStagingPlayState = PlayState::Running;
65 } else {
66 mStagingPlayState = PlayState::Restarted;
67 }
68 onStagingPlayStateChanged(); }
Chris Craikb9ce116d2015-08-20 15:14:06 -070069 ANDROID_API void end() { mStagingPlayState = PlayState::Finished; onStagingPlayStateChanged(); }
John Reck52244ff2014-05-01 21:27:37 -070070
John Reck8d8af3c2014-07-01 15:23:45 -070071 void attach(RenderNode* target);
72 virtual void onAttached() {}
Chris Craikd41c4d82015-01-05 15:51:13 -080073 void detach() { mTarget = nullptr; }
John Reck119907c2014-08-14 09:02:01 -070074 void pushStaging(AnimationContext& context);
75 bool animate(AnimationContext& context);
John Reckff941dc2014-05-14 16:34:14 -070076
Chris Craikb9ce116d2015-08-20 15:14:06 -070077 bool isRunning() { return mPlayState == PlayState::Running; }
78 bool isFinished() { return mPlayState == PlayState::Finished; }
John Reckff941dc2014-05-14 16:34:14 -070079 float finalValue() { return mFinalValue; }
John Reck52244ff2014-05-01 21:27:37 -070080
John Recka7c2ea22014-08-08 13:21:00 -070081 ANDROID_API virtual uint32_t dirtyMask() = 0;
John Reck22184722014-06-20 07:19:30 -070082
John Recke2478d42014-09-03 16:46:05 -070083 void forceEndNow(AnimationContext& context);
84
John Reck52244ff2014-05-01 21:27:37 -070085protected:
Doris Liuf276acd2016-01-07 13:49:26 -080086 // PlayState is used by mStagingPlayState and mPlayState to track the state initiated from UI
87 // thread and Render Thread animation state, respectively.
88 // From the UI thread, mStagingPlayState transition looks like
89 // NotStarted -> Running -> Finished
90 // ^ |
91 // | |
92 // Restarted <------
93 // Note: For mStagingState, the Finished state (optional) is only set when the animation is
94 // terminated by user.
95 //
96 // On Render Thread, mPlayState transition:
97 // NotStart -> Running -> Finished
98 // ^ |
99 // | |
100 // -------------
101
Chris Craikb9ce116d2015-08-20 15:14:06 -0700102 enum class PlayState {
103 NotStarted,
104 Running,
105 Finished,
Doris Liuf276acd2016-01-07 13:49:26 -0800106 Restarted,
Chris Craikb9ce116d2015-08-20 15:14:06 -0700107 };
108
John Reckff941dc2014-05-14 16:34:14 -0700109 BaseRenderNodeAnimator(float finalValue);
110 virtual ~BaseRenderNodeAnimator();
John Reck52244ff2014-05-01 21:27:37 -0700111
John Reckff941dc2014-05-14 16:34:14 -0700112 virtual float getValue(RenderNode* target) const = 0;
113 virtual void setValue(RenderNode* target, float value) = 0;
John Reck8d8af3c2014-07-01 15:23:45 -0700114 RenderNode* target() { return mTarget; }
John Reck52244ff2014-05-01 21:27:37 -0700115
John Reck119907c2014-08-14 09:02:01 -0700116 void callOnFinishedListener(AnimationContext& context);
John Reck52244ff2014-05-01 21:27:37 -0700117
John Reck8d8af3c2014-07-01 15:23:45 -0700118 virtual void onStagingPlayStateChanged() {}
Doris Liuf276acd2016-01-07 13:49:26 -0800119 virtual void onPlayTimeChanged(nsecs_t playTime) {}
John Reck8d8af3c2014-07-01 15:23:45 -0700120
John Reck8d8af3c2014-07-01 15:23:45 -0700121 RenderNode* mTarget;
122
John Reckff941dc2014-05-14 16:34:14 -0700123 float mFinalValue;
124 float mDeltaValue;
125 float mFromValue;
126
Chris Craik51d6a3d2014-12-22 17:16:56 -0800127 std::unique_ptr<Interpolator> mInterpolator;
John Reck68bfe0a2014-06-24 15:34:58 -0700128 PlayState mStagingPlayState;
John Reck52244ff2014-05-01 21:27:37 -0700129 PlayState mPlayState;
John Reck68bfe0a2014-06-24 15:34:58 -0700130 bool mHasStartValue;
Alan Viverettead2f8e32014-05-16 13:28:33 -0700131 nsecs_t mStartTime;
Alan Viverettead2f8e32014-05-16 13:28:33 -0700132 nsecs_t mDuration;
133 nsecs_t mStartDelay;
John Reckf5945a02014-09-05 15:57:47 -0700134 bool mMayRunAsync;
John Reck52244ff2014-05-01 21:27:37 -0700135
Alan Viverettead2f8e32014-05-16 13:28:33 -0700136 sp<AnimationListener> mListener;
John Reck68bfe0a2014-06-24 15:34:58 -0700137
138private:
John Reck68bfe0a2014-06-24 15:34:58 -0700139 inline void checkMutable();
John Reck119907c2014-08-14 09:02:01 -0700140 virtual void transitionToRunning(AnimationContext& context);
John Reck8d8af3c2014-07-01 15:23:45 -0700141 void doSetStartValue(float value);
John Reck52244ff2014-05-01 21:27:37 -0700142};
143
John Reck52244ff2014-05-01 21:27:37 -0700144class RenderPropertyAnimator : public BaseRenderNodeAnimator {
145public:
John Recke45b1fd2014-04-15 09:50:16 -0700146 enum RenderProperty {
147 TRANSLATION_X = 0,
148 TRANSLATION_Y,
149 TRANSLATION_Z,
150 SCALE_X,
151 SCALE_Y,
152 ROTATION,
153 ROTATION_X,
154 ROTATION_Y,
155 X,
156 Y,
157 Z,
158 ALPHA,
159 };
160
John Reckff941dc2014-05-14 16:34:14 -0700161 ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue);
162
John Reck22184722014-06-20 07:19:30 -0700163 ANDROID_API virtual uint32_t dirtyMask();
164
John Recke45b1fd2014-04-15 09:50:16 -0700165protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800166 virtual float getValue(RenderNode* target) const override;
167 virtual void setValue(RenderNode* target, float value) override;
168 virtual void onAttached() override;
169 virtual void onStagingPlayStateChanged() override;
John Recke45b1fd2014-04-15 09:50:16 -0700170
171private:
John Reck79c7de72014-05-23 10:33:31 -0700172 typedef bool (RenderProperties::*SetFloatProperty)(float value);
John Reck52244ff2014-05-01 21:27:37 -0700173 typedef float (RenderProperties::*GetFloatProperty)() const;
174
John Reckff941dc2014-05-14 16:34:14 -0700175 struct PropertyAccessors;
176 const PropertyAccessors* mPropertyAccess;
John Reck52244ff2014-05-01 21:27:37 -0700177
178 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[];
179};
180
181class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator {
182public:
183 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property,
John Reckff941dc2014-05-14 16:34:14 -0700184 float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700185
186 ANDROID_API virtual uint32_t dirtyMask();
187
John Reck52244ff2014-05-01 21:27:37 -0700188protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800189 virtual float getValue(RenderNode* target) const override;
190 virtual void setValue(RenderNode* target, float value) override;
John Reck52244ff2014-05-01 21:27:37 -0700191private:
192 sp<CanvasPropertyPrimitive> mProperty;
193};
194
195class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator {
196public:
197 enum PaintField {
198 STROKE_WIDTH = 0,
199 ALPHA,
200 };
201
202 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property,
John Reckff941dc2014-05-14 16:34:14 -0700203 PaintField field, float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700204
205 ANDROID_API virtual uint32_t dirtyMask();
206
John Reck52244ff2014-05-01 21:27:37 -0700207protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800208 virtual float getValue(RenderNode* target) const override;
209 virtual void setValue(RenderNode* target, float value) override;
John Reck52244ff2014-05-01 21:27:37 -0700210private:
211 sp<CanvasPropertyPaint> mProperty;
212 PaintField mField;
John Recke45b1fd2014-04-15 09:50:16 -0700213};
214
John Reckd3de42c2014-07-15 14:29:33 -0700215class RevealAnimator : public BaseRenderNodeAnimator {
216public:
Chris Craikaf4d04c2014-07-29 12:50:14 -0700217 ANDROID_API RevealAnimator(int centerX, int centerY,
John Reckd3de42c2014-07-15 14:29:33 -0700218 float startValue, float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700219
220 ANDROID_API virtual uint32_t dirtyMask();
221
John Reckd3de42c2014-07-15 14:29:33 -0700222protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800223 virtual float getValue(RenderNode* target) const override;
224 virtual void setValue(RenderNode* target, float value) override;
John Reckd3de42c2014-07-15 14:29:33 -0700225
226private:
227 int mCenterX, mCenterY;
John Reckd3de42c2014-07-15 14:29:33 -0700228};
229
John Recke45b1fd2014-04-15 09:50:16 -0700230} /* namespace uirenderer */
231} /* namespace android */
232
233#endif /* ANIMATOR_H */