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