blob: a981b5a7d4af36b9f90bd001a5e93c5c5fab3f74 [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 }
John Reck68bfe0a2014-06-24 15:34:58 -070053 ANDROID_API void start() { mStagingPlayState = RUNNING; }
54 ANDROID_API void cancel() { mStagingPlayState = FINISHED; }
John Reck52244ff2014-05-01 21:27:37 -070055
John Reck68bfe0a2014-06-24 15:34:58 -070056 virtual void onAttached(RenderNode* target) {}
57 virtual void pushStaging(RenderNode* target, TreeInfo& info);
John Reckff941dc2014-05-14 16:34:14 -070058 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
John Reck22184722014-06-20 07:19:30 -070063 ANDROID_API virtual uint32_t dirtyMask() { return 0; }
64
John Reck52244ff2014-05-01 21:27:37 -070065protected:
John Reckff941dc2014-05-14 16:34:14 -070066 BaseRenderNodeAnimator(float finalValue);
67 virtual ~BaseRenderNodeAnimator();
John Reck52244ff2014-05-01 21:27:37 -070068
John Reckff941dc2014-05-14 16:34:14 -070069 virtual float getValue(RenderNode* target) const = 0;
70 virtual void setValue(RenderNode* target, float value) = 0;
John Reck52244ff2014-05-01 21:27:37 -070071
John Reck52244ff2014-05-01 21:27:37 -070072 void callOnFinishedListener(TreeInfo& info);
73
74 enum PlayState {
John Reck68bfe0a2014-06-24 15:34:58 -070075 NOT_STARTED,
John Reck52244ff2014-05-01 21:27:37 -070076 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;
John Reck68bfe0a2014-06-24 15:34:58 -070085 PlayState mStagingPlayState;
John Reck52244ff2014-05-01 21:27:37 -070086 PlayState mPlayState;
John Reck68bfe0a2014-06-24 15:34:58 -070087 bool mHasStartValue;
Alan Viverettead2f8e32014-05-16 13:28:33 -070088 nsecs_t mStartTime;
Alan Viverettead2f8e32014-05-16 13:28:33 -070089 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 Reck68bfe0a2014-06-24 15:34:58 -070093
94private:
95 void doSetStartValue(float value);
96 inline void checkMutable();
97 void transitionToRunning(TreeInfo& info);
John Reck52244ff2014-05-01 21:27:37 -070098};
99
John Reck52244ff2014-05-01 21:27:37 -0700100class RenderPropertyAnimator : public BaseRenderNodeAnimator {
101public:
John Recke45b1fd2014-04-15 09:50:16 -0700102 enum RenderProperty {
103 TRANSLATION_X = 0,
104 TRANSLATION_Y,
105 TRANSLATION_Z,
106 SCALE_X,
107 SCALE_Y,
108 ROTATION,
109 ROTATION_X,
110 ROTATION_Y,
111 X,
112 Y,
113 Z,
114 ALPHA,
115 };
116
John Reckff941dc2014-05-14 16:34:14 -0700117 ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue);
118
John Reck68bfe0a2014-06-24 15:34:58 -0700119 virtual void onAttached(RenderNode* target);
John Recke45b1fd2014-04-15 09:50:16 -0700120
John Reck22184722014-06-20 07:19:30 -0700121 ANDROID_API virtual uint32_t dirtyMask();
122
John Recke45b1fd2014-04-15 09:50:16 -0700123protected:
John Reckff941dc2014-05-14 16:34:14 -0700124 virtual float getValue(RenderNode* target) const;
125 virtual void setValue(RenderNode* target, float value);
John Recke45b1fd2014-04-15 09:50:16 -0700126
127private:
John Reck79c7de72014-05-23 10:33:31 -0700128 typedef bool (RenderProperties::*SetFloatProperty)(float value);
John Reck52244ff2014-05-01 21:27:37 -0700129 typedef float (RenderProperties::*GetFloatProperty)() const;
130
John Reckff941dc2014-05-14 16:34:14 -0700131 struct PropertyAccessors;
132 const PropertyAccessors* mPropertyAccess;
John Reck52244ff2014-05-01 21:27:37 -0700133
134 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[];
135};
136
137class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator {
138public:
139 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property,
John Reckff941dc2014-05-14 16:34:14 -0700140 float finalValue);
John Reck52244ff2014-05-01 21:27:37 -0700141protected:
John Reckff941dc2014-05-14 16:34:14 -0700142 virtual float getValue(RenderNode* target) const;
143 virtual void setValue(RenderNode* target, float value);
John Reck52244ff2014-05-01 21:27:37 -0700144private:
145 sp<CanvasPropertyPrimitive> mProperty;
146};
147
148class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator {
149public:
150 enum PaintField {
151 STROKE_WIDTH = 0,
152 ALPHA,
153 };
154
155 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property,
John Reckff941dc2014-05-14 16:34:14 -0700156 PaintField field, float finalValue);
John Reck52244ff2014-05-01 21:27:37 -0700157protected:
John Reckff941dc2014-05-14 16:34:14 -0700158 virtual float getValue(RenderNode* target) const;
159 virtual void setValue(RenderNode* target, float value);
John Reck52244ff2014-05-01 21:27:37 -0700160private:
161 sp<CanvasPropertyPaint> mProperty;
162 PaintField mField;
John Recke45b1fd2014-04-15 09:50:16 -0700163};
164
165} /* namespace uirenderer */
166} /* namespace android */
167
168#endif /* ANIMATOR_H */