]> Git Repo - qemu.git/blob - ui/egl-helpers.c
egl-helpers: add modifier support to egl_get_fd_for_texture().
[qemu.git] / ui / egl-helpers.c
1 /*
2  * Copyright (C) 2015-2016 Gerd Hoffmann <[email protected]>
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
23 EGLDisplay *qemu_egl_display;
24 EGLConfig qemu_egl_config;
25 DisplayGLMode qemu_egl_mode;
26
27 /* ------------------------------------------------------------------ */
28
29 static void egl_fb_delete_texture(egl_fb *fb)
30 {
31     if (!fb->delete_texture) {
32         return;
33     }
34
35     glDeleteTextures(1, &fb->texture);
36     fb->delete_texture = false;
37 }
38
39 void egl_fb_destroy(egl_fb *fb)
40 {
41     if (!fb->framebuffer) {
42         return;
43     }
44
45     egl_fb_delete_texture(fb);
46     glDeleteFramebuffers(1, &fb->framebuffer);
47
48     fb->width = 0;
49     fb->height = 0;
50     fb->texture = 0;
51     fb->framebuffer = 0;
52 }
53
54 void egl_fb_setup_default(egl_fb *fb, int width, int height)
55 {
56     fb->width = width;
57     fb->height = height;
58     fb->framebuffer = 0; /* default framebuffer */
59 }
60
61 void egl_fb_setup_for_tex(egl_fb *fb, int width, int height,
62                           GLuint texture, bool delete)
63 {
64     egl_fb_delete_texture(fb);
65
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);
72     }
73
74     glBindFramebuffer(GL_FRAMEBUFFER_EXT, fb->framebuffer);
75     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
76                               GL_TEXTURE_2D, fb->texture, 0);
77 }
78
79 void egl_fb_setup_new_tex(egl_fb *fb, int width, int height)
80 {
81     GLuint texture;
82
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);
87
88     egl_fb_setup_for_tex(fb, width, height, texture, true);
89 }
90
91 void egl_fb_blit(egl_fb *dst, egl_fb *src, bool flip)
92 {
93     GLuint y1, y2;
94
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);
103 }
104
105 void egl_fb_read(void *dst, egl_fb *src)
106 {
107     glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer);
108     glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
109     glReadPixels(0, 0, src->width, src->height,
110                  GL_BGRA, GL_UNSIGNED_BYTE, dst);
111 }
112
113 void egl_texture_blit(QemuGLShader *gls, egl_fb *dst, egl_fb *src, bool flip)
114 {
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);
120 }
121
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)
124 {
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);
132     }
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);
139 }
140
141 /* ---------------------------------------------------------------------- */
142
143 #ifdef CONFIG_OPENGL_DMABUF
144
145 int qemu_egl_rn_fd;
146 struct gbm_device *qemu_egl_rn_gbm_dev;
147 EGLContext qemu_egl_rn_ctx;
148
149 int egl_rendernode_init(const char *rendernode, DisplayGLMode mode)
150 {
151     qemu_egl_rn_fd = -1;
152     int rc;
153
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;
158     }
159
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;
164     }
165
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;
171     }
172
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;
177     }
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;
182     }
183
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;
188     }
189
190     return 0;
191
192 err:
193     if (qemu_egl_rn_gbm_dev) {
194         gbm_device_destroy(qemu_egl_rn_gbm_dev);
195     }
196     if (qemu_egl_rn_fd != -1) {
197         close(qemu_egl_rn_fd);
198     }
199
200     return -1;
201 }
202
203 int egl_get_fd_for_texture(uint32_t tex_id, EGLint *stride, EGLint *fourcc,
204                            EGLuint64KHR *modifier)
205 {
206     EGLImageKHR image;
207     EGLint num_planes, fd;
208
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;
215     }
216
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;
222     }
223     eglExportDMABUFImageMESA(qemu_egl_display, image, &fd, stride, NULL);
224     eglDestroyImageKHR(qemu_egl_display, image);
225
226     return fd;
227 }
228
229 void egl_dmabuf_import_texture(QemuDmaBuf *dmabuf)
230 {
231     EGLImageKHR image = EGL_NO_IMAGE_KHR;
232     EGLint attrs[] = {
233         EGL_DMA_BUF_PLANE0_FD_EXT,      dmabuf->fd,
234         EGL_DMA_BUF_PLANE0_PITCH_EXT,   dmabuf->stride,
235         EGL_DMA_BUF_PLANE0_OFFSET_EXT,  0,
236         EGL_WIDTH,                      dmabuf->width,
237         EGL_HEIGHT,                     dmabuf->height,
238         EGL_LINUX_DRM_FOURCC_EXT,       dmabuf->fourcc,
239         EGL_NONE, /* end of list */
240     };
241
242     if (dmabuf->texture != 0) {
243         return;
244     }
245
246     image = eglCreateImageKHR(qemu_egl_display,
247                               EGL_NO_CONTEXT,
248                               EGL_LINUX_DMA_BUF_EXT,
249                               NULL, attrs);
250     if (image == EGL_NO_IMAGE_KHR) {
251         error_report("eglCreateImageKHR failed");
252         return;
253     }
254
255     glGenTextures(1, &dmabuf->texture);
256     glBindTexture(GL_TEXTURE_2D, dmabuf->texture);
257     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
258     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
259
260     glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
261     eglDestroyImageKHR(qemu_egl_display, image);
262 }
263
264 void egl_dmabuf_release_texture(QemuDmaBuf *dmabuf)
265 {
266     if (dmabuf->texture == 0) {
267         return;
268     }
269
270     glDeleteTextures(1, &dmabuf->texture);
271     dmabuf->texture = 0;
272 }
273
274 #endif /* CONFIG_OPENGL_DMABUF */
275
276 /* ---------------------------------------------------------------------- */
277
278 EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, EGLNativeWindowType win)
279 {
280     EGLSurface esurface;
281     EGLBoolean b;
282
283     esurface = eglCreateWindowSurface(qemu_egl_display,
284                                       qemu_egl_config,
285                                       win, NULL);
286     if (esurface == EGL_NO_SURFACE) {
287         error_report("egl: eglCreateWindowSurface failed");
288         return NULL;
289     }
290
291     b = eglMakeCurrent(qemu_egl_display, esurface, esurface, ectx);
292     if (b == EGL_FALSE) {
293         error_report("egl: eglMakeCurrent failed");
294         return NULL;
295     }
296
297     return esurface;
298 }
299
300 /* ---------------------------------------------------------------------- */
301
302 /*
303  * Taken from glamor_egl.h from the Xorg xserver, which is MIT licensed
304  *
305  * Create an EGLDisplay from a native display type. This is a little quirky
306  * for a few reasons.
307  *
308  * 1: GetPlatformDisplayEXT and GetPlatformDisplay are the API you want to
309  * use, but have different function signatures in the third argument; this
310  * happens not to matter for us, at the moment, but it means epoxy won't alias
311  * them together.
312  *
313  * 2: epoxy 1.3 and earlier don't understand EGL client extensions, which
314  * means you can't call "eglGetPlatformDisplayEXT" directly, as the resolver
315  * will crash.
316  *
317  * 3: You can't tell whether you have EGL 1.5 at this point, because
318  * eglQueryString(EGL_VERSION) is a property of the display, which we don't
319  * have yet. So you have to query for extensions no matter what. Fortunately
320  * epoxy_has_egl_extension _does_ let you query for client extensions, so
321  * we don't have to write our own extension string parsing.
322  *
323  * 4. There is no EGL_KHR_platform_base to complement the EXT one, thus one
324  * needs to know EGL 1.5 is supported in order to use the eglGetPlatformDisplay
325  * function pointer.
326  * We can workaround this (circular dependency) by probing for the EGL 1.5
327  * platform extensions (EGL_KHR_platform_gbm and friends) yet it doesn't seem
328  * like mesa will be able to advertise these (even though it can do EGL 1.5).
329  */
330 static EGLDisplay qemu_egl_get_display(EGLNativeDisplayType native,
331                                        EGLenum platform)
332 {
333     EGLDisplay dpy = EGL_NO_DISPLAY;
334
335     /* In practise any EGL 1.5 implementation would support the EXT extension */
336     if (epoxy_has_egl_extension(NULL, "EGL_EXT_platform_base")) {
337         PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplayEXT =
338             (void *) eglGetProcAddress("eglGetPlatformDisplayEXT");
339         if (getPlatformDisplayEXT && platform != 0) {
340             dpy = getPlatformDisplayEXT(platform, native, NULL);
341         }
342     }
343
344     if (dpy == EGL_NO_DISPLAY) {
345         /* fallback */
346         dpy = eglGetDisplay(native);
347     }
348     return dpy;
349 }
350
351 static int qemu_egl_init_dpy(EGLNativeDisplayType dpy,
352                              EGLenum platform,
353                              DisplayGLMode mode)
354 {
355     static const EGLint conf_att_core[] = {
356         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
357         EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
358         EGL_RED_SIZE,   5,
359         EGL_GREEN_SIZE, 5,
360         EGL_BLUE_SIZE,  5,
361         EGL_ALPHA_SIZE, 0,
362         EGL_NONE,
363     };
364     static const EGLint conf_att_gles[] = {
365         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
366         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
367         EGL_RED_SIZE,   5,
368         EGL_GREEN_SIZE, 5,
369         EGL_BLUE_SIZE,  5,
370         EGL_ALPHA_SIZE, 0,
371         EGL_NONE,
372     };
373     EGLint major, minor;
374     EGLBoolean b;
375     EGLint n;
376     bool gles = (mode == DISPLAYGL_MODE_ES);
377
378     qemu_egl_display = qemu_egl_get_display(dpy, platform);
379     if (qemu_egl_display == EGL_NO_DISPLAY) {
380         error_report("egl: eglGetDisplay failed");
381         return -1;
382     }
383
384     b = eglInitialize(qemu_egl_display, &major, &minor);
385     if (b == EGL_FALSE) {
386         error_report("egl: eglInitialize failed");
387         return -1;
388     }
389
390     b = eglBindAPI(gles ?  EGL_OPENGL_ES_API : EGL_OPENGL_API);
391     if (b == EGL_FALSE) {
392         error_report("egl: eglBindAPI failed (%s mode)",
393                      gles ? "gles" : "core");
394         return -1;
395     }
396
397     b = eglChooseConfig(qemu_egl_display,
398                         gles ? conf_att_gles : conf_att_core,
399                         &qemu_egl_config, 1, &n);
400     if (b == EGL_FALSE || n != 1) {
401         error_report("egl: eglChooseConfig failed (%s mode)",
402                      gles ? "gles" : "core");
403         return -1;
404     }
405
406     qemu_egl_mode = gles ? DISPLAYGL_MODE_ES : DISPLAYGL_MODE_CORE;
407     return 0;
408 }
409
410 int qemu_egl_init_dpy_x11(EGLNativeDisplayType dpy, DisplayGLMode mode)
411 {
412 #ifdef EGL_KHR_platform_x11
413     return qemu_egl_init_dpy(dpy, EGL_PLATFORM_X11_KHR, mode);
414 #else
415     return qemu_egl_init_dpy(dpy, 0, mode);
416 #endif
417 }
418
419 int qemu_egl_init_dpy_mesa(EGLNativeDisplayType dpy, DisplayGLMode mode)
420 {
421 #ifdef EGL_MESA_platform_gbm
422     return qemu_egl_init_dpy(dpy, EGL_PLATFORM_GBM_MESA, mode);
423 #else
424     return qemu_egl_init_dpy(dpy, 0, mode);
425 #endif
426 }
427
428 EGLContext qemu_egl_init_ctx(void)
429 {
430     static const EGLint ctx_att_core[] = {
431         EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT,
432         EGL_NONE
433     };
434     static const EGLint ctx_att_gles[] = {
435         EGL_CONTEXT_CLIENT_VERSION, 2,
436         EGL_NONE
437     };
438     bool gles = (qemu_egl_mode == DISPLAYGL_MODE_ES);
439     EGLContext ectx;
440     EGLBoolean b;
441
442     ectx = eglCreateContext(qemu_egl_display, qemu_egl_config, EGL_NO_CONTEXT,
443                             gles ? ctx_att_gles : ctx_att_core);
444     if (ectx == EGL_NO_CONTEXT) {
445         error_report("egl: eglCreateContext failed");
446         return NULL;
447     }
448
449     b = eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, ectx);
450     if (b == EGL_FALSE) {
451         error_report("egl: eglMakeCurrent failed");
452         return NULL;
453     }
454
455     return ectx;
456 }
This page took 0.049797 seconds and 4 git commands to generate.