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 #ifdef CONFIG_OPENGL_DMABUF
32 struct gbm_device
*qemu_egl_rn_gbm_dev
;
33 EGLContext qemu_egl_rn_ctx
;
35 static int qemu_egl_rendernode_open(const char *rendernode
)
43 return open(rendernode
, O_RDWR
| O_CLOEXEC
| O_NOCTTY
| O_NONBLOCK
);
46 dir
= opendir("/dev/dri");
52 while ((e
= readdir(dir
))) {
53 if (e
->d_type
!= DT_CHR
) {
57 if (strncmp(e
->d_name
, "renderD", 7)) {
61 p
= g_strdup_printf("/dev/dri/%s", e
->d_name
);
63 r
= open(p
, O_RDWR
| O_CLOEXEC
| O_NOCTTY
| O_NONBLOCK
);
80 int egl_rendernode_init(const char *rendernode
)
85 qemu_egl_rn_fd
= qemu_egl_rendernode_open(rendernode
);
86 if (qemu_egl_rn_fd
== -1) {
87 error_report("egl: no drm render node available");
91 qemu_egl_rn_gbm_dev
= gbm_create_device(qemu_egl_rn_fd
);
92 if (!qemu_egl_rn_gbm_dev
) {
93 error_report("egl: gbm_create_device failed");
97 rc
= qemu_egl_init_dpy_mesa((EGLNativeDisplayType
)qemu_egl_rn_gbm_dev
);
99 /* qemu_egl_init_dpy_mesa reports error */
103 if (!epoxy_has_egl_extension(qemu_egl_display
,
104 "EGL_KHR_surfaceless_context")) {
105 error_report("egl: EGL_KHR_surfaceless_context not supported");
108 if (!epoxy_has_egl_extension(qemu_egl_display
,
109 "EGL_MESA_image_dma_buf_export")) {
110 error_report("egl: EGL_MESA_image_dma_buf_export not supported");
114 qemu_egl_rn_ctx
= qemu_egl_init_ctx();
115 if (!qemu_egl_rn_ctx
) {
116 error_report("egl: egl_init_ctx failed");
123 if (qemu_egl_rn_gbm_dev
) {
124 gbm_device_destroy(qemu_egl_rn_gbm_dev
);
126 if (qemu_egl_rn_fd
!= -1) {
127 close(qemu_egl_rn_fd
);
133 int egl_get_fd_for_texture(uint32_t tex_id
, EGLint
*stride
, EGLint
*fourcc
)
136 EGLint num_planes
, fd
;
138 image
= eglCreateImageKHR(qemu_egl_display
, eglGetCurrentContext(),
139 EGL_GL_TEXTURE_2D_KHR
,
140 (EGLClientBuffer
)(unsigned long)tex_id
,
146 eglExportDMABUFImageQueryMESA(qemu_egl_display
, image
, fourcc
,
148 if (num_planes
!= 1) {
149 eglDestroyImageKHR(qemu_egl_display
, image
);
152 eglExportDMABUFImageMESA(qemu_egl_display
, image
, &fd
, stride
, NULL
);
153 eglDestroyImageKHR(qemu_egl_display
, image
);
158 #endif /* CONFIG_OPENGL_DMABUF */
160 /* ---------------------------------------------------------------------- */
162 EGLSurface
qemu_egl_init_surface_x11(EGLContext ectx
, Window win
)
167 esurface
= eglCreateWindowSurface(qemu_egl_display
,
169 (EGLNativeWindowType
)win
, NULL
);
170 if (esurface
== EGL_NO_SURFACE
) {
171 error_report("egl: eglCreateWindowSurface failed");
175 b
= eglMakeCurrent(qemu_egl_display
, esurface
, esurface
, ectx
);
176 if (b
== EGL_FALSE
) {
177 error_report("egl: eglMakeCurrent failed");
184 /* ---------------------------------------------------------------------- */
187 * Taken from glamor_egl.h from the Xorg xserver, which is MIT licensed
189 * Create an EGLDisplay from a native display type. This is a little quirky
192 * 1: GetPlatformDisplayEXT and GetPlatformDisplay are the API you want to
193 * use, but have different function signatures in the third argument; this
194 * happens not to matter for us, at the moment, but it means epoxy won't alias
197 * 2: epoxy 1.3 and earlier don't understand EGL client extensions, which
198 * means you can't call "eglGetPlatformDisplayEXT" directly, as the resolver
201 * 3: You can't tell whether you have EGL 1.5 at this point, because
202 * eglQueryString(EGL_VERSION) is a property of the display, which we don't
203 * have yet. So you have to query for extensions no matter what. Fortunately
204 * epoxy_has_egl_extension _does_ let you query for client extensions, so
205 * we don't have to write our own extension string parsing.
207 * 4. There is no EGL_KHR_platform_base to complement the EXT one, thus one
208 * needs to know EGL 1.5 is supported in order to use the eglGetPlatformDisplay
210 * We can workaround this (circular dependency) by probing for the EGL 1.5
211 * platform extensions (EGL_KHR_platform_gbm and friends) yet it doesn't seem
212 * like mesa will be able to advertise these (even though it can do EGL 1.5).
214 static EGLDisplay
qemu_egl_get_display(EGLNativeDisplayType native
,
217 EGLDisplay dpy
= EGL_NO_DISPLAY
;
219 /* In practise any EGL 1.5 implementation would support the EXT extension */
220 if (epoxy_has_egl_extension(NULL
, "EGL_EXT_platform_base")) {
221 PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplayEXT
=
222 (void *) eglGetProcAddress("eglGetPlatformDisplayEXT");
223 if (getPlatformDisplayEXT
&& platform
!= 0) {
224 dpy
= getPlatformDisplayEXT(platform
, native
, NULL
);
228 if (dpy
== EGL_NO_DISPLAY
) {
230 dpy
= eglGetDisplay(native
);
235 static int qemu_egl_init_dpy(EGLNativeDisplayType dpy
,
238 static const EGLint conf_att_gl
[] = {
239 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
240 EGL_RENDERABLE_TYPE
, EGL_OPENGL_BIT
,
251 qemu_egl_display
= qemu_egl_get_display(dpy
, platform
);
252 if (qemu_egl_display
== EGL_NO_DISPLAY
) {
253 error_report("egl: eglGetDisplay failed");
257 b
= eglInitialize(qemu_egl_display
, &major
, &minor
);
258 if (b
== EGL_FALSE
) {
259 error_report("egl: eglInitialize failed");
263 b
= eglBindAPI(EGL_OPENGL_API
);
264 if (b
== EGL_FALSE
) {
265 error_report("egl: eglBindAPI failed");
269 b
= eglChooseConfig(qemu_egl_display
, conf_att_gl
,
270 &qemu_egl_config
, 1, &n
);
271 if (b
== EGL_FALSE
|| n
!= 1) {
272 error_report("egl: eglChooseConfig failed");
278 int qemu_egl_init_dpy_x11(EGLNativeDisplayType dpy
)
280 #ifdef EGL_KHR_platform_x11
281 return qemu_egl_init_dpy(dpy
, EGL_PLATFORM_X11_KHR
);
283 return qemu_egl_init_dpy(dpy
, 0);
287 int qemu_egl_init_dpy_mesa(EGLNativeDisplayType dpy
)
289 #ifdef EGL_MESA_platform_gbm
290 return qemu_egl_init_dpy(dpy
, EGL_PLATFORM_GBM_MESA
);
292 return qemu_egl_init_dpy(dpy
, 0);
296 EGLContext
qemu_egl_init_ctx(void)
298 static const EGLint ctx_att_gl
[] = {
299 EGL_CONTEXT_OPENGL_PROFILE_MASK
, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT
,
305 ectx
= eglCreateContext(qemu_egl_display
, qemu_egl_config
, EGL_NO_CONTEXT
,
307 if (ectx
== EGL_NO_CONTEXT
) {
308 error_report("egl: eglCreateContext failed");
312 b
= eglMakeCurrent(qemu_egl_display
, EGL_NO_SURFACE
, EGL_NO_SURFACE
, ectx
);
313 if (b
== EGL_FALSE
) {
314 error_report("egl: eglMakeCurrent failed");