1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
// 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 <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <time.h>
#include <stdarg.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES2/gl2.h>
#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;
}
|