-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcreate-disp_threads.cpp
More file actions
257 lines (212 loc) · 7.74 KB
/
create-disp_threads.cpp
File metadata and controls
257 lines (212 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "create-disp_shared.h"
namespace create_disp {
void update_thread_main()
{
for (;;) {
int disp = -1;
if (g_running.load(std::memory_order_acquire) &&
g_update_pending_mask.load(std::memory_order_acquire) == 0) {
uint32_t seq = g_update_wake_seq.load(std::memory_order_acquire);
if (g_running.load(std::memory_order_acquire) &&
g_update_pending_mask.load(std::memory_order_acquire) == 0) {
g_update_wake_seq.wait(seq, std::memory_order_relaxed);
}
}
if (!g_running.load(std::memory_order_acquire)) {
break;
}
if (!take_next_update_display(disp)) [[unlikely]] {
continue;
}
const uint8_t work =
g_update_work[disp].exchange(kUpdateWorkNone, std::memory_order_acquire);
const bool do_disconnect = (work & kUpdateWorkDisconnect) != 0;
const bool do_update = (work & kUpdateWorkRefresh) != 0;
if (do_disconnect) {
disconnect_display(disp);
}
if (do_update) {
(void)update_display(disp);
}
}
}
void poll_thread_main()
{
int hard_poll_failures = 0;
for (;;) {
if (!g_running.load(std::memory_order_acquire)) {
break;
}
if (g_reopen_requested.exchange(false, std::memory_order_acquire)) {
std::unique_lock<std::shared_mutex> lk(g_drm_mutex);
int old_fd = drm_fd.exchange(-1, std::memory_order_acq_rel);
if (old_fd >= 0) {
::close(old_fd);
}
drm_ready.store(false, std::memory_order_release);
for (int tries = 0; tries < 30; ++tries) {
int fd = open_evdi_lindroid_or_create();
if (fd >= 0) {
drm_fd.store(fd, std::memory_order_release);
drm_ready.store(true, std::memory_order_release);
fprintf(stderr, "Reopened evdi-lindroid fd=%d\n", fd);
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
hard_poll_failures = 0;
if (drm_ready.load(std::memory_order_acquire)) {
std::array<int, kMaxDriverDisplays> reconnect_displays = {};
int reconnect_count = 0;
{
std::lock_guard<std::mutex> slk(g_display_mutex);
for (int d = 0; d < kMaxDriverDisplays; ++d) {
auto& D = g_displays[d];
reset_display_bindings_locked(d);
if (D.connected && D.hwcDisplay) {
D.width = 0;
D.height = 0;
D.stride = 0;
publish_display_runtime_locked(d);
g_resync_pending[d].store(true, std::memory_order_release);
if (reconnect_count < kMaxDriverDisplays) {
reconnect_displays[reconnect_count++] = d;
}
}
}
}
for (int i = 0; i < reconnect_count; ++i) {
schedule_update(reconnect_displays[i]);
}
}
}
drm_evdi_poll poll_cmd = {};
std::array<uint8_t, 32> poll_payload{};
poll_cmd.data = poll_payload.data();
int ret = drm_ioctl(DRM_IOCTL_EVDI_POLL, &poll_cmd);
if (ret < 0) [[unlikely]] {
if (errno == EINTR || errno == ERESTART) {
if (!g_running.load(std::memory_order_acquire)) {
break;
}
continue;
}
if (errno == ENODEV || errno == EBADF) {
if (should_request_reopen(errno) && ++hard_poll_failures >= 3) {
request_reopen();
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
} else {
hard_poll_failures = 0;
}
continue;
}
hard_poll_failures = 0;
if (poll_cmd.event != none) [[likely]] {
if (poll_cmd.event == swap_to) [[likely]] {
swap_to_buff(poll_payload, poll_cmd.poll_id);
} else {
switch (poll_cmd.event) {
case get_buf:
get_buf_from_map(poll_payload, poll_cmd.poll_id);
break;
case destroy_buf:
destroy_buff(poll_payload, poll_cmd.poll_id);
break;
case create_buf:
create_buff(poll_payload, poll_cmd.poll_id);
break;
default:
break;
}
}
}
}
}
int run_create_disp()
{
int composerSequenceId = 0;
sd_notifyf(0, "MAINPID=%lu", (unsigned long)getpid());
sd_notify(0, "STATUS=Initializing create-disp…");
init_free_driver_slots_once();
buffer_table_reserve_ids(kExpectedHandles);
g_hwc_to_drv.reserve(kMaxDriverDisplays);
g_drv_to_hwc.reserve(kMaxDriverDisplays);
drm_fd.store(-1, std::memory_order_relaxed);
for (int i = 0; i < 5 * 1000; ++i) {
int fd = find_evdi_lindroid_device();
if (fd >= 0) {
drm_fd.store(fd, std::memory_order_relaxed);
break;
}
usleep(1000);
}
if (drm_fd.load(std::memory_order_relaxed) < 0) {
int fd = open_evdi_lindroid_or_create();
if (fd >= 0) {
drm_fd.store(fd, std::memory_order_relaxed);
}
}
if (drm_fd.load(std::memory_order_relaxed) < 0) {
return EXIT_FAILURE;
}
drm_ready.store(true, std::memory_order_release);
hwcDevice = hwc2_compat_device_new(false);
if (!hwcDevice) {
return EXIT_FAILURE;
}
assert(hwcDevice);
hwc2_compat_device_register_callback(hwcDevice, &eventListener, composerSequenceId);
try {
g_update_thread = std::thread(update_thread_main);
} catch (...) {
fprintf(stderr, "Failed to create update thread\n");
{
int fd = drm_fd.exchange(-1, std::memory_order_relaxed);
if (fd >= 0) ::close(fd);
}
return EXIT_FAILURE;
}
install_signal_handlers();
sd_notify(0, "READY=1");
sd_notify(0, "STATUS=create-disp ready.");
try {
g_poll_thread = std::thread(poll_thread_main);
} catch (...) {
fprintf(stderr, "Failed to create poll thread\n");
g_running.store(false, std::memory_order_release);
g_update_wake_seq.fetch_add(1, std::memory_order_release);
g_update_wake_seq.notify_all();
if (g_update_thread.joinable()) {
g_update_thread.join();
}
{
int fd = drm_fd.exchange(-1, std::memory_order_acq_rel);
if (fd >= 0) ::close(fd);
}
return EXIT_FAILURE;
}
while (g_running.load(std::memory_order_acquire)) {
pause();
}
g_update_wake_seq.fetch_add(1, std::memory_order_release);
g_update_wake_seq.notify_all();
if (g_poll_thread.joinable()) {
kick_thread_out_of_ioctl(g_poll_thread);
}
sd_notify(0, "STATUS=Stopping poll thread…");
if (g_poll_thread.joinable()) {
g_poll_thread.join();
}
drm_shutdown_close_fd();
sd_notify(0, "STATUS=Stopping update thread…");
g_update_wake_seq.fetch_add(1, std::memory_order_release);
g_update_wake_seq.notify_all();
if (g_update_thread.joinable()) {
g_update_thread.join();
}
buffer_table_shutdown();
sd_notify(0, "STATUS=Shutting down…");
return EXIT_SUCCESS;
}
}