• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         virtual int64_t GetPhaseOffset() = 0;
46     };
47     struct ListenerRefreshRateData {
48         sptr<OHOS::Rosen::VSyncGenerator::Callback> cb = nullptr;
49         // id, refreshRate
50         std::vector<std::pair<uint64_t, uint32_t>> refreshRates;
51     };
52     struct ListenerPhaseOffsetData {
53         sptr<OHOS::Rosen::VSyncGenerator::Callback> cb = nullptr;
54         int32_t phaseByPulseNum = 0;
55     };
56     VSyncGenerator() = default;
57     virtual ~VSyncGenerator() noexcept = default;
58     virtual VsyncError UpdateMode(int64_t period, int64_t phase, int64_t referenceTime) = 0;
59     virtual VsyncError AddListener(
60         const sptr<Callback>& cb, bool isRS = false, bool isUrgent = false) = 0;
61     virtual VsyncError RemoveListener(const sptr<Callback>& cb) = 0;
62     virtual VsyncError ChangePhaseOffset(const sptr<Callback>& cb, int64_t offset) = 0;
63     virtual bool IsEnable() = 0;
64     virtual VsyncError ChangeGeneratorRefreshRateModel(const ListenerRefreshRateData &listenerRefreshRates,
65                                                        const ListenerPhaseOffsetData &listenerPhaseOffset,
66                                                        uint32_t generatorRefreshRate,
67                                                        int64_t &rsVsyncCount,
68                                                        int64_t expectNextVsyncTime = 0) = 0;
69     virtual int64_t GetVSyncPulse() = 0;
70     virtual VsyncError SetVSyncMode(VSyncMode vsyncMode) = 0;
71     virtual VSyncMode GetVSyncMode() = 0;
72     virtual VsyncError SetVSyncPhaseByPulseNum(int32_t phaseByPulseNum) = 0;
73     virtual uint32_t GetVsyncRefreshRate() = 0;
74     virtual uint32_t GetVSyncMaxRefreshRate() = 0;
75     virtual VsyncError SetVSyncMaxRefreshRate(uint32_t refreshRate) = 0;
76     virtual void Dump(std::string &result) = 0;
77     virtual bool GetFrameRateChaingStatus() = 0;
78     virtual VsyncError SetReferenceTimeOffset(int32_t phaseByPulseNum) = 0;
79     virtual VsyncError CheckAndUpdateReferenceTime(int64_t hardwareVsyncInterval, int64_t referenceTime) = 0;
80     virtual void SetPendingMode(int64_t period, int64_t timestamp) = 0;
81     virtual VsyncError StartRefresh() = 0;
82 
83     virtual void SetRSDistributor(sptr<VSyncDistributor> &rsVSyncDistributor) = 0;
84     virtual void SetFrameRateChangingStatus(bool frameRateChanging) = 0;
85     virtual void SetAppDistributor(sptr<VSyncDistributor> &appVSyncDistributor) = 0;
86     virtual int64_t GetVSyncOffset() = 0;
87     // Start of DVSync
88     virtual int64_t SetCurrentRefreshRate(uint32_t currRefreshRate, uint32_t lastRefreshRate) = 0;
89     virtual bool DVSyncRateChanged(uint32_t currRefreshRate, bool &frameRateChanged, bool needChangeDssRefreshRate) = 0;
90     virtual VsyncError RemoveDVSyncListener(const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb) = 0;
91     virtual VsyncError AddDVSyncListener(int64_t phase, const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb) = 0;
92     virtual bool IsUiDvsyncOn() = 0;
93     // End of DVSync
94     virtual void PrintGeneratorStatus() = 0;
95     virtual bool CheckSampleIsAdaptive(int64_t hardwareVsyncInterval) = 0;
96     virtual bool NeedPreexecuteAndUpdateTs(
97         int64_t& timestamp, int64_t& period, int64_t& offset, int64_t lastVsyncTime) = 0;
98 };
99 
100 sptr<VSyncGenerator> CreateVSyncGenerator();
101 void DestroyVSyncGenerator();
102 
103 namespace impl {
104 uint32_t CalculateRefreshRate(int64_t period);
105 
106 class VSyncGenerator : public OHOS::Rosen::VSyncGenerator {
107 public:
108     static sptr<OHOS::Rosen::VSyncGenerator> GetInstance() noexcept;
109     static void DeleteInstance() noexcept;
110 
111     // nocopyable
112     VSyncGenerator(const VSyncGenerator &) = delete;
113     VSyncGenerator &operator=(const VSyncGenerator &) = delete;
114     VsyncError UpdateMode(int64_t period, int64_t phase, int64_t referenceTime) override;
115     VsyncError AddListener(
116         const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb, bool isRS, bool isUrgent) override;
117     VsyncError RemoveListener(const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb) override;
118     VsyncError ChangePhaseOffset(const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb, int64_t offset) override;
119     bool IsEnable() override;
120     VsyncError ChangeGeneratorRefreshRateModel(const ListenerRefreshRateData &listenerRefreshRates,
121                                                const ListenerPhaseOffsetData &listenerPhaseOffset,
122                                                uint32_t generatorRefreshRate,
123                                                int64_t &rsVsyncCount,
124                                                int64_t expectNextVsyncTime = 0) override;
125     int64_t GetVSyncPulse() override;
126     VsyncError SetVSyncMode(VSyncMode vsyncMode) override;
127     VSyncMode GetVSyncMode() override;
128     VsyncError SetVSyncPhaseByPulseNum(int32_t phaseByPulseNum) override;
129     uint32_t GetVsyncRefreshRate() override;
130     uint32_t GetVSyncMaxRefreshRate() override;
131     VsyncError SetVSyncMaxRefreshRate(uint32_t refreshRate) override;
132     void Dump(std::string &result) override;
133     bool GetFrameRateChaingStatus() override;
134     VsyncError SetReferenceTimeOffset(int32_t phaseByPulseNum) override;
135     VsyncError CheckAndUpdateReferenceTime(int64_t hardwareVsyncInterval, int64_t referenceTime) override;
136     void SetPendingMode(int64_t period, int64_t timestamp) override;
137     VsyncError StartRefresh() override;
138 
139     void SetRSDistributor(sptr<VSyncDistributor> &rsVSyncDistributor) override;
140     void SetFrameRateChangingStatus(bool frameRateChanging) override;
141     void SetAppDistributor(sptr<VSyncDistributor> &appVSyncDistributor) override;
142     int64_t GetVSyncOffset() override;
143 
144     // Start of DVSync
145     int64_t SetCurrentRefreshRate(uint32_t currRefreshRate, uint32_t lastRefreshRate) override;
146     bool DVSyncRateChanged(uint32_t currRefreshRate, bool &frameRateChanged, bool needChangeDssRefreshRate) override;
147     VsyncError RemoveDVSyncListener(const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb) override;
148     VsyncError AddDVSyncListener(int64_t phase, const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb) override;
149     bool IsUiDvsyncOn() override;
150     // End of DVSync
151     void PrintGeneratorStatus() override;
152     bool CheckSampleIsAdaptive(int64_t hardwareVsyncInterval) override;
153     bool NeedPreexecuteAndUpdateTs(
154         int64_t& timestamp, int64_t& period, int64_t& offset, int64_t lastVsyncTime) override;
155 private:
156     friend class OHOS::Rosen::VSyncGenerator;
157 
158     struct Listener {
159         int64_t phase_;
160         sptr<OHOS::Rosen::VSyncGenerator::Callback> callback_;
161         int64_t lastTime_;
162         bool isRS_ = false;
163     };
164 
165     VSyncGenerator();
166     ~VSyncGenerator() override;
167 
168     int64_t ComputeNextVSyncTimeStamp(int64_t now, int64_t referenceTime);
169     std::vector<Listener> GetListenerTimeouted(int64_t now, int64_t occurTimestamp, int64_t referenceTime);
170     int64_t ComputeListenerNextVSyncTimeStamp(const Listener &listen, int64_t now, int64_t referenceTime);
171     void ThreadLoop();
172     void WaitForTimeout(int64_t occurTimestamp, int64_t nextTimeStamp, int64_t occurReferenceTime);
173     void UpdateWakeupDelay(int64_t occurTimestamp, int64_t nextTimeStamp);
174     bool ChangeListenerOffsetInternal();
175     bool ChangeListenerRefreshRatesInternal();
176     uint32_t JudgeRefreshRateLocked(int64_t period);
177     bool CheckTimingCorrect(int64_t now, int64_t referenceTime, int64_t nextVSyncTime);
178     bool UpdateChangeDataLocked(int64_t now, int64_t referenceTime, int64_t nextVSyncTime);
179     void UpdateVSyncModeLocked();
180     std::vector<Listener> GetListenerTimeoutedLTPO(int64_t now, int64_t referenceTime);
181     void ListenerVsyncEventCB(int64_t occurTimestamp, int64_t nextTimeStamp,
182         int64_t occurReferenceTime, bool isWakeup);
183     VsyncError UpdatePeriodLocked(int64_t period);
184     VsyncError UpdateReferenceTimeLocked(int64_t referenceTime);
185 #ifdef COMPOSER_SCHED_ENABLE
186     void SubScribeSystemAbility();
187 #endif
188     void PeriodCheckLocked(int64_t hardwareVsyncInterval);
189     void UpdateChangeRefreshRatesLocked(const ListenerRefreshRateData &listenerRefreshRates);
190     VsyncError SetExpectNextVsyncTimeInternal(int64_t expectNextVsyncTime);
191     void ClearAllSamplesInternal(bool clearAllSamplesFlag);
192     void CalculateReferenceTimeOffsetPulseNumLocked(int64_t referenceTime);
193     void WaitForTimeoutConNotifyLocked();
194     void WaitForTimeoutConNotifyLockedForListener();
195 
196     sptr<VSyncSystemAbilityListener> saStatusChangeListener_ = nullptr;
197     int64_t period_ = 0;
198     int64_t phase_ = 0;
199     int64_t referenceTime_ = 0;
200     int64_t wakeupDelay_ = 0;
201 
202     std::vector<Listener> listeners_;
203 
204     std::mutex mutex_;
205     std::condition_variable con_;
206     std::mutex waitForTimeoutMtx_;
207     std::condition_variable waitForTimeoutCon_;
208     std::thread thread_;
209     bool vsyncThreadRunning_;
210     static std::once_flag createFlag_;
211     static sptr<OHOS::Rosen::VSyncGenerator> instance_;
212     int64_t pulse_ = 0; // by ns
213     uint32_t currRefreshRate_ = 0; // by Hz
214     int32_t referenceTimeOffsetPulseNum_ = 0;
215     int32_t defaultReferenceTimeOffsetPulseNum_ = 0;
216     ListenerRefreshRateData changingRefreshRates_ = {};
217     ListenerPhaseOffsetData changingPhaseOffset_ = {};
218     uint32_t changingGeneratorRefreshRate_ = 0;
219     bool needChangeRefreshRates_ = false;
220     bool needChangePhaseOffset_ = false;
221     bool needChangeGeneratorRefreshRate_ = false;
222     VSyncMode vsyncMode_ = VSYNC_MODE_LTPS; //default LTPS
223     VSyncMode pendingVsyncMode_ = VSYNC_MODE_INVALID;
224     std::vector<Listener> listenersRecord_;
225     bool refreshRateIsChanged_ = false;
226     bool frameRateChanging_ = false;
227     int64_t pendingPeriod_ = 0;
228     int64_t pendingReferenceTime_ = 0;
229     bool startRefresh_ = false;
230     int64_t phaseRecord_ = 0;
231     int64_t periodRecord_ = 0;
232     sptr<VSyncDistributor> rsVSyncDistributor_;
233     int32_t periodCheckCounter_ = 0;
234     int64_t lastPeriod_ = 0;
235     sptr<VSyncDistributor> appVSyncDistributor_;
236     int64_t expectNextVsyncTime_ = 0;
237     bool expectTimeFlag_ = false;
238     int64_t targetPeriod_ = 0;
239     bool clearAllSamplesFlag_ = false;
240     uint32_t vsyncMaxRefreshRate_ = 360; // default max TE
241     int64_t vsyncOffset_ = 0;
242     int64_t nextTimeStamp_ = 0;
243     // Start of DVSync
244     int64_t ComputeDVSyncListenerNextVSyncTimeStamp(const Listener &listener, int64_t now,
245         int64_t referenceTime, int64_t period);
246     int64_t CollectDVSyncListener(const Listener &listener, int64_t now, std::vector<VSyncGenerator::Listener> &ret);
247     void ComputeDVSyncListenerTimeStamp(const Listener &listener, int64_t now, int64_t &nextVSyncTime);
248     bool isLtpoNeedChange_ = false;
249     int64_t occurDvsyncReferenceTime_ = 0;
250     int64_t dvsyncPeriodRecord_ = 0;
251     Listener dvsyncListener_ = {0, nullptr, 0};
252     // End of DVSync
253 };
254 } // impl
255 } // namespace Rosen
256 } // namespace OHOS
257 
258 #endif
259