1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_ANIMATION_FRACTION_H
17 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_ANIMATION_FRACTION_H
18 
19 #include "atomic"
20 
21 #include "animation/rs_animation_timing_protocol.h"
22 #include "common/rs_macros.h"
23 
24 namespace OHOS {
25 namespace Rosen {
26 enum class ForwardDirection {
27     NORMAL = 0,
28     REVERSE,
29 };
30 
31 class RSB_EXPORT RSAnimationFraction : public RSAnimationTimingProtocol {
32 public:
33     RSAnimationFraction();
34     ~RSAnimationFraction() = default;
35 
36     static void Init();
37     static float GetAnimationScale();
38     static void SetAnimationScale(float animationScale);
39     static void OnAnimationScaleChangedCallback(const char *key, const char *value, void *context);
40 
41     // return <fraction, isInStartDelay, isFinished, isRepeatFinished> as tuple
42     std::tuple<float, bool, bool, bool> GetAnimationFraction(int64_t time);
43     void UpdateRemainTimeFraction(float fraction, int remainTime = 0);
44     float GetStartFraction() const;
45     float GetEndFraction() const;
46     void SetDirectionAfterStart(const ForwardDirection& direction);
47     void SetLastFrameTime(int64_t lastFrameTime);
48     int64_t GetLastFrameTime() const;
49     void ResetFraction();
50     int GetRemainingRepeatCount() const;
51     bool GetCurrentIsReverseCycle() const;
GetCurrentTimeFraction()52     float GetCurrentTimeFraction() const
53     {
54         return currentTimeFraction_;
55     }
56 
SetRepeatCallbackEnable(bool isEnable)57     void SetRepeatCallbackEnable(bool isEnable)
58     {
59         isRepeatCallbackEnable_ = isEnable;
60     }
61 
GetRepeatCallbackEnable()62     bool GetRepeatCallbackEnable() const
63     {
64         return isRepeatCallbackEnable_;
65     }
66 
67 private:
68     bool IsInRepeat() const;
69     bool IsFinished() const;
70     void UpdateReverseState(bool finish);
71     bool IsStartRunning(const int64_t deltaTime, const int64_t startDelayNs);
72 
73     static std::atomic<float> animationScale_;
74     static std::atomic<bool> isInitialized_;
75 
76     ForwardDirection direction_ { ForwardDirection::NORMAL };
77     int64_t playTime_ { 0 };
78     float currentTimeFraction_ { 0.0f };
79     int currentRepeatCount_ { 0 };
80     int64_t runningTime_ { 0 };
81     bool currentIsReverseCycle_ { false };
82     int64_t lastFrameTime_ { -1 };
83     bool isRepeatCallbackEnable_ {false};
84 };
85 } // namespace Rosen
86 } // namespace OHOS
87 
88 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_ANIMATION_FRACTION_H
89