File: gl_in_two_pthreads.cpp

package info (click to toggle)
emscripten 3.1.6~dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 114,112 kB
  • sloc: ansic: 583,052; cpp: 391,943; javascript: 79,361; python: 54,180; sh: 49,997; pascal: 4,658; makefile: 3,426; asm: 2,191; lisp: 1,869; ruby: 488; cs: 142
file content (108 lines) | stat: -rw-r--r-- 2,998 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <GLES2/gl2.h>
#include <math.h>
#include <assert.h>
#include <emscripten/emscripten.h>
#include <emscripten/html5.h>
#include <emscripten/threading.h>
#include <bits/errno.h>
#include <stdlib.h>

// Pass #define TEST_OFFSCREENCANVAS when testing against OffscreenCanvas support

void fill(int color)
{
  switch(color)
  {
    case 1: glClearColor(1, 0, 0, 1); printf("On thread %ld: you should see a red canvas.\n", (long)pthread_self()); break;
    case 2: glClearColor(0, 1, 0, 1); printf("On thread %ld: you should see a green canvas.\n", (long)pthread_self()); break;
    case 3: glClearColor(0, 0, 1, 1); printf("On thread %ld: you should see a blue canvas.\n", (long)pthread_self()); break;
  }
  glClear(GL_COLOR_BUFFER_BIT);
  EMSCRIPTEN_RESULT r = emscripten_webgl_commit_frame();
  assert(r == EMSCRIPTEN_RESULT_SUCCESS);

  emscripten_thread_sleep(2000);
}

void *thread_main(void *param)
{
  EmscriptenWebGLContextAttributes attr;
  emscripten_webgl_init_context_attributes(&attr);
  attr.explicitSwapControl = EM_TRUE;
  EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context("#canvas", &attr);
  assert(ctx);

  EMSCRIPTEN_RESULT r = emscripten_webgl_make_context_current(ctx);
  assert(r == EMSCRIPTEN_RESULT_SUCCESS);

  fill((int)(intptr_t)param);

  r = emscripten_webgl_make_context_current(0);
  assert(r == EMSCRIPTEN_RESULT_SUCCESS);

  pthread_exit(0);
}

void run_thread(int param)
{
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  emscripten_pthread_attr_settransferredcanvases(&attr, "#canvas");
  pthread_t thread;
  int rc = pthread_create(&thread, &attr, thread_main, (void*)param);
  assert(rc == 0);
  rc = pthread_join(thread, NULL);
  assert(rc == 0);
}

int main()
{
#ifdef TEST_OFFSCREENCANVAS
  if (!emscripten_supports_offscreencanvas())
  {
    printf("Current browser does not support OffscreenCanvas. Skipping this test.\n");
#ifdef REPORT_RESULT
    REPORT_RESULT(1);
#endif
    return 0;
  }
#endif

  // Create a context
  EmscriptenWebGLContextAttributes attr;
  emscripten_webgl_init_context_attributes(&attr);
  attr.explicitSwapControl = EM_TRUE;
  EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context("#canvas", &attr);
  assert(ctx);

  // Activate it
  EMSCRIPTEN_RESULT r = emscripten_webgl_make_context_current(ctx);
  assert(r == EMSCRIPTEN_RESULT_SUCCESS);

  // Draw
  fill(3);

  // Release context for the threads to capture the canvas
  r = emscripten_webgl_make_context_current(0);
  assert(r == EMSCRIPTEN_RESULT_SUCCESS);

  // TODO: Optionally, also delete the above context
  // TODO: Test that threads share the context (main thread creates, pthread uses)
  run_thread(1);
  run_thread(2);
  run_thread(3);

  // Reactivate the context we got
  r = emscripten_webgl_make_context_current(ctx);
  assert(r == EMSCRIPTEN_RESULT_SUCCESS);

  // And render with it again
  fill(1);

#ifdef REPORT_RESULT
  REPORT_RESULT(1);
#endif
}