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