1 #include "qemu/osdep.h"
5 #include "config-host.h"
6 #include "ui/egl-helpers.h"
8 EGLDisplay
*qemu_egl_display
;
9 EGLConfig qemu_egl_config
;
11 /* ---------------------------------------------------------------------- */
16 #define egl_dbg(_x ...) \
19 fprintf(stderr, "egl: " _x); \
23 /* ---------------------------------------------------------------------- */
25 #ifdef CONFIG_OPENGL_DMABUF
28 struct gbm_device
*qemu_egl_rn_gbm_dev
;
29 EGLContext qemu_egl_rn_ctx
;
31 int qemu_egl_rendernode_open(void)
38 dir
= opendir("/dev/dri");
44 while ((e
= readdir(dir
))) {
45 if (e
->d_type
!= DT_CHR
) {
49 if (strncmp(e
->d_name
, "renderD", 7)) {
53 r
= asprintf(&p
, "/dev/dri/%s", e
->d_name
);
58 r
= open(p
, O_RDWR
| O_CLOEXEC
| O_NOCTTY
| O_NONBLOCK
);
75 int egl_rendernode_init(void)
79 qemu_egl_rn_fd
= qemu_egl_rendernode_open();
80 if (qemu_egl_rn_fd
== -1) {
81 fprintf(stderr
, "egl: no drm render node available\n");
85 qemu_egl_rn_gbm_dev
= gbm_create_device(qemu_egl_rn_fd
);
86 if (!qemu_egl_rn_gbm_dev
) {
87 fprintf(stderr
, "egl: gbm_create_device failed\n");
91 qemu_egl_init_dpy((EGLNativeDisplayType
)qemu_egl_rn_gbm_dev
, false, false);
93 if (!epoxy_has_egl_extension(qemu_egl_display
,
94 "EGL_KHR_surfaceless_context")) {
95 fprintf(stderr
, "egl: EGL_KHR_surfaceless_context not supported\n");
98 if (!epoxy_has_egl_extension(qemu_egl_display
,
99 "EGL_MESA_image_dma_buf_export")) {
100 fprintf(stderr
, "egl: EGL_MESA_image_dma_buf_export not supported\n");
104 qemu_egl_rn_ctx
= qemu_egl_init_ctx();
105 if (!qemu_egl_rn_ctx
) {
106 fprintf(stderr
, "egl: egl_init_ctx failed\n");
113 if (qemu_egl_rn_gbm_dev
) {
114 gbm_device_destroy(qemu_egl_rn_gbm_dev
);
116 if (qemu_egl_rn_fd
!= -1) {
117 close(qemu_egl_rn_fd
);
123 int egl_get_fd_for_texture(uint32_t tex_id
, EGLint
*stride
, EGLint
*fourcc
)
126 EGLint num_planes
, fd
;
128 image
= eglCreateImageKHR(qemu_egl_display
, eglGetCurrentContext(),
129 EGL_GL_TEXTURE_2D_KHR
,
130 (EGLClientBuffer
)(unsigned long)tex_id
,
136 eglExportDMABUFImageQueryMESA(qemu_egl_display
, image
, fourcc
,
138 if (num_planes
!= 1) {
139 eglDestroyImageKHR(qemu_egl_display
, image
);
142 eglExportDMABUFImageMESA(qemu_egl_display
, image
, &fd
, stride
, NULL
);
143 eglDestroyImageKHR(qemu_egl_display
, image
);
148 #endif /* CONFIG_OPENGL_DMABUF */
150 /* ---------------------------------------------------------------------- */
152 EGLSurface
qemu_egl_init_surface_x11(EGLContext ectx
, Window win
)
157 egl_dbg("eglCreateWindowSurface (x11 win id 0x%lx) ...\n",
158 (unsigned long) win
);
159 esurface
= eglCreateWindowSurface(qemu_egl_display
,
161 (EGLNativeWindowType
)win
, NULL
);
162 if (esurface
== EGL_NO_SURFACE
) {
163 fprintf(stderr
, "egl: eglCreateWindowSurface failed\n");
167 b
= eglMakeCurrent(qemu_egl_display
, esurface
, esurface
, ectx
);
168 if (b
== EGL_FALSE
) {
169 fprintf(stderr
, "egl: eglMakeCurrent failed\n");
176 /* ---------------------------------------------------------------------- */
178 int qemu_egl_init_dpy(EGLNativeDisplayType dpy
, bool gles
, bool debug
)
180 static const EGLint conf_att_gl
[] = {
181 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
182 EGL_RENDERABLE_TYPE
, EGL_OPENGL_BIT
,
189 static const EGLint conf_att_gles
[] = {
190 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
191 EGL_RENDERABLE_TYPE
, EGL_OPENGL_ES2_BIT
,
204 setenv("EGL_LOG_LEVEL", "debug", true);
205 setenv("LIBGL_DEBUG", "verbose", true);
208 egl_dbg("eglGetDisplay (dpy %p) ...\n", dpy
);
209 qemu_egl_display
= eglGetDisplay(dpy
);
210 if (qemu_egl_display
== EGL_NO_DISPLAY
) {
211 fprintf(stderr
, "egl: eglGetDisplay failed\n");
215 egl_dbg("eglInitialize ...\n");
216 b
= eglInitialize(qemu_egl_display
, &major
, &minor
);
217 if (b
== EGL_FALSE
) {
218 fprintf(stderr
, "egl: eglInitialize failed\n");
222 egl_dbg("eglBindAPI ...\n");
223 b
= eglBindAPI(gles
? EGL_OPENGL_ES_API
: EGL_OPENGL_API
);
224 if (b
== EGL_FALSE
) {
225 fprintf(stderr
, "egl: eglBindAPI failed\n");
229 egl_dbg("eglChooseConfig ...\n");
230 b
= eglChooseConfig(qemu_egl_display
,
231 gles
? conf_att_gles
: conf_att_gl
,
232 &qemu_egl_config
, 1, &n
);
233 if (b
== EGL_FALSE
|| n
!= 1) {
234 fprintf(stderr
, "egl: eglChooseConfig failed\n");
242 EGLContext
qemu_egl_init_ctx(void)
244 static const EGLint ctx_att_gl
[] = {
247 static const EGLint ctx_att_gles
[] = {
248 EGL_CONTEXT_CLIENT_VERSION
, 2,
255 egl_dbg("eglCreateContext ...\n");
256 ectx
= eglCreateContext(qemu_egl_display
, qemu_egl_config
, EGL_NO_CONTEXT
,
257 egl_gles
? ctx_att_gles
: ctx_att_gl
);
258 if (ectx
== EGL_NO_CONTEXT
) {
259 fprintf(stderr
, "egl: eglCreateContext failed\n");
263 b
= eglMakeCurrent(qemu_egl_display
, EGL_NO_SURFACE
, EGL_NO_SURFACE
, ectx
);
264 if (b
== EGL_FALSE
) {
265 fprintf(stderr
, "egl: eglMakeCurrent failed\n");