summaryrefslogtreecommitdiffstats
path: root/tools/guest-diag/sdl_cap.c
blob: 23c83abf8c34fbc10d366dae7c2af3f262bad244 (plain)
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
// SPDX-License-Identifier: BSD-3-Clause
// sdl_cap.c - LD_PRELOAD capture shim: records every frame GZDoom presents
// through SDL into an append-only file, so the host can harvest the guest's
// real rendered frames without any X/driver readback path.
//
// Hooks:
//   SDL_RenderPresent(SDL_Renderer*)      -> SDL_RenderReadPixels(ARGB8888)
//   SDL_UpdateWindowSurface(SDL_Window*)  -> SDL_GetWindowSurface pixels
// Both forward to the real SDL after capturing.
//
// File layout (little endian):
//   [0]   8B  magic "GZCAPV01"  (+ 4088 B reserved zero)
//   [4096] frame records:
//         4B "GZF2" | u32 w | u32 h | u32 stride | u32 bpp
//         u64 ts_ns (CLOCK_MONOTONIC) | u64 seq | stride*h bytes (top-down)
//   A trailing record with seq = 0xFFFFFFFFFFFFFFFF marks clean stop.
//
// Env: GZCAP (path, default /dev/shm/gzframes.bin), GZCAP_MAXF (0=unlimited)
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#define ARGB8888 0x16362004u

typedef struct SDL_Renderer SDL_Renderer;
typedef struct SDL_Window SDL_Window;

typedef struct SDL_Surface {
    uint32_t flags;
    void *format;
    int w, h;
    int pitch;
    void *pixels;
    void *userdata;
    int locked;
    void *list_blitmap;
    struct { int x, y, w, h; } clip_rect;
    void *map;
    int refcount;
} SDL_Surface;

static int (*real_RenderReadPixels)(SDL_Renderer *, const void *, uint32_t,
                                    void *, int);
static int (*real_RenderPresent)(SDL_Renderer *);
static SDL_Surface *(*real_GetWindowSurface)(SDL_Window *);
static int (*real_UpdateWindowSurface)(SDL_Window *);
static int (*real_GetRendererOutputSize)(SDL_Renderer *, int *, int *);
static void (*real_GL_SwapWindow)(SDL_Window *);
static int (*real_GL_GetDrawableSize)(SDL_Window *, int *, int *);
static void (*real_glReadPixels)(int, int, int, int, unsigned, unsigned, void *);

static FILE *cap_fp;
static unsigned long long cap_count, cap_max = 0;
static int cap_stop;

__attribute__((constructor)) static void cap_init(void)
{
    const char *p = getenv("GZCAP");
    const char *m = getenv("GZCAP_MAXF");
    char hdr[4096];

    if (m)
        cap_max = strtoull(m, NULL, 0);
    if (!p)
        p = "/dev/shm/gzframes.bin";
    real_RenderReadPixels = dlsym(RTLD_NEXT, "SDL_RenderReadPixels");
    real_RenderPresent = dlsym(RTLD_NEXT, "SDL_RenderPresent");
    real_GetWindowSurface = dlsym(RTLD_NEXT, "SDL_GetWindowSurface");
    real_UpdateWindowSurface = dlsym(RTLD_NEXT, "SDL_UpdateWindowSurface");
    real_GetRendererOutputSize =
        dlsym(RTLD_NEXT, "SDL_GetRendererOutputSize");
    real_GL_SwapWindow = dlsym(RTLD_NEXT, "SDL_GL_SwapWindow");
    real_GL_GetDrawableSize = dlsym(RTLD_NEXT, "SDL_GL_GetDrawableSize");
    real_glReadPixels = dlsym(RTLD_NEXT, "glReadPixels");
    if (!real_glReadPixels)
        real_glReadPixels = dlsym(RTLD_DEFAULT, "glReadPixels");
    if (!real_glReadPixels) {
        void *h = dlopen("libGL.so.1", RTLD_NOW | RTLD_GLOBAL);

        if (!h)
            h = dlopen("libGLESv2.so.2", RTLD_NOW | RTLD_GLOBAL);
        if (h)
            real_glReadPixels = dlsym(h, "glReadPixels");
    }
    fprintf(stderr, "[gzcap] init path=%s present=%p updwin=%p swap=%p read=%p gros=%p\n",
            p, (void *)real_RenderPresent, (void *)real_UpdateWindowSurface,
            (void *)real_GL_SwapWindow, (void *)real_RenderReadPixels,
            (void *)real_GetRendererOutputSize, (void *)real_glReadPixels);
    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);
}

