Path: blob/main/test/browser/gl_only_in_pthread.c
7085 views
// Copyright 2018 The Emscripten Authors. All rights reserved.1// Emscripten is available under two separate licenses, the MIT license and the2// University of Illinois/NCSA Open Source License. Both these licenses can be3// found in the LICENSE file.45#include <stdio.h>6#include <string.h>7#include <pthread.h>8#include <GLES2/gl2.h>9#include <math.h>10#include <assert.h>11#include <emscripten/emscripten.h>12#include <emscripten/html5.h>13#include <emscripten/threading.h>14#include <bits/errno.h>15#include <stdlib.h>1617pthread_t thread;1819EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx;2021_Atomic int threadRunning = 0;2223void *ThreadMain(void *arg)24{25printf("Thread started. You should see the WebGL canvas fade from black to red.\n");26EmscriptenWebGLContextAttributes attr;27emscripten_webgl_init_context_attributes(&attr);28attr.explicitSwapControl = true;29ctx = emscripten_webgl_create_context("#canvas", &attr);30emscripten_webgl_make_context_current(ctx);3132double color = 0;33for(int i = 0; i < 100; ++i)34{35color += 0.01;36glClearColor(color, 0, 0, 1);37glClear(GL_COLOR_BUFFER_BIT);38EMSCRIPTEN_RESULT r = emscripten_webgl_commit_frame();39assert(r == EMSCRIPTEN_RESULT_SUCCESS);4041double now = emscripten_get_now();42while(emscripten_get_now() - now < 16) /*no-op*/;43}4445#if 0 // TODO: this doesn't work yet for some reason46unsigned char data[64];47glReadPixels(0, 0, 4, 4, GL_RGBA, GL_UNSIGNED_BYTE, data);48assert(glGetError() == 0);49printf("%d %d %d %d\n", data[0], data[1], data[2], data[3]);50#endif5152emscripten_webgl_make_context_current(0);53emscripten_webgl_destroy_context(ctx);54threadRunning = 0;55printf("Thread quit\n");56pthread_exit(0);57}5859void CreateThread()60{61pthread_attr_t attr;62pthread_attr_init(&attr);63emscripten_pthread_attr_settransferredcanvases(&attr, "#canvas");64int rc = pthread_create(&thread, &attr, ThreadMain, 0);65if (rc == ENOSYS)66{67printf("Test Skipped! OffscreenCanvas is not supported!\n");68#ifdef REPORT_RESULT69REPORT_RESULT(0); // for now, report a skip as success70#endif71exit(1);72}73pthread_attr_destroy(&attr);74threadRunning = 1;75}7677void PollThreadExit(void *arg)78{79if (!threadRunning)80{81printf("Test finished.\n");82#ifdef REPORT_RESULT83REPORT_RESULT(0);84#endif85} else {86emscripten_async_call(PollThreadExit, 0, 100);87}88}8990int main()91{92if (!emscripten_supports_offscreencanvas())93{94printf("Current browser does not support OffscreenCanvas. Skipping this test.\n");95#ifdef REPORT_RESULT96REPORT_RESULT(0);97#endif98return 0;99}100CreateThread();101emscripten_async_call(PollThreadExit, 0, 100);102return 0;103}104105106