blob: 1b3d8e7f4842bfcda604d51e1d9b7cca453f1d11 [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
Chris Craik51d6a3d2014-12-22 17:16:56 -080019#include <memory>
John Recke45b1fd2014-04-15 09:50:16 -070020#include <cutils/compiler.h>
John Reck9fa40712014-05-09 15:26:59 -070021#include <utils/RefBase.h>
John Reck52244ff2014-05-01 21:27:37 -070022#include <utils/StrongPointer.h>
Tom Hudson2dc236b2014-10-15 15:46:42 -040023#include <utils/Timers.h>
John Recke45b1fd2014-04-15 09:50:16 -070024
John Reck52244ff2014-05-01 21:27:37 -070025#include "utils/Macros.h"
John Recke45b1fd2014-04-15 09:50:16 -070026
27namespace android {
28namespace uirenderer {
29
John Reck119907c2014-08-14 09:02:01 -070030class AnimationContext;
31class BaseRenderNodeAnimator;
Tom Hudson2dc236b2014-10-15 15:46:42 -040032class CanvasPropertyPrimitive;
33class CanvasPropertyPaint;
34class Interpolator;
John Reck52244ff2014-05-01 21:27:37 -070035class RenderNode;
John Recke45b1fd2014-04-15 09:50:16 -070036class RenderProperties;
John Recke45b1fd2014-04-15 09:50:16 -070037
John Reck52244ff2014-05-01 21:27:37 -070038class AnimationListener : public VirtualLightRefBase {
39public:
John Reckff941dc2014-05-14 16:34:14 -070040 ANDROID_API virtual void onAnimationFinished(BaseRenderNodeAnimator*) = 0;
John Reck52244ff2014-05-01 21:27:37 -070041protected:
42 ANDROID_API virtual ~AnimationListener() {}
43};
44
John Reckff941dc2014-05-14 16:34:14 -070045class BaseRenderNodeAnimator : public VirtualLightRefBase {
46 PREVENT_COPY_AND_ASSIGN(BaseRenderNodeAnimator);
John Reck52244ff2014-05-01 21:27:37 -070047public:
John Reckc6b32642014-06-02 11:00:09 -070048 ANDROID_API void setStartValue(float value);
John Reck52244ff2014-05-01 21:27:37 -070049 ANDROID_API void setInterpolator(Interpolator* interpolator);
50 ANDROID_API void setDuration(nsecs_t durationInMs);
John Reck315c3292014-05-09 19:21:04 -070051 ANDROID_API nsecs_t duration() { return mDuration; }
Alan Viverettead2f8e32014-05-16 13:28:33 -070052 ANDROID_API void setStartDelay(nsecs_t startDelayInMs);
53 ANDROID_API nsecs_t startDelay() { return mStartDelay; }
John Reck52244ff2014-05-01 21:27:37 -070054 ANDROID_API void setListener(AnimationListener* listener) {
55 mListener = listener;
56 }
John Reck119907c2014-08-14 09:02:01 -070057 AnimationListener* listener() { return mListener.get(); }
John Reckf5945a02014-09-05 15:57:47 -070058 ANDROID_API void setAllowRunningAsync(bool mayRunAsync) {
59 mMayRunAsync = mayRunAsync;
60 }
61 bool mayRunAsync() { return mMayRunAsync; }
John Reck8d8af3c2014-07-01 15:23:45 -070062 ANDROID_API void start() { mStagingPlayState = RUNNING; onStagingPlayStateChanged(); }
John Reckd3de42c2014-07-15 14:29:33 -070063 ANDROID_API void end() { mStagingPlayState = FINISHED; onStagingPlayStateChanged(); }
John Reck52244ff2014-05-01 21:27:37 -070064
John Reck8d8af3c2014-07-01 15:23:45 -070065 void attach(RenderNode* target);
66 virtual void onAttached() {}
Chris Craikd41c4d82015-01-05 15:51:13 -080067 void detach() { mTarget = nullptr; }
John Reck119907c2014-08-14 09:02:01 -070068 void pushStaging(AnimationContext& context);
69 bool animate(AnimationContext& context);
John Reckff941dc2014-05-14 16:34:14 -070070
John Reck119907c2014-08-14 09:02:01 -070071 bool isRunning() { return mPlayState == RUNNING; }
John Reck52244ff2014-05-01 21:27:37 -070072 bool isFinished() { return mPlayState == FINISHED; }
John Reckff941dc2014-05-14 16:34:14 -070073 float finalValue() { return mFinalValue; }
John Reck52244ff2014-05-01 21:27:37 -070074
John Recka7c2ea22014-08-08 13:21:00 -070075 ANDROID_API virtual uint32_t dirtyMask() = 0;
John Reck22184722014-06-20 07:19:30 -070076
John Recke2478d42014-09-03 16:46:05 -070077 void forceEndNow(AnimationContext& context);
78
John Reck52244ff2014-05-01 21:27:37 -070079protected:
John Reckff941dc2014-05-14 16:34:14 -070080 BaseRenderNodeAnimator(float finalValue);
81 virtual ~BaseRenderNodeAnimator();
John Reck52244ff2014-05-01 21:27:37 -070082
John Reckff941dc2014-05-14 16:34:14 -070083 virtual float getValue(RenderNode* target) const = 0;
84 virtual void setValue(RenderNode* target, float value) = 0;
John Reck8d8af3c2014-07-01 15:23:45 -070085 RenderNode* target() { return mTarget; }
John Reck52244ff2014-05-01 21:27:37 -070086
John Reck119907c2014-08-14 09:02:01 -070087 void callOnFinishedListener(AnimationContext& context);
John Reck52244ff2014-05-01 21:27:37 -070088
John Reck8d8af3c2014-07-01 15:23:45 -070089 virtual void onStagingPlayStateChanged() {}
90
John Reck52244ff2014-05-01 21:27:37 -070091 enum PlayState {
John Reck68bfe0a2014-06-24 15:34:58 -070092 NOT_STARTED,
John Reck52244ff2014-05-01 21:27:37 -070093 RUNNING,
94 FINISHED,
95 };
96
John Reck8d8af3c2014-07-01 15:23:45 -070097 RenderNode* mTarget;
98
John Reckff941dc2014-05-14 16:34:14 -070099 float mFinalValue;
100 float mDeltaValue;
101 float mFromValue;
102
Chris Craik51d6a3d2014-12-22 17:16:56 -0800103 std::unique_ptr<Interpolator> mInterpolator;
John Reck68bfe0a2014-06-24 15:34:58 -0700104 PlayState mStagingPlayState;
John Reck52244ff2014-05-01 21:27:37 -0700105 PlayState mPlayState;
John Reck68bfe0a2014-06-24 15:34:58 -0700106 bool mHasStartValue;
Alan Viverettead2f8e32014-05-16 13:28:33 -0700107 nsecs_t mStartTime;
Alan Viverettead2f8e32014-05-16 13:28:33 -0700108 nsecs_t mDuration;
109 nsecs_t mStartDelay;
John Reckf5945a02014-09-05 15:57:47 -0700110 bool mMayRunAsync;
John Reck52244ff2014-05-01 21:27:37 -0700111
Alan Viverettead2f8e32014-05-16 13:28:33 -0700112 sp<AnimationListener> mListener;
John Reck68bfe0a2014-06-24 15:34:58 -0700113
114private:
John Reck68bfe0a2014-06-24 15:34:58 -0700115 inline void checkMutable();
John Reck119907c2014-08-14 09:02:01 -0700116 virtual void transitionToRunning(AnimationContext& context);
John Reck8d8af3c2014-07-01 15:23:45 -0700117 void doSetStartValue(float value);
John Reck52244ff2014-05-01 21:27:37 -0700118};
119
John Reck52244ff2014-05-01 21:27:37 -0700120class RenderPropertyAnimator : public BaseRenderNodeAnimator {
121public:
John Recke45b1fd2014-04-15 09:50:16 -0700122 enum RenderProperty {
123 TRANSLATION_X = 0,
124 TRANSLATION_Y,
125 TRANSLATION_Z,
126 SCALE_X,
127 SCALE_Y,
128 ROTATION,
129 ROTATION_X,
130 ROTATION_Y,
131 X,
132 Y,
133 Z,
134 ALPHA,
135 };
136
John Reckff941dc2014-05-14 16:34:14 -0700137 ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue);
138
John Reck22184722014-06-20 07:19:30 -0700139 ANDROID_API virtual uint32_t dirtyMask();
140
John Recke45b1fd2014-04-15 09:50:16 -0700141protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800142 virtual float getValue(RenderNode* target) const override;
143 virtual void setValue(RenderNode* target, float value) override;
144 virtual void onAttached() override;
145 virtual void onStagingPlayStateChanged() override;
John Recke45b1fd2014-04-15 09:50:16 -0700146
147private:
John Reck79c7de72014-05-23 10:33:31 -0700148 typedef bool (RenderProperties::*SetFloatProperty)(float value);
John Reck52244ff2014-05-01 21:27:37 -0700149 typedef float (RenderProperties::*GetFloatProperty)() const;
150
John Reckff941dc2014-05-14 16:34:14 -0700151 struct PropertyAccessors;
152 const PropertyAccessors* mPropertyAccess;
John Reck52244ff2014-05-01 21:27:37 -0700153
154 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[];
155};
156
157class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator {
158public:
159 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property,
John Reckff941dc2014-05-14 16:34:14 -0700160 float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700161
162 ANDROID_API virtual uint32_t dirtyMask();
163
John Reck52244ff2014-05-01 21:27:37 -0700164protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800165 virtual float getValue(RenderNode* target) const override;
166 virtual void setValue(RenderNode* target, float value) override;
John Reck52244ff2014-05-01 21:27:37 -0700167private:
168 sp<CanvasPropertyPrimitive> mProperty;
169};
170
171class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator {
172public:
173 enum PaintField {
174 STROKE_WIDTH = 0,
175 ALPHA,
176 };
177
178 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property,
John Reckff941dc2014-05-14 16:34:14 -0700179 PaintField field, float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700180
181 ANDROID_API virtual uint32_t dirtyMask();
182
John Reck52244ff2014-05-01 21:27:37 -0700183protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800184 virtual float getValue(RenderNode* target) const override;
185 virtual void setValue(RenderNode* target, float value) override;
John Reck52244ff2014-05-01 21:27:37 -0700186private:
187 sp<CanvasPropertyPaint> mProperty;
188 PaintField mField;
John Recke45b1fd2014-04-15 09:50:16 -0700189};
190
John Reckd3de42c2014-07-15 14:29:33 -0700191class RevealAnimator : public BaseRenderNodeAnimator {
192public:
Chris Craikaf4d04c2014-07-29 12:50:14 -0700193 ANDROID_API RevealAnimator(int centerX, int centerY,
John Reckd3de42c2014-07-15 14:29:33 -0700194 float startValue, float finalValue);
John Recka7c2ea22014-08-08 13:21:00 -0700195
196 ANDROID_API virtual uint32_t dirtyMask();
197
John Reckd3de42c2014-07-15 14:29:33 -0700198protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800199 virtual float getValue(RenderNode* target) const override;
200 virtual void setValue(RenderNode* target, float value) override;
John Reckd3de42c2014-07-15 14:29:33 -0700201
202private:
203 int mCenterX, mCenterY;
John Reckd3de42c2014-07-15 14:29:33 -0700204};
205
John Recke45b1fd2014-04-15 09:50:16 -0700206} /* namespace uirenderer */
207} /* namespace android */
208
209#endif /* ANIMATOR_H */