Repository navigation
Expand file tree
/
Copy pathsync_thread.cpp
More file actions
472 lines (401 loc) · 16.5 KB
/
Copy pathsync_thread.cpp
File metadata and controls
472 lines (401 loc) · 16.5 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "sync_thread.h"
#if GFXSTREAM_ENABLE_HOST_GLES
#include "OpenGLESDispatch/OpenGLDispatchLoader.h"
#endif
#include "gfxstream/system/System.h"
#include "gfxstream/threads/Thread.h"
#include "gfxstream/host/tracing.h"
#include "gfxstream/common/logging.h"
#include "gfxstream/host/sync_device.h"
#ifndef _MSC_VER
#include <sys/time.h>
#endif
#include <memory>
namespace gfxstream {
namespace host {
#if GFXSTREAM_ENABLE_HOST_GLES
using gl::EGLDispatch;
using gl::EmulatedEglFenceSync;
#endif
#define DEBUG 0
#if DEBUG
static uint64_t curr_ns() {
auto now = std::chrono::system_clock::now();
auto now_ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(now);
return now_ns.time_since_epoch().count();
}
#define DPRINT(fmt, ...) \
do { \
GFXSTREAM_INFO("@ time=%llu, %s: " fmt, curr_ns(), __func__, ##__VA_ARGS__); \
} while (0)
#else
#define DPRINT(...)
#endif
#define SYNC_THREAD_CHECK(condition) \
do { \
if (!(condition)) { \
GFXSTREAM_FATAL(#condition " was false."); \
} \
} while (0)
// The single global sync thread instance.
class GlobalSyncThread {
public:
GlobalSyncThread() = default;
void initialize(bool hasGl) {
AutoLock mutex(mLock);
SYNC_THREAD_CHECK(!mSyncThread);
mSyncThread = std::make_unique<SyncThread>(hasGl);
}
SyncThread* syncThreadPtr() {
AutoLock mutex(mLock);
return mSyncThread.get();
}
void destroy() {
AutoLock mutex(mLock);
mSyncThread = nullptr;
}
private:
std::unique_ptr<SyncThread> mSyncThread = nullptr;
// lock for the access to this object
gfxstream::base::Lock mLock;
using AutoLock = gfxstream::base::AutoLock;
};
static GlobalSyncThread* sGlobalSyncThread() {
static GlobalSyncThread* t = new GlobalSyncThread;
return t;
}
static const uint32_t kTimelineInterval = 1;
static const uint64_t kDefaultTimeoutNsecs = 5ULL * 1000ULL * 1000ULL * 1000ULL;
SyncThread::SyncThread(bool hasGl)
: gfxstream::base::Thread(gfxstream::base::ThreadFlags::MaskSignals, 512 * 1024),
mWorkerThreadPool(kNumWorkerThreads,
[](ThreadPool::WorkerId id) {
GFXSTREAM_TRACE_NAME_THREAD(
std::string("Gfxstream SyncThread Worker ") +
std::to_string(id));
},
[this](Command&& command, ThreadPool::WorkerId id) {
doSyncThreadCmd(std::move(command), id);
}),
mHasGl(hasGl) {
this->start();
mWorkerThreadPool.start();
#if GFXSTREAM_ENABLE_HOST_GLES
if (hasGl) {
initSyncEGLContext();
}
#endif
}
SyncThread::~SyncThread() {
cleanup();
}
#if GFXSTREAM_ENABLE_HOST_GLES
void SyncThread::triggerWait(EmulatedEglFenceSync* fenceSync,
uint64_t timeline) {
std::stringstream ss;
ss << "triggerWait fenceSyncInfo=0x" << std::hex << reinterpret_cast<uintptr_t>(fenceSync)
<< " timeline=0x" << std::hex << timeline;
sendAsync(
[fenceSync, timeline, this](WorkerId) {
doSyncWait(fenceSync, [timeline] {
DPRINT("wait done (with fence), use goldfish sync timeline inc");
gfxstream_sync_timeline_inc(timeline, kTimelineInterval);
});
},
ss.str());
}
void SyncThread::triggerWaitWithCompletionCallback(EmulatedEglFenceSync* fenceSync, FenceCompletionCallback cb) {
std::stringstream ss;
ss << "triggerWaitWithCompletionCallback fenceSyncInfo=0x" << std::hex
<< reinterpret_cast<uintptr_t>(fenceSync);
sendAsync(
[fenceSync, cb = std::move(cb), this](WorkerId) { doSyncWait(fenceSync, std::move(cb)); },
ss.str());
}
void SyncThread::initSyncEGLContext() {
// b/383543476: force sequential init to work around a deadlock that is
// believed to be a driver issue.
std::mutex initMutex;
mWorkerThreadPool.broadcast([&, this] {
return Command{
.mTask = std::packaged_task<int(WorkerId)>([&, this](WorkerId workerId) {
DPRINT("for worker id: %d", workerId);
std::lock_guard<std::mutex> initLock(initMutex);
// We shouldn't initialize EGL context, when SyncThread is initialized
// without GL enabled.
SYNC_THREAD_CHECK(mHasGl);
const EGLDispatch* egl = gl::LazyLoadedEGLDispatch::get();
mDisplay = egl->eglGetDisplay(EGL_DEFAULT_DISPLAY);
int eglMaj, eglMin;
egl->eglInitialize(mDisplay, &eglMaj, &eglMin);
const EGLint configAttribs[] = {
EGL_SURFACE_TYPE,
EGL_PBUFFER_BIT,
EGL_RENDERABLE_TYPE,
EGL_OPENGL_ES2_BIT,
EGL_RED_SIZE,
8,
EGL_GREEN_SIZE,
8,
EGL_BLUE_SIZE,
8,
EGL_NONE,
};
EGLint nConfigs;
EGLConfig config;
egl->eglChooseConfig(mDisplay, configAttribs, &config, 1, &nConfigs);
const EGLint pbufferAttribs[] = {
EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE,
};
mSurface[workerId] = egl->eglCreatePbufferSurface(mDisplay, config, pbufferAttribs);
const EGLint contextAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
mContext[workerId] =
egl->eglCreateContext(mDisplay, config, EGL_NO_CONTEXT, contextAttribs);
egl->eglMakeCurrent(mDisplay, mSurface[workerId], mSurface[workerId],
mContext[workerId]);
return 0;
}),
.mDescription = "init sync EGL context",
};
});
mWorkerThreadPool.waitAllItems();
}
void SyncThread::doSyncWait(EmulatedEglFenceSync* fenceSync, std::function<void()> onComplete) {
DPRINT("enter");
if (!EmulatedEglFenceSync::getFromHandle((uint64_t)(uintptr_t)fenceSync)) {
if (onComplete) {
onComplete();
}
return;
}
// We shouldn't use EmulatedEglFenceSync to wait, when SyncThread is initialized
// without GL enabled, because EmulatedEglFenceSync uses EGL/GLES.
SYNC_THREAD_CHECK(mHasGl);
EGLint wait_result = 0x0;
DPRINT("wait on sync obj: %p", fenceSync);
wait_result = fenceSync->wait(kDefaultTimeoutNsecs);
DPRINT(
"done waiting, with wait result=0x%x. "
"increment timeline (and signal fence)",
wait_result);
if (wait_result != EGL_CONDITION_SATISFIED_KHR) {
EGLint error = gl::s_egl.eglGetError();
DPRINT("error: eglClientWaitSync abnormal exit 0x%x. sync handle 0x%llx. egl error = %#x\n",
wait_result, (unsigned long long)fenceSync, error);
(void)error;
}
DPRINT("issue timeline increment");
// We always unconditionally increment timeline at this point, even
// if the call to eglClientWaitSync returned abnormally.
// There are three cases to consider:
// - EGL_CONDITION_SATISFIED_KHR: either the sync object is already
// signaled and we need to increment this timeline immediately, or
// we have waited until the object is signaled, and then
// we increment the timeline.
// - EGL_TIMEOUT_EXPIRED_KHR: the fence command we put in earlier
// in the OpenGL stream is not actually ever signaled, and we
// end up blocking in the above eglClientWaitSyncKHR call until
// our timeout runs out. In this case, provided we have waited
// for |kDefaultTimeoutNsecs|, the guest will have received all
// relevant error messages about fence fd's not being signaled
// in time, so we are properly emulating bad behavior even if
// we now increment the timeline.
// - EGL_FALSE (error): chances are, the underlying EGL implementation
// on the host doesn't actually support fence objects. In this case,
// we should fail safe: 1) It must be only very old or faulty
// graphics drivers / GPU's that don't support fence objects.
// 2) The consequences of signaling too early are generally, out of
// order frames and scrambled textures in some apps. But, not
// incrementing the timeline means that the app's rendering freezes.
// So, despite the faulty GPU driver, not incrementing is too heavyweight a response.
if (onComplete) {
onComplete();
}
EmulatedEglFenceSync::incrementTimelineAndDeleteOldFences();
DPRINT("done timeline increment");
DPRINT("exit");
}
#endif
void SyncThread::triggerWaitVk(VkFence vkFence, uint64_t timeline) {
std::stringstream ss;
ss << "triggerWaitVk vkFence=0x" << std::hex << reinterpret_cast<uintptr_t>(vkFence)
<< " timeline=0x" << std::hex << timeline;
sendAsync(
[vkFence, timeline](WorkerId) {
doSyncWaitVk(vkFence, [timeline] {
DPRINT("vk wait done, use goldfish sync timeline inc");
gfxstream_sync_timeline_inc(timeline, kTimelineInterval);
});
},
ss.str());
}
void SyncThread::triggerWaitVkWithCompletionCallback(VkFence vkFence, FenceCompletionCallback cb) {
std::stringstream ss;
ss << "triggerWaitVkWithCompletionCallback vkFence=0x" << std::hex
<< reinterpret_cast<uintptr_t>(vkFence);
sendAsync([vkFence, cb = std::move(cb)](WorkerId) { doSyncWaitVk(vkFence, std::move(cb)); },
ss.str());
}
void SyncThread::triggerWaitVkQsriWithCompletionCallback(VkImage vkImage, FenceCompletionCallback cb) {
std::stringstream ss;
ss << "triggerWaitVkQsriWithCompletionCallback vkImage=0x"
<< reinterpret_cast<uintptr_t>(vkImage);
sendAsync(
[vkImage, cb = std::move(cb)](WorkerId) {
auto decoder = vk::VkDecoderGlobalState::get();
auto res = decoder->registerQsriCallback(vkImage, cb);
// If registerQsriCallback does not schedule the callback, we still need to complete
// the task, otherwise we may hit deadlocks on tasks on the same ring.
if (!res.CallbackScheduledOrFired()) {
cb();
}
},
ss.str());
}
void SyncThread::triggerWaitVkQsri(VkImage vkImage, uint64_t timeline) {
std::stringstream ss;
ss << "triggerWaitVkQsri vkImage=0x" << std::hex << vkImage
<< " timeline=0x" << std::hex << timeline;
sendAsync(
[vkImage, timeline](WorkerId) {
auto decoder = vk::VkDecoderGlobalState::get();
auto res = decoder->registerQsriCallback(vkImage, [timeline](){
gfxstream_sync_timeline_inc(timeline, kTimelineInterval);
});
// If registerQsriCallback does not schedule the callback, we still need to complete
// the task, otherwise we may hit deadlocks on tasks on the same ring.
if (!res.CallbackScheduledOrFired()) {
gfxstream_sync_timeline_inc(timeline, kTimelineInterval);
}
},
ss.str());
}
void SyncThread::triggerGeneral(FenceCompletionCallback cb, std::string description) {
std::stringstream ss;
ss << "triggerGeneral: " << description;
sendAsync(std::bind(std::move(cb)), ss.str());
}
void SyncThread::cleanup() {
#if GFXSTREAM_ENABLE_HOST_GLES
if (mHasGl) {
// This works fine because ThreadPool::enqueue will distribute the
// tasks to workers in a linear order. Otherwise, we'll need to
// implement a broadcast event to ensure all threads destroy their
// resources correctly out of order.
for (uint32_t i = 0; i < kNumWorkerThreads; i++) {
sendAndWaitForResult(
[this](WorkerId workerId) {
const EGLDispatch* egl = gl::LazyLoadedEGLDispatch::get();
egl->eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (mContext[workerId] != EGL_NO_CONTEXT) {
egl->eglDestroyContext(mDisplay, mContext[workerId]);
mContext[workerId] = EGL_NO_CONTEXT;
}
if (mSurface[workerId] != EGL_NO_SURFACE) {
egl->eglDestroySurface(mDisplay, mSurface[workerId]);
mSurface[workerId] = EGL_NO_SURFACE;
}
return 0;
},
"gl_cleanup");
}
}
#endif
DPRINT("signal");
mLock.lock();
mExiting = true;
mCv.signalAndUnlock(&mLock);
DPRINT("exit");
// Wait for the control thread to exit. We can't destroy the SyncThread
// before we wait the control thread.
if (!wait(nullptr)) {
GFXSTREAM_ERROR("Fail to wait the control thread of the SyncThread to exit.");
}
}
// Private methods below////////////////////////////////////////////////////////
intptr_t SyncThread::main() {
DPRINT("in sync thread");
mLock.lock();
mCv.wait(&mLock, [this] { return mExiting; });
mLock.unlock();
mWorkerThreadPool.done();
mWorkerThreadPool.join();
DPRINT("exited sync thread");
return 0;
}
int SyncThread::sendAndWaitForResult(std::function<int(WorkerId)> job, std::string description) {
DPRINT("sendAndWaitForResult task(%s)", description.c_str());
std::packaged_task<int(WorkerId)> task(std::move(job));
std::future<int> resFuture = task.get_future();
Command command = {
.mTask = std::move(task),
.mDescription = std::move(description),
};
mWorkerThreadPool.enqueue(std::move(command));
auto res = resFuture.get();
DPRINT("exit");
return res;
}
void SyncThread::sendAsync(std::function<void(WorkerId)> job, std::string description) {
DPRINT("send task(%s)", description.c_str());
mWorkerThreadPool.enqueue(Command{
.mTask =
std::packaged_task<int(WorkerId)>([job = std::move(job)](WorkerId workerId) mutable {
job(workerId);
return 0;
}),
.mDescription = std::move(description),
});
DPRINT("exit");
}
void SyncThread::doSyncThreadCmd(Command&& command, WorkerId workerId) {
std::unique_ptr<std::unordered_map<std::string, std::string>> syncThreadData =
std::make_unique<std::unordered_map<std::string, std::string>>();
syncThreadData->insert({{"syncthread_cmd_desc", command.mDescription}});
command.mTask(workerId);
}
int SyncThread::doSyncWaitVk(VkFence vkFence, std::function<void()> onComplete) {
DPRINT("enter");
auto decoder = vk::VkDecoderGlobalState::get();
auto result = decoder->waitForFence(vkFence, kDefaultTimeoutNsecs);
if (result == VK_TIMEOUT) {
DPRINT("SYNC_WAIT_VK timeout: vkFence=%p", vkFence);
} else if (result != VK_SUCCESS) {
DPRINT("SYNC_WAIT_VK error: %d vkFence=%p", result, vkFence);
}
DPRINT("issue timeline increment");
// We always unconditionally increment timeline at this point, even
// if the call to vkWaitForFences returned abnormally.
// See comments in |doSyncWait| about the rationale.
if (onComplete) {
onComplete();
}
DPRINT("done timeline increment");
DPRINT("exit");
return result;
}
/* static */
SyncThread* SyncThread::get() {
auto res = sGlobalSyncThread()->syncThreadPtr();
SYNC_THREAD_CHECK(res);
return res;
}
void SyncThread::initialize(bool hasGl) { sGlobalSyncThread()->initialize(hasGl); }
void SyncThread::destroy() { sGlobalSyncThread()->destroy(); }
} // namespace host
} // namespace gfxstream