1 /*
2  * Copyright (c) 2021 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 
17 #ifndef VSYNC_VSYNC_GENERATOR_H
18 #define VSYNC_VSYNC_GENERATOR_H
19 
20 #include <cstdint>
21 #include <refbase.h>
22 #include "graphic_common.h"
23 
24 #include <mutex>
25 #include <vector>
26 #include <thread>
27 #include <condition_variable>
28 #include "vsync_type.h"
29 #include "vsync_system_ability_listener.h"
30 
31 namespace OHOS {
32 namespace Rosen {
33 
34 class VSyncDistributor;
35 
36 class VSyncGenerator : public RefBase {
37 public:
38     class Callback : public RefBase {
39     public:
40         virtual void OnVSyncEvent(int64_t now, int64_t period,
41             uint32_t refreshRate, VSyncMode vsyncMode, uint32_t vsyncMaxRefreshRate) = 0;
42         virtual void OnPhaseOffsetChanged(int64_t phaseOffset) = 0;
43         /* std::pair<id, refresh rate> */
44         virtual void OnConnsRefreshRateChanged(const std::vector<std::pair<uint64_t, uint32_t>> &refreshRates) = 0;
45     };
46     struct ListenerRefreshRateData {
47         sptr<OHOS::Rosen::VSyncGenerator::Callback> cb = nullptr;
48         // id, refreshRate
49         std::vector<std::pair<uint64_t, uint32_t>> refreshRates;
50     };
51     struct ListenerPhaseOffsetData {
52         sptr<OHOS::Rosen::VSyncGenerator::Callback> cb = nullptr;
53         int32_t phaseByPulseNum = 0;
54     };
55     VSyncGenerator() = default;
56     virtual ~VSyncGenerator() noexcept = default;
57     virtual VsyncError UpdateMode(int64_t period, int64_t phase, int64_t referenceTime) = 0;
58     virtual VsyncError AddListener(int64_t phase, const sptr<Callback>& cb) = 0;
59     virtual VsyncError RemoveListener(const sptr<Callback>& cb) = 0;
60     virtual VsyncError ChangePhaseOffset(const sptr<Callback>& cb, int64_t offset) = 0;
61     virtual bool IsEnable() = 0;
62     virtual VsyncError ChangeGeneratorRefreshRateModel(const ListenerRefreshRateData &listenerRefreshRates,
63                                                        const ListenerPhaseOffsetData &listenerPhaseOffset,
64                                                        uint32_t generatorRefreshRate,
65                                                        int64_t &rsVsyncCount,
66                                                        int64_t expectNextVsyncTime = 0) = 0;
67     virtual int64_t GetVSyncPulse() = 0;
68     virtual VsyncError SetVSyncMode(VSyncMode vsyncMode) = 0;
69     virtual VSyncMode GetVSyncMode() = 0;
70     virtual VsyncError SetVSyncPhaseByPulseNum(int32_t phaseByPulseNum) = 0;
71     virtual uint32_t GetVSyncMaxRefreshRate() = 0;
72     virtual VsyncError SetVSyncMaxRefreshRate(uint32_t refreshRate) = 0;
73     virtual void Dump(std::string &result) = 0;
74     virtual bool GetFrameRateChaingStatus() = 0;
75     virtual VsyncError SetReferenceTimeOffset(int32_t phaseByPulseNum) = 0;
76     virtual VsyncError CheckAndUpdateReferenceTime(int64_t hardwareVsyncInterval, int64_t referenceTime) = 0;
77     virtual void SetPendingMode(int64_t period, int64_t timestamp) = 0;
78     virtual VsyncError StartRefresh() = 0;
79 
80     virtual void SetRSDistributor(sptr<VSyncDistributor> &rsVSyncDistributor) = 0;
81     virtual void SetFrameRateChangingStatus(bool frameRateChanging) = 0;
82     virtual void SetAppDistributor(sptr<VSyncDistributor> &appVSyncDistributor) = 0;
83 };
84 
85 sptr<VSyncGenerator> CreateVSyncGenerator();
86 void DestroyVSyncGenerator();
87 
88 namespace impl {
89 uint32_t CalculateRefreshRate(int64_t period);
90 
91 class VSyncGenerator : public OHOS::Rosen::VSyncGenerator {
92 public:
93     static sptr<OHOS::Rosen::VSyncGenerator> GetInstance() noexcept;
94     static void DeleteInstance() noexcept;
95 
96     // nocopyable
97     VSyncGenerator(const VSyncGenerator &) = delete;
98     VSyncGenerator &operator=(const VSyncGenerator &) = delete;
99     VsyncError UpdateMode(int64_t period, int64_t phase, int64_t referenceTime) override;
100     VsyncError AddListener(int64_t phase, const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb) override;
101     VsyncError RemoveListener(const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb) override;
102     VsyncError ChangePhaseOffset(const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb, int64_t offset) override;
103     bool IsEnable() override;
104     VsyncError ChangeGeneratorRefreshRateModel(const ListenerRefreshRateData &listenerRefreshRates,
105                                                const ListenerPhaseOffsetData &listenerPhaseOffset,
106                                                uint32_t generatorRefreshRate,
107                                                int64_t &rsVsyncCount,
108                                                int64_t expectNextVsyncTime = 0) override;
109     int64_t GetVSyncPulse() override;
110     VsyncError SetVSyncMode(VSyncMode vsyncMode) override;
111     VSyncMode GetVSyncMode() override;
112     VsyncError SetVSyncPhaseByPulseNum(int32_t phaseByPulseNum) override;
113     uint32_t GetVSyncMaxRefreshRate() override;
114     VsyncError SetVSyncMaxRefreshRate(uint32_t refreshRate) override;
115     void Dump(std::string &result) override;
116     bool GetFrameRateChaingStatus() override;
117     VsyncError SetReferenceTimeOffset(int32_t phaseByPulseNum) override;
118     VsyncError CheckAndUpdateReferenceTime(int64_t hardwareVsyncInterval, int64_t referenceTime) override;
119     void SetPendingMode(int64_t period, int64_t timestamp) override;
120     VsyncError StartRefresh() override;
121 
122     void SetRSDistributor(sptr<VSyncDistributor> &rsVSyncDistributor) override;
123     void SetFrameRateChangingStatus(bool frameRateChanging) override;
124     void SetAppDistributor(sptr<VSyncDistributor> &appVSyncDistributor) override;
125 
126 private:
127     friend class OHOS::Rosen::VSyncGenerator;
128 
129     struct Listener {
130         int64_t phase_;
131         sptr<OHOS::Rosen::VSyncGenerator::Callback> callback_;
132         int64_t lastTime_;
133         int64_t lastTimeRecord_;
134     };
135 
136     VSyncGenerator();
137     ~VSyncGenerator() override;
138 
139     int64_t ComputeNextVSyncTimeStamp(int64_t now, int64_t referenceTime);
140     std::vector<Listener> GetListenerTimeouted(int64_t now, int64_t occurTimestamp, int64_t referenceTime);
141     int64_t ComputeListenerNextVSyncTimeStamp(const Listener &listen, int64_t now, int64_t referenceTime);
142     void ThreadLoop();
143     void WaitForTimeout(int64_t occurTimestamp, int64_t nextTimeStamp, int64_t occurReferenceTime);
144     void UpdateWakeupDelay(int64_t occurTimestamp, int64_t nextTimeStamp);
145     bool ChangeListenerOffsetInternal();
146     bool ChangeListenerRefreshRatesInternal();
147     uint32_t JudgeRefreshRateLocked(int64_t period);
148     bool CheckTimingCorrect(int64_t now, int64_t referenceTime, int64_t nextVSyncTime);
149     bool UpdateChangeDataLocked(int64_t now, int64_t referenceTime, int64_t nextVSyncTime);
150     void UpdateVSyncModeLocked();
151     std::vector<Listener> GetListenerTimeoutedLTPO(int64_t now, int64_t referenceTime);
152     void ListenerVsyncEventCB(int64_t occurTimestamp, int64_t nextTimeStamp,
153         int64_t occurReferenceTime, bool isWakeup);
154     VsyncError UpdatePeriodLocked(int64_t period);
155     VsyncError UpdateReferenceTimeLocked(int64_t referenceTime);
156 #ifdef COMPOSER_SCHED_ENABLE
157     void SubScribeSystemAbility();
158 #endif
159     void PeriodCheckLocked(int64_t hardwareVsyncInterval);
160     void UpdateChangeRefreshRatesLocked(const ListenerRefreshRateData &listenerRefreshRates);
161     VsyncError SetExpectNextVsyncTimeInternal(int64_t expectNextVsyncTime);
162     void ClearAllSamplesInternal(bool clearAllSamplesFlag);
163     void CalculateReferenceTimeOffsetPulseNumLocked(int64_t referenceTime);
164 
165     sptr<VSyncSystemAbilityListener> saStatusChangeListener_ = nullptr;
166     int64_t period_ = 0;
167     int64_t phase_ = 0;
168     int64_t referenceTime_ = 0;
169     int64_t wakeupDelay_ = 0;
170 
171     std::vector<Listener> listeners_;
172 
173     std::mutex mutex_;
174     std::condition_variable con_;
175     std::mutex waitForTimeoutMtx_;
176     std::condition_variable waitForTimeoutCon_;
177     std::thread thread_;
178     bool vsyncThreadRunning_;
179     static std::once_flag createFlag_;
180     static sptr<OHOS::Rosen::VSyncGenerator> instance_;
181     int64_t pulse_ = 0; // by ns
182     uint32_t currRefreshRate_ = 0; // by Hz
183     int32_t referenceTimeOffsetPulseNum_ = 0;
184     int32_t defaultReferenceTimeOffsetPulseNum_ = 0;
185     ListenerRefreshRateData changingRefreshRates_ = {};
186     ListenerPhaseOffsetData changingPhaseOffset_ = {};
187     uint32_t changingGeneratorRefreshRate_ = 0;
188     bool needChangeRefreshRates_ = false;
189     bool needChangePhaseOffset_ = false;
190     bool needChangeGeneratorRefreshRate_ = false;
191     VSyncMode vsyncMode_ = VSYNC_MODE_LTPS; //default LTPS
192     VSyncMode pendingVsyncMode_ = VSYNC_MODE_INVALID;
193     std::vector<Listener> listenersRecord_;
194     bool refreshRateIsChanged_ = false;
195     bool frameRateChanging_ = false;
196     int64_t pendingPeriod_ = 0;
197     int64_t pendingReferenceTime_ = 0;
198     bool startRefresh_ = false;
199     int64_t phaseRecord_ = 0;
200     int64_t periodRecord_ = 0;
201     sptr<VSyncDistributor> rsVSyncDistributor_;
202     int32_t periodCheckCounter_ = 0;
203     int64_t lastPeriod_ = 0;
204     sptr<VSyncDistributor> appVSyncDistributor_;
205     int64_t expectNextVsyncTime_ = 0;
206     bool expectTimeFlag_ = false;
207     int64_t targetPeriod_ = 0;
208     bool clearAllSamplesFlag_ = false;
209     uint32_t vsyncMaxRefreshRate_ = 360; // default max TE
210 };
211 } // impl
212 } // namespace Rosen
213 } // namespace OHOS
214 
215 #endif
216