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 | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 31 | class RenderNode; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 32 | class RenderProperties; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 33 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 34 | class AnimationListener : public VirtualLightRefBase { |
| 35 | public: |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 36 | ANDROID_API virtual void onAnimationFinished(BaseRenderNodeAnimator*) = 0; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 37 | protected: |
| 38 | ANDROID_API virtual ~AnimationListener() {} |
| 39 | }; |
| 40 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 41 | class BaseRenderNodeAnimator : public VirtualLightRefBase { |
| 42 | PREVENT_COPY_AND_ASSIGN(BaseRenderNodeAnimator); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 43 | public: |
John Reck | c6b3264 | 2014-06-02 11:00:09 -0700 | [diff] [blame] | 44 | ANDROID_API void setStartValue(float value); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 45 | ANDROID_API void setInterpolator(Interpolator* interpolator); |
| 46 | ANDROID_API void setDuration(nsecs_t durationInMs); |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 47 | ANDROID_API nsecs_t duration() { return mDuration; } |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 48 | ANDROID_API void setStartDelay(nsecs_t startDelayInMs); |
| 49 | ANDROID_API nsecs_t startDelay() { return mStartDelay; } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 50 | ANDROID_API void setListener(AnimationListener* listener) { |
| 51 | mListener = listener; |
| 52 | } |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame^] | 53 | ANDROID_API void start() { mStagingPlayState = RUNNING; } |
| 54 | ANDROID_API void cancel() { mStagingPlayState = FINISHED; } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 55 | |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame^] | 56 | virtual void onAttached(RenderNode* target) {} |
| 57 | virtual void pushStaging(RenderNode* target, TreeInfo& info); |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 58 | bool animate(RenderNode* target, TreeInfo& info); |
| 59 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 60 | bool isFinished() { return mPlayState == FINISHED; } |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 61 | float finalValue() { return mFinalValue; } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 62 | |
John Reck | 2218472 | 2014-06-20 07:19:30 -0700 | [diff] [blame] | 63 | ANDROID_API virtual uint32_t dirtyMask() { return 0; } |
| 64 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 65 | protected: |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 66 | BaseRenderNodeAnimator(float finalValue); |
| 67 | virtual ~BaseRenderNodeAnimator(); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 68 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 69 | virtual float getValue(RenderNode* target) const = 0; |
| 70 | virtual void setValue(RenderNode* target, float value) = 0; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 71 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 72 | void callOnFinishedListener(TreeInfo& info); |
| 73 | |
| 74 | enum PlayState { |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame^] | 75 | NOT_STARTED, |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 76 | RUNNING, |
| 77 | FINISHED, |
| 78 | }; |
| 79 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 80 | float mFinalValue; |
| 81 | float mDeltaValue; |
| 82 | float mFromValue; |
| 83 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 84 | Interpolator* mInterpolator; |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame^] | 85 | PlayState mStagingPlayState; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 86 | PlayState mPlayState; |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame^] | 87 | bool mHasStartValue; |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 88 | nsecs_t mStartTime; |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 89 | nsecs_t mDuration; |
| 90 | nsecs_t mStartDelay; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 91 | |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 92 | sp<AnimationListener> mListener; |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame^] | 93 | |
| 94 | private: |
| 95 | void doSetStartValue(float value); |
| 96 | inline void checkMutable(); |
| 97 | void transitionToRunning(TreeInfo& info); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 100 | class RenderPropertyAnimator : public BaseRenderNodeAnimator { |
| 101 | public: |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 102 | enum RenderProperty { |
| 103 | TRANSLATION_X = 0, |
| 104 | TRANSLATION_Y, |
| 105 | TRANSLATION_Z, |
| 106 | SCALE_X, |
| 107 | SCALE_Y, |
| 108 | ROTATION, |
| 109 | ROTATION_X, |
| 110 | ROTATION_Y, |
| 111 | X, |
| 112 | Y, |
| 113 | Z, |
| 114 | ALPHA, |
| 115 | }; |
| 116 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 117 | ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue); |
| 118 | |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame^] | 119 | virtual void onAttached(RenderNode* target); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 120 | |
John Reck | 2218472 | 2014-06-20 07:19:30 -0700 | [diff] [blame] | 121 | ANDROID_API virtual uint32_t dirtyMask(); |
| 122 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 123 | protected: |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 124 | virtual float getValue(RenderNode* target) const; |
| 125 | virtual void setValue(RenderNode* target, float value); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 126 | |
| 127 | private: |
John Reck | 79c7de7 | 2014-05-23 10:33:31 -0700 | [diff] [blame] | 128 | typedef bool (RenderProperties::*SetFloatProperty)(float value); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 129 | typedef float (RenderProperties::*GetFloatProperty)() const; |
| 130 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 131 | struct PropertyAccessors; |
| 132 | const PropertyAccessors* mPropertyAccess; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 133 | |
| 134 | static const PropertyAccessors PROPERTY_ACCESSOR_LUT[]; |
| 135 | }; |
| 136 | |
| 137 | class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator { |
| 138 | public: |
| 139 | ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property, |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 140 | float finalValue); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 141 | protected: |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 142 | virtual float getValue(RenderNode* target) const; |
| 143 | virtual void setValue(RenderNode* target, float value); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 144 | private: |
| 145 | sp<CanvasPropertyPrimitive> mProperty; |
| 146 | }; |
| 147 | |
| 148 | class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator { |
| 149 | public: |
| 150 | enum PaintField { |
| 151 | STROKE_WIDTH = 0, |
| 152 | ALPHA, |
| 153 | }; |
| 154 | |
| 155 | ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property, |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 156 | PaintField field, float finalValue); |
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<CanvasPropertyPaint> mProperty; |
| 162 | PaintField mField; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | } /* namespace uirenderer */ |
| 166 | } /* namespace android */ |
| 167 | |
| 168 | #endif /* ANIMATOR_H */ |