Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include "Animator.h" |
| 20 | #include "PropertyValuesHolder.h" |
| 21 | #include "Interpolator.h" |
| 22 | |
| 23 | namespace android { |
| 24 | namespace uirenderer { |
| 25 | |
| 26 | class PropertyAnimator { |
| 27 | public: |
| 28 | PropertyAnimator(PropertyValuesHolder* holder, Interpolator* interpolator, nsecs_t startDelay, |
Doris Liu | f7167e8 | 2016-08-03 17:54:28 -0700 | [diff] [blame] | 29 | nsecs_t duration, int repeatCount, RepeatMode repeatMode); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 30 | void setCurrentPlayTime(nsecs_t playTime); |
| 31 | nsecs_t getTotalDuration() { |
| 32 | return mTotalDuration; |
| 33 | } |
Doris Liu | f7167e8 | 2016-08-03 17:54:28 -0700 | [diff] [blame] | 34 | // fraction range: [0, 1], iteration range [0, repeatCount] |
| 35 | void setFraction(float fraction, long iteration); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 36 | |
| 37 | private: |
| 38 | std::unique_ptr<PropertyValuesHolder> mPropertyValuesHolder; |
| 39 | std::unique_ptr<Interpolator> mInterpolator; |
| 40 | nsecs_t mStartDelay; |
| 41 | nsecs_t mDuration; |
| 42 | uint32_t mRepeatCount; |
| 43 | nsecs_t mTotalDuration; |
Doris Liu | f7167e8 | 2016-08-03 17:54:28 -0700 | [diff] [blame] | 44 | RepeatMode mRepeatMode; |
| 45 | double mLatestFraction = 0; |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
Doris Liu | 718cd3e | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 48 | // TODO: This class should really be named VectorDrawableAnimator |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 49 | class ANDROID_API PropertyValuesAnimatorSet : public BaseRenderNodeAnimator { |
| 50 | public: |
| 51 | friend class PropertyAnimatorSetListener; |
| 52 | PropertyValuesAnimatorSet(); |
| 53 | |
| 54 | void start(AnimationListener* listener); |
| 55 | void reverse(AnimationListener* listener); |
Doris Liu | 718cd3e | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 56 | virtual void reset() override; |
| 57 | virtual void end() override; |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 58 | |
| 59 | void addPropertyAnimator(PropertyValuesHolder* propertyValuesHolder, |
| 60 | Interpolator* interpolators, int64_t startDelays, |
Doris Liu | f7167e8 | 2016-08-03 17:54:28 -0700 | [diff] [blame] | 61 | nsecs_t durations, int repeatCount, RepeatMode repeatMode); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 62 | virtual uint32_t dirtyMask(); |
Doris Liu | 718cd3e | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 63 | bool isInfinite() { return mIsInfinite; } |
| 64 | void setVectorDrawable(VectorDrawableRoot* vd) { mVectorDrawable = vd; } |
Doris Liu | 98980cd | 2016-07-12 18:48:52 -0700 | [diff] [blame] | 65 | VectorDrawableRoot* getVectorDrawable() const { return mVectorDrawable.get(); } |
Doris Liu | 718cd3e | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 66 | AnimationListener* getOneShotListener() { return mOneShotListener.get(); } |
| 67 | void clearOneShotListener() { mOneShotListener = nullptr; } |
| 68 | uint32_t getRequestId() const { return mRequestId; } |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 69 | |
| 70 | protected: |
| 71 | virtual float getValue(RenderNode* target) const override; |
| 72 | virtual void setValue(RenderNode* target, float value) override; |
| 73 | virtual void onPlayTimeChanged(nsecs_t playTime) override; |
| 74 | |
| 75 | private: |
| 76 | void init(); |
| 77 | void onFinished(BaseRenderNodeAnimator* animator); |
| 78 | // Listener set from outside |
| 79 | sp<AnimationListener> mOneShotListener; |
| 80 | std::vector< std::unique_ptr<PropertyAnimator> > mAnimators; |
| 81 | float mLastFraction = 0.0f; |
| 82 | bool mInitialized = false; |
Doris Liu | 98980cd | 2016-07-12 18:48:52 -0700 | [diff] [blame] | 83 | sp<VectorDrawableRoot> mVectorDrawable; |
Doris Liu | 718cd3e | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 84 | bool mIsInfinite = false; |
| 85 | // This request id gets incremented (on UI thread only) when a new request to modfiy the |
| 86 | // lifecycle of an animation happens, namely when start/end/reset/reverse is called. |
| 87 | uint32_t mRequestId = 0; |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | class PropertyAnimatorSetListener : public AnimationListener { |
| 91 | public: |
Chih-Hung Hsieh | a619ec7 | 2016-08-29 14:52:43 -0700 | [diff] [blame] | 92 | explicit PropertyAnimatorSetListener(PropertyValuesAnimatorSet* set) : mSet(set) {} |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 93 | virtual void onAnimationFinished(BaseRenderNodeAnimator* animator) override; |
| 94 | |
| 95 | private: |
| 96 | PropertyValuesAnimatorSet* mSet; |
| 97 | }; |
| 98 | |
| 99 | } // namespace uirenderer |
| 100 | } // namespace android |