-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathstd_thread_transferred_canvas.cpp
More file actions
56 lines (45 loc) · 1.47 KB
/
std_thread_transferred_canvas.cpp
File metadata and controls
56 lines (45 loc) · 1.47 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
#include <atomic>
#include <thread>
#include <stdio.h>
#include <GLES2/gl2.h>
#include <emscripten/emscripten.h>
#include <emscripten/html5.h>
#include <emscripten/threading.h>
static std::atomic<bool> g_done = false;
static std::atomic<bool> g_ok = false;
static void thread_main() {
EmscriptenWebGLContextAttributes attr;
emscripten_webgl_init_context_attributes(&attr);
attr.explicitSwapControl = true;
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context("#canvas", &attr);
if (ctx > 0 && emscripten_webgl_make_context_current(ctx) == EMSCRIPTEN_RESULT_SUCCESS) {
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
emscripten_webgl_commit_frame();
emscripten_webgl_make_context_current(0);
emscripten_webgl_destroy_context(ctx);
g_ok = true;
}
g_done = true;
}
static void poll_done(void*) {
if (!g_done) {
emscripten_async_call(poll_done, nullptr, 20);
return;
}
emscripten_force_exit(g_ok ? 0 : 1);
}
int main() {
if (!emscripten_supports_offscreencanvas()) {
printf("Current browser does not support OffscreenCanvas. Skipping this test.\n");
return 0;
}
// The new API is intended for std::thread users that cannot pass
// pthread_attr_t into thread construction.
emscripten_set_next_thread_transferredcanvases("#canvas");
std::thread worker(thread_main);
worker.detach();
emscripten_async_call(poll_done, nullptr, 20);
emscripten_exit_with_live_runtime();
__builtin_trap();
}