1 /*
2  * Copyright 2019 The Emscripten Authors.  All rights reserved.
3  * Emscripten is available under two separate licenses, the MIT license and the
4  * University of Illinois/NCSA Open Source License.  Both these licenses can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef EXPLICIT_SWAP
9   #error EXPLICIT_SWAP is required.
10 #endif
11 
12 #include <stdlib.h>
13 #include <emscripten/emscripten.h>
14 #include <emscripten/html5.h>
15 #include <GLES2/gl2.h>
16 
17 #ifdef REPORT_RESULT
18 #define DIE() do { REPORT_RESULT(1); exit(1); } while (0)
19 #else
20 #define DIE() do { exit(1); } while (0)
21 #endif
22 
main()23 int main()
24 {
25   EmscriptenWebGLContextAttributes attr;
26   emscripten_webgl_init_context_attributes(&attr);
27   attr.explicitSwapControl = 1;
28 #if TEST_WEBGL2
29   attr.majorVersion = 2;
30 #endif
31 #if !TEST_ANTIALIAS
32   attr.antialias = 0;
33 #endif
34 
35   EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context("#canvas", &attr);
36   emscripten_webgl_make_context_current(ctx);
37 
38 #if !TEST_WEBGL2 && TEST_VAO
39   // This test cannot run without browser support for OES_vertex_array_object.
40   if (!emscripten_webgl_enable_extension(ctx, "OES_vertex_array_object")) {
41     DIE();
42   }
43 #endif
44 
45   glClearColor(0, 1, 0, 1);
46   glClear(GL_COLOR_BUFFER_BIT);
47 
48   glEnableVertexAttribArray(1);
49   emscripten_webgl_commit_frame();
50 
51   if (glGetError() != GL_NO_ERROR) {
52     DIE();
53   }
54 
55   // C doesn't have access to the "frontbuffer" (canvas contents).
56   // Escape via JavaScript to ensure the test passed.
57   int canvas_is_green = EM_ASM_INT({
58     GLctx.bindFramebuffer(GLctx.FRAMEBUFFER, null);
59     var pixels = new Uint8Array(4);
60     GLctx.readPixels(5, 5, 1, 1, GLctx.RGBA, GLctx.UNSIGNED_BYTE, pixels);
61     return pixels[0] == 0 && pixels[1] == 255 && pixels[2] == 0 && pixels[3] == 255;
62   });
63   if (!canvas_is_green) {
64     DIE();
65   }
66 
67 #ifdef REPORT_RESULT
68   REPORT_RESULT(0);
69 #endif
70 }
71