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