// SPDX-License-Identifier: BSD-3-Clause // sdl_cap.c - LD_PRELOAD capture shim: records every frame GZDoom presents // through SDL into an append-only file, so the host can harvest the guest's // real rendered frames without any X/driver readback path. // // Hooks: // SDL_RenderPresent(SDL_Renderer*) -> SDL_RenderReadPixels(ARGB8888) // SDL_UpdateWindowSurface(SDL_Window*) -> SDL_GetWindowSurface pixels // Both forward to the real SDL after capturing. // // File layout (little endian): // [0] 8B magic "GZCAPV01" (+ 4088 B reserved zero) // [4096] frame records: // 4B "GZF2" | u32 w | u32 h | u32 stride | u32 fmt // u64 ts_ns (CLOCK_MONOTONIC) | u64 seq | stride*h bytes (top-down) // fmt is a pixel-order tag (not bits per pixel): 32 = RGBA8 from the GL // swap path (glReadPixels GL_RGBA), 33 = BGRA8 from SDL_RenderReadPixels // ARGB8888 or an X window surface. A trailing record with // seq = 0xFFFFFFFFFFFFFFFF marks clean stop. // // Env: GZCAP (path, default /dev/shm/gzframes.bin), GZCAP_MAXF (0=unlimited) #define _GNU_SOURCE #include #include #include #include #include #include #include #define ARGB8888 0x16362004u #define GZF2_FMT_RGBA 32 #define GZF2_FMT_BGRA 33 typedef struct SDL_Renderer SDL_Renderer; typedef struct SDL_Window SDL_Window; typedef struct SDL_Surface { uint32_t flags; void *format; int w, h; int pitch; void *pixels; void *userdata; int locked; void *list_blitmap; struct { int x, y, w, h; } clip_rect; void *map; int refcount; } SDL_Surface; static int (*real_RenderReadPixels)(SDL_Renderer *, const void *, uint32_t, void *, int); static int (*real_RenderPresent)(SDL_Renderer *); static SDL_Surface *(*real_GetWindowSurface)(SDL_Window *); static int (*real_UpdateWindowSurface)(SDL_Window *); static int (*real_GetRendererOutputSize)(SDL_Renderer *, int *, int *); static void (*real_GL_SwapWindow)(SDL_Window *); static int (*real_GL_GetDrawableSize)(SDL_Window *, int *, int *); static void (*real_glReadPixels)(int, int, int, int, unsigned, unsigned, void *); static void (*real_glFinish)(void); static void (*real_glFlush)(void); static void (*real_glBindFramebuffer)(unsigned, unsigned); static void (*real_glReadBuffer)(unsigned); static unsigned (*real_glGetError)(void); static void (*real_glGenBuffers)(int, unsigned *); static void (*real_glBindBuffer)(unsigned, unsigned); static void (*real_glBufferData)(unsigned, long, const void *, unsigned); static void *(*real_glMapBufferRange)(unsigned, long, long, unsigned); static unsigned char (*real_glUnmapBuffer)(unsigned); static void (*real_glDeleteBuffers)(int, const unsigned *); static void *(*real_glFenceSync)(unsigned, unsigned); static unsigned (*real_glClientWaitSync)(void *, unsigned, unsigned long long); static void (*real_glDeleteSync)(void *); static FILE *cap_fp; static unsigned long long cap_count, cap_max = 0; static int cap_stop; __attribute__((constructor)) static void cap_init(void) { const char *p = getenv("GZCAP"); const char *m = getenv("GZCAP_MAXF"); char hdr[4096]; if (m) cap_max = strtoull(m, NULL, 0); if (!p) p = "/dev/shm/gzframes.bin"; real_RenderReadPixels = dlsym(RTLD_NEXT, "SDL_RenderReadPixels"); real_RenderPresent = dlsym(RTLD_NEXT, "SDL_RenderPresent"); real_GetWindowSurface = dlsym(RTLD_NEXT, "SDL_GetWindowSurface"); real_UpdateWindowSurface = dlsym(RTLD_NEXT, "SDL_UpdateWindowSurface"); real_GetRendererOutputSize = dlsym(RTLD_NEXT, "SDL_GetRendererOutputSize"); real_GL_SwapWindow = dlsym(RTLD_NEXT, "SDL_GL_SwapWindow"); real_GL_GetDrawableSize = dlsym(RTLD_NEXT, "SDL_GL_GetDrawableSize"); real_glReadPixels = dlsym(RTLD_NEXT, "glReadPixels"); real_glFinish = dlsym(RTLD_NEXT, "glFinish"); real_glBindFramebuffer = dlsym(RTLD_NEXT, "glBindFramebuffer"); real_glReadBuffer = dlsym(RTLD_NEXT, "glReadBuffer"); real_glGetError = dlsym(RTLD_NEXT, "glGetError"); real_glFlush = dlsym(RTLD_NEXT, "glFlush"); real_glGenBuffers = dlsym(RTLD_NEXT, "glGenBuffers"); real_glBindBuffer = dlsym(RTLD_NEXT, "glBindBuffer"); real_glBufferData = dlsym(RTLD_NEXT, "glBufferData"); real_glMapBufferRange = dlsym(RTLD_NEXT, "glMapBufferRange"); real_glUnmapBuffer = dlsym(RTLD_NEXT, "glUnmapBuffer"); real_glDeleteBuffers = dlsym(RTLD_NEXT, "glDeleteBuffers"); real_glFenceSync = dlsym(RTLD_NEXT, "glFenceSync"); real_glClientWaitSync = dlsym(RTLD_NEXT, "glClientWaitSync"); real_glDeleteSync = dlsym(RTLD_NEXT, "glDeleteSync"); if (!real_glReadPixels) real_glReadPixels = dlsym(RTLD_DEFAULT, "glReadPixels"); if (!real_glReadPixels) { void *h = dlopen("libGL.so.1", RTLD_NOW | RTLD_GLOBAL); if (!h) h = dlopen("libGLESv2.so.2", RTLD_NOW | RTLD_GLOBAL); if (h) { real_glReadPixels = dlsym(h, "glReadPixels"); real_glFinish = dlsym(h, "glFinish"); real_glBindFramebuffer = dlsym(h, "glBindFramebuffer"); real_glReadBuffer = dlsym(h, "glReadBuffer"); real_glGetError = dlsym(h, "glGetError"); real_glFlush = dlsym(h, "glFlush"); real_glGenBuffers = dlsym(h, "glGenBuffers"); real_glBindBuffer = dlsym(h, "glBindBuffer"); real_glBufferData = dlsym(h, "glBufferData"); real_glMapBufferRange = dlsym(h, "glMapBufferRange"); real_glUnmapBuffer = dlsym(h, "glUnmapBuffer"); real_glDeleteBuffers = dlsym(h, "glDeleteBuffers"); real_glFenceSync = dlsym(h, "glFenceSync"); real_glClientWaitSync = dlsym(h, "glClientWaitSync"); real_glDeleteSync = dlsym(h, "glDeleteSync"); } } fprintf(stderr, "[gzcap] init pid=%d path=%s present=%p updwin=%p swap=%p read=%p gros=%p readpix=%p\n", (int)getpid(), p, (void *)real_RenderPresent, (void *)real_UpdateWindowSurface, (void *)real_GL_SwapWindow, (void *)real_RenderReadPixels, (void *)real_GetRendererOutputSize, (void *)real_glReadPixels); if (!real_glReadPixels && !real_RenderReadPixels) return; cap_fp = fopen(p, "wb"); if (!cap_fp) return; memset(hdr, 0, sizeof(hdr)); memcpy(hdr, "GZCAPV01", 8); fwrite(hdr, 1, sizeof(hdr), cap_fp); fflush(cap_fp); } __attribute__((destructor)) static void cap_fini(void) { uint64_t endseq = 0xFFFFFFFFFFFFFFFFull; if (cap_fp && !cap_stop) { char m[4] = { 'G', 'Z', 'F', '2' }; fwrite(m, 1, 4, cap_fp); fwrite(&endseq, 8, 1, cap_fp); fflush(cap_fp); cap_stop = 1; fprintf(stderr, "[gzcap] final frames=%llu (clean sentinel)\n", cap_count); } if (cap_fp) { fclose(cap_fp); cap_fp = NULL; } } static void cap_record(const void *px, int w, int h, int stride, int fmt) { struct timespec ts; uint64_t tns, seq; uint32_t rh[4]; char m[4] = { 'G', 'Z', 'F', '2' }; if (!cap_fp || cap_stop) return; if (cap_max && cap_count >= cap_max) { uint64_t endseq = 0xFFFFFFFFFFFFFFFFull; fwrite(m, 1, 4, cap_fp); fwrite(&endseq, 8, 1, cap_fp); fflush(cap_fp); cap_stop = 1; return; } clock_gettime(CLOCK_MONOTONIC, &ts); tns = (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; seq = cap_count; rh[0] = (uint32_t)w; rh[1] = (uint32_t)h; rh[2] = (uint32_t)stride; rh[3] = (uint32_t)fmt; fwrite(m, 1, 4, cap_fp); fwrite(rh, 4, 4, cap_fp); fwrite(&tns, 8, 1, cap_fp); fwrite(&seq, 8, 1, cap_fp); fwrite(px, 1, (size_t)stride * h, cap_fp); fflush(cap_fp); cap_count++; if ((cap_count & 31) == 0) fprintf(stderr, "[gzcap] frames=%llu\n", cap_count); } int SDL_RenderPresent(SDL_Renderer *r) { static int hit; if (!hit++) fprintf(stderr, "[gzcap] hook RenderPresent\n"); if (cap_fp && !cap_stop && real_RenderReadPixels && real_GetRendererOutputSize) { int w = 0, h = 0; if (real_GetRendererOutputSize(r, &w, &h) == 0 && w > 0 && h > 0 && w <= 4096 && h <= 4096) { static unsigned char *buf; static size_t bufsz; size_t need = (size_t)w * 4 * h; if (bufsz < need) { free(buf); buf = malloc(need); bufsz = buf ? need : 0; } if (buf && real_RenderReadPixels(r, NULL, ARGB8888, buf, w * 4) == 0) cap_record(buf, w, h, w * 4, GZF2_FMT_BGRA); } } if (real_RenderPresent) return real_RenderPresent(r); return 0; } int SDL_UpdateWindowSurface(SDL_Window *win) { static int hit; if (!hit++) fprintf(stderr, "[gzcap] hook UpdateWindowSurface\n"); if (cap_fp && !cap_stop && real_GetWindowSurface) { SDL_Surface *s = real_GetWindowSurface(win); if (s && s->pixels && s->w > 0 && s->h > 0 && s->pitch > 0 && s->w <= 4096 && s->h <= 4096) cap_record(s->pixels, s->w, s->h, s->pitch, GZF2_FMT_BGRA); } if (real_UpdateWindowSurface) return real_UpdateWindowSurface(win); return 0; } /* * 2026-11-16 (c899): the synchronous glFinish + glReadPixels path is what * wedged GZDoom: the emulated GPU's work does not retire, the guest process * sits in an uninterruptible nv ioctl and even SIGKILL cannot reap it, so the * mission bash never finishes. Capture is now fully asynchronous: each swap * queues a glReadPixels into a PBO and a fence; the NEXT swap polls the fence * with a zero timeout and only maps/records when it has signalled. A fence * that never signals is abandoned after CAP_FENCE_MS and the PBO is retired * (GL defers deletion until pending work completes), so GZDoom's render loop * keeps running and frames are captured as soon as the host GPU path starts * retiring work. */ static unsigned cap_pbo; static void *cap_sync; static int cap_pw, cap_ph; static unsigned long long cap_sync_ns; #define CAP_FENCE_MS 5000 #define GL_PIXEL_PACK_BUFFER 0x88EB #define GL_PIXEL_PACK_BUFFER_BINDING 0x88EF #define GL_STREAM_READ 0x88E1 #define GL_MAP_READ_BIT 0x0001 #define GL_SYNC_GPU_COMMANDS_COMPLETE 0x9117 #define GL_ALREADY_SIGNALED 0x911A #define GL_CONDITION_SATISFIED 0x911C #define GL_SYNC_FLUSH_COMMANDS_BIT 0x0001 static unsigned long long now_ms(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (unsigned long long)ts.tv_sec * 1000ull + ts.tv_nsec / 1000000ull; } static void cap_try_harvest(void) { unsigned st; int prev = 0; void *p; if (!cap_sync) return; st = real_glClientWaitSync(cap_sync, 0, 0); if (st == GL_ALREADY_SIGNALED || st == GL_CONDITION_SATISFIED) { real_glBindBuffer(GL_PIXEL_PACK_BUFFER, cap_pbo); p = real_glMapBufferRange(GL_PIXEL_PACK_BUFFER, 0, (long)cap_pw * 4 * cap_ph, GL_MAP_READ_BIT); if (p) { unsigned char *tmp = malloc((size_t)cap_pw * 4 * cap_ph); if (tmp) { int y; for (y = 0; y < cap_ph; y++) memcpy(tmp + (size_t)y * cap_pw * 4, (unsigned char *)p + (size_t)(cap_ph - 1 - y) * cap_pw * 4, (size_t)cap_pw * 4); cap_record(tmp, cap_pw, cap_ph, cap_pw * 4, GZF2_FMT_RGBA); free(tmp); } real_glUnmapBuffer(GL_PIXEL_PACK_BUFFER); } real_glBindBuffer(GL_PIXEL_PACK_BUFFER, prev); real_glDeleteSync(cap_sync); cap_sync = NULL; } else if (now_ms() - cap_sync_ns > CAP_FENCE_MS) { static int given; real_glDeleteSync(cap_sync); cap_sync = NULL; if (!given++) fprintf(stderr, "[gzcap] fence abandoned after %d ms (GPU work not retiring); capture continues asynchronously\n", CAP_FENCE_MS); } } void SDL_GL_SwapWindow(SDL_Window *win) { static int hit; static unsigned long long hook_ms; if (!hit++) fprintf(stderr, "[gzcap] hook GLSwapWindow pid=%d fp=%p read=%p gros=%p pbo=%d\n", (int)getpid(), (void *)cap_fp, (void *)real_glReadPixels, (void *)real_GL_GetDrawableSize, real_glGenBuffers != NULL); if (cap_fp && !cap_stop && real_glReadPixels && real_GL_GetDrawableSize) { int w = 0, h = 0; cap_try_harvest(); if (!cap_sync && real_GL_GetDrawableSize(win, &w, &h) == 0 && w > 0 && h > 0 && w <= 4096 && h <= 4096) { if (!hook_ms) hook_ms = now_ms(); if (real_glGenBuffers && real_glBindBuffer && real_glBufferData && real_glFenceSync && real_glClientWaitSync && real_glMapBufferRange) { int prev = 0; if (!cap_pbo) { real_glGenBuffers(1, &cap_pbo); fprintf(stderr, "[gzcap] async capture armed %dx%d pbo=%u\n", w, h, cap_pbo); } if (cap_pbo) { if (real_glBindFramebuffer) real_glBindFramebuffer(0x8CA8, 0); if (real_glReadBuffer) real_glReadBuffer(0x0404); while (real_glGetError && real_glGetError() != 0) ; real_glBindBuffer(GL_PIXEL_PACK_BUFFER, cap_pbo); real_glBufferData(GL_PIXEL_PACK_BUFFER, (long)w * 4 * h, NULL, GL_STREAM_READ); real_glReadPixels(0, 0, w, h, 0x1908, 0x1401, NULL); cap_pw = w; cap_ph = h; cap_sync = real_glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, GL_SYNC_FLUSH_COMMANDS_BIT); cap_sync_ns = now_ms(); if (!cap_sync) { if (real_glDeleteBuffers) { unsigned b = cap_pbo; real_glDeleteBuffers(1, &b); } cap_pbo = 0; } } real_glBindBuffer(GL_PIXEL_PACK_BUFFER, prev); if (real_glFlush) real_glFlush(); } else if (real_glReadPixels) { /* No PBO support: keep a bounded synchronous fallback. */ unsigned char *buf = malloc((size_t)w * 4 * h); if (buf) { unsigned char *tmp = malloc((size_t)w * 4 * h); int y; if (tmp) { real_glReadPixels(0, 0, w, h, 0x1908, 0x1401, buf); for (y = 0; y < h; y++) memcpy(tmp + (size_t)y * w * 4, buf + (size_t)(h - 1 - y) * w * 4, (size_t)w * 4); cap_record(tmp, w, h, w * 4, GZF2_FMT_RGBA); free(tmp); } free(buf); } } } } if (real_glFlush && !real_GL_SwapWindow) real_glFlush(); if (real_GL_SwapWindow) real_GL_SwapWindow(win); }