// gz_view.c - display GZDoom's real-NVIDIA EGL frames in an X11 window. // Reads the /dev/shm ring published by gz_es.so and XPutImage's each new // frame, scaled 2x. This window is a CAPTURE SURFACE ONLY: the pixels are // produced by GZDoom through the guest's real NVIDIA GL driver (vGPU). // // build: gcc -O2 -o gz_view gz_view.c -lX11 // run: DISPLAY=:0 ./gz_view [/dev/shm/gzframes.bin] #include #include #include #include #include #include #include #include #include #include #define GZFRAME_MAGIC 0x31465a47u struct gzframe_hdr { unsigned int magic; unsigned int w; unsigned int h; unsigned int seq; unsigned int nonblack; unsigned int pad[3]; }; static unsigned int frame_seq(void *p) { struct gzframe_hdr *h = p; __sync_synchronize(); return h->seq; } int main(int argc, char **argv) { const char *path = argc > 1 ? argv[1] : "/dev/shm/gzframes.bin"; Display *d; Window w; GC gc; XImage *img; Pixmap pm; int scr, sw, sh, W = 1280, H = 960; int fd, mapw = 0, maph = 0, running = 1; void *p = MAP_FAILED; size_t maplen = 0; unsigned int last = 0; unsigned long frames = 0; long nonblack = -1; time_t t0 = time(NULL); char title[256]; d = XOpenDisplay(NULL); if (!d) { fprintf(stderr, "gz_view: cannot open display\n"); return 1; } scr = DefaultScreen(d); sw = DisplayWidth(d, scr); sh = DisplayHeight(d, scr); if (W > sw) W = sw; if (H > sh) H = sh; w = XCreateSimpleWindow(d, RootWindow(d, scr), 20, 40, W, H, 1, BlackPixel(d, scr), BlackPixel(d, scr)); snprintf(title, sizeof(title), "GZDoom -> real NVIDIA EGL (GPU frames) [capture surface]"); XStoreName(d, w, title); XSelectInput(d, w, ExposureMask | KeyPressMask | StructureNotifyMask); XMapWindow(d, w); gc = XCreateGC(d, w, 0, NULL); XSetForeground(d, gc, WhitePixel(d, scr)); img = XCreateImage(d, DefaultVisual(d, scr), DefaultDepth(d, scr), ZPixmap, 0, NULL, W, H, 32, 0); if (!img) { fprintf(stderr, "gz_view: XCreateImage failed\n"); return 1; } pm = XCreatePixmap(d, w, W, H, DefaultDepth(d, scr)); while (running) { while (XPending(d)) { XEvent e; XNextEvent(d, &e); if (e.type == KeyPress) { running = 0; } else if (e.type == ClientMessage) { running = 0; } else if (e.type == DestroyNotify) { running = 0; } } if (p == MAP_FAILED) { struct stat st; fd = open(path, O_RDONLY); if (fd >= 0 && fstat(fd, &st) == 0 && st.st_size > 32) { maplen = st.st_size; p = mmap(NULL, maplen, PROT_READ, MAP_SHARED, fd, 0); if (p != MAP_FAILED) { struct gzframe_hdr *h = p; mapw = h->w; maph = h->h; } } if (fd >= 0) close(fd); } if (p != MAP_FAILED) { struct gzframe_hdr *h = p; unsigned int s = frame_seq(p); if ((s & 1u) == 0 && s != last && h->magic == GZFRAME_MAGIC && mapw > 0 && maph > 0 && maplen >= 32 + (size_t)mapw * maph * 4) { unsigned char *px = (unsigned char *)(h + 1); unsigned int x, y, *dst = (unsigned int *)img->data; int stride = img->bytes_per_line / 4; for (y = 0; y < (unsigned int)maph; y++) { unsigned int yy0 = y * H / maph; unsigned int yy1 = (y + 1) * H / maph; for (x = 0; x < (unsigned int)mapw; x++) { unsigned char *sp = px + ((size_t)y * mapw + x) * 4; unsigned int v = (sp[0] << 16) | (sp[1] << 8) | sp[2]; unsigned int xx0 = x * W / mapw; unsigned int xx1 = (x + 1) * W / mapw; unsigned int x2, y2; for (y2 = yy0; y2 < yy1 && y2 < (unsigned int)H; y2++) for (x2 = xx0; x2 < xx1 && x2 < (unsigned int)W; x2++) dst[(size_t)y2 * stride + x2] = v; } } XPutImage(d, pm, gc, img, 0, 0, 0, 0, W, H); XCopyArea(d, pm, w, gc, 0, 0, W, H, 0, 0); XSetForeground(d, gc, 0x000000); XFillRectangle(d, w, gc, 0, 0, W, 28); XSetForeground(d, gc, 0x00ff00); XDrawString(d, w, gc, 8, 19, "GZDoom -> real NVIDIA EGL (GPU frames) | this window is a capture surface | /dev/shm/gzframes.bin", strlen("GZDoom -> real NVIDIA EGL (GPU frames) | this window is a capture surface | /dev/shm/gzframes.bin")); last = s; frames++; nonblack = (long)h->nonblack; } if (time(NULL) - t0 >= 2 || frames == 0) { char st[256]; int n = snprintf(st, sizeof(st), "frames=%lu nonblack=%ld", frames, nonblack); XSetForeground(d, gc, 0x000000); XFillRectangle(d, w, gc, 0, 28, 420, 22); XSetForeground(d, gc, 0x00ffff); XDrawString(d, w, gc, 8, 45, st, n); t0 = time(NULL); } } else { XSetForeground(d, gc, WhitePixel(d, scr)); XDrawString(d, w, gc, 20, 60, "waiting for /dev/shm/gzframes.bin (start GZDoom with GZ_EGL=1)", strlen("waiting for /dev/shm/gzframes.bin (start GZDoom with GZ_EGL=1)")); } XFlush(d); usleep(33000); } return 0; }