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