Animator.h revision ad2f8e334f3ef22d3e412b0660a2e1f996f94116
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> 20#include <utils/RefBase.h> 21#include <utils/StrongPointer.h> 22 23#include "CanvasProperty.h" 24#include "Interpolator.h" 25#include "TreeInfo.h" 26#include "utils/Macros.h" 27 28namespace android { 29namespace uirenderer { 30 31class RenderNode; 32class RenderProperties; 33 34class AnimationListener : public VirtualLightRefBase { 35public: 36 ANDROID_API virtual void onAnimationFinished(BaseRenderNodeAnimator*) = 0; 37protected: 38 ANDROID_API virtual ~AnimationListener() {} 39}; 40 41class BaseRenderNodeAnimator : public VirtualLightRefBase { 42 PREVENT_COPY_AND_ASSIGN(BaseRenderNodeAnimator); 43public: 44 ANDROID_API void setInterpolator(Interpolator* interpolator); 45 ANDROID_API void setDuration(nsecs_t durationInMs); 46 ANDROID_API nsecs_t duration() { return mDuration; } 47 ANDROID_API void setStartDelay(nsecs_t startDelayInMs); 48 ANDROID_API nsecs_t startDelay() { return mStartDelay; } 49 ANDROID_API void setListener(AnimationListener* listener) { 50 mListener = listener; 51 } 52 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 60 bool isFinished() { return mPlayState == FINISHED; } 61 float finalValue() { return mFinalValue; } 62 63protected: 64 BaseRenderNodeAnimator(float finalValue); 65 virtual ~BaseRenderNodeAnimator(); 66 67 void setStartValue(float value); 68 virtual float getValue(RenderNode* target) const = 0; 69 virtual void setValue(RenderNode* target, float value) = 0; 70 71private: 72 void callOnFinishedListener(TreeInfo& info); 73 74 enum PlayState { 75 NEEDS_START, 76 PENDING, 77 RUNNING, 78 FINISHED, 79 }; 80 81 float mFinalValue; 82 float mDeltaValue; 83 float mFromValue; 84 85 Interpolator* mInterpolator; 86 PlayState mPlayState; 87 nsecs_t mStartTime; 88 nsecs_t mDelayUntil; 89 nsecs_t mDuration; 90 nsecs_t mStartDelay; 91 92 sp<AnimationListener> mListener; 93}; 94 95class RenderPropertyAnimator : public BaseRenderNodeAnimator { 96public: 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 112 ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue); 113 114 ANDROID_API virtual void onAttached(RenderNode* target); 115 116protected: 117 virtual float getValue(RenderNode* target) const; 118 virtual void setValue(RenderNode* target, float value); 119 120private: 121 typedef void (RenderProperties::*SetFloatProperty)(float value); 122 typedef float (RenderProperties::*GetFloatProperty)() const; 123 124 struct PropertyAccessors; 125 const PropertyAccessors* mPropertyAccess; 126 127 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[]; 128}; 129 130class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator { 131public: 132 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property, 133 float finalValue); 134protected: 135 virtual float getValue(RenderNode* target) const; 136 virtual void setValue(RenderNode* target, float value); 137private: 138 sp<CanvasPropertyPrimitive> mProperty; 139}; 140 141class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator { 142public: 143 enum PaintField { 144 STROKE_WIDTH = 0, 145 ALPHA, 146 }; 147 148 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property, 149 PaintField field, float finalValue); 150protected: 151 virtual float getValue(RenderNode* target) const; 152 virtual void setValue(RenderNode* target, float value); 153private: 154 sp<CanvasPropertyPaint> mProperty; 155 PaintField mField; 156}; 157 158} /* namespace uirenderer */ 159} /* namespace android */ 160 161#endif /* ANIMATOR_H */ 162