Animator.h revision 68bfe0a37a0dcef52abd81688d8520c5d16e1a85
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 setStartValue(float value); 45 ANDROID_API void setInterpolator(Interpolator* interpolator); 46 ANDROID_API void setDuration(nsecs_t durationInMs); 47 ANDROID_API nsecs_t duration() { return mDuration; } 48 ANDROID_API void setStartDelay(nsecs_t startDelayInMs); 49 ANDROID_API nsecs_t startDelay() { return mStartDelay; } 50 ANDROID_API void setListener(AnimationListener* listener) { 51 mListener = listener; 52 } 53 ANDROID_API void start() { mStagingPlayState = RUNNING; } 54 ANDROID_API void cancel() { mStagingPlayState = FINISHED; } 55 56 virtual void onAttached(RenderNode* target) {} 57 virtual void pushStaging(RenderNode* target, TreeInfo& info); 58 bool animate(RenderNode* target, TreeInfo& info); 59 60 bool isFinished() { return mPlayState == FINISHED; } 61 float finalValue() { return mFinalValue; } 62 63 ANDROID_API virtual uint32_t dirtyMask() { return 0; } 64 65protected: 66 BaseRenderNodeAnimator(float finalValue); 67 virtual ~BaseRenderNodeAnimator(); 68 69 virtual float getValue(RenderNode* target) const = 0; 70 virtual void setValue(RenderNode* target, float value) = 0; 71 72 void callOnFinishedListener(TreeInfo& info); 73 74 enum PlayState { 75 NOT_STARTED, 76 RUNNING, 77 FINISHED, 78 }; 79 80 float mFinalValue; 81 float mDeltaValue; 82 float mFromValue; 83 84 Interpolator* mInterpolator; 85 PlayState mStagingPlayState; 86 PlayState mPlayState; 87 bool mHasStartValue; 88 nsecs_t mStartTime; 89 nsecs_t mDuration; 90 nsecs_t mStartDelay; 91 92 sp<AnimationListener> mListener; 93 94private: 95 void doSetStartValue(float value); 96 inline void checkMutable(); 97 void transitionToRunning(TreeInfo& info); 98}; 99 100class RenderPropertyAnimator : public BaseRenderNodeAnimator { 101public: 102 enum RenderProperty { 103 TRANSLATION_X = 0, 104 TRANSLATION_Y, 105 TRANSLATION_Z, 106 SCALE_X, 107 SCALE_Y, 108 ROTATION, 109 ROTATION_X, 110 ROTATION_Y, 111 X, 112 Y, 113 Z, 114 ALPHA, 115 }; 116 117 ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue); 118 119 virtual void onAttached(RenderNode* target); 120 121 ANDROID_API virtual uint32_t dirtyMask(); 122 123protected: 124 virtual float getValue(RenderNode* target) const; 125 virtual void setValue(RenderNode* target, float value); 126 127private: 128 typedef bool (RenderProperties::*SetFloatProperty)(float value); 129 typedef float (RenderProperties::*GetFloatProperty)() const; 130 131 struct PropertyAccessors; 132 const PropertyAccessors* mPropertyAccess; 133 134 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[]; 135}; 136 137class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator { 138public: 139 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property, 140 float finalValue); 141protected: 142 virtual float getValue(RenderNode* target) const; 143 virtual void setValue(RenderNode* target, float value); 144private: 145 sp<CanvasPropertyPrimitive> mProperty; 146}; 147 148class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator { 149public: 150 enum PaintField { 151 STROKE_WIDTH = 0, 152 ALPHA, 153 }; 154 155 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property, 156 PaintField field, float finalValue); 157protected: 158 virtual float getValue(RenderNode* target) const; 159 virtual void setValue(RenderNode* target, float value); 160private: 161 sp<CanvasPropertyPaint> mProperty; 162 PaintField mField; 163}; 164 165} /* namespace uirenderer */ 166} /* namespace android */ 167 168#endif /* ANIMATOR_H */ 169