summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tools/guest-diag/sdl_cap.c92
1 files changed, 82 insertions, 10 deletions
diff --git a/tools/guest-diag/sdl_cap.c b/tools/guest-diag/sdl_cap.c
index 84aa35b..4c034f0 100644
--- a/tools/guest-diag/sdl_cap.c
+++ b/tools/guest-diag/sdl_cap.c
@@ -76,6 +76,7 @@ static void (*real_glDeleteSync)(void *);
static FILE *cap_fp;
static unsigned long long cap_count, cap_max = 0;
static int cap_stop;
+static unsigned cap_readbuf = 0x0404; /* GL_FRONT by default (single-buffered pbuffer) */
__attribute__((constructor)) static void cap_init(void)
{
@@ -87,6 +88,12 @@ __attribute__((constructor)) static void cap_init(void)
cap_max = strtoull(m, NULL, 0);
if (!p)
p = "/dev/shm/gzframes.bin";
+ {
+ const char *rb = getenv("GZCAP_READBUF");
+
+ if (rb && (rb[0] == 'b' || rb[0] == 'B'))
+ cap_readbuf = 0x0405; /* GL_BACK: double-buffered window (Xvfb) */
+ }
real_RenderReadPixels = dlsym(RTLD_NEXT, "SDL_RenderReadPixels");
real_RenderPresent = dlsym(RTLD_NEXT, "SDL_RenderPresent");
real_GetWindowSurface = dlsym(RTLD_NEXT, "SDL_GetWindowSurface");
@@ -139,14 +146,39 @@ __attribute__((constructor)) static void cap_init(void)
(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)
+ /*
+ * Only the renderer process may own the capture file. Every process
+ * that inherits LD_PRELOAD runs this constructor; a helper without SDL
+ * (gdb, a crash handler, pipewire) used to fall through the
+ * glReadPixels-only gate and truncate the capture with "wb" at exit
+ * (the 288-frame run ended as a 4108-byte file). Require an SDL
+ * presentation symbol before touching the path, and never truncate an
+ * existing non-empty capture: append instead.
+ */
+ if (!real_GL_SwapWindow && !real_RenderPresent && !real_UpdateWindowSurface)
return;
- memset(hdr, 0, sizeof(hdr));
- memcpy(hdr, "GZCAPV01", 8);
- fwrite(hdr, 1, sizeof(hdr), cap_fp);
+ cap_fp = fopen(p, "r+b");
+ if (cap_fp) {
+ long sz;
+
+ fseek(cap_fp, 0, SEEK_END);
+ sz = ftell(cap_fp);
+ if (sz < (long)sizeof(hdr)) {
+ rewind(cap_fp);
+ memset(hdr, 0, sizeof(hdr));
+ memcpy(hdr, "GZCAPV01", 8);
+ fwrite(hdr, 1, sizeof(hdr), cap_fp);
+ } else {
+ fseek(cap_fp, 0, SEEK_END);
+ }
+ } else {
+ 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);
}
@@ -343,6 +375,46 @@ void SDL_GL_SwapWindow(SDL_Window *win)
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);
+ {
+ static int force_sync = -1;
+
+ if (force_sync < 0)
+ force_sync = getenv("GZCAP_SYNC") != NULL;
+ if (force_sync && cap_fp && !cap_stop && real_glReadPixels &&
+ real_GL_GetDrawableSize) {
+ int w = 0, h = 0;
+
+ if (real_GL_GetDrawableSize(win, &w, &h) == 0 && w > 0 &&
+ h > 0 && w <= 4096 && h <= 4096) {
+ unsigned char *buf = malloc((size_t)w * 4 * h);
+ unsigned char *tmp = malloc((size_t)w * 4 * h);
+
+ if (buf && tmp) {
+ int y;
+
+ if (real_glBindFramebuffer)
+ real_glBindFramebuffer(0x8CA8, 0);
+ if (real_glReadBuffer)
+ real_glReadBuffer(cap_readbuf);
+ while (real_glGetError && real_glGetError() != 0)
+ ;
+ if (real_glFinish)
+ real_glFinish();
+ 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(buf);
+ free(tmp);
+ }
+ if (real_GL_SwapWindow)
+ real_GL_SwapWindow(win);
+ return;
+ }
+ }
if (cap_fp && !cap_stop && real_glReadPixels && real_GL_GetDrawableSize) {
int w = 0, h = 0;
@@ -358,14 +430,14 @@ void SDL_GL_SwapWindow(SDL_Window *win)
if (!cap_pbo) {
real_glGenBuffers(1, &cap_pbo);
- fprintf(stderr, "[gzcap] async capture armed %dx%d pbo=%u\n",
- w, h, cap_pbo);
+ fprintf(stderr, "[gzcap] async capture armed %dx%d pbo=%u readbuf=%#x\n",
+ w, h, cap_pbo, cap_readbuf);
}
if (cap_pbo) {
if (real_glBindFramebuffer)
real_glBindFramebuffer(0x8CA8, 0);
if (real_glReadBuffer)
- real_glReadBuffer(0x0404);
+ real_glReadBuffer(cap_readbuf);
while (real_glGetError && real_glGetError() != 0)
;
real_glBindBuffer(GL_PIXEL_PACK_BUFFER, cap_pbo);