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