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