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