virtio: introduce virtqueue_alloc_element
[qemu.git] / ui / egl-helpers.c
blob87d77afaa852bbd50e3269be8cf261be3ad359f0
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <stdbool.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <glob.h>
11 #include "ui/egl-helpers.h"
13 EGLDisplay *qemu_egl_display;
14 EGLConfig qemu_egl_config;
16 /* ---------------------------------------------------------------------- */
18 static bool egl_gles;
19 static int egl_debug;
21 #define egl_dbg(_x ...) \
22 do { \
23 if (egl_debug) { \
24 fprintf(stderr, "egl: " _x); \
25 } \
26 } while (0);
28 /* ---------------------------------------------------------------------- */
30 EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, Window win)
32 EGLSurface esurface;
33 EGLBoolean b;
35 egl_dbg("eglCreateWindowSurface (x11 win id 0x%lx) ...\n",
36 (unsigned long) win);
37 esurface = eglCreateWindowSurface(qemu_egl_display,
38 qemu_egl_config,
39 (EGLNativeWindowType)win, NULL);
40 if (esurface == EGL_NO_SURFACE) {
41 fprintf(stderr, "egl: eglCreateWindowSurface failed\n");
42 return NULL;
45 b = eglMakeCurrent(qemu_egl_display, esurface, esurface, ectx);
46 if (b == EGL_FALSE) {
47 fprintf(stderr, "egl: eglMakeCurrent failed\n");
48 return NULL;
51 return esurface;
54 /* ---------------------------------------------------------------------- */
56 int qemu_egl_init_dpy(EGLNativeDisplayType dpy, bool gles, bool debug)
58 static const EGLint conf_att_gl[] = {
59 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
60 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
61 EGL_RED_SIZE, 5,
62 EGL_GREEN_SIZE, 5,
63 EGL_BLUE_SIZE, 5,
64 EGL_ALPHA_SIZE, 0,
65 EGL_NONE,
67 static const EGLint conf_att_gles[] = {
68 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
69 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
70 EGL_RED_SIZE, 5,
71 EGL_GREEN_SIZE, 5,
72 EGL_BLUE_SIZE, 5,
73 EGL_ALPHA_SIZE, 0,
74 EGL_NONE,
76 EGLint major, minor;
77 EGLBoolean b;
78 EGLint n;
80 if (debug) {
81 egl_debug = 1;
82 setenv("EGL_LOG_LEVEL", "debug", true);
83 setenv("LIBGL_DEBUG", "verbose", true);
86 egl_dbg("eglGetDisplay (dpy %p) ...\n", dpy);
87 qemu_egl_display = eglGetDisplay(dpy);
88 if (qemu_egl_display == EGL_NO_DISPLAY) {
89 fprintf(stderr, "egl: eglGetDisplay failed\n");
90 return -1;
93 egl_dbg("eglInitialize ...\n");
94 b = eglInitialize(qemu_egl_display, &major, &minor);
95 if (b == EGL_FALSE) {
96 fprintf(stderr, "egl: eglInitialize failed\n");
97 return -1;
100 egl_dbg("eglBindAPI ...\n");
101 b = eglBindAPI(gles ? EGL_OPENGL_ES_API : EGL_OPENGL_API);
102 if (b == EGL_FALSE) {
103 fprintf(stderr, "egl: eglBindAPI failed\n");
104 return -1;
107 egl_dbg("eglChooseConfig ...\n");
108 b = eglChooseConfig(qemu_egl_display,
109 gles ? conf_att_gles : conf_att_gl,
110 &qemu_egl_config, 1, &n);
111 if (b == EGL_FALSE || n != 1) {
112 fprintf(stderr, "egl: eglChooseConfig failed\n");
113 return -1;
116 egl_gles = gles;
117 return 0;
120 EGLContext qemu_egl_init_ctx(void)
122 static const EGLint ctx_att_gl[] = {
123 EGL_NONE
125 static const EGLint ctx_att_gles[] = {
126 EGL_CONTEXT_CLIENT_VERSION, 2,
127 EGL_NONE
130 EGLContext ectx;
131 EGLBoolean b;
133 egl_dbg("eglCreateContext ...\n");
134 ectx = eglCreateContext(qemu_egl_display, qemu_egl_config, EGL_NO_CONTEXT,
135 egl_gles ? ctx_att_gles : ctx_att_gl);
136 if (ectx == EGL_NO_CONTEXT) {
137 fprintf(stderr, "egl: eglCreateContext failed\n");
138 return NULL;
141 b = eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, ectx);
142 if (b == EGL_FALSE) {
143 fprintf(stderr, "egl: eglMakeCurrent failed\n");
144 return NULL;
147 return ectx;