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: |
| 36 | ANDROID_API virtual void onAnimationFinished(BaseAnimator*) = 0; |
| 37 | protected: |
| 38 | ANDROID_API virtual ~AnimationListener() {} |
| 39 | }; |
| 40 | |
| 41 | // Helper class to contain generic animator helpers |
| 42 | class BaseAnimator : public VirtualLightRefBase { |
| 43 | PREVENT_COPY_AND_ASSIGN(BaseAnimator); |
| 44 | public: |
| 45 | |
| 46 | ANDROID_API void setInterpolator(Interpolator* interpolator); |
| 47 | ANDROID_API void setDuration(nsecs_t durationInMs); |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame^] | 48 | ANDROID_API nsecs_t duration() { return mDuration; } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 49 | ANDROID_API void setListener(AnimationListener* listener) { |
| 50 | mListener = listener; |
| 51 | } |
| 52 | |
| 53 | bool isFinished() { return mPlayState == FINISHED; } |
| 54 | |
| 55 | protected: |
| 56 | BaseAnimator(); |
| 57 | virtual ~BaseAnimator(); |
| 58 | |
| 59 | // This is the main animation entrypoint that subclasses should call |
| 60 | // to generate the onAnimation* lifecycle events |
| 61 | // Returns true if the animation has finished, false otherwise |
| 62 | bool animateFrame(TreeInfo& info); |
| 63 | |
| 64 | // Called when PlayState switches from PENDING to RUNNING |
| 65 | virtual void onAnimationStarted() {} |
| 66 | virtual void onAnimationUpdated(float fraction) = 0; |
| 67 | virtual void onAnimationFinished() {} |
| 68 | |
| 69 | private: |
| 70 | void callOnFinishedListener(TreeInfo& info); |
| 71 | |
| 72 | enum PlayState { |
| 73 | PENDING, |
| 74 | RUNNING, |
| 75 | FINISHED, |
| 76 | }; |
| 77 | |
| 78 | Interpolator* mInterpolator; |
| 79 | PlayState mPlayState; |
| 80 | long mStartTime; |
| 81 | long mDuration; |
| 82 | |
| 83 | sp<AnimationListener> mListener; |
| 84 | }; |
| 85 | |
| 86 | class BaseRenderNodeAnimator : public BaseAnimator { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 87 | public: |
| 88 | // Since the UI thread doesn't necessarily know what the current values |
| 89 | // actually are and thus can't do the calculations, this is used to inform |
| 90 | // the animator how to lazy-resolve the input value |
| 91 | enum DeltaValueType { |
| 92 | // The delta value represents an absolute value endpoint |
| 93 | // mDeltaValue needs to be recalculated to be mDelta = (mDelta - fromValue) |
| 94 | // in onAnimationStarted() |
| 95 | ABSOLUTE = 0, |
| 96 | // The final value represents an offset from the current value |
| 97 | // No recalculation is needed |
| 98 | DELTA, |
| 99 | }; |
| 100 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 101 | bool animate(RenderNode* target, TreeInfo& info); |
| 102 | |
| 103 | protected: |
| 104 | BaseRenderNodeAnimator(DeltaValueType deltaType, float deltaValue); |
| 105 | |
| 106 | RenderNode* target() const { return mTarget; } |
| 107 | virtual float getValue() const = 0; |
| 108 | virtual void setValue(float value) = 0; |
| 109 | |
| 110 | private: |
| 111 | virtual void onAnimationStarted(); |
| 112 | virtual void onAnimationUpdated(float fraction); |
| 113 | |
| 114 | // mTarget is only valid inside animate() |
| 115 | RenderNode* mTarget; |
| 116 | |
| 117 | BaseRenderNodeAnimator::DeltaValueType mDeltaValueType; |
| 118 | float mDeltaValue; |
| 119 | float mFromValue; |
| 120 | }; |
| 121 | |
| 122 | class RenderPropertyAnimator : public BaseRenderNodeAnimator { |
| 123 | public: |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 124 | enum RenderProperty { |
| 125 | TRANSLATION_X = 0, |
| 126 | TRANSLATION_Y, |
| 127 | TRANSLATION_Z, |
| 128 | SCALE_X, |
| 129 | SCALE_Y, |
| 130 | ROTATION, |
| 131 | ROTATION_X, |
| 132 | ROTATION_Y, |
| 133 | X, |
| 134 | Y, |
| 135 | Z, |
| 136 | ALPHA, |
| 137 | }; |
| 138 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 139 | ANDROID_API RenderPropertyAnimator(RenderProperty property, |
| 140 | DeltaValueType deltaType, float deltaValue); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 141 | |
| 142 | protected: |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 143 | ANDROID_API virtual float getValue() const; |
| 144 | ANDROID_API virtual void setValue(float value); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 145 | |
| 146 | private: |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 147 | typedef void (RenderProperties::*SetFloatProperty)(float value); |
| 148 | typedef float (RenderProperties::*GetFloatProperty)() const; |
| 149 | |
| 150 | struct PropertyAccessors { |
| 151 | GetFloatProperty getter; |
| 152 | SetFloatProperty setter; |
| 153 | }; |
| 154 | |
| 155 | PropertyAccessors mPropertyAccess; |
| 156 | |
| 157 | static const PropertyAccessors PROPERTY_ACCESSOR_LUT[]; |
| 158 | }; |
| 159 | |
| 160 | class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator { |
| 161 | public: |
| 162 | ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property, |
| 163 | DeltaValueType deltaType, float deltaValue); |
| 164 | protected: |
| 165 | ANDROID_API virtual float getValue() const; |
| 166 | ANDROID_API virtual void setValue(float value); |
| 167 | private: |
| 168 | sp<CanvasPropertyPrimitive> mProperty; |
| 169 | }; |
| 170 | |
| 171 | class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator { |
| 172 | public: |
| 173 | enum PaintField { |
| 174 | STROKE_WIDTH = 0, |
| 175 | ALPHA, |
| 176 | }; |
| 177 | |
| 178 | ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property, |
| 179 | PaintField field, DeltaValueType deltaType, float deltaValue); |
| 180 | protected: |
| 181 | ANDROID_API virtual float getValue() const; |
| 182 | ANDROID_API virtual void setValue(float value); |
| 183 | private: |
| 184 | sp<CanvasPropertyPaint> mProperty; |
| 185 | PaintField mField; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | } /* namespace uirenderer */ |
| 189 | } /* namespace android */ |
| 190 | |
| 191 | #endif /* ANIMATOR_H */ |