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