Animator.h revision c6b3264e16f1d2b72e7f9508559981ce9970157c
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 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 61 bool isFinished() { return mPlayState == FINISHED; } 62 float finalValue() { return mFinalValue; } 63 64protected: 65 BaseRenderNodeAnimator(float finalValue); 66 virtual ~BaseRenderNodeAnimator(); 67 68 virtual float getValue(RenderNode* target) const = 0; 69 virtual void setValue(RenderNode* target, float value) = 0; 70 71 void callOnFinishedListener(TreeInfo& info); 72 73 enum PlayState { 74 NEEDS_START, 75 PENDING, 76 RUNNING, 77 FINISHED, 78 }; 79 80 float mFinalValue; 81 float mDeltaValue; 82 float mFromValue; 83 84 Interpolator* mInterpolator; 85 PlayState mPlayState; 86 nsecs_t mStartTime; 87 nsecs_t mDelayUntil; 88 nsecs_t mDuration; 89 nsecs_t mStartDelay; 90 91 sp<AnimationListener> mListener; 92}; 93 94class RenderPropertyAnimator : public BaseRenderNodeAnimator { 95public: 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 111 ANDROID_API RenderPropertyAnimator(RenderProperty property, float finalValue); 112 113 ANDROID_API virtual void onAttached(RenderNode* target); 114 115protected: 116 virtual float getValue(RenderNode* target) const; 117 virtual void setValue(RenderNode* target, float value); 118 119private: 120 typedef void (RenderProperties::*SetFloatProperty)(float value); 121 typedef float (RenderProperties::*GetFloatProperty)() const; 122 123 struct PropertyAccessors; 124 const PropertyAccessors* mPropertyAccess; 125 126 static const PropertyAccessors PROPERTY_ACCESSOR_LUT[]; 127}; 128 129class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator { 130public: 131 ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property, 132 float finalValue); 133protected: 134 virtual float getValue(RenderNode* target) const; 135 virtual void setValue(RenderNode* target, float value); 136private: 137 sp<CanvasPropertyPrimitive> mProperty; 138}; 139 140class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator { 141public: 142 enum PaintField { 143 STROKE_WIDTH = 0, 144 ALPHA, 145 }; 146 147 ANDROID_API CanvasPropertyPaintAnimator(CanvasPropertyPaint* property, 148 PaintField field, float finalValue); 149protected: 150 virtual float getValue(RenderNode* target) const; 151 virtual void setValue(RenderNode* target, float value); 152private: 153 sp<CanvasPropertyPaint> mProperty; 154 PaintField mField; 155}; 156 157} /* namespace uirenderer */ 158} /* namespace android */ 159 160#endif /* ANIMATOR_H */ 161