|
| 1 | +/* |
| 2 | + * Copyright 2025 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 | +#include <stdio.h> |
| 9 | +#include <assert.h> |
| 10 | +#include <stdint.h> |
| 11 | +#include <emscripten/emscripten.h> |
| 12 | +#include <GL/glut.h> |
| 13 | + |
| 14 | +typedef struct { |
| 15 | + int32_t width; |
| 16 | + int32_t height; |
| 17 | +} rect_size_t; |
| 18 | + |
| 19 | +static rect_size_t browser_window_size = { 0, 0 }; |
| 20 | +static rect_size_t glut_init_size = { 0, 0 }; |
| 21 | +static rect_size_t glut_reshape_size = { 0, 0 }; |
| 22 | +static rect_size_t target_size = { 0, 0 }; |
| 23 | + |
| 24 | +/* |
| 25 | + * Set run_async_verification to 0 for sync test cases, and 1 for async tests. |
| 26 | + * |
| 27 | + * Callback sequence for test case 1 & 2 (synchronous): |
| 28 | + * glutMainLoop -> GLUT.onSize -> Browser.setCanvasSize -> updateResizeListeners -> GLUT.reshapeFunc |
| 29 | + * glutResizeWindow -> Browser.setCanvasSize -> updateResizeListeners -> GLUT.reshapeFunc |
| 30 | + * |
| 31 | + * Callback sequence for test cases 3-5 (async): |
| 32 | + * window resize -> async update -> GLUT.onSize -> Browser.setCanvasSize -> updateResizeListeners -> GLUT.reshapeFunc |
| 33 | + * |
| 34 | + * Because window resize does not immediately call GLUT.onSize, we wait to run verification of a test until we get |
| 35 | + * confirmation in GLUT.reshapeFunc. And after verification is done, we move on to the next test. |
| 36 | + * |
| 37 | + */ |
| 38 | +static int run_async_verification = 0; |
| 39 | + |
| 40 | +void print_size_test(int test_num, const char* name, rect_size_t rect_size) { |
| 41 | + printf("Test %d: %s = %d x %d\n", test_num, name, rect_size.width, rect_size.height); |
| 42 | +} |
| 43 | + |
| 44 | +int equal_size(rect_size_t rect_1, rect_size_t rect_2) { |
| 45 | + return (rect_1.width == rect_2.width && rect_1.height == rect_2.height); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Obtain various dimensions |
| 50 | + */ |
| 51 | +EM_JS(void, get_browser_window_size, (int32_t* width, int32_t* height), { |
| 52 | + setValue(Number(width), window.innerWidth, 'i32'); |
| 53 | + setValue(Number(height), window.innerHeight, 'i32'); |
| 54 | +}); |
| 55 | + |
| 56 | +EM_JS(void, get_canvas_client_size, (int32_t* width, int32_t* height), { |
| 57 | + const canvas = Module.canvas; |
| 58 | + setValue(Number(width), canvas.clientWidth, 'i32'); |
| 59 | + setValue(Number(height), canvas.clientHeight, 'i32'); |
| 60 | +}); |
| 61 | + |
| 62 | +EM_JS(void, get_canvas_size, (int32_t* width, int32_t* height), { |
| 63 | + const canvas = Module.canvas; |
| 64 | + setValue(Number(width), canvas.width, 'i32'); |
| 65 | + setValue(Number(height), canvas.height, 'i32'); |
| 66 | +}); |
| 67 | + |
| 68 | +/** |
| 69 | + * Update canvas style with given width and height, then invoke window resize event |
| 70 | + */ |
| 71 | +EM_JS(void, test_resize_with_CSS, (const char* position, const char* width, const char* height), { |
| 72 | + const canvas = Module.canvas; |
| 73 | + canvas.style.position = UTF8ToString(Number(position)); |
| 74 | + canvas.style.width = UTF8ToString(Number(width)); |
| 75 | + canvas.style.height = UTF8ToString(Number(height)); |
| 76 | + |
| 77 | + window.dispatchEvent(new UIEvent('resize')); |
| 78 | +}); |
| 79 | + |
| 80 | +/** |
| 81 | + * Verify canvas and reshape callback match target size, and also that |
| 82 | + * canvas width, height matches canvas clientWidth, clientHeight |
| 83 | + */ |
| 84 | +void assert_canvas_and_target_sizes_equal() { |
| 85 | + /* verify target size match */ |
| 86 | + rect_size_t canvas_size; |
| 87 | + get_canvas_size(&canvas_size.width, &canvas_size.height); |
| 88 | + assert(equal_size(canvas_size, target_size)); |
| 89 | + assert(equal_size(glut_reshape_size, target_size)); |
| 90 | + |
| 91 | + /* verify canvas client size match */ |
| 92 | + rect_size_t canvas_client_size; |
| 93 | + get_canvas_client_size(&canvas_client_size.width, &canvas_client_size.height); |
| 94 | + assert(equal_size(canvas_size, canvas_client_size)); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Verify the result of the previous test and then run the next one |
| 99 | + */ |
| 100 | +void verify_test_and_run_next() { |
| 101 | + void run_next_test(); |
| 102 | + |
| 103 | + assert_canvas_and_target_sizes_equal(); |
| 104 | + run_next_test(); |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Resizing tests |
| 109 | + */ |
| 110 | +void run_next_test() { |
| 111 | + static int test_num = 0; |
| 112 | + ++test_num; |
| 113 | + |
| 114 | + switch(test_num) { |
| 115 | + case 1: { |
| 116 | + /* startup */ |
| 117 | + target_size = glut_init_size; |
| 118 | + print_size_test(test_num, "startup, no CSS: canvas == glutReshapeFunc == glutInitWindow size", target_size); |
| 119 | + verify_test_and_run_next(); |
| 120 | + break; |
| 121 | + } |
| 122 | + case 2: { |
| 123 | + /* glutReshapeWindow */ |
| 124 | + target_size.width = glut_init_size.width + 40; |
| 125 | + target_size.height = glut_init_size.height + 20; |
| 126 | + print_size_test(test_num, "glut reshape, no CSS: canvas == glutReshapeFunc == glutReshapeWindow size", target_size); |
| 127 | + glutReshapeWindow(target_size.width, target_size.height); |
| 128 | + verify_test_and_run_next(); |
| 129 | + break; |
| 130 | + } |
| 131 | + case 3: { |
| 132 | + /* 100% scale CSS */ |
| 133 | + target_size = browser_window_size; |
| 134 | + print_size_test(test_num, "100% window scale CSS: canvas == glutReshapeFunc == browser window size", target_size); |
| 135 | + run_async_verification = 1; |
| 136 | + test_resize_with_CSS("fixed", "100%", "100%"); /* fixed, so canvas is driven by window size */ |
| 137 | + break; |
| 138 | + } |
| 139 | + case 4: { |
| 140 | + /* specific pixel size CSS */ |
| 141 | + target_size.width = glut_init_size.width - 20; |
| 142 | + target_size.height = glut_init_size.height + 40; |
| 143 | + print_size_test(test_num, "specific pixel size CSS: canvas == glutReshapeFunc == CSS specific size", target_size); |
| 144 | + char css_width[16], css_height[16]; |
| 145 | + snprintf (css_width, 16, "%dpx", target_size.width); |
| 146 | + snprintf (css_height, 16, "%dpx", target_size.height); |
| 147 | + run_async_verification = 1; |
| 148 | + test_resize_with_CSS("static", css_width, css_height); /* static, canvas is driven by CSS size */ |
| 149 | + break; |
| 150 | + } |
| 151 | + case 5: { |
| 152 | + /* mix of CSS scale and pixel size */ |
| 153 | + target_size.width = browser_window_size.width; |
| 154 | + target_size.height = 100; |
| 155 | + print_size_test(test_num, "100% width, 100px height CSS: canvas == glutReshapeFunc == CSS mixed size", target_size); |
| 156 | + run_async_verification = 1; |
| 157 | + test_resize_with_CSS("fixed", "100%", "100px"); /* fixed, canvas width is driven by window size */ |
| 158 | + break; |
| 159 | + } |
| 160 | + default: { |
| 161 | + /* all tests complete */ |
| 162 | + emscripten_force_exit(0); |
| 163 | + break; |
| 164 | + } |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +/** |
| 169 | + * Idle callback - start tests |
| 170 | + */ |
| 171 | +void start_tests() { |
| 172 | + glutIdleFunc(NULL); |
| 173 | + run_next_test(); |
| 174 | +} |
| 175 | + |
| 176 | +/** |
| 177 | + * Reshape callback - record latest size, verify and run next test if async |
| 178 | + */ |
| 179 | +void reshape(int w, int h) { |
| 180 | + glut_reshape_size.width = (int32_t)w; |
| 181 | + glut_reshape_size.height = (int32_t)h; |
| 182 | + |
| 183 | + if (run_async_verification) { |
| 184 | + run_async_verification = 0; /* Only one verification per test */ |
| 185 | + verify_test_and_run_next(); |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +int main(int argc, char *argv[]) { |
| 190 | + get_browser_window_size(&browser_window_size.width, &browser_window_size.height); |
| 191 | + |
| 192 | + /* Make glut initial canvas size be 1/2 of browser window */ |
| 193 | + glut_init_size.width = browser_window_size.width / 2; |
| 194 | + glut_init_size.height = browser_window_size.height / 2; |
| 195 | + |
| 196 | + glutInit(&argc, argv); |
| 197 | + glutInitWindowSize(glut_init_size.width, glut_init_size.height); |
| 198 | + glutInitDisplayMode(GLUT_RGB); |
| 199 | + glutCreateWindow("test_glut_resize.c"); |
| 200 | + |
| 201 | + /* Set up glut callback functions */ |
| 202 | + glutIdleFunc(start_tests); |
| 203 | + glutReshapeFunc(reshape); |
| 204 | + glutDisplayFunc(NULL); |
| 205 | + |
| 206 | + glutMainLoop(); |
| 207 | + return 0; |
| 208 | +} |
0 commit comments