Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/gl_in_pthread.c
7085 views
1
// Copyright 2016 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
int numThreadsCreated = 0;
23
24
int result = 0;
25
26
void *ThreadMain(void *arg)
27
{
28
printf("ThreadMain\n");
29
switch(numThreadsCreated)
30
{
31
case 1: printf("Thread 1 started: you should see the WebGL canvas fade from black to red.\n"); break;
32
case 2: printf("Thread 2 started: you should see the WebGL canvas fade from black to green.\n"); break;
33
case 3: printf("Thread 3 started: you should see the WebGL canvas fade from black to blue.\n"); break;
34
}
35
EmscriptenWebGLContextAttributes attr;
36
emscripten_webgl_init_context_attributes(&attr);
37
attr.explicitSwapControl = true;
38
ctx = emscripten_webgl_create_context("#canvas", &attr);
39
emscripten_webgl_make_context_current(ctx);
40
41
double color = 0;
42
for(int i = 0; i < 100; ++i)
43
{
44
color += 0.01;
45
switch(numThreadsCreated)
46
{
47
case 1: glClearColor(color, 0, 0, 1); break;
48
case 2: glClearColor(0, color, 0, 1); break;
49
case 3: glClearColor(0, 0, color, 1); break;
50
}
51
glClear(GL_COLOR_BUFFER_BIT);
52
EMSCRIPTEN_RESULT r = emscripten_webgl_commit_frame();
53
assert(r == EMSCRIPTEN_RESULT_SUCCESS);
54
55
double now = emscripten_get_now();
56
while(emscripten_get_now() - now < 16) /*no-op*/;
57
}
58
59
emscripten_webgl_make_context_current(0);
60
emscripten_webgl_destroy_context(ctx);
61
printf("Thread quit\n");
62
pthread_exit(0);
63
}
64
65
void CreateThread()
66
{
67
pthread_attr_t attr;
68
pthread_attr_init(&attr);
69
emscripten_pthread_attr_settransferredcanvases(&attr, "#canvas");
70
int rc = pthread_create(&thread, &attr, ThreadMain, 0);
71
if (rc == ENOSYS)
72
{
73
printf("Test Skipped! OffscreenCanvas is not supported!\n");
74
#ifdef REPORT_RESULT
75
REPORT_RESULT(1); // But report success, so that runs on non-supporting browsers don't raise noisy errors.
76
#endif
77
exit(0);
78
}
79
if (rc)
80
{
81
printf("Failed to create thread! error: %d\n", rc);
82
exit(0);
83
}
84
pthread_attr_destroy(&attr);
85
++numThreadsCreated;
86
}
87
88
void *mymain(void* arg)
89
{
90
for(int i = 0; i < 3; ++i)
91
{
92
printf("Creating thread %d\n", i+1);
93
CreateThread();
94
printf("Waiting for thread to finish.\n");
95
pthread_join(thread, 0);
96
thread = 0;
97
}
98
printf("All done!\n");
99
return 0;
100
}
101
102
// Tests that the OffscreenCanvas context can travel from main thread -> thread 1 -> thread 2
103
// #define TEST_CHAINED_WEBGL_CONTEXT_PASSING
104
105
// If set, the OffscreenCanvas is transferred from the main thread directly to thread 2, without giving it to thread 1
106
// in between.
107
// #define TRANSFER_TO_CHAINED_THREAD_FROM_MAIN_THREAD
108
109
int main()
110
{
111
#ifdef TEST_CHAINED_WEBGL_CONTEXT_PASSING
112
pthread_attr_t attr;
113
pthread_attr_init(&attr);
114
#ifndef TRANSFER_TO_CHAINED_THREAD_FROM_MAIN_THREAD
115
emscripten_pthread_attr_settransferredcanvases(&attr, "#canvas");
116
#endif
117
int rc = pthread_create(&thread, &attr, mymain, 0);
118
if (rc == ENOSYS)
119
{
120
printf("Test Skipped! OffscreenCanvas is not supported!\n");
121
#ifdef REPORT_RESULT
122
REPORT_RESULT(1); // But report success, so that runs on non-supporting browsers don't raise noisy errors.
123
#endif
124
exit(0);
125
}
126
emscripten_exit_with_live_runtime();
127
#else
128
mymain(0);
129
#endif
130
return 0;
131
}
132
133