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 | |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 19 | #include <memory> |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 20 | #include <cutils/compiler.h> |
John Reck | 9fa4071 | 2014-05-09 15:26:59 -0700 | [diff] [blame] | 21 | #include <utils/RefBase.h> |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 22 | #include <utils/StrongPointer.h> |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 23 | #include <utils/Timers.h> |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 24 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 25 | #include "utils/Macros.h" |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 26 | |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 27 | #include <vector> |
| 28 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | namespace uirenderer { |
| 31 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 32 | class AnimationContext; |
| 33 | class BaseRenderNodeAnimator; |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 34 | class CanvasPropertyPrimitive; |
| 35 | class CanvasPropertyPaint; |
| 36 | class Interpolator; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 37 | class RenderNode; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 38 | class RenderProperties; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 39 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 40 | class AnimationListener : public VirtualLightRefBase { |
| 41 | public: |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 42 | ANDROID_API virtual void onAnimationFinished(BaseRenderNodeAnimator*) = 0; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 43 | protected: |
| 44 | ANDROID_API virtual ~AnimationListener() {} |
| 45 | }; |
| 46 | |
Doris Liu | f7167e8 | 2016-08-03 17:54:28 -0700 | [diff] [blame] | 47 | enum class RepeatMode { |
| 48 | // These are the same values as the RESTART and REVERSE in ValueAnimator.java. |
| 49 | Restart = 1, |
| 50 | Reverse = 2 |
| 51 | }; |
| 52 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 53 | class BaseRenderNodeAnimator : public VirtualLightRefBase { |
| 54 | PREVENT_COPY_AND_ASSIGN(BaseRenderNodeAnimator); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 55 | public: |
John Reck | c6b3264 | 2014-06-02 11:00:09 -0700 | [diff] [blame] | 56 | ANDROID_API void setStartValue(float value); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 57 | ANDROID_API void setInterpolator(Interpolator* interpolator); |
| 58 | ANDROID_API void setDuration(nsecs_t durationInMs); |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 59 | ANDROID_API nsecs_t duration() { return mDuration; } |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 60 | ANDROID_API void setStartDelay(nsecs_t startDelayInMs); |
| 61 | ANDROID_API nsecs_t startDelay() { return mStartDelay; } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 62 | ANDROID_API void setListener(AnimationListener* listener) { |
| 63 | mListener = listener; |
| 64 | } |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 65 | AnimationListener* listener() { return mListener.get(); } |
John Reck | f5945a0 | 2014-09-05 15:57:47 -0700 | [diff] [blame] | 66 | ANDROID_API void setAllowRunningAsync(bool mayRunAsync) { |
| 67 | mMayRunAsync = mayRunAsync; |
| 68 | } |
| 69 | bool mayRunAsync() { return mMayRunAsync; } |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 70 | ANDROID_API void start(); |
Doris Liu | 718cd3e | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 71 | ANDROID_API virtual void reset(); |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 72 | ANDROID_API void reverse(); |
| 73 | // Terminates the animation at its current progress. |
| 74 | ANDROID_API void cancel(); |
| 75 | |
| 76 | // Terminates the animation and skip to the end of the animation. |
Doris Liu | 718cd3e | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 77 | ANDROID_API virtual void end(); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 78 | |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 79 | void attach(RenderNode* target); |
| 80 | virtual void onAttached() {} |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 81 | void detach() { mTarget = nullptr; } |
Doris Liu | 718cd3e | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 82 | ANDROID_API void pushStaging(AnimationContext& context); |
| 83 | ANDROID_API bool animate(AnimationContext& context); |
| 84 | |
| 85 | // Returns the remaining time in ms for the animation. Note this should only be called during |
| 86 | // an animation on RenderThread. |
| 87 | ANDROID_API nsecs_t getRemainingPlayTime(); |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 88 | |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 89 | bool isRunning() { return mPlayState == PlayState::Running |
| 90 | || mPlayState == PlayState::Reversing; } |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 91 | bool isFinished() { return mPlayState == PlayState::Finished; } |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 92 | float finalValue() { return mFinalValue; } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 93 | |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 94 | ANDROID_API virtual uint32_t dirtyMask() = 0; |
John Reck | 2218472 | 2014-06-20 07:19:30 -0700 | [diff] [blame] | 95 | |
John Reck | e2478d4 | 2014-09-03 16:46:05 -0700 | [diff] [blame] | 96 | void forceEndNow(AnimationContext& context); |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 97 | RenderNode* target() { return mTarget; } |
Doris Liu | 8b08320 | 2016-02-19 21:46:06 +0000 | [diff] [blame] | 98 | RenderNode* stagingTarget() { return mStagingTarget; } |
John Reck | e2478d4 | 2014-09-03 16:46:05 -0700 | [diff] [blame] | 99 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 100 | protected: |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 101 | // PlayState is used by mStagingPlayState and mPlayState to track the state initiated from UI |
| 102 | // thread and Render Thread animation state, respectively. |
| 103 | // From the UI thread, mStagingPlayState transition looks like |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 104 | // NotStarted -> Running/Reversing -> Finished |
| 105 | // ^ | |
| 106 | // | | |
| 107 | // ---------------------- |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 108 | // Note: For mStagingState, the Finished state (optional) is only set when the animation is |
| 109 | // terminated by user. |
| 110 | // |
| 111 | // On Render Thread, mPlayState transition: |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 112 | // NotStart -> Running/Reversing-> Finished |
| 113 | // ^ | |
| 114 | // | | |
| 115 | // ------------------ |
| 116 | // Note that if the animation is in Running/Reversing state, calling start or reverse again |
| 117 | // would do nothing if the animation has the same play direction as the request; otherwise, |
| 118 | // the animation would start from where it is and change direction (i.e. Reversing <-> Running) |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 119 | |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 120 | enum class PlayState { |
| 121 | NotStarted, |
| 122 | Running, |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 123 | Reversing, |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 124 | Finished, |
| 125 | }; |
| 126 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 127 | BaseRenderNodeAnimator(float finalValue); |
| 128 | virtual ~BaseRenderNodeAnimator(); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 129 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 130 | virtual float getValue(RenderNode* target) const = 0; |
| 131 | virtual void setValue(RenderNode* target, float value) = 0; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 132 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 133 | void callOnFinishedListener(AnimationContext& context); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 134 | |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 135 | virtual void onStagingPlayStateChanged() {} |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 136 | virtual void onPlayTimeChanged(nsecs_t playTime) {} |
Doris Liu | 8b08320 | 2016-02-19 21:46:06 +0000 | [diff] [blame] | 137 | virtual void onPushStaging() {} |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 138 | |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 139 | RenderNode* mTarget; |
Doris Liu | 8b08320 | 2016-02-19 21:46:06 +0000 | [diff] [blame] | 140 | RenderNode* mStagingTarget; |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 141 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 142 | float mFinalValue; |
| 143 | float mDeltaValue; |
| 144 | float mFromValue; |
| 145 | |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 146 | std::unique_ptr<Interpolator> mInterpolator; |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 147 | PlayState mStagingPlayState; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 148 | PlayState mPlayState; |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 149 | bool mHasStartValue; |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 150 | nsecs_t mStartTime; |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 151 | nsecs_t mDuration; |
| 152 | nsecs_t mStartDelay; |
John Reck | f5945a0 | 2014-09-05 15:57:47 -0700 | [diff] [blame] | 153 | bool mMayRunAsync; |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 154 | // Play Time tracks the progress of animation, it should always be [0, mDuration], 0 being |
| 155 | // the beginning of the animation, will reach mDuration at the end of an animation. |
| 156 | nsecs_t mPlayTime; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 157 | |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 158 | sp<AnimationListener> mListener; |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 159 | |
| 160 | private: |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 161 | enum class Request { |
| 162 | Start, |
| 163 | Reverse, |
| 164 | Reset, |
| 165 | Cancel, |
| 166 | End |
| 167 | }; |
Doris Liu | 6725d58 | 2016-08-04 13:20:17 -0700 | [diff] [blame] | 168 | |
| 169 | // Defines different actions upon finish. |
| 170 | enum class Action { |
| 171 | // For animations that got canceled or finished normally. no more action needs to be done. |
| 172 | None, |
| 173 | // For animations that get reset, the reset will happen in the next animation pulse. |
| 174 | Reset, |
| 175 | // For animations being ended, in the next animation pulse the animation will skip to end. |
| 176 | End |
| 177 | }; |
| 178 | |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 179 | inline void checkMutable(); |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 180 | virtual void transitionToRunning(AnimationContext& context); |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 181 | void doSetStartValue(float value); |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 182 | bool updatePlayTime(nsecs_t playTime); |
| 183 | void resolveStagingRequest(Request request); |
| 184 | |
| 185 | std::vector<Request> mStagingRequests; |
Doris Liu | 6725d58 | 2016-08-04 13:20:17 -0700 | [diff] [blame] | 186 | Action mPendingActionUponFinish = Action::None; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 187 | }; |
| 188 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 189 | class RenderPropertyAnimator : public BaseRenderNodeAnimator { |
| 190 | public: |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 191 | enum RenderProperty { |
| 192 | TRANSLATION_X = 0, |
| 193 | TRANSLATION_Y, |
| 194 | TRANSLATION_Z, |
| 195 | SCALE_X, |
| 196 | SCALE_Y, |
| 197 | ROTATION, |
| 198 | ROTATION_X, |
| 199 | ROTATION_Y, |
| 200 | X, |
| 201 | Y, |
| 202 | Z, |
| 203 | ALPHA, |
| 204 | }; |
| 205 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 206 | ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue); |
| 207 | |
John Reck | 2218472 | 2014-06-20 07:19:30 -0700 | [diff] [blame] | 208 | ANDROID_API virtual uint32_t dirtyMask(); |
| 209 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 210 | protected: |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 211 | virtual float getValue(RenderNode* target) const override; |
| 212 | virtual void setValue(RenderNode* target, float value) override; |
| 213 | virtual void onAttached() override; |
| 214 | virtual void onStagingPlayStateChanged() override; |
Doris Liu | 8b08320 | 2016-02-19 21:46:06 +0000 | [diff] [blame] | 215 | virtual void onPushStaging() override; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 216 | |
| 217 | private: |
John Reck | 79c7de7 | 2014-05-23 10:33:31 -0700 | [diff] [blame] | 218 | typedef bool (RenderProperties::*SetFloatProperty)(float value); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 219 | typedef float (RenderProperties::*GetFloatProperty)() const; |
| 220 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 221 | struct PropertyAccessors; |
| 222 | const PropertyAccessors* mPropertyAccess; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 223 | |
| 224 | static const PropertyAccessors PROPERTY_ACCESSOR_LUT[]; |
Doris Liu | 8b08320 | 2016-02-19 21:46:06 +0000 | [diff] [blame] | 225 | bool mShouldSyncPropertyFields = false; |
| 226 | bool mShouldUpdateStagingProperties = false; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator { |
| 230 | public: |
| 231 | ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property, |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 232 | float finalValue); |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 233 | |
| 234 | ANDROID_API virtual uint32_t dirtyMask(); |
| 235 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 236 | protected: |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 237 | virtual float getValue(RenderNode* target) const override; |
| 238 | virtual void setValue(RenderNode* target, float value) override; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 239 | private: |
| 240 | sp<CanvasPropertyPrimitive> mProperty; |
| 241 | }; |
| 242 | |
| 243 | class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator { |
| 244 | public: |
| 245 | enum PaintField { |
| 246 | STROKE_WIDTH = 0, |
| 247 | ALPHA, |
| 248 | }; |
| 249 | |
| 250 | ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property, |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 251 | PaintField field, float finalValue); |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 252 | |
| 253 | ANDROID_API virtual uint32_t dirtyMask(); |
| 254 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 255 | protected: |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 256 | virtual float getValue(RenderNode* target) const override; |
| 257 | virtual void setValue(RenderNode* target, float value) override; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 258 | private: |
| 259 | sp<CanvasPropertyPaint> mProperty; |
| 260 | PaintField mField; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 261 | }; |
| 262 | |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 263 | class RevealAnimator : public BaseRenderNodeAnimator { |
| 264 | public: |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 265 | ANDROID_API RevealAnimator(int centerX, int centerY, |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 266 | float startValue, float finalValue); |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 267 | |
| 268 | ANDROID_API virtual uint32_t dirtyMask(); |
| 269 | |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 270 | protected: |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 271 | virtual float getValue(RenderNode* target) const override; |
| 272 | virtual void setValue(RenderNode* target, float value) override; |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 273 | |
| 274 | private: |
| 275 | int mCenterX, mCenterY; |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 276 | }; |
| 277 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 278 | } /* namespace uirenderer */ |
| 279 | } /* namespace android */ |
| 280 | |
| 281 | #endif /* ANIMATOR_H */ |