• 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);
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 GetVSyncPeriod(int64_t &period) override;
54     VsyncError OnVSyncRemoteDied();
55 
56     int32_t PostEvent(int64_t now, int64_t period, int64_t vsyncCount);
57 
58     int32_t rate_;
59     int32_t highPriorityRate_ = -1;
60     bool highPriorityState_ = false;
61     ConnectionInfo info_;
62 private:
63     class VSyncConnectionDeathRecipient : public IRemoteObject::DeathRecipient {
64     public:
65         explicit VSyncConnectionDeathRecipient(wptr<VSyncConnection> conn);
66         virtual ~VSyncConnectionDeathRecipient() = default;
67 
68         void OnRemoteDied(const wptr<IRemoteObject>& token) override;
69 
70     private:
71         wptr<VSyncConnection> conn_;
72     };
73     sptr<VSyncConnectionDeathRecipient> vsyncConnDeathRecipient_ = nullptr;
74     sptr<IRemoteObject> token_ = nullptr;
75     // Circular reference, need check
76     wptr<VSyncDistributor> distributor_;
77     sptr<LocalSocketPair> socketPair_;
78     bool isRemoteDead_;
79     std::mutex mutex_;
80 };
81 
82 class VSyncDistributor : public RefBase, public VSyncController::Callback {
83 public:
84 
85     VSyncDistributor(sptr<VSyncController> controller, std::string name);
86     ~VSyncDistributor();
87     // nocopyable
88     VSyncDistributor(const VSyncDistributor &) = delete;
89     VSyncDistributor &operator=(const VSyncDistributor &) = delete;
90 
91     VsyncError AddConnection(const sptr<VSyncConnection>& connection);
92     VsyncError RemoveConnection(const sptr<VSyncConnection> &connection);
93     VsyncError RequestNextVSync(const sptr<VSyncConnection>& connection);
94     VsyncError SetVSyncRate(int32_t rate, const sptr<VSyncConnection>& connection);
95     VsyncError SetHighPriorityVSyncRate(int32_t highPriorityRate, const sptr<VSyncConnection>& connection);
96     VsyncError GetVSyncConnectionInfos(std::vector<ConnectionInfo>& infos);
97     VsyncError GetQosVSyncRateInfos(std::vector<std::pair<uint32_t, int32_t>>& vsyncRateInfos);
98     VsyncError SetQosVSyncRate(uint32_t pid, int32_t rate);
99     VsyncError GetVSyncPeriod(int64_t &period);
100 
101 private:
102 
103     // check, add more info
104     struct VSyncEvent {
105         int64_t timestamp;
106         int64_t vsyncCount;
107         int64_t period;
108     };
109     void ThreadMain();
110     void EnableVSync();
111     void DisableVSync();
112     void OnVSyncEvent(int64_t now, int64_t period);
113     void CollectConnections(bool &waitForVSync, int64_t timestamp,
114                             std::vector<sptr<VSyncConnection>> &conns, int64_t vsyncCount);
115     VsyncError QosGetPidByName(const std::string& name, uint32_t& pid);
116 
117     std::thread threadLoop_;
118     sptr<VSyncController> controller_;
119     std::mutex mutex_;
120     std::condition_variable con_;
121     std::vector<sptr<VSyncConnection> > connections_;
122     VSyncEvent event_;
123     bool vsyncEnabled_;
124     std::string name_;
125     bool vsyncThreadRunning_;
126 };
127 } // namespace Rosen
128 } // namespace OHOS
129 
130 #endif
131