|
| 1 | +#include <atomic> |
| 2 | +#include <thread> |
| 3 | +#include <stdio.h> |
| 4 | + |
| 5 | +#include <GLES2/gl2.h> |
| 6 | + |
| 7 | +#include <emscripten/emscripten.h> |
| 8 | +#include <emscripten/html5.h> |
| 9 | +#include <emscripten/threading.h> |
| 10 | + |
| 11 | +static std::atomic<bool> g_done = false; |
| 12 | +static std::atomic<bool> g_ok = false; |
| 13 | + |
| 14 | +static void thread_main() { |
| 15 | + EmscriptenWebGLContextAttributes attr; |
| 16 | + emscripten_webgl_init_context_attributes(&attr); |
| 17 | + attr.explicitSwapControl = true; |
| 18 | + |
| 19 | + EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context("#canvas", &attr); |
| 20 | + if (ctx > 0 && emscripten_webgl_make_context_current(ctx) == EMSCRIPTEN_RESULT_SUCCESS) { |
| 21 | + glClearColor(0.0f, 1.0f, 0.0f, 1.0f); |
| 22 | + glClear(GL_COLOR_BUFFER_BIT); |
| 23 | + emscripten_webgl_commit_frame(); |
| 24 | + emscripten_webgl_make_context_current(0); |
| 25 | + emscripten_webgl_destroy_context(ctx); |
| 26 | + g_ok = true; |
| 27 | + } |
| 28 | + |
| 29 | + g_done = true; |
| 30 | +} |
| 31 | + |
| 32 | +static void poll_done(void*) { |
| 33 | + if (!g_done) { |
| 34 | + emscripten_async_call(poll_done, nullptr, 20); |
| 35 | + return; |
| 36 | + } |
| 37 | + emscripten_force_exit(g_ok ? 0 : 1); |
| 38 | +} |
| 39 | + |
| 40 | +int main() { |
| 41 | + if (!emscripten_supports_offscreencanvas()) { |
| 42 | + printf("Current browser does not support OffscreenCanvas. Skipping this test.\n"); |
| 43 | + return 0; |
| 44 | + } |
| 45 | + |
| 46 | + // The new API is intended for std::thread users that cannot pass |
| 47 | + // pthread_attr_t into thread construction. |
| 48 | + emscripten_set_next_thread_transferredcanvases("#canvas"); |
| 49 | + |
| 50 | + std::thread worker(thread_main); |
| 51 | + worker.detach(); |
| 52 | + |
| 53 | + emscripten_async_call(poll_done, nullptr, 20); |
| 54 | + emscripten_exit_with_live_runtime(); |
| 55 | + __builtin_trap(); |
| 56 | +} |
0 commit comments