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_DISTRIBUTOR_H 18 #define VSYNC_VSYNC_DISTRIBUTOR_H 19 20 #include <refbase.h> 21 22 #include <mutex> 23 #include <vector> 24 #include <thread> 25 #include <condition_variable> 26 27 #include "local_socketpair.h" 28 #include "vsync_controller.h" 29 #include "vsync_connection_stub.h" 30 31 #include "vsync_system_ability_listener.h" 32 33 #if defined(RS_ENABLE_DVSYNC) 34 #include "dvsync.h" 35 #endif 36 37 namespace OHOS { 38 namespace Rosen { 39 class VSyncDistributor; 40 struct ConnectionInfo { 41 std::string name_; 42 uint64_t postVSyncCount_; ConnectionInfoConnectionInfo43 ConnectionInfo(std::string name): postVSyncCount_(0) 44 { 45 this->name_ = name; 46 } 47 }; 48 typedef void (*GCNotifyTask)(bool); 49 50 struct DVSyncFeatureParam { 51 std::vector<bool> switchParams; 52 std::vector<uint32_t> bufferCountParams; 53 std::unordered_map<std::string, std::string> adaptiveConfigs; 54 }; 55 56 class VSyncConnection : public VSyncConnectionStub { 57 public: 58 // id for LTPO, windowNodeId for vsync rate control 59 VSyncConnection(const sptr<VSyncDistributor>& distributor, std::string name, 60 const sptr<IRemoteObject>& token = nullptr, uint64_t id = 0, uint64_t windowNodeId = 0); 61 ~VSyncConnection(); 62 63 virtual VsyncError RequestNextVSync() override; 64 virtual VsyncError RequestNextVSync(const std::string &fromWhom, int64_t lastVSyncTS) override; 65 virtual VsyncError GetReceiveFd(int32_t &fd) override; 66 virtual VsyncError SetVSyncRate(int32_t rate) override; 67 virtual VsyncError Destroy() override; 68 virtual VsyncError SetUiDvsyncSwitch(bool vsyncSwitch) override; 69 virtual VsyncError SetUiDvsyncConfig(int32_t bufferCount) override; 70 virtual VsyncError SetNativeDVSyncSwitch(bool dvsyncSwitch) override; 71 int32_t PostEvent(int64_t now, int64_t period, int64_t vsyncCount); SetGCNotifyTask(GCNotifyTask hook)72 inline void SetGCNotifyTask(GCNotifyTask hook) 73 { 74 gcNotifyTask_ = hook; 75 } 76 void RegisterDeathRecipient(); 77 78 int32_t rate_; // used for LTPS 79 int32_t highPriorityRate_ = -1; 80 bool highPriorityState_ = false; 81 ConnectionInfo info_; 82 bool triggerThisTime_ = false; // used for LTPO 83 uint64_t id_ = 0; 84 uint64_t windowNodeId_ = 0; 85 uint32_t vsyncPulseFreq_ = 1; 86 int64_t referencePulseCount_ = 0; 87 uint32_t refreshRate_ = 0; 88 int32_t proxyPid_; 89 bool rnvTrigger_ = false; 90 private: 91 VsyncError CleanAllLocked(); 92 class VSyncConnectionDeathRecipient : public IRemoteObject::DeathRecipient { 93 public: 94 explicit VSyncConnectionDeathRecipient(wptr<VSyncConnection> conn); 95 virtual ~VSyncConnectionDeathRecipient() = default; 96 97 void OnRemoteDied(const wptr<IRemoteObject>& token) override; 98 99 private: 100 wptr<VSyncConnection> conn_; 101 }; 102 GCNotifyTask gcNotifyTask_ = nullptr; 103 sptr<VSyncConnectionDeathRecipient> vsyncConnDeathRecipient_ = nullptr; 104 sptr<IRemoteObject> token_ = nullptr; 105 // Circular reference, need check 106 wptr<VSyncDistributor> distributor_; 107 sptr<LocalSocketPair> socketPair_; 108 bool isDead_; 109 std::mutex mutex_; 110 std::mutex postEventMutex_; 111 bool isFirstRequestVsync_ = true; 112 bool isFirstSendVsync_ = true; 113 }; 114 115 class VSyncDistributor : public RefBase, public VSyncController::Callback { 116 public: 117 118 VSyncDistributor(sptr<VSyncController> controller, std::string name, DVSyncFeatureParam dvsyncParam = {}); 119 ~VSyncDistributor(); 120 // nocopyable 121 VSyncDistributor(const VSyncDistributor &) = delete; 122 VSyncDistributor &operator=(const VSyncDistributor &) = delete; 123 124 VsyncError AddConnection(const sptr<VSyncConnection>& connection, uint64_t windowNodeId = 0); 125 VsyncError RemoveConnection(const sptr<VSyncConnection> &connection); 126 127 // fromWhom indicates whether the source is animate or non-animate 128 // lastVSyncTS indicates last vsync time, 0 when non-animate 129 VsyncError RequestNextVSync(const sptr<VSyncConnection> &connection, const std::string &fromWhom = "unknown", 130 int64_t lastVSyncTS = 0); 131 VsyncError SetVSyncRate(int32_t rate, const sptr<VSyncConnection>& connection); 132 VsyncError SetHighPriorityVSyncRate(int32_t highPriorityRate, const sptr<VSyncConnection>& connection); 133 VsyncError SetQosVSyncRate(uint64_t windowNodeId, int32_t rate, bool isSystemAnimateScene = false); 134 VsyncError SetQosVSyncRateByPidPublic(uint32_t pid, uint32_t rate, bool isSystemAnimateScene); 135 136 // used by DVSync 137 bool IsDVsyncOn(); 138 void SetFrameIsRender(bool isRender); 139 void MarkRSAnimate(); 140 void UnmarkRSAnimate(); 141 bool HasPendingUIRNV(); 142 uint32_t GetRefreshRate(); 143 void RecordVsyncModeChange(uint32_t refreshRate, int64_t period); 144 bool IsUiDvsyncOn(); 145 VsyncError SetUiDvsyncSwitch(bool dvsyncSwitch, const sptr<VSyncConnection>& connection); 146 VsyncError SetUiDvsyncConfig(int32_t bufferCount); 147 int64_t GetUiCommandDelayTime(); 148 void UpdatePendingReferenceTime(int64_t &timeStamp); 149 void SetHardwareTaskNum(uint32_t num); 150 int64_t GetVsyncCount(); 151 uint64_t GetRealTimeOffsetOfDvsync(int64_t time); 152 VsyncError SetNativeDVSyncSwitch(bool dvsyncSwitch, const sptr<VSyncConnection> &connection); 153 void SetHasNativeBuffer(); 154 void PrintConnectionsStatus(); 155 void FirstRequestVsync(); 156 void NotifyPackageEvent(const std::vector<std::string>& packageList); 157 void NotifyTouchEvent(int32_t touchStatus, int32_t touchCnt); 158 void SetBufferInfo(uint64_t id, const std::string &name, uint32_t queueSize, 159 int32_t bufferCount, int64_t lastConsumeTime); 160 bool AdaptiveDVSyncEnable(const std::string &nodeName, int64_t timeStamp, int32_t bufferCount, bool &needConsume); 161 162 // used by V Rate 163 std::vector<uint64_t> GetSurfaceNodeLinkerIds(uint64_t windowNodeId); 164 165 private: 166 167 // check, add more info 168 struct VSyncEvent { 169 int64_t timestamp; 170 int64_t vsyncCount; // used for LTPS 171 int64_t period; 172 int64_t vsyncPulseCount; // used for LTPO 173 uint32_t refreshRate; 174 }; 175 void ThreadMain(); 176 void EnableVSync(); 177 void DisableVSync(); 178 void OnVSyncEvent(int64_t now, int64_t period, 179 uint32_t refreshRate, VSyncMode vsyncMode, uint32_t vsyncMaxRefreshRate); 180 void CollectConnections(bool &waitForVSync, int64_t timestamp, 181 std::vector<sptr<VSyncConnection>> &conns, int64_t vsyncCount, bool isDvsyncThread = false); 182 VsyncError QosGetPidByName(const std::string& name, uint32_t& pid); 183 constexpr pid_t ExtractPid(uint64_t id); 184 void PostVSyncEvent(const std::vector<sptr<VSyncConnection>> &conns, int64_t timestamp, bool isDvsyncThread); 185 void ChangeConnsRateLocked(uint32_t vsyncMaxRefreshRate); 186 void CollectConnectionsLTPO(bool &waitForVSync, int64_t timestamp, 187 std::vector<sptr<VSyncConnection>> &conns, int64_t vsyncCount, bool isDvsyncThread = false); 188 /* std::pair<id, refresh rate> */ 189 void OnConnsRefreshRateChanged(const std::vector<std::pair<uint64_t, uint32_t>> &refreshRates); 190 VsyncError SetQosVSyncRateByPid(uint32_t pid, int32_t rate, bool isSystemAnimateScene = false); 191 192 #ifdef COMPOSER_SCHED_ENABLE 193 void SubScribeSystemAbility(const std::string& threadName); 194 #endif 195 void WaitForVsyncOrRequest(std::unique_lock<std::mutex> &locker); 196 void WaitForVsyncOrTimeOut(std::unique_lock<std::mutex> &locker); 197 void CollectConns(bool &waitForVSync, int64_t ×tamp, 198 std::vector<sptr<VSyncConnection>> &conns, bool isDvsyncThread); 199 bool PostVSyncEventPreProcess(int64_t ×tamp, std::vector<sptr<VSyncConnection>> &conns); 200 void CheckNeedDisableDvsync(int64_t now, int64_t period); 201 void OnVSyncTrigger(int64_t now, int64_t period, 202 uint32_t refreshRate, VSyncMode vsyncMode, uint32_t vsyncMaxRefreshRate); 203 204 sptr<VSyncSystemAbilityListener> saStatusChangeListener_ = nullptr; 205 std::thread threadLoop_; 206 sptr<VSyncController> controller_; 207 std::mutex mutex_; 208 std::condition_variable con_; 209 std::vector<sptr<VSyncConnection> > connections_; 210 std::map<uint64_t, std::vector<sptr<VSyncConnection>>> connectionsMap_; 211 std::map<uint64_t, std::vector<uint64_t>> pidWindowIdMap_; 212 VSyncEvent event_; 213 bool vsyncEnabled_; 214 std::string name_; 215 bool vsyncThreadRunning_ = false; 216 std::unordered_map<uint64_t, uint32_t> changingConnsRefreshRates_; // std::pair<id, refresh rate> 217 VSyncMode vsyncMode_ = VSYNC_MODE_LTPS; // default LTPS 218 std::mutex changingConnsRefreshRatesMtx_; 219 uint32_t generatorRefreshRate_ = 0; 220 std::unordered_map<int32_t, int32_t> connectionCounter_; 221 uint32_t countTraceValue_ = 0; 222 #if defined(RS_ENABLE_DVSYNC) 223 int32_t GetUIDVsyncPid(); 224 void SendConnectionsToVSyncWindow(int64_t now, int64_t period, uint32_t refreshRate, VSyncMode vsyncMode, 225 std::unique_lock<std::mutex> &locker); 226 void OnDVSyncTrigger(int64_t now, int64_t period, 227 uint32_t refreshRate, VSyncMode vsyncMode, uint32_t vsyncMaxRefreshRate); 228 sptr<DVsync> dvsync_ = nullptr; 229 bool pendingRNVInVsync_ = false; // for vsync switch to dvsync 230 std::atomic<int64_t> lastDVsyncTS_ = 0; // for dvsync switch to vsync 231 #endif 232 bool isRs_ = false; 233 std::atomic<bool> hasVsync_ = false; 234 void ConnectionsPostEvent(std::vector<sptr<VSyncConnection>> &conns, int64_t now, int64_t period, 235 uint32_t generatorRefreshRate, int64_t vsyncCount, bool isDvsyncController); 236 void ConnPostEvent(sptr<VSyncConnection> con, int64_t now, int64_t period, int64_t vsyncCount); 237 void TriggerNext(sptr<VSyncConnection> con); 238 // Start of DVSync 239 void DisableDVSyncController(); 240 void OnDVSyncEvent(int64_t now, int64_t period, 241 uint32_t refreshRate, VSyncMode vsyncMode, uint32_t vsyncMaxRefreshRate); 242 void InitDVSync(DVSyncFeatureParam dvsyncParam = {}); 243 void DVSyncAddConnection(const sptr<VSyncConnection> &connection); 244 void DVSyncDisableVSync(); 245 void RecordEnableVsync(); 246 void DVSyncRecordVSync(int64_t now, int64_t period, uint32_t refreshRate, bool isDvsyncController); 247 bool DVSyncCheckSkipAndUpdateTs(const sptr<VSyncConnection> &connection, int64_t &timeStamp); 248 bool DVSyncNeedSkipUi(const sptr<VSyncConnection> &connection); 249 void DVSyncRecordRNV(const sptr<VSyncConnection> &connection, const std::string &fromWhom, int64_t lastVSyncTS); 250 bool DVSyncCheckPreexecuteAndUpdateTs(const sptr<VSyncConnection> &connection, int64_t ×tamp, 251 int64_t &period, int64_t &vsyncCount); 252 sptr<VSyncController> dvsyncController_ = nullptr; 253 bool dvsyncControllerEnabled_ = false; 254 // End of DVSync 255 int64_t beforeWaitRnvTime_ = 0; 256 int64_t afterWaitRnvTime_ = 0; 257 int64_t lastNotifyTime_ = 0; 258 std::atomic<int64_t> beforePostEvent_ = 0; 259 std::atomic<int64_t> startPostEvent_ = 0; 260 bool isFirstRequest_ = false; 261 bool isFirstSend_ = false; 262 }; 263 } // namespace Rosen 264 } // namespace OHOS 265 266 #endif 267