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 class VSyncConnection : public VSyncConnectionStub {
51 public:
52     // id for LTPO, windowNodeId for vsync rate control
53     VSyncConnection(const sptr<VSyncDistributor>& distributor, std::string name,
54                     const sptr<IRemoteObject>& token = nullptr, uint64_t id = 0, uint64_t windowNodeId = 0);
55     ~VSyncConnection();
56 
57     virtual VsyncError RequestNextVSync() override;
58     virtual VsyncError RequestNextVSync(const std::string &fromWhom, int64_t lastVSyncTS) override;
59     virtual VsyncError GetReceiveFd(int32_t &fd) override;
60     virtual VsyncError SetVSyncRate(int32_t rate) override;
61     virtual VsyncError Destroy() override;
62     virtual VsyncError SetUiDvsyncSwitch(bool vsyncSwitch) override;
63     virtual VsyncError SetUiDvsyncConfig(int32_t bufferCount) override;
64     virtual VsyncError SetNativeDVSyncSwitch(bool dvsyncSwitch) override;
65     int32_t PostEvent(int64_t now, int64_t period, int64_t vsyncCount);
SetGCNotifyTask(GCNotifyTask hook)66     inline void SetGCNotifyTask(GCNotifyTask hook)
67     {
68         gcNotifyTask_ = hook;
69     }
70 
71     int32_t rate_; // used for LTPS
72     int32_t highPriorityRate_ = -1;
73     bool highPriorityState_ = false;
74     ConnectionInfo info_;
75     bool triggerThisTime_ = false; // used for LTPO
76     uint64_t id_ = 0;
77     uint64_t windowNodeId_ = 0;
78     uint32_t vsyncPulseFreq_ = 1;
79     int64_t referencePulseCount_ = 0;
80     uint32_t refreshRate_ = 0;
81     int32_t proxyPid_;
82     bool rnvTrigger_ = false;
83 private:
84     VsyncError CleanAllLocked();
85     class VSyncConnectionDeathRecipient : public IRemoteObject::DeathRecipient {
86     public:
87         explicit VSyncConnectionDeathRecipient(wptr<VSyncConnection> conn);
88         virtual ~VSyncConnectionDeathRecipient() = default;
89 
90         void OnRemoteDied(const wptr<IRemoteObject>& token) override;
91 
92     private:
93         wptr<VSyncConnection> conn_;
94     };
95     GCNotifyTask gcNotifyTask_ = nullptr;
96     sptr<VSyncConnectionDeathRecipient> vsyncConnDeathRecipient_ = nullptr;
97     sptr<IRemoteObject> token_ = nullptr;
98     // Circular reference, need check
99     wptr<VSyncDistributor> distributor_;
100     sptr<LocalSocketPair> socketPair_;
101     bool isDead_;
102     std::mutex mutex_;
103     std::mutex postEventMutex_;
104     bool isFirstRequestVsync_ = true;
105     bool isFirstSendVsync_ = true;
106 };
107 
108 class VSyncDistributor : public RefBase, public VSyncController::Callback {
109 public:
110 
111     VSyncDistributor(sptr<VSyncController> controller, std::string name);
112     ~VSyncDistributor();
113     // nocopyable
114     VSyncDistributor(const VSyncDistributor &) = delete;
115     VSyncDistributor &operator=(const VSyncDistributor &) = delete;
116 
117     VsyncError AddConnection(const sptr<VSyncConnection>& connection, uint64_t windowNodeId = 0);
118     VsyncError RemoveConnection(const sptr<VSyncConnection> &connection);
119 
120     // fromWhom indicates whether the source is animate or non-animate
121     // lastVSyncTS indicates last vsync time, 0 when non-animate
122     VsyncError RequestNextVSync(const sptr<VSyncConnection> &connection, const std::string &fromWhom = "unknown",
123                                 int64_t lastVSyncTS = 0);
124     VsyncError SetVSyncRate(int32_t rate, const sptr<VSyncConnection>& connection);
125     VsyncError SetHighPriorityVSyncRate(int32_t highPriorityRate, const sptr<VSyncConnection>& connection);
126     VsyncError SetQosVSyncRate(uint64_t windowNodeId, int32_t rate, bool isSystemAnimateScene = false);
127 
128     // used by DVSync
129     bool IsDVsyncOn();
130     void SetFrameIsRender(bool isRender);
131     void MarkRSAnimate();
132     void UnmarkRSAnimate();
133     bool HasPendingUIRNV();
134     uint32_t GetRefreshRate();
135     void RecordVsyncModeChange(uint32_t refreshRate, int64_t period);
136     bool IsUiDvsyncOn();
137     VsyncError SetUiDvsyncSwitch(bool dvsyncSwitch, const sptr<VSyncConnection>& connection);
138     VsyncError SetUiDvsyncConfig(int32_t bufferCount);
139     int64_t GetUiCommandDelayTime();
140     void UpdatePendingReferenceTime(int64_t &timeStamp);
141     void SetHardwareTaskNum(uint32_t num);
142     uint64_t GetRealTimeOffsetOfDvsync(int64_t time);
143     VsyncError SetNativeDVSyncSwitch(bool dvsyncSwitch, const sptr<VSyncConnection> &connection);
144     void SetHasNativeBuffer();
145 
146 private:
147 
148     // check, add more info
149     struct VSyncEvent {
150         int64_t timestamp;
151         int64_t vsyncCount; // used for LTPS
152         int64_t period;
153         int64_t vsyncPulseCount; // used for LTPO
154         uint32_t refreshRate;
155     };
156     void ThreadMain();
157     void EnableVSync();
158     void DisableVSync();
159     void OnVSyncEvent(int64_t now, int64_t period, uint32_t refreshRate, VSyncMode vsyncMode);
160     void CollectConnections(bool &waitForVSync, int64_t timestamp,
161                             std::vector<sptr<VSyncConnection>> &conns, int64_t vsyncCount, bool isDvsyncThread = false);
162     VsyncError QosGetPidByName(const std::string& name, uint32_t& pid);
163     constexpr pid_t ExtractPid(uint64_t id);
164     void PostVSyncEvent(const std::vector<sptr<VSyncConnection>> &conns, int64_t timestamp, bool isDvsyncThread);
165     void ChangeConnsRateLocked();
166     void CollectConnectionsLTPO(bool &waitForVSync, int64_t timestamp,
167         std::vector<sptr<VSyncConnection>> &conns, int64_t vsyncCount, bool isDvsyncThread = false);
168     /* std::pair<id, refresh rate> */
169     void OnConnsRefreshRateChanged(const std::vector<std::pair<uint64_t, uint32_t>> &refreshRates);
170     VsyncError SetQosVSyncRateByPid(uint32_t pid, int32_t rate, bool isSystemAnimateScene = false);
171 
172 #ifdef COMPOSER_SCHED_ENABLE
173     void SubScribeSystemAbility(const std::string& threadName);
174 #endif
175     void WaitForVsyncOrRequest(std::unique_lock<std::mutex> &locker);
176     void WaitForVsyncOrTimeOut(std::unique_lock<std::mutex> &locker);
177     void CollectConns(bool &waitForVSync, int64_t &timestamp,
178         std::vector<sptr<VSyncConnection>> &conns, bool isDvsyncThread);
179     bool PostVSyncEventPreProcess(int64_t &timestamp, std::vector<sptr<VSyncConnection>> &conns);
180     void CheckNeedDisableDvsync(int64_t now, int64_t period);
181     void OnVSyncTrigger(int64_t now, int64_t period, uint32_t refreshRate, VSyncMode vsyncMode);
182     void OnVSyncTriggerPostEvent(int64_t now, uint32_t generatorRefreshRate,
183         std::vector<sptr<VSyncConnection>>& conns, int64_t period, int64_t vsyncCount);
184 
185     sptr<VSyncSystemAbilityListener> saStatusChangeListener_ = nullptr;
186     std::thread threadLoop_;
187     sptr<VSyncController> controller_;
188     std::mutex mutex_;
189     std::condition_variable con_;
190     std::vector<sptr<VSyncConnection> > connections_;
191     std::map<uint64_t, std::vector<sptr<VSyncConnection>>> connectionsMap_;
192     VSyncEvent event_;
193     bool vsyncEnabled_;
194     std::string name_;
195     bool vsyncThreadRunning_ = false;
196     std::vector<std::pair<uint64_t, uint32_t>> changingConnsRefreshRates_; // std::pair<id, refresh rate>
197     VSyncMode vsyncMode_ = VSYNC_MODE_LTPS; // default LTPS
198     std::mutex changingConnsRefreshRatesMtx_;
199     uint32_t generatorRefreshRate_ = 0;
200     std::unordered_map<int32_t, int32_t> connectionCounter_;
201     uint32_t countTraceValue_ = 0;
202 #if defined(RS_ENABLE_DVSYNC)
203     int32_t GetUIDVsyncPid();
204     void SendConnectionsToVSyncWindow(int64_t now, int64_t period, uint32_t refreshRate, VSyncMode vsyncMode,
205         std::unique_lock<std::mutex> &locker);
206     void OnDVSyncTrigger(int64_t now, int64_t period, uint32_t refreshRate, VSyncMode vsyncMode);
207     sptr<DVsync> dvsync_ = nullptr;
208     bool pendingRNVInVsync_ = false;  // for vsync switch to dvsync
209     std::atomic<int64_t> lastDVsyncTS_ = 0;  // for dvsync switch to vsync
210 #endif
211     bool isRs_ = false;
212     std::atomic<bool> hasVsync_ = false;
213 };
214 } // namespace Rosen
215 } // namespace OHOS
216 
217 #endif
218