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