static void cap_record(const void *px, int w, int h, int stride, int bpp)
{
    struct timespec ts;
    uint64_t tns, seq;
    uint32_t rh[4];
    char m[4] = { 'G', 'Z', 'F', '2' };

    if (!cap_fp || cap_stop)
        return;
    if (cap_max && cap_count >= cap_max) {
        uint64_t endseq = 0xFFFFFFFFFFFFFFFFull;

        fwrite(m, 1, 4, cap_fp);
        fwrite(&endseq, 8, 1, cap_fp);
        fflush(cap_fp);
        cap_stop = 1;
        return;
    }
    clock_gettime(CLOCK_MONOTONIC, &ts);
    tns = (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
    seq = cap_count;
    rh[0] = (uint32_t)w;
    rh[1] = (uint32_t)h;
    rh[2] = (uint32_t)stride;
    rh[3] = (uint32_t)bpp;
    fwrite(m, 1, 4, cap_fp);
    fwrite(rh, 4, 4, cap_fp);
    fwrite(&tns, 8, 1, cap_fp);
    fwrite(&seq, 8, 1, cap_fp);
    fwrite(px, 1, (size_t)stride * h, cap_fp);
    fflush(cap_fp);
    cap_count++;
    if ((cap_count & 31) == 0)
        fprintf(stderr, "[gzcap] frames=%llu\n", cap_count);
}

int SDL_RenderPresent(SDL_Renderer *r)
{
    static int hit;

    if (!hit++)
        fprintf(stderr, "[gzcap] hook RenderPresent\n");
    if (cap_fp && !cap_stop && real_RenderReadPixels &&
        real_GetRendererOutputSize) {
        int w = 0, h = 0;

        if (real_GetRendererOutputSize(r, &w, &h) == 0 && w > 0 && h > 0 &&
            w <= 4096 && h <= 4096) {
            static unsigned char *buf;
            static size_t bufsz;
            size_t need = (size_t)w * 4 * h;

            if (bufsz < need) {
                free(buf);
                buf = malloc(need);
                bufsz = buf ? need : 0;
            }
            if (buf && real_RenderReadPixels(r, NULL, ARGB8888, buf,
                                             w * 4) == 0)
                cap_record(buf, w, h, w * 4, 32);
        }
    }
    if (real_RenderPresent)
        return real_RenderPresent(r);
    return 0;
}

int SDL_UpdateWindowSurface(SDL_Window *win)
{
    static int hit;

    if (!hit++)
        fprintf(stderr, "[gzcap] hook UpdateWindowSurface\n");
    if (cap_fp && !cap_stop && real_GetWindowSurface) {
        SDL_Surface *s = real_GetWindowSurface(win);

        if (s && s->pixels && s->w > 0 && s->h > 0 && s->pitch > 0 &&
            s->w <= 4096 && s->h <= 4096)
            cap_record(s->pixels, s->w, s->h, s->pitch, 32);
    }
    if (real_UpdateWindowSurface)
        return real_UpdateWindowSurface(win);
    return 0;
}

void SDL_GL_SwapWindow(SDL_Window *win)
{
    static int hit;
    static unsigned char *gbuf;
    static size_t gbufsz;

    if (!hit++)
        fprintf(stderr, "[gzcap] hook GLSwapWindow\n");
    if (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) {
            size_t need = (size_t)w * 4 * h;

            if (gbufsz < need) {
                free(gbuf);
                gbuf = malloc(need);
                gbufsz = gbuf ? need : 0;
            }
            if (gbuf) {
                real_glReadPixels(0, 0, w, h, 0x1908, 0x1401, gbuf);
                /* GL rows are bottom-up: flip into a scratch record */
                {
                    unsigned char *tmp = malloc(need);
                    int y;

                    if (tmp) {
                        for (y = 0; y < h; y++)
                            memcpy(tmp + (size_t)y * w * 4,
                                   gbuf + (size_t)(h - 1 - y) * w * 4,
                                   (size_t)w * 4);
                        cap_record(tmp, w, h, w * 4, 32);
                        free(tmp);
                    }
                }
            }
        }
    }
    if (real_GL_SwapWindow)
        real_GL_SwapWindow(win);
}