// gz_es.c - give SDL2's offscreen driver a real NVIDIA EGL context and // publish every presented frame to /dev/shm for a desktop viewer. // // The guest's vGPU 3D stream is replayed by vgsp; GZDoom renders through // the real NVIDIA EGL/GL driver (GL_RENDERER = NVIDIA GeForce RTX 2080 // SUPER, 4.6.0 NVIDIA 570.144). This shim creates the context on a // pbuffer surface (so the default framebuffer is real), and on each // SDL_GL_SwapWindow reads the frame back into /dev/shm/gzframes.bin. // gz_view then displays those frames in an X11 window on the guest // desktop - a capture surface, not a renderer. // // build: gcc -shared -fPIC -O2 -o gz_es.so gz_es.c -lEGL -lGLESv2 -ldl #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef GL_BACK #define GL_BACK 0x0405 #endif #ifndef GL_FRONT #define GL_FRONT 0x0404 #endif #ifndef GL_RGBA8 #define GL_RGBA8 0x8058 #endif #define GZFRAME_MAGIC 0x31465a47u /* "GZF1" */ struct gzframe_hdr { unsigned int magic; unsigned int w; unsigned int h; unsigned int seq; unsigned int nonblack; unsigned int pad[3]; }; static EGLDisplay dpy; static EGLContext ctx; static EGLSurface surf; static int fw = 640, fh = 480; static int (*real_make)(void *, void *); static struct gzframe_hdr *shm; static unsigned char *shm_px; static size_t shm_len; static unsigned int seq; static int readbuf = GL_BACK; static int tried_front; static int logfd = -1; static void gzlog(const char *fmt, ...) __attribute__((format(printf, 1, 2))); static void gzlog(const char *fmt, ...) { char buf[512]; va_list ap; int n; va_start(ap, fmt); n = vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); if (n < 0) return; if (n > (int)sizeof(buf) - 1) n = sizeof(buf) - 1; if (logfd >= 0) (void)write(logfd, buf, n); (void)write(2, buf, n); } static int env_int(const char *name, int def) { const char *v = getenv(name); return (v && *v) ? atoi(v) : def; } static void shm_open_frame(void) { const char *path = getenv("GZFRAME"); int fd; size_t need; void *p; if (!path || !*path) path = "/dev/shm/gzframes.bin"; fw = env_int("GZFRAME_W", fw); fh = env_int("GZFRAME_H", fh); if (fw <= 0 || fw > 4096 || fh <= 0 || fh > 4096) { fw = 640; fh = 480; } need = sizeof(struct gzframe_hdr) + (size_t)fw * fh * 4; fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666); if (fd < 0) return; if (ftruncate(fd, need) != 0) { close(fd); return; } p = mmap(NULL, need, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); close(fd); if (p == MAP_FAILED) return; shm = p; shm_px = (unsigned char *)(shm + 1); shm_len = need; memset(shm, 0, need); shm->magic = GZFRAME_MAGIC; shm->w = fw; shm->h = fh; gzlog("[gzes] frame shm %s w=%d h=%d len=%zu\n", path, fw, fh, need); } static void publish_frame(void) { unsigned int i, nb = 0; if (!shm || !shm_px) return; glFinish(); glReadPixels(0, 0, fw, fh, GL_RGBA, GL_UNSIGNED_BYTE, shm_px); for (i = 0; i < (unsigned int)fw * fh; i++) { const unsigned char *p = shm_px + i * 4; if (p[0] || p[1] || p[2]) nb++; } /* * A pbuffer's default read buffer can be front (single buffered) or * back. If the first frames are entirely black, retry the other * buffer once; the nonblack count is published either way. */ if (!nb && !tried_front && readbuf == GL_BACK) { tried_front = 1; readbuf = GL_FRONT; glReadPixels(0, 0, fw, fh, GL_RGBA, GL_UNSIGNED_BYTE, shm_px); for (i = 0; i < (unsigned int)fw * fh; i++) { const unsigned char *p = shm_px + i * 4; if (p[0] || p[1] || p[2]) nb++; } gzlog("[gzes] first frame black on GL_BACK; GL_FRONT nonblack=%u\n", nb); } seq += 2; shm->seq = seq; shm->nonblack = nb; __sync_synchronize(); shm->seq = seq + 1; } static int create_context(void) { PFNEGLGETPLATFORMDISPLAYEXTPROC gp = (PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress( "eglGetPlatformDisplayEXT"); PFNEGLQUERYDEVICESEXTPROC qd = (PFNEGLQUERYDEVICESEXTPROC)eglGetProcAddress( "eglQueryDevicesEXT"); EGLint maj, min, nc = 0; EGLConfig cfg; EGLint ca_gl[] = { EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_DEPTH_SIZE, 24, EGL_NONE }; EGLint ca_es[] = { EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_NONE }; EGLint at_gl[] = { EGL_CONTEXT_MAJOR_VERSION, 3, EGL_CONTEXT_MINOR_VERSION, 3, EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT, EGL_NONE }; EGLint at_es[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; EGLint pb[] = { EGL_WIDTH, fw, EGL_HEIGHT, fh, EGL_NONE }; void (*load)(const char *) = dlsym(RTLD_NEXT, "SDL_GL_LoadLibrary"); int desktop = 0; if (load) load(NULL); if (!gp || !qd) { gzlog("[gzes] no EGL platform ext\n"); return -1; } { EGLDeviceEXT devs[8]; EGLint n = 0; if (qd(8, devs, &n) && n > 0) dpy = gp(EGL_PLATFORM_DEVICE_EXT, devs[0], NULL); } if (dpy == EGL_NO_DISPLAY) dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (!eglInitialize(dpy, &maj, &min)) { gzlog("[gzes] eglInitialize 0x%x\n", eglGetError()); return -1; } if (eglBindAPI(EGL_OPENGL_API) && eglChooseConfig(dpy, ca_gl, &cfg, 1, &nc) && nc >= 1) { ctx = eglCreateContext(dpy, cfg, EGL_NO_CONTEXT, at_gl); if (ctx != EGL_NO_CONTEXT) { desktop = 1; surf = eglCreatePbufferSurface(dpy, cfg, pb); if (surf == EGL_NO_SURFACE) { gzlog("[gzes] pbuffer 0x%x (desktop)\n", eglGetError()); eglDestroyContext(dpy, ctx); ctx = EGL_NO_CONTEXT; } } } if (ctx == EGL_NO_CONTEXT) { EGLint sa = EGL_NONE; eglBindAPI(EGL_OPENGL_ES_API); if (!eglChooseConfig(dpy, ca_es, &cfg, 1, &nc) || nc < 1) { gzlog("[gzes] choose 0x%x\n", eglGetError()); return -1; } ctx = eglCreateContext(dpy, cfg, EGL_NO_CONTEXT, at_es); if (ctx == EGL_NO_CONTEXT) { gzlog("[gzes] ctx 0x%x\n", eglGetError()); return -1; } /* ES2 allows a pbuffer too; keep a surface so readback works. */ surf = eglCreatePbufferSurface(dpy, cfg, pb); (void)sa; } else { eglBindAPI(EGL_OPENGL_API); } if (surf == EGL_NO_SURFACE) { if (!eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx)) { gzlog("[gzes] current 0x%x\n", eglGetError()); return -1; } } else if (!eglMakeCurrent(dpy, surf, surf, ctx)) { gzlog("[gzes] current(surf) 0x%x\n", eglGetError()); return -1; } shm_open_frame(); gzlog("[gzes] OK api=%s renderer=%s version=%s surf=%d desktop=%d\n", eglQueryString(dpy, EGL_CLIENT_APIS) ?: "?", glGetString(GL_RENDERER) ? (const char *)glGetString(GL_RENDERER) : "?", glGetString(GL_VERSION) ? (const char *)glGetString(GL_VERSION) : "?", surf != EGL_NO_SURFACE, desktop); return 0; } void *SDL_GL_CreateContext(void *win) { (void)win; if (!logfd) logfd = open("/dev/shm/gzes.log", O_WRONLY | O_CREAT | O_APPEND, 0666); if (create_context()) return NULL; return (void *)ctx; } int SDL_GL_MakeCurrent(void *win, void *c) { if (!real_make) real_make = dlsym(RTLD_NEXT, "SDL_GL_MakeCurrent"); if (!dpy || !c) return real_make ? real_make(win, c) : -1; if (surf != EGL_NO_SURFACE) return eglMakeCurrent(dpy, surf, surf, (EGLContext)c) ? 0 : -1; return eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, (EGLContext)c) ? 0 : -1; } void SDL_GL_SwapWindow(void *win) { (void)win; publish_frame(); glFlush(); } void SDL_GL_GetDrawableSize(void *win, int *w, int *h) { (void)win; if (w) *w = fw; if (h) *h = fh; } void SDL_GL_GetWindowSize(void *win, int *w, int *h) { SDL_GL_GetDrawableSize(win, w, h); } void *SDL_GL_GetCurrentContext(void) { return (void *)ctx; } void SDL_GL_DeleteContext(void *c) { (void)c; }