Animator.h revision e2478d45ccbe5b6abb360ac9d44771b5f4a50bde
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 AnimationContext; 32class BaseRenderNodeAnimator; 33class RenderNode; 34class RenderProperties; 35 36class AnimationListener : public VirtualLightRefBase { 37public: 38 ANDROID_API virtual void onAnimationFinished(BaseRenderNodeAnimator*) = 0; 39protected: 40 ANDROID_API virtual ~AnimationListener() {} 41}; 42 43class BaseRenderNodeAnimator : public VirtualLightRefBase { 44 PREVENT_COPY_AND_ASSIGN(BaseRenderNodeAnimator); 45public: 46 ANDROID_API void setStartValue(float value); 47 ANDROID_API void setInterpolator(Interpolator* interpolator); 48 ANDROID_API void setDuration(nsecs_t durationInMs); 49 ANDROID_API nsecs_t duration() { return mDuration; } 50 ANDROID_API void setStartDelay(nsecs_t startDelayInMs); 51 ANDROID_API nsecs_t startDelay() { return mStartDelay; } 52 ANDROID_API void setListener(AnimationListener* listener) { 53 mListener = listener; 54 } 55 AnimationListener* listener() { return mListener.get(); } 56 ANDROID_API void start() { mStagingPlayState = RUNNING; onStagingPlayStateChanged(); } 57 ANDROID_API void end() { mStagingPlayState = FINISHED; onStagingPlayStateChanged(); } 58 59 void attach(RenderNode* target); 60 virtual void onAttached() {} 61 void detach() { mTarget = 0; } 62 void pushStaging(AnimationContext& context); 63 bool animate(AnimationContext& context); 64 65 bool isRunning() { return mPlayState == RUNNING; } 66 bool isFinished() { return mPlayState == FINISHED; } 67 float finalValue() { return mFinalValue; } 68 69 ANDROID_API virtual uint32_t dirtyMask() = 0; 70 71 void forceEndNow(AnimationContext& context); 72 73protected: 74 BaseRenderNodeAnimator(float finalValue); 75 virtual ~BaseRenderNodeAnimator(); 76 77 virtual float getValue(RenderNode* target) const = 0; 78 virtual void setValue(RenderNode* target, float value) = 0; 79 RenderNode* target() { return mTarget; } 80 81 void callOnFinishedListener(AnimationContext& context); 82 83 virtual void onStagingPlayStateChanged() {} 84 85 enum PlayState { 86 NOT_STARTED, 87 RUNNING, 88 FINISHED, 89 }; 90 91 RenderNode* mTarget; 92 93 float mFinalValue; 94 float mDeltaValue; 95 float mFromValue; 96 97 Interpolator* mInterpolator; 98 PlayState mStagingPlayState; 99 PlayState mPlayState; 100 bool mHasStartValue; 101 nsecs_t mStartTime; 102 nsecs_t mDuration; 103 nsecs_t mStartDelay; 104 105 sp<AnimationListener> mListener; 106 107private: 108 inline void checkMutable(); 109 virtual void transitionToRunning(AnimationContext& context); 110 void doSetStartValue(float value); 111}; 112 113class RenderPropertyAnimator : public BaseRenderNodeAnimator { 114public: 115 enum RenderProperty { 116 TRANSLATION_X = 0, 117 TRANSLATION_Y, 118 TRANSLATION_Z, 119 SCALE_X, 120 SCALE_Y, 121 ROTATION, 122 ROTATION_X, 123 ROTATION_Y, 124 X, 125 Y, 126 Z, 127 ALPHA, 128 }; 129 130 ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue); 131 132 ANDROID_API virtual uint32_t dirtyMask(); 133 134protected: 135 virtual float getValue(RenderNode* target) const; 136 virtual void setValue(RenderNode* target, float value); 137 virtual void onAttached(); 138 virtual void onStagingPlayStateChanged(); 139 140private: 141 typedef bool (RenderProperties::*SetFloatProperty)(float value); 142 typedef float (RenderProperties::*GetFloatProperty)() const; 143 144 struct PropertyAccessors; 145 const PropertyAccessors* mPropertyAccess; 146 147 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[]; 148}; 149 150class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator { 151public: 152 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property, 153 float finalValue); 154 155 ANDROID_API virtual uint32_t dirtyMask(); 156 157protected: 158 virtual float getValue(RenderNode* target) const; 159 virtual void setValue(RenderNode* target, float value); 160private: 161 sp<CanvasPropertyPrimitive> mProperty; 162}; 163 164class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator { 165public: 166 enum PaintField { 167 STROKE_WIDTH = 0, 168 ALPHA, 169 }; 170 171 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property, 172 PaintField field, float finalValue); 173 174 ANDROID_API virtual uint32_t dirtyMask(); 175 176protected: 177 virtual float getValue(RenderNode* target) const; 178 virtual void setValue(RenderNode* target, float value); 179private: 180 sp<CanvasPropertyPaint> mProperty; 181 PaintField mField; 182}; 183 184class RevealAnimator : public BaseRenderNodeAnimator { 185public: 186 ANDROID_API RevealAnimator(int centerX, int centerY, 187 float startValue, float finalValue); 188 189 ANDROID_API virtual uint32_t dirtyMask(); 190 191protected: 192 virtual float getValue(RenderNode* target) const; 193 virtual void setValue(RenderNode* target, float value); 194 195private: 196 int mCenterX, mCenterY; 197}; 198 199} /* namespace uirenderer */ 200} /* namespace android */ 201 202#endif /* ANIMATOR_H */ 203