summaryrefslogtreecommitdiff
path: root/stream-servers/SyncThread.cpp
blob: 1b3e0e74323af4d299df9731c2a219bedf98bc8c (plain)
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
/*
* 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 "SyncThread.h"

#include "base/System.h"
#include "base/Thread.h"
#include "OpenGLESDispatch/OpenGLDispatchLoader.h"
#include "host-common/crash_reporter.h"
#include "host-common/sync_device.h"

#ifndef _MSC_VER
#include <sys/time.h>
#endif
#include <memory>

#define DEBUG 0

#if DEBUG

static uint64_t curr_ms() {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_usec / 1000 + tv.tv_sec * 1000;
}

#define DPRINT(fmt, ...) do { \
    if (!VERBOSE_CHECK(syncthreads)) VERBOSE_ENABLE(syncthreads); \
    VERBOSE_TID_FUNCTION_DPRINT(syncthreads, "@ time=%llu: " fmt, curr_ms(), ##__VA_ARGS__); \
} while(0)

#else

#define DPRINT(...)

#endif

// The single global sync thread instance.
class GlobalSyncThread {
public:
    GlobalSyncThread() = default;

    SyncThread* syncThreadPtr() { return &mSyncThread; }

private:
    SyncThread mSyncThread;
};

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;
static const uint64_t kNumWorkerThreads = 1u;

SyncThread::SyncThread()
    : android::base::Thread(android::base::ThreadFlags::MaskSignals, 512 * 1024),
      mWorkerThreadPool(kNumWorkerThreads, [this](SyncThreadCmd&& cmd) {
          doSyncThreadCmd(&cmd);
      }) {
    this->start();
    mWorkerThreadPool.start();
    initSyncContext();
}

SyncThread::~SyncThread() {
    cleanup();
}

void SyncThread::triggerWait(FenceSync* fenceSync,
                             uint64_t timeline) {
    DPRINT("fenceSyncInfo=0x%llx timeline=0x%lx ...",
            fenceSync, timeline);
    SyncThreadCmd to_send;
    to_send.opCode = SYNC_THREAD_WAIT;
    to_send.fenceSync = fenceSync;
    to_send.timeline = timeline;
    DPRINT("opcode=%u", to_send.opCode);
    sendAsync(to_send);
    DPRINT("exit");
}

void SyncThread::triggerWaitVk(VkFence vkFence, uint64_t timeline) {
    DPRINT("fenceSyncInfo=0x%llx timeline=0x%lx ...", fenceSync, timeline);
    SyncThreadCmd to_send;
    to_send.opCode = SYNC_THREAD_WAIT_VK;
    to_send.vkFence = vkFence;
    to_send.timeline = timeline;
    DPRINT("opcode=%u", to_send.opCode);
    sendAsync(to_send);
    DPRINT("exit");
}

void SyncThread::triggerBlockedWaitNoTimeline(FenceSync* fenceSync) {
    DPRINT("fenceSyncInfo=0x%llx ...", fenceSync);
    SyncThreadCmd to_send;
    to_send.opCode = SYNC_THREAD_BLOCKED_WAIT_NO_TIMELINE;
    to_send.fenceSync = fenceSync;
    DPRINT("opcode=%u", to_send.opCode);
    sendAndWaitForResult(to_send);
    DPRINT("exit");
}

void SyncThread::cleanup() {
    DPRINT("enter");
    SyncThreadCmd to_send;
    to_send.opCode = SYNC_THREAD_EXIT;
    sendAndWaitForResult(to_send);
    DPRINT("signal");
    mLock.lock();
    mExiting = true;
    mCv.signalAndUnlock(&mLock);
    DPRINT("exit");
}

// Private methods below////////////////////////////////////////////////////////

void SyncThread::initSyncContext() {
    DPRINT("enter");
    SyncThreadCmd to_send;
    to_send.opCode = SYNC_THREAD_INIT;
    sendAndWaitForResult(to_send);
    DPRINT("exit");
}

intptr_t SyncThread::main() {
    DPRINT("in sync thread");
    mLock.lock();
    mCv.wait(&mLock, [this] { return mExiting; });

    mWorkerThreadPool.done();
    mWorkerThreadPool.join();
    DPRINT("exited sync thread");
    return 0;
}

int SyncThread::sendAndWaitForResult(SyncThreadCmd& cmd) {
    DPRINT("send with opcode=%d", cmd.opCode);
    android::base::Lock lock;
    android::base::ConditionVariable cond;
    android::base::Optional<int> result = android::base::kNullopt;
    cmd.lock = &lock;
    cmd.cond = &cond;
    cmd.result = &result;

    lock.lock();
    mWorkerThreadPool.enqueue(std::move(cmd));
    cond.wait(&lock, [&result] { return result.hasValue(); });

    DPRINT("result=%d", *result);
    return *result;
}

void SyncThread::sendAsync(SyncThreadCmd& cmd) {
    DPRINT("send with opcode=%u fenceSyncInfo=0x%llx",
           cmd.opCode, cmd.fenceSync);
    mWorkerThreadPool.enqueue(std::move(cmd));
}

void SyncThread::doSyncContextInit() {
    const EGLDispatch* egl = emugl::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 =
        egl->eglCreatePbufferSurface(mDisplay, config, pbufferAttribs);

    const EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
    mContext = egl->eglCreateContext(mDisplay, config, EGL_NO_CONTEXT, contextAttribs);

    egl->eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
}

void SyncThread::doSyncWait(SyncThreadCmd* cmd) {
    DPRINT("enter");

    FenceSync* fenceSync =
        FenceSync::getFromHandle((uint64_t)(uintptr_t)cmd->fenceSync);

    if (!fenceSync) {
        emugl::emugl_sync_timeline_inc(cmd->timeline, kTimelineInterval);
        return;
    }

    EGLint wait_result = 0x0;

    DPRINT("wait on sync obj: %p", cmd->fenceSync);
    wait_result = cmd->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) {
        DPRINT("error: eglClientWaitSync abnormal exit 0x%x. sync handle 0x%llx\n",
               wait_result, (unsigned long long)cmd->fenceSync);
    }

    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.

    emugl::emugl_sync_timeline_inc(cmd->timeline, kTimelineInterval);
    FenceSync::incrementTimelineAndDeleteOldFences();

    DPRINT("done timeline increment");

    DPRINT("exit");
}

int SyncThread::doSyncWaitVk(SyncThreadCmd* cmd) {
    DPRINT("enter");

    auto decoder = goldfish_vk::VkDecoderGlobalState::get();
    auto result = decoder->waitForFence(cmd->vkFence, kDefaultTimeoutNsecs);
    if (result == VK_TIMEOUT) {
        fprintf(stderr, "SyncThread::%s: SYNC_WAIT_VK timeout: vkFence=%p\n",
                __func__, cmd->vkFence);
    } else if (result != VK_SUCCESS) {
        fprintf(stderr, "SyncThread::%s: SYNC_WAIT_VK error: %d vkFence=%p\n",
                __func__, result, cmd->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.
    emugl::emugl_sync_timeline_inc(cmd->timeline, kTimelineInterval);

    DPRINT("done timeline increment");

    DPRINT("exit");
    return result;
}

void SyncThread::doSyncBlockedWaitNoTimeline(SyncThreadCmd* cmd) {
    DPRINT("enter");

    FenceSync* fenceSync =
        FenceSync::getFromHandle((uint64_t)(uintptr_t)cmd->fenceSync);

    if (!fenceSync) {
        return;
    }

    EGLint wait_result = 0x0;

    DPRINT("wait on sync obj: %p", cmd->fenceSync);
    wait_result = cmd->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) {
        fprintf(stderr, "error: eglClientWaitSync abnormal exit 0x%x %p\n",
                wait_result, cmd->fenceSync);
    }
}

void SyncThread::doExit() {

    if (mContext == EGL_NO_CONTEXT) return;

    const EGLDispatch* egl = emugl::LazyLoadedEGLDispatch::get();

    egl->eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
    egl->eglDestroyContext(mDisplay, mContext);
    egl->eglDestroySurface(mDisplay, mContext);
    mContext = EGL_NO_CONTEXT;
    mSurface = EGL_NO_SURFACE;
}

int SyncThread::doSyncThreadCmd(SyncThreadCmd* cmd) {
#if DEBUG
    thread_local static auto threadId = android::base::getCurrentThreadId();
    thread_local static size_t numCommands = 0U;
    DPRINT("threadId = %lu numCommands = %lu cmd = %p", threadId, ++numCommands,
           cmd);
#endif  // DEBUG

    int result = 0;
    switch (cmd->opCode) {
    case SYNC_THREAD_INIT:
        DPRINT("exec SYNC_THREAD_INIT");
        doSyncContextInit();
        break;
    case SYNC_THREAD_WAIT:
        DPRINT("exec SYNC_THREAD_WAIT");
        doSyncWait(cmd);
        break;
    case SYNC_THREAD_WAIT_VK:
        DPRINT("exec SYNC_THREAD_WAIT_VK");
        result = doSyncWaitVk(cmd);
        break;
    case SYNC_THREAD_EXIT:
        DPRINT("exec SYNC_THREAD_EXIT");
        doExit();
        break;
    case SYNC_THREAD_BLOCKED_WAIT_NO_TIMELINE:
        DPRINT("exec SYNC_THREAD_BLOCKED_WAIT_NO_TIMELINE");
        doSyncBlockedWaitNoTimeline(cmd);
        break;
    }

    bool need_reply = cmd->lock != nullptr && cmd->cond != nullptr;
    if (need_reply) {
        cmd->lock->lock();
        *cmd->result = android::base::makeOptional(result);
        cmd->cond->signalAndUnlock(cmd->lock);
    }
    return result;
}

/* static */
SyncThread* SyncThread::get() {
    return sGlobalSyncThread()->syncThreadPtr();
}