tests/tcg: Fix some targets default cross compiler path
[qemu/ar7.git] / ui / egl-helpers.c
blob385a3fa7522361631f5e94ec4386d18a0981e551
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 "qemu/drm.h"
19 #include "qemu/error-report.h"
20 #include "ui/console.h"
21 #include "ui/egl-helpers.h"
23 EGLDisplay *qemu_egl_display;
24 EGLConfig qemu_egl_config;
25 DisplayGLMode qemu_egl_mode;
27 /* ------------------------------------------------------------------ */
29 static void egl_fb_delete_texture(egl_fb *fb)
31 if (!fb->delete_texture) {
32 return;
35 glDeleteTextures(1, &fb->texture);
36 fb->delete_texture = false;
39 void egl_fb_destroy(egl_fb *fb)
41 if (!fb->framebuffer) {
42 return;
45 egl_fb_delete_texture(fb);
46 glDeleteFramebuffers(1, &fb->framebuffer);
48 fb->width = 0;
49 fb->height = 0;
50 fb->texture = 0;
51 fb->framebuffer = 0;
54 void egl_fb_setup_default(egl_fb *fb, int width, int height)
56 fb->width = width;
57 fb->height = height;
58 fb->framebuffer = 0; /* default framebuffer */
61 void egl_fb_setup_for_tex(egl_fb *fb, int width, int height,
62 GLuint texture, bool delete)
64 egl_fb_delete_texture(fb);
66 fb->width = width;
67 fb->height = height;
68 fb->texture = texture;
69 fb->delete_texture = delete;
70 if (!fb->framebuffer) {
71 glGenFramebuffers(1, &fb->framebuffer);
74 glBindFramebuffer(GL_FRAMEBUFFER_EXT, fb->framebuffer);
75 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
76 GL_TEXTURE_2D, fb->texture, 0);
79 void egl_fb_setup_new_tex(egl_fb *fb, int width, int height)
81 GLuint texture;
83 glGenTextures(1, &texture);
84 glBindTexture(GL_TEXTURE_2D, texture);
85 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height,
86 0, GL_BGRA, GL_UNSIGNED_BYTE, 0);
88 egl_fb_setup_for_tex(fb, width, height, texture, true);
91 void egl_fb_blit(egl_fb *dst, egl_fb *src, bool flip)
93 GLuint y1, y2;
95 glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer);
96 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst->framebuffer);
97 glViewport(0, 0, dst->width, dst->height);
98 y1 = flip ? src->height : 0;
99 y2 = flip ? 0 : src->height;
100 glBlitFramebuffer(0, y1, src->width, y2,
101 0, 0, dst->width, dst->height,
102 GL_COLOR_BUFFER_BIT, GL_LINEAR);
105 void egl_fb_read(DisplaySurface *dst, egl_fb *src)
107 glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer);
108 glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
109 glReadPixels(0, 0, surface_width(dst), surface_height(dst),
110 GL_BGRA, GL_UNSIGNED_BYTE, surface_data(dst));
113 void egl_texture_blit(QemuGLShader *gls, egl_fb *dst, egl_fb *src, bool flip)
115 glBindFramebuffer(GL_FRAMEBUFFER_EXT, dst->framebuffer);
116 glViewport(0, 0, dst->width, dst->height);
117 glEnable(GL_TEXTURE_2D);
118 glBindTexture(GL_TEXTURE_2D, src->texture);
119 qemu_gl_run_texture_blit(gls, flip);
122 void egl_texture_blend(QemuGLShader *gls, egl_fb *dst, egl_fb *src, bool flip,
123 int x, int y, double scale_x, double scale_y)
125 glBindFramebuffer(GL_FRAMEBUFFER_EXT, dst->framebuffer);
126 int w = scale_x * src->width;
127 int h = scale_y * src->height;
128 if (flip) {
129 glViewport(x, y, w, h);
130 } else {
131 glViewport(x, dst->height - h - y, w, h);
133 glEnable(GL_TEXTURE_2D);
134 glBindTexture(GL_TEXTURE_2D, src->texture);
135 glEnable(GL_BLEND);
136 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
137 qemu_gl_run_texture_blit(gls, flip);
138 glDisable(GL_BLEND);
141 /* ---------------------------------------------------------------------- */
143 #ifdef CONFIG_GBM
145 int qemu_egl_rn_fd;
146 struct gbm_device *qemu_egl_rn_gbm_dev;
147 EGLContext qemu_egl_rn_ctx;
149 int egl_rendernode_init(const char *rendernode, DisplayGLMode mode)
151 qemu_egl_rn_fd = -1;
152 int rc;
154 qemu_egl_rn_fd = qemu_drm_rendernode_open(rendernode);
155 if (qemu_egl_rn_fd == -1) {
156 error_report("egl: no drm render node available");
157 goto err;
160 qemu_egl_rn_gbm_dev = gbm_create_device(qemu_egl_rn_fd);
161 if (!qemu_egl_rn_gbm_dev) {
162 error_report("egl: gbm_create_device failed");
163 goto err;
166 rc = qemu_egl_init_dpy_mesa((EGLNativeDisplayType)qemu_egl_rn_gbm_dev,
167 mode);
168 if (rc != 0) {
169 /* qemu_egl_init_dpy_mesa reports error */
170 goto err;
173 if (!epoxy_has_egl_extension(qemu_egl_display,
174 "EGL_KHR_surfaceless_context")) {
175 error_report("egl: EGL_KHR_surfaceless_context not supported");
176 goto err;
178 if (!epoxy_has_egl_extension(qemu_egl_display,
179 "EGL_MESA_image_dma_buf_export")) {
180 error_report("egl: EGL_MESA_image_dma_buf_export not supported");
181 goto err;
184 qemu_egl_rn_ctx = qemu_egl_init_ctx();
185 if (!qemu_egl_rn_ctx) {
186 error_report("egl: egl_init_ctx failed");
187 goto err;
190 return 0;
192 err:
193 if (qemu_egl_rn_gbm_dev) {
194 gbm_device_destroy(qemu_egl_rn_gbm_dev);
196 if (qemu_egl_rn_fd != -1) {
197 close(qemu_egl_rn_fd);
200 return -1;
203 int egl_get_fd_for_texture(uint32_t tex_id, EGLint *stride, EGLint *fourcc,
204 EGLuint64KHR *modifier)
206 EGLImageKHR image;
207 EGLint num_planes, fd;
209 image = eglCreateImageKHR(qemu_egl_display, eglGetCurrentContext(),
210 EGL_GL_TEXTURE_2D_KHR,
211 (EGLClientBuffer)(unsigned long)tex_id,
212 NULL);
213 if (!image) {
214 return -1;
217 eglExportDMABUFImageQueryMESA(qemu_egl_display, image, fourcc,
218 &num_planes, modifier);
219 if (num_planes != 1) {
220 eglDestroyImageKHR(qemu_egl_display, image);
221 return -1;
223 eglExportDMABUFImageMESA(qemu_egl_display, image, &fd, stride, NULL);
224 eglDestroyImageKHR(qemu_egl_display, image);
226 return fd;
229 void egl_dmabuf_import_texture(QemuDmaBuf *dmabuf)
231 EGLImageKHR image = EGL_NO_IMAGE_KHR;
232 EGLint attrs[64];
233 int i = 0;
235 if (dmabuf->texture != 0) {
236 return;
239 attrs[i++] = EGL_WIDTH;
240 attrs[i++] = dmabuf->width;
241 attrs[i++] = EGL_HEIGHT;
242 attrs[i++] = dmabuf->height;
243 attrs[i++] = EGL_LINUX_DRM_FOURCC_EXT;
244 attrs[i++] = dmabuf->fourcc;
246 attrs[i++] = EGL_DMA_BUF_PLANE0_FD_EXT;
247 attrs[i++] = dmabuf->fd;
248 attrs[i++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
249 attrs[i++] = dmabuf->stride;
250 attrs[i++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
251 attrs[i++] = 0;
252 #ifdef EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT
253 if (dmabuf->modifier) {
254 attrs[i++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT;
255 attrs[i++] = (dmabuf->modifier >> 0) & 0xffffffff;
256 attrs[i++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT;
257 attrs[i++] = (dmabuf->modifier >> 32) & 0xffffffff;
259 #endif
260 attrs[i++] = EGL_NONE;
262 image = eglCreateImageKHR(qemu_egl_display,
263 EGL_NO_CONTEXT,
264 EGL_LINUX_DMA_BUF_EXT,
265 NULL, attrs);
266 if (image == EGL_NO_IMAGE_KHR) {
267 error_report("eglCreateImageKHR failed");
268 return;
271 glGenTextures(1, &dmabuf->texture);
272 glBindTexture(GL_TEXTURE_2D, dmabuf->texture);
273 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
274 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
276 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
277 eglDestroyImageKHR(qemu_egl_display, image);
280 void egl_dmabuf_release_texture(QemuDmaBuf *dmabuf)
282 if (dmabuf->texture == 0) {
283 return;
286 glDeleteTextures(1, &dmabuf->texture);
287 dmabuf->texture = 0;
290 void egl_dmabuf_create_sync(QemuDmaBuf *dmabuf)
292 EGLSyncKHR sync;
294 if (epoxy_has_egl_extension(qemu_egl_display,
295 "EGL_KHR_fence_sync") &&
296 epoxy_has_egl_extension(qemu_egl_display,
297 "EGL_ANDROID_native_fence_sync")) {
298 sync = eglCreateSyncKHR(qemu_egl_display,
299 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
300 if (sync != EGL_NO_SYNC_KHR) {
301 dmabuf->sync = sync;
306 void egl_dmabuf_create_fence(QemuDmaBuf *dmabuf)
308 if (dmabuf->sync) {
309 dmabuf->fence_fd = eglDupNativeFenceFDANDROID(qemu_egl_display,
310 dmabuf->sync);
311 eglDestroySyncKHR(qemu_egl_display, dmabuf->sync);
312 dmabuf->sync = NULL;
316 #endif /* CONFIG_GBM */
318 /* ---------------------------------------------------------------------- */
320 EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, EGLNativeWindowType win)
322 EGLSurface esurface;
323 EGLBoolean b;
325 esurface = eglCreateWindowSurface(qemu_egl_display,
326 qemu_egl_config,
327 win, NULL);
328 if (esurface == EGL_NO_SURFACE) {
329 error_report("egl: eglCreateWindowSurface failed");
330 return NULL;
333 b = eglMakeCurrent(qemu_egl_display, esurface, esurface, ectx);
334 if (b == EGL_FALSE) {
335 error_report("egl: eglMakeCurrent failed");
336 return NULL;
339 return esurface;
342 /* ---------------------------------------------------------------------- */
344 #if defined(CONFIG_X11) || defined(CONFIG_GBM)
347 * Taken from glamor_egl.h from the Xorg xserver, which is MIT licensed
349 * Create an EGLDisplay from a native display type. This is a little quirky
350 * for a few reasons.
352 * 1: GetPlatformDisplayEXT and GetPlatformDisplay are the API you want to
353 * use, but have different function signatures in the third argument; this
354 * happens not to matter for us, at the moment, but it means epoxy won't alias
355 * them together.
357 * 2: epoxy 1.3 and earlier don't understand EGL client extensions, which
358 * means you can't call "eglGetPlatformDisplayEXT" directly, as the resolver
359 * will crash.
361 * 3: You can't tell whether you have EGL 1.5 at this point, because
362 * eglQueryString(EGL_VERSION) is a property of the display, which we don't
363 * have yet. So you have to query for extensions no matter what. Fortunately
364 * epoxy_has_egl_extension _does_ let you query for client extensions, so
365 * we don't have to write our own extension string parsing.
367 * 4. There is no EGL_KHR_platform_base to complement the EXT one, thus one
368 * needs to know EGL 1.5 is supported in order to use the eglGetPlatformDisplay
369 * function pointer.
370 * We can workaround this (circular dependency) by probing for the EGL 1.5
371 * platform extensions (EGL_KHR_platform_gbm and friends) yet it doesn't seem
372 * like mesa will be able to advertise these (even though it can do EGL 1.5).
374 static EGLDisplay qemu_egl_get_display(EGLNativeDisplayType native,
375 EGLenum platform)
377 EGLDisplay dpy = EGL_NO_DISPLAY;
379 /* In practise any EGL 1.5 implementation would support the EXT extension */
380 if (epoxy_has_egl_extension(NULL, "EGL_EXT_platform_base")) {
381 PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplayEXT =
382 (void *) eglGetProcAddress("eglGetPlatformDisplayEXT");
383 if (getPlatformDisplayEXT && platform != 0) {
384 dpy = getPlatformDisplayEXT(platform, native, NULL);
388 if (dpy == EGL_NO_DISPLAY) {
389 /* fallback */
390 dpy = eglGetDisplay(native);
392 return dpy;
395 static int qemu_egl_init_dpy(EGLNativeDisplayType dpy,
396 EGLenum platform,
397 DisplayGLMode mode)
399 static const EGLint conf_att_core[] = {
400 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
401 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
402 EGL_RED_SIZE, 5,
403 EGL_GREEN_SIZE, 5,
404 EGL_BLUE_SIZE, 5,
405 EGL_ALPHA_SIZE, 0,
406 EGL_NONE,
408 static const EGLint conf_att_gles[] = {
409 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
410 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
411 EGL_RED_SIZE, 5,
412 EGL_GREEN_SIZE, 5,
413 EGL_BLUE_SIZE, 5,
414 EGL_ALPHA_SIZE, 0,
415 EGL_NONE,
417 EGLint major, minor;
418 EGLBoolean b;
419 EGLint n;
420 bool gles = (mode == DISPLAYGL_MODE_ES);
422 qemu_egl_display = qemu_egl_get_display(dpy, platform);
423 if (qemu_egl_display == EGL_NO_DISPLAY) {
424 error_report("egl: eglGetDisplay failed");
425 return -1;
428 b = eglInitialize(qemu_egl_display, &major, &minor);
429 if (b == EGL_FALSE) {
430 error_report("egl: eglInitialize failed");
431 return -1;
434 b = eglBindAPI(gles ? EGL_OPENGL_ES_API : EGL_OPENGL_API);
435 if (b == EGL_FALSE) {
436 error_report("egl: eglBindAPI failed (%s mode)",
437 gles ? "gles" : "core");
438 return -1;
441 b = eglChooseConfig(qemu_egl_display,
442 gles ? conf_att_gles : conf_att_core,
443 &qemu_egl_config, 1, &n);
444 if (b == EGL_FALSE || n != 1) {
445 error_report("egl: eglChooseConfig failed (%s mode)",
446 gles ? "gles" : "core");
447 return -1;
450 qemu_egl_mode = gles ? DISPLAYGL_MODE_ES : DISPLAYGL_MODE_CORE;
451 return 0;
454 int qemu_egl_init_dpy_x11(EGLNativeDisplayType dpy, DisplayGLMode mode)
456 #ifdef EGL_KHR_platform_x11
457 return qemu_egl_init_dpy(dpy, EGL_PLATFORM_X11_KHR, mode);
458 #else
459 return qemu_egl_init_dpy(dpy, 0, mode);
460 #endif
463 int qemu_egl_init_dpy_mesa(EGLNativeDisplayType dpy, DisplayGLMode mode)
465 #ifdef EGL_MESA_platform_gbm
466 return qemu_egl_init_dpy(dpy, EGL_PLATFORM_GBM_MESA, mode);
467 #else
468 return qemu_egl_init_dpy(dpy, 0, mode);
469 #endif
472 #endif
474 bool qemu_egl_has_dmabuf(void)
476 if (qemu_egl_display == EGL_NO_DISPLAY) {
477 return false;
480 return epoxy_has_egl_extension(qemu_egl_display,
481 "EGL_EXT_image_dma_buf_import");
484 EGLContext qemu_egl_init_ctx(void)
486 static const EGLint ctx_att_core[] = {
487 EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT,
488 EGL_NONE
490 static const EGLint ctx_att_gles[] = {
491 EGL_CONTEXT_CLIENT_VERSION, 2,
492 EGL_NONE
494 bool gles = (qemu_egl_mode == DISPLAYGL_MODE_ES);
495 EGLContext ectx;
496 EGLBoolean b;
498 ectx = eglCreateContext(qemu_egl_display, qemu_egl_config, EGL_NO_CONTEXT,
499 gles ? ctx_att_gles : ctx_att_core);
500 if (ectx == EGL_NO_CONTEXT) {
501 error_report("egl: eglCreateContext failed");
502 return NULL;
505 b = eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, ectx);
506 if (b == EGL_FALSE) {
507 error_report("egl: eglMakeCurrent failed");
508 return NULL;
511 return ectx;