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 
27 namespace android {
28 namespace uirenderer {
29 
30 class AnimationContext;
31 class BaseRenderNodeAnimator;
32 class CanvasPropertyPrimitive;
33 class CanvasPropertyPaint;
34 class Interpolator;
35 class RenderNode;
36 class RenderProperties;
37 
38 class AnimationListener : public VirtualLightRefBase {
39 public:
40     ANDROID_API virtual void onAnimationFinished(BaseRenderNodeAnimator*) = 0;
41 protected:
~AnimationListener()42     ANDROID_API virtual ~AnimationListener() {}
43 };
44 
45 class BaseRenderNodeAnimator : public VirtualLightRefBase {
46     PREVENT_COPY_AND_ASSIGN(BaseRenderNodeAnimator);
47 public:
48     ANDROID_API void setStartValue(float value);
49     ANDROID_API void setInterpolator(Interpolator* interpolator);
50     ANDROID_API void setDuration(nsecs_t durationInMs);
duration()51     ANDROID_API nsecs_t duration() { return mDuration; }
52     ANDROID_API void setStartDelay(nsecs_t startDelayInMs);
startDelay()53     ANDROID_API nsecs_t startDelay() { return mStartDelay; }
setListener(AnimationListener * listener)54     ANDROID_API void setListener(AnimationListener* listener) {
55         mListener = listener;
56     }
listener()57     AnimationListener* listener() { return mListener.get(); }
setAllowRunningAsync(bool mayRunAsync)58     ANDROID_API void setAllowRunningAsync(bool mayRunAsync) {
59         mMayRunAsync = mayRunAsync;
60     }
mayRunAsync()61     bool mayRunAsync() { return mMayRunAsync; }
start()62     ANDROID_API void start() { mStagingPlayState = RUNNING; onStagingPlayStateChanged(); }
end()63     ANDROID_API void end() { mStagingPlayState = FINISHED; onStagingPlayStateChanged(); }
64 
65     void attach(RenderNode* target);
onAttached()66     virtual void onAttached() {}
detach()67     void detach() { mTarget = nullptr; }
68     void pushStaging(AnimationContext& context);
69     bool animate(AnimationContext& context);
70 
isRunning()71     bool isRunning() { return mPlayState == RUNNING; }
isFinished()72     bool isFinished() { return mPlayState == FINISHED; }
finalValue()73     float finalValue() { return mFinalValue; }
74 
75     ANDROID_API virtual uint32_t dirtyMask() = 0;
76 
77     void forceEndNow(AnimationContext& context);
78 
79 protected:
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;
target()85     RenderNode* target() { return mTarget; }
86 
87     void callOnFinishedListener(AnimationContext& context);
88 
onStagingPlayStateChanged()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 
114 private:
115     inline void checkMutable();
116     virtual void transitionToRunning(AnimationContext& context);
117     void doSetStartValue(float value);
118 };
119 
120 class RenderPropertyAnimator : public BaseRenderNodeAnimator {
121 public:
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 
141 protected:
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 
147 private:
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 
157 class CanvasPropertyPrimitiveAnimator : public BaseRenderNodeAnimator {
158 public:
159     ANDROID_API CanvasPropertyPrimitiveAnimator(CanvasPropertyPrimitive* property,
160             float finalValue);
161 
162     ANDROID_API virtual uint32_t dirtyMask();
163 
164 protected:
165     virtual float getValue(RenderNode* target) const override;
166     virtual void setValue(RenderNode* target, float value) override;
167 private:
168     sp<CanvasPropertyPrimitive> mProperty;
169 };
170 
171 class CanvasPropertyPaintAnimator : public BaseRenderNodeAnimator {
172 public:
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 
183 protected:
184     virtual float getValue(RenderNode* target) const override;
185     virtual void setValue(RenderNode* target, float value) override;
186 private:
187     sp<CanvasPropertyPaint> mProperty;
188     PaintField mField;
189 };
190 
191 class RevealAnimator : public BaseRenderNodeAnimator {
192 public:
193     ANDROID_API RevealAnimator(int centerX, int centerY,
194             float startValue, float finalValue);
195 
196     ANDROID_API virtual uint32_t dirtyMask();
197 
198 protected:
199     virtual float getValue(RenderNode* target) const override;
200     virtual void setValue(RenderNode* target, float value) override;
201 
202 private:
203     int mCenterX, mCenterY;
204 };
205 
206 } /* namespace uirenderer */
207 } /* namespace android */
208 
209 #endif /* ANIMATOR_H */
210