Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/gl_in_two_pthreads.c
7085 views
1
#include <stdio.h>
2
#include <string.h>
3
#include <pthread.h>
4
#include <GLES2/gl2.h>
5
#include <math.h>
6
#include <assert.h>
7
#include <emscripten/emscripten.h>
8
#include <emscripten/html5.h>
9
#include <emscripten/threading.h>
10
#include <bits/errno.h>
11
#include <stdlib.h>
12
13
// Pass #define TEST_OFFSCREENCANVAS when testing against OffscreenCanvas support
14
15
void fill(int color)
16
{
17
switch(color)
18
{
19
case 1: glClearColor(1, 0, 0, 1); printf("On thread %p: you should see a red canvas.\n", pthread_self()); break;
20
case 2: glClearColor(0, 1, 0, 1); printf("On thread %p: you should see a green canvas.\n", pthread_self()); break;
21
case 3: glClearColor(0, 0, 1, 1); printf("On thread %p: you should see a blue canvas.\n", pthread_self()); break;
22
}
23
glClear(GL_COLOR_BUFFER_BIT);
24
EMSCRIPTEN_RESULT r = emscripten_webgl_commit_frame();
25
assert(r == EMSCRIPTEN_RESULT_SUCCESS);
26
27
emscripten_thread_sleep(2000);
28
}
29
30
void *thread_main(void *param)
31
{
32
EmscriptenWebGLContextAttributes attr;
33
emscripten_webgl_init_context_attributes(&attr);
34
attr.explicitSwapControl = true;
35
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context("#canvas", &attr);
36
assert(ctx);
37
38
EMSCRIPTEN_RESULT r = emscripten_webgl_make_context_current(ctx);
39
assert(r == EMSCRIPTEN_RESULT_SUCCESS);
40
41
fill((int)(intptr_t)param);
42
43
r = emscripten_webgl_make_context_current(0);
44
assert(r == EMSCRIPTEN_RESULT_SUCCESS);
45
46
pthread_exit(0);
47
}
48
49
void run_thread(int param)
50
{
51
pthread_attr_t attr;
52
pthread_attr_init(&attr);
53
emscripten_pthread_attr_settransferredcanvases(&attr, "#canvas");
54
pthread_t thread;
55
int rc = pthread_create(&thread, &attr, thread_main, (void*)param);
56
assert(rc == 0);
57
rc = pthread_join(thread, NULL);
58
assert(rc == 0);
59
}
60
61
int main()
62
{
63
#ifdef TEST_OFFSCREENCANVAS
64
if (!emscripten_supports_offscreencanvas())
65
{
66
printf("Current browser does not support OffscreenCanvas. Skipping this test.\n");
67
#ifdef REPORT_RESULT
68
REPORT_RESULT(1);
69
#endif
70
return 0;
71
}
72
#endif
73
74
// Create a context
75
EmscriptenWebGLContextAttributes attr;
76
emscripten_webgl_init_context_attributes(&attr);
77
attr.explicitSwapControl = true;
78
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context("#canvas", &attr);
79
assert(ctx);
80
81
// Activate it
82
EMSCRIPTEN_RESULT r = emscripten_webgl_make_context_current(ctx);
83
assert(r == EMSCRIPTEN_RESULT_SUCCESS);
84
85
// Draw
86
fill(3);
87
88
// Release context for the threads to capture the canvas
89
r = emscripten_webgl_make_context_current(0);
90
assert(r == EMSCRIPTEN_RESULT_SUCCESS);
91
92
// TODO: Optionally, also delete the above context
93
// TODO: Test that threads share the context (main thread creates, pthread uses)
94
run_thread(1);
95
run_thread(2);
96
run_thread(3);
97
98
// Reactivate the context we got
99
r = emscripten_webgl_make_context_current(ctx);
100
assert(r == EMSCRIPTEN_RESULT_SUCCESS);
101
102
// And render with it again
103
fill(1);
104
105
#ifdef REPORT_RESULT
106
REPORT_RESULT(1);
107
#endif
108
}
109
110