]>
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" |
b1d38037 | 18 | #include "qemu/drm.h" |
38a55bdd | 19 | #include "qemu/error-report.h" |
86c0522c | 20 | #include "ui/console.h" |
7ced9e9f GH |
21 | #include "ui/egl-helpers.h" |
22 | ||
23 | EGLDisplay *qemu_egl_display; | |
24 | EGLConfig qemu_egl_config; | |
54d208ff | 25 | DisplayGLMode qemu_egl_mode; |
7ced9e9f | 26 | |
6fafc260 GH |
27 | /* ------------------------------------------------------------------ */ |
28 | ||
74083f9c GH |
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 | ||
6fafc260 GH |
39 | void egl_fb_destroy(egl_fb *fb) |
40 | { | |
41 | if (!fb->framebuffer) { | |
42 | return; | |
43 | } | |
44 | ||
74083f9c | 45 | egl_fb_delete_texture(fb); |
6fafc260 GH |
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 | ||
74083f9c GH |
61 | void egl_fb_setup_for_tex(egl_fb *fb, int width, int height, |
62 | GLuint texture, bool delete) | |
6fafc260 | 63 | { |
74083f9c GH |
64 | egl_fb_delete_texture(fb); |
65 | ||
6fafc260 GH |
66 | fb->width = width; |
67 | fb->height = height; | |
68 | fb->texture = texture; | |
74083f9c | 69 | fb->delete_texture = delete; |
6fafc260 GH |
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 | ||
74083f9c | 79 | void egl_fb_setup_new_tex(egl_fb *fb, int width, int height) |
6fafc260 GH |
80 | { |
81 | GLuint texture; | |
82 | ||
83 | glGenTextures(1, &texture); | |
84 | glBindTexture(GL_TEXTURE_2D, texture); | |
41126214 | 85 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, |
6fafc260 GH |
86 | 0, GL_BGRA, GL_UNSIGNED_BYTE, 0); |
87 | ||
74083f9c | 88 | egl_fb_setup_for_tex(fb, width, height, texture, true); |
6fafc260 GH |
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 | ||
0eb50c27 GH |
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, | |
051a0cde | 123 | int x, int y, double scale_x, double scale_y) |
0eb50c27 GH |
124 | { |
125 | glBindFramebuffer(GL_FRAMEBUFFER_EXT, dst->framebuffer); | |
051a0cde CZ |
126 | int w = scale_x * src->width; |
127 | int h = scale_y * src->height; | |
0eb50c27 | 128 | if (flip) { |
051a0cde | 129 | glViewport(x, y, w, h); |
0eb50c27 | 130 | } else { |
051a0cde | 131 | glViewport(x, dst->height - h - y, w, h); |
0eb50c27 GH |
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 | ||
54d208ff | 149 | int egl_rendernode_init(const char *rendernode, DisplayGLMode mode) |
1e316598 GH |
150 | { |
151 | qemu_egl_rn_fd = -1; | |
151c8e60 | 152 | int rc; |
1e316598 | 153 | |
b1d38037 | 154 | qemu_egl_rn_fd = qemu_drm_rendernode_open(rendernode); |
1e316598 | 155 | if (qemu_egl_rn_fd == -1) { |
38a55bdd | 156 | error_report("egl: no drm render node available"); |
1e316598 GH |
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) { | |
38a55bdd | 162 | error_report("egl: gbm_create_device failed"); |
1e316598 GH |
163 | goto err; |
164 | } | |
165 | ||
54d208ff GH |
166 | rc = qemu_egl_init_dpy_mesa((EGLNativeDisplayType)qemu_egl_rn_gbm_dev, |
167 | mode); | |
151c8e60 GH |
168 | if (rc != 0) { |
169 | /* qemu_egl_init_dpy_mesa reports error */ | |
170 | goto err; | |
171 | } | |
1e316598 GH |
172 | |
173 | if (!epoxy_has_egl_extension(qemu_egl_display, | |
174 | "EGL_KHR_surfaceless_context")) { | |
38a55bdd | 175 | error_report("egl: EGL_KHR_surfaceless_context not supported"); |
1e316598 GH |
176 | goto err; |
177 | } | |
178 | if (!epoxy_has_egl_extension(qemu_egl_display, | |
179 | "EGL_MESA_image_dma_buf_export")) { | |
38a55bdd | 180 | error_report("egl: EGL_MESA_image_dma_buf_export not supported"); |
1e316598 GH |
181 | goto err; |
182 | } | |
183 | ||
184 | qemu_egl_rn_ctx = qemu_egl_init_ctx(); | |
185 | if (!qemu_egl_rn_ctx) { | |
38a55bdd | 186 | error_report("egl: egl_init_ctx failed"); |
1e316598 GH |
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 | { | |
205 | EGLImageKHR image; | |
206 | EGLint num_planes, fd; | |
207 | ||
208 | image = eglCreateImageKHR(qemu_egl_display, eglGetCurrentContext(), | |
209 | EGL_GL_TEXTURE_2D_KHR, | |
210 | (EGLClientBuffer)(unsigned long)tex_id, | |
211 | NULL); | |
212 | if (!image) { | |
213 | return -1; | |
214 | } | |
215 | ||
216 | eglExportDMABUFImageQueryMESA(qemu_egl_display, image, fourcc, | |
217 | &num_planes, NULL); | |
218 | if (num_planes != 1) { | |
219 | eglDestroyImageKHR(qemu_egl_display, image); | |
220 | return -1; | |
221 | } | |
222 | eglExportDMABUFImageMESA(qemu_egl_display, image, &fd, stride, NULL); | |
223 | eglDestroyImageKHR(qemu_egl_display, image); | |
224 | ||
225 | return fd; | |
226 | } | |
227 | ||
86c0522c GH |
228 | void egl_dmabuf_import_texture(QemuDmaBuf *dmabuf) |
229 | { | |
230 | EGLImageKHR image = EGL_NO_IMAGE_KHR; | |
231 | EGLint attrs[] = { | |
232 | EGL_DMA_BUF_PLANE0_FD_EXT, dmabuf->fd, | |
233 | EGL_DMA_BUF_PLANE0_PITCH_EXT, dmabuf->stride, | |
234 | EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0, | |
235 | EGL_WIDTH, dmabuf->width, | |
236 | EGL_HEIGHT, dmabuf->height, | |
237 | EGL_LINUX_DRM_FOURCC_EXT, dmabuf->fourcc, | |
238 | EGL_NONE, /* end of list */ | |
239 | }; | |
240 | ||
241 | if (dmabuf->texture != 0) { | |
242 | return; | |
243 | } | |
244 | ||
245 | image = eglCreateImageKHR(qemu_egl_display, | |
246 | EGL_NO_CONTEXT, | |
247 | EGL_LINUX_DMA_BUF_EXT, | |
248 | NULL, attrs); | |
249 | if (image == EGL_NO_IMAGE_KHR) { | |
250 | error_report("eglCreateImageKHR failed"); | |
251 | return; | |
252 | } | |
253 | ||
254 | glGenTextures(1, &dmabuf->texture); | |
255 | glBindTexture(GL_TEXTURE_2D, dmabuf->texture); | |
256 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
257 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
258 | ||
259 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image); | |
260 | eglDestroyImageKHR(qemu_egl_display, image); | |
261 | } | |
262 | ||
263 | void egl_dmabuf_release_texture(QemuDmaBuf *dmabuf) | |
264 | { | |
265 | if (dmabuf->texture == 0) { | |
266 | return; | |
267 | } | |
268 | ||
269 | glDeleteTextures(1, &dmabuf->texture); | |
270 | dmabuf->texture = 0; | |
271 | } | |
272 | ||
1e316598 GH |
273 | #endif /* CONFIG_OPENGL_DMABUF */ |
274 | ||
275 | /* ---------------------------------------------------------------------- */ | |
276 | ||
fbd57c75 | 277 | EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, EGLNativeWindowType win) |
7ced9e9f GH |
278 | { |
279 | EGLSurface esurface; | |
280 | EGLBoolean b; | |
281 | ||
7ced9e9f GH |
282 | esurface = eglCreateWindowSurface(qemu_egl_display, |
283 | qemu_egl_config, | |
fbd57c75 | 284 | win, NULL); |
7ced9e9f | 285 | if (esurface == EGL_NO_SURFACE) { |
38a55bdd | 286 | error_report("egl: eglCreateWindowSurface failed"); |
7ced9e9f GH |
287 | return NULL; |
288 | } | |
289 | ||
290 | b = eglMakeCurrent(qemu_egl_display, esurface, esurface, ectx); | |
291 | if (b == EGL_FALSE) { | |
38a55bdd | 292 | error_report("egl: eglMakeCurrent failed"); |
7ced9e9f GH |
293 | return NULL; |
294 | } | |
295 | ||
296 | return esurface; | |
297 | } | |
298 | ||
299 | /* ---------------------------------------------------------------------- */ | |
300 | ||
8bce03e3 GH |
301 | /* |
302 | * Taken from glamor_egl.h from the Xorg xserver, which is MIT licensed | |
303 | * | |
304 | * Create an EGLDisplay from a native display type. This is a little quirky | |
305 | * for a few reasons. | |
306 | * | |
307 | * 1: GetPlatformDisplayEXT and GetPlatformDisplay are the API you want to | |
308 | * use, but have different function signatures in the third argument; this | |
309 | * happens not to matter for us, at the moment, but it means epoxy won't alias | |
310 | * them together. | |
311 | * | |
312 | * 2: epoxy 1.3 and earlier don't understand EGL client extensions, which | |
313 | * means you can't call "eglGetPlatformDisplayEXT" directly, as the resolver | |
314 | * will crash. | |
315 | * | |
316 | * 3: You can't tell whether you have EGL 1.5 at this point, because | |
317 | * eglQueryString(EGL_VERSION) is a property of the display, which we don't | |
318 | * have yet. So you have to query for extensions no matter what. Fortunately | |
319 | * epoxy_has_egl_extension _does_ let you query for client extensions, so | |
320 | * we don't have to write our own extension string parsing. | |
321 | * | |
322 | * 4. There is no EGL_KHR_platform_base to complement the EXT one, thus one | |
323 | * needs to know EGL 1.5 is supported in order to use the eglGetPlatformDisplay | |
324 | * function pointer. | |
325 | * We can workaround this (circular dependency) by probing for the EGL 1.5 | |
326 | * platform extensions (EGL_KHR_platform_gbm and friends) yet it doesn't seem | |
327 | * like mesa will be able to advertise these (even though it can do EGL 1.5). | |
328 | */ | |
e1913dbb GH |
329 | static EGLDisplay qemu_egl_get_display(EGLNativeDisplayType native, |
330 | EGLenum platform) | |
8bce03e3 GH |
331 | { |
332 | EGLDisplay dpy = EGL_NO_DISPLAY; | |
333 | ||
8bce03e3 GH |
334 | /* In practise any EGL 1.5 implementation would support the EXT extension */ |
335 | if (epoxy_has_egl_extension(NULL, "EGL_EXT_platform_base")) { | |
336 | PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplayEXT = | |
337 | (void *) eglGetProcAddress("eglGetPlatformDisplayEXT"); | |
e1913dbb GH |
338 | if (getPlatformDisplayEXT && platform != 0) { |
339 | dpy = getPlatformDisplayEXT(platform, native, NULL); | |
8bce03e3 GH |
340 | } |
341 | } | |
8bce03e3 GH |
342 | |
343 | if (dpy == EGL_NO_DISPLAY) { | |
344 | /* fallback */ | |
345 | dpy = eglGetDisplay(native); | |
346 | } | |
347 | return dpy; | |
348 | } | |
349 | ||
e1913dbb | 350 | static int qemu_egl_init_dpy(EGLNativeDisplayType dpy, |
54d208ff GH |
351 | EGLenum platform, |
352 | DisplayGLMode mode) | |
7ced9e9f | 353 | { |
54d208ff | 354 | static const EGLint conf_att_core[] = { |
7ced9e9f GH |
355 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
356 | EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, | |
357 | EGL_RED_SIZE, 5, | |
358 | EGL_GREEN_SIZE, 5, | |
359 | EGL_BLUE_SIZE, 5, | |
360 | EGL_ALPHA_SIZE, 0, | |
361 | EGL_NONE, | |
362 | }; | |
54d208ff GH |
363 | static const EGLint conf_att_gles[] = { |
364 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, | |
365 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, | |
366 | EGL_RED_SIZE, 5, | |
367 | EGL_GREEN_SIZE, 5, | |
368 | EGL_BLUE_SIZE, 5, | |
369 | EGL_ALPHA_SIZE, 0, | |
370 | EGL_NONE, | |
371 | }; | |
7ced9e9f GH |
372 | EGLint major, minor; |
373 | EGLBoolean b; | |
374 | EGLint n; | |
54d208ff | 375 | bool gles = (mode == DISPLAYGL_MODE_ES); |
7ced9e9f | 376 | |
e1913dbb | 377 | qemu_egl_display = qemu_egl_get_display(dpy, platform); |
7ced9e9f | 378 | if (qemu_egl_display == EGL_NO_DISPLAY) { |
38a55bdd | 379 | error_report("egl: eglGetDisplay failed"); |
7ced9e9f GH |
380 | return -1; |
381 | } | |
382 | ||
7ced9e9f GH |
383 | b = eglInitialize(qemu_egl_display, &major, &minor); |
384 | if (b == EGL_FALSE) { | |
38a55bdd | 385 | error_report("egl: eglInitialize failed"); |
7ced9e9f GH |
386 | return -1; |
387 | } | |
388 | ||
54d208ff | 389 | b = eglBindAPI(gles ? EGL_OPENGL_ES_API : EGL_OPENGL_API); |
7ced9e9f | 390 | if (b == EGL_FALSE) { |
54d208ff GH |
391 | error_report("egl: eglBindAPI failed (%s mode)", |
392 | gles ? "gles" : "core"); | |
7ced9e9f GH |
393 | return -1; |
394 | } | |
395 | ||
54d208ff GH |
396 | b = eglChooseConfig(qemu_egl_display, |
397 | gles ? conf_att_gles : conf_att_core, | |
7ced9e9f GH |
398 | &qemu_egl_config, 1, &n); |
399 | if (b == EGL_FALSE || n != 1) { | |
54d208ff GH |
400 | error_report("egl: eglChooseConfig failed (%s mode)", |
401 | gles ? "gles" : "core"); | |
7ced9e9f GH |
402 | return -1; |
403 | } | |
54d208ff GH |
404 | |
405 | qemu_egl_mode = gles ? DISPLAYGL_MODE_ES : DISPLAYGL_MODE_CORE; | |
7ced9e9f GH |
406 | return 0; |
407 | } | |
408 | ||
54d208ff | 409 | int qemu_egl_init_dpy_x11(EGLNativeDisplayType dpy, DisplayGLMode mode) |
e1913dbb GH |
410 | { |
411 | #ifdef EGL_KHR_platform_x11 | |
54d208ff | 412 | return qemu_egl_init_dpy(dpy, EGL_PLATFORM_X11_KHR, mode); |
e1913dbb | 413 | #else |
54d208ff | 414 | return qemu_egl_init_dpy(dpy, 0, mode); |
e1913dbb GH |
415 | #endif |
416 | } | |
417 | ||
54d208ff | 418 | int qemu_egl_init_dpy_mesa(EGLNativeDisplayType dpy, DisplayGLMode mode) |
e1913dbb GH |
419 | { |
420 | #ifdef EGL_MESA_platform_gbm | |
54d208ff | 421 | return qemu_egl_init_dpy(dpy, EGL_PLATFORM_GBM_MESA, mode); |
e1913dbb | 422 | #else |
54d208ff | 423 | return qemu_egl_init_dpy(dpy, 0, mode); |
e1913dbb GH |
424 | #endif |
425 | } | |
426 | ||
7ced9e9f GH |
427 | EGLContext qemu_egl_init_ctx(void) |
428 | { | |
54d208ff | 429 | static const EGLint ctx_att_core[] = { |
bc8c946f | 430 | EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT, |
7ced9e9f GH |
431 | EGL_NONE |
432 | }; | |
54d208ff GH |
433 | static const EGLint ctx_att_gles[] = { |
434 | EGL_CONTEXT_CLIENT_VERSION, 2, | |
435 | EGL_NONE | |
436 | }; | |
437 | bool gles = (qemu_egl_mode == DISPLAYGL_MODE_ES); | |
7ced9e9f GH |
438 | EGLContext ectx; |
439 | EGLBoolean b; | |
440 | ||
7ced9e9f | 441 | ectx = eglCreateContext(qemu_egl_display, qemu_egl_config, EGL_NO_CONTEXT, |
54d208ff | 442 | gles ? ctx_att_gles : ctx_att_core); |
7ced9e9f | 443 | if (ectx == EGL_NO_CONTEXT) { |
38a55bdd | 444 | error_report("egl: eglCreateContext failed"); |
7ced9e9f GH |
445 | return NULL; |
446 | } | |
447 | ||
448 | b = eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, ectx); | |
449 | if (b == EGL_FALSE) { | |
38a55bdd | 450 | error_report("egl: eglMakeCurrent failed"); |
7ced9e9f GH |
451 | return NULL; |
452 | } | |
453 | ||
454 | return ectx; | |
455 | } |