| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#ifndef ANIMATOR_H |
| 17 |
#define ANIMATOR_H |
| 18 |
|
| 19 |
#include "CanvasProperty.h" |
| 20 |
|
| 21 |
#include <cutils/compiler.h> |
| 22 |
#include <utils/RefBase.h> |
| 23 |
#include <utils/StrongPointer.h> |
| 24 |
#include <utils/Timers.h> |
| 25 |
#include <memory> |
| 26 |
|
| 27 |
#include "utils/Macros.h" |
| 28 |
|
| 29 |
#include <vector> |
| 30 |
|
| 31 |
namespace android { |
| 32 |
namespace uirenderer { |
| 33 |
|
| 34 |
class AnimationContext; |
| 35 |
class BaseRenderNodeAnimator; |
| 36 |
class Interpolator; |
| 37 |
class RenderNode; |
| 38 |
class RenderProperties; |
| 39 |
|
| 40 |
class AnimationListener : public VirtualLightRefBase { |
| 41 |
public: |
| 42 |
virtual void onAnimationFinished(BaseRenderNodeAnimator*) = 0; |
| 43 |
|
| 44 |
protected: |
| 45 |
virtual ~AnimationListener() {} |
| 46 |
}; |
| 47 |
|
| 48 |
enum class RepeatMode { |
| 49 |
|
| 50 |
Restart = 1, |
| 51 |
Reverse = 2 |
| 52 |
}; |
| 53 |
|
| 54 |
class BaseRenderNodeAnimator : public VirtualLightRefBase { |
| 55 |
PREVENT_COPY_AND_ASSIGN(BaseRenderNodeAnimator); |
| 56 |
|
| 57 |
public: |
| 58 |
void setStartValue(float value); |
| 59 |
void setInterpolator(Interpolator* interpolator); |
| 60 |
void setDuration(nsecs_t durationInMs); |
| 61 |
nsecs_t duration() { return mDuration; } |
| 62 |
void setStartDelay(nsecs_t startDelayInMs); |
| 63 |
nsecs_t startDelay() { return mStartDelay; } |
| 64 |
void setListener(AnimationListener* listener) { mListener = listener; } |
| 65 |
AnimationListener* listener() { return mListener.get(); } |
| 66 |
void setAllowRunningAsync(bool mayRunAsync) { mMayRunAsync = mayRunAsync; } |
| 67 |
bool mayRunAsync() { return mMayRunAsync; } |
| 68 |
void start(); |
| 69 |
virtual void reset(); |
| 70 |
void reverse(); |
| 71 |
|
| 72 |
void cancel(); |
| 73 |
|
| 74 |
|
| 75 |
virtual void end(); |
| 76 |
|
| 77 |
void attach(RenderNode* target); |
| 78 |
virtual void onAttached() {} |
| 79 |
void detach() { mTarget = nullptr; } |
| 80 |
void pushStaging(AnimationContext& context); |
| 81 |
bool animate(AnimationContext& context); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
nsecs_t getRemainingPlayTime(); |
| 86 |
|
| 87 |
bool isRunning() { |
| 88 |
return mPlayState == PlayState::Running || mPlayState == PlayState::Reversing; |
| 89 |
} |
| 90 |
bool isFinished() { return mPlayState == PlayState::Finished; } |
| 91 |
float finalValue() { return mFinalValue; } |
| 92 |
|
| 93 |
virtual uint32_t dirtyMask() = 0; |
| 94 |
|
| 95 |
void forceEndNow(AnimationContext& context); |
| 96 |
RenderNode* target() { return mTarget; } |
| 97 |
RenderNode* stagingTarget() { return mStagingTarget; } |
| 98 |
|
| 99 |
protected: |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
enum class PlayState { |
| 120 |
NotStarted, |
| 121 |
Running, |
| 122 |
Reversing, |
| 123 |
Finished, |
| 124 |
}; |
| 125 |
|
| 126 |
explicit BaseRenderNodeAnimator(float finalValue); |
| 127 |
virtual ~BaseRenderNodeAnimator(); |
| 128 |
|
| 129 |
virtual float getValue(RenderNode* target) const = 0; |
| 130 |
virtual void setValue(RenderNode* target, float value) = 0; |
| 131 |
|
| 132 |
void callOnFinishedListener(AnimationContext& context); |
| 133 |
|
| 134 |
virtual void onStagingPlayStateChanged() {} |
| 135 |
virtual void onPlayTimeChanged(nsecs_t playTime) {} |
| 136 |
virtual void onPushStaging() {} |
| 137 |
|
| 138 |
RenderNode* mTarget; |
| 139 |
RenderNode* mStagingTarget; |
| 140 |
|
| 141 |
float mFinalValue; |
| 142 |
float mDeltaValue; |
| 143 |
float mFromValue; |
| 144 |
|
| 145 |
std::unique_ptr<Interpolator> mInterpolator; |
| 146 |
PlayState mStagingPlayState; |
| 147 |
PlayState mPlayState; |
| 148 |
bool mHasStartValue; |
| 149 |
nsecs_t mStartTime; |
| 150 |
nsecs_t mDuration; |
| 151 |
nsecs_t mStartDelay; |
| 152 |
bool mMayRunAsync; |
| 153 |
|
| 154 |
|
| 155 |
nsecs_t mPlayTime; |
| 156 |
|
| 157 |
sp<AnimationListener> mListener; |
| 158 |
|
| 159 |
private: |
| 160 |
enum class Request { Start, Reverse, Reset, Cancel, End }; |
| 161 |
|
| 162 |
|
| 163 |
enum class Action { |
| 164 |
|
| 165 |
None, |
| 166 |
|
| 167 |
Reset, |
| 168 |
|
| 169 |
End |
| 170 |
}; |
| 171 |
|
| 172 |
inline void checkMutable(); |
| 173 |
virtual void transitionToRunning(AnimationContext& context); |
| 174 |
void doSetStartValue(float value); |
| 175 |
bool updatePlayTime(nsecs_t playTime); |
| 176 |
void resolveStagingRequest(Request request); |
| 177 |
|
| 178 |
std::vector<Request> mStagingRequests; |
| 179 |
Action mPendingActionUponFinish = Action::None; |
| 180 |
}; |
| 181 |
|
| 182 |
class RenderPropertyAnimator : public BaseRenderNodeAnimator { |
| 183 |
public: |
| 184 |
enum RenderProperty { |
| 185 |
TRANSLATION_X = 0, |
| 186 |
TRANSLATION_Y, |
| 187 |
TRANSLATION_Z, |
| 188 |
SCALE_X, |
| 189 |
SCALE_Y, |
| 190 |
ROTATION, |
| 191 |
ROTATION_X, |
| 192 |
ROTATION_Y, |
| 193 |
X, |
| 194 |
Y, |
| 195 |
Z, |
| 196 |
ALPHA, |
| 197 |
}; |
| 198 |
|
| 199 |
RenderPropertyAnimator(RenderProperty property, float finalValue); |
| 200 |
|
| 201 |
virtual uint32_t dirtyMask(); |
| 202 |
|
| 203 |
protected: |
| 204 |
virtual float getValue(RenderNode* target) const override; |
| 205 |
virtual void setValue(RenderNode* target, float value) override; |
| 206 |
virtual void onAttached() override; |
| 207 |
virtual void onStagingPlayStateChanged() override; |
| 208 |
virtual void onPushStaging() override; |
| 209 |
|
| 210 |
private: |
| 211 |
typedef bool (RenderProperties::*SetFloatProperty)(float value); |
| 212 |
typedef float (RenderProperties::*GetFloatProperty)() const; |
| 213 |
|
| 214 |
struct PropertyAccessors; |
| 215 |
const PropertyAccessors* mPropertyAccess; |
| 216 |
|
| 217 |
static const PropertyAccessors PROPERTY_ACCESSOR_LUT[]; |
| 218 |
bool mShouldSyncPropertyFields = false; |
| 219 |
bool mShouldUpdateStagingProperties = false; |
| 220 |
}; |
| 221 |
|
| 222 |
class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator { |
| 223 |
public: |
| 224 |
CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property, |
| 225 |
float finalValue); |
| 226 |
|
| 227 |
virtual uint32_t dirtyMask(); |
| 228 |
|
| 229 |
protected: |
| 230 |
virtual float getValue(RenderNode* target) const override; |
| 231 |
virtual void setValue(RenderNode* target, float value) override; |
| 232 |
|
| 233 |
private: |
| 234 |
sp<CanvasPropertyPrimitive> mProperty; |
| 235 |
}; |
| 236 |
|
| 237 |
class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator { |
| 238 |
public: |
| 239 |
enum PaintField { |
| 240 |
STROKE_WIDTH = 0, |
| 241 |
ALPHA, |
| 242 |
}; |
| 243 |
|
| 244 |
CanvasPropertyPaintAnimator(CanvasPropertyPaint* property, PaintField field, |
| 245 |
float finalValue); |
| 246 |
|
| 247 |
virtual uint32_t dirtyMask(); |
| 248 |
|
| 249 |
protected: |
| 250 |
virtual float getValue(RenderNode* target) const override; |
| 251 |
virtual void setValue(RenderNode* target, float value) override; |
| 252 |
|
| 253 |
private: |
| 254 |
sp<CanvasPropertyPaint> mProperty; |
| 255 |
PaintField mField; |
| 256 |
}; |
| 257 |
|
| 258 |
class RevealAnimator : public BaseRenderNodeAnimator { |
| 259 |
public: |
| 260 |
RevealAnimator(int centerX, int centerY, float startValue, float finalValue); |
| 261 |
|
| 262 |
virtual uint32_t dirtyMask(); |
| 263 |
|
| 264 |
protected: |
| 265 |
virtual float getValue(RenderNode* target) const override; |
| 266 |
virtual void setValue(RenderNode* target, float value) override; |
| 267 |
|
| 268 |
private: |
| 269 |
int mCenterX, mCenterY; |
| 270 |
}; |
| 271 |
|
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
#endif |