tools/client/sweep_capture.c (5379B)
1 #define _GNU_SOURCE 2 #include <assert.h> 3 #include <dlfcn.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <time.h> 7 #include <unistd.h> 8 #include <SDL2/SDL.h> 9 #include <SDL2/SDL_opengl.h> 10 11 static unsigned long frames, draws, frame_draws, most_draws; 12 static double reported; 13 static double input_started, input_times[4]; 14 static unsigned input_next = 4, input_window; 15 16 int SDL_PollEvent(SDL_Event *event) 17 { 18 static int (*poll_event)(SDL_Event *); 19 if (!poll_event) poll_event = dlsym(RTLD_NEXT, "SDL_PollEvent"); 20 if (!event || input_next == 4) return poll_event(event); 21 const double times[] = {1, 1.6, 1.8, 2.8}; 22 const SDL_Keycode keys[] = {SDLK_LEFT, SDLK_LEFT, SDLK_w, SDLK_w}; 23 struct timespec timestamp; 24 assert(!clock_gettime(CLOCK_MONOTONIC, ×tamp)); 25 double elapsed = timestamp.tv_sec + timestamp.tv_nsec / 1e9 - input_started; 26 27 /* Let arrival handling settle, then replay through the normal keyboard handler. */ 28 if (elapsed >= times[input_next]) { 29 memset(event, 0, sizeof(*event)); 30 event->type = input_next & 1 ? SDL_KEYUP : SDL_KEYDOWN; 31 event->key.timestamp = SDL_GetTicks(); 32 event->key.windowID = input_window; 33 event->key.state = input_next & 1 ? SDL_RELEASED : SDL_PRESSED; 34 event->key.keysym.sym = keys[input_next]; 35 event->key.keysym.scancode = SDL_GetScancodeFromKey(keys[input_next]); 36 input_times[input_next++] = elapsed; 37 fprintf(stderr, "[sweep-input] %.3f key=%d state=%d\n", 38 elapsed, event->key.keysym.sym, event->key.state); 39 if (input_next == 4) { 40 char path[1024], temporary[1024]; 41 snprintf(temporary, sizeof(temporary), "%s/input.tmp", getenv("AO_SWEEP_DIRECTORY")); 42 FILE *file = fopen(temporary, "w"); 43 assert(file); 44 fprintf(file, "{\"events\":4,\"times\":[%.6f,%.6f,%.6f,%.6f]}\n", 45 input_times[0], input_times[1], input_times[2], input_times[3]); 46 assert(!fclose(file)); 47 snprintf(path, sizeof(path), "%s/input.done", getenv("AO_SWEEP_DIRECTORY")); 48 assert(!rename(temporary, path)); 49 } 50 return 1; 51 } 52 return poll_event(event); 53 } 54 55 void glDrawElements(GLenum mode, GLsizei count, GLenum type, const void *indices) 56 { 57 static void (*draw)(GLenum, GLsizei, GLenum, const void *); 58 if (!draw) draw = dlsym(RTLD_NEXT, "glDrawElements"); 59 ++draws; 60 ++frame_draws; 61 draw(mode, count, type, indices); 62 } 63 64 void SDL_GL_SwapWindow(SDL_Window *window) 65 { 66 static void (*swap)(SDL_Window *); 67 if (!swap) swap = dlsym(RTLD_NEXT, "SDL_GL_SwapWindow"); 68 const char *directory = getenv("AO_SWEEP_DIRECTORY"); 69 char path[1024], temporary[1024]; 70 71 /* A per-arrival request starts one short turn followed by one forward step. */ 72 snprintf(path, sizeof(path), "%s/input.request", directory); 73 if (!access(path, F_OK)) { 74 assert(input_next == 4); 75 assert(!unlink(path)); 76 struct timespec timestamp; 77 assert(!clock_gettime(CLOCK_MONOTONIC, ×tamp)); 78 input_started = timestamp.tv_sec + timestamp.tv_nsec / 1e9; 79 input_next = 0; 80 input_window = SDL_GetWindowID(window); 81 } 82 83 /* Capture on the rendering thread before presenting its back buffer. */ 84 snprintf(path, sizeof(path), "%s/capture.request", directory); 85 if (!access(path, F_OK)) { 86 assert(!unlink(path)); 87 int width, height; 88 SDL_GL_GetDrawableSize(window, &width, &height); 89 unsigned char *pixels = malloc((size_t)width * height * 3); 90 assert(pixels); 91 GLint read_buffer, pack_alignment; 92 glGetIntegerv(GL_READ_BUFFER, &read_buffer); 93 glGetIntegerv(GL_PACK_ALIGNMENT, &pack_alignment); 94 glReadBuffer(GL_BACK); 95 glPixelStorei(GL_PACK_ALIGNMENT, 1); 96 glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels); 97 glReadBuffer(read_buffer); 98 glPixelStorei(GL_PACK_ALIGNMENT, pack_alignment); 99 snprintf(temporary, sizeof(temporary), "%s/capture.tmp", directory); 100 FILE *file = fopen(temporary, "wb"); 101 assert(file); 102 fprintf(file, "P6\n%d %d\n255\n", width, height); 103 for (int y = height - 1; y >= 0; --y) 104 assert(fwrite(pixels + (size_t)y * width * 3, 3, width, file) == (size_t)width); 105 assert(!fclose(file)); 106 free(pixels); 107 snprintf(path, sizeof(path), "%s/capture.ppm", directory); 108 assert(!rename(temporary, path)); 109 } 110 swap(window); 111 ++frames; 112 if (frame_draws > most_draws) most_draws = frame_draws; 113 frame_draws = 0; 114 115 /* Publish complete telemetry once a second without partial JSON reads. */ 116 struct timespec timestamp; 117 assert(!clock_gettime(CLOCK_MONOTONIC, ×tamp)); 118 double seconds = timestamp.tv_sec + timestamp.tv_nsec / 1e9; 119 if (seconds - reported >= 1) { 120 snprintf(temporary, sizeof(temporary), "%s/frame.tmp", directory); 121 FILE *file = fopen(temporary, "w"); 122 assert(file); 123 fprintf(file, "{\"time\":%.6f,\"frames\":%lu,\"draws\":%lu,\"max_draws\":%lu}\n", 124 seconds, frames, draws, most_draws); 125 assert(!fclose(file)); 126 snprintf(path, sizeof(path), "%s/frame.json", directory); 127 assert(!rename(temporary, path)); 128 reported = seconds; 129 most_draws = 0; 130 } 131 }