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