John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 1 | /* |
| 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 Reck | 9fa4071 | 2014-05-09 15:26:59 -0700 | [diff] [blame] | 20 | #include <utils/RefBase.h> |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 21 | #include <utils/StrongPointer.h> |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 22 | #include <utils/Timers.h> |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 23 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 24 | #include "utils/Macros.h" |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace uirenderer { |
| 28 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 29 | class AnimationContext; |
| 30 | class BaseRenderNodeAnimator; |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 31 | class CanvasPropertyPrimitive; |
| 32 | class CanvasPropertyPaint; |
| 33 | class Interpolator; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 34 | class RenderNode; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 35 | class RenderProperties; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 36 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 37 | class AnimationListener : public VirtualLightRefBase { |
| 38 | public: |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 39 | ANDROID_API virtual void onAnimationFinished(BaseRenderNodeAnimator*) = 0; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 40 | protected: |
| 41 | ANDROID_API virtual ~AnimationListener() {} |
| 42 | }; |
| 43 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 44 | class BaseRenderNodeAnimator : public VirtualLightRefBase { |
| 45 | PREVENT_COPY_AND_ASSIGN(BaseRenderNodeAnimator); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 46 | public: |
John Reck | c6b3264 | 2014-06-02 11:00:09 -0700 | [diff] [blame] | 47 | ANDROID_API void setStartValue(float value); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 48 | ANDROID_API void setInterpolator(Interpolator* interpolator); |
| 49 | ANDROID_API void setDuration(nsecs_t durationInMs); |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 50 | ANDROID_API nsecs_t duration() { return mDuration; } |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 51 | ANDROID_API void setStartDelay(nsecs_t startDelayInMs); |
| 52 | ANDROID_API nsecs_t startDelay() { return mStartDelay; } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 53 | ANDROID_API void setListener(AnimationListener* listener) { |
| 54 | mListener = listener; |
| 55 | } |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 56 | AnimationListener* listener() { return mListener.get(); } |
John Reck | f5945a0 | 2014-09-05 15:57:47 -0700 | [diff] [blame] | 57 | ANDROID_API void setAllowRunningAsync(bool mayRunAsync) { |
| 58 | mMayRunAsync = mayRunAsync; |
| 59 | } |
| 60 | bool mayRunAsync() { return mMayRunAsync; } |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 61 | ANDROID_API void start() { mStagingPlayState = RUNNING; onStagingPlayStateChanged(); } |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 62 | ANDROID_API void end() { mStagingPlayState = FINISHED; onStagingPlayStateChanged(); } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 63 | |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 64 | void attach(RenderNode* target); |
| 65 | virtual void onAttached() {} |
| 66 | void detach() { mTarget = 0; } |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 67 | void pushStaging(AnimationContext& context); |
| 68 | bool animate(AnimationContext& context); |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 69 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 70 | bool isRunning() { return mPlayState == RUNNING; } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 71 | bool isFinished() { return mPlayState == FINISHED; } |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 72 | float finalValue() { return mFinalValue; } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 73 | |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 74 | ANDROID_API virtual uint32_t dirtyMask() = 0; |
John Reck | 2218472 | 2014-06-20 07:19:30 -0700 | [diff] [blame] | 75 | |
John Reck | e2478d4 | 2014-09-03 16:46:05 -0700 | [diff] [blame] | 76 | void forceEndNow(AnimationContext& context); |
| 77 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 78 | protected: |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 79 | BaseRenderNodeAnimator(float finalValue); |
| 80 | virtual ~BaseRenderNodeAnimator(); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 81 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 82 | virtual float getValue(RenderNode* target) const = 0; |
| 83 | virtual void setValue(RenderNode* target, float value) = 0; |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 84 | RenderNode* target() { return mTarget; } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 85 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 86 | void callOnFinishedListener(AnimationContext& context); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 87 | |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 88 | virtual void onStagingPlayStateChanged() {} |
| 89 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 90 | enum PlayState { |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 91 | NOT_STARTED, |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 92 | RUNNING, |
| 93 | FINISHED, |
| 94 | }; |
| 95 | |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 96 | RenderNode* mTarget; |
| 97 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 98 | float mFinalValue; |
| 99 | float mDeltaValue; |
| 100 | float mFromValue; |
| 101 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 102 | Interpolator* mInterpolator; |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 103 | PlayState mStagingPlayState; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 104 | PlayState mPlayState; |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 105 | bool mHasStartValue; |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 106 | nsecs_t mStartTime; |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 107 | nsecs_t mDuration; |
| 108 | nsecs_t mStartDelay; |
John Reck | f5945a0 | 2014-09-05 15:57:47 -0700 | [diff] [blame] | 109 | bool mMayRunAsync; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 110 | |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 111 | sp<AnimationListener> mListener; |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 112 | |
| 113 | private: |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 114 | inline void checkMutable(); |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 115 | virtual void transitionToRunning(AnimationContext& context); |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 116 | void doSetStartValue(float value); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 119 | class RenderPropertyAnimator : public BaseRenderNodeAnimator { |
| 120 | public: |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 121 | enum RenderProperty { |
| 122 | TRANSLATION_X = 0, |
| 123 | TRANSLATION_Y, |
| 124 | TRANSLATION_Z, |
| 125 | SCALE_X, |
| 126 | SCALE_Y, |
| 127 | ROTATION, |
| 128 | ROTATION_X, |
| 129 | ROTATION_Y, |
| 130 | X, |
| 131 | Y, |
| 132 | Z, |
| 133 | ALPHA, |
| 134 | }; |
| 135 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 136 | ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue); |
| 137 | |
John Reck | 2218472 | 2014-06-20 07:19:30 -0700 | [diff] [blame] | 138 | ANDROID_API virtual uint32_t dirtyMask(); |
| 139 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 140 | protected: |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 141 | virtual float getValue(RenderNode* target) const; |
| 142 | virtual void setValue(RenderNode* target, float value); |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 143 | virtual void onAttached(); |
| 144 | virtual void onStagingPlayStateChanged(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 145 | |
| 146 | private: |
John Reck | 79c7de7 | 2014-05-23 10:33:31 -0700 | [diff] [blame] | 147 | typedef bool (RenderProperties::*SetFloatProperty)(float value); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 148 | typedef float (RenderProperties::*GetFloatProperty)() const; |
| 149 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 150 | struct PropertyAccessors; |
| 151 | const PropertyAccessors* mPropertyAccess; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 152 | |
| 153 | static const PropertyAccessors PROPERTY_ACCESSOR_LUT[]; |
| 154 | }; |
| 155 | |
| 156 | class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator { |
| 157 | public: |
| 158 | ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property, |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 159 | float finalValue); |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 160 | |
| 161 | ANDROID_API virtual uint32_t dirtyMask(); |
| 162 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 163 | protected: |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 164 | virtual float getValue(RenderNode* target) const; |
| 165 | virtual void setValue(RenderNode* target, float value); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 166 | private: |
| 167 | sp<CanvasPropertyPrimitive> mProperty; |
| 168 | }; |
| 169 | |
| 170 | class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator { |
| 171 | public: |
| 172 | enum PaintField { |
| 173 | STROKE_WIDTH = 0, |
| 174 | ALPHA, |
| 175 | }; |
| 176 | |
| 177 | ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property, |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 178 | PaintField field, float finalValue); |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 179 | |
| 180 | ANDROID_API virtual uint32_t dirtyMask(); |
| 181 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 182 | protected: |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 183 | virtual float getValue(RenderNode* target) const; |
| 184 | virtual void setValue(RenderNode* target, float value); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 185 | private: |
| 186 | sp<CanvasPropertyPaint> mProperty; |
| 187 | PaintField mField; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 190 | class RevealAnimator : public BaseRenderNodeAnimator { |
| 191 | public: |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 192 | ANDROID_API RevealAnimator(int centerX, int centerY, |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 193 | float startValue, float finalValue); |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 194 | |
| 195 | ANDROID_API virtual uint32_t dirtyMask(); |
| 196 | |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 197 | protected: |
| 198 | virtual float getValue(RenderNode* target) const; |
| 199 | virtual void setValue(RenderNode* target, float value); |
| 200 | |
| 201 | private: |
| 202 | int mCenterX, mCenterY; |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 203 | }; |
| 204 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 205 | } /* namespace uirenderer */ |
| 206 | } /* namespace android */ |
| 207 | |
| 208 | #endif /* ANIMATOR_H */ |