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