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