blob: 99f89560dad67a0043a9470489c94b1c7d54187a [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>
Tom Hudson2dc236b2014-10-15 15:46:42 -040022#include <utils/Timers.h>
John Recke45b1fd2014-04-15 09:50:16 -070023
John Reck52244ff2014-05-01 21:27:37 -070024#include "utils/Macros.h"
John Recke45b1fd2014-04-15 09:50:16 -070025
26namespace android {
27namespace uirenderer {
28
John Reck119907c2014-08-14 09:02:01 -070029class AnimationContext;
30class BaseRenderNodeAnimator;
Tom Hudson2dc236b2014-10-15 15:46:42 -040031class CanvasPropertyPrimitive;
32class CanvasPropertyPaint;
33class Interpolator;
John Reck52244ff2014-05-01 21:27:37 -070034class RenderNode;
John Recke45b1fd2014-04-15 09:50:16 -070035class RenderProperties;
John Recke45b1fd2014-04-15 09:50:16 -070036
John Reck52244ff2014-05-01 21:27:37 -070037class AnimationListener : public VirtualLightRefBase {
38public:
John Reckff941dc2014-05-14 16:34:14 -070039 ANDROID_API virtual void onAnimationFinished(BaseRenderNodeAnimator*) = 0;
John Reck52244ff2014-05-01 21:27:37 -070040protected:
41 ANDROID_API virtual ~AnimationListener() {}
42};
43
John Reckff941dc2014-05-14 16:34:14 -070044class BaseRenderNodeAnimator : public VirtualLightRefBase {
45 PREVENT_COPY_AND_ASSIGN(BaseRenderNodeAnimator);
John Reck52244ff2014-05-01 21:27:37 -070046public:
John Reckc6b32642014-06-02 11:00:09 -070047 ANDROID_API void setStartValue(float value);
John Reck52244ff2014-05-01 21:27:37 -070048 ANDROID_API void setInterpolator(Interpolator* interpolator);
49 ANDROID_API void setDuration(nsecs_t durationInMs);
John Reck315c3292014-05-09 19:21:04 -070050 ANDROID_API nsecs_t duration() { return mDuration; }
Alan Viverettead2f8e32014-05-16 13:28:33 -070051 ANDROID_API void setStartDelay(nsecs_t startDelayInMs);
52 ANDROID_API nsecs_t startDelay() { return mStartDelay; }
John Reck52244ff2014-05-01 21:27:37 -070053 ANDROID_API void setListener(AnimationListener* listener) {
54 mListener = listener;
55 }
John Reck119907c2014-08-14 09:02:01 -070056 AnimationListener* listener() { return mListener.get(); }
John Reckf5945a02014-09-05 15:57:47 -070057 ANDROID_API void setAllowRunningAsync(bool mayRunAsync) {
58 mMayRunAsync = mayRunAsync;
59 }
60 bool mayRunAsync() { return mMayRunAsync; }
John Reck8d8af3c2014-07-01 15:23:45 -070061 ANDROID_API void start() { mStagingPlayState = RUNNING; onStagingPlayStateChanged(); }
John Reckd3de42c2014-07-15 14:29:33 -070062 ANDROID_API void end() { mStagingPlayState = FINISHED; onStagingPlayStateChanged(); }
John Reck52244ff2014-05-01 21:27:37 -070063
John Reck8d8af3c2014-07-01 15:23:45 -070064 void attach(RenderNode* target);
65 virtual void onAttached() {}
66 void detach() { mTarget = 0; }
John Reck119907c2014-08-14 09:02:01 -070067 void pushStaging(AnimationContext& context);
68 bool animate(AnimationContext& context);
John Reckff941dc2014-05-14 16:34:14 -070069
John Reck119907c2014-08-14 09:02:01 -070070 bool isRunning() { return mPlayState == RUNNING; }
John Reck52244ff2014-05-01 21:27:37 -070071 bool isFinished() { return mPlayState == FINISHED; }
John Reckff941dc2014-05-14 16:34:14 -070072 float finalValue() { return mFinalValue; }
John Reck52244ff2014-05-01 21:27:37 -070073
John Recka7c2ea22014-08-08 13:21:00 -070074 ANDROID_API virtual uint32_t dirtyMask() = 0;
John Reck22184722014-06-20 07:19:30 -070075
John Recke2478d42014-09-03 16:46:05 -070076 void forceEndNow(AnimationContext& context);
77
John Reck52244ff2014-05-01 21:27:37 -070078protected:
John Reckff941dc2014-05-14 16:34:14 -070079 BaseRenderNodeAnimator(float finalValue);
80 virtual ~BaseRenderNodeAnimator();
John Reck52244ff2014-05-01 21:27:37 -070081
John Reckff941dc2014-05-14 16:34:14 -070082 virtual float getValue(RenderNode* target) const = 0;
83 virtual void setValue(RenderNode* target, float value) = 0;
John Reck8d8af3c2014-07-01 15:23:45 -070084 RenderNode* target() { return mTarget; }
John Reck52244ff2014-05-01 21:27:37 -070085
John Reck119907c2014-08-14 09:02:01 -070086 void callOnFinishedListener(AnimationContext& context);
John Reck52244ff2014-05-01 21:27:37 -070087
John Reck8d8af3c2014-07-01 15:23:45 -070088 virtual void onStagingPlayStateChanged() {}
89
John Reck52244ff2014-05-01 21:27:37 -070090 enum PlayState {
John Reck68bfe0a2014-06-24 15:34:58 -070091 NOT_STARTED,
John Reck52244ff2014-05-01 21:27:37 -070092 RUNNING,
93 FINISHED,
94 };
95
John Reck8d8af3c2014-07-01 15:23:45 -070096 RenderNode* mTarget;
97
John Reckff941dc2014-05-14 16:34:14 -070098 float mFinalValue;
99 float mDeltaValue;
100 float mFromValue;
101
John Reck52244ff2014-05-01 21:27:37 -0700102 Interpolator* mInterpolator;
John Reck68bfe0a2014-06-24 15:34:58 -0700103 PlayState mStagingPlayState;
John Reck52244ff2014-05-01 21:27:37 -0700104 PlayState mPlayState;
John Reck68bfe0a2014-06-24 15:34:58 -0700105 bool mHasStartValue;
Alan Viverettead2f8e32014-05-16 13:28:33 -0700106 nsecs_t mStartTime;
Alan Viverettead2f8e32014-05-16 13:28:33 -0700107 nsecs_t mDuration;
108 nsecs_t mStartDelay;
John Reckf5945a02014-09-05 15:57:47 -0700109 bool mMayRunAsync;
John Reck52244ff2014-05-01 21:27:37 -0700110
Alan Viverettead2f8e32014-05-16 13:28:33 -0700111 sp<AnimationListener> mListener;
John Reck68bfe0a2014-06-24 15:34:58 -0700112
113private:
John Reck68bfe0a2014-06-24 15:34:58 -0700114 inline void checkMutable();
John Reck119907c2014-08-14 09:02:01 -0700115 virtual void transitionToRunning(AnimationContext& context);
John Reck8d8af3c2014-07-01 15:23:45 -0700116 void doSetStartValue(float value);
John Reck52244ff2014-05-01 21:27:37 -0700117};
118
John Reck52244ff2014-05-01 21:27:37 -0700119class RenderPropertyAnimator : public BaseRenderNodeAnimator {
120public:
John Recke45b1fd2014-04-15 09:50:16 -0700121 enum RenderProperty {
122 TRANSLATION_X = 0,
123 TRANSLATION_Y,
124 TRANSLATION_Z,
125 SCALE_X,
126 SCALE_Y,
127 ROTATION,
128 ROTATION_X,
129 ROTATION_Y,
130 X,
131 Y,
132 Z,
133 ALPHA,
134 };
135
John Reckff941dc2014-05-14 16:34:14 -0700136 ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue);
137
John Reck22184722014-06-20 07:19:30 -0700138 ANDROID_API virtual uint32_t dirtyMask();
139
John Recke45b1fd2014-04-15 09:50:16 -0700140protected:
John Reckff941dc2014-05-14 16:34:14 -0700141 virtual float getValue(RenderNode* target) const;
142 virtual void setValue(RenderNode* target, float value);
John Reck8d8af3c2014-07-01 15:23:45 -0700143 virtual void onAttached();
144 virtual void onStagingPlayStateChanged();
John Recke45b1fd2014-04-15 09:50:16 -0700145
146private:
John Reck79c7de72014-05-23 10:33:31 -0700147 typedef bool (RenderProperties::*SetFloatProperty)(float value);
John Reck52244ff2014-05-01 21:27:37 -0700148 typedef float (RenderProperties::*GetFloatProperty)() const;
149
John Reckff941dc2014-05-14 16:34:14 -0700150 struct PropertyAccessors;
151 const PropertyAccessors* mPropertyAccess;
John Reck52244ff2014-05-01 21:27:37 -0700152
153 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[];
154};
155
156class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator {
157public:
158 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property,
John Reckff941dc2014-05-14 16:34:14 -0700159 float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700160
161 ANDROID_API virtual uint32_t dirtyMask();
162
John Reck52244ff2014-05-01 21:27:37 -0700163protected:
John Reckff941dc2014-05-14 16:34:14 -0700164 virtual float getValue(RenderNode* target) const;
165 virtual void setValue(RenderNode* target, float value);
John Reck52244ff2014-05-01 21:27:37 -0700166private:
167 sp<CanvasPropertyPrimitive> mProperty;
168};
169
170class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator {
171public:
172 enum PaintField {
173 STROKE_WIDTH = 0,
174 ALPHA,
175 };
176
177 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property,
John Reckff941dc2014-05-14 16:34:14 -0700178 PaintField field, float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700179
180 ANDROID_API virtual uint32_t dirtyMask();
181
John Reck52244ff2014-05-01 21:27:37 -0700182protected:
John Reckff941dc2014-05-14 16:34:14 -0700183 virtual float getValue(RenderNode* target) const;
184 virtual void setValue(RenderNode* target, float value);
John Reck52244ff2014-05-01 21:27:37 -0700185private:
186 sp<CanvasPropertyPaint> mProperty;
187 PaintField mField;
John Recke45b1fd2014-04-15 09:50:16 -0700188};
189
John Reckd3de42c2014-07-15 14:29:33 -0700190class RevealAnimator : public BaseRenderNodeAnimator {
191public:
Chris Craikaf4d04c2014-07-29 12:50:14 -0700192 ANDROID_API RevealAnimator(int centerX, int centerY,
John Reckd3de42c2014-07-15 14:29:33 -0700193 float startValue, float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700194
195 ANDROID_API virtual uint32_t dirtyMask();
196
John Reckd3de42c2014-07-15 14:29:33 -0700197protected:
198 virtual float getValue(RenderNode* target) const;
199 virtual void setValue(RenderNode* target, float value);
200
201private:
202 int mCenterX, mCenterY;
John Reckd3de42c2014-07-15 14:29:33 -0700203};
204
John Recke45b1fd2014-04-15 09:50:16 -0700205} /* namespace uirenderer */
206} /* namespace android */
207
208#endif /* ANIMATOR_H */