auxbus: use get_uint() for "addr" property
[qemu/ar7.git] / ui / egl-helpers.c
blob4a4d3370ee6db505b95e78bb3a0b3bf39e36ee11
1 /*
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"
18 #include <glob.h>
19 #include <dirent.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
31 int qemu_egl_rn_fd;
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)
37 DIR *dir;
38 struct dirent *e;
39 int r, fd;
40 char *p;
42 if (rendernode) {
43 return open(rendernode, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
46 dir = opendir("/dev/dri");
47 if (!dir) {
48 return -1;
51 fd = -1;
52 while ((e = readdir(dir))) {
53 if (e->d_type != DT_CHR) {
54 continue;
57 if (strncmp(e->d_name, "renderD", 7)) {
58 continue;
61 p = g_strdup_printf("/dev/dri/%s", e->d_name);
63 r = open(p, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
64 if (r < 0) {
65 g_free(p);
66 continue;
68 fd = r;
69 g_free(p);
70 break;
73 closedir(dir);
74 if (fd < 0) {
75 return -1;
77 return fd;
80 int egl_rendernode_init(const char *rendernode)
82 qemu_egl_rn_fd = -1;
83 int rc;
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");
88 goto err;
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");
94 goto err;
97 rc = qemu_egl_init_dpy_mesa((EGLNativeDisplayType)qemu_egl_rn_gbm_dev);
98 if (rc != 0) {
99 /* qemu_egl_init_dpy_mesa reports error */
100 goto err;
103 if (!epoxy_has_egl_extension(qemu_egl_display,
104 "EGL_KHR_surfaceless_context")) {
105 error_report("egl: EGL_KHR_surfaceless_context not supported");
106 goto err;
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");
111 goto err;
114 qemu_egl_rn_ctx = qemu_egl_init_ctx();
115 if (!qemu_egl_rn_ctx) {
116 error_report("egl: egl_init_ctx failed");
117 goto err;
120 return 0;
122 err:
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);
130 return -1;
133 int egl_get_fd_for_texture(uint32_t tex_id, EGLint *stride, EGLint *fourcc)
135 EGLImageKHR image;
136 EGLint num_planes, fd;
138 image = eglCreateImageKHR(qemu_egl_display, eglGetCurrentContext(),
139 EGL_GL_TEXTURE_2D_KHR,
140 (EGLClientBuffer)(unsigned long)tex_id,
141 NULL);
142 if (!image) {
143 return -1;
146 eglExportDMABUFImageQueryMESA(qemu_egl_display, image, fourcc,
147 &num_planes, NULL);
148 if (num_planes != 1) {
149 eglDestroyImageKHR(qemu_egl_display, image);
150 return -1;
152 eglExportDMABUFImageMESA(qemu_egl_display, image, &fd, stride, NULL);
153 eglDestroyImageKHR(qemu_egl_display, image);
155 return fd;
158 #endif /* CONFIG_OPENGL_DMABUF */
160 /* ---------------------------------------------------------------------- */
162 EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, Window win)
164 EGLSurface esurface;
165 EGLBoolean b;
167 esurface = eglCreateWindowSurface(qemu_egl_display,
168 qemu_egl_config,
169 (EGLNativeWindowType)win, NULL);
170 if (esurface == EGL_NO_SURFACE) {
171 error_report("egl: eglCreateWindowSurface failed");
172 return NULL;
175 b = eglMakeCurrent(qemu_egl_display, esurface, esurface, ectx);
176 if (b == EGL_FALSE) {
177 error_report("egl: eglMakeCurrent failed");
178 return NULL;
181 return esurface;
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
190 * for a few reasons.
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
195 * them together.
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
199 * will crash.
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
209 * function pointer.
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,
215 EGLenum platform)
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) {
229 /* fallback */
230 dpy = eglGetDisplay(native);
232 return dpy;
235 static int qemu_egl_init_dpy(EGLNativeDisplayType dpy,
236 EGLenum platform)
238 static const EGLint conf_att_gl[] = {
239 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
240 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
241 EGL_RED_SIZE, 5,
242 EGL_GREEN_SIZE, 5,
243 EGL_BLUE_SIZE, 5,
244 EGL_ALPHA_SIZE, 0,
245 EGL_NONE,
247 EGLint major, minor;
248 EGLBoolean b;
249 EGLint n;
251 qemu_egl_display = qemu_egl_get_display(dpy, platform);
252 if (qemu_egl_display == EGL_NO_DISPLAY) {
253 error_report("egl: eglGetDisplay failed");
254 return -1;
257 b = eglInitialize(qemu_egl_display, &major, &minor);
258 if (b == EGL_FALSE) {
259 error_report("egl: eglInitialize failed");
260 return -1;
263 b = eglBindAPI(EGL_OPENGL_API);
264 if (b == EGL_FALSE) {
265 error_report("egl: eglBindAPI failed");
266 return -1;
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");
273 return -1;
275 return 0;
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);
282 #else
283 return qemu_egl_init_dpy(dpy, 0);
284 #endif
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);
291 #else
292 return qemu_egl_init_dpy(dpy, 0);
293 #endif
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,
300 EGL_NONE
302 EGLContext ectx;
303 EGLBoolean b;
305 ectx = eglCreateContext(qemu_egl_display, qemu_egl_config, EGL_NO_CONTEXT,
306 ctx_att_gl);
307 if (ectx == EGL_NO_CONTEXT) {
308 error_report("egl: eglCreateContext failed");
309 return NULL;
312 b = eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, ectx);
313 if (b == EGL_FALSE) {
314 error_report("egl: eglMakeCurrent failed");
315 return NULL;
318 return ectx;