• 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 <thread>
25 #include <condition_variable>
26 
27 #include "local_socketpair.h"
28 #include "vsync_controller.h"
29 #include "vsync_connection_stub.h"
30 
31 namespace OHOS {
32 namespace Rosen {
33 class VSyncDistributor;
34 struct ConnectionInfo {
35     std::string name_;
36     uint64_t postVSyncCount_;
ConnectionInfoConnectionInfo37     ConnectionInfo(std::string name): postVSyncCount_(0)
38     {
39         this->name_ = name;
40     }
41 };
42 
43 class VSyncConnection : public VSyncConnectionStub {
44 public:
45 
46     VSyncConnection(const sptr<VSyncDistributor>& distributor, std::string name,
47                     const sptr<IRemoteObject>& token = nullptr, uint64_t id = 0);
48     ~VSyncConnection();
49 
50     virtual VsyncError RequestNextVSync() override;
51     virtual VsyncError GetReceiveFd(int32_t &fd) override;
52     virtual VsyncError SetVSyncRate(int32_t rate) override;
53     virtual VsyncError Destroy() override;
54 
55     int32_t PostEvent(int64_t now, int64_t period, int64_t vsyncCount);
56 
57     int32_t rate_; // used for LTPS
58     int32_t highPriorityRate_ = -1;
59     bool highPriorityState_ = false;
60     ConnectionInfo info_;
61     int32_t proxyPid_;
62     bool triggerThisTime_ = false; // used for LTPO
63     uint64_t id_ = 0;
64     uint32_t vsyncPulseFreq_ = 1;
65     int64_t referencePulseCount_ = 0;
66     uint32_t refreshRate_ = 0;
67 private:
68     VsyncError CleanAllLocked();
69     class VSyncConnectionDeathRecipient : public IRemoteObject::DeathRecipient {
70     public:
71         explicit VSyncConnectionDeathRecipient(wptr<VSyncConnection> conn);
72         virtual ~VSyncConnectionDeathRecipient() = default;
73 
74         void OnRemoteDied(const wptr<IRemoteObject>& token) override;
75 
76     private:
77         wptr<VSyncConnection> conn_;
78     };
79     sptr<VSyncConnectionDeathRecipient> vsyncConnDeathRecipient_ = nullptr;
80     sptr<IRemoteObject> token_ = nullptr;
81     // Circular reference, need check
82     wptr<VSyncDistributor> distributor_;
83     sptr<LocalSocketPair> socketPair_;
84     bool isDead_;
85     std::mutex mutex_;
86 };
87 
88 class VSyncDistributor : public RefBase, public VSyncController::Callback {
89 public:
90 
91     VSyncDistributor(sptr<VSyncController> controller, std::string name);
92     ~VSyncDistributor();
93     // nocopyable
94     VSyncDistributor(const VSyncDistributor &) = delete;
95     VSyncDistributor &operator=(const VSyncDistributor &) = delete;
96 
97     VsyncError AddConnection(const sptr<VSyncConnection>& connection);
98     VsyncError RemoveConnection(const sptr<VSyncConnection> &connection);
99     VsyncError RequestNextVSync(const sptr<VSyncConnection>& connection);
100     VsyncError SetVSyncRate(int32_t rate, const sptr<VSyncConnection>& connection);
101     VsyncError SetHighPriorityVSyncRate(int32_t highPriorityRate, const sptr<VSyncConnection>& connection);
102     VsyncError GetVSyncConnectionInfos(std::vector<ConnectionInfo>& infos);
103     VsyncError GetQosVSyncRateInfos(std::vector<std::pair<uint32_t, int32_t>>& vsyncRateInfos);
104     VsyncError SetQosVSyncRate(uint32_t pid, int32_t rate);
105 
106 private:
107 
108     // check, add more info
109     struct VSyncEvent {
110         int64_t timestamp;
111         int64_t vsyncCount; // used for LTPS
112         int64_t period;
113         int64_t vsyncPulseCount; // used for LTPO
114         int32_t refreshRate;
115     };
116     void ThreadMain();
117     void EnableVSync();
118     void DisableVSync();
119     void OnVSyncEvent(int64_t now, int64_t period, uint32_t refreshRate, VSyncMode vsyncMode);
120     void CollectConnections(bool &waitForVSync, int64_t timestamp,
121                             std::vector<sptr<VSyncConnection>> &conns, int64_t vsyncCount);
122     VsyncError QosGetPidByName(const std::string& name, uint32_t& pid);
123     void PostVSyncEvent(const std::vector<sptr<VSyncConnection>> &conns, int64_t timestamp);
124     void ChangeConnsRateLocked();
125     void CollectConnectionsLTPO(bool &waitForVSync, int64_t timestamp,
126                                 std::vector<sptr<VSyncConnection>> &conns, int64_t vsyncCount);
127     /* std::pair<id, refresh rate> */
128     void OnConnsRefreshRateChanged(const std::vector<std::pair<uint64_t, uint32_t>> &refreshRates);
129 
130     std::thread threadLoop_;
131     sptr<VSyncController> controller_;
132     std::mutex mutex_;
133     std::condition_variable con_;
134     std::vector<sptr<VSyncConnection> > connections_;
135     std::map<uint32_t, std::vector<sptr<VSyncConnection>>> connectionsMap_;
136     VSyncEvent event_;
137     bool vsyncEnabled_;
138     std::string name_;
139     bool vsyncThreadRunning_;
140     std::unordered_map<int32_t, int32_t> connectionCounter_;
141     std::vector<std::pair<uint64_t, uint32_t>> changingConnsRefreshRates_; // std::pair<id, refresh rate>
142     VSyncMode vsyncMode_ = VSYNC_MODE_LTPS; // default LTPS
143     std::mutex changingConnsRefreshRatesMtx_;
144     uint32_t generatorRefreshRate_ = 0;
145 };
146 } // namespace Rosen
147 } // namespace OHOS
148 
149 #endif
150