blob: 7741617d28e33fe36b7bbad8332cdf147712d3c7 [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 Reck52244ff2014-05-01 21:27:37 -070044 ANDROID_API void setInterpolator(Interpolator* interpolator);
45 ANDROID_API void setDuration(nsecs_t durationInMs);
John Reck315c3292014-05-09 19:21:04 -070046 ANDROID_API nsecs_t duration() { return mDuration; }
Alan Viverettead2f8e32014-05-16 13:28:33 -070047 ANDROID_API void setStartDelay(nsecs_t startDelayInMs);
48 ANDROID_API nsecs_t startDelay() { return mStartDelay; }
John Reck52244ff2014-05-01 21:27:37 -070049 ANDROID_API void setListener(AnimationListener* listener) {
50 mListener = listener;
51 }
52
John Reckff941dc2014-05-14 16:34:14 -070053 ANDROID_API virtual void onAttached(RenderNode* target) {}
54
55 // Guaranteed to happen before the staging push
56 void setupStartValueIfNecessary(RenderNode* target, TreeInfo& info);
57
58 bool animate(RenderNode* target, TreeInfo& info);
59
John Reck52244ff2014-05-01 21:27:37 -070060 bool isFinished() { return mPlayState == FINISHED; }
John Reckff941dc2014-05-14 16:34:14 -070061 float finalValue() { return mFinalValue; }
John Reck52244ff2014-05-01 21:27:37 -070062
63protected:
John Reckff941dc2014-05-14 16:34:14 -070064 BaseRenderNodeAnimator(float finalValue);
65 virtual ~BaseRenderNodeAnimator();
John Reck52244ff2014-05-01 21:27:37 -070066
John Reckff941dc2014-05-14 16:34:14 -070067 void setStartValue(float value);
68 virtual float getValue(RenderNode* target) const = 0;
69 virtual void setValue(RenderNode* target, float value) = 0;
John Reck52244ff2014-05-01 21:27:37 -070070
71private:
72 void callOnFinishedListener(TreeInfo& info);
73
74 enum PlayState {
John Reckff941dc2014-05-14 16:34:14 -070075 NEEDS_START,
John Reck52244ff2014-05-01 21:27:37 -070076 PENDING,
77 RUNNING,
78 FINISHED,
79 };
80
John Reckff941dc2014-05-14 16:34:14 -070081 float mFinalValue;
82 float mDeltaValue;
83 float mFromValue;
84
John Reck52244ff2014-05-01 21:27:37 -070085 Interpolator* mInterpolator;
86 PlayState mPlayState;
Alan Viverettead2f8e32014-05-16 13:28:33 -070087 nsecs_t mStartTime;
88 nsecs_t mDelayUntil;
89 nsecs_t mDuration;
90 nsecs_t mStartDelay;
John Reck52244ff2014-05-01 21:27:37 -070091
Alan Viverettead2f8e32014-05-16 13:28:33 -070092 sp<AnimationListener> mListener;
John Reck52244ff2014-05-01 21:27:37 -070093};
94
John Reck52244ff2014-05-01 21:27:37 -070095class RenderPropertyAnimator : public BaseRenderNodeAnimator {
96public:
John Recke45b1fd2014-04-15 09:50:16 -070097 enum RenderProperty {
98 TRANSLATION_X = 0,
99 TRANSLATION_Y,
100 TRANSLATION_Z,
101 SCALE_X,
102 SCALE_Y,
103 ROTATION,
104 ROTATION_X,
105 ROTATION_Y,
106 X,
107 Y,
108 Z,
109 ALPHA,
110 };
111
John Reckff941dc2014-05-14 16:34:14 -0700112 ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue);
113
114 ANDROID_API virtual void onAttached(RenderNode* target);
John Recke45b1fd2014-04-15 09:50:16 -0700115
116protected:
John Reckff941dc2014-05-14 16:34:14 -0700117 virtual float getValue(RenderNode* target) const;
118 virtual void setValue(RenderNode* target, float value);
John Recke45b1fd2014-04-15 09:50:16 -0700119
120private:
John Reck52244ff2014-05-01 21:27:37 -0700121 typedef void (RenderProperties::*SetFloatProperty)(float value);
122 typedef float (RenderProperties::*GetFloatProperty)() const;
123
John Reckff941dc2014-05-14 16:34:14 -0700124 struct PropertyAccessors;
125 const PropertyAccessors* mPropertyAccess;
John Reck52244ff2014-05-01 21:27:37 -0700126
127 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[];
128};
129
130class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator {
131public:
132 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property,
John Reckff941dc2014-05-14 16:34:14 -0700133 float finalValue);
John Reck52244ff2014-05-01 21:27:37 -0700134protected:
John Reckff941dc2014-05-14 16:34:14 -0700135 virtual float getValue(RenderNode* target) const;
136 virtual void setValue(RenderNode* target, float value);
John Reck52244ff2014-05-01 21:27:37 -0700137private:
138 sp<CanvasPropertyPrimitive> mProperty;
139};
140
141class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator {
142public:
143 enum PaintField {
144 STROKE_WIDTH = 0,
145 ALPHA,
146 };
147
148 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property,
John Reckff941dc2014-05-14 16:34:14 -0700149 PaintField field, float finalValue);
John Reck52244ff2014-05-01 21:27:37 -0700150protected:
John Reckff941dc2014-05-14 16:34:14 -0700151 virtual float getValue(RenderNode* target) const;
152 virtual void setValue(RenderNode* target, float value);
John Reck52244ff2014-05-01 21:27:37 -0700153private:
154 sp<CanvasPropertyPaint> mProperty;
155 PaintField mField;
John Recke45b1fd2014-04-15 09:50:16 -0700156};
157
158} /* namespace uirenderer */
159} /* namespace android */
160
161#endif /* ANIMATOR_H */