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"
21 #include "qemu/error-report.h"
22 #include "ui/console.h"
23 #include "ui/egl-helpers.h"
25 EGLDisplay
*qemu_egl_display
;
26 EGLConfig qemu_egl_config
;
28 /* ------------------------------------------------------------------ */
30 static void egl_fb_delete_texture(egl_fb
*fb
)
32 if (!fb
->delete_texture
) {
36 glDeleteTextures(1, &fb
->texture
);
37 fb
->delete_texture
= false;
40 void egl_fb_destroy(egl_fb
*fb
)
42 if (!fb
->framebuffer
) {
46 egl_fb_delete_texture(fb
);
47 glDeleteFramebuffers(1, &fb
->framebuffer
);
55 void egl_fb_setup_default(egl_fb
*fb
, int width
, int height
)
59 fb
->framebuffer
= 0; /* default framebuffer */
62 void egl_fb_setup_for_tex(egl_fb
*fb
, int width
, int height
,
63 GLuint texture
, bool delete)
65 egl_fb_delete_texture(fb
);
69 fb
->texture
= texture
;
70 fb
->delete_texture
= delete;
71 if (!fb
->framebuffer
) {
72 glGenFramebuffers(1, &fb
->framebuffer
);
75 glBindFramebuffer(GL_FRAMEBUFFER_EXT
, fb
->framebuffer
);
76 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
, GL_COLOR_ATTACHMENT0_EXT
,
77 GL_TEXTURE_2D
, fb
->texture
, 0);
80 void egl_fb_setup_new_tex(egl_fb
*fb
, int width
, int height
)
84 glGenTextures(1, &texture
);
85 glBindTexture(GL_TEXTURE_2D
, texture
);
86 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, width
, height
,
87 0, GL_BGRA
, GL_UNSIGNED_BYTE
, 0);
89 egl_fb_setup_for_tex(fb
, width
, height
, texture
, true);
92 void egl_fb_blit(egl_fb
*dst
, egl_fb
*src
, bool flip
)
96 glBindFramebuffer(GL_READ_FRAMEBUFFER
, src
->framebuffer
);
97 glBindFramebuffer(GL_DRAW_FRAMEBUFFER
, dst
->framebuffer
);
98 glViewport(0, 0, dst
->width
, dst
->height
);
99 y1
= flip
? src
->height
: 0;
100 y2
= flip
? 0 : src
->height
;
101 glBlitFramebuffer(0, y1
, src
->width
, y2
,
102 0, 0, dst
->width
, dst
->height
,
103 GL_COLOR_BUFFER_BIT
, GL_LINEAR
);
106 void egl_fb_read(void *dst
, egl_fb
*src
)
108 glBindFramebuffer(GL_READ_FRAMEBUFFER
, src
->framebuffer
);
109 glReadBuffer(GL_COLOR_ATTACHMENT0_EXT
);
110 glReadPixels(0, 0, src
->width
, src
->height
,
111 GL_BGRA
, GL_UNSIGNED_BYTE
, dst
);
114 void egl_texture_blit(QemuGLShader
*gls
, egl_fb
*dst
, egl_fb
*src
, bool flip
)
116 glBindFramebuffer(GL_FRAMEBUFFER_EXT
, dst
->framebuffer
);
117 glViewport(0, 0, dst
->width
, dst
->height
);
118 glEnable(GL_TEXTURE_2D
);
119 glBindTexture(GL_TEXTURE_2D
, src
->texture
);
120 qemu_gl_run_texture_blit(gls
, flip
);
123 void egl_texture_blend(QemuGLShader
*gls
, egl_fb
*dst
, egl_fb
*src
, bool flip
,
126 glBindFramebuffer(GL_FRAMEBUFFER_EXT
, dst
->framebuffer
);
128 glViewport(x
, y
, src
->width
, src
->height
);
130 glViewport(x
, dst
->height
- src
->height
- y
,
131 src
->width
, src
->height
);
133 glEnable(GL_TEXTURE_2D
);
134 glBindTexture(GL_TEXTURE_2D
, src
->texture
);
136 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
137 qemu_gl_run_texture_blit(gls
, flip
);
141 /* ---------------------------------------------------------------------- */
143 #ifdef CONFIG_OPENGL_DMABUF
146 struct gbm_device
*qemu_egl_rn_gbm_dev
;
147 EGLContext qemu_egl_rn_ctx
;
149 static int qemu_egl_rendernode_open(const char *rendernode
)
157 return open(rendernode
, O_RDWR
| O_CLOEXEC
| O_NOCTTY
| O_NONBLOCK
);
160 dir
= opendir("/dev/dri");
166 while ((e
= readdir(dir
))) {
167 if (e
->d_type
!= DT_CHR
) {
171 if (strncmp(e
->d_name
, "renderD", 7)) {
175 p
= g_strdup_printf("/dev/dri/%s", e
->d_name
);
177 r
= open(p
, O_RDWR
| O_CLOEXEC
| O_NOCTTY
| O_NONBLOCK
);
194 int egl_rendernode_init(const char *rendernode
)
199 qemu_egl_rn_fd
= qemu_egl_rendernode_open(rendernode
);
200 if (qemu_egl_rn_fd
== -1) {
201 error_report("egl: no drm render node available");
205 qemu_egl_rn_gbm_dev
= gbm_create_device(qemu_egl_rn_fd
);
206 if (!qemu_egl_rn_gbm_dev
) {
207 error_report("egl: gbm_create_device failed");
211 rc
= qemu_egl_init_dpy_mesa((EGLNativeDisplayType
)qemu_egl_rn_gbm_dev
);
213 /* qemu_egl_init_dpy_mesa reports error */
217 if (!epoxy_has_egl_extension(qemu_egl_display
,
218 "EGL_KHR_surfaceless_context")) {
219 error_report("egl: EGL_KHR_surfaceless_context not supported");
222 if (!epoxy_has_egl_extension(qemu_egl_display
,
223 "EGL_MESA_image_dma_buf_export")) {
224 error_report("egl: EGL_MESA_image_dma_buf_export not supported");
228 qemu_egl_rn_ctx
= qemu_egl_init_ctx();
229 if (!qemu_egl_rn_ctx
) {
230 error_report("egl: egl_init_ctx failed");
237 if (qemu_egl_rn_gbm_dev
) {
238 gbm_device_destroy(qemu_egl_rn_gbm_dev
);
240 if (qemu_egl_rn_fd
!= -1) {
241 close(qemu_egl_rn_fd
);
247 int egl_get_fd_for_texture(uint32_t tex_id
, EGLint
*stride
, EGLint
*fourcc
)
250 EGLint num_planes
, fd
;
252 image
= eglCreateImageKHR(qemu_egl_display
, eglGetCurrentContext(),
253 EGL_GL_TEXTURE_2D_KHR
,
254 (EGLClientBuffer
)(unsigned long)tex_id
,
260 eglExportDMABUFImageQueryMESA(qemu_egl_display
, image
, fourcc
,
262 if (num_planes
!= 1) {
263 eglDestroyImageKHR(qemu_egl_display
, image
);
266 eglExportDMABUFImageMESA(qemu_egl_display
, image
, &fd
, stride
, NULL
);
267 eglDestroyImageKHR(qemu_egl_display
, image
);
272 void egl_dmabuf_import_texture(QemuDmaBuf
*dmabuf
)
274 EGLImageKHR image
= EGL_NO_IMAGE_KHR
;
276 EGL_DMA_BUF_PLANE0_FD_EXT
, dmabuf
->fd
,
277 EGL_DMA_BUF_PLANE0_PITCH_EXT
, dmabuf
->stride
,
278 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, 0,
279 EGL_WIDTH
, dmabuf
->width
,
280 EGL_HEIGHT
, dmabuf
->height
,
281 EGL_LINUX_DRM_FOURCC_EXT
, dmabuf
->fourcc
,
282 EGL_NONE
, /* end of list */
285 if (dmabuf
->texture
!= 0) {
289 image
= eglCreateImageKHR(qemu_egl_display
,
291 EGL_LINUX_DMA_BUF_EXT
,
293 if (image
== EGL_NO_IMAGE_KHR
) {
294 error_report("eglCreateImageKHR failed");
298 glGenTextures(1, &dmabuf
->texture
);
299 glBindTexture(GL_TEXTURE_2D
, dmabuf
->texture
);
300 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
301 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
303 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D
, (GLeglImageOES
)image
);
304 eglDestroyImageKHR(qemu_egl_display
, image
);
307 void egl_dmabuf_release_texture(QemuDmaBuf
*dmabuf
)
309 if (dmabuf
->texture
== 0) {
313 glDeleteTextures(1, &dmabuf
->texture
);
317 #endif /* CONFIG_OPENGL_DMABUF */
319 /* ---------------------------------------------------------------------- */
321 EGLSurface
qemu_egl_init_surface_x11(EGLContext ectx
, Window win
)
326 esurface
= eglCreateWindowSurface(qemu_egl_display
,
328 (EGLNativeWindowType
)win
, NULL
);
329 if (esurface
== EGL_NO_SURFACE
) {
330 error_report("egl: eglCreateWindowSurface failed");
334 b
= eglMakeCurrent(qemu_egl_display
, esurface
, esurface
, ectx
);
335 if (b
== EGL_FALSE
) {
336 error_report("egl: eglMakeCurrent failed");
343 /* ---------------------------------------------------------------------- */
346 * Taken from glamor_egl.h from the Xorg xserver, which is MIT licensed
348 * Create an EGLDisplay from a native display type. This is a little quirky
351 * 1: GetPlatformDisplayEXT and GetPlatformDisplay are the API you want to
352 * use, but have different function signatures in the third argument; this
353 * happens not to matter for us, at the moment, but it means epoxy won't alias
356 * 2: epoxy 1.3 and earlier don't understand EGL client extensions, which
357 * means you can't call "eglGetPlatformDisplayEXT" directly, as the resolver
360 * 3: You can't tell whether you have EGL 1.5 at this point, because
361 * eglQueryString(EGL_VERSION) is a property of the display, which we don't
362 * have yet. So you have to query for extensions no matter what. Fortunately
363 * epoxy_has_egl_extension _does_ let you query for client extensions, so
364 * we don't have to write our own extension string parsing.
366 * 4. There is no EGL_KHR_platform_base to complement the EXT one, thus one
367 * needs to know EGL 1.5 is supported in order to use the eglGetPlatformDisplay
369 * We can workaround this (circular dependency) by probing for the EGL 1.5
370 * platform extensions (EGL_KHR_platform_gbm and friends) yet it doesn't seem
371 * like mesa will be able to advertise these (even though it can do EGL 1.5).
373 static EGLDisplay
qemu_egl_get_display(EGLNativeDisplayType native
,
376 EGLDisplay dpy
= EGL_NO_DISPLAY
;
378 /* In practise any EGL 1.5 implementation would support the EXT extension */
379 if (epoxy_has_egl_extension(NULL
, "EGL_EXT_platform_base")) {
380 PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplayEXT
=
381 (void *) eglGetProcAddress("eglGetPlatformDisplayEXT");
382 if (getPlatformDisplayEXT
&& platform
!= 0) {
383 dpy
= getPlatformDisplayEXT(platform
, native
, NULL
);
387 if (dpy
== EGL_NO_DISPLAY
) {
389 dpy
= eglGetDisplay(native
);
394 static int qemu_egl_init_dpy(EGLNativeDisplayType dpy
,
397 static const EGLint conf_att_gl
[] = {
398 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
399 EGL_RENDERABLE_TYPE
, EGL_OPENGL_BIT
,
410 qemu_egl_display
= qemu_egl_get_display(dpy
, platform
);
411 if (qemu_egl_display
== EGL_NO_DISPLAY
) {
412 error_report("egl: eglGetDisplay failed");
416 b
= eglInitialize(qemu_egl_display
, &major
, &minor
);
417 if (b
== EGL_FALSE
) {
418 error_report("egl: eglInitialize failed");
422 b
= eglBindAPI(EGL_OPENGL_API
);
423 if (b
== EGL_FALSE
) {
424 error_report("egl: eglBindAPI failed");
428 b
= eglChooseConfig(qemu_egl_display
, conf_att_gl
,
429 &qemu_egl_config
, 1, &n
);
430 if (b
== EGL_FALSE
|| n
!= 1) {
431 error_report("egl: eglChooseConfig failed");
437 int qemu_egl_init_dpy_x11(EGLNativeDisplayType dpy
)
439 #ifdef EGL_KHR_platform_x11
440 return qemu_egl_init_dpy(dpy
, EGL_PLATFORM_X11_KHR
);
442 return qemu_egl_init_dpy(dpy
, 0);
446 int qemu_egl_init_dpy_mesa(EGLNativeDisplayType dpy
)
448 #ifdef EGL_MESA_platform_gbm
449 return qemu_egl_init_dpy(dpy
, EGL_PLATFORM_GBM_MESA
);
451 return qemu_egl_init_dpy(dpy
, 0);
455 EGLContext
qemu_egl_init_ctx(void)
457 static const EGLint ctx_att_gl
[] = {
458 EGL_CONTEXT_OPENGL_PROFILE_MASK
, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT
,
464 ectx
= eglCreateContext(qemu_egl_display
, qemu_egl_config
, EGL_NO_CONTEXT
,
466 if (ectx
== EGL_NO_CONTEXT
) {
467 error_report("egl: eglCreateContext failed");
471 b
= eglMakeCurrent(qemu_egl_display
, EGL_NO_SURFACE
, EGL_NO_SURFACE
, ectx
);
472 if (b
== EGL_FALSE
) {
473 error_report("egl: eglMakeCurrent failed");