1 #include "qemu/osdep.h"
5 #include "ui/egl-helpers.h"
7 EGLDisplay
*qemu_egl_display
;
8 EGLConfig qemu_egl_config
;
10 /* ---------------------------------------------------------------------- */
15 #define egl_dbg(_x ...) \
18 fprintf(stderr, "egl: " _x); \
22 /* ---------------------------------------------------------------------- */
24 #ifdef CONFIG_OPENGL_DMABUF
27 struct gbm_device
*qemu_egl_rn_gbm_dev
;
28 EGLContext qemu_egl_rn_ctx
;
30 int qemu_egl_rendernode_open(void)
37 dir
= opendir("/dev/dri");
43 while ((e
= readdir(dir
))) {
44 if (e
->d_type
!= DT_CHR
) {
48 if (strncmp(e
->d_name
, "renderD", 7)) {
52 p
= g_strdup_printf("/dev/dri/%s", e
->d_name
);
54 r
= open(p
, O_RDWR
| O_CLOEXEC
| O_NOCTTY
| O_NONBLOCK
);
71 int egl_rendernode_init(void)
75 qemu_egl_rn_fd
= qemu_egl_rendernode_open();
76 if (qemu_egl_rn_fd
== -1) {
77 fprintf(stderr
, "egl: no drm render node available\n");
81 qemu_egl_rn_gbm_dev
= gbm_create_device(qemu_egl_rn_fd
);
82 if (!qemu_egl_rn_gbm_dev
) {
83 fprintf(stderr
, "egl: gbm_create_device failed\n");
87 qemu_egl_init_dpy((EGLNativeDisplayType
)qemu_egl_rn_gbm_dev
, false, false);
89 if (!epoxy_has_egl_extension(qemu_egl_display
,
90 "EGL_KHR_surfaceless_context")) {
91 fprintf(stderr
, "egl: EGL_KHR_surfaceless_context not supported\n");
94 if (!epoxy_has_egl_extension(qemu_egl_display
,
95 "EGL_MESA_image_dma_buf_export")) {
96 fprintf(stderr
, "egl: EGL_MESA_image_dma_buf_export not supported\n");
100 qemu_egl_rn_ctx
= qemu_egl_init_ctx();
101 if (!qemu_egl_rn_ctx
) {
102 fprintf(stderr
, "egl: egl_init_ctx failed\n");
109 if (qemu_egl_rn_gbm_dev
) {
110 gbm_device_destroy(qemu_egl_rn_gbm_dev
);
112 if (qemu_egl_rn_fd
!= -1) {
113 close(qemu_egl_rn_fd
);
119 int egl_get_fd_for_texture(uint32_t tex_id
, EGLint
*stride
, EGLint
*fourcc
)
122 EGLint num_planes
, fd
;
124 image
= eglCreateImageKHR(qemu_egl_display
, eglGetCurrentContext(),
125 EGL_GL_TEXTURE_2D_KHR
,
126 (EGLClientBuffer
)(unsigned long)tex_id
,
132 eglExportDMABUFImageQueryMESA(qemu_egl_display
, image
, fourcc
,
134 if (num_planes
!= 1) {
135 eglDestroyImageKHR(qemu_egl_display
, image
);
138 eglExportDMABUFImageMESA(qemu_egl_display
, image
, &fd
, stride
, NULL
);
139 eglDestroyImageKHR(qemu_egl_display
, image
);
144 #endif /* CONFIG_OPENGL_DMABUF */
146 /* ---------------------------------------------------------------------- */
148 EGLSurface
qemu_egl_init_surface_x11(EGLContext ectx
, Window win
)
153 egl_dbg("eglCreateWindowSurface (x11 win id 0x%lx) ...\n",
154 (unsigned long) win
);
155 esurface
= eglCreateWindowSurface(qemu_egl_display
,
157 (EGLNativeWindowType
)win
, NULL
);
158 if (esurface
== EGL_NO_SURFACE
) {
159 fprintf(stderr
, "egl: eglCreateWindowSurface failed\n");
163 b
= eglMakeCurrent(qemu_egl_display
, esurface
, esurface
, ectx
);
164 if (b
== EGL_FALSE
) {
165 fprintf(stderr
, "egl: eglMakeCurrent failed\n");
172 /* ---------------------------------------------------------------------- */
174 int qemu_egl_init_dpy(EGLNativeDisplayType dpy
, bool gles
, bool debug
)
176 static const EGLint conf_att_gl
[] = {
177 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
178 EGL_RENDERABLE_TYPE
, EGL_OPENGL_BIT
,
185 static const EGLint conf_att_gles
[] = {
186 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
187 EGL_RENDERABLE_TYPE
, EGL_OPENGL_ES2_BIT
,
200 setenv("EGL_LOG_LEVEL", "debug", true);
201 setenv("LIBGL_DEBUG", "verbose", true);
204 egl_dbg("eglGetDisplay (dpy %p) ...\n", dpy
);
205 qemu_egl_display
= eglGetDisplay(dpy
);
206 if (qemu_egl_display
== EGL_NO_DISPLAY
) {
207 fprintf(stderr
, "egl: eglGetDisplay failed\n");
211 egl_dbg("eglInitialize ...\n");
212 b
= eglInitialize(qemu_egl_display
, &major
, &minor
);
213 if (b
== EGL_FALSE
) {
214 fprintf(stderr
, "egl: eglInitialize failed\n");
218 egl_dbg("eglBindAPI ...\n");
219 b
= eglBindAPI(gles
? EGL_OPENGL_ES_API
: EGL_OPENGL_API
);
220 if (b
== EGL_FALSE
) {
221 fprintf(stderr
, "egl: eglBindAPI failed\n");
225 egl_dbg("eglChooseConfig ...\n");
226 b
= eglChooseConfig(qemu_egl_display
,
227 gles
? conf_att_gles
: conf_att_gl
,
228 &qemu_egl_config
, 1, &n
);
229 if (b
== EGL_FALSE
|| n
!= 1) {
230 fprintf(stderr
, "egl: eglChooseConfig failed\n");
238 EGLContext
qemu_egl_init_ctx(void)
240 static const EGLint ctx_att_gl
[] = {
243 static const EGLint ctx_att_gles
[] = {
244 EGL_CONTEXT_CLIENT_VERSION
, 2,
251 egl_dbg("eglCreateContext ...\n");
252 ectx
= eglCreateContext(qemu_egl_display
, qemu_egl_config
, EGL_NO_CONTEXT
,
253 egl_gles
? ctx_att_gles
: ctx_att_gl
);
254 if (ectx
== EGL_NO_CONTEXT
) {
255 fprintf(stderr
, "egl: eglCreateContext failed\n");
259 b
= eglMakeCurrent(qemu_egl_display
, EGL_NO_SURFACE
, EGL_NO_SURFACE
, ectx
);
260 if (b
== EGL_FALSE
) {
261 fprintf(stderr
, "egl: eglMakeCurrent failed\n");