blob: b0dcf2d33bbf4ebf46059fc86dc5f7d36e2af556 [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 Reck52244ff2014-05-01 21:27:37 -070031class RenderNode;
John Recke45b1fd2014-04-15 09:50:16 -070032class RenderProperties;
John Recke45b1fd2014-04-15 09:50:16 -070033
John Reck52244ff2014-05-01 21:27:37 -070034class AnimationListener : public VirtualLightRefBase {
35public:
John Reckff941dc2014-05-14 16:34:14 -070036 ANDROID_API virtual void onAnimationFinished(BaseRenderNodeAnimator*) = 0;
John Reck52244ff2014-05-01 21:27:37 -070037protected:
38 ANDROID_API virtual ~AnimationListener() {}
39};
40
John Reckff941dc2014-05-14 16:34:14 -070041class BaseRenderNodeAnimator : public VirtualLightRefBase {
42 PREVENT_COPY_AND_ASSIGN(BaseRenderNodeAnimator);
John Reck52244ff2014-05-01 21:27:37 -070043public:
John Reckc6b32642014-06-02 11:00:09 -070044 ANDROID_API void setStartValue(float value);
John Reck52244ff2014-05-01 21:27:37 -070045 ANDROID_API void setInterpolator(Interpolator* interpolator);
46 ANDROID_API void setDuration(nsecs_t durationInMs);
John Reck315c3292014-05-09 19:21:04 -070047 ANDROID_API nsecs_t duration() { return mDuration; }
Alan Viverettead2f8e32014-05-16 13:28:33 -070048 ANDROID_API void setStartDelay(nsecs_t startDelayInMs);
49 ANDROID_API nsecs_t startDelay() { return mStartDelay; }
John Reck52244ff2014-05-01 21:27:37 -070050 ANDROID_API void setListener(AnimationListener* listener) {
51 mListener = listener;
52 }
John Reck8d8af3c2014-07-01 15:23:45 -070053 ANDROID_API void start() { mStagingPlayState = RUNNING; onStagingPlayStateChanged(); }
John Reckd3de42c2014-07-15 14:29:33 -070054 ANDROID_API void end() { mStagingPlayState = FINISHED; onStagingPlayStateChanged(); }
John Reck52244ff2014-05-01 21:27:37 -070055
John Reck8d8af3c2014-07-01 15:23:45 -070056 void attach(RenderNode* target);
57 virtual void onAttached() {}
58 void detach() { mTarget = 0; }
59 void pushStaging(TreeInfo& info);
60 bool animate(TreeInfo& info);
John Reckff941dc2014-05-14 16:34:14 -070061
John Reck52244ff2014-05-01 21:27:37 -070062 bool isFinished() { return mPlayState == FINISHED; }
John Reckff941dc2014-05-14 16:34:14 -070063 float finalValue() { return mFinalValue; }
John Reck52244ff2014-05-01 21:27:37 -070064
John Reck22184722014-06-20 07:19:30 -070065 ANDROID_API virtual uint32_t dirtyMask() { return 0; }
66
John Reck52244ff2014-05-01 21:27:37 -070067protected:
John Reckff941dc2014-05-14 16:34:14 -070068 BaseRenderNodeAnimator(float finalValue);
69 virtual ~BaseRenderNodeAnimator();
John Reck52244ff2014-05-01 21:27:37 -070070
John Reckff941dc2014-05-14 16:34:14 -070071 virtual float getValue(RenderNode* target) const = 0;
72 virtual void setValue(RenderNode* target, float value) = 0;
John Reck8d8af3c2014-07-01 15:23:45 -070073 RenderNode* target() { return mTarget; }
John Reck52244ff2014-05-01 21:27:37 -070074
John Reck52244ff2014-05-01 21:27:37 -070075 void callOnFinishedListener(TreeInfo& info);
76
John Reck8d8af3c2014-07-01 15:23:45 -070077 virtual void onStagingPlayStateChanged() {}
78
John Reck52244ff2014-05-01 21:27:37 -070079 enum PlayState {
John Reck68bfe0a2014-06-24 15:34:58 -070080 NOT_STARTED,
John Reck52244ff2014-05-01 21:27:37 -070081 RUNNING,
82 FINISHED,
83 };
84
John Reck8d8af3c2014-07-01 15:23:45 -070085 RenderNode* mTarget;
86
John Reckff941dc2014-05-14 16:34:14 -070087 float mFinalValue;
88 float mDeltaValue;
89 float mFromValue;
90
John Reck52244ff2014-05-01 21:27:37 -070091 Interpolator* mInterpolator;
John Reck68bfe0a2014-06-24 15:34:58 -070092 PlayState mStagingPlayState;
John Reck52244ff2014-05-01 21:27:37 -070093 PlayState mPlayState;
John Reck68bfe0a2014-06-24 15:34:58 -070094 bool mHasStartValue;
Alan Viverettead2f8e32014-05-16 13:28:33 -070095 nsecs_t mStartTime;
Alan Viverettead2f8e32014-05-16 13:28:33 -070096 nsecs_t mDuration;
97 nsecs_t mStartDelay;
John Reck52244ff2014-05-01 21:27:37 -070098
Alan Viverettead2f8e32014-05-16 13:28:33 -070099 sp<AnimationListener> mListener;
John Reck68bfe0a2014-06-24 15:34:58 -0700100
101private:
John Reck68bfe0a2014-06-24 15:34:58 -0700102 inline void checkMutable();
John Reck8d8af3c2014-07-01 15:23:45 -0700103 virtual void transitionToRunning(TreeInfo& info);
104 void doSetStartValue(float value);
John Reck52244ff2014-05-01 21:27:37 -0700105};
106
John Reck52244ff2014-05-01 21:27:37 -0700107class RenderPropertyAnimator : public BaseRenderNodeAnimator {
108public:
John Recke45b1fd2014-04-15 09:50:16 -0700109 enum RenderProperty {
110 TRANSLATION_X = 0,
111 TRANSLATION_Y,
112 TRANSLATION_Z,
113 SCALE_X,
114 SCALE_Y,
115 ROTATION,
116 ROTATION_X,
117 ROTATION_Y,
118 X,
119 Y,
120 Z,
121 ALPHA,
122 };
123
John Reckff941dc2014-05-14 16:34:14 -0700124 ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue);
125
John Reck22184722014-06-20 07:19:30 -0700126 ANDROID_API virtual uint32_t dirtyMask();
127
John Recke45b1fd2014-04-15 09:50:16 -0700128protected:
John Reckff941dc2014-05-14 16:34:14 -0700129 virtual float getValue(RenderNode* target) const;
130 virtual void setValue(RenderNode* target, float value);
John Reck8d8af3c2014-07-01 15:23:45 -0700131 virtual void onAttached();
132 virtual void onStagingPlayStateChanged();
John Recke45b1fd2014-04-15 09:50:16 -0700133
134private:
John Reck79c7de72014-05-23 10:33:31 -0700135 typedef bool (RenderProperties::*SetFloatProperty)(float value);
John Reck52244ff2014-05-01 21:27:37 -0700136 typedef float (RenderProperties::*GetFloatProperty)() const;
137
John Reckff941dc2014-05-14 16:34:14 -0700138 struct PropertyAccessors;
139 const PropertyAccessors* mPropertyAccess;
John Reck52244ff2014-05-01 21:27:37 -0700140
141 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[];
142};
143
144class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator {
145public:
146 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property,
John Reckff941dc2014-05-14 16:34:14 -0700147 float finalValue);
John Reck52244ff2014-05-01 21:27:37 -0700148protected:
John Reckff941dc2014-05-14 16:34:14 -0700149 virtual float getValue(RenderNode* target) const;
150 virtual void setValue(RenderNode* target, float value);
John Reck52244ff2014-05-01 21:27:37 -0700151private:
152 sp<CanvasPropertyPrimitive> mProperty;
153};
154
155class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator {
156public:
157 enum PaintField {
158 STROKE_WIDTH = 0,
159 ALPHA,
160 };
161
162 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property,
John Reckff941dc2014-05-14 16:34:14 -0700163 PaintField field, float finalValue);
John Reck52244ff2014-05-01 21:27:37 -0700164protected:
John Reckff941dc2014-05-14 16:34:14 -0700165 virtual float getValue(RenderNode* target) const;
166 virtual void setValue(RenderNode* target, float value);
John Reck52244ff2014-05-01 21:27:37 -0700167private:
168 sp<CanvasPropertyPaint> mProperty;
169 PaintField mField;
John Recke45b1fd2014-04-15 09:50:16 -0700170};
171
John Reckd3de42c2014-07-15 14:29:33 -0700172class RevealAnimator : public BaseRenderNodeAnimator {
173public:
Chris Craikaf4d04c2014-07-29 12:50:14 -0700174 ANDROID_API RevealAnimator(int centerX, int centerY,
John Reckd3de42c2014-07-15 14:29:33 -0700175 float startValue, float finalValue);
176protected:
177 virtual float getValue(RenderNode* target) const;
178 virtual void setValue(RenderNode* target, float value);
179
180private:
181 int mCenterX, mCenterY;
182 bool mInverseClip;
183};
184
John Recke45b1fd2014-04-15 09:50:16 -0700185} /* namespace uirenderer */
186} /* namespace android */
187
188#endif /* ANIMATOR_H */