blob: 0b074ccd0922e7f424bb0f8a6fa802423749159b [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 Reck52244ff2014-05-01 21:27:37 -070020#include <utils/StrongPointer.h>
John Recke45b1fd2014-04-15 09:50:16 -070021
John Reck52244ff2014-05-01 21:27:37 -070022#include "CanvasProperty.h"
John Recke45b1fd2014-04-15 09:50:16 -070023#include "Interpolator.h"
24#include "TreeInfo.h"
John Reck52244ff2014-05-01 21:27:37 -070025#include "utils/Macros.h"
John Recke45b1fd2014-04-15 09:50:16 -070026#include "utils/VirtualLightRefBase.h"
27
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:
36 ANDROID_API virtual void onAnimationFinished(BaseAnimator*) = 0;
37protected:
38 ANDROID_API virtual ~AnimationListener() {}
39};
40
41// Helper class to contain generic animator helpers
42class BaseAnimator : public VirtualLightRefBase {
43 PREVENT_COPY_AND_ASSIGN(BaseAnimator);
44public:
45
46 ANDROID_API void setInterpolator(Interpolator* interpolator);
47 ANDROID_API void setDuration(nsecs_t durationInMs);
48 ANDROID_API void setListener(AnimationListener* listener) {
49 mListener = listener;
50 }
51
52 bool isFinished() { return mPlayState == FINISHED; }
53
54protected:
55 BaseAnimator();
56 virtual ~BaseAnimator();
57
58 // This is the main animation entrypoint that subclasses should call
59 // to generate the onAnimation* lifecycle events
60 // Returns true if the animation has finished, false otherwise
61 bool animateFrame(TreeInfo& info);
62
63 // Called when PlayState switches from PENDING to RUNNING
64 virtual void onAnimationStarted() {}
65 virtual void onAnimationUpdated(float fraction) = 0;
66 virtual void onAnimationFinished() {}
67
68private:
69 void callOnFinishedListener(TreeInfo& info);
70
71 enum PlayState {
72 PENDING,
73 RUNNING,
74 FINISHED,
75 };
76
77 Interpolator* mInterpolator;
78 PlayState mPlayState;
79 long mStartTime;
80 long mDuration;
81
82 sp<AnimationListener> mListener;
83};
84
85class BaseRenderNodeAnimator : public BaseAnimator {
John Recke45b1fd2014-04-15 09:50:16 -070086public:
87 // Since the UI thread doesn't necessarily know what the current values
88 // actually are and thus can't do the calculations, this is used to inform
89 // the animator how to lazy-resolve the input value
90 enum DeltaValueType {
91 // The delta value represents an absolute value endpoint
92 // mDeltaValue needs to be recalculated to be mDelta = (mDelta - fromValue)
93 // in onAnimationStarted()
94 ABSOLUTE = 0,
95 // The final value represents an offset from the current value
96 // No recalculation is needed
97 DELTA,
98 };
99
John Reck52244ff2014-05-01 21:27:37 -0700100 bool animate(RenderNode* target, TreeInfo& info);
101
102protected:
103 BaseRenderNodeAnimator(DeltaValueType deltaType, float deltaValue);
104
105 RenderNode* target() const { return mTarget; }
106 virtual float getValue() const = 0;
107 virtual void setValue(float value) = 0;
108
109private:
110 virtual void onAnimationStarted();
111 virtual void onAnimationUpdated(float fraction);
112
113 // mTarget is only valid inside animate()
114 RenderNode* mTarget;
115
116 BaseRenderNodeAnimator::DeltaValueType mDeltaValueType;
117 float mDeltaValue;
118 float mFromValue;
119};
120
121class RenderPropertyAnimator : public BaseRenderNodeAnimator {
122public:
John Recke45b1fd2014-04-15 09:50:16 -0700123 enum RenderProperty {
124 TRANSLATION_X = 0,
125 TRANSLATION_Y,
126 TRANSLATION_Z,
127 SCALE_X,
128 SCALE_Y,
129 ROTATION,
130 ROTATION_X,
131 ROTATION_Y,
132 X,
133 Y,
134 Z,
135 ALPHA,
136 };
137
John Reck52244ff2014-05-01 21:27:37 -0700138 ANDROID_API RenderPropertyAnimator(RenderProperty property,
139 DeltaValueType deltaType, float deltaValue);
John Recke45b1fd2014-04-15 09:50:16 -0700140
141protected:
John Reck52244ff2014-05-01 21:27:37 -0700142 ANDROID_API virtual float getValue() const;
143 ANDROID_API virtual void setValue(float value);
John Recke45b1fd2014-04-15 09:50:16 -0700144
145private:
John Reck52244ff2014-05-01 21:27:37 -0700146 typedef void (RenderProperties::*SetFloatProperty)(float value);
147 typedef float (RenderProperties::*GetFloatProperty)() const;
148
149 struct PropertyAccessors {
150 GetFloatProperty getter;
151 SetFloatProperty setter;
152 };
153
154 PropertyAccessors mPropertyAccess;
155
156 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[];
157};
158
159class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator {
160public:
161 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property,
162 DeltaValueType deltaType, float deltaValue);
163protected:
164 ANDROID_API virtual float getValue() const;
165 ANDROID_API virtual void setValue(float value);
166private:
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,
178 PaintField field, DeltaValueType deltaType, float deltaValue);
179protected:
180 ANDROID_API virtual float getValue() const;
181 ANDROID_API virtual void setValue(float value);
182private:
183 sp<CanvasPropertyPaint> mProperty;
184 PaintField mField;
John Recke45b1fd2014-04-15 09:50:16 -0700185};
186
187} /* namespace uirenderer */
188} /* namespace android */
189
190#endif /* ANIMATOR_H */