1 #include "qemu/osdep.h"
5 #include "qemu/error-report.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 p
= g_strdup_printf("/dev/dri/%s", e
->d_name
);
55 r
= open(p
, O_RDWR
| O_CLOEXEC
| O_NOCTTY
| O_NONBLOCK
);
72 int egl_rendernode_init(void)
76 qemu_egl_rn_fd
= qemu_egl_rendernode_open();
77 if (qemu_egl_rn_fd
== -1) {
78 error_report("egl: no drm render node available");
82 qemu_egl_rn_gbm_dev
= gbm_create_device(qemu_egl_rn_fd
);
83 if (!qemu_egl_rn_gbm_dev
) {
84 error_report("egl: gbm_create_device failed");
88 qemu_egl_init_dpy((EGLNativeDisplayType
)qemu_egl_rn_gbm_dev
, false, false);
90 if (!epoxy_has_egl_extension(qemu_egl_display
,
91 "EGL_KHR_surfaceless_context")) {
92 error_report("egl: EGL_KHR_surfaceless_context not supported");
95 if (!epoxy_has_egl_extension(qemu_egl_display
,
96 "EGL_MESA_image_dma_buf_export")) {
97 error_report("egl: EGL_MESA_image_dma_buf_export not supported");
101 qemu_egl_rn_ctx
= qemu_egl_init_ctx();
102 if (!qemu_egl_rn_ctx
) {
103 error_report("egl: egl_init_ctx failed");
110 if (qemu_egl_rn_gbm_dev
) {
111 gbm_device_destroy(qemu_egl_rn_gbm_dev
);
113 if (qemu_egl_rn_fd
!= -1) {
114 close(qemu_egl_rn_fd
);
120 int egl_get_fd_for_texture(uint32_t tex_id
, EGLint
*stride
, EGLint
*fourcc
)
123 EGLint num_planes
, fd
;
125 image
= eglCreateImageKHR(qemu_egl_display
, eglGetCurrentContext(),
126 EGL_GL_TEXTURE_2D_KHR
,
127 (EGLClientBuffer
)(unsigned long)tex_id
,
133 eglExportDMABUFImageQueryMESA(qemu_egl_display
, image
, fourcc
,
135 if (num_planes
!= 1) {
136 eglDestroyImageKHR(qemu_egl_display
, image
);
139 eglExportDMABUFImageMESA(qemu_egl_display
, image
, &fd
, stride
, NULL
);
140 eglDestroyImageKHR(qemu_egl_display
, image
);
145 #endif /* CONFIG_OPENGL_DMABUF */
147 /* ---------------------------------------------------------------------- */
149 EGLSurface
qemu_egl_init_surface_x11(EGLContext ectx
, Window win
)
154 egl_dbg("eglCreateWindowSurface (x11 win id 0x%lx) ...\n",
155 (unsigned long) win
);
156 esurface
= eglCreateWindowSurface(qemu_egl_display
,
158 (EGLNativeWindowType
)win
, NULL
);
159 if (esurface
== EGL_NO_SURFACE
) {
160 error_report("egl: eglCreateWindowSurface failed");
164 b
= eglMakeCurrent(qemu_egl_display
, esurface
, esurface
, ectx
);
165 if (b
== EGL_FALSE
) {
166 error_report("egl: eglMakeCurrent failed");
173 /* ---------------------------------------------------------------------- */
175 int qemu_egl_init_dpy(EGLNativeDisplayType dpy
, bool gles
, bool debug
)
177 static const EGLint conf_att_gl
[] = {
178 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
179 EGL_RENDERABLE_TYPE
, EGL_OPENGL_BIT
,
186 static const EGLint conf_att_gles
[] = {
187 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
188 EGL_RENDERABLE_TYPE
, EGL_OPENGL_ES2_BIT
,
201 setenv("EGL_LOG_LEVEL", "debug", true);
202 setenv("LIBGL_DEBUG", "verbose", true);
205 egl_dbg("eglGetDisplay (dpy %p) ...\n", dpy
);
206 qemu_egl_display
= eglGetDisplay(dpy
);
207 if (qemu_egl_display
== EGL_NO_DISPLAY
) {
208 error_report("egl: eglGetDisplay failed");
212 egl_dbg("eglInitialize ...\n");
213 b
= eglInitialize(qemu_egl_display
, &major
, &minor
);
214 if (b
== EGL_FALSE
) {
215 error_report("egl: eglInitialize failed");
219 egl_dbg("eglBindAPI ...\n");
220 b
= eglBindAPI(gles
? EGL_OPENGL_ES_API
: EGL_OPENGL_API
);
221 if (b
== EGL_FALSE
) {
222 error_report("egl: eglBindAPI failed");
226 egl_dbg("eglChooseConfig ...\n");
227 b
= eglChooseConfig(qemu_egl_display
,
228 gles
? conf_att_gles
: conf_att_gl
,
229 &qemu_egl_config
, 1, &n
);
230 if (b
== EGL_FALSE
|| n
!= 1) {
231 error_report("egl: eglChooseConfig failed");
239 EGLContext
qemu_egl_init_ctx(void)
241 static const EGLint ctx_att_gl
[] = {
244 static const EGLint ctx_att_gles
[] = {
245 EGL_CONTEXT_CLIENT_VERSION
, 2,
252 egl_dbg("eglCreateContext ...\n");
253 ectx
= eglCreateContext(qemu_egl_display
, qemu_egl_config
, EGL_NO_CONTEXT
,
254 egl_gles
? ctx_att_gles
: ctx_att_gl
);
255 if (ectx
== EGL_NO_CONTEXT
) {
256 error_report("egl: eglCreateContext failed");
260 b
= eglMakeCurrent(qemu_egl_display
, EGL_NO_SURFACE
, EGL_NO_SURFACE
, ectx
);
261 if (b
== EGL_FALSE
) {
262 error_report("egl: eglMakeCurrent failed");