sun4u: use sunhme as default on-board NIC
[qemu/ar7.git] / ui / egl-helpers.c
blobbb19a5eeca05dbf5a3506dbdef4cbbf20058ef0d
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 void egl_fb_destroy(egl_fb *fb)
31 if (!fb->framebuffer) {
32 return;
35 if (fb->delete_texture) {
36 glDeleteTextures(1, &fb->texture);
37 fb->delete_texture = false;
39 glDeleteFramebuffers(1, &fb->framebuffer);
41 fb->width = 0;
42 fb->height = 0;
43 fb->texture = 0;
44 fb->framebuffer = 0;
47 void egl_fb_setup_default(egl_fb *fb, int width, int height)
49 fb->width = width;
50 fb->height = height;
51 fb->framebuffer = 0; /* default framebuffer */
54 void egl_fb_create_for_tex(egl_fb *fb, int width, int height, GLuint texture)
56 fb->width = width;
57 fb->height = height;
58 fb->texture = texture;
59 if (!fb->framebuffer) {
60 glGenFramebuffers(1, &fb->framebuffer);
63 glBindFramebuffer(GL_FRAMEBUFFER_EXT, fb->framebuffer);
64 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
65 GL_TEXTURE_2D, fb->texture, 0);
68 void egl_fb_create_new_tex(egl_fb *fb, int width, int height)
70 GLuint texture;
72 glGenTextures(1, &texture);
73 glBindTexture(GL_TEXTURE_2D, texture);
74 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height,
75 0, GL_BGRA, GL_UNSIGNED_BYTE, 0);
77 egl_fb_create_for_tex(fb, width, height, texture);
78 fb->delete_texture = true;
81 void egl_fb_blit(egl_fb *dst, egl_fb *src, bool flip)
83 GLuint y1, y2;
85 glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer);
86 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst->framebuffer);
87 glViewport(0, 0, dst->width, dst->height);
88 y1 = flip ? src->height : 0;
89 y2 = flip ? 0 : src->height;
90 glBlitFramebuffer(0, y1, src->width, y2,
91 0, 0, dst->width, dst->height,
92 GL_COLOR_BUFFER_BIT, GL_LINEAR);
95 void egl_fb_read(void *dst, egl_fb *src)
97 glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer);
98 glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
99 glReadPixels(0, 0, src->width, src->height,
100 GL_BGRA, GL_UNSIGNED_BYTE, dst);
103 /* ---------------------------------------------------------------------- */
105 #ifdef CONFIG_OPENGL_DMABUF
107 int qemu_egl_rn_fd;
108 struct gbm_device *qemu_egl_rn_gbm_dev;
109 EGLContext qemu_egl_rn_ctx;
111 static int qemu_egl_rendernode_open(const char *rendernode)
113 DIR *dir;
114 struct dirent *e;
115 int r, fd;
116 char *p;
118 if (rendernode) {
119 return open(rendernode, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
122 dir = opendir("/dev/dri");
123 if (!dir) {
124 return -1;
127 fd = -1;
128 while ((e = readdir(dir))) {
129 if (e->d_type != DT_CHR) {
130 continue;
133 if (strncmp(e->d_name, "renderD", 7)) {
134 continue;
137 p = g_strdup_printf("/dev/dri/%s", e->d_name);
139 r = open(p, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
140 if (r < 0) {
141 g_free(p);
142 continue;
144 fd = r;
145 g_free(p);
146 break;
149 closedir(dir);
150 if (fd < 0) {
151 return -1;
153 return fd;
156 int egl_rendernode_init(const char *rendernode)
158 qemu_egl_rn_fd = -1;
159 int rc;
161 qemu_egl_rn_fd = qemu_egl_rendernode_open(rendernode);
162 if (qemu_egl_rn_fd == -1) {
163 error_report("egl: no drm render node available");
164 goto err;
167 qemu_egl_rn_gbm_dev = gbm_create_device(qemu_egl_rn_fd);
168 if (!qemu_egl_rn_gbm_dev) {
169 error_report("egl: gbm_create_device failed");
170 goto err;
173 rc = qemu_egl_init_dpy_mesa((EGLNativeDisplayType)qemu_egl_rn_gbm_dev);
174 if (rc != 0) {
175 /* qemu_egl_init_dpy_mesa reports error */
176 goto err;
179 if (!epoxy_has_egl_extension(qemu_egl_display,
180 "EGL_KHR_surfaceless_context")) {
181 error_report("egl: EGL_KHR_surfaceless_context not supported");
182 goto err;
184 if (!epoxy_has_egl_extension(qemu_egl_display,
185 "EGL_MESA_image_dma_buf_export")) {
186 error_report("egl: EGL_MESA_image_dma_buf_export not supported");
187 goto err;
190 qemu_egl_rn_ctx = qemu_egl_init_ctx();
191 if (!qemu_egl_rn_ctx) {
192 error_report("egl: egl_init_ctx failed");
193 goto err;
196 return 0;
198 err:
199 if (qemu_egl_rn_gbm_dev) {
200 gbm_device_destroy(qemu_egl_rn_gbm_dev);
202 if (qemu_egl_rn_fd != -1) {
203 close(qemu_egl_rn_fd);
206 return -1;
209 int egl_get_fd_for_texture(uint32_t tex_id, EGLint *stride, EGLint *fourcc)
211 EGLImageKHR image;
212 EGLint num_planes, fd;
214 image = eglCreateImageKHR(qemu_egl_display, eglGetCurrentContext(),
215 EGL_GL_TEXTURE_2D_KHR,
216 (EGLClientBuffer)(unsigned long)tex_id,
217 NULL);
218 if (!image) {
219 return -1;
222 eglExportDMABUFImageQueryMESA(qemu_egl_display, image, fourcc,
223 &num_planes, NULL);
224 if (num_planes != 1) {
225 eglDestroyImageKHR(qemu_egl_display, image);
226 return -1;
228 eglExportDMABUFImageMESA(qemu_egl_display, image, &fd, stride, NULL);
229 eglDestroyImageKHR(qemu_egl_display, image);
231 return fd;
234 #endif /* CONFIG_OPENGL_DMABUF */
236 /* ---------------------------------------------------------------------- */
238 EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, Window win)
240 EGLSurface esurface;
241 EGLBoolean b;
243 esurface = eglCreateWindowSurface(qemu_egl_display,
244 qemu_egl_config,
245 (EGLNativeWindowType)win, NULL);
246 if (esurface == EGL_NO_SURFACE) {
247 error_report("egl: eglCreateWindowSurface failed");
248 return NULL;
251 b = eglMakeCurrent(qemu_egl_display, esurface, esurface, ectx);
252 if (b == EGL_FALSE) {
253 error_report("egl: eglMakeCurrent failed");
254 return NULL;
257 return esurface;
260 /* ---------------------------------------------------------------------- */
263 * Taken from glamor_egl.h from the Xorg xserver, which is MIT licensed
265 * Create an EGLDisplay from a native display type. This is a little quirky
266 * for a few reasons.
268 * 1: GetPlatformDisplayEXT and GetPlatformDisplay are the API you want to
269 * use, but have different function signatures in the third argument; this
270 * happens not to matter for us, at the moment, but it means epoxy won't alias
271 * them together.
273 * 2: epoxy 1.3 and earlier don't understand EGL client extensions, which
274 * means you can't call "eglGetPlatformDisplayEXT" directly, as the resolver
275 * will crash.
277 * 3: You can't tell whether you have EGL 1.5 at this point, because
278 * eglQueryString(EGL_VERSION) is a property of the display, which we don't
279 * have yet. So you have to query for extensions no matter what. Fortunately
280 * epoxy_has_egl_extension _does_ let you query for client extensions, so
281 * we don't have to write our own extension string parsing.
283 * 4. There is no EGL_KHR_platform_base to complement the EXT one, thus one
284 * needs to know EGL 1.5 is supported in order to use the eglGetPlatformDisplay
285 * function pointer.
286 * We can workaround this (circular dependency) by probing for the EGL 1.5
287 * platform extensions (EGL_KHR_platform_gbm and friends) yet it doesn't seem
288 * like mesa will be able to advertise these (even though it can do EGL 1.5).
290 static EGLDisplay qemu_egl_get_display(EGLNativeDisplayType native,
291 EGLenum platform)
293 EGLDisplay dpy = EGL_NO_DISPLAY;
295 /* In practise any EGL 1.5 implementation would support the EXT extension */
296 if (epoxy_has_egl_extension(NULL, "EGL_EXT_platform_base")) {
297 PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplayEXT =
298 (void *) eglGetProcAddress("eglGetPlatformDisplayEXT");
299 if (getPlatformDisplayEXT && platform != 0) {
300 dpy = getPlatformDisplayEXT(platform, native, NULL);
304 if (dpy == EGL_NO_DISPLAY) {
305 /* fallback */
306 dpy = eglGetDisplay(native);
308 return dpy;
311 static int qemu_egl_init_dpy(EGLNativeDisplayType dpy,
312 EGLenum platform)
314 static const EGLint conf_att_gl[] = {
315 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
316 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
317 EGL_RED_SIZE, 5,
318 EGL_GREEN_SIZE, 5,
319 EGL_BLUE_SIZE, 5,
320 EGL_ALPHA_SIZE, 0,
321 EGL_NONE,
323 EGLint major, minor;
324 EGLBoolean b;
325 EGLint n;
327 qemu_egl_display = qemu_egl_get_display(dpy, platform);
328 if (qemu_egl_display == EGL_NO_DISPLAY) {
329 error_report("egl: eglGetDisplay failed");
330 return -1;
333 b = eglInitialize(qemu_egl_display, &major, &minor);
334 if (b == EGL_FALSE) {
335 error_report("egl: eglInitialize failed");
336 return -1;
339 b = eglBindAPI(EGL_OPENGL_API);
340 if (b == EGL_FALSE) {
341 error_report("egl: eglBindAPI failed");
342 return -1;
345 b = eglChooseConfig(qemu_egl_display, conf_att_gl,
346 &qemu_egl_config, 1, &n);
347 if (b == EGL_FALSE || n != 1) {
348 error_report("egl: eglChooseConfig failed");
349 return -1;
351 return 0;
354 int qemu_egl_init_dpy_x11(EGLNativeDisplayType dpy)
356 #ifdef EGL_KHR_platform_x11
357 return qemu_egl_init_dpy(dpy, EGL_PLATFORM_X11_KHR);
358 #else
359 return qemu_egl_init_dpy(dpy, 0);
360 #endif
363 int qemu_egl_init_dpy_mesa(EGLNativeDisplayType dpy)
365 #ifdef EGL_MESA_platform_gbm
366 return qemu_egl_init_dpy(dpy, EGL_PLATFORM_GBM_MESA);
367 #else
368 return qemu_egl_init_dpy(dpy, 0);
369 #endif
372 EGLContext qemu_egl_init_ctx(void)
374 static const EGLint ctx_att_gl[] = {
375 EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT,
376 EGL_NONE
378 EGLContext ectx;
379 EGLBoolean b;
381 ectx = eglCreateContext(qemu_egl_display, qemu_egl_config, EGL_NO_CONTEXT,
382 ctx_att_gl);
383 if (ectx == EGL_NO_CONTEXT) {
384 error_report("egl: eglCreateContext failed");
385 return NULL;
388 b = eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, ectx);
389 if (b == EGL_FALSE) {
390 error_report("egl: eglMakeCurrent failed");
391 return NULL;
394 return ectx;