blob: fe88cbf1d7a8782ddd8eb63d70321ee21a6299b5 [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; }
John Reck52244ff2014-05-01 21:27:37 -070047 ANDROID_API void setListener(AnimationListener* listener) {
48 mListener = listener;
49 }
50
John Reckff941dc2014-05-14 16:34:14 -070051 ANDROID_API virtual void onAttached(RenderNode* target) {}
52
53 // Guaranteed to happen before the staging push
54 void setupStartValueIfNecessary(RenderNode* target, TreeInfo& info);
55
56 bool animate(RenderNode* target, TreeInfo& info);
57
John Reck52244ff2014-05-01 21:27:37 -070058 bool isFinished() { return mPlayState == FINISHED; }
John Reckff941dc2014-05-14 16:34:14 -070059 float finalValue() { return mFinalValue; }
John Reck52244ff2014-05-01 21:27:37 -070060
61protected:
John Reckff941dc2014-05-14 16:34:14 -070062 BaseRenderNodeAnimator(float finalValue);
63 virtual ~BaseRenderNodeAnimator();
John Reck52244ff2014-05-01 21:27:37 -070064
John Reckff941dc2014-05-14 16:34:14 -070065 void setStartValue(float value);
66 virtual float getValue(RenderNode* target) const = 0;
67 virtual void setValue(RenderNode* target, float value) = 0;
John Reck52244ff2014-05-01 21:27:37 -070068
69private:
70 void callOnFinishedListener(TreeInfo& info);
71
72 enum PlayState {
John Reckff941dc2014-05-14 16:34:14 -070073 NEEDS_START,
John Reck52244ff2014-05-01 21:27:37 -070074 PENDING,
75 RUNNING,
76 FINISHED,
77 };
78
John Reckff941dc2014-05-14 16:34:14 -070079 float mFinalValue;
80 float mDeltaValue;
81 float mFromValue;
82
John Reck52244ff2014-05-01 21:27:37 -070083 Interpolator* mInterpolator;
84 PlayState mPlayState;
85 long mStartTime;
86 long mDuration;
87
88 sp<AnimationListener> mListener;
89};
90
John Reck52244ff2014-05-01 21:27:37 -070091class RenderPropertyAnimator : public BaseRenderNodeAnimator {
92public:
John Recke45b1fd2014-04-15 09:50:16 -070093 enum RenderProperty {
94 TRANSLATION_X = 0,
95 TRANSLATION_Y,
96 TRANSLATION_Z,
97 SCALE_X,
98 SCALE_Y,
99 ROTATION,
100 ROTATION_X,
101 ROTATION_Y,
102 X,
103 Y,
104 Z,
105 ALPHA,
106 };
107
John Reckff941dc2014-05-14 16:34:14 -0700108 ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue);
109
110 ANDROID_API virtual void onAttached(RenderNode* target);
John Recke45b1fd2014-04-15 09:50:16 -0700111
112protected:
John Reckff941dc2014-05-14 16:34:14 -0700113 virtual float getValue(RenderNode* target) const;
114 virtual void setValue(RenderNode* target, float value);
John Recke45b1fd2014-04-15 09:50:16 -0700115
116private:
John Reck52244ff2014-05-01 21:27:37 -0700117 typedef void (RenderProperties::*SetFloatProperty)(float value);
118 typedef float (RenderProperties::*GetFloatProperty)() const;
119
John Reckff941dc2014-05-14 16:34:14 -0700120 struct PropertyAccessors;
121 const PropertyAccessors* mPropertyAccess;
John Reck52244ff2014-05-01 21:27:37 -0700122
123 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[];
124};
125
126class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator {
127public:
128 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property,
John Reckff941dc2014-05-14 16:34:14 -0700129 float finalValue);
John Reck52244ff2014-05-01 21:27:37 -0700130protected:
John Reckff941dc2014-05-14 16:34:14 -0700131 virtual float getValue(RenderNode* target) const;
132 virtual void setValue(RenderNode* target, float value);
John Reck52244ff2014-05-01 21:27:37 -0700133private:
134 sp<CanvasPropertyPrimitive> mProperty;
135};
136
137class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator {
138public:
139 enum PaintField {
140 STROKE_WIDTH = 0,
141 ALPHA,
142 };
143
144 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property,
John Reckff941dc2014-05-14 16:34:14 -0700145 PaintField field, float finalValue);
John Reck52244ff2014-05-01 21:27:37 -0700146protected:
John Reckff941dc2014-05-14 16:34:14 -0700147 virtual float getValue(RenderNode* target) const;
148 virtual void setValue(RenderNode* target, float value);
John Reck52244ff2014-05-01 21:27:37 -0700149private:
150 sp<CanvasPropertyPaint> mProperty;
151 PaintField mField;
John Recke45b1fd2014-04-15 09:50:16 -0700152};
153
154} /* namespace uirenderer */
155} /* namespace android */
156
157#endif /* ANIMATOR_H */