xref: /openbmc/qemu/ui/egl-helpers.c (revision e8d1e0cd)
1 /*
2  * Copyright (C) 2015-2016 Gerd Hoffmann <kraxel@redhat.com>
3  *
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.
8  *
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.
13  *
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/>.
16  */
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"
22 #include "sysemu/sysemu.h"
23 #include "qapi/error.h"
24 
25 EGLDisplay *qemu_egl_display;
26 EGLConfig qemu_egl_config;
27 DisplayGLMode qemu_egl_mode;
28 
29 /* ------------------------------------------------------------------ */
30 
31 #if defined(CONFIG_X11) || defined(CONFIG_GBM)
32 static const char *egl_get_error_string(void)
33 {
34     EGLint error = eglGetError();
35 
36     switch (error) {
37     case EGL_SUCCESS:
38         return "EGL_SUCCESS";
39     case EGL_NOT_INITIALIZED:
40         return "EGL_NOT_INITIALIZED";
41     case EGL_BAD_ACCESS:
42         return "EGL_BAD_ACCESS";
43     case EGL_BAD_ALLOC:
44         return "EGL_BAD_ALLOC";
45     case EGL_BAD_ATTRIBUTE:
46         return "EGL_BAD_ATTRIBUTE";
47     case EGL_BAD_CONTEXT:
48         return "EGL_BAD_CONTEXT";
49     case EGL_BAD_CONFIG:
50         return "EGL_BAD_CONFIG";
51     case EGL_BAD_CURRENT_SURFACE:
52         return "EGL_BAD_CURRENT_SURFACE";
53     case EGL_BAD_DISPLAY:
54         return "EGL_BAD_DISPLAY";
55     case EGL_BAD_SURFACE:
56         return "EGL_BAD_SURFACE";
57     case EGL_BAD_MATCH:
58         return "EGL_BAD_MATCH";
59     case EGL_BAD_PARAMETER:
60         return "EGL_BAD_PARAMETER";
61     case EGL_BAD_NATIVE_PIXMAP:
62         return "EGL_BAD_NATIVE_PIXMAP";
63     case EGL_BAD_NATIVE_WINDOW:
64         return "EGL_BAD_NATIVE_WINDOW";
65     case EGL_CONTEXT_LOST:
66         return "EGL_CONTEXT_LOST";
67     default:
68         return "Unknown EGL error";
69     }
70 }
71 #endif
72 
73 static void egl_fb_delete_texture(egl_fb *fb)
74 {
75     if (!fb->delete_texture) {
76         return;
77     }
78 
79     glDeleteTextures(1, &fb->texture);
80     fb->delete_texture = false;
81 }
82 
83 void egl_fb_destroy(egl_fb *fb)
84 {
85     if (!fb->framebuffer) {
86         return;
87     }
88 
89     egl_fb_delete_texture(fb);
90     glDeleteFramebuffers(1, &fb->framebuffer);
91 
92     fb->width = 0;
93     fb->height = 0;
94     fb->texture = 0;
95     fb->framebuffer = 0;
96 }
97 
98 void egl_fb_setup_default(egl_fb *fb, int width, int height)
99 {
100     fb->width = width;
101     fb->height = height;
102     fb->framebuffer = 0; /* default framebuffer */
103 }
104 
105 void egl_fb_setup_for_tex(egl_fb *fb, int width, int height,
106                           GLuint texture, bool delete)
107 {
108     egl_fb_delete_texture(fb);
109 
110     fb->width = width;
111     fb->height = height;
112     fb->texture = texture;
113     fb->delete_texture = delete;
114     if (!fb->framebuffer) {
115         glGenFramebuffers(1, &fb->framebuffer);
116     }
117 
118     glBindFramebuffer(GL_FRAMEBUFFER_EXT, fb->framebuffer);
119     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
120                               GL_TEXTURE_2D, fb->texture, 0);
121 }
122 
123 void egl_fb_setup_new_tex(egl_fb *fb, int width, int height)
124 {
125     GLuint texture;
126 
127     glGenTextures(1, &texture);
128     glBindTexture(GL_TEXTURE_2D, texture);
129     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height,
130                  0, GL_BGRA, GL_UNSIGNED_BYTE, 0);
131 
132     egl_fb_setup_for_tex(fb, width, height, texture, true);
133 }
134 
135 void egl_fb_blit(egl_fb *dst, egl_fb *src, bool flip)
136 {
137     GLuint x1 = 0;
138     GLuint y1 = 0;
139     GLuint x2, y2;
140     GLuint w = src->width;
141     GLuint h = src->height;
142 
143     glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer);
144     glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst->framebuffer);
145     glViewport(0, 0, dst->width, dst->height);
146 
147     if (src->dmabuf) {
148         x1 = src->dmabuf->x;
149         y1 = src->dmabuf->y;
150         w = src->dmabuf->scanout_width;
151         h = src->dmabuf->scanout_height;
152     }
153 
154     w = (x1 + w) > src->width ? src->width - x1 : w;
155     h = (y1 + h) > src->height ? src->height - y1 : h;
156 
157     y2 = flip ? y1 : h + y1;
158     y1 = flip ? h + y1 : y1;
159     x2 = x1 + w;
160 
161     glBlitFramebuffer(x1, y1, x2, y2,
162                       0, 0, dst->width, dst->height,
163                       GL_COLOR_BUFFER_BIT, GL_LINEAR);
164 }
165 
166 void egl_fb_read(DisplaySurface *dst, egl_fb *src)
167 {
168     glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer);
169     glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
170     glReadPixels(0, 0, surface_width(dst), surface_height(dst),
171                  GL_BGRA, GL_UNSIGNED_BYTE, surface_data(dst));
172 }
173 
174 void egl_texture_blit(QemuGLShader *gls, egl_fb *dst, egl_fb *src, bool flip)
175 {
176     glBindFramebuffer(GL_FRAMEBUFFER_EXT, dst->framebuffer);
177     glViewport(0, 0, dst->width, dst->height);
178     glEnable(GL_TEXTURE_2D);
179     glBindTexture(GL_TEXTURE_2D, src->texture);
180     qemu_gl_run_texture_blit(gls, flip);
181 }
182 
183 void egl_texture_blend(QemuGLShader *gls, egl_fb *dst, egl_fb *src, bool flip,
184                        int x, int y, double scale_x, double scale_y)
185 {
186     glBindFramebuffer(GL_FRAMEBUFFER_EXT, dst->framebuffer);
187     int w = scale_x * src->width;
188     int h = scale_y * src->height;
189     if (flip) {
190         glViewport(x, y, w, h);
191     } else {
192         glViewport(x, dst->height - h - y, w, h);
193     }
194     glEnable(GL_TEXTURE_2D);
195     glBindTexture(GL_TEXTURE_2D, src->texture);
196     glEnable(GL_BLEND);
197     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
198     qemu_gl_run_texture_blit(gls, flip);
199     glDisable(GL_BLEND);
200 }
201 
202 /* ---------------------------------------------------------------------- */
203 
204 #ifdef CONFIG_GBM
205 
206 int qemu_egl_rn_fd;
207 struct gbm_device *qemu_egl_rn_gbm_dev;
208 EGLContext qemu_egl_rn_ctx;
209 
210 int egl_rendernode_init(const char *rendernode, DisplayGLMode mode)
211 {
212     qemu_egl_rn_fd = -1;
213     int rc;
214 
215     qemu_egl_rn_fd = qemu_drm_rendernode_open(rendernode);
216     if (qemu_egl_rn_fd == -1) {
217         error_report("egl: no drm render node available");
218         goto err;
219     }
220 
221     qemu_egl_rn_gbm_dev = gbm_create_device(qemu_egl_rn_fd);
222     if (!qemu_egl_rn_gbm_dev) {
223         error_report("egl: gbm_create_device failed");
224         goto err;
225     }
226 
227     rc = qemu_egl_init_dpy_mesa((EGLNativeDisplayType)qemu_egl_rn_gbm_dev,
228                                 mode);
229     if (rc != 0) {
230         /* qemu_egl_init_dpy_mesa reports error */
231         goto err;
232     }
233 
234     if (!epoxy_has_egl_extension(qemu_egl_display,
235                                  "EGL_KHR_surfaceless_context")) {
236         error_report("egl: EGL_KHR_surfaceless_context not supported");
237         goto err;
238     }
239     if (!epoxy_has_egl_extension(qemu_egl_display,
240                                  "EGL_MESA_image_dma_buf_export")) {
241         error_report("egl: EGL_MESA_image_dma_buf_export not supported");
242         goto err;
243     }
244 
245     qemu_egl_rn_ctx = qemu_egl_init_ctx();
246     if (!qemu_egl_rn_ctx) {
247         error_report("egl: egl_init_ctx failed");
248         goto err;
249     }
250 
251     return 0;
252 
253 err:
254     if (qemu_egl_rn_gbm_dev) {
255         gbm_device_destroy(qemu_egl_rn_gbm_dev);
256     }
257     if (qemu_egl_rn_fd != -1) {
258         close(qemu_egl_rn_fd);
259     }
260 
261     return -1;
262 }
263 
264 int egl_get_fd_for_texture(uint32_t tex_id, EGLint *stride, EGLint *fourcc,
265                            EGLuint64KHR *modifier)
266 {
267     EGLImageKHR image;
268     EGLint num_planes, fd;
269 
270     image = eglCreateImageKHR(qemu_egl_display, eglGetCurrentContext(),
271                               EGL_GL_TEXTURE_2D_KHR,
272                               (EGLClientBuffer)(unsigned long)tex_id,
273                               NULL);
274     if (!image) {
275         return -1;
276     }
277 
278     eglExportDMABUFImageQueryMESA(qemu_egl_display, image, fourcc,
279                                   &num_planes, modifier);
280     if (num_planes != 1) {
281         eglDestroyImageKHR(qemu_egl_display, image);
282         return -1;
283     }
284     eglExportDMABUFImageMESA(qemu_egl_display, image, &fd, stride, NULL);
285     eglDestroyImageKHR(qemu_egl_display, image);
286 
287     return fd;
288 }
289 
290 void egl_dmabuf_import_texture(QemuDmaBuf *dmabuf)
291 {
292     EGLImageKHR image = EGL_NO_IMAGE_KHR;
293     EGLint attrs[64];
294     int i = 0;
295 
296     if (dmabuf->texture != 0) {
297         return;
298     }
299 
300     attrs[i++] = EGL_WIDTH;
301     attrs[i++] = dmabuf->width;
302     attrs[i++] = EGL_HEIGHT;
303     attrs[i++] = dmabuf->height;
304     attrs[i++] = EGL_LINUX_DRM_FOURCC_EXT;
305     attrs[i++] = dmabuf->fourcc;
306 
307     attrs[i++] = EGL_DMA_BUF_PLANE0_FD_EXT;
308     attrs[i++] = dmabuf->fd;
309     attrs[i++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
310     attrs[i++] = dmabuf->stride;
311     attrs[i++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
312     attrs[i++] = 0;
313 #ifdef EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT
314     if (dmabuf->modifier) {
315         attrs[i++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT;
316         attrs[i++] = (dmabuf->modifier >>  0) & 0xffffffff;
317         attrs[i++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT;
318         attrs[i++] = (dmabuf->modifier >> 32) & 0xffffffff;
319     }
320 #endif
321     attrs[i++] = EGL_NONE;
322 
323     image = eglCreateImageKHR(qemu_egl_display,
324                               EGL_NO_CONTEXT,
325                               EGL_LINUX_DMA_BUF_EXT,
326                               NULL, attrs);
327     if (image == EGL_NO_IMAGE_KHR) {
328         error_report("eglCreateImageKHR failed");
329         return;
330     }
331 
332     glGenTextures(1, &dmabuf->texture);
333     glBindTexture(GL_TEXTURE_2D, dmabuf->texture);
334     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
335     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
336 
337     glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
338     eglDestroyImageKHR(qemu_egl_display, image);
339 }
340 
341 void egl_dmabuf_release_texture(QemuDmaBuf *dmabuf)
342 {
343     if (dmabuf->texture == 0) {
344         return;
345     }
346 
347     glDeleteTextures(1, &dmabuf->texture);
348     dmabuf->texture = 0;
349 }
350 
351 void egl_dmabuf_create_sync(QemuDmaBuf *dmabuf)
352 {
353     EGLSyncKHR sync;
354 
355     if (epoxy_has_egl_extension(qemu_egl_display,
356                                 "EGL_KHR_fence_sync") &&
357         epoxy_has_egl_extension(qemu_egl_display,
358                                 "EGL_ANDROID_native_fence_sync")) {
359         sync = eglCreateSyncKHR(qemu_egl_display,
360                                 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
361         if (sync != EGL_NO_SYNC_KHR) {
362             dmabuf->sync = sync;
363         }
364     }
365 }
366 
367 void egl_dmabuf_create_fence(QemuDmaBuf *dmabuf)
368 {
369     if (dmabuf->sync) {
370         dmabuf->fence_fd = eglDupNativeFenceFDANDROID(qemu_egl_display,
371                                                       dmabuf->sync);
372         eglDestroySyncKHR(qemu_egl_display, dmabuf->sync);
373         dmabuf->sync = NULL;
374     }
375 }
376 
377 #endif /* CONFIG_GBM */
378 
379 /* ---------------------------------------------------------------------- */
380 
381 EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, EGLNativeWindowType win)
382 {
383     EGLSurface esurface;
384     EGLBoolean b;
385 
386     esurface = eglCreateWindowSurface(qemu_egl_display,
387                                       qemu_egl_config,
388                                       win, NULL);
389     if (esurface == EGL_NO_SURFACE) {
390         error_report("egl: eglCreateWindowSurface failed");
391         return NULL;
392     }
393 
394     b = eglMakeCurrent(qemu_egl_display, esurface, esurface, ectx);
395     if (b == EGL_FALSE) {
396         error_report("egl: eglMakeCurrent failed");
397         return NULL;
398     }
399 
400     return esurface;
401 }
402 
403 /* ---------------------------------------------------------------------- */
404 
405 #if defined(CONFIG_X11) || defined(CONFIG_GBM)
406 
407 /*
408  * Taken from glamor_egl.h from the Xorg xserver, which is MIT licensed
409  *
410  * Create an EGLDisplay from a native display type. This is a little quirky
411  * for a few reasons.
412  *
413  * 1: GetPlatformDisplayEXT and GetPlatformDisplay are the API you want to
414  * use, but have different function signatures in the third argument; this
415  * happens not to matter for us, at the moment, but it means epoxy won't alias
416  * them together.
417  *
418  * 2: epoxy 1.3 and earlier don't understand EGL client extensions, which
419  * means you can't call "eglGetPlatformDisplayEXT" directly, as the resolver
420  * will crash.
421  *
422  * 3: You can't tell whether you have EGL 1.5 at this point, because
423  * eglQueryString(EGL_VERSION) is a property of the display, which we don't
424  * have yet. So you have to query for extensions no matter what. Fortunately
425  * epoxy_has_egl_extension _does_ let you query for client extensions, so
426  * we don't have to write our own extension string parsing.
427  *
428  * 4. There is no EGL_KHR_platform_base to complement the EXT one, thus one
429  * needs to know EGL 1.5 is supported in order to use the eglGetPlatformDisplay
430  * function pointer.
431  * We can workaround this (circular dependency) by probing for the EGL 1.5
432  * platform extensions (EGL_KHR_platform_gbm and friends) yet it doesn't seem
433  * like mesa will be able to advertise these (even though it can do EGL 1.5).
434  */
435 static EGLDisplay qemu_egl_get_display(EGLNativeDisplayType native,
436                                        EGLenum platform)
437 {
438     EGLDisplay dpy = EGL_NO_DISPLAY;
439 
440     /* In practise any EGL 1.5 implementation would support the EXT extension */
441     if (epoxy_has_egl_extension(NULL, "EGL_EXT_platform_base")) {
442         PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplayEXT =
443             (void *) eglGetProcAddress("eglGetPlatformDisplayEXT");
444         if (getPlatformDisplayEXT && platform != 0) {
445             dpy = getPlatformDisplayEXT(platform, native, NULL);
446         }
447     }
448 
449     if (dpy == EGL_NO_DISPLAY) {
450         /* fallback */
451         dpy = eglGetDisplay(native);
452     }
453     return dpy;
454 }
455 
456 static int qemu_egl_init_dpy(EGLNativeDisplayType dpy,
457                              EGLenum platform,
458                              DisplayGLMode mode)
459 {
460     static const EGLint conf_att_core[] = {
461         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
462         EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
463         EGL_RED_SIZE,   5,
464         EGL_GREEN_SIZE, 5,
465         EGL_BLUE_SIZE,  5,
466         EGL_ALPHA_SIZE, 0,
467         EGL_NONE,
468     };
469     static const EGLint conf_att_gles[] = {
470         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
471         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
472         EGL_RED_SIZE,   5,
473         EGL_GREEN_SIZE, 5,
474         EGL_BLUE_SIZE,  5,
475         EGL_ALPHA_SIZE, 0,
476         EGL_NONE,
477     };
478     EGLint major, minor;
479     EGLBoolean b;
480     EGLint n;
481     bool gles = (mode == DISPLAYGL_MODE_ES);
482 
483     qemu_egl_display = qemu_egl_get_display(dpy, platform);
484     if (qemu_egl_display == EGL_NO_DISPLAY) {
485         error_report("egl: eglGetDisplay failed: %s", egl_get_error_string());
486         return -1;
487     }
488 
489     b = eglInitialize(qemu_egl_display, &major, &minor);
490     if (b == EGL_FALSE) {
491         error_report("egl: eglInitialize failed: %s", egl_get_error_string());
492         return -1;
493     }
494 
495     b = eglBindAPI(gles ?  EGL_OPENGL_ES_API : EGL_OPENGL_API);
496     if (b == EGL_FALSE) {
497         error_report("egl: eglBindAPI failed (%s mode): %s",
498                      gles ? "gles" : "core", egl_get_error_string());
499         return -1;
500     }
501 
502     b = eglChooseConfig(qemu_egl_display,
503                         gles ? conf_att_gles : conf_att_core,
504                         &qemu_egl_config, 1, &n);
505     if (b == EGL_FALSE || n != 1) {
506         error_report("egl: eglChooseConfig failed (%s mode): %s",
507                      gles ? "gles" : "core", egl_get_error_string());
508         return -1;
509     }
510 
511     qemu_egl_mode = gles ? DISPLAYGL_MODE_ES : DISPLAYGL_MODE_CORE;
512     return 0;
513 }
514 
515 int qemu_egl_init_dpy_x11(EGLNativeDisplayType dpy, DisplayGLMode mode)
516 {
517 #ifdef EGL_KHR_platform_x11
518     return qemu_egl_init_dpy(dpy, EGL_PLATFORM_X11_KHR, mode);
519 #else
520     return qemu_egl_init_dpy(dpy, 0, mode);
521 #endif
522 }
523 
524 int qemu_egl_init_dpy_mesa(EGLNativeDisplayType dpy, DisplayGLMode mode)
525 {
526 #ifdef EGL_MESA_platform_gbm
527     return qemu_egl_init_dpy(dpy, EGL_PLATFORM_GBM_MESA, mode);
528 #else
529     return qemu_egl_init_dpy(dpy, 0, mode);
530 #endif
531 }
532 
533 #endif
534 
535 bool qemu_egl_has_dmabuf(void)
536 {
537     if (qemu_egl_display == EGL_NO_DISPLAY) {
538         return false;
539     }
540 
541     return epoxy_has_egl_extension(qemu_egl_display,
542                                    "EGL_EXT_image_dma_buf_import");
543 }
544 
545 EGLContext qemu_egl_init_ctx(void)
546 {
547     static const EGLint ctx_att_core[] = {
548         EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT,
549         EGL_NONE
550     };
551     static const EGLint ctx_att_gles[] = {
552         EGL_CONTEXT_CLIENT_VERSION, 2,
553         EGL_NONE
554     };
555     bool gles = (qemu_egl_mode == DISPLAYGL_MODE_ES);
556     EGLContext ectx;
557     EGLBoolean b;
558 
559     ectx = eglCreateContext(qemu_egl_display, qemu_egl_config, EGL_NO_CONTEXT,
560                             gles ? ctx_att_gles : ctx_att_core);
561     if (ectx == EGL_NO_CONTEXT) {
562         error_report("egl: eglCreateContext failed");
563         return NULL;
564     }
565 
566     b = eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, ectx);
567     if (b == EGL_FALSE) {
568         error_report("egl: eglMakeCurrent failed");
569         return NULL;
570     }
571 
572     return ectx;
573 }
574 
575 bool egl_init(const char *rendernode, DisplayGLMode mode, Error **errp)
576 {
577     ERRP_GUARD();
578 
579     if (mode == DISPLAYGL_MODE_OFF) {
580         error_setg(errp, "egl: turning off GL doesn't make sense");
581         return false;
582     }
583 
584 #ifdef CONFIG_GBM
585     if (egl_rendernode_init(rendernode, mode) < 0) {
586         error_setg(errp, "egl: render node init failed");
587         return false;
588     }
589     display_opengl = 1;
590     return true;
591 #else
592     error_setg(errp, "egl: not available on this platform");
593     return false;
594 #endif
595 }
596