blob: 1ab6235bb2d750525894784141064210408e9ec2 [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 Recke2478d42014-09-03 16:46:05 -070071 void forceEndNow(AnimationContext& context);
72
John Reck52244ff2014-05-01 21:27:37 -070073protected:
John Reckff941dc2014-05-14 16:34:14 -070074 BaseRenderNodeAnimator(float finalValue);
75 virtual ~BaseRenderNodeAnimator();
John Reck52244ff2014-05-01 21:27:37 -070076
John Reckff941dc2014-05-14 16:34:14 -070077 virtual float getValue(RenderNode* target) const = 0;
78 virtual void setValue(RenderNode* target, float value) = 0;
John Reck8d8af3c2014-07-01 15:23:45 -070079 RenderNode* target() { return mTarget; }
John Reck52244ff2014-05-01 21:27:37 -070080
John Reck119907c2014-08-14 09:02:01 -070081 void callOnFinishedListener(AnimationContext& context);
John Reck52244ff2014-05-01 21:27:37 -070082
John Reck8d8af3c2014-07-01 15:23:45 -070083 virtual void onStagingPlayStateChanged() {}
84
John Reck52244ff2014-05-01 21:27:37 -070085 enum PlayState {
John Reck68bfe0a2014-06-24 15:34:58 -070086 NOT_STARTED,
John Reck52244ff2014-05-01 21:27:37 -070087 RUNNING,
88 FINISHED,
89 };
90
John Reck8d8af3c2014-07-01 15:23:45 -070091 RenderNode* mTarget;
92
John Reckff941dc2014-05-14 16:34:14 -070093 float mFinalValue;
94 float mDeltaValue;
95 float mFromValue;
96
John Reck52244ff2014-05-01 21:27:37 -070097 Interpolator* mInterpolator;
John Reck68bfe0a2014-06-24 15:34:58 -070098 PlayState mStagingPlayState;
John Reck52244ff2014-05-01 21:27:37 -070099 PlayState mPlayState;
John Reck68bfe0a2014-06-24 15:34:58 -0700100 bool mHasStartValue;
Alan Viverettead2f8e32014-05-16 13:28:33 -0700101 nsecs_t mStartTime;
Alan Viverettead2f8e32014-05-16 13:28:33 -0700102 nsecs_t mDuration;
103 nsecs_t mStartDelay;
John Reck52244ff2014-05-01 21:27:37 -0700104
Alan Viverettead2f8e32014-05-16 13:28:33 -0700105 sp<AnimationListener> mListener;
John Reck68bfe0a2014-06-24 15:34:58 -0700106
107private:
John Reck68bfe0a2014-06-24 15:34:58 -0700108 inline void checkMutable();
John Reck119907c2014-08-14 09:02:01 -0700109 virtual void transitionToRunning(AnimationContext& context);
John Reck8d8af3c2014-07-01 15:23:45 -0700110 void doSetStartValue(float value);
John Reck52244ff2014-05-01 21:27:37 -0700111};
112
John Reck52244ff2014-05-01 21:27:37 -0700113class RenderPropertyAnimator : public BaseRenderNodeAnimator {
114public:
John Recke45b1fd2014-04-15 09:50:16 -0700115 enum RenderProperty {
116 TRANSLATION_X = 0,
117 TRANSLATION_Y,
118 TRANSLATION_Z,
119 SCALE_X,
120 SCALE_Y,
121 ROTATION,
122 ROTATION_X,
123 ROTATION_Y,
124 X,
125 Y,
126 Z,
127 ALPHA,
128 };
129
John Reckff941dc2014-05-14 16:34:14 -0700130 ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue);
131
John Reck22184722014-06-20 07:19:30 -0700132 ANDROID_API virtual uint32_t dirtyMask();
133
John Recke45b1fd2014-04-15 09:50:16 -0700134protected:
John Reckff941dc2014-05-14 16:34:14 -0700135 virtual float getValue(RenderNode* target) const;
136 virtual void setValue(RenderNode* target, float value);
John Reck8d8af3c2014-07-01 15:23:45 -0700137 virtual void onAttached();
138 virtual void onStagingPlayStateChanged();
John Recke45b1fd2014-04-15 09:50:16 -0700139
140private:
John Reck79c7de72014-05-23 10:33:31 -0700141 typedef bool (RenderProperties::*SetFloatProperty)(float value);
John Reck52244ff2014-05-01 21:27:37 -0700142 typedef float (RenderProperties::*GetFloatProperty)() const;
143
John Reckff941dc2014-05-14 16:34:14 -0700144 struct PropertyAccessors;
145 const PropertyAccessors* mPropertyAccess;
John Reck52244ff2014-05-01 21:27:37 -0700146
147 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[];
148};
149
150class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator {
151public:
152 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property,
John Reckff941dc2014-05-14 16:34:14 -0700153 float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700154
155 ANDROID_API virtual uint32_t dirtyMask();
156
John Reck52244ff2014-05-01 21:27:37 -0700157protected:
John Reckff941dc2014-05-14 16:34:14 -0700158 virtual float getValue(RenderNode* target) const;
159 virtual void setValue(RenderNode* target, float value);
John Reck52244ff2014-05-01 21:27:37 -0700160private:
161 sp<CanvasPropertyPrimitive> mProperty;
162};
163
164class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator {
165public:
166 enum PaintField {
167 STROKE_WIDTH = 0,
168 ALPHA,
169 };
170
171 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property,
John Reckff941dc2014-05-14 16:34:14 -0700172 PaintField field, float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700173
174 ANDROID_API virtual uint32_t dirtyMask();
175
John Reck52244ff2014-05-01 21:27:37 -0700176protected:
John Reckff941dc2014-05-14 16:34:14 -0700177 virtual float getValue(RenderNode* target) const;
178 virtual void setValue(RenderNode* target, float value);
John Reck52244ff2014-05-01 21:27:37 -0700179private:
180 sp<CanvasPropertyPaint> mProperty;
181 PaintField mField;
John Recke45b1fd2014-04-15 09:50:16 -0700182};
183
John Reckd3de42c2014-07-15 14:29:33 -0700184class RevealAnimator : public BaseRenderNodeAnimator {
185public:
Chris Craikaf4d04c2014-07-29 12:50:14 -0700186 ANDROID_API RevealAnimator(int centerX, int centerY,
John Reckd3de42c2014-07-15 14:29:33 -0700187 float startValue, float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700188
189 ANDROID_API virtual uint32_t dirtyMask();
190
John Reckd3de42c2014-07-15 14:29:33 -0700191protected:
192 virtual float getValue(RenderNode* target) const;
193 virtual void setValue(RenderNode* target, float value);
194
195private:
196 int mCenterX, mCenterY;
John Reckd3de42c2014-07-15 14:29:33 -0700197};
198
John Recke45b1fd2014-04-15 09:50:16 -0700199} /* namespace uirenderer */
200} /* namespace android */
201
202#endif /* ANIMATOR_H */