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