Path: blob/main/test/browser/gl_in_two_pthreads.c
7085 views
#include <stdio.h>1#include <string.h>2#include <pthread.h>3#include <GLES2/gl2.h>4#include <math.h>5#include <assert.h>6#include <emscripten/emscripten.h>7#include <emscripten/html5.h>8#include <emscripten/threading.h>9#include <bits/errno.h>10#include <stdlib.h>1112// Pass #define TEST_OFFSCREENCANVAS when testing against OffscreenCanvas support1314void fill(int color)15{16switch(color)17{18case 1: glClearColor(1, 0, 0, 1); printf("On thread %p: you should see a red canvas.\n", pthread_self()); break;19case 2: glClearColor(0, 1, 0, 1); printf("On thread %p: you should see a green canvas.\n", pthread_self()); break;20case 3: glClearColor(0, 0, 1, 1); printf("On thread %p: you should see a blue canvas.\n", pthread_self()); break;21}22glClear(GL_COLOR_BUFFER_BIT);23EMSCRIPTEN_RESULT r = emscripten_webgl_commit_frame();24assert(r == EMSCRIPTEN_RESULT_SUCCESS);2526emscripten_thread_sleep(2000);27}2829void *thread_main(void *param)30{31EmscriptenWebGLContextAttributes attr;32emscripten_webgl_init_context_attributes(&attr);33attr.explicitSwapControl = true;34EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context("#canvas", &attr);35assert(ctx);3637EMSCRIPTEN_RESULT r = emscripten_webgl_make_context_current(ctx);38assert(r == EMSCRIPTEN_RESULT_SUCCESS);3940fill((int)(intptr_t)param);4142r = emscripten_webgl_make_context_current(0);43assert(r == EMSCRIPTEN_RESULT_SUCCESS);4445pthread_exit(0);46}4748void run_thread(int param)49{50pthread_attr_t attr;51pthread_attr_init(&attr);52emscripten_pthread_attr_settransferredcanvases(&attr, "#canvas");53pthread_t thread;54int rc = pthread_create(&thread, &attr, thread_main, (void*)param);55assert(rc == 0);56rc = pthread_join(thread, NULL);57assert(rc == 0);58}5960int main()61{62#ifdef TEST_OFFSCREENCANVAS63if (!emscripten_supports_offscreencanvas())64{65printf("Current browser does not support OffscreenCanvas. Skipping this test.\n");66#ifdef REPORT_RESULT67REPORT_RESULT(1);68#endif69return 0;70}71#endif7273// Create a context74EmscriptenWebGLContextAttributes attr;75emscripten_webgl_init_context_attributes(&attr);76attr.explicitSwapControl = true;77EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context("#canvas", &attr);78assert(ctx);7980// Activate it81EMSCRIPTEN_RESULT r = emscripten_webgl_make_context_current(ctx);82assert(r == EMSCRIPTEN_RESULT_SUCCESS);8384// Draw85fill(3);8687// Release context for the threads to capture the canvas88r = emscripten_webgl_make_context_current(0);89assert(r == EMSCRIPTEN_RESULT_SUCCESS);9091// TODO: Optionally, also delete the above context92// TODO: Test that threads share the context (main thread creates, pthread uses)93run_thread(1);94run_thread(2);95run_thread(3);9697// Reactivate the context we got98r = emscripten_webgl_make_context_current(ctx);99assert(r == EMSCRIPTEN_RESULT_SUCCESS);100101// And render with it again102fill(1);103104#ifdef REPORT_RESULT105REPORT_RESULT(1);106#endif107}108109110