blob: 203cdffea4335a92dacc049eedb940d1508b4ffe [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 }
53
John Reckff941dc2014-05-14 16:34:14 -070054 ANDROID_API virtual void onAttached(RenderNode* target) {}
55
56 // Guaranteed to happen before the staging push
57 void setupStartValueIfNecessary(RenderNode* target, TreeInfo& info);
58
59 bool animate(RenderNode* target, TreeInfo& info);
60
John Reck52244ff2014-05-01 21:27:37 -070061 bool isFinished() { return mPlayState == FINISHED; }
John Reckff941dc2014-05-14 16:34:14 -070062 float finalValue() { return mFinalValue; }
John Reck52244ff2014-05-01 21:27:37 -070063
64protected:
John Reckff941dc2014-05-14 16:34:14 -070065 BaseRenderNodeAnimator(float finalValue);
66 virtual ~BaseRenderNodeAnimator();
John Reck52244ff2014-05-01 21:27:37 -070067
John Reckff941dc2014-05-14 16:34:14 -070068 virtual float getValue(RenderNode* target) const = 0;
69 virtual void setValue(RenderNode* target, float value) = 0;
John Reck52244ff2014-05-01 21:27:37 -070070
John Reck52244ff2014-05-01 21:27:37 -070071 void callOnFinishedListener(TreeInfo& info);
72
73 enum PlayState {
John Reckff941dc2014-05-14 16:34:14 -070074 NEEDS_START,
John Reck52244ff2014-05-01 21:27:37 -070075 PENDING,
76 RUNNING,
77 FINISHED,
78 };
79
John Reckff941dc2014-05-14 16:34:14 -070080 float mFinalValue;
81 float mDeltaValue;
82 float mFromValue;
83
John Reck52244ff2014-05-01 21:27:37 -070084 Interpolator* mInterpolator;
85 PlayState mPlayState;
Alan Viverettead2f8e32014-05-16 13:28:33 -070086 nsecs_t mStartTime;
87 nsecs_t mDelayUntil;
88 nsecs_t mDuration;
89 nsecs_t mStartDelay;
John Reck52244ff2014-05-01 21:27:37 -070090
Alan Viverettead2f8e32014-05-16 13:28:33 -070091 sp<AnimationListener> mListener;
John Reck52244ff2014-05-01 21:27:37 -070092};
93
John Reck52244ff2014-05-01 21:27:37 -070094class RenderPropertyAnimator : public BaseRenderNodeAnimator {
95public:
John Recke45b1fd2014-04-15 09:50:16 -070096 enum RenderProperty {
97 TRANSLATION_X = 0,
98 TRANSLATION_Y,
99 TRANSLATION_Z,
100 SCALE_X,
101 SCALE_Y,
102 ROTATION,
103 ROTATION_X,
104 ROTATION_Y,
105 X,
106 Y,
107 Z,
108 ALPHA,
109 };
110
John Reckff941dc2014-05-14 16:34:14 -0700111 ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue);
112
113 ANDROID_API virtual void onAttached(RenderNode* target);
John Recke45b1fd2014-04-15 09:50:16 -0700114
115protected:
John Reckff941dc2014-05-14 16:34:14 -0700116 virtual float getValue(RenderNode* target) const;
117 virtual void setValue(RenderNode* target, float value);
John Recke45b1fd2014-04-15 09:50:16 -0700118
119private:
John Reck79c7de72014-05-23 10:33:31 -0700120 typedef bool (RenderProperties::*SetFloatProperty)(float value);
John Reck52244ff2014-05-01 21:27:37 -0700121 typedef float (RenderProperties::*GetFloatProperty)() const;
122
John Reckff941dc2014-05-14 16:34:14 -0700123 struct PropertyAccessors;
124 const PropertyAccessors* mPropertyAccess;
John Reck52244ff2014-05-01 21:27:37 -0700125
126 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[];
127};
128
129class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator {
130public:
131 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property,
John Reckff941dc2014-05-14 16:34:14 -0700132 float finalValue);
John Reck52244ff2014-05-01 21:27:37 -0700133protected:
John Reckff941dc2014-05-14 16:34:14 -0700134 virtual float getValue(RenderNode* target) const;
135 virtual void setValue(RenderNode* target, float value);
John Reck52244ff2014-05-01 21:27:37 -0700136private:
137 sp<CanvasPropertyPrimitive> mProperty;
138};
139
140class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator {
141public:
142 enum PaintField {
143 STROKE_WIDTH = 0,
144 ALPHA,
145 };
146
147 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property,
John Reckff941dc2014-05-14 16:34:14 -0700148 PaintField field, float finalValue);
John Reck52244ff2014-05-01 21:27:37 -0700149protected:
John Reckff941dc2014-05-14 16:34:14 -0700150 virtual float getValue(RenderNode* target) const;
151 virtual void setValue(RenderNode* target, float value);
John Reck52244ff2014-05-01 21:27:37 -0700152private:
153 sp<CanvasPropertyPaint> mProperty;
154 PaintField mField;
John Recke45b1fd2014-04-15 09:50:16 -0700155};
156
157} /* namespace uirenderer */
158} /* namespace android */
159
160#endif /* ANIMATOR_H */