summaryrefslogtreecommitdiffstats
path: root/tools/guest-diag/gz_es.c
blob: b371e4ffb0b3516d0b10273b443cc29d117623a6 (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
// gz_es.c - give SDL2's offscreen driver a real NVIDIA EGL/GLES context.
// no X, no GLX, no llvmpipe.  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 <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES2/gl2.h>

static EGLDisplay dpy;
static EGLContext ctx;
static int (*real_make)(void *, void *);

void *SDL_GL_CreateContext(void *win)
{
	PFNEGLGETPLATFORMDISPLAYEXTPROC gp =
		(PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress(
			"eglGetPlatformDisplayEXT");
	PFNEGLQUERYDEVICESEXTPROC qd =
		(PFNEGLQUERYDEVICESEXTPROC)eglGetProcAddress(
			"eglQueryDevicesEXT");
	EGLint maj, min, nc = 0;
	EGLConfig cfg;
	EGLint ca[] = { 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[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
	void (*load)(const char *) = dlsym(RTLD_NEXT, "SDL_GL_LoadLibrary");

	if (load)
		load(NULL);
	if (!gp || !qd) {
		fprintf(stderr, "[gzes] no EGL platform ext\n");
		return NULL;
	}
	{
		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)) {
		fprintf(stderr, "[gzes] eglInitialize 0x%x\n", eglGetError());
		return NULL;
	}
	if (!eglChooseConfig(dpy, ca, &cfg, 1, &nc) || nc < 1) {
		fprintf(stderr, "[gzes] choose 0x%x\n", eglGetError());
		return NULL;
	}
	ctx = eglCreateContext(dpy, cfg, EGL_NO_CONTEXT, at);
	if (ctx == EGL_NO_CONTEXT) {
		fprintf(stderr, "[gzes] ctx 0x%x\n", eglGetError());
		return NULL;
	}
	if (!eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx)) {
		fprintf(stderr, "[gzes] current 0x%x\n", eglGetError());
		return NULL;
	}
	fprintf(stderr, "[gzes] OK %s | %s ctx=%p\n",
		glGetString(GL_RENDERER), glGetString(GL_VERSION), (void *)ctx);
	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;
	return eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
			      (EGLContext)c) ? 0 : -1;
}

void SDL_GL_SwapWindow(void *win)
{
	(void)win;
	glFlush();
}

void SDL_GL_GetDrawableSize(void *win, int *w, int *h)
{
	(void)win;
	if (w)
		*w = 640;
	if (h)
		*h = 480;
}

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;
}