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