xref: /qemu/ui/egl-helpers.c (revision f2ad72b3)
1 #include "qemu/osdep.h"
2 #include <glob.h>
3 
4 #include "ui/egl-helpers.h"
5 
6 EGLDisplay *qemu_egl_display;
7 EGLConfig qemu_egl_config;
8 
9 /* ---------------------------------------------------------------------- */
10 
11 static bool egl_gles;
12 static int egl_debug;
13 
14 #define egl_dbg(_x ...)                          \
15     do {                                         \
16         if (egl_debug) {                         \
17             fprintf(stderr, "egl: " _x);         \
18         }                                        \
19     } while (0);
20 
21 /* ---------------------------------------------------------------------- */
22 
23 EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, Window win)
24 {
25     EGLSurface esurface;
26     EGLBoolean b;
27 
28     egl_dbg("eglCreateWindowSurface (x11 win id 0x%lx) ...\n",
29             (unsigned long) win);
30     esurface = eglCreateWindowSurface(qemu_egl_display,
31                                       qemu_egl_config,
32                                       (EGLNativeWindowType)win, NULL);
33     if (esurface == EGL_NO_SURFACE) {
34         fprintf(stderr, "egl: eglCreateWindowSurface failed\n");
35         return NULL;
36     }
37 
38     b = eglMakeCurrent(qemu_egl_display, esurface, esurface, ectx);
39     if (b == EGL_FALSE) {
40         fprintf(stderr, "egl: eglMakeCurrent failed\n");
41         return NULL;
42     }
43 
44     return esurface;
45 }
46 
47 /* ---------------------------------------------------------------------- */
48 
49 int qemu_egl_init_dpy(EGLNativeDisplayType dpy, bool gles, bool debug)
50 {
51     static const EGLint conf_att_gl[] = {
52         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
53         EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
54         EGL_RED_SIZE,   5,
55         EGL_GREEN_SIZE, 5,
56         EGL_BLUE_SIZE,  5,
57         EGL_ALPHA_SIZE, 0,
58         EGL_NONE,
59     };
60     static const EGLint conf_att_gles[] = {
61         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
62         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
63         EGL_RED_SIZE,   5,
64         EGL_GREEN_SIZE, 5,
65         EGL_BLUE_SIZE,  5,
66         EGL_ALPHA_SIZE, 0,
67         EGL_NONE,
68     };
69     EGLint major, minor;
70     EGLBoolean b;
71     EGLint n;
72 
73     if (debug) {
74         egl_debug = 1;
75         setenv("EGL_LOG_LEVEL", "debug", true);
76         setenv("LIBGL_DEBUG", "verbose", true);
77     }
78 
79     egl_dbg("eglGetDisplay (dpy %p) ...\n", dpy);
80     qemu_egl_display = eglGetDisplay(dpy);
81     if (qemu_egl_display == EGL_NO_DISPLAY) {
82         fprintf(stderr, "egl: eglGetDisplay failed\n");
83         return -1;
84     }
85 
86     egl_dbg("eglInitialize ...\n");
87     b = eglInitialize(qemu_egl_display, &major, &minor);
88     if (b == EGL_FALSE) {
89         fprintf(stderr, "egl: eglInitialize failed\n");
90         return -1;
91     }
92 
93     egl_dbg("eglBindAPI ...\n");
94     b = eglBindAPI(gles ? EGL_OPENGL_ES_API : EGL_OPENGL_API);
95     if (b == EGL_FALSE) {
96         fprintf(stderr, "egl: eglBindAPI failed\n");
97         return -1;
98     }
99 
100     egl_dbg("eglChooseConfig ...\n");
101     b = eglChooseConfig(qemu_egl_display,
102                         gles ? conf_att_gles : conf_att_gl,
103                         &qemu_egl_config, 1, &n);
104     if (b == EGL_FALSE || n != 1) {
105         fprintf(stderr, "egl: eglChooseConfig failed\n");
106         return -1;
107     }
108 
109     egl_gles = gles;
110     return 0;
111 }
112 
113 EGLContext qemu_egl_init_ctx(void)
114 {
115     static const EGLint ctx_att_gl[] = {
116         EGL_NONE
117     };
118     static const EGLint ctx_att_gles[] = {
119         EGL_CONTEXT_CLIENT_VERSION, 2,
120         EGL_NONE
121     };
122 
123     EGLContext ectx;
124     EGLBoolean b;
125 
126     egl_dbg("eglCreateContext ...\n");
127     ectx = eglCreateContext(qemu_egl_display, qemu_egl_config, EGL_NO_CONTEXT,
128                             egl_gles ? ctx_att_gles : ctx_att_gl);
129     if (ectx == EGL_NO_CONTEXT) {
130         fprintf(stderr, "egl: eglCreateContext failed\n");
131         return NULL;
132     }
133 
134     b = eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, ectx);
135     if (b == EGL_FALSE) {
136         fprintf(stderr, "egl: eglMakeCurrent failed\n");
137         return NULL;
138     }
139 
140     return ectx;
141 }
142