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
|
// 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 <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 <X11/Xlib.h>
#include <X11/Xutil.h>
#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;
}
|