tools/client/sweep_browser_capture.c (1498B)
1 #include "gl_renderer.h" 2 #include <assert.h> 3 #include <stdatomic.h> 4 #include <stdlib.h> 5 #include <GLES3/gl3.h> 6 #include <emscripten.h> 7 8 static atomic_int capture_requested; 9 10 EMSCRIPTEN_KEEPALIVE void sweep_capture(void) 11 { 12 atomic_store(&capture_requested, 1); 13 } 14 15 void __real_gl_target_present(struct gl_target *, const int32_t *, int, int); 16 17 void __wrap_gl_target_present(struct gl_target *target, const int32_t rectangle[4], int width, int height) 18 { 19 static unsigned frames; 20 static double reported; 21 __real_gl_target_present(target, rectangle, width, height); 22 23 /* Report actual game presentations, independently of the browser UI timer. */ 24 ++frames; 25 double now = emscripten_get_now(); 26 if (now - reported >= 1000) { 27 double timestamp = EM_ASM_DOUBLE({ return Date.now(); }); 28 MAIN_THREAD_ASYNC_EM_ASM({ Module.sweepFrame($0, $1); }, frames, timestamp); 29 reported = now; 30 } 31 32 /* Read back only at the end of an arrival test, on the GL context's worker. */ 33 if (atomic_exchange(&capture_requested, 0)) { 34 unsigned char *pixels = malloc((size_t)width * height * 4); 35 assert(pixels); 36 glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); 37 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); 38 assert(glGetError() == GL_NO_ERROR); 39 MAIN_THREAD_EM_ASM({ 40 Module.sweepCapture(HEAPU8.slice($0, $0 + $1 * $2 * 4), $1, $2); 41 }, pixels, width, height); 42 free(pixels); 43 } 44 }