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_;
ConnectionInfoOHOS::Rosen::ConnectionInfo43     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     bool isFirstRequestVsync_ = true;
104     bool isFirstSendVsync_ = true;
105 };
106 
107 class VSyncDistributor : public RefBase, public VSyncController::Callback {
108 public:
109 
110     VSyncDistributor(sptr<VSyncController> controller, std::string name);
111     ~VSyncDistributor();
112     // nocopyable
113     VSyncDistributor(const VSyncDistributor &) = delete;
114     VSyncDistributor &operator=(const VSyncDistributor &) = delete;
115 
116     VsyncError AddConnection(const sptr<VSyncConnection>& connection, uint64_t windowNodeId = 0);
117     VsyncError RemoveConnection(const sptr<VSyncConnection> &connection);
118 
119     // fromWhom indicates whether the source is animate or non-animate
120     // lastVSyncTS indicates last vsync time, 0 when non-animate
121     VsyncError RequestNextVSync(const sptr<VSyncConnection> &connection, const std::string &fromWhom = "unknown",
122                                 int64_t lastVSyncTS = 0);
123     VsyncError SetVSyncRate(int32_t rate, const sptr<VSyncConnection>& connection);
124     VsyncError SetHighPriorityVSyncRate(int32_t highPriorityRate, const sptr<VSyncConnection>& connection);
125     VsyncError SetQosVSyncRate(uint64_t windowNodeId, int32_t rate, bool isSystemAnimateScene = false);
126 
127     // used by DVSync
128     bool IsDVsyncOn();
129     void SetFrameIsRender(bool isRender);
130     void MarkRSAnimate();
131     void UnmarkRSAnimate();
132     bool HasPendingUIRNV();
133     uint32_t GetRefreshRate();
134     void RecordVsyncModeChange(uint32_t refreshRate, int64_t period);
135     bool IsUiDvsyncOn();
136     VsyncError SetUiDvsyncSwitch(bool dvsyncSwitch, const sptr<VSyncConnection>& connection);
137     VsyncError SetUiDvsyncConfig(int32_t bufferCount);
138     int64_t GetUiCommandDelayTime();
139     void UpdatePendingReferenceTime(int64_t &timeStamp);
140     void SetHardwareTaskNum(uint32_t num);
141     int64_t GetVsyncCount();
142     uint64_t GetRealTimeOffsetOfDvsync(int64_t time);
143     VsyncError SetNativeDVSyncSwitch(bool dvsyncSwitch, const sptr<VSyncConnection> &connection);
144 
145 private:
146 
147     // check, add more info
148     struct VSyncEvent {
149         int64_t timestamp;
150         int64_t vsyncCount; // used for LTPS
151         int64_t period;
152         int64_t vsyncPulseCount; // used for LTPO
153         uint32_t refreshRate;
154     };
155     void ThreadMain();
156     void EnableVSync();
157     void DisableVSync();
158     void OnVSyncEvent(int64_t now, int64_t period,
159         uint32_t refreshRate, VSyncMode vsyncMode, uint32_t vsyncMaxRefreshRate);
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(uint32_t vsyncMaxRefreshRate);
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,
182         uint32_t refreshRate, VSyncMode vsyncMode, uint32_t vsyncMaxRefreshRate);
183 
184     sptr<VSyncSystemAbilityListener> saStatusChangeListener_ = nullptr;
185     std::thread threadLoop_;
186     sptr<VSyncController> controller_;
187     std::mutex mutex_;
188     std::condition_variable con_;
189     std::vector<sptr<VSyncConnection> > connections_;
190     std::map<uint64_t, std::vector<sptr<VSyncConnection>>> connectionsMap_;
191     VSyncEvent event_;
192     bool vsyncEnabled_;
193     std::string name_;
194     bool vsyncThreadRunning_ = false;
195     std::vector<std::pair<uint64_t, uint32_t>> changingConnsRefreshRates_; // std::pair<id, refresh rate>
196     VSyncMode vsyncMode_ = VSYNC_MODE_LTPS; // default LTPS
197     std::mutex changingConnsRefreshRatesMtx_;
198     uint32_t generatorRefreshRate_ = 0;
199     std::unordered_map<int32_t, int32_t> connectionCounter_;
200     uint32_t countTraceValue_ = 0;
201 #if defined(RS_ENABLE_DVSYNC)
202     int32_t GetUIDVsyncPid();
203     void SendConnectionsToVSyncWindow(int64_t now, int64_t period, uint32_t refreshRate, VSyncMode vsyncMode,
204         std::unique_lock<std::mutex> &locker);
205     void OnDVSyncTrigger(int64_t now, int64_t period,
206         uint32_t refreshRate, VSyncMode vsyncMode, uint32_t vsyncMaxRefreshRate);
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