Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/gl_only_in_pthread.c
7085 views
1
// Copyright 2018 The Emscripten Authors. All rights reserved.
2
// Emscripten is available under two separate licenses, the MIT license and the
3
// University of Illinois/NCSA Open Source License. Both these licenses can be
4
// found in the LICENSE file.
5
6
#include <stdio.h>
7
#include <string.h>
8
#include <pthread.h>
9
#include <GLES2/gl2.h>
10
#include <math.h>
11
#include <assert.h>
12
#include <emscripten/emscripten.h>
13
#include <emscripten/html5.h>
14
#include <emscripten/threading.h>
15
#include <bits/errno.h>
16
#include <stdlib.h>
17
18
pthread_t thread;
19
20
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx;
21
22
_Atomic int threadRunning = 0;
23
24
void *ThreadMain(void *arg)
25
{
26
printf("Thread started. You should see the WebGL canvas fade from black to red.\n");
27
EmscriptenWebGLContextAttributes attr;
28
emscripten_webgl_init_context_attributes(&attr);
29
attr.explicitSwapControl = true;
30
ctx = emscripten_webgl_create_context("#canvas", &attr);
31
emscripten_webgl_make_context_current(ctx);
32
33
double color = 0;
34
for(int i = 0; i < 100; ++i)
35
{
36
color += 0.01;
37
glClearColor(color, 0, 0, 1);
38
glClear(GL_COLOR_BUFFER_BIT);
39
EMSCRIPTEN_RESULT r = emscripten_webgl_commit_frame();
40
assert(r == EMSCRIPTEN_RESULT_SUCCESS);
41
42
double now = emscripten_get_now();
43
while(emscripten_get_now() - now < 16) /*no-op*/;
44
}
45
46
#if 0 // TODO: this doesn't work yet for some reason
47
unsigned char data[64];
48
glReadPixels(0, 0, 4, 4, GL_RGBA, GL_UNSIGNED_BYTE, data);
49
assert(glGetError() == 0);
50
printf("%d %d %d %d\n", data[0], data[1], data[2], data[3]);
51
#endif
52
53
emscripten_webgl_make_context_current(0);
54
emscripten_webgl_destroy_context(ctx);
55
threadRunning = 0;
56
printf("Thread quit\n");
57
pthread_exit(0);
58
}
59
60
void CreateThread()
61
{
62
pthread_attr_t attr;
63
pthread_attr_init(&attr);
64
emscripten_pthread_attr_settransferredcanvases(&attr, "#canvas");
65
int rc = pthread_create(&thread, &attr, ThreadMain, 0);
66
if (rc == ENOSYS)
67
{
68
printf("Test Skipped! OffscreenCanvas is not supported!\n");
69
#ifdef REPORT_RESULT
70
REPORT_RESULT(0); // for now, report a skip as success
71
#endif
72
exit(1);
73
}
74
pthread_attr_destroy(&attr);
75
threadRunning = 1;
76
}
77
78
void PollThreadExit(void *arg)
79
{
80
if (!threadRunning)
81
{
82
printf("Test finished.\n");
83
#ifdef REPORT_RESULT
84
REPORT_RESULT(0);
85
#endif
86
} else {
87
emscripten_async_call(PollThreadExit, 0, 100);
88
}
89
}
90
91
int main()
92
{
93
if (!emscripten_supports_offscreencanvas())
94
{
95
printf("Current browser does not support OffscreenCanvas. Skipping this test.\n");
96
#ifdef REPORT_RESULT
97
REPORT_RESULT(0);
98
#endif
99
return 0;
100
}
101
CreateThread();
102
emscripten_async_call(PollThreadExit, 0, 100);
103
return 0;
104
}
105
106