blob: 35a4a09c287f96ce81f15abbae0b35913c816a44 [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>
John Recke45b1fd2014-04-15 09:50:16 -070022
John Reck52244ff2014-05-01 21:27:37 -070023#include "CanvasProperty.h"
John Recke45b1fd2014-04-15 09:50:16 -070024#include "Interpolator.h"
25#include "TreeInfo.h"
John Reck52244ff2014-05-01 21:27:37 -070026#include "utils/Macros.h"
John Recke45b1fd2014-04-15 09:50:16 -070027
28namespace android {
29namespace uirenderer {
30
John Reck119907c2014-08-14 09:02:01 -070031class AnimationContext;
32class BaseRenderNodeAnimator;
John Reck52244ff2014-05-01 21:27:37 -070033class RenderNode;
John Recke45b1fd2014-04-15 09:50:16 -070034class RenderProperties;
John Recke45b1fd2014-04-15 09:50:16 -070035
John Reck52244ff2014-05-01 21:27:37 -070036class AnimationListener : public VirtualLightRefBase {
37public:
John Reckff941dc2014-05-14 16:34:14 -070038 ANDROID_API virtual void onAnimationFinished(BaseRenderNodeAnimator*) = 0;
John Reck52244ff2014-05-01 21:27:37 -070039protected:
40 ANDROID_API virtual ~AnimationListener() {}
41};
42
John Reckff941dc2014-05-14 16:34:14 -070043class BaseRenderNodeAnimator : public VirtualLightRefBase {
44 PREVENT_COPY_AND_ASSIGN(BaseRenderNodeAnimator);
John Reck52244ff2014-05-01 21:27:37 -070045public:
John Reckc6b32642014-06-02 11:00:09 -070046 ANDROID_API void setStartValue(float value);
John Reck52244ff2014-05-01 21:27:37 -070047 ANDROID_API void setInterpolator(Interpolator* interpolator);
48 ANDROID_API void setDuration(nsecs_t durationInMs);
John Reck315c3292014-05-09 19:21:04 -070049 ANDROID_API nsecs_t duration() { return mDuration; }
Alan Viverettead2f8e32014-05-16 13:28:33 -070050 ANDROID_API void setStartDelay(nsecs_t startDelayInMs);
51 ANDROID_API nsecs_t startDelay() { return mStartDelay; }
John Reck52244ff2014-05-01 21:27:37 -070052 ANDROID_API void setListener(AnimationListener* listener) {
53 mListener = listener;
54 }
John Reck119907c2014-08-14 09:02:01 -070055 AnimationListener* listener() { return mListener.get(); }
John Reckf5945a02014-09-05 15:57:47 -070056 ANDROID_API void setAllowRunningAsync(bool mayRunAsync) {
57 mMayRunAsync = mayRunAsync;
58 }
59 bool mayRunAsync() { return mMayRunAsync; }
John Reck8d8af3c2014-07-01 15:23:45 -070060 ANDROID_API void start() { mStagingPlayState = RUNNING; onStagingPlayStateChanged(); }
John Reckd3de42c2014-07-15 14:29:33 -070061 ANDROID_API void end() { mStagingPlayState = FINISHED; onStagingPlayStateChanged(); }
John Reck52244ff2014-05-01 21:27:37 -070062
John Reck8d8af3c2014-07-01 15:23:45 -070063 void attach(RenderNode* target);
64 virtual void onAttached() {}
65 void detach() { mTarget = 0; }
John Reck119907c2014-08-14 09:02:01 -070066 void pushStaging(AnimationContext& context);
67 bool animate(AnimationContext& context);
John Reckff941dc2014-05-14 16:34:14 -070068
John Reck119907c2014-08-14 09:02:01 -070069 bool isRunning() { return mPlayState == RUNNING; }
John Reck52244ff2014-05-01 21:27:37 -070070 bool isFinished() { return mPlayState == FINISHED; }
John Reckff941dc2014-05-14 16:34:14 -070071 float finalValue() { return mFinalValue; }
John Reck52244ff2014-05-01 21:27:37 -070072
John Recka7c2ea22014-08-08 13:21:00 -070073 ANDROID_API virtual uint32_t dirtyMask() = 0;
John Reck22184722014-06-20 07:19:30 -070074
John Recke2478d42014-09-03 16:46:05 -070075 void forceEndNow(AnimationContext& context);
76
John Reck52244ff2014-05-01 21:27:37 -070077protected:
John Reckff941dc2014-05-14 16:34:14 -070078 BaseRenderNodeAnimator(float finalValue);
79 virtual ~BaseRenderNodeAnimator();
John Reck52244ff2014-05-01 21:27:37 -070080
John Reckff941dc2014-05-14 16:34:14 -070081 virtual float getValue(RenderNode* target) const = 0;
82 virtual void setValue(RenderNode* target, float value) = 0;
John Reck8d8af3c2014-07-01 15:23:45 -070083 RenderNode* target() { return mTarget; }
John Reck52244ff2014-05-01 21:27:37 -070084
John Reck119907c2014-08-14 09:02:01 -070085 void callOnFinishedListener(AnimationContext& context);
John Reck52244ff2014-05-01 21:27:37 -070086
John Reck8d8af3c2014-07-01 15:23:45 -070087 virtual void onStagingPlayStateChanged() {}
88
John Reck52244ff2014-05-01 21:27:37 -070089 enum PlayState {
John Reck68bfe0a2014-06-24 15:34:58 -070090 NOT_STARTED,
John Reck52244ff2014-05-01 21:27:37 -070091 RUNNING,
92 FINISHED,
93 };
94
John Reck8d8af3c2014-07-01 15:23:45 -070095 RenderNode* mTarget;
96
John Reckff941dc2014-05-14 16:34:14 -070097 float mFinalValue;
98 float mDeltaValue;
99 float mFromValue;
100
John Reck52244ff2014-05-01 21:27:37 -0700101 Interpolator* mInterpolator;
John Reck68bfe0a2014-06-24 15:34:58 -0700102 PlayState mStagingPlayState;
John Reck52244ff2014-05-01 21:27:37 -0700103 PlayState mPlayState;
John Reck68bfe0a2014-06-24 15:34:58 -0700104 bool mHasStartValue;
Alan Viverettead2f8e32014-05-16 13:28:33 -0700105 nsecs_t mStartTime;
Alan Viverettead2f8e32014-05-16 13:28:33 -0700106 nsecs_t mDuration;
107 nsecs_t mStartDelay;
John Reckf5945a02014-09-05 15:57:47 -0700108 bool mMayRunAsync;
John Reck52244ff2014-05-01 21:27:37 -0700109
Alan Viverettead2f8e32014-05-16 13:28:33 -0700110 sp<AnimationListener> mListener;
John Reck68bfe0a2014-06-24 15:34:58 -0700111
112private:
John Reck68bfe0a2014-06-24 15:34:58 -0700113 inline void checkMutable();
John Reck119907c2014-08-14 09:02:01 -0700114 virtual void transitionToRunning(AnimationContext& context);
John Reck8d8af3c2014-07-01 15:23:45 -0700115 void doSetStartValue(float value);
John Reck52244ff2014-05-01 21:27:37 -0700116};
117
John Reck52244ff2014-05-01 21:27:37 -0700118class RenderPropertyAnimator : public BaseRenderNodeAnimator {
119public:
John Recke45b1fd2014-04-15 09:50:16 -0700120 enum RenderProperty {
121 TRANSLATION_X = 0,
122 TRANSLATION_Y,
123 TRANSLATION_Z,
124 SCALE_X,
125 SCALE_Y,
126 ROTATION,
127 ROTATION_X,
128 ROTATION_Y,
129 X,
130 Y,
131 Z,
132 ALPHA,
133 };
134
John Reckff941dc2014-05-14 16:34:14 -0700135 ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue);
136
John Reck22184722014-06-20 07:19:30 -0700137 ANDROID_API virtual uint32_t dirtyMask();
138
John Recke45b1fd2014-04-15 09:50:16 -0700139protected:
John Reckff941dc2014-05-14 16:34:14 -0700140 virtual float getValue(RenderNode* target) const;
141 virtual void setValue(RenderNode* target, float value);
John Reck8d8af3c2014-07-01 15:23:45 -0700142 virtual void onAttached();
143 virtual void onStagingPlayStateChanged();
John Recke45b1fd2014-04-15 09:50:16 -0700144
145private:
John Reck79c7de72014-05-23 10:33:31 -0700146 typedef bool (RenderProperties::*SetFloatProperty)(float value);
John Reck52244ff2014-05-01 21:27:37 -0700147 typedef float (RenderProperties::*GetFloatProperty)() const;
148
John Reckff941dc2014-05-14 16:34:14 -0700149 struct PropertyAccessors;
150 const PropertyAccessors* mPropertyAccess;
John Reck52244ff2014-05-01 21:27:37 -0700151
152 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[];
153};
154
155class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator {
156public:
157 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property,
John Reckff941dc2014-05-14 16:34:14 -0700158 float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700159
160 ANDROID_API virtual uint32_t dirtyMask();
161
John Reck52244ff2014-05-01 21:27:37 -0700162protected:
John Reckff941dc2014-05-14 16:34:14 -0700163 virtual float getValue(RenderNode* target) const;
164 virtual void setValue(RenderNode* target, float value);
John Reck52244ff2014-05-01 21:27:37 -0700165private:
166 sp<CanvasPropertyPrimitive> mProperty;
167};
168
169class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator {
170public:
171 enum PaintField {
172 STROKE_WIDTH = 0,
173 ALPHA,
174 };
175
176 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property,
John Reckff941dc2014-05-14 16:34:14 -0700177 PaintField field, float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700178
179 ANDROID_API virtual uint32_t dirtyMask();
180
John Reck52244ff2014-05-01 21:27:37 -0700181protected:
John Reckff941dc2014-05-14 16:34:14 -0700182 virtual float getValue(RenderNode* target) const;
183 virtual void setValue(RenderNode* target, float value);
John Reck52244ff2014-05-01 21:27:37 -0700184private:
185 sp<CanvasPropertyPaint> mProperty;
186 PaintField mField;
John Recke45b1fd2014-04-15 09:50:16 -0700187};
188
John Reckd3de42c2014-07-15 14:29:33 -0700189class RevealAnimator : public BaseRenderNodeAnimator {
190public:
Chris Craikaf4d04c2014-07-29 12:50:14 -0700191 ANDROID_API RevealAnimator(int centerX, int centerY,
John Reckd3de42c2014-07-15 14:29:33 -0700192 float startValue, float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700193
194 ANDROID_API virtual uint32_t dirtyMask();
195
John Reckd3de42c2014-07-15 14:29:33 -0700196protected:
197 virtual float getValue(RenderNode* target) const;
198 virtual void setValue(RenderNode* target, float value);
199
200private:
201 int mCenterX, mCenterY;
John Reckd3de42c2014-07-15 14:29:33 -0700202};
203
John Recke45b1fd2014-04-15 09:50:16 -0700204} /* namespace uirenderer */
205} /* namespace android */
206
207#endif /* ANIMATOR_H */