xref: /qemu/ui/egl-helpers.c (revision 3e1210e8b0d86a2190e51632985c3da3e5721336)
1 /*
2  * Copyright (C) 2015-2016 Gerd Hoffmann <kraxel@redhat.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16  */
17 #include "qemu/osdep.h"
18 
19 #include "qemu/drm.h"
20 #include "qemu/error-report.h"
21 #include "ui/console.h"
22 #include "ui/egl-helpers.h"
23 #include "system/system.h"
24 #include "qapi/error.h"
25 #include "trace.h"
26 #include "standard-headers/drm/drm_fourcc.h"
27 
28 EGLDisplay *qemu_egl_display;
29 EGLConfig qemu_egl_config;
30 DisplayGLMode qemu_egl_mode;
31 bool qemu_egl_angle_d3d;
32 
33 /* ------------------------------------------------------------------ */
34 
qemu_egl_get_error_string(void)35 const char *qemu_egl_get_error_string(void)
36 {
37     EGLint error = eglGetError();
38 
39     switch (error) {
40     case EGL_SUCCESS:
41         return "EGL_SUCCESS";
42     case EGL_NOT_INITIALIZED:
43         return "EGL_NOT_INITIALIZED";
44     case EGL_BAD_ACCESS:
45         return "EGL_BAD_ACCESS";
46     case EGL_BAD_ALLOC:
47         return "EGL_BAD_ALLOC";
48     case EGL_BAD_ATTRIBUTE:
49         return "EGL_BAD_ATTRIBUTE";
50     case EGL_BAD_CONTEXT:
51         return "EGL_BAD_CONTEXT";
52     case EGL_BAD_CONFIG:
53         return "EGL_BAD_CONFIG";
54     case EGL_BAD_CURRENT_SURFACE:
55         return "EGL_BAD_CURRENT_SURFACE";
56     case EGL_BAD_DISPLAY:
57         return "EGL_BAD_DISPLAY";
58     case EGL_BAD_SURFACE:
59         return "EGL_BAD_SURFACE";
60     case EGL_BAD_MATCH:
61         return "EGL_BAD_MATCH";
62     case EGL_BAD_PARAMETER:
63         return "EGL_BAD_PARAMETER";
64     case EGL_BAD_NATIVE_PIXMAP:
65         return "EGL_BAD_NATIVE_PIXMAP";
66     case EGL_BAD_NATIVE_WINDOW:
67         return "EGL_BAD_NATIVE_WINDOW";
68     case EGL_CONTEXT_LOST:
69         return "EGL_CONTEXT_LOST";
70     default:
71         return "Unknown EGL error";
72     }
73 }
74 
egl_fb_delete_texture(egl_fb * fb)75 static void egl_fb_delete_texture(egl_fb *fb)
76 {
77     if (!fb->delete_texture) {
78         return;
79     }
80 
81     glDeleteTextures(1, &fb->texture);
82     fb->delete_texture = false;
83 }
84 
egl_fb_destroy(egl_fb * fb)85 void egl_fb_destroy(egl_fb *fb)
86 {
87     if (!fb->framebuffer) {
88         return;
89     }
90 
91     egl_fb_delete_texture(fb);
92     glDeleteFramebuffers(1, &fb->framebuffer);
93 
94     fb->width = 0;
95     fb->height = 0;
96     fb->texture = 0;
97     fb->framebuffer = 0;
98 }
99 
egl_fb_setup_default(egl_fb * fb,int width,int height)100 void egl_fb_setup_default(egl_fb *fb, int width, int height)
101 {
102     fb->width = width;
103     fb->height = height;
104     fb->framebuffer = 0; /* default framebuffer */
105 }
106 
egl_fb_setup_for_tex(egl_fb * fb,int width,int height,GLuint texture,bool delete)107 void egl_fb_setup_for_tex(egl_fb *fb, int width, int height,
108                           GLuint texture, bool delete)
109 {
110     egl_fb_delete_texture(fb);
111 
112     fb->width = width;
113     fb->height = height;
114     fb->texture = texture;
115     fb->delete_texture = delete;
116     if (!fb->framebuffer) {
117         glGenFramebuffers(1, &fb->framebuffer);
118     }
119 
120     glBindFramebuffer(GL_FRAMEBUFFER_EXT, fb->framebuffer);
121     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
122                               GL_TEXTURE_2D, fb->texture, 0);
123 }
124 
egl_fb_setup_new_tex(egl_fb * fb,int width,int height)125 void egl_fb_setup_new_tex(egl_fb *fb, int width, int height)
126 {
127     GLuint texture;
128 
129     glGenTextures(1, &texture);
130     glBindTexture(GL_TEXTURE_2D, texture);
131     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height,
132                  0, GL_BGRA, GL_UNSIGNED_BYTE, 0);
133 
134     egl_fb_setup_for_tex(fb, width, height, texture, true);
135 }
136 
egl_fb_blit(egl_fb * dst,egl_fb * src,bool flip)137 void egl_fb_blit(egl_fb *dst, egl_fb *src, bool flip)
138 {
139     GLuint x1 = 0;
140     GLuint y1 = 0;
141     GLuint x2, y2;
142     GLuint w = src->width;
143     GLuint h = src->height;
144 
145     glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer);
146     glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst->framebuffer);
147     glViewport(0, 0, dst->width, dst->height);
148 
149     if (src->dmabuf) {
150         x1 = qemu_dmabuf_get_x(src->dmabuf);
151         y1 = qemu_dmabuf_get_y(src->dmabuf);
152         w = qemu_dmabuf_get_width(src->dmabuf);
153         h = qemu_dmabuf_get_height(src->dmabuf);
154     }
155 
156     w = (x1 + w) > src->width ? src->width - x1 : w;
157     h = (y1 + h) > src->height ? src->height - y1 : h;
158 
159     y2 = flip ? y1 : h + y1;
160     y1 = flip ? h + y1 : y1;
161     x2 = x1 + w;
162 
163     glBlitFramebuffer(x1, y1, x2, y2,
164                       0, 0, dst->width, dst->height,
165                       GL_COLOR_BUFFER_BIT, GL_LINEAR);
166 }
167 
egl_fb_read(DisplaySurface * dst,egl_fb * src)168 void egl_fb_read(DisplaySurface *dst, egl_fb *src)
169 {
170     glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer);
171     glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
172     glReadPixels(0, 0, surface_width(dst), surface_height(dst),
173                  GL_BGRA, GL_UNSIGNED_BYTE, surface_data(dst));
174 }
175 
egl_fb_read_rect(DisplaySurface * dst,egl_fb * src,int x,int y,int w,int h)176 void egl_fb_read_rect(DisplaySurface *dst, egl_fb *src, int x, int y, int w, int h)
177 {
178     assert(surface_width(dst) == src->width);
179     assert(surface_height(dst) == src->height);
180     assert(surface_format(dst) == PIXMAN_x8r8g8b8);
181 
182     glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer);
183     glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
184     glPixelStorei(GL_PACK_ROW_LENGTH, surface_stride(dst) / 4);
185     glReadPixels(x, y, w, h,
186                  GL_BGRA, GL_UNSIGNED_BYTE, surface_data(dst) + x * 4);
187     glPixelStorei(GL_PACK_ROW_LENGTH, 0);
188 }
189 
egl_texture_blit(QemuGLShader * gls,egl_fb * dst,egl_fb * src,bool flip)190 void egl_texture_blit(QemuGLShader *gls, egl_fb *dst, egl_fb *src, bool flip)
191 {
192     glBindFramebuffer(GL_FRAMEBUFFER_EXT, dst->framebuffer);
193     glViewport(0, 0, dst->width, dst->height);
194     glEnable(GL_TEXTURE_2D);
195     glBindTexture(GL_TEXTURE_2D, src->texture);
196     qemu_gl_run_texture_blit(gls, flip);
197 }
198 
egl_texture_blend(QemuGLShader * gls,egl_fb * dst,egl_fb * src,bool flip,int x,int y,double scale_x,double scale_y)199 void egl_texture_blend(QemuGLShader *gls, egl_fb *dst, egl_fb *src, bool flip,
200                        int x, int y, double scale_x, double scale_y)
201 {
202     glBindFramebuffer(GL_FRAMEBUFFER_EXT, dst->framebuffer);
203     int w = scale_x * src->width;
204     int h = scale_y * src->height;
205     if (flip) {
206         glViewport(x, y, w, h);
207     } else {
208         glViewport(x, dst->height - h - y, w, h);
209     }
210     glEnable(GL_TEXTURE_2D);
211     glBindTexture(GL_TEXTURE_2D, src->texture);
212     glEnable(GL_BLEND);
213     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
214     qemu_gl_run_texture_blit(gls, flip);
215     glDisable(GL_BLEND);
216 }
217 
218 /* ---------------------------------------------------------------------- */
219 
220 EGLContext qemu_egl_rn_ctx;
221 
222 #ifdef CONFIG_GBM
223 
224 int qemu_egl_rn_fd;
225 struct gbm_device *qemu_egl_rn_gbm_dev;
226 
egl_rendernode_init(const char * rendernode,DisplayGLMode mode)227 int egl_rendernode_init(const char *rendernode, DisplayGLMode mode)
228 {
229     qemu_egl_rn_fd = -1;
230     int rc;
231 
232     qemu_egl_rn_fd = qemu_drm_rendernode_open(rendernode);
233     if (qemu_egl_rn_fd == -1) {
234         error_report("egl: no drm render node available");
235         goto err;
236     }
237 
238     qemu_egl_rn_gbm_dev = gbm_create_device(qemu_egl_rn_fd);
239     if (!qemu_egl_rn_gbm_dev) {
240         error_report("egl: gbm_create_device failed");
241         goto err;
242     }
243 
244     rc = qemu_egl_init_dpy_mesa((EGLNativeDisplayType)qemu_egl_rn_gbm_dev,
245                                 mode);
246     if (rc != 0) {
247         /* qemu_egl_init_dpy_mesa reports error */
248         goto err;
249     }
250 
251     if (!epoxy_has_egl_extension(qemu_egl_display,
252                                  "EGL_KHR_surfaceless_context")) {
253         error_report("egl: EGL_KHR_surfaceless_context not supported");
254         goto err;
255     }
256     if (!epoxy_has_egl_extension(qemu_egl_display,
257                                  "EGL_MESA_image_dma_buf_export")) {
258         error_report("egl: EGL_MESA_image_dma_buf_export not supported");
259         goto err;
260     }
261     if (!epoxy_has_egl_extension(qemu_egl_display,
262                                  "EGL_EXT_image_dma_buf_import_modifiers")) {
263         error_report("egl: EGL_EXT_image_dma_buf_import_modifiers not supported");
264         goto err;
265     }
266 
267     qemu_egl_rn_ctx = qemu_egl_init_ctx();
268     if (!qemu_egl_rn_ctx) {
269         error_report("egl: egl_init_ctx failed");
270         goto err;
271     }
272 
273     return 0;
274 
275 err:
276     if (qemu_egl_rn_gbm_dev) {
277         gbm_device_destroy(qemu_egl_rn_gbm_dev);
278     }
279     if (qemu_egl_rn_fd != -1) {
280         close(qemu_egl_rn_fd);
281     }
282 
283     return -1;
284 }
285 
egl_dmabuf_export_texture(uint32_t tex_id,int * fd,EGLint * offset,EGLint * stride,EGLint * fourcc,int * num_planes,EGLuint64KHR * modifier)286 bool egl_dmabuf_export_texture(uint32_t tex_id, int *fd, EGLint *offset,
287                                EGLint *stride, EGLint *fourcc, int *num_planes,
288                                EGLuint64KHR *modifier)
289 {
290     EGLImageKHR image;
291     EGLuint64KHR modifiers[DMABUF_MAX_PLANES];
292 
293     image = eglCreateImageKHR(qemu_egl_display, eglGetCurrentContext(),
294                               EGL_GL_TEXTURE_2D_KHR,
295                               (EGLClientBuffer)(unsigned long)tex_id,
296                               NULL);
297     if (!image) {
298         return false;
299     }
300 
301     eglExportDMABUFImageQueryMESA(qemu_egl_display, image, fourcc,
302                                   num_planes, modifiers);
303     eglExportDMABUFImageMESA(qemu_egl_display, image, fd, stride, offset);
304     eglDestroyImageKHR(qemu_egl_display, image);
305 
306     /* Only first modifier matters. */
307     if (modifier) {
308         *modifier = modifiers[0];
309     }
310 
311     return true;
312 }
313 
egl_dmabuf_import_texture(QemuDmaBuf * dmabuf)314 void egl_dmabuf_import_texture(QemuDmaBuf *dmabuf)
315 {
316     EGLImageKHR image = EGL_NO_IMAGE_KHR;
317     EGLint attrs[64];
318     int i = 0, j;
319     uint64_t modifier = qemu_dmabuf_get_modifier(dmabuf);
320     uint32_t texture = qemu_dmabuf_get_texture(dmabuf);
321     int nfds, noffsets, nstrides;
322     const int *fds = qemu_dmabuf_get_fds(dmabuf, &nfds);
323     const uint32_t *offsets = qemu_dmabuf_get_offsets(dmabuf, &noffsets);
324     const uint32_t *strides = qemu_dmabuf_get_strides(dmabuf, &nstrides);
325     uint32_t num_planes = qemu_dmabuf_get_num_planes(dmabuf);
326 
327     EGLint fd_attrs[] = {
328         EGL_DMA_BUF_PLANE0_FD_EXT,
329         EGL_DMA_BUF_PLANE1_FD_EXT,
330         EGL_DMA_BUF_PLANE2_FD_EXT,
331         EGL_DMA_BUF_PLANE3_FD_EXT,
332     };
333     EGLint offset_attrs[] = {
334         EGL_DMA_BUF_PLANE0_OFFSET_EXT,
335         EGL_DMA_BUF_PLANE1_OFFSET_EXT,
336         EGL_DMA_BUF_PLANE2_OFFSET_EXT,
337         EGL_DMA_BUF_PLANE3_OFFSET_EXT,
338     };
339     EGLint stride_attrs[] = {
340         EGL_DMA_BUF_PLANE0_PITCH_EXT,
341         EGL_DMA_BUF_PLANE1_PITCH_EXT,
342         EGL_DMA_BUF_PLANE2_PITCH_EXT,
343         EGL_DMA_BUF_PLANE3_PITCH_EXT,
344     };
345     EGLint modifier_lo_attrs[] = {
346         EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT,
347         EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT,
348         EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT,
349         EGL_DMA_BUF_PLANE3_MODIFIER_LO_EXT,
350     };
351     EGLint modifier_hi_attrs[] = {
352         EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT,
353         EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT,
354         EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT,
355         EGL_DMA_BUF_PLANE3_MODIFIER_HI_EXT,
356     };
357 
358     if (texture != 0) {
359         return;
360     }
361 
362     assert(nfds >= num_planes);
363     assert(noffsets >= num_planes);
364     assert(nstrides >= num_planes);
365 
366     attrs[i++] = EGL_WIDTH;
367     attrs[i++] = qemu_dmabuf_get_backing_width(dmabuf);
368     attrs[i++] = EGL_HEIGHT;
369     attrs[i++] = qemu_dmabuf_get_backing_height(dmabuf);
370     attrs[i++] = EGL_LINUX_DRM_FOURCC_EXT;
371     attrs[i++] = qemu_dmabuf_get_fourcc(dmabuf);
372 
373     for (j = 0; j < num_planes; j++) {
374         attrs[i++] = fd_attrs[j];
375         /* fd[1-3] may be -1 if using a joint buffer for all planes */
376         attrs[i++] = fds[j] >= 0 ? fds[j] : fds[0];
377         attrs[i++] = stride_attrs[j];
378         attrs[i++] = strides[j];
379         attrs[i++] = offset_attrs[j];
380         attrs[i++] = offsets[j];
381         if (modifier != DRM_FORMAT_MOD_INVALID) {
382             attrs[i++] = modifier_lo_attrs[j];
383             attrs[i++] = (modifier >>  0) & 0xffffffff;
384             attrs[i++] = modifier_hi_attrs[j];
385             attrs[i++] = (modifier >> 32) & 0xffffffff;
386         }
387     }
388 
389     attrs[i++] = EGL_NONE;
390 
391     image = eglCreateImageKHR(qemu_egl_display,
392                               EGL_NO_CONTEXT,
393                               EGL_LINUX_DMA_BUF_EXT,
394                               NULL, attrs);
395     if (image == EGL_NO_IMAGE_KHR) {
396         error_report("eglCreateImageKHR failed");
397         return;
398     }
399 
400     glGenTextures(1, &texture);
401     qemu_dmabuf_set_texture(dmabuf, texture);
402     glBindTexture(GL_TEXTURE_2D, texture);
403     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
404     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
405 
406     glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
407     eglDestroyImageKHR(qemu_egl_display, image);
408 }
409 
egl_dmabuf_release_texture(QemuDmaBuf * dmabuf)410 void egl_dmabuf_release_texture(QemuDmaBuf *dmabuf)
411 {
412     uint32_t texture;
413 
414     texture = qemu_dmabuf_get_texture(dmabuf);
415     if (texture == 0) {
416         return;
417     }
418 
419     glDeleteTextures(1, &texture);
420     qemu_dmabuf_set_texture(dmabuf, 0);
421 }
422 
egl_dmabuf_create_sync(QemuDmaBuf * dmabuf)423 void egl_dmabuf_create_sync(QemuDmaBuf *dmabuf)
424 {
425     EGLSyncKHR sync;
426 
427     if (epoxy_has_egl_extension(qemu_egl_display,
428                                 "EGL_KHR_fence_sync") &&
429         epoxy_has_egl_extension(qemu_egl_display,
430                                 "EGL_ANDROID_native_fence_sync")) {
431         sync = eglCreateSyncKHR(qemu_egl_display,
432                                 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
433         if (sync != EGL_NO_SYNC_KHR) {
434             qemu_dmabuf_set_sync(dmabuf, sync);
435         }
436     }
437 }
438 
egl_dmabuf_create_fence(QemuDmaBuf * dmabuf)439 void egl_dmabuf_create_fence(QemuDmaBuf *dmabuf)
440 {
441     void *sync = qemu_dmabuf_get_sync(dmabuf);
442     int fence_fd;
443 
444     if (sync) {
445         fence_fd = eglDupNativeFenceFDANDROID(qemu_egl_display,
446                                               sync);
447         qemu_dmabuf_set_fence_fd(dmabuf, fence_fd);
448         eglDestroySyncKHR(qemu_egl_display, sync);
449         qemu_dmabuf_set_sync(dmabuf, NULL);
450     }
451 }
452 
453 #endif /* CONFIG_GBM */
454 
455 /* ---------------------------------------------------------------------- */
456 
qemu_egl_init_surface_x11(EGLContext ectx,EGLNativeWindowType win)457 EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, EGLNativeWindowType win)
458 {
459     EGLSurface esurface;
460     EGLBoolean b;
461 
462     esurface = eglCreateWindowSurface(qemu_egl_display,
463                                       qemu_egl_config,
464                                       win, NULL);
465     if (esurface == EGL_NO_SURFACE) {
466         error_report("egl: eglCreateWindowSurface failed");
467         return NULL;
468     }
469 
470     b = eglMakeCurrent(qemu_egl_display, esurface, esurface, ectx);
471     if (b == EGL_FALSE) {
472         error_report("egl: eglMakeCurrent failed");
473         return NULL;
474     }
475 
476     return esurface;
477 }
478 
479 /* ---------------------------------------------------------------------- */
480 
481 #if defined(CONFIG_X11) || defined(CONFIG_GBM) || defined(WIN32)
482 
483 /*
484  * Taken from glamor_egl.h from the Xorg xserver, which is MIT licensed
485  *
486  * Create an EGLDisplay from a native display type. This is a little quirky
487  * for a few reasons.
488  *
489  * 1: GetPlatformDisplayEXT and GetPlatformDisplay are the API you want to
490  * use, but have different function signatures in the third argument; this
491  * happens not to matter for us, at the moment, but it means epoxy won't alias
492  * them together.
493  *
494  * 2: epoxy 1.3 and earlier don't understand EGL client extensions, which
495  * means you can't call "eglGetPlatformDisplayEXT" directly, as the resolver
496  * will crash.
497  *
498  * 3: You can't tell whether you have EGL 1.5 at this point, because
499  * eglQueryString(EGL_VERSION) is a property of the display, which we don't
500  * have yet. So you have to query for extensions no matter what. Fortunately
501  * epoxy_has_egl_extension _does_ let you query for client extensions, so
502  * we don't have to write our own extension string parsing.
503  *
504  * 4. There is no EGL_KHR_platform_base to complement the EXT one, thus one
505  * needs to know EGL 1.5 is supported in order to use the eglGetPlatformDisplay
506  * function pointer.
507  * We can workaround this (circular dependency) by probing for the EGL 1.5
508  * platform extensions (EGL_KHR_platform_gbm and friends) yet it doesn't seem
509  * like mesa will be able to advertise these (even though it can do EGL 1.5).
510  */
qemu_egl_get_display(EGLNativeDisplayType native,EGLenum platform)511 static EGLDisplay qemu_egl_get_display(EGLNativeDisplayType native,
512                                        EGLenum platform)
513 {
514     EGLDisplay dpy = EGL_NO_DISPLAY;
515 
516     /* In practise any EGL 1.5 implementation would support the EXT extension */
517     if (epoxy_has_egl_extension(NULL, "EGL_EXT_platform_base")) {
518         if (platform != 0) {
519             dpy = eglGetPlatformDisplayEXT(platform, native, NULL);
520         }
521     }
522 
523     if (dpy == EGL_NO_DISPLAY) {
524         /* fallback */
525         dpy = eglGetDisplay(native);
526     }
527     return dpy;
528 }
529 
qemu_egl_init_dpy(EGLNativeDisplayType dpy,EGLenum platform,DisplayGLMode mode)530 static int qemu_egl_init_dpy(EGLNativeDisplayType dpy,
531                              EGLenum platform,
532                              DisplayGLMode mode)
533 {
534     static const EGLint conf_att_core[] = {
535         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
536         EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
537         EGL_RED_SIZE,   5,
538         EGL_GREEN_SIZE, 5,
539         EGL_BLUE_SIZE,  5,
540         EGL_ALPHA_SIZE, 0,
541         EGL_NONE,
542     };
543     static const EGLint conf_att_gles[] = {
544         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
545         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
546         EGL_RED_SIZE,   5,
547         EGL_GREEN_SIZE, 5,
548         EGL_BLUE_SIZE,  5,
549         EGL_ALPHA_SIZE, 0,
550         EGL_NONE,
551     };
552     EGLint major, minor;
553     EGLBoolean b;
554     EGLint n;
555     bool gles = (mode == DISPLAY_GL_MODE_ES);
556 
557     qemu_egl_display = qemu_egl_get_display(dpy, platform);
558     if (qemu_egl_display == EGL_NO_DISPLAY) {
559         error_report("egl: eglGetDisplay failed: %s", qemu_egl_get_error_string());
560         return -1;
561     }
562 
563     b = eglInitialize(qemu_egl_display, &major, &minor);
564     if (b == EGL_FALSE) {
565         error_report("egl: eglInitialize failed: %s", qemu_egl_get_error_string());
566         return -1;
567     }
568 
569     b = eglBindAPI(gles ?  EGL_OPENGL_ES_API : EGL_OPENGL_API);
570     if (b == EGL_FALSE) {
571         error_report("egl: eglBindAPI failed (%s mode): %s",
572                      gles ? "gles" : "core", qemu_egl_get_error_string());
573         return -1;
574     }
575 
576     b = eglChooseConfig(qemu_egl_display,
577                         gles ? conf_att_gles : conf_att_core,
578                         &qemu_egl_config, 1, &n);
579     if (b == EGL_FALSE || n != 1) {
580         error_report("egl: eglChooseConfig failed (%s mode): %s",
581                      gles ? "gles" : "core", qemu_egl_get_error_string());
582         return -1;
583     }
584 
585     qemu_egl_mode = gles ? DISPLAY_GL_MODE_ES : DISPLAY_GL_MODE_CORE;
586     return 0;
587 }
588 
589 #endif
590 
591 #if defined(CONFIG_X11) || defined(CONFIG_GBM)
qemu_egl_init_dpy_x11(EGLNativeDisplayType dpy,DisplayGLMode mode)592 int qemu_egl_init_dpy_x11(EGLNativeDisplayType dpy, DisplayGLMode mode)
593 {
594 #ifdef EGL_KHR_platform_x11
595     return qemu_egl_init_dpy(dpy, EGL_PLATFORM_X11_KHR, mode);
596 #else
597     return qemu_egl_init_dpy(dpy, 0, mode);
598 #endif
599 }
600 
qemu_egl_init_dpy_mesa(EGLNativeDisplayType dpy,DisplayGLMode mode)601 int qemu_egl_init_dpy_mesa(EGLNativeDisplayType dpy, DisplayGLMode mode)
602 {
603 #ifdef EGL_MESA_platform_gbm
604     return qemu_egl_init_dpy(dpy, EGL_PLATFORM_GBM_MESA, mode);
605 #else
606     return qemu_egl_init_dpy(dpy, 0, mode);
607 #endif
608 }
609 #endif
610 
611 
612 #ifdef WIN32
qemu_egl_init_dpy_win32(EGLNativeDisplayType dpy,DisplayGLMode mode)613 int qemu_egl_init_dpy_win32(EGLNativeDisplayType dpy, DisplayGLMode mode)
614 {
615     /* prefer GL ES, as that's what ANGLE supports */
616     if (mode == DISPLAY_GL_MODE_ON) {
617         mode = DISPLAY_GL_MODE_ES;
618     }
619 
620     if (qemu_egl_init_dpy(dpy, 0, mode) < 0) {
621         return -1;
622     }
623 
624 #ifdef EGL_D3D11_DEVICE_ANGLE
625     if (epoxy_has_egl_extension(qemu_egl_display, "EGL_EXT_device_query")) {
626         EGLDeviceEXT device;
627         void *d3d11_device;
628 
629         if (!eglQueryDisplayAttribEXT(qemu_egl_display,
630                                       EGL_DEVICE_EXT,
631                                       (EGLAttrib *)&device)) {
632             return 0;
633         }
634 
635         if (!eglQueryDeviceAttribEXT(device,
636                                      EGL_D3D11_DEVICE_ANGLE,
637                                      (EGLAttrib *)&d3d11_device)) {
638             return 0;
639         }
640 
641         trace_egl_init_d3d11_device(device);
642         qemu_egl_angle_d3d = device != NULL;
643     }
644 #endif
645 
646     return 0;
647 }
648 #endif
649 
qemu_egl_has_dmabuf(void)650 bool qemu_egl_has_dmabuf(void)
651 {
652     if (qemu_egl_display == EGL_NO_DISPLAY) {
653         return false;
654     }
655 
656     return epoxy_has_egl_extension(qemu_egl_display,
657                                    "EGL_EXT_image_dma_buf_import");
658 }
659 
qemu_egl_init_ctx(void)660 EGLContext qemu_egl_init_ctx(void)
661 {
662     static const EGLint ctx_att_core[] = {
663         EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT,
664         EGL_NONE
665     };
666     static const EGLint ctx_att_gles[] = {
667         EGL_CONTEXT_CLIENT_VERSION, 2,
668         EGL_NONE
669     };
670     bool gles = (qemu_egl_mode == DISPLAY_GL_MODE_ES);
671     EGLContext ectx;
672     EGLBoolean b;
673 
674     ectx = eglCreateContext(qemu_egl_display, qemu_egl_config, EGL_NO_CONTEXT,
675                             gles ? ctx_att_gles : ctx_att_core);
676     if (ectx == EGL_NO_CONTEXT) {
677         error_report("egl: eglCreateContext failed");
678         return NULL;
679     }
680 
681     b = eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, ectx);
682     if (b == EGL_FALSE) {
683         error_report("egl: eglMakeCurrent failed");
684         return NULL;
685     }
686 
687     return ectx;
688 }
689 
egl_init(const char * rendernode,DisplayGLMode mode,Error ** errp)690 bool egl_init(const char *rendernode, DisplayGLMode mode, Error **errp)
691 {
692     ERRP_GUARD();
693 
694     if (mode == DISPLAY_GL_MODE_OFF) {
695         error_setg(errp, "egl: turning off GL doesn't make sense");
696         return false;
697     }
698 
699 #ifdef WIN32
700     if (qemu_egl_init_dpy_win32(EGL_DEFAULT_DISPLAY, mode) < 0) {
701         error_setg(errp, "egl: init failed");
702         return false;
703     }
704     qemu_egl_rn_ctx = qemu_egl_init_ctx();
705     if (!qemu_egl_rn_ctx) {
706         error_setg(errp, "egl: egl_init_ctx failed");
707         return false;
708     }
709 #elif defined(CONFIG_GBM)
710     if (egl_rendernode_init(rendernode, mode) < 0) {
711         error_setg(errp, "egl: render node init failed");
712         return false;
713     }
714 #endif
715 
716     if (!qemu_egl_rn_ctx) {
717         error_setg(errp, "egl: not available on this platform");
718         return false;
719     }
720 
721     display_opengl = 1;
722     return true;
723 }
724