2 * Copyright (C) 2015-2016 Gerd Hoffmann <kraxel@redhat.com>
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.
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.
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/>.
17 #include "qemu/osdep.h"
19 #include "qemu/error-report.h"
20 #include "ui/console.h"
21 #include "ui/egl-helpers.h"
22 #include "sysemu/sysemu.h"
23 #include "qapi/error.h"
25 EGLDisplay
*qemu_egl_display
;
26 EGLConfig qemu_egl_config
;
27 DisplayGLMode qemu_egl_mode
;
29 /* ------------------------------------------------------------------ */
31 const char *qemu_egl_get_error_string(void)
33 EGLint error
= eglGetError();
38 case EGL_NOT_INITIALIZED
:
39 return "EGL_NOT_INITIALIZED";
41 return "EGL_BAD_ACCESS";
43 return "EGL_BAD_ALLOC";
44 case EGL_BAD_ATTRIBUTE
:
45 return "EGL_BAD_ATTRIBUTE";
47 return "EGL_BAD_CONTEXT";
49 return "EGL_BAD_CONFIG";
50 case EGL_BAD_CURRENT_SURFACE
:
51 return "EGL_BAD_CURRENT_SURFACE";
53 return "EGL_BAD_DISPLAY";
55 return "EGL_BAD_SURFACE";
57 return "EGL_BAD_MATCH";
58 case EGL_BAD_PARAMETER
:
59 return "EGL_BAD_PARAMETER";
60 case EGL_BAD_NATIVE_PIXMAP
:
61 return "EGL_BAD_NATIVE_PIXMAP";
62 case EGL_BAD_NATIVE_WINDOW
:
63 return "EGL_BAD_NATIVE_WINDOW";
64 case EGL_CONTEXT_LOST
:
65 return "EGL_CONTEXT_LOST";
67 return "Unknown EGL error";
71 static void egl_fb_delete_texture(egl_fb
*fb
)
73 if (!fb
->delete_texture
) {
77 glDeleteTextures(1, &fb
->texture
);
78 fb
->delete_texture
= false;
81 void egl_fb_destroy(egl_fb
*fb
)
83 if (!fb
->framebuffer
) {
87 egl_fb_delete_texture(fb
);
88 glDeleteFramebuffers(1, &fb
->framebuffer
);
96 void egl_fb_setup_default(egl_fb
*fb
, int width
, int height
)
100 fb
->framebuffer
= 0; /* default framebuffer */
103 void egl_fb_setup_for_tex(egl_fb
*fb
, int width
, int height
,
104 GLuint texture
, bool delete)
106 egl_fb_delete_texture(fb
);
110 fb
->texture
= texture
;
111 fb
->delete_texture
= delete;
112 if (!fb
->framebuffer
) {
113 glGenFramebuffers(1, &fb
->framebuffer
);
116 glBindFramebuffer(GL_FRAMEBUFFER_EXT
, fb
->framebuffer
);
117 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
, GL_COLOR_ATTACHMENT0_EXT
,
118 GL_TEXTURE_2D
, fb
->texture
, 0);
121 void egl_fb_setup_new_tex(egl_fb
*fb
, int width
, int height
)
125 glGenTextures(1, &texture
);
126 glBindTexture(GL_TEXTURE_2D
, texture
);
127 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, width
, height
,
128 0, GL_BGRA
, GL_UNSIGNED_BYTE
, 0);
130 egl_fb_setup_for_tex(fb
, width
, height
, texture
, true);
133 void egl_fb_blit(egl_fb
*dst
, egl_fb
*src
, bool flip
)
138 GLuint w
= src
->width
;
139 GLuint h
= src
->height
;
141 glBindFramebuffer(GL_READ_FRAMEBUFFER
, src
->framebuffer
);
142 glBindFramebuffer(GL_DRAW_FRAMEBUFFER
, dst
->framebuffer
);
143 glViewport(0, 0, dst
->width
, dst
->height
);
148 w
= src
->dmabuf
->scanout_width
;
149 h
= src
->dmabuf
->scanout_height
;
152 w
= (x1
+ w
) > src
->width
? src
->width
- x1
: w
;
153 h
= (y1
+ h
) > src
->height
? src
->height
- y1
: h
;
155 y2
= flip
? y1
: h
+ y1
;
156 y1
= flip
? h
+ y1
: y1
;
159 glBlitFramebuffer(x1
, y1
, x2
, y2
,
160 0, 0, dst
->width
, dst
->height
,
161 GL_COLOR_BUFFER_BIT
, GL_LINEAR
);
164 void egl_fb_read(DisplaySurface
*dst
, egl_fb
*src
)
166 glBindFramebuffer(GL_READ_FRAMEBUFFER
, src
->framebuffer
);
167 glReadBuffer(GL_COLOR_ATTACHMENT0_EXT
);
168 glReadPixels(0, 0, surface_width(dst
), surface_height(dst
),
169 GL_BGRA
, GL_UNSIGNED_BYTE
, surface_data(dst
));
172 void egl_fb_read_rect(DisplaySurface
*dst
, egl_fb
*src
, int x
, int y
, int w
, int h
)
174 assert(surface_width(dst
) == src
->width
);
175 assert(surface_height(dst
) == src
->height
);
176 assert(surface_format(dst
) == PIXMAN_x8r8g8b8
);
178 glBindFramebuffer(GL_READ_FRAMEBUFFER
, src
->framebuffer
);
179 glReadBuffer(GL_COLOR_ATTACHMENT0_EXT
);
180 glPixelStorei(GL_PACK_ROW_LENGTH
, surface_stride(dst
) / 4);
181 glReadPixels(x
, y
, w
, h
,
182 GL_BGRA
, GL_UNSIGNED_BYTE
, surface_data(dst
) + x
* 4);
183 glPixelStorei(GL_PACK_ROW_LENGTH
, 0);
186 void egl_texture_blit(QemuGLShader
*gls
, egl_fb
*dst
, egl_fb
*src
, bool flip
)
188 glBindFramebuffer(GL_FRAMEBUFFER_EXT
, dst
->framebuffer
);
189 glViewport(0, 0, dst
->width
, dst
->height
);
190 glEnable(GL_TEXTURE_2D
);
191 glBindTexture(GL_TEXTURE_2D
, src
->texture
);
192 qemu_gl_run_texture_blit(gls
, flip
);
195 void egl_texture_blend(QemuGLShader
*gls
, egl_fb
*dst
, egl_fb
*src
, bool flip
,
196 int x
, int y
, double scale_x
, double scale_y
)
198 glBindFramebuffer(GL_FRAMEBUFFER_EXT
, dst
->framebuffer
);
199 int w
= scale_x
* src
->width
;
200 int h
= scale_y
* src
->height
;
202 glViewport(x
, y
, w
, h
);
204 glViewport(x
, dst
->height
- h
- y
, w
, h
);
206 glEnable(GL_TEXTURE_2D
);
207 glBindTexture(GL_TEXTURE_2D
, src
->texture
);
209 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
210 qemu_gl_run_texture_blit(gls
, flip
);
214 /* ---------------------------------------------------------------------- */
216 EGLContext qemu_egl_rn_ctx
;
221 struct gbm_device
*qemu_egl_rn_gbm_dev
;
223 int egl_rendernode_init(const char *rendernode
, DisplayGLMode mode
)
228 qemu_egl_rn_fd
= qemu_drm_rendernode_open(rendernode
);
229 if (qemu_egl_rn_fd
== -1) {
230 error_report("egl: no drm render node available");
234 qemu_egl_rn_gbm_dev
= gbm_create_device(qemu_egl_rn_fd
);
235 if (!qemu_egl_rn_gbm_dev
) {
236 error_report("egl: gbm_create_device failed");
240 rc
= qemu_egl_init_dpy_mesa((EGLNativeDisplayType
)qemu_egl_rn_gbm_dev
,
243 /* qemu_egl_init_dpy_mesa reports error */
247 if (!epoxy_has_egl_extension(qemu_egl_display
,
248 "EGL_KHR_surfaceless_context")) {
249 error_report("egl: EGL_KHR_surfaceless_context not supported");
252 if (!epoxy_has_egl_extension(qemu_egl_display
,
253 "EGL_MESA_image_dma_buf_export")) {
254 error_report("egl: EGL_MESA_image_dma_buf_export not supported");
258 qemu_egl_rn_ctx
= qemu_egl_init_ctx();
259 if (!qemu_egl_rn_ctx
) {
260 error_report("egl: egl_init_ctx failed");
267 if (qemu_egl_rn_gbm_dev
) {
268 gbm_device_destroy(qemu_egl_rn_gbm_dev
);
270 if (qemu_egl_rn_fd
!= -1) {
271 close(qemu_egl_rn_fd
);
277 int egl_get_fd_for_texture(uint32_t tex_id
, EGLint
*stride
, EGLint
*fourcc
,
278 EGLuint64KHR
*modifier
)
281 EGLint num_planes
, fd
;
283 image
= eglCreateImageKHR(qemu_egl_display
, eglGetCurrentContext(),
284 EGL_GL_TEXTURE_2D_KHR
,
285 (EGLClientBuffer
)(unsigned long)tex_id
,
291 eglExportDMABUFImageQueryMESA(qemu_egl_display
, image
, fourcc
,
292 &num_planes
, modifier
);
293 if (num_planes
!= 1) {
294 eglDestroyImageKHR(qemu_egl_display
, image
);
297 eglExportDMABUFImageMESA(qemu_egl_display
, image
, &fd
, stride
, NULL
);
298 eglDestroyImageKHR(qemu_egl_display
, image
);
303 void egl_dmabuf_import_texture(QemuDmaBuf
*dmabuf
)
305 EGLImageKHR image
= EGL_NO_IMAGE_KHR
;
309 if (dmabuf
->texture
!= 0) {
313 attrs
[i
++] = EGL_WIDTH
;
314 attrs
[i
++] = dmabuf
->width
;
315 attrs
[i
++] = EGL_HEIGHT
;
316 attrs
[i
++] = dmabuf
->height
;
317 attrs
[i
++] = EGL_LINUX_DRM_FOURCC_EXT
;
318 attrs
[i
++] = dmabuf
->fourcc
;
320 attrs
[i
++] = EGL_DMA_BUF_PLANE0_FD_EXT
;
321 attrs
[i
++] = dmabuf
->fd
;
322 attrs
[i
++] = EGL_DMA_BUF_PLANE0_PITCH_EXT
;
323 attrs
[i
++] = dmabuf
->stride
;
324 attrs
[i
++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT
;
326 #ifdef EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT
327 if (dmabuf
->modifier
) {
328 attrs
[i
++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT
;
329 attrs
[i
++] = (dmabuf
->modifier
>> 0) & 0xffffffff;
330 attrs
[i
++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT
;
331 attrs
[i
++] = (dmabuf
->modifier
>> 32) & 0xffffffff;
334 attrs
[i
++] = EGL_NONE
;
336 image
= eglCreateImageKHR(qemu_egl_display
,
338 EGL_LINUX_DMA_BUF_EXT
,
340 if (image
== EGL_NO_IMAGE_KHR
) {
341 error_report("eglCreateImageKHR failed");
345 glGenTextures(1, &dmabuf
->texture
);
346 glBindTexture(GL_TEXTURE_2D
, dmabuf
->texture
);
347 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
348 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
350 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D
, (GLeglImageOES
)image
);
351 eglDestroyImageKHR(qemu_egl_display
, image
);
354 void egl_dmabuf_release_texture(QemuDmaBuf
*dmabuf
)
356 if (dmabuf
->texture
== 0) {
360 glDeleteTextures(1, &dmabuf
->texture
);
364 void egl_dmabuf_create_sync(QemuDmaBuf
*dmabuf
)
368 if (epoxy_has_egl_extension(qemu_egl_display
,
369 "EGL_KHR_fence_sync") &&
370 epoxy_has_egl_extension(qemu_egl_display
,
371 "EGL_ANDROID_native_fence_sync")) {
372 sync
= eglCreateSyncKHR(qemu_egl_display
,
373 EGL_SYNC_NATIVE_FENCE_ANDROID
, NULL
);
374 if (sync
!= EGL_NO_SYNC_KHR
) {
380 void egl_dmabuf_create_fence(QemuDmaBuf
*dmabuf
)
383 dmabuf
->fence_fd
= eglDupNativeFenceFDANDROID(qemu_egl_display
,
385 eglDestroySyncKHR(qemu_egl_display
, dmabuf
->sync
);
390 #endif /* CONFIG_GBM */
392 /* ---------------------------------------------------------------------- */
394 EGLSurface
qemu_egl_init_surface_x11(EGLContext ectx
, EGLNativeWindowType win
)
399 esurface
= eglCreateWindowSurface(qemu_egl_display
,
402 if (esurface
== EGL_NO_SURFACE
) {
403 error_report("egl: eglCreateWindowSurface failed");
407 b
= eglMakeCurrent(qemu_egl_display
, esurface
, esurface
, ectx
);
408 if (b
== EGL_FALSE
) {
409 error_report("egl: eglMakeCurrent failed");
416 /* ---------------------------------------------------------------------- */
418 #if defined(CONFIG_X11) || defined(CONFIG_GBM) || defined(WIN32)
421 * Taken from glamor_egl.h from the Xorg xserver, which is MIT licensed
423 * Create an EGLDisplay from a native display type. This is a little quirky
426 * 1: GetPlatformDisplayEXT and GetPlatformDisplay are the API you want to
427 * use, but have different function signatures in the third argument; this
428 * happens not to matter for us, at the moment, but it means epoxy won't alias
431 * 2: epoxy 1.3 and earlier don't understand EGL client extensions, which
432 * means you can't call "eglGetPlatformDisplayEXT" directly, as the resolver
435 * 3: You can't tell whether you have EGL 1.5 at this point, because
436 * eglQueryString(EGL_VERSION) is a property of the display, which we don't
437 * have yet. So you have to query for extensions no matter what. Fortunately
438 * epoxy_has_egl_extension _does_ let you query for client extensions, so
439 * we don't have to write our own extension string parsing.
441 * 4. There is no EGL_KHR_platform_base to complement the EXT one, thus one
442 * needs to know EGL 1.5 is supported in order to use the eglGetPlatformDisplay
444 * We can workaround this (circular dependency) by probing for the EGL 1.5
445 * platform extensions (EGL_KHR_platform_gbm and friends) yet it doesn't seem
446 * like mesa will be able to advertise these (even though it can do EGL 1.5).
448 static EGLDisplay
qemu_egl_get_display(EGLNativeDisplayType native
,
451 EGLDisplay dpy
= EGL_NO_DISPLAY
;
453 /* In practise any EGL 1.5 implementation would support the EXT extension */
454 if (epoxy_has_egl_extension(NULL
, "EGL_EXT_platform_base")) {
456 dpy
= eglGetPlatformDisplayEXT(platform
, native
, NULL
);
460 if (dpy
== EGL_NO_DISPLAY
) {
462 dpy
= eglGetDisplay(native
);
467 static int qemu_egl_init_dpy(EGLNativeDisplayType dpy
,
471 static const EGLint conf_att_core
[] = {
472 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
473 EGL_RENDERABLE_TYPE
, EGL_OPENGL_BIT
,
480 static const EGLint conf_att_gles
[] = {
481 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
482 EGL_RENDERABLE_TYPE
, EGL_OPENGL_ES2_BIT
,
492 bool gles
= (mode
== DISPLAYGL_MODE_ES
);
494 qemu_egl_display
= qemu_egl_get_display(dpy
, platform
);
495 if (qemu_egl_display
== EGL_NO_DISPLAY
) {
496 error_report("egl: eglGetDisplay failed: %s", qemu_egl_get_error_string());
500 b
= eglInitialize(qemu_egl_display
, &major
, &minor
);
501 if (b
== EGL_FALSE
) {
502 error_report("egl: eglInitialize failed: %s", qemu_egl_get_error_string());
506 b
= eglBindAPI(gles
? EGL_OPENGL_ES_API
: EGL_OPENGL_API
);
507 if (b
== EGL_FALSE
) {
508 error_report("egl: eglBindAPI failed (%s mode): %s",
509 gles
? "gles" : "core", qemu_egl_get_error_string());
513 b
= eglChooseConfig(qemu_egl_display
,
514 gles
? conf_att_gles
: conf_att_core
,
515 &qemu_egl_config
, 1, &n
);
516 if (b
== EGL_FALSE
|| n
!= 1) {
517 error_report("egl: eglChooseConfig failed (%s mode): %s",
518 gles
? "gles" : "core", qemu_egl_get_error_string());
522 qemu_egl_mode
= gles
? DISPLAYGL_MODE_ES
: DISPLAYGL_MODE_CORE
;
528 #if defined(CONFIG_X11) || defined(CONFIG_GBM)
529 int qemu_egl_init_dpy_x11(EGLNativeDisplayType dpy
, DisplayGLMode mode
)
531 #ifdef EGL_KHR_platform_x11
532 return qemu_egl_init_dpy(dpy
, EGL_PLATFORM_X11_KHR
, mode
);
534 return qemu_egl_init_dpy(dpy
, 0, mode
);
538 int qemu_egl_init_dpy_mesa(EGLNativeDisplayType dpy
, DisplayGLMode mode
)
540 #ifdef EGL_MESA_platform_gbm
541 return qemu_egl_init_dpy(dpy
, EGL_PLATFORM_GBM_MESA
, mode
);
543 return qemu_egl_init_dpy(dpy
, 0, mode
);
550 int qemu_egl_init_dpy_win32(EGLNativeDisplayType dpy
, DisplayGLMode mode
)
552 /* prefer GL ES, as that's what ANGLE supports */
553 if (mode
== DISPLAYGL_MODE_ON
) {
554 mode
= DISPLAYGL_MODE_ES
;
556 return qemu_egl_init_dpy(dpy
, 0, mode
);
560 bool qemu_egl_has_dmabuf(void)
562 if (qemu_egl_display
== EGL_NO_DISPLAY
) {
566 return epoxy_has_egl_extension(qemu_egl_display
,
567 "EGL_EXT_image_dma_buf_import");
570 EGLContext
qemu_egl_init_ctx(void)
572 static const EGLint ctx_att_core
[] = {
573 EGL_CONTEXT_OPENGL_PROFILE_MASK
, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT
,
576 static const EGLint ctx_att_gles
[] = {
577 EGL_CONTEXT_CLIENT_VERSION
, 2,
580 bool gles
= (qemu_egl_mode
== DISPLAYGL_MODE_ES
);
584 ectx
= eglCreateContext(qemu_egl_display
, qemu_egl_config
, EGL_NO_CONTEXT
,
585 gles
? ctx_att_gles
: ctx_att_core
);
586 if (ectx
== EGL_NO_CONTEXT
) {
587 error_report("egl: eglCreateContext failed");
591 b
= eglMakeCurrent(qemu_egl_display
, EGL_NO_SURFACE
, EGL_NO_SURFACE
, ectx
);
592 if (b
== EGL_FALSE
) {
593 error_report("egl: eglMakeCurrent failed");
600 bool egl_init(const char *rendernode
, DisplayGLMode mode
, Error
**errp
)
604 if (mode
== DISPLAYGL_MODE_OFF
) {
605 error_setg(errp
, "egl: turning off GL doesn't make sense");
610 if (qemu_egl_init_dpy_win32(EGL_DEFAULT_DISPLAY
, mode
) < 0) {
611 error_setg(errp
, "egl: init failed");
614 qemu_egl_rn_ctx
= qemu_egl_init_ctx();
615 if (!qemu_egl_rn_ctx
) {
616 error_setg(errp
, "egl: egl_init_ctx failed");
619 #elif defined(CONFIG_GBM)
620 if (egl_rendernode_init(rendernode
, mode
) < 0) {
621 error_setg(errp
, "egl: render node init failed");
626 if (!qemu_egl_rn_ctx
) {
627 error_setg(errp
, "egl: not available on this platform");