xref: /openbmc/qemu/ui/egl-helpers.c (revision ac70568902c3fb77516d93b169cddd0bcaabfb4e)
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
286 int egl_get_fd_for_texture(uint32_t tex_id, EGLint *stride, EGLint *fourcc,
287                            EGLuint64KHR *modifier)
288 {
289     EGLImageKHR image;
290     EGLint num_planes, fd;
291 
292     image = eglCreateImageKHR(qemu_egl_display, eglGetCurrentContext(),
293                               EGL_GL_TEXTURE_2D_KHR,
294                               (EGLClientBuffer)(unsigned long)tex_id,
295                               NULL);
296     if (!image) {
297         return -1;
298     }
299 
300     eglExportDMABUFImageQueryMESA(qemu_egl_display, image, fourcc,
301                                   &num_planes, modifier);
302     if (num_planes != 1) {
303         eglDestroyImageKHR(qemu_egl_display, image);
304         return -1;
305     }
306     eglExportDMABUFImageMESA(qemu_egl_display, image, &fd, stride, NULL);
307     eglDestroyImageKHR(qemu_egl_display, image);
308 
309     return fd;
310 }
311 
312 void egl_dmabuf_import_texture(QemuDmaBuf *dmabuf)
313 {
314     EGLImageKHR image = EGL_NO_IMAGE_KHR;
315     EGLint attrs[64];
316     int i = 0;
317     uint64_t modifier = qemu_dmabuf_get_modifier(dmabuf);
318     uint32_t texture = qemu_dmabuf_get_texture(dmabuf);
319 
320     if (texture != 0) {
321         return;
322     }
323 
324     attrs[i++] = EGL_WIDTH;
325     attrs[i++] = qemu_dmabuf_get_backing_width(dmabuf);
326     attrs[i++] = EGL_HEIGHT;
327     attrs[i++] = qemu_dmabuf_get_backing_height(dmabuf);
328     attrs[i++] = EGL_LINUX_DRM_FOURCC_EXT;
329     attrs[i++] = qemu_dmabuf_get_fourcc(dmabuf);
330 
331     attrs[i++] = EGL_DMA_BUF_PLANE0_FD_EXT;
332     attrs[i++] = qemu_dmabuf_get_fds(dmabuf, NULL)[0];
333     attrs[i++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
334     attrs[i++] = qemu_dmabuf_get_strides(dmabuf, NULL)[0];
335     attrs[i++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
336     attrs[i++] = 0;
337     if (modifier != DRM_FORMAT_MOD_INVALID) {
338         attrs[i++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT;
339         attrs[i++] = (modifier >>  0) & 0xffffffff;
340         attrs[i++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT;
341         attrs[i++] = (modifier >> 32) & 0xffffffff;
342     }
343     attrs[i++] = EGL_NONE;
344 
345     image = eglCreateImageKHR(qemu_egl_display,
346                               EGL_NO_CONTEXT,
347                               EGL_LINUX_DMA_BUF_EXT,
348                               NULL, attrs);
349     if (image == EGL_NO_IMAGE_KHR) {
350         error_report("eglCreateImageKHR failed");
351         return;
352     }
353 
354     glGenTextures(1, &texture);
355     qemu_dmabuf_set_texture(dmabuf, texture);
356     glBindTexture(GL_TEXTURE_2D, texture);
357     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
358     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
359 
360     glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
361     eglDestroyImageKHR(qemu_egl_display, image);
362 }
363 
364 void egl_dmabuf_release_texture(QemuDmaBuf *dmabuf)
365 {
366     uint32_t texture;
367 
368     texture = qemu_dmabuf_get_texture(dmabuf);
369     if (texture == 0) {
370         return;
371     }
372 
373     glDeleteTextures(1, &texture);
374     qemu_dmabuf_set_texture(dmabuf, 0);
375 }
376 
377 void egl_dmabuf_create_sync(QemuDmaBuf *dmabuf)
378 {
379     EGLSyncKHR sync;
380 
381     if (epoxy_has_egl_extension(qemu_egl_display,
382                                 "EGL_KHR_fence_sync") &&
383         epoxy_has_egl_extension(qemu_egl_display,
384                                 "EGL_ANDROID_native_fence_sync")) {
385         sync = eglCreateSyncKHR(qemu_egl_display,
386                                 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
387         if (sync != EGL_NO_SYNC_KHR) {
388             qemu_dmabuf_set_sync(dmabuf, sync);
389         }
390     }
391 }
392 
393 void egl_dmabuf_create_fence(QemuDmaBuf *dmabuf)
394 {
395     void *sync = qemu_dmabuf_get_sync(dmabuf);
396     int fence_fd;
397 
398     if (sync) {
399         fence_fd = eglDupNativeFenceFDANDROID(qemu_egl_display,
400                                               sync);
401         qemu_dmabuf_set_fence_fd(dmabuf, fence_fd);
402         eglDestroySyncKHR(qemu_egl_display, sync);
403         qemu_dmabuf_set_sync(dmabuf, NULL);
404     }
405 }
406 
407 #endif /* CONFIG_GBM */
408 
409 /* ---------------------------------------------------------------------- */
410 
411 EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, EGLNativeWindowType win)
412 {
413     EGLSurface esurface;
414     EGLBoolean b;
415 
416     esurface = eglCreateWindowSurface(qemu_egl_display,
417                                       qemu_egl_config,
418                                       win, NULL);
419     if (esurface == EGL_NO_SURFACE) {
420         error_report("egl: eglCreateWindowSurface failed");
421         return NULL;
422     }
423 
424     b = eglMakeCurrent(qemu_egl_display, esurface, esurface, ectx);
425     if (b == EGL_FALSE) {
426         error_report("egl: eglMakeCurrent failed");
427         return NULL;
428     }
429 
430     return esurface;
431 }
432 
433 /* ---------------------------------------------------------------------- */
434 
435 #if defined(CONFIG_X11) || defined(CONFIG_GBM) || defined(WIN32)
436 
437 /*
438  * Taken from glamor_egl.h from the Xorg xserver, which is MIT licensed
439  *
440  * Create an EGLDisplay from a native display type. This is a little quirky
441  * for a few reasons.
442  *
443  * 1: GetPlatformDisplayEXT and GetPlatformDisplay are the API you want to
444  * use, but have different function signatures in the third argument; this
445  * happens not to matter for us, at the moment, but it means epoxy won't alias
446  * them together.
447  *
448  * 2: epoxy 1.3 and earlier don't understand EGL client extensions, which
449  * means you can't call "eglGetPlatformDisplayEXT" directly, as the resolver
450  * will crash.
451  *
452  * 3: You can't tell whether you have EGL 1.5 at this point, because
453  * eglQueryString(EGL_VERSION) is a property of the display, which we don't
454  * have yet. So you have to query for extensions no matter what. Fortunately
455  * epoxy_has_egl_extension _does_ let you query for client extensions, so
456  * we don't have to write our own extension string parsing.
457  *
458  * 4. There is no EGL_KHR_platform_base to complement the EXT one, thus one
459  * needs to know EGL 1.5 is supported in order to use the eglGetPlatformDisplay
460  * function pointer.
461  * We can workaround this (circular dependency) by probing for the EGL 1.5
462  * platform extensions (EGL_KHR_platform_gbm and friends) yet it doesn't seem
463  * like mesa will be able to advertise these (even though it can do EGL 1.5).
464  */
465 static EGLDisplay qemu_egl_get_display(EGLNativeDisplayType native,
466                                        EGLenum platform)
467 {
468     EGLDisplay dpy = EGL_NO_DISPLAY;
469 
470     /* In practise any EGL 1.5 implementation would support the EXT extension */
471     if (epoxy_has_egl_extension(NULL, "EGL_EXT_platform_base")) {
472         if (platform != 0) {
473             dpy = eglGetPlatformDisplayEXT(platform, native, NULL);
474         }
475     }
476 
477     if (dpy == EGL_NO_DISPLAY) {
478         /* fallback */
479         dpy = eglGetDisplay(native);
480     }
481     return dpy;
482 }
483 
484 static int qemu_egl_init_dpy(EGLNativeDisplayType dpy,
485                              EGLenum platform,
486                              DisplayGLMode mode)
487 {
488     static const EGLint conf_att_core[] = {
489         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
490         EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
491         EGL_RED_SIZE,   5,
492         EGL_GREEN_SIZE, 5,
493         EGL_BLUE_SIZE,  5,
494         EGL_ALPHA_SIZE, 0,
495         EGL_NONE,
496     };
497     static const EGLint conf_att_gles[] = {
498         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
499         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
500         EGL_RED_SIZE,   5,
501         EGL_GREEN_SIZE, 5,
502         EGL_BLUE_SIZE,  5,
503         EGL_ALPHA_SIZE, 0,
504         EGL_NONE,
505     };
506     EGLint major, minor;
507     EGLBoolean b;
508     EGLint n;
509     bool gles = (mode == DISPLAY_GL_MODE_ES);
510 
511     qemu_egl_display = qemu_egl_get_display(dpy, platform);
512     if (qemu_egl_display == EGL_NO_DISPLAY) {
513         error_report("egl: eglGetDisplay failed: %s", qemu_egl_get_error_string());
514         return -1;
515     }
516 
517     b = eglInitialize(qemu_egl_display, &major, &minor);
518     if (b == EGL_FALSE) {
519         error_report("egl: eglInitialize failed: %s", qemu_egl_get_error_string());
520         return -1;
521     }
522 
523     b = eglBindAPI(gles ?  EGL_OPENGL_ES_API : EGL_OPENGL_API);
524     if (b == EGL_FALSE) {
525         error_report("egl: eglBindAPI failed (%s mode): %s",
526                      gles ? "gles" : "core", qemu_egl_get_error_string());
527         return -1;
528     }
529 
530     b = eglChooseConfig(qemu_egl_display,
531                         gles ? conf_att_gles : conf_att_core,
532                         &qemu_egl_config, 1, &n);
533     if (b == EGL_FALSE || n != 1) {
534         error_report("egl: eglChooseConfig failed (%s mode): %s",
535                      gles ? "gles" : "core", qemu_egl_get_error_string());
536         return -1;
537     }
538 
539     qemu_egl_mode = gles ? DISPLAY_GL_MODE_ES : DISPLAY_GL_MODE_CORE;
540     return 0;
541 }
542 
543 #endif
544 
545 #if defined(CONFIG_X11) || defined(CONFIG_GBM)
546 int qemu_egl_init_dpy_x11(EGLNativeDisplayType dpy, DisplayGLMode mode)
547 {
548 #ifdef EGL_KHR_platform_x11
549     return qemu_egl_init_dpy(dpy, EGL_PLATFORM_X11_KHR, mode);
550 #else
551     return qemu_egl_init_dpy(dpy, 0, mode);
552 #endif
553 }
554 
555 int qemu_egl_init_dpy_mesa(EGLNativeDisplayType dpy, DisplayGLMode mode)
556 {
557 #ifdef EGL_MESA_platform_gbm
558     return qemu_egl_init_dpy(dpy, EGL_PLATFORM_GBM_MESA, mode);
559 #else
560     return qemu_egl_init_dpy(dpy, 0, mode);
561 #endif
562 }
563 #endif
564 
565 
566 #ifdef WIN32
567 int qemu_egl_init_dpy_win32(EGLNativeDisplayType dpy, DisplayGLMode mode)
568 {
569     /* prefer GL ES, as that's what ANGLE supports */
570     if (mode == DISPLAY_GL_MODE_ON) {
571         mode = DISPLAY_GL_MODE_ES;
572     }
573 
574     if (qemu_egl_init_dpy(dpy, 0, mode) < 0) {
575         return -1;
576     }
577 
578 #ifdef EGL_D3D11_DEVICE_ANGLE
579     if (epoxy_has_egl_extension(qemu_egl_display, "EGL_EXT_device_query")) {
580         EGLDeviceEXT device;
581         void *d3d11_device;
582 
583         if (!eglQueryDisplayAttribEXT(qemu_egl_display,
584                                       EGL_DEVICE_EXT,
585                                       (EGLAttrib *)&device)) {
586             return 0;
587         }
588 
589         if (!eglQueryDeviceAttribEXT(device,
590                                      EGL_D3D11_DEVICE_ANGLE,
591                                      (EGLAttrib *)&d3d11_device)) {
592             return 0;
593         }
594 
595         trace_egl_init_d3d11_device(device);
596         qemu_egl_angle_d3d = device != NULL;
597     }
598 #endif
599 
600     return 0;
601 }
602 #endif
603 
604 bool qemu_egl_has_dmabuf(void)
605 {
606     if (qemu_egl_display == EGL_NO_DISPLAY) {
607         return false;
608     }
609 
610     return epoxy_has_egl_extension(qemu_egl_display,
611                                    "EGL_EXT_image_dma_buf_import");
612 }
613 
614 EGLContext qemu_egl_init_ctx(void)
615 {
616     static const EGLint ctx_att_core[] = {
617         EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT,
618         EGL_NONE
619     };
620     static const EGLint ctx_att_gles[] = {
621         EGL_CONTEXT_CLIENT_VERSION, 2,
622         EGL_NONE
623     };
624     bool gles = (qemu_egl_mode == DISPLAY_GL_MODE_ES);
625     EGLContext ectx;
626     EGLBoolean b;
627 
628     ectx = eglCreateContext(qemu_egl_display, qemu_egl_config, EGL_NO_CONTEXT,
629                             gles ? ctx_att_gles : ctx_att_core);
630     if (ectx == EGL_NO_CONTEXT) {
631         error_report("egl: eglCreateContext failed");
632         return NULL;
633     }
634 
635     b = eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, ectx);
636     if (b == EGL_FALSE) {
637         error_report("egl: eglMakeCurrent failed");
638         return NULL;
639     }
640 
641     return ectx;
642 }
643 
644 bool egl_init(const char *rendernode, DisplayGLMode mode, Error **errp)
645 {
646     ERRP_GUARD();
647 
648     if (mode == DISPLAY_GL_MODE_OFF) {
649         error_setg(errp, "egl: turning off GL doesn't make sense");
650         return false;
651     }
652 
653 #ifdef WIN32
654     if (qemu_egl_init_dpy_win32(EGL_DEFAULT_DISPLAY, mode) < 0) {
655         error_setg(errp, "egl: init failed");
656         return false;
657     }
658     qemu_egl_rn_ctx = qemu_egl_init_ctx();
659     if (!qemu_egl_rn_ctx) {
660         error_setg(errp, "egl: egl_init_ctx failed");
661         return false;
662     }
663 #elif defined(CONFIG_GBM)
664     if (egl_rendernode_init(rendernode, mode) < 0) {
665         error_setg(errp, "egl: render node init failed");
666         return false;
667     }
668 #endif
669 
670     if (!qemu_egl_rn_ctx) {
671         error_setg(errp, "egl: not available on this platform");
672         return false;
673     }
674 
675     display_opengl = 1;
676     return true;
677 }
678