]>
Commit | Line | Data |
---|---|---|
6250dff3 FZ |
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 | */ | |
e16f4c87 | 17 | #include "qemu/osdep.h" |
7ced9e9f | 18 | #include <glob.h> |
1e316598 | 19 | #include <dirent.h> |
7ced9e9f | 20 | |
38a55bdd | 21 | #include "qemu/error-report.h" |
86c0522c | 22 | #include "ui/console.h" |
7ced9e9f GH |
23 | #include "ui/egl-helpers.h" |
24 | ||
25 | EGLDisplay *qemu_egl_display; | |
26 | EGLConfig qemu_egl_config; | |
27 | ||
6fafc260 GH |
28 | /* ------------------------------------------------------------------ */ |
29 | ||
74083f9c GH |
30 | static void egl_fb_delete_texture(egl_fb *fb) |
31 | { | |
32 | if (!fb->delete_texture) { | |
33 | return; | |
34 | } | |
35 | ||
36 | glDeleteTextures(1, &fb->texture); | |
37 | fb->delete_texture = false; | |
38 | } | |
39 | ||
6fafc260 GH |
40 | void egl_fb_destroy(egl_fb *fb) |
41 | { | |
42 | if (!fb->framebuffer) { | |
43 | return; | |
44 | } | |
45 | ||
74083f9c | 46 | egl_fb_delete_texture(fb); |
6fafc260 GH |
47 | glDeleteFramebuffers(1, &fb->framebuffer); |
48 | ||
49 | fb->width = 0; | |
50 | fb->height = 0; | |
51 | fb->texture = 0; | |
52 | fb->framebuffer = 0; | |
53 | } | |
54 | ||
55 | void egl_fb_setup_default(egl_fb *fb, int width, int height) | |
56 | { | |
57 | fb->width = width; | |
58 | fb->height = height; | |
59 | fb->framebuffer = 0; /* default framebuffer */ | |
60 | } | |
61 | ||
74083f9c GH |
62 | void egl_fb_setup_for_tex(egl_fb *fb, int width, int height, |
63 | GLuint texture, bool delete) | |
6fafc260 | 64 | { |
74083f9c GH |
65 | egl_fb_delete_texture(fb); |
66 | ||
6fafc260 GH |
67 | fb->width = width; |
68 | fb->height = height; | |
69 | fb->texture = texture; | |
74083f9c | 70 | fb->delete_texture = delete; |
6fafc260 GH |
71 | if (!fb->framebuffer) { |
72 | glGenFramebuffers(1, &fb->framebuffer); | |
73 | } | |
74 | ||
75 | glBindFramebuffer(GL_FRAMEBUFFER_EXT, fb->framebuffer); | |
76 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, | |
77 | GL_TEXTURE_2D, fb->texture, 0); | |
78 | } | |
79 | ||
74083f9c | 80 | void egl_fb_setup_new_tex(egl_fb *fb, int width, int height) |
6fafc260 GH |
81 | { |
82 | GLuint texture; | |
83 | ||
84 | glGenTextures(1, &texture); | |
85 | glBindTexture(GL_TEXTURE_2D, texture); | |
41126214 | 86 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, |
6fafc260 GH |
87 | 0, GL_BGRA, GL_UNSIGNED_BYTE, 0); |
88 | ||
74083f9c | 89 | egl_fb_setup_for_tex(fb, width, height, texture, true); |
6fafc260 GH |
90 | } |
91 | ||
92 | void egl_fb_blit(egl_fb *dst, egl_fb *src, bool flip) | |
93 | { | |
94 | GLuint y1, y2; | |
95 | ||
96 | glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer); | |
97 | glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst->framebuffer); | |
98 | glViewport(0, 0, dst->width, dst->height); | |
99 | y1 = flip ? src->height : 0; | |
100 | y2 = flip ? 0 : src->height; | |
101 | glBlitFramebuffer(0, y1, src->width, y2, | |
102 | 0, 0, dst->width, dst->height, | |
103 | GL_COLOR_BUFFER_BIT, GL_LINEAR); | |
104 | } | |
105 | ||
106 | void egl_fb_read(void *dst, egl_fb *src) | |
107 | { | |
108 | glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer); | |
109 | glReadBuffer(GL_COLOR_ATTACHMENT0_EXT); | |
110 | glReadPixels(0, 0, src->width, src->height, | |
111 | GL_BGRA, GL_UNSIGNED_BYTE, dst); | |
112 | } | |
113 | ||
0eb50c27 GH |
114 | void egl_texture_blit(QemuGLShader *gls, egl_fb *dst, egl_fb *src, bool flip) |
115 | { | |
116 | glBindFramebuffer(GL_FRAMEBUFFER_EXT, dst->framebuffer); | |
117 | glViewport(0, 0, dst->width, dst->height); | |
118 | glEnable(GL_TEXTURE_2D); | |
119 | glBindTexture(GL_TEXTURE_2D, src->texture); | |
120 | qemu_gl_run_texture_blit(gls, flip); | |
121 | } | |
122 | ||
123 | void egl_texture_blend(QemuGLShader *gls, egl_fb *dst, egl_fb *src, bool flip, | |
124 | int x, int y) | |
125 | { | |
126 | glBindFramebuffer(GL_FRAMEBUFFER_EXT, dst->framebuffer); | |
127 | if (flip) { | |
128 | glViewport(x, y, src->width, src->height); | |
129 | } else { | |
130 | glViewport(x, dst->height - src->height - y, | |
131 | src->width, src->height); | |
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 | ||
7ced9e9f GH |
141 | /* ---------------------------------------------------------------------- */ |
142 | ||
1e316598 GH |
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 | ||
7b525508 | 149 | static int qemu_egl_rendernode_open(const char *rendernode) |
1e316598 GH |
150 | { |
151 | DIR *dir; | |
152 | struct dirent *e; | |
153 | int r, fd; | |
154 | char *p; | |
155 | ||
7b525508 MAL |
156 | if (rendernode) { |
157 | return open(rendernode, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK); | |
158 | } | |
159 | ||
1e316598 GH |
160 | dir = opendir("/dev/dri"); |
161 | if (!dir) { | |
162 | return -1; | |
163 | } | |
164 | ||
165 | fd = -1; | |
166 | while ((e = readdir(dir))) { | |
167 | if (e->d_type != DT_CHR) { | |
168 | continue; | |
169 | } | |
170 | ||
171 | if (strncmp(e->d_name, "renderD", 7)) { | |
172 | continue; | |
173 | } | |
174 | ||
f454f49c | 175 | p = g_strdup_printf("/dev/dri/%s", e->d_name); |
1e316598 GH |
176 | |
177 | r = open(p, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK); | |
178 | if (r < 0) { | |
f454f49c | 179 | g_free(p); |
1e316598 GH |
180 | continue; |
181 | } | |
182 | fd = r; | |
f454f49c | 183 | g_free(p); |
1e316598 GH |
184 | break; |
185 | } | |
186 | ||
187 | closedir(dir); | |
188 | if (fd < 0) { | |
189 | return -1; | |
190 | } | |
191 | return fd; | |
192 | } | |
193 | ||
7b525508 | 194 | int egl_rendernode_init(const char *rendernode) |
1e316598 GH |
195 | { |
196 | qemu_egl_rn_fd = -1; | |
151c8e60 | 197 | int rc; |
1e316598 | 198 | |
7b525508 | 199 | qemu_egl_rn_fd = qemu_egl_rendernode_open(rendernode); |
1e316598 | 200 | if (qemu_egl_rn_fd == -1) { |
38a55bdd | 201 | error_report("egl: no drm render node available"); |
1e316598 GH |
202 | goto err; |
203 | } | |
204 | ||
205 | qemu_egl_rn_gbm_dev = gbm_create_device(qemu_egl_rn_fd); | |
206 | if (!qemu_egl_rn_gbm_dev) { | |
38a55bdd | 207 | error_report("egl: gbm_create_device failed"); |
1e316598 GH |
208 | goto err; |
209 | } | |
210 | ||
151c8e60 GH |
211 | rc = qemu_egl_init_dpy_mesa((EGLNativeDisplayType)qemu_egl_rn_gbm_dev); |
212 | if (rc != 0) { | |
213 | /* qemu_egl_init_dpy_mesa reports error */ | |
214 | goto err; | |
215 | } | |
1e316598 GH |
216 | |
217 | if (!epoxy_has_egl_extension(qemu_egl_display, | |
218 | "EGL_KHR_surfaceless_context")) { | |
38a55bdd | 219 | error_report("egl: EGL_KHR_surfaceless_context not supported"); |
1e316598 GH |
220 | goto err; |
221 | } | |
222 | if (!epoxy_has_egl_extension(qemu_egl_display, | |
223 | "EGL_MESA_image_dma_buf_export")) { | |
38a55bdd | 224 | error_report("egl: EGL_MESA_image_dma_buf_export not supported"); |
1e316598 GH |
225 | goto err; |
226 | } | |
227 | ||
228 | qemu_egl_rn_ctx = qemu_egl_init_ctx(); | |
229 | if (!qemu_egl_rn_ctx) { | |
38a55bdd | 230 | error_report("egl: egl_init_ctx failed"); |
1e316598 GH |
231 | goto err; |
232 | } | |
233 | ||
234 | return 0; | |
235 | ||
236 | err: | |
237 | if (qemu_egl_rn_gbm_dev) { | |
238 | gbm_device_destroy(qemu_egl_rn_gbm_dev); | |
239 | } | |
240 | if (qemu_egl_rn_fd != -1) { | |
241 | close(qemu_egl_rn_fd); | |
242 | } | |
243 | ||
244 | return -1; | |
245 | } | |
246 | ||
247 | int egl_get_fd_for_texture(uint32_t tex_id, EGLint *stride, EGLint *fourcc) | |
248 | { | |
249 | EGLImageKHR image; | |
250 | EGLint num_planes, fd; | |
251 | ||
252 | image = eglCreateImageKHR(qemu_egl_display, eglGetCurrentContext(), | |
253 | EGL_GL_TEXTURE_2D_KHR, | |
254 | (EGLClientBuffer)(unsigned long)tex_id, | |
255 | NULL); | |
256 | if (!image) { | |
257 | return -1; | |
258 | } | |
259 | ||
260 | eglExportDMABUFImageQueryMESA(qemu_egl_display, image, fourcc, | |
261 | &num_planes, NULL); | |
262 | if (num_planes != 1) { | |
263 | eglDestroyImageKHR(qemu_egl_display, image); | |
264 | return -1; | |
265 | } | |
266 | eglExportDMABUFImageMESA(qemu_egl_display, image, &fd, stride, NULL); | |
267 | eglDestroyImageKHR(qemu_egl_display, image); | |
268 | ||
269 | return fd; | |
270 | } | |
271 | ||
86c0522c GH |
272 | void egl_dmabuf_import_texture(QemuDmaBuf *dmabuf) |
273 | { | |
274 | EGLImageKHR image = EGL_NO_IMAGE_KHR; | |
275 | EGLint attrs[] = { | |
276 | EGL_DMA_BUF_PLANE0_FD_EXT, dmabuf->fd, | |
277 | EGL_DMA_BUF_PLANE0_PITCH_EXT, dmabuf->stride, | |
278 | EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0, | |
279 | EGL_WIDTH, dmabuf->width, | |
280 | EGL_HEIGHT, dmabuf->height, | |
281 | EGL_LINUX_DRM_FOURCC_EXT, dmabuf->fourcc, | |
282 | EGL_NONE, /* end of list */ | |
283 | }; | |
284 | ||
285 | if (dmabuf->texture != 0) { | |
286 | return; | |
287 | } | |
288 | ||
289 | image = eglCreateImageKHR(qemu_egl_display, | |
290 | EGL_NO_CONTEXT, | |
291 | EGL_LINUX_DMA_BUF_EXT, | |
292 | NULL, attrs); | |
293 | if (image == EGL_NO_IMAGE_KHR) { | |
294 | error_report("eglCreateImageKHR failed"); | |
295 | return; | |
296 | } | |
297 | ||
298 | glGenTextures(1, &dmabuf->texture); | |
299 | glBindTexture(GL_TEXTURE_2D, dmabuf->texture); | |
300 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
301 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
302 | ||
303 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image); | |
304 | eglDestroyImageKHR(qemu_egl_display, image); | |
305 | } | |
306 | ||
307 | void egl_dmabuf_release_texture(QemuDmaBuf *dmabuf) | |
308 | { | |
309 | if (dmabuf->texture == 0) { | |
310 | return; | |
311 | } | |
312 | ||
313 | glDeleteTextures(1, &dmabuf->texture); | |
314 | dmabuf->texture = 0; | |
315 | } | |
316 | ||
1e316598 GH |
317 | #endif /* CONFIG_OPENGL_DMABUF */ |
318 | ||
319 | /* ---------------------------------------------------------------------- */ | |
320 | ||
7ced9e9f GH |
321 | EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, Window win) |
322 | { | |
323 | EGLSurface esurface; | |
324 | EGLBoolean b; | |
325 | ||
7ced9e9f GH |
326 | esurface = eglCreateWindowSurface(qemu_egl_display, |
327 | qemu_egl_config, | |
328 | (EGLNativeWindowType)win, NULL); | |
329 | if (esurface == EGL_NO_SURFACE) { | |
38a55bdd | 330 | error_report("egl: eglCreateWindowSurface failed"); |
7ced9e9f GH |
331 | return NULL; |
332 | } | |
333 | ||
334 | b = eglMakeCurrent(qemu_egl_display, esurface, esurface, ectx); | |
335 | if (b == EGL_FALSE) { | |
38a55bdd | 336 | error_report("egl: eglMakeCurrent failed"); |
7ced9e9f GH |
337 | return NULL; |
338 | } | |
339 | ||
340 | return esurface; | |
341 | } | |
342 | ||
343 | /* ---------------------------------------------------------------------- */ | |
344 | ||
8bce03e3 GH |
345 | /* |
346 | * Taken from glamor_egl.h from the Xorg xserver, which is MIT licensed | |
347 | * | |
348 | * Create an EGLDisplay from a native display type. This is a little quirky | |
349 | * for a few reasons. | |
350 | * | |
351 | * 1: GetPlatformDisplayEXT and GetPlatformDisplay are the API you want to | |
352 | * use, but have different function signatures in the third argument; this | |
353 | * happens not to matter for us, at the moment, but it means epoxy won't alias | |
354 | * them together. | |
355 | * | |
356 | * 2: epoxy 1.3 and earlier don't understand EGL client extensions, which | |
357 | * means you can't call "eglGetPlatformDisplayEXT" directly, as the resolver | |
358 | * will crash. | |
359 | * | |
360 | * 3: You can't tell whether you have EGL 1.5 at this point, because | |
361 | * eglQueryString(EGL_VERSION) is a property of the display, which we don't | |
362 | * have yet. So you have to query for extensions no matter what. Fortunately | |
363 | * epoxy_has_egl_extension _does_ let you query for client extensions, so | |
364 | * we don't have to write our own extension string parsing. | |
365 | * | |
366 | * 4. There is no EGL_KHR_platform_base to complement the EXT one, thus one | |
367 | * needs to know EGL 1.5 is supported in order to use the eglGetPlatformDisplay | |
368 | * function pointer. | |
369 | * We can workaround this (circular dependency) by probing for the EGL 1.5 | |
370 | * platform extensions (EGL_KHR_platform_gbm and friends) yet it doesn't seem | |
371 | * like mesa will be able to advertise these (even though it can do EGL 1.5). | |
372 | */ | |
e1913dbb GH |
373 | static EGLDisplay qemu_egl_get_display(EGLNativeDisplayType native, |
374 | EGLenum platform) | |
8bce03e3 GH |
375 | { |
376 | EGLDisplay dpy = EGL_NO_DISPLAY; | |
377 | ||
8bce03e3 GH |
378 | /* In practise any EGL 1.5 implementation would support the EXT extension */ |
379 | if (epoxy_has_egl_extension(NULL, "EGL_EXT_platform_base")) { | |
380 | PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplayEXT = | |
381 | (void *) eglGetProcAddress("eglGetPlatformDisplayEXT"); | |
e1913dbb GH |
382 | if (getPlatformDisplayEXT && platform != 0) { |
383 | dpy = getPlatformDisplayEXT(platform, native, NULL); | |
8bce03e3 GH |
384 | } |
385 | } | |
8bce03e3 GH |
386 | |
387 | if (dpy == EGL_NO_DISPLAY) { | |
388 | /* fallback */ | |
389 | dpy = eglGetDisplay(native); | |
390 | } | |
391 | return dpy; | |
392 | } | |
393 | ||
e1913dbb GH |
394 | static int qemu_egl_init_dpy(EGLNativeDisplayType dpy, |
395 | EGLenum platform) | |
7ced9e9f GH |
396 | { |
397 | static const EGLint conf_att_gl[] = { | |
398 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, | |
399 | EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, | |
400 | EGL_RED_SIZE, 5, | |
401 | EGL_GREEN_SIZE, 5, | |
402 | EGL_BLUE_SIZE, 5, | |
403 | EGL_ALPHA_SIZE, 0, | |
404 | EGL_NONE, | |
405 | }; | |
7ced9e9f GH |
406 | EGLint major, minor; |
407 | EGLBoolean b; | |
408 | EGLint n; | |
409 | ||
e1913dbb | 410 | qemu_egl_display = qemu_egl_get_display(dpy, platform); |
7ced9e9f | 411 | if (qemu_egl_display == EGL_NO_DISPLAY) { |
38a55bdd | 412 | error_report("egl: eglGetDisplay failed"); |
7ced9e9f GH |
413 | return -1; |
414 | } | |
415 | ||
7ced9e9f GH |
416 | b = eglInitialize(qemu_egl_display, &major, &minor); |
417 | if (b == EGL_FALSE) { | |
38a55bdd | 418 | error_report("egl: eglInitialize failed"); |
7ced9e9f GH |
419 | return -1; |
420 | } | |
421 | ||
9f728c79 | 422 | b = eglBindAPI(EGL_OPENGL_API); |
7ced9e9f | 423 | if (b == EGL_FALSE) { |
38a55bdd | 424 | error_report("egl: eglBindAPI failed"); |
7ced9e9f GH |
425 | return -1; |
426 | } | |
427 | ||
9f728c79 | 428 | b = eglChooseConfig(qemu_egl_display, conf_att_gl, |
7ced9e9f GH |
429 | &qemu_egl_config, 1, &n); |
430 | if (b == EGL_FALSE || n != 1) { | |
38a55bdd | 431 | error_report("egl: eglChooseConfig failed"); |
7ced9e9f GH |
432 | return -1; |
433 | } | |
7ced9e9f GH |
434 | return 0; |
435 | } | |
436 | ||
e1913dbb GH |
437 | int qemu_egl_init_dpy_x11(EGLNativeDisplayType dpy) |
438 | { | |
439 | #ifdef EGL_KHR_platform_x11 | |
440 | return qemu_egl_init_dpy(dpy, EGL_PLATFORM_X11_KHR); | |
441 | #else | |
442 | return qemu_egl_init_dpy(dpy, 0); | |
443 | #endif | |
444 | } | |
445 | ||
446 | int qemu_egl_init_dpy_mesa(EGLNativeDisplayType dpy) | |
447 | { | |
448 | #ifdef EGL_MESA_platform_gbm | |
449 | return qemu_egl_init_dpy(dpy, EGL_PLATFORM_GBM_MESA); | |
450 | #else | |
451 | return qemu_egl_init_dpy(dpy, 0); | |
452 | #endif | |
453 | } | |
454 | ||
7ced9e9f GH |
455 | EGLContext qemu_egl_init_ctx(void) |
456 | { | |
457 | static const EGLint ctx_att_gl[] = { | |
bc8c946f | 458 | EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT, |
7ced9e9f GH |
459 | EGL_NONE |
460 | }; | |
7ced9e9f GH |
461 | EGLContext ectx; |
462 | EGLBoolean b; | |
463 | ||
7ced9e9f | 464 | ectx = eglCreateContext(qemu_egl_display, qemu_egl_config, EGL_NO_CONTEXT, |
9f728c79 | 465 | ctx_att_gl); |
7ced9e9f | 466 | if (ectx == EGL_NO_CONTEXT) { |
38a55bdd | 467 | error_report("egl: eglCreateContext failed"); |
7ced9e9f GH |
468 | return NULL; |
469 | } | |
470 | ||
471 | b = eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, ectx); | |
472 | if (b == EGL_FALSE) { | |
38a55bdd | 473 | error_report("egl: eglMakeCurrent failed"); |
7ced9e9f GH |
474 | return NULL; |
475 | } | |
476 | ||
477 | return ectx; | |
478 | } |