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