xref: /aosp_15_r20/frameworks/base/libs/hwui/PropertyValuesAnimatorSet.h (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1*d57664e9SAndroid Build Coastguard Worker /*
2*d57664e9SAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*d57664e9SAndroid Build Coastguard Worker  *
4*d57664e9SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*d57664e9SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*d57664e9SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*d57664e9SAndroid Build Coastguard Worker  *
8*d57664e9SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*d57664e9SAndroid Build Coastguard Worker  *
10*d57664e9SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*d57664e9SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*d57664e9SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*d57664e9SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*d57664e9SAndroid Build Coastguard Worker  * limitations under the License.
15*d57664e9SAndroid Build Coastguard Worker  */
16*d57664e9SAndroid Build Coastguard Worker 
17*d57664e9SAndroid Build Coastguard Worker #pragma once
18*d57664e9SAndroid Build Coastguard Worker 
19*d57664e9SAndroid Build Coastguard Worker #include "Animator.h"
20*d57664e9SAndroid Build Coastguard Worker #include "Interpolator.h"
21*d57664e9SAndroid Build Coastguard Worker #include "PropertyValuesHolder.h"
22*d57664e9SAndroid Build Coastguard Worker 
23*d57664e9SAndroid Build Coastguard Worker namespace android {
24*d57664e9SAndroid Build Coastguard Worker namespace uirenderer {
25*d57664e9SAndroid Build Coastguard Worker 
26*d57664e9SAndroid Build Coastguard Worker class PropertyAnimator {
27*d57664e9SAndroid Build Coastguard Worker public:
28*d57664e9SAndroid Build Coastguard Worker     PropertyAnimator(PropertyValuesHolder* holder, Interpolator* interpolator, nsecs_t startDelay,
29*d57664e9SAndroid Build Coastguard Worker                      nsecs_t duration, int repeatCount, RepeatMode repeatMode);
30*d57664e9SAndroid Build Coastguard Worker     void setCurrentPlayTime(nsecs_t playTime);
getTotalDuration()31*d57664e9SAndroid Build Coastguard Worker     nsecs_t getTotalDuration() { return mTotalDuration; }
32*d57664e9SAndroid Build Coastguard Worker     // fraction range: [0, 1], iteration range [0, repeatCount]
33*d57664e9SAndroid Build Coastguard Worker     void setFraction(float fraction, long iteration);
34*d57664e9SAndroid Build Coastguard Worker 
35*d57664e9SAndroid Build Coastguard Worker private:
36*d57664e9SAndroid Build Coastguard Worker     std::unique_ptr<PropertyValuesHolder> mPropertyValuesHolder;
37*d57664e9SAndroid Build Coastguard Worker     std::unique_ptr<Interpolator> mInterpolator;
38*d57664e9SAndroid Build Coastguard Worker     nsecs_t mStartDelay;
39*d57664e9SAndroid Build Coastguard Worker     nsecs_t mDuration;
40*d57664e9SAndroid Build Coastguard Worker     uint32_t mRepeatCount;
41*d57664e9SAndroid Build Coastguard Worker     nsecs_t mTotalDuration;
42*d57664e9SAndroid Build Coastguard Worker     RepeatMode mRepeatMode;
43*d57664e9SAndroid Build Coastguard Worker     double mLatestFraction = 0;
44*d57664e9SAndroid Build Coastguard Worker };
45*d57664e9SAndroid Build Coastguard Worker 
46*d57664e9SAndroid Build Coastguard Worker // TODO: This class should really be named VectorDrawableAnimator
47*d57664e9SAndroid Build Coastguard Worker class PropertyValuesAnimatorSet : public BaseRenderNodeAnimator {
48*d57664e9SAndroid Build Coastguard Worker public:
49*d57664e9SAndroid Build Coastguard Worker     friend class PropertyAnimatorSetListener;
50*d57664e9SAndroid Build Coastguard Worker     PropertyValuesAnimatorSet();
51*d57664e9SAndroid Build Coastguard Worker 
52*d57664e9SAndroid Build Coastguard Worker     void start(AnimationListener* listener);
53*d57664e9SAndroid Build Coastguard Worker     void reverse(AnimationListener* listener);
54*d57664e9SAndroid Build Coastguard Worker     virtual void reset() override;
55*d57664e9SAndroid Build Coastguard Worker     virtual void end() override;
56*d57664e9SAndroid Build Coastguard Worker 
57*d57664e9SAndroid Build Coastguard Worker     void addPropertyAnimator(PropertyValuesHolder* propertyValuesHolder,
58*d57664e9SAndroid Build Coastguard Worker                              Interpolator* interpolators, int64_t startDelays, nsecs_t durations,
59*d57664e9SAndroid Build Coastguard Worker                              int repeatCount, RepeatMode repeatMode);
60*d57664e9SAndroid Build Coastguard Worker     virtual uint32_t dirtyMask();
isInfinite()61*d57664e9SAndroid Build Coastguard Worker     bool isInfinite() { return mIsInfinite; }
setVectorDrawable(VectorDrawableRoot * vd)62*d57664e9SAndroid Build Coastguard Worker     void setVectorDrawable(VectorDrawableRoot* vd) { mVectorDrawable = vd; }
getVectorDrawable()63*d57664e9SAndroid Build Coastguard Worker     VectorDrawableRoot* getVectorDrawable() const { return mVectorDrawable.get(); }
getOneShotListener()64*d57664e9SAndroid Build Coastguard Worker     AnimationListener* getOneShotListener() { return mOneShotListener.get(); }
clearOneShotListener()65*d57664e9SAndroid Build Coastguard Worker     void clearOneShotListener() { mOneShotListener = nullptr; }
getRequestId()66*d57664e9SAndroid Build Coastguard Worker     uint32_t getRequestId() const { return mRequestId; }
67*d57664e9SAndroid Build Coastguard Worker 
68*d57664e9SAndroid Build Coastguard Worker protected:
69*d57664e9SAndroid Build Coastguard Worker     virtual float getValue(RenderNode* target) const override;
70*d57664e9SAndroid Build Coastguard Worker     virtual void setValue(RenderNode* target, float value) override;
71*d57664e9SAndroid Build Coastguard Worker     virtual void onPlayTimeChanged(nsecs_t playTime) override;
72*d57664e9SAndroid Build Coastguard Worker 
73*d57664e9SAndroid Build Coastguard Worker private:
74*d57664e9SAndroid Build Coastguard Worker     void init();
75*d57664e9SAndroid Build Coastguard Worker     void onFinished(BaseRenderNodeAnimator* animator);
76*d57664e9SAndroid Build Coastguard Worker     // Listener set from outside
77*d57664e9SAndroid Build Coastguard Worker     sp<AnimationListener> mOneShotListener;
78*d57664e9SAndroid Build Coastguard Worker     std::vector<std::unique_ptr<PropertyAnimator> > mAnimators;
79*d57664e9SAndroid Build Coastguard Worker     float mLastFraction = 0.0f;
80*d57664e9SAndroid Build Coastguard Worker     bool mInitialized = false;
81*d57664e9SAndroid Build Coastguard Worker     sp<VectorDrawableRoot> mVectorDrawable;
82*d57664e9SAndroid Build Coastguard Worker     bool mIsInfinite = false;
83*d57664e9SAndroid Build Coastguard Worker     // This request id gets incremented (on UI thread only) when a new request to modfiy the
84*d57664e9SAndroid Build Coastguard Worker     // lifecycle of an animation happens, namely when start/end/reset/reverse is called.
85*d57664e9SAndroid Build Coastguard Worker     uint32_t mRequestId = 0;
86*d57664e9SAndroid Build Coastguard Worker };
87*d57664e9SAndroid Build Coastguard Worker 
88*d57664e9SAndroid Build Coastguard Worker class PropertyAnimatorSetListener : public AnimationListener {
89*d57664e9SAndroid Build Coastguard Worker public:
PropertyAnimatorSetListener(PropertyValuesAnimatorSet * set)90*d57664e9SAndroid Build Coastguard Worker     explicit PropertyAnimatorSetListener(PropertyValuesAnimatorSet* set) : mSet(set) {}
91*d57664e9SAndroid Build Coastguard Worker     virtual void onAnimationFinished(BaseRenderNodeAnimator* animator) override;
92*d57664e9SAndroid Build Coastguard Worker 
93*d57664e9SAndroid Build Coastguard Worker private:
94*d57664e9SAndroid Build Coastguard Worker     PropertyValuesAnimatorSet* mSet;
95*d57664e9SAndroid Build Coastguard Worker };
96*d57664e9SAndroid Build Coastguard Worker 
97*d57664e9SAndroid Build Coastguard Worker }  // namespace uirenderer
98*d57664e9SAndroid Build Coastguard Worker }  // namespace android
99