blob: c52a93fddaa8a0b82b526ca329d0a8598ec22aa3 [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 Reck8d8af3c2014-07-01 15:23:45 -070056 ANDROID_API void start() { mStagingPlayState = RUNNING; onStagingPlayStateChanged(); }
John Reckd3de42c2014-07-15 14:29:33 -070057 ANDROID_API void end() { mStagingPlayState = FINISHED; onStagingPlayStateChanged(); }
John Reck52244ff2014-05-01 21:27:37 -070058
John Reck8d8af3c2014-07-01 15:23:45 -070059 void attach(RenderNode* target);
60 virtual void onAttached() {}
61 void detach() { mTarget = 0; }
John Reck119907c2014-08-14 09:02:01 -070062 void pushStaging(AnimationContext& context);
63 bool animate(AnimationContext& context);
John Reckff941dc2014-05-14 16:34:14 -070064
John Reck119907c2014-08-14 09:02:01 -070065 bool isRunning() { return mPlayState == RUNNING; }
John Reck52244ff2014-05-01 21:27:37 -070066 bool isFinished() { return mPlayState == FINISHED; }
John Reckff941dc2014-05-14 16:34:14 -070067 float finalValue() { return mFinalValue; }
John Reck52244ff2014-05-01 21:27:37 -070068
John Recka7c2ea22014-08-08 13:21:00 -070069 ANDROID_API virtual uint32_t dirtyMask() = 0;
John Reck22184722014-06-20 07:19:30 -070070
John Reck52244ff2014-05-01 21:27:37 -070071protected:
John Reckff941dc2014-05-14 16:34:14 -070072 BaseRenderNodeAnimator(float finalValue);
73 virtual ~BaseRenderNodeAnimator();
John Reck52244ff2014-05-01 21:27:37 -070074
John Reckff941dc2014-05-14 16:34:14 -070075 virtual float getValue(RenderNode* target) const = 0;
76 virtual void setValue(RenderNode* target, float value) = 0;
John Reck8d8af3c2014-07-01 15:23:45 -070077 RenderNode* target() { return mTarget; }
John Reck52244ff2014-05-01 21:27:37 -070078
John Reck119907c2014-08-14 09:02:01 -070079 void callOnFinishedListener(AnimationContext& context);
John Reck52244ff2014-05-01 21:27:37 -070080
John Reck8d8af3c2014-07-01 15:23:45 -070081 virtual void onStagingPlayStateChanged() {}
82
John Reck52244ff2014-05-01 21:27:37 -070083 enum PlayState {
John Reck68bfe0a2014-06-24 15:34:58 -070084 NOT_STARTED,
John Reck52244ff2014-05-01 21:27:37 -070085 RUNNING,
86 FINISHED,
87 };
88
John Reck8d8af3c2014-07-01 15:23:45 -070089 RenderNode* mTarget;
90
John Reckff941dc2014-05-14 16:34:14 -070091 float mFinalValue;
92 float mDeltaValue;
93 float mFromValue;
94
John Reck52244ff2014-05-01 21:27:37 -070095 Interpolator* mInterpolator;
John Reck68bfe0a2014-06-24 15:34:58 -070096 PlayState mStagingPlayState;
John Reck52244ff2014-05-01 21:27:37 -070097 PlayState mPlayState;
John Reck68bfe0a2014-06-24 15:34:58 -070098 bool mHasStartValue;
Alan Viverettead2f8e32014-05-16 13:28:33 -070099 nsecs_t mStartTime;
Alan Viverettead2f8e32014-05-16 13:28:33 -0700100 nsecs_t mDuration;
101 nsecs_t mStartDelay;
John Reck52244ff2014-05-01 21:27:37 -0700102
Alan Viverettead2f8e32014-05-16 13:28:33 -0700103 sp<AnimationListener> mListener;
John Reck68bfe0a2014-06-24 15:34:58 -0700104
105private:
John Reck68bfe0a2014-06-24 15:34:58 -0700106 inline void checkMutable();
John Reck119907c2014-08-14 09:02:01 -0700107 virtual void transitionToRunning(AnimationContext& context);
John Reck8d8af3c2014-07-01 15:23:45 -0700108 void doSetStartValue(float value);
John Reck52244ff2014-05-01 21:27:37 -0700109};
110
John Reck52244ff2014-05-01 21:27:37 -0700111class RenderPropertyAnimator : public BaseRenderNodeAnimator {
112public:
John Recke45b1fd2014-04-15 09:50:16 -0700113 enum RenderProperty {
114 TRANSLATION_X = 0,
115 TRANSLATION_Y,
116 TRANSLATION_Z,
117 SCALE_X,
118 SCALE_Y,
119 ROTATION,
120 ROTATION_X,
121 ROTATION_Y,
122 X,
123 Y,
124 Z,
125 ALPHA,
126 };
127
John Reckff941dc2014-05-14 16:34:14 -0700128 ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue);
129
John Reck22184722014-06-20 07:19:30 -0700130 ANDROID_API virtual uint32_t dirtyMask();
131
John Recke45b1fd2014-04-15 09:50:16 -0700132protected:
John Reckff941dc2014-05-14 16:34:14 -0700133 virtual float getValue(RenderNode* target) const;
134 virtual void setValue(RenderNode* target, float value);
John Reck8d8af3c2014-07-01 15:23:45 -0700135 virtual void onAttached();
136 virtual void onStagingPlayStateChanged();
John Recke45b1fd2014-04-15 09:50:16 -0700137
138private:
John Reck79c7de72014-05-23 10:33:31 -0700139 typedef bool (RenderProperties::*SetFloatProperty)(float value);
John Reck52244ff2014-05-01 21:27:37 -0700140 typedef float (RenderProperties::*GetFloatProperty)() const;
141
John Reckff941dc2014-05-14 16:34:14 -0700142 struct PropertyAccessors;
143 const PropertyAccessors* mPropertyAccess;
John Reck52244ff2014-05-01 21:27:37 -0700144
145 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[];
146};
147
148class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator {
149public:
150 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property,
John Reckff941dc2014-05-14 16:34:14 -0700151 float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700152
153 ANDROID_API virtual uint32_t dirtyMask();
154
John Reck52244ff2014-05-01 21:27:37 -0700155protected:
John Reckff941dc2014-05-14 16:34:14 -0700156 virtual float getValue(RenderNode* target) const;
157 virtual void setValue(RenderNode* target, float value);
John Reck52244ff2014-05-01 21:27:37 -0700158private:
159 sp<CanvasPropertyPrimitive> mProperty;
160};
161
162class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator {
163public:
164 enum PaintField {
165 STROKE_WIDTH = 0,
166 ALPHA,
167 };
168
169 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property,
John Reckff941dc2014-05-14 16:34:14 -0700170 PaintField field, float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700171
172 ANDROID_API virtual uint32_t dirtyMask();
173
John Reck52244ff2014-05-01 21:27:37 -0700174protected:
John Reckff941dc2014-05-14 16:34:14 -0700175 virtual float getValue(RenderNode* target) const;
176 virtual void setValue(RenderNode* target, float value);
John Reck52244ff2014-05-01 21:27:37 -0700177private:
178 sp<CanvasPropertyPaint> mProperty;
179 PaintField mField;
John Recke45b1fd2014-04-15 09:50:16 -0700180};
181
John Reckd3de42c2014-07-15 14:29:33 -0700182class RevealAnimator : public BaseRenderNodeAnimator {
183public:
Chris Craikaf4d04c2014-07-29 12:50:14 -0700184 ANDROID_API RevealAnimator(int centerX, int centerY,
John Reckd3de42c2014-07-15 14:29:33 -0700185 float startValue, float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700186
187 ANDROID_API virtual uint32_t dirtyMask();
188
John Reckd3de42c2014-07-15 14:29:33 -0700189protected:
190 virtual float getValue(RenderNode* target) const;
191 virtual void setValue(RenderNode* target, float value);
192
193private:
194 int mCenterX, mCenterY;
John Reckd3de42c2014-07-15 14:29:33 -0700195};
196
John Recke45b1fd2014-04-15 09:50:16 -0700197} /* namespace uirenderer */
198} /* namespace android */
199
200#endif /* ANIMATOR_H */