blob: 52a180758fb8f3cee8e118a47eed93eaf3edd8e2 [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:
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);
John Reck315c3292014-05-09 19:21:04 -070048 ANDROID_API nsecs_t duration() { return mDuration; }
John Reck52244ff2014-05-01 21:27:37 -070049 ANDROID_API void setListener(AnimationListener* listener) {
50 mListener = listener;
51 }
52
53 bool isFinished() { return mPlayState == FINISHED; }
54
55protected:
56 BaseAnimator();
57 virtual ~BaseAnimator();
58
59 // This is the main animation entrypoint that subclasses should call
60 // to generate the onAnimation* lifecycle events
61 // Returns true if the animation has finished, false otherwise
62 bool animateFrame(TreeInfo& info);
63
64 // Called when PlayState switches from PENDING to RUNNING
65 virtual void onAnimationStarted() {}
66 virtual void onAnimationUpdated(float fraction) = 0;
67 virtual void onAnimationFinished() {}
68
69private:
70 void callOnFinishedListener(TreeInfo& info);
71
72 enum PlayState {
73 PENDING,
74 RUNNING,
75 FINISHED,
76 };
77
78 Interpolator* mInterpolator;
79 PlayState mPlayState;
80 long mStartTime;
81 long mDuration;
82
83 sp<AnimationListener> mListener;
84};
85
86class BaseRenderNodeAnimator : public BaseAnimator {
John Recke45b1fd2014-04-15 09:50:16 -070087public:
88 // Since the UI thread doesn't necessarily know what the current values
89 // actually are and thus can't do the calculations, this is used to inform
90 // the animator how to lazy-resolve the input value
91 enum DeltaValueType {
92 // The delta value represents an absolute value endpoint
93 // mDeltaValue needs to be recalculated to be mDelta = (mDelta - fromValue)
94 // in onAnimationStarted()
95 ABSOLUTE = 0,
96 // The final value represents an offset from the current value
97 // No recalculation is needed
98 DELTA,
99 };
100
John Reck52244ff2014-05-01 21:27:37 -0700101 bool animate(RenderNode* target, TreeInfo& info);
102
103protected:
104 BaseRenderNodeAnimator(DeltaValueType deltaType, float deltaValue);
105
106 RenderNode* target() const { return mTarget; }
107 virtual float getValue() const = 0;
108 virtual void setValue(float value) = 0;
109
110private:
111 virtual void onAnimationStarted();
112 virtual void onAnimationUpdated(float fraction);
113
114 // mTarget is only valid inside animate()
115 RenderNode* mTarget;
116
117 BaseRenderNodeAnimator::DeltaValueType mDeltaValueType;
118 float mDeltaValue;
119 float mFromValue;
120};
121
122class RenderPropertyAnimator : public BaseRenderNodeAnimator {
123public:
John Recke45b1fd2014-04-15 09:50:16 -0700124 enum RenderProperty {
125 TRANSLATION_X = 0,
126 TRANSLATION_Y,
127 TRANSLATION_Z,
128 SCALE_X,
129 SCALE_Y,
130 ROTATION,
131 ROTATION_X,
132 ROTATION_Y,
133 X,
134 Y,
135 Z,
136 ALPHA,
137 };
138
John Reck52244ff2014-05-01 21:27:37 -0700139 ANDROID_API RenderPropertyAnimator(RenderProperty property,
140 DeltaValueType deltaType, float deltaValue);
John Recke45b1fd2014-04-15 09:50:16 -0700141
142protected:
John Reck52244ff2014-05-01 21:27:37 -0700143 ANDROID_API virtual float getValue() const;
144 ANDROID_API virtual void setValue(float value);
John Recke45b1fd2014-04-15 09:50:16 -0700145
146private:
John Reck52244ff2014-05-01 21:27:37 -0700147 typedef void (RenderProperties::*SetFloatProperty)(float value);
148 typedef float (RenderProperties::*GetFloatProperty)() const;
149
150 struct PropertyAccessors {
151 GetFloatProperty getter;
152 SetFloatProperty setter;
153 };
154
155 PropertyAccessors mPropertyAccess;
156
157 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[];
158};
159
160class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator {
161public:
162 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property,
163 DeltaValueType deltaType, float deltaValue);
164protected:
165 ANDROID_API virtual float getValue() const;
166 ANDROID_API virtual void setValue(float value);
167private:
168 sp<CanvasPropertyPrimitive> mProperty;
169};
170
171class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator {
172public:
173 enum PaintField {
174 STROKE_WIDTH = 0,
175 ALPHA,
176 };
177
178 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property,
179 PaintField field, DeltaValueType deltaType, float deltaValue);
180protected:
181 ANDROID_API virtual float getValue() const;
182 ANDROID_API virtual void setValue(float value);
183private:
184 sp<CanvasPropertyPaint> mProperty;
185 PaintField mField;
John Recke45b1fd2014-04-15 09:50:16 -0700186};
187
188} /* namespace uirenderer */
189} /* namespace android */
190
191#endif /* ANIMATOR_H */