blob: 6cb72c4ccb020379a004ecc216d1e8dc01e89f48 [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
John Reck22184722014-06-20 07:19:30 -070064 ANDROID_API virtual uint32_t dirtyMask() { return 0; }
65
John Reck52244ff2014-05-01 21:27:37 -070066protected:
John Reckff941dc2014-05-14 16:34:14 -070067 BaseRenderNodeAnimator(float finalValue);
68 virtual ~BaseRenderNodeAnimator();
John Reck52244ff2014-05-01 21:27:37 -070069
John Reckff941dc2014-05-14 16:34:14 -070070 virtual float getValue(RenderNode* target) const = 0;
71 virtual void setValue(RenderNode* target, float value) = 0;
John Reck52244ff2014-05-01 21:27:37 -070072
John Reck52244ff2014-05-01 21:27:37 -070073 void callOnFinishedListener(TreeInfo& info);
74
75 enum PlayState {
John Reckff941dc2014-05-14 16:34:14 -070076 NEEDS_START,
John Reck52244ff2014-05-01 21:27:37 -070077 PENDING,
78 RUNNING,
79 FINISHED,
80 };
81
John Reckff941dc2014-05-14 16:34:14 -070082 float mFinalValue;
83 float mDeltaValue;
84 float mFromValue;
85
John Reck52244ff2014-05-01 21:27:37 -070086 Interpolator* mInterpolator;
87 PlayState mPlayState;
Alan Viverettead2f8e32014-05-16 13:28:33 -070088 nsecs_t mStartTime;
89 nsecs_t mDelayUntil;
90 nsecs_t mDuration;
91 nsecs_t mStartDelay;
John Reck52244ff2014-05-01 21:27:37 -070092
Alan Viverettead2f8e32014-05-16 13:28:33 -070093 sp<AnimationListener> mListener;
John Reck52244ff2014-05-01 21:27:37 -070094};
95
John Reck52244ff2014-05-01 21:27:37 -070096class RenderPropertyAnimator : public BaseRenderNodeAnimator {
97public:
John Recke45b1fd2014-04-15 09:50:16 -070098 enum RenderProperty {
99 TRANSLATION_X = 0,
100 TRANSLATION_Y,
101 TRANSLATION_Z,
102 SCALE_X,
103 SCALE_Y,
104 ROTATION,
105 ROTATION_X,
106 ROTATION_Y,
107 X,
108 Y,
109 Z,
110 ALPHA,
111 };
112
John Reckff941dc2014-05-14 16:34:14 -0700113 ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue);
114
115 ANDROID_API virtual void onAttached(RenderNode* target);
John Recke45b1fd2014-04-15 09:50:16 -0700116
John Reck22184722014-06-20 07:19:30 -0700117 ANDROID_API virtual uint32_t dirtyMask();
118
John Recke45b1fd2014-04-15 09:50:16 -0700119protected:
John Reckff941dc2014-05-14 16:34:14 -0700120 virtual float getValue(RenderNode* target) const;
121 virtual void setValue(RenderNode* target, float value);
John Recke45b1fd2014-04-15 09:50:16 -0700122
123private:
John Reck79c7de72014-05-23 10:33:31 -0700124 typedef bool (RenderProperties::*SetFloatProperty)(float value);
John Reck52244ff2014-05-01 21:27:37 -0700125 typedef float (RenderProperties::*GetFloatProperty)() const;
126
John Reckff941dc2014-05-14 16:34:14 -0700127 struct PropertyAccessors;
128 const PropertyAccessors* mPropertyAccess;
John Reck52244ff2014-05-01 21:27:37 -0700129
130 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[];
131};
132
133class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator {
134public:
135 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property,
John Reckff941dc2014-05-14 16:34:14 -0700136 float finalValue);
John Reck52244ff2014-05-01 21:27:37 -0700137protected:
John Reckff941dc2014-05-14 16:34:14 -0700138 virtual float getValue(RenderNode* target) const;
139 virtual void setValue(RenderNode* target, float value);
John Reck52244ff2014-05-01 21:27:37 -0700140private:
141 sp<CanvasPropertyPrimitive> mProperty;
142};
143
144class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator {
145public:
146 enum PaintField {
147 STROKE_WIDTH = 0,
148 ALPHA,
149 };
150
151 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property,
John Reckff941dc2014-05-14 16:34:14 -0700152 PaintField field, float finalValue);
John Reck52244ff2014-05-01 21:27:37 -0700153protected:
John Reckff941dc2014-05-14 16:34:14 -0700154 virtual float getValue(RenderNode* target) const;
155 virtual void setValue(RenderNode* target, float value);
John Reck52244ff2014-05-01 21:27:37 -0700156private:
157 sp<CanvasPropertyPaint> mProperty;
158 PaintField mField;
John Recke45b1fd2014-04-15 09:50:16 -0700159};
160
161} /* namespace uirenderer */
162} /* namespace android */
163
164#endif /* ANIMATOR_H */