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/egl-helpers.h"
24 EGLDisplay
*qemu_egl_display
;
25 EGLConfig qemu_egl_config
;
27 /* ------------------------------------------------------------------ */
29 void egl_fb_destroy(egl_fb
*fb
)
31 if (!fb
->framebuffer
) {
35 if (fb
->delete_texture
) {
36 glDeleteTextures(1, &fb
->texture
);
37 fb
->delete_texture
= false;
39 glDeleteFramebuffers(1, &fb
->framebuffer
);
47 void egl_fb_setup_default(egl_fb
*fb
, int width
, int height
)
51 fb
->framebuffer
= 0; /* default framebuffer */
54 void egl_fb_create_for_tex(egl_fb
*fb
, int width
, int height
, GLuint texture
)
58 fb
->texture
= texture
;
59 if (!fb
->framebuffer
) {
60 glGenFramebuffers(1, &fb
->framebuffer
);
63 glBindFramebuffer(GL_FRAMEBUFFER_EXT
, fb
->framebuffer
);
64 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
, GL_COLOR_ATTACHMENT0_EXT
,
65 GL_TEXTURE_2D
, fb
->texture
, 0);
68 void egl_fb_create_new_tex(egl_fb
*fb
, int width
, int height
)
72 glGenTextures(1, &texture
);
73 glBindTexture(GL_TEXTURE_2D
, texture
);
74 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, width
, height
,
75 0, GL_BGRA
, GL_UNSIGNED_BYTE
, 0);
77 egl_fb_create_for_tex(fb
, width
, height
, texture
);
78 fb
->delete_texture
= true;
81 void egl_fb_blit(egl_fb
*dst
, egl_fb
*src
, bool flip
)
85 glBindFramebuffer(GL_READ_FRAMEBUFFER
, src
->framebuffer
);
86 glBindFramebuffer(GL_DRAW_FRAMEBUFFER
, dst
->framebuffer
);
87 glViewport(0, 0, dst
->width
, dst
->height
);
88 y1
= flip
? src
->height
: 0;
89 y2
= flip
? 0 : src
->height
;
90 glBlitFramebuffer(0, y1
, src
->width
, y2
,
91 0, 0, dst
->width
, dst
->height
,
92 GL_COLOR_BUFFER_BIT
, GL_LINEAR
);
95 void egl_fb_read(void *dst
, egl_fb
*src
)
97 glBindFramebuffer(GL_READ_FRAMEBUFFER
, src
->framebuffer
);
98 glReadBuffer(GL_COLOR_ATTACHMENT0_EXT
);
99 glReadPixels(0, 0, src
->width
, src
->height
,
100 GL_BGRA
, GL_UNSIGNED_BYTE
, dst
);
103 /* ---------------------------------------------------------------------- */
105 #ifdef CONFIG_OPENGL_DMABUF
108 struct gbm_device
*qemu_egl_rn_gbm_dev
;
109 EGLContext qemu_egl_rn_ctx
;
111 static int qemu_egl_rendernode_open(const char *rendernode
)
119 return open(rendernode
, O_RDWR
| O_CLOEXEC
| O_NOCTTY
| O_NONBLOCK
);
122 dir
= opendir("/dev/dri");
128 while ((e
= readdir(dir
))) {
129 if (e
->d_type
!= DT_CHR
) {
133 if (strncmp(e
->d_name
, "renderD", 7)) {
137 p
= g_strdup_printf("/dev/dri/%s", e
->d_name
);
139 r
= open(p
, O_RDWR
| O_CLOEXEC
| O_NOCTTY
| O_NONBLOCK
);
156 int egl_rendernode_init(const char *rendernode
)
161 qemu_egl_rn_fd
= qemu_egl_rendernode_open(rendernode
);
162 if (qemu_egl_rn_fd
== -1) {
163 error_report("egl: no drm render node available");
167 qemu_egl_rn_gbm_dev
= gbm_create_device(qemu_egl_rn_fd
);
168 if (!qemu_egl_rn_gbm_dev
) {
169 error_report("egl: gbm_create_device failed");
173 rc
= qemu_egl_init_dpy_mesa((EGLNativeDisplayType
)qemu_egl_rn_gbm_dev
);
175 /* qemu_egl_init_dpy_mesa reports error */
179 if (!epoxy_has_egl_extension(qemu_egl_display
,
180 "EGL_KHR_surfaceless_context")) {
181 error_report("egl: EGL_KHR_surfaceless_context not supported");
184 if (!epoxy_has_egl_extension(qemu_egl_display
,
185 "EGL_MESA_image_dma_buf_export")) {
186 error_report("egl: EGL_MESA_image_dma_buf_export not supported");
190 qemu_egl_rn_ctx
= qemu_egl_init_ctx();
191 if (!qemu_egl_rn_ctx
) {
192 error_report("egl: egl_init_ctx failed");
199 if (qemu_egl_rn_gbm_dev
) {
200 gbm_device_destroy(qemu_egl_rn_gbm_dev
);
202 if (qemu_egl_rn_fd
!= -1) {
203 close(qemu_egl_rn_fd
);
209 int egl_get_fd_for_texture(uint32_t tex_id
, EGLint
*stride
, EGLint
*fourcc
)
212 EGLint num_planes
, fd
;
214 image
= eglCreateImageKHR(qemu_egl_display
, eglGetCurrentContext(),
215 EGL_GL_TEXTURE_2D_KHR
,
216 (EGLClientBuffer
)(unsigned long)tex_id
,
222 eglExportDMABUFImageQueryMESA(qemu_egl_display
, image
, fourcc
,
224 if (num_planes
!= 1) {
225 eglDestroyImageKHR(qemu_egl_display
, image
);
228 eglExportDMABUFImageMESA(qemu_egl_display
, image
, &fd
, stride
, NULL
);
229 eglDestroyImageKHR(qemu_egl_display
, image
);
234 #endif /* CONFIG_OPENGL_DMABUF */
236 /* ---------------------------------------------------------------------- */
238 EGLSurface
qemu_egl_init_surface_x11(EGLContext ectx
, Window win
)
243 esurface
= eglCreateWindowSurface(qemu_egl_display
,
245 (EGLNativeWindowType
)win
, NULL
);
246 if (esurface
== EGL_NO_SURFACE
) {
247 error_report("egl: eglCreateWindowSurface failed");
251 b
= eglMakeCurrent(qemu_egl_display
, esurface
, esurface
, ectx
);
252 if (b
== EGL_FALSE
) {
253 error_report("egl: eglMakeCurrent failed");
260 /* ---------------------------------------------------------------------- */
263 * Taken from glamor_egl.h from the Xorg xserver, which is MIT licensed
265 * Create an EGLDisplay from a native display type. This is a little quirky
268 * 1: GetPlatformDisplayEXT and GetPlatformDisplay are the API you want to
269 * use, but have different function signatures in the third argument; this
270 * happens not to matter for us, at the moment, but it means epoxy won't alias
273 * 2: epoxy 1.3 and earlier don't understand EGL client extensions, which
274 * means you can't call "eglGetPlatformDisplayEXT" directly, as the resolver
277 * 3: You can't tell whether you have EGL 1.5 at this point, because
278 * eglQueryString(EGL_VERSION) is a property of the display, which we don't
279 * have yet. So you have to query for extensions no matter what. Fortunately
280 * epoxy_has_egl_extension _does_ let you query for client extensions, so
281 * we don't have to write our own extension string parsing.
283 * 4. There is no EGL_KHR_platform_base to complement the EXT one, thus one
284 * needs to know EGL 1.5 is supported in order to use the eglGetPlatformDisplay
286 * We can workaround this (circular dependency) by probing for the EGL 1.5
287 * platform extensions (EGL_KHR_platform_gbm and friends) yet it doesn't seem
288 * like mesa will be able to advertise these (even though it can do EGL 1.5).
290 static EGLDisplay
qemu_egl_get_display(EGLNativeDisplayType native
,
293 EGLDisplay dpy
= EGL_NO_DISPLAY
;
295 /* In practise any EGL 1.5 implementation would support the EXT extension */
296 if (epoxy_has_egl_extension(NULL
, "EGL_EXT_platform_base")) {
297 PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplayEXT
=
298 (void *) eglGetProcAddress("eglGetPlatformDisplayEXT");
299 if (getPlatformDisplayEXT
&& platform
!= 0) {
300 dpy
= getPlatformDisplayEXT(platform
, native
, NULL
);
304 if (dpy
== EGL_NO_DISPLAY
) {
306 dpy
= eglGetDisplay(native
);
311 static int qemu_egl_init_dpy(EGLNativeDisplayType dpy
,
314 static const EGLint conf_att_gl
[] = {
315 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
316 EGL_RENDERABLE_TYPE
, EGL_OPENGL_BIT
,
327 qemu_egl_display
= qemu_egl_get_display(dpy
, platform
);
328 if (qemu_egl_display
== EGL_NO_DISPLAY
) {
329 error_report("egl: eglGetDisplay failed");
333 b
= eglInitialize(qemu_egl_display
, &major
, &minor
);
334 if (b
== EGL_FALSE
) {
335 error_report("egl: eglInitialize failed");
339 b
= eglBindAPI(EGL_OPENGL_API
);
340 if (b
== EGL_FALSE
) {
341 error_report("egl: eglBindAPI failed");
345 b
= eglChooseConfig(qemu_egl_display
, conf_att_gl
,
346 &qemu_egl_config
, 1, &n
);
347 if (b
== EGL_FALSE
|| n
!= 1) {
348 error_report("egl: eglChooseConfig failed");
354 int qemu_egl_init_dpy_x11(EGLNativeDisplayType dpy
)
356 #ifdef EGL_KHR_platform_x11
357 return qemu_egl_init_dpy(dpy
, EGL_PLATFORM_X11_KHR
);
359 return qemu_egl_init_dpy(dpy
, 0);
363 int qemu_egl_init_dpy_mesa(EGLNativeDisplayType dpy
)
365 #ifdef EGL_MESA_platform_gbm
366 return qemu_egl_init_dpy(dpy
, EGL_PLATFORM_GBM_MESA
);
368 return qemu_egl_init_dpy(dpy
, 0);
372 EGLContext
qemu_egl_init_ctx(void)
374 static const EGLint ctx_att_gl
[] = {
375 EGL_CONTEXT_OPENGL_PROFILE_MASK
, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT
,
381 ectx
= eglCreateContext(qemu_egl_display
, qemu_egl_config
, EGL_NO_CONTEXT
,
383 if (ectx
== EGL_NO_CONTEXT
) {
384 error_report("egl: eglCreateContext failed");
388 b
= eglMakeCurrent(qemu_egl_display
, EGL_NO_SURFACE
, EGL_NO_SURFACE
, ectx
);
389 if (b
== EGL_FALSE
) {
390 error_report("egl: eglMakeCurrent failed");