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 /* ---------------------------------------------------------------------- */
32 #define egl_dbg(_x ...) \
35 fprintf(stderr, "egl: " _x); \
39 /* ---------------------------------------------------------------------- */
41 #ifdef CONFIG_OPENGL_DMABUF
44 struct gbm_device
*qemu_egl_rn_gbm_dev
;
45 EGLContext qemu_egl_rn_ctx
;
47 static int qemu_egl_rendernode_open(const char *rendernode
)
55 return open(rendernode
, O_RDWR
| O_CLOEXEC
| O_NOCTTY
| O_NONBLOCK
);
58 dir
= opendir("/dev/dri");
64 while ((e
= readdir(dir
))) {
65 if (e
->d_type
!= DT_CHR
) {
69 if (strncmp(e
->d_name
, "renderD", 7)) {
73 p
= g_strdup_printf("/dev/dri/%s", e
->d_name
);
75 r
= open(p
, O_RDWR
| O_CLOEXEC
| O_NOCTTY
| O_NONBLOCK
);
92 int egl_rendernode_init(const char *rendernode
)
96 qemu_egl_rn_fd
= qemu_egl_rendernode_open(rendernode
);
97 if (qemu_egl_rn_fd
== -1) {
98 error_report("egl: no drm render node available");
102 qemu_egl_rn_gbm_dev
= gbm_create_device(qemu_egl_rn_fd
);
103 if (!qemu_egl_rn_gbm_dev
) {
104 error_report("egl: gbm_create_device failed");
108 qemu_egl_init_dpy((EGLNativeDisplayType
)qemu_egl_rn_gbm_dev
, false, false);
110 if (!epoxy_has_egl_extension(qemu_egl_display
,
111 "EGL_KHR_surfaceless_context")) {
112 error_report("egl: EGL_KHR_surfaceless_context not supported");
115 if (!epoxy_has_egl_extension(qemu_egl_display
,
116 "EGL_MESA_image_dma_buf_export")) {
117 error_report("egl: EGL_MESA_image_dma_buf_export not supported");
121 qemu_egl_rn_ctx
= qemu_egl_init_ctx();
122 if (!qemu_egl_rn_ctx
) {
123 error_report("egl: egl_init_ctx failed");
130 if (qemu_egl_rn_gbm_dev
) {
131 gbm_device_destroy(qemu_egl_rn_gbm_dev
);
133 if (qemu_egl_rn_fd
!= -1) {
134 close(qemu_egl_rn_fd
);
140 int egl_get_fd_for_texture(uint32_t tex_id
, EGLint
*stride
, EGLint
*fourcc
)
143 EGLint num_planes
, fd
;
145 image
= eglCreateImageKHR(qemu_egl_display
, eglGetCurrentContext(),
146 EGL_GL_TEXTURE_2D_KHR
,
147 (EGLClientBuffer
)(unsigned long)tex_id
,
153 eglExportDMABUFImageQueryMESA(qemu_egl_display
, image
, fourcc
,
155 if (num_planes
!= 1) {
156 eglDestroyImageKHR(qemu_egl_display
, image
);
159 eglExportDMABUFImageMESA(qemu_egl_display
, image
, &fd
, stride
, NULL
);
160 eglDestroyImageKHR(qemu_egl_display
, image
);
165 #endif /* CONFIG_OPENGL_DMABUF */
167 /* ---------------------------------------------------------------------- */
169 EGLSurface
qemu_egl_init_surface_x11(EGLContext ectx
, Window win
)
174 egl_dbg("eglCreateWindowSurface (x11 win id 0x%lx) ...\n",
175 (unsigned long) win
);
176 esurface
= eglCreateWindowSurface(qemu_egl_display
,
178 (EGLNativeWindowType
)win
, NULL
);
179 if (esurface
== EGL_NO_SURFACE
) {
180 error_report("egl: eglCreateWindowSurface failed");
184 b
= eglMakeCurrent(qemu_egl_display
, esurface
, esurface
, ectx
);
185 if (b
== EGL_FALSE
) {
186 error_report("egl: eglMakeCurrent failed");
193 /* ---------------------------------------------------------------------- */
196 * Taken from glamor_egl.h from the Xorg xserver, which is MIT licensed
198 * Create an EGLDisplay from a native display type. This is a little quirky
201 * 1: GetPlatformDisplayEXT and GetPlatformDisplay are the API you want to
202 * use, but have different function signatures in the third argument; this
203 * happens not to matter for us, at the moment, but it means epoxy won't alias
206 * 2: epoxy 1.3 and earlier don't understand EGL client extensions, which
207 * means you can't call "eglGetPlatformDisplayEXT" directly, as the resolver
210 * 3: You can't tell whether you have EGL 1.5 at this point, because
211 * eglQueryString(EGL_VERSION) is a property of the display, which we don't
212 * have yet. So you have to query for extensions no matter what. Fortunately
213 * epoxy_has_egl_extension _does_ let you query for client extensions, so
214 * we don't have to write our own extension string parsing.
216 * 4. There is no EGL_KHR_platform_base to complement the EXT one, thus one
217 * needs to know EGL 1.5 is supported in order to use the eglGetPlatformDisplay
219 * We can workaround this (circular dependency) by probing for the EGL 1.5
220 * platform extensions (EGL_KHR_platform_gbm and friends) yet it doesn't seem
221 * like mesa will be able to advertise these (even though it can do EGL 1.5).
223 static EGLDisplay
qemu_egl_get_display(void *native
)
225 EGLDisplay dpy
= EGL_NO_DISPLAY
;
227 #ifdef EGL_MESA_platform_gbm
228 /* In practise any EGL 1.5 implementation would support the EXT extension */
229 if (epoxy_has_egl_extension(NULL
, "EGL_EXT_platform_base")) {
230 PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplayEXT
=
231 (void *) eglGetProcAddress("eglGetPlatformDisplayEXT");
232 if (getPlatformDisplayEXT
) {
233 dpy
= getPlatformDisplayEXT(EGL_PLATFORM_GBM_MESA
, native
, NULL
);
238 if (dpy
== EGL_NO_DISPLAY
) {
240 dpy
= eglGetDisplay(native
);
245 int qemu_egl_init_dpy(EGLNativeDisplayType dpy
, bool gles
, bool debug
)
247 static const EGLint conf_att_gl
[] = {
248 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
249 EGL_RENDERABLE_TYPE
, EGL_OPENGL_BIT
,
256 static const EGLint conf_att_gles
[] = {
257 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
258 EGL_RENDERABLE_TYPE
, EGL_OPENGL_ES2_BIT
,
271 setenv("EGL_LOG_LEVEL", "debug", true);
272 setenv("LIBGL_DEBUG", "verbose", true);
275 egl_dbg("qemu_egl_get_display (dpy %p) ...\n", dpy
);
276 qemu_egl_display
= qemu_egl_get_display(dpy
);
277 if (qemu_egl_display
== EGL_NO_DISPLAY
) {
278 error_report("egl: eglGetDisplay failed");
282 egl_dbg("eglInitialize ...\n");
283 b
= eglInitialize(qemu_egl_display
, &major
, &minor
);
284 if (b
== EGL_FALSE
) {
285 error_report("egl: eglInitialize failed");
289 egl_dbg("eglBindAPI ...\n");
290 b
= eglBindAPI(gles
? EGL_OPENGL_ES_API
: EGL_OPENGL_API
);
291 if (b
== EGL_FALSE
) {
292 error_report("egl: eglBindAPI failed");
296 egl_dbg("eglChooseConfig ...\n");
297 b
= eglChooseConfig(qemu_egl_display
,
298 gles
? conf_att_gles
: conf_att_gl
,
299 &qemu_egl_config
, 1, &n
);
300 if (b
== EGL_FALSE
|| n
!= 1) {
301 error_report("egl: eglChooseConfig failed");
309 EGLContext
qemu_egl_init_ctx(void)
311 static const EGLint ctx_att_gl
[] = {
314 static const EGLint ctx_att_gles
[] = {
315 EGL_CONTEXT_CLIENT_VERSION
, 2,
322 egl_dbg("eglCreateContext ...\n");
323 ectx
= eglCreateContext(qemu_egl_display
, qemu_egl_config
, EGL_NO_CONTEXT
,
324 egl_gles
? ctx_att_gles
: ctx_att_gl
);
325 if (ectx
== EGL_NO_CONTEXT
) {
326 error_report("egl: eglCreateContext failed");
330 b
= eglMakeCurrent(qemu_egl_display
, EGL_NO_SURFACE
, EGL_NO_SURFACE
, ectx
);
331 if (b
== EGL_FALSE
) {
332 error_report("egl: eglMakeCurrent failed");