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