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