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 | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 44 | ANDROID_API void setInterpolator(Interpolator* interpolator); |
| 45 | ANDROID_API void setDuration(nsecs_t durationInMs); |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 46 | ANDROID_API nsecs_t duration() { return mDuration; } |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame^] | 47 | ANDROID_API void setStartDelay(nsecs_t startDelayInMs); |
| 48 | ANDROID_API nsecs_t startDelay() { return mStartDelay; } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 49 | ANDROID_API void setListener(AnimationListener* listener) { |
| 50 | mListener = listener; |
| 51 | } |
| 52 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 53 | ANDROID_API virtual void onAttached(RenderNode* target) {} |
| 54 | |
| 55 | // Guaranteed to happen before the staging push |
| 56 | void setupStartValueIfNecessary(RenderNode* target, TreeInfo& info); |
| 57 | |
| 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 | |
| 63 | protected: |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 64 | BaseRenderNodeAnimator(float finalValue); |
| 65 | virtual ~BaseRenderNodeAnimator(); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 66 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 67 | void setStartValue(float value); |
| 68 | virtual float getValue(RenderNode* target) const = 0; |
| 69 | virtual void setValue(RenderNode* target, float value) = 0; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 70 | |
| 71 | private: |
| 72 | void callOnFinishedListener(TreeInfo& info); |
| 73 | |
| 74 | enum PlayState { |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 75 | NEEDS_START, |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 76 | PENDING, |
| 77 | RUNNING, |
| 78 | FINISHED, |
| 79 | }; |
| 80 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 81 | float mFinalValue; |
| 82 | float mDeltaValue; |
| 83 | float mFromValue; |
| 84 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 85 | Interpolator* mInterpolator; |
| 86 | PlayState mPlayState; |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame^] | 87 | nsecs_t mStartTime; |
| 88 | nsecs_t mDelayUntil; |
| 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 | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 95 | class RenderPropertyAnimator : public BaseRenderNodeAnimator { |
| 96 | public: |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 97 | enum RenderProperty { |
| 98 | TRANSLATION_X = 0, |
| 99 | TRANSLATION_Y, |
| 100 | TRANSLATION_Z, |
| 101 | SCALE_X, |
| 102 | SCALE_Y, |
| 103 | ROTATION, |
| 104 | ROTATION_X, |
| 105 | ROTATION_Y, |
| 106 | X, |
| 107 | Y, |
| 108 | Z, |
| 109 | ALPHA, |
| 110 | }; |
| 111 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 112 | ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue); |
| 113 | |
| 114 | ANDROID_API virtual void onAttached(RenderNode* target); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 115 | |
| 116 | protected: |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 117 | virtual float getValue(RenderNode* target) const; |
| 118 | virtual void setValue(RenderNode* target, float value); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 119 | |
| 120 | private: |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 121 | typedef void (RenderProperties::*SetFloatProperty)(float value); |
| 122 | typedef float (RenderProperties::*GetFloatProperty)() const; |
| 123 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 124 | struct PropertyAccessors; |
| 125 | const PropertyAccessors* mPropertyAccess; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 126 | |
| 127 | static const PropertyAccessors PROPERTY_ACCESSOR_LUT[]; |
| 128 | }; |
| 129 | |
| 130 | class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator { |
| 131 | public: |
| 132 | ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property, |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 133 | float finalValue); |
John Reck | 52244ff | 2014-05-01 21:27:37 -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 | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 137 | private: |
| 138 | sp<CanvasPropertyPrimitive> mProperty; |
| 139 | }; |
| 140 | |
| 141 | class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator { |
| 142 | public: |
| 143 | enum PaintField { |
| 144 | STROKE_WIDTH = 0, |
| 145 | ALPHA, |
| 146 | }; |
| 147 | |
| 148 | ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property, |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 149 | PaintField field, float finalValue); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 150 | protected: |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 151 | virtual float getValue(RenderNode* target) const; |
| 152 | virtual void setValue(RenderNode* target, float value); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 153 | private: |
| 154 | sp<CanvasPropertyPaint> mProperty; |
| 155 | PaintField mField; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | } /* namespace uirenderer */ |
| 159 | } /* namespace android */ |
| 160 | |
| 161 | #endif /* ANIMATOR_H */ |