Frediano Ziglio | 6250dff | 2016-12-08 10:45:39 +0000 | [diff] [blame] | 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 | */ |
Peter Maydell | e16f4c8 | 2016-01-29 17:49:51 +0000 | [diff] [blame] | 17 | #include "qemu/osdep.h" |
Marc-André Lureau | 06c63a3 | 2023-06-06 15:56:55 +0400 | [diff] [blame] | 18 | |
Marc-André Lureau | b1d3803 | 2018-07-13 15:09:06 +0200 | [diff] [blame] | 19 | #include "qemu/drm.h" |
Cole Robinson | 38a55bd | 2016-05-18 12:40:49 -0400 | [diff] [blame] | 20 | #include "qemu/error-report.h" |
Gerd Hoffmann | 86c0522 | 2017-10-10 15:54:51 +0200 | [diff] [blame] | 21 | #include "ui/console.h" |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 22 | #include "ui/egl-helpers.h" |
Marc-André Lureau | 0e1be59 | 2023-02-14 16:11:24 +0400 | [diff] [blame] | 23 | #include "sysemu/sysemu.h" |
| 24 | #include "qapi/error.h" |
Marc-André Lureau | 06c63a3 | 2023-06-06 15:56:55 +0400 | [diff] [blame] | 25 | #include "trace.h" |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 26 | |
| 27 | EGLDisplay *qemu_egl_display; |
| 28 | EGLConfig qemu_egl_config; |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 29 | DisplayGLMode qemu_egl_mode; |
Marc-André Lureau | 06c63a3 | 2023-06-06 15:56:55 +0400 | [diff] [blame] | 30 | bool qemu_egl_angle_d3d; |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 31 | |
Gerd Hoffmann | 6fafc26 | 2017-06-14 10:41:46 +0200 | [diff] [blame] | 32 | /* ------------------------------------------------------------------ */ |
| 33 | |
Marc-André Lureau | 044ca4b | 2023-06-06 15:56:38 +0400 | [diff] [blame] | 34 | const char *qemu_egl_get_error_string(void) |
Marc-André Lureau | 1f086ef | 2023-02-14 15:46:58 +0400 | [diff] [blame] | 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 | } |
Marc-André Lureau | 1f086ef | 2023-02-14 15:46:58 +0400 | [diff] [blame] | 73 | |
Gerd Hoffmann | 74083f9 | 2017-09-27 13:50:31 +0200 | [diff] [blame] | 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 | |
Gerd Hoffmann | 6fafc26 | 2017-06-14 10:41:46 +0200 | [diff] [blame] | 84 | void egl_fb_destroy(egl_fb *fb) |
| 85 | { |
| 86 | if (!fb->framebuffer) { |
| 87 | return; |
| 88 | } |
| 89 | |
Gerd Hoffmann | 74083f9 | 2017-09-27 13:50:31 +0200 | [diff] [blame] | 90 | egl_fb_delete_texture(fb); |
Gerd Hoffmann | 6fafc26 | 2017-06-14 10:41:46 +0200 | [diff] [blame] | 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 | |
Gerd Hoffmann | 74083f9 | 2017-09-27 13:50:31 +0200 | [diff] [blame] | 106 | void egl_fb_setup_for_tex(egl_fb *fb, int width, int height, |
| 107 | GLuint texture, bool delete) |
Gerd Hoffmann | 6fafc26 | 2017-06-14 10:41:46 +0200 | [diff] [blame] | 108 | { |
Gerd Hoffmann | 74083f9 | 2017-09-27 13:50:31 +0200 | [diff] [blame] | 109 | egl_fb_delete_texture(fb); |
| 110 | |
Gerd Hoffmann | 6fafc26 | 2017-06-14 10:41:46 +0200 | [diff] [blame] | 111 | fb->width = width; |
| 112 | fb->height = height; |
| 113 | fb->texture = texture; |
Gerd Hoffmann | 74083f9 | 2017-09-27 13:50:31 +0200 | [diff] [blame] | 114 | fb->delete_texture = delete; |
Gerd Hoffmann | 6fafc26 | 2017-06-14 10:41:46 +0200 | [diff] [blame] | 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 | |
Gerd Hoffmann | 74083f9 | 2017-09-27 13:50:31 +0200 | [diff] [blame] | 124 | void egl_fb_setup_new_tex(egl_fb *fb, int width, int height) |
Gerd Hoffmann | 6fafc26 | 2017-06-14 10:41:46 +0200 | [diff] [blame] | 125 | { |
| 126 | GLuint texture; |
| 127 | |
| 128 | glGenTextures(1, &texture); |
| 129 | glBindTexture(GL_TEXTURE_2D, texture); |
Gerd Hoffmann | 4112621 | 2018-02-20 12:04:33 +0100 | [diff] [blame] | 130 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, |
Gerd Hoffmann | 6fafc26 | 2017-06-14 10:41:46 +0200 | [diff] [blame] | 131 | 0, GL_BGRA, GL_UNSIGNED_BYTE, 0); |
| 132 | |
Gerd Hoffmann | 74083f9 | 2017-09-27 13:50:31 +0200 | [diff] [blame] | 133 | egl_fb_setup_for_tex(fb, width, height, texture, true); |
Gerd Hoffmann | 6fafc26 | 2017-06-14 10:41:46 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void egl_fb_blit(egl_fb *dst, egl_fb *src, bool flip) |
| 137 | { |
Dongwon Kim | 1350ff1 | 2021-11-03 23:51:53 -0700 | [diff] [blame] | 138 | GLuint x1 = 0; |
| 139 | GLuint y1 = 0; |
| 140 | GLuint x2, y2; |
| 141 | GLuint w = src->width; |
| 142 | GLuint h = src->height; |
Gerd Hoffmann | 6fafc26 | 2017-06-14 10:41:46 +0200 | [diff] [blame] | 143 | |
| 144 | glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer); |
| 145 | glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst->framebuffer); |
| 146 | glViewport(0, 0, dst->width, dst->height); |
Dongwon Kim | 1350ff1 | 2021-11-03 23:51:53 -0700 | [diff] [blame] | 147 | |
| 148 | if (src->dmabuf) { |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 149 | x1 = qemu_dmabuf_get_x(src->dmabuf); |
| 150 | y1 = qemu_dmabuf_get_y(src->dmabuf); |
| 151 | w = qemu_dmabuf_get_width(src->dmabuf); |
| 152 | h = qemu_dmabuf_get_height(src->dmabuf); |
Dongwon Kim | 1350ff1 | 2021-11-03 23:51:53 -0700 | [diff] [blame] | 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, |
Gerd Hoffmann | 6fafc26 | 2017-06-14 10:41:46 +0200 | [diff] [blame] | 163 | 0, 0, dst->width, dst->height, |
| 164 | GL_COLOR_BUFFER_BIT, GL_LINEAR); |
| 165 | } |
| 166 | |
Gerd Hoffmann | d232923 | 2019-09-09 09:39:11 +0200 | [diff] [blame] | 167 | void egl_fb_read(DisplaySurface *dst, egl_fb *src) |
Gerd Hoffmann | 6fafc26 | 2017-06-14 10:41:46 +0200 | [diff] [blame] | 168 | { |
| 169 | glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer); |
| 170 | glReadBuffer(GL_COLOR_ATTACHMENT0_EXT); |
Gerd Hoffmann | d232923 | 2019-09-09 09:39:11 +0200 | [diff] [blame] | 171 | glReadPixels(0, 0, surface_width(dst), surface_height(dst), |
| 172 | GL_BGRA, GL_UNSIGNED_BYTE, surface_data(dst)); |
Gerd Hoffmann | 6fafc26 | 2017-06-14 10:41:46 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Marc-André Lureau | da9eb58 | 2023-06-06 15:56:51 +0400 | [diff] [blame] | 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 | |
Gerd Hoffmann | 0eb50c2 | 2017-10-10 15:54:52 +0200 | [diff] [blame] | 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, |
Chen Zhang | 051a0cd | 2019-01-25 15:47:23 +0800 | [diff] [blame] | 199 | int x, int y, double scale_x, double scale_y) |
Gerd Hoffmann | 0eb50c2 | 2017-10-10 15:54:52 +0200 | [diff] [blame] | 200 | { |
| 201 | glBindFramebuffer(GL_FRAMEBUFFER_EXT, dst->framebuffer); |
Chen Zhang | 051a0cd | 2019-01-25 15:47:23 +0800 | [diff] [blame] | 202 | int w = scale_x * src->width; |
| 203 | int h = scale_y * src->height; |
Gerd Hoffmann | 0eb50c2 | 2017-10-10 15:54:52 +0200 | [diff] [blame] | 204 | if (flip) { |
Chen Zhang | 051a0cd | 2019-01-25 15:47:23 +0800 | [diff] [blame] | 205 | glViewport(x, y, w, h); |
Gerd Hoffmann | 0eb50c2 | 2017-10-10 15:54:52 +0200 | [diff] [blame] | 206 | } else { |
Chen Zhang | 051a0cd | 2019-01-25 15:47:23 +0800 | [diff] [blame] | 207 | glViewport(x, dst->height - h - y, w, h); |
Gerd Hoffmann | 0eb50c2 | 2017-10-10 15:54:52 +0200 | [diff] [blame] | 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 | |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 217 | /* ---------------------------------------------------------------------- */ |
| 218 | |
Marc-André Lureau | 39324b4 | 2023-06-06 15:56:49 +0400 | [diff] [blame] | 219 | EGLContext qemu_egl_rn_ctx; |
| 220 | |
Akihiko Odaki | bc6a356 | 2021-02-23 15:03:07 +0900 | [diff] [blame] | 221 | #ifdef CONFIG_GBM |
Gerd Hoffmann | 1e31659 | 2015-10-12 12:03:49 +0200 | [diff] [blame] | 222 | |
| 223 | int qemu_egl_rn_fd; |
| 224 | struct gbm_device *qemu_egl_rn_gbm_dev; |
Gerd Hoffmann | 1e31659 | 2015-10-12 12:03:49 +0200 | [diff] [blame] | 225 | |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 226 | int egl_rendernode_init(const char *rendernode, DisplayGLMode mode) |
Gerd Hoffmann | 1e31659 | 2015-10-12 12:03:49 +0200 | [diff] [blame] | 227 | { |
| 228 | qemu_egl_rn_fd = -1; |
Gerd Hoffmann | 151c8e6 | 2017-05-05 12:40:59 +0200 | [diff] [blame] | 229 | int rc; |
Gerd Hoffmann | 1e31659 | 2015-10-12 12:03:49 +0200 | [diff] [blame] | 230 | |
Marc-André Lureau | b1d3803 | 2018-07-13 15:09:06 +0200 | [diff] [blame] | 231 | qemu_egl_rn_fd = qemu_drm_rendernode_open(rendernode); |
Gerd Hoffmann | 1e31659 | 2015-10-12 12:03:49 +0200 | [diff] [blame] | 232 | if (qemu_egl_rn_fd == -1) { |
Cole Robinson | 38a55bd | 2016-05-18 12:40:49 -0400 | [diff] [blame] | 233 | error_report("egl: no drm render node available"); |
Gerd Hoffmann | 1e31659 | 2015-10-12 12:03:49 +0200 | [diff] [blame] | 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) { |
Cole Robinson | 38a55bd | 2016-05-18 12:40:49 -0400 | [diff] [blame] | 239 | error_report("egl: gbm_create_device failed"); |
Gerd Hoffmann | 1e31659 | 2015-10-12 12:03:49 +0200 | [diff] [blame] | 240 | goto err; |
| 241 | } |
| 242 | |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 243 | rc = qemu_egl_init_dpy_mesa((EGLNativeDisplayType)qemu_egl_rn_gbm_dev, |
| 244 | mode); |
Gerd Hoffmann | 151c8e6 | 2017-05-05 12:40:59 +0200 | [diff] [blame] | 245 | if (rc != 0) { |
| 246 | /* qemu_egl_init_dpy_mesa reports error */ |
| 247 | goto err; |
| 248 | } |
Gerd Hoffmann | 1e31659 | 2015-10-12 12:03:49 +0200 | [diff] [blame] | 249 | |
| 250 | if (!epoxy_has_egl_extension(qemu_egl_display, |
| 251 | "EGL_KHR_surfaceless_context")) { |
Cole Robinson | 38a55bd | 2016-05-18 12:40:49 -0400 | [diff] [blame] | 252 | error_report("egl: EGL_KHR_surfaceless_context not supported"); |
Gerd Hoffmann | 1e31659 | 2015-10-12 12:03:49 +0200 | [diff] [blame] | 253 | goto err; |
| 254 | } |
| 255 | if (!epoxy_has_egl_extension(qemu_egl_display, |
| 256 | "EGL_MESA_image_dma_buf_export")) { |
Cole Robinson | 38a55bd | 2016-05-18 12:40:49 -0400 | [diff] [blame] | 257 | error_report("egl: EGL_MESA_image_dma_buf_export not supported"); |
Gerd Hoffmann | 1e31659 | 2015-10-12 12:03:49 +0200 | [diff] [blame] | 258 | goto err; |
| 259 | } |
| 260 | |
| 261 | qemu_egl_rn_ctx = qemu_egl_init_ctx(); |
| 262 | if (!qemu_egl_rn_ctx) { |
Cole Robinson | 38a55bd | 2016-05-18 12:40:49 -0400 | [diff] [blame] | 263 | error_report("egl: egl_init_ctx failed"); |
Gerd Hoffmann | 1e31659 | 2015-10-12 12:03:49 +0200 | [diff] [blame] | 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 | |
Gerd Hoffmann | 5fc1fb6 | 2019-05-29 09:21:43 +0200 | [diff] [blame] | 280 | int egl_get_fd_for_texture(uint32_t tex_id, EGLint *stride, EGLint *fourcc, |
| 281 | EGLuint64KHR *modifier) |
Gerd Hoffmann | 1e31659 | 2015-10-12 12:03:49 +0200 | [diff] [blame] | 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, |
Gerd Hoffmann | 5fc1fb6 | 2019-05-29 09:21:43 +0200 | [diff] [blame] | 295 | &num_planes, modifier); |
Gerd Hoffmann | 1e31659 | 2015-10-12 12:03:49 +0200 | [diff] [blame] | 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 | |
Gerd Hoffmann | 86c0522 | 2017-10-10 15:54:51 +0200 | [diff] [blame] | 306 | void egl_dmabuf_import_texture(QemuDmaBuf *dmabuf) |
| 307 | { |
| 308 | EGLImageKHR image = EGL_NO_IMAGE_KHR; |
Gerd Hoffmann | 15ee0d9 | 2019-05-29 09:21:44 +0200 | [diff] [blame] | 309 | EGLint attrs[64]; |
| 310 | int i = 0; |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 311 | uint64_t modifier; |
| 312 | uint32_t texture = qemu_dmabuf_get_texture(dmabuf); |
Gerd Hoffmann | 86c0522 | 2017-10-10 15:54:51 +0200 | [diff] [blame] | 313 | |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 314 | if (texture != 0) { |
Gerd Hoffmann | 86c0522 | 2017-10-10 15:54:51 +0200 | [diff] [blame] | 315 | return; |
| 316 | } |
| 317 | |
Gerd Hoffmann | 15ee0d9 | 2019-05-29 09:21:44 +0200 | [diff] [blame] | 318 | attrs[i++] = EGL_WIDTH; |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 319 | attrs[i++] = qemu_dmabuf_get_backing_width(dmabuf); |
Gerd Hoffmann | 15ee0d9 | 2019-05-29 09:21:44 +0200 | [diff] [blame] | 320 | attrs[i++] = EGL_HEIGHT; |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 321 | attrs[i++] = qemu_dmabuf_get_backing_height(dmabuf); |
Gerd Hoffmann | 15ee0d9 | 2019-05-29 09:21:44 +0200 | [diff] [blame] | 322 | attrs[i++] = EGL_LINUX_DRM_FOURCC_EXT; |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 323 | attrs[i++] = qemu_dmabuf_get_fourcc(dmabuf); |
Gerd Hoffmann | 15ee0d9 | 2019-05-29 09:21:44 +0200 | [diff] [blame] | 324 | |
| 325 | attrs[i++] = EGL_DMA_BUF_PLANE0_FD_EXT; |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 326 | attrs[i++] = qemu_dmabuf_get_fd(dmabuf); |
Gerd Hoffmann | 15ee0d9 | 2019-05-29 09:21:44 +0200 | [diff] [blame] | 327 | attrs[i++] = EGL_DMA_BUF_PLANE0_PITCH_EXT; |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 328 | attrs[i++] = qemu_dmabuf_get_stride(dmabuf); |
Gerd Hoffmann | 15ee0d9 | 2019-05-29 09:21:44 +0200 | [diff] [blame] | 329 | attrs[i++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT; |
| 330 | attrs[i++] = 0; |
| 331 | #ifdef EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 332 | modifier = qemu_dmabuf_get_modifier(dmabuf); |
| 333 | if (modifier) { |
Gerd Hoffmann | 15ee0d9 | 2019-05-29 09:21:44 +0200 | [diff] [blame] | 334 | attrs[i++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT; |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 335 | attrs[i++] = (modifier >> 0) & 0xffffffff; |
Gerd Hoffmann | 15ee0d9 | 2019-05-29 09:21:44 +0200 | [diff] [blame] | 336 | attrs[i++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT; |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 337 | attrs[i++] = (modifier >> 32) & 0xffffffff; |
Gerd Hoffmann | 15ee0d9 | 2019-05-29 09:21:44 +0200 | [diff] [blame] | 338 | } |
| 339 | #endif |
| 340 | attrs[i++] = EGL_NONE; |
| 341 | |
Gerd Hoffmann | 86c0522 | 2017-10-10 15:54:51 +0200 | [diff] [blame] | 342 | image = eglCreateImageKHR(qemu_egl_display, |
| 343 | EGL_NO_CONTEXT, |
| 344 | EGL_LINUX_DMA_BUF_EXT, |
| 345 | NULL, attrs); |
| 346 | if (image == EGL_NO_IMAGE_KHR) { |
| 347 | error_report("eglCreateImageKHR failed"); |
| 348 | return; |
| 349 | } |
| 350 | |
Dongwon Kim | fa64268 | 2024-05-08 10:54:01 -0700 | [diff] [blame] | 351 | glGenTextures(1, &texture); |
| 352 | qemu_dmabuf_set_texture(dmabuf, texture); |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 353 | glBindTexture(GL_TEXTURE_2D, texture); |
Gerd Hoffmann | 86c0522 | 2017-10-10 15:54:51 +0200 | [diff] [blame] | 354 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 355 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 356 | |
| 357 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image); |
| 358 | eglDestroyImageKHR(qemu_egl_display, image); |
| 359 | } |
| 360 | |
| 361 | void egl_dmabuf_release_texture(QemuDmaBuf *dmabuf) |
| 362 | { |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 363 | uint32_t texture; |
| 364 | |
| 365 | texture = qemu_dmabuf_get_texture(dmabuf); |
| 366 | if (texture == 0) { |
Gerd Hoffmann | 86c0522 | 2017-10-10 15:54:51 +0200 | [diff] [blame] | 367 | return; |
| 368 | } |
| 369 | |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 370 | glDeleteTextures(1, &texture); |
Dongwon Kim | fa64268 | 2024-05-08 10:54:01 -0700 | [diff] [blame] | 371 | qemu_dmabuf_set_texture(dmabuf, 0); |
Gerd Hoffmann | 86c0522 | 2017-10-10 15:54:51 +0200 | [diff] [blame] | 372 | } |
| 373 | |
Vivek Kasireddy | 121abaf | 2021-09-14 14:18:34 -0700 | [diff] [blame] | 374 | void egl_dmabuf_create_sync(QemuDmaBuf *dmabuf) |
| 375 | { |
| 376 | EGLSyncKHR sync; |
| 377 | |
| 378 | if (epoxy_has_egl_extension(qemu_egl_display, |
| 379 | "EGL_KHR_fence_sync") && |
| 380 | epoxy_has_egl_extension(qemu_egl_display, |
| 381 | "EGL_ANDROID_native_fence_sync")) { |
| 382 | sync = eglCreateSyncKHR(qemu_egl_display, |
| 383 | EGL_SYNC_NATIVE_FENCE_ANDROID, NULL); |
| 384 | if (sync != EGL_NO_SYNC_KHR) { |
Dongwon Kim | fa64268 | 2024-05-08 10:54:01 -0700 | [diff] [blame] | 385 | qemu_dmabuf_set_sync(dmabuf, sync); |
Vivek Kasireddy | 121abaf | 2021-09-14 14:18:34 -0700 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | void egl_dmabuf_create_fence(QemuDmaBuf *dmabuf) |
| 391 | { |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 392 | void *sync = qemu_dmabuf_get_sync(dmabuf); |
Dongwon Kim | fa64268 | 2024-05-08 10:54:01 -0700 | [diff] [blame] | 393 | int fence_fd; |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 394 | |
| 395 | if (sync) { |
Dongwon Kim | fa64268 | 2024-05-08 10:54:01 -0700 | [diff] [blame] | 396 | fence_fd = eglDupNativeFenceFDANDROID(qemu_egl_display, |
| 397 | sync); |
| 398 | qemu_dmabuf_set_fence_fd(dmabuf, fence_fd); |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 399 | eglDestroySyncKHR(qemu_egl_display, sync); |
Dongwon Kim | fa64268 | 2024-05-08 10:54:01 -0700 | [diff] [blame] | 400 | qemu_dmabuf_set_sync(dmabuf, NULL); |
Vivek Kasireddy | 121abaf | 2021-09-14 14:18:34 -0700 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
Akihiko Odaki | bc6a356 | 2021-02-23 15:03:07 +0900 | [diff] [blame] | 404 | #endif /* CONFIG_GBM */ |
Gerd Hoffmann | 1e31659 | 2015-10-12 12:03:49 +0200 | [diff] [blame] | 405 | |
| 406 | /* ---------------------------------------------------------------------- */ |
| 407 | |
Alexander Kanavin | fbd57c7 | 2019-01-16 12:37:51 +0100 | [diff] [blame] | 408 | EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, EGLNativeWindowType win) |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 409 | { |
| 410 | EGLSurface esurface; |
| 411 | EGLBoolean b; |
| 412 | |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 413 | esurface = eglCreateWindowSurface(qemu_egl_display, |
| 414 | qemu_egl_config, |
Alexander Kanavin | fbd57c7 | 2019-01-16 12:37:51 +0100 | [diff] [blame] | 415 | win, NULL); |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 416 | if (esurface == EGL_NO_SURFACE) { |
Cole Robinson | 38a55bd | 2016-05-18 12:40:49 -0400 | [diff] [blame] | 417 | error_report("egl: eglCreateWindowSurface failed"); |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 418 | return NULL; |
| 419 | } |
| 420 | |
| 421 | b = eglMakeCurrent(qemu_egl_display, esurface, esurface, ectx); |
| 422 | if (b == EGL_FALSE) { |
Cole Robinson | 38a55bd | 2016-05-18 12:40:49 -0400 | [diff] [blame] | 423 | error_report("egl: eglMakeCurrent failed"); |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 424 | return NULL; |
| 425 | } |
| 426 | |
| 427 | return esurface; |
| 428 | } |
| 429 | |
| 430 | /* ---------------------------------------------------------------------- */ |
| 431 | |
Marc-André Lureau | 39324b4 | 2023-06-06 15:56:49 +0400 | [diff] [blame] | 432 | #if defined(CONFIG_X11) || defined(CONFIG_GBM) || defined(WIN32) |
Akihiko Odaki | bc6a356 | 2021-02-23 15:03:07 +0900 | [diff] [blame] | 433 | |
Gerd Hoffmann | 8bce03e | 2017-03-20 09:04:02 +0100 | [diff] [blame] | 434 | /* |
| 435 | * Taken from glamor_egl.h from the Xorg xserver, which is MIT licensed |
| 436 | * |
| 437 | * Create an EGLDisplay from a native display type. This is a little quirky |
| 438 | * for a few reasons. |
| 439 | * |
| 440 | * 1: GetPlatformDisplayEXT and GetPlatformDisplay are the API you want to |
| 441 | * use, but have different function signatures in the third argument; this |
| 442 | * happens not to matter for us, at the moment, but it means epoxy won't alias |
| 443 | * them together. |
| 444 | * |
| 445 | * 2: epoxy 1.3 and earlier don't understand EGL client extensions, which |
| 446 | * means you can't call "eglGetPlatformDisplayEXT" directly, as the resolver |
| 447 | * will crash. |
| 448 | * |
| 449 | * 3: You can't tell whether you have EGL 1.5 at this point, because |
| 450 | * eglQueryString(EGL_VERSION) is a property of the display, which we don't |
| 451 | * have yet. So you have to query for extensions no matter what. Fortunately |
| 452 | * epoxy_has_egl_extension _does_ let you query for client extensions, so |
| 453 | * we don't have to write our own extension string parsing. |
| 454 | * |
| 455 | * 4. There is no EGL_KHR_platform_base to complement the EXT one, thus one |
| 456 | * needs to know EGL 1.5 is supported in order to use the eglGetPlatformDisplay |
| 457 | * function pointer. |
| 458 | * We can workaround this (circular dependency) by probing for the EGL 1.5 |
| 459 | * platform extensions (EGL_KHR_platform_gbm and friends) yet it doesn't seem |
| 460 | * like mesa will be able to advertise these (even though it can do EGL 1.5). |
| 461 | */ |
Gerd Hoffmann | e1913db | 2017-05-05 12:40:58 +0200 | [diff] [blame] | 462 | static EGLDisplay qemu_egl_get_display(EGLNativeDisplayType native, |
| 463 | EGLenum platform) |
Gerd Hoffmann | 8bce03e | 2017-03-20 09:04:02 +0100 | [diff] [blame] | 464 | { |
| 465 | EGLDisplay dpy = EGL_NO_DISPLAY; |
| 466 | |
Gerd Hoffmann | 8bce03e | 2017-03-20 09:04:02 +0100 | [diff] [blame] | 467 | /* In practise any EGL 1.5 implementation would support the EXT extension */ |
| 468 | if (epoxy_has_egl_extension(NULL, "EGL_EXT_platform_base")) { |
Marc-André Lureau | 72cbcea | 2023-04-18 11:32:37 +0400 | [diff] [blame] | 469 | if (platform != 0) { |
| 470 | dpy = eglGetPlatformDisplayEXT(platform, native, NULL); |
Gerd Hoffmann | 8bce03e | 2017-03-20 09:04:02 +0100 | [diff] [blame] | 471 | } |
| 472 | } |
Gerd Hoffmann | 8bce03e | 2017-03-20 09:04:02 +0100 | [diff] [blame] | 473 | |
| 474 | if (dpy == EGL_NO_DISPLAY) { |
| 475 | /* fallback */ |
| 476 | dpy = eglGetDisplay(native); |
| 477 | } |
| 478 | return dpy; |
| 479 | } |
| 480 | |
Gerd Hoffmann | e1913db | 2017-05-05 12:40:58 +0200 | [diff] [blame] | 481 | static int qemu_egl_init_dpy(EGLNativeDisplayType dpy, |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 482 | EGLenum platform, |
| 483 | DisplayGLMode mode) |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 484 | { |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 485 | static const EGLint conf_att_core[] = { |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 486 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 487 | EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, |
| 488 | EGL_RED_SIZE, 5, |
| 489 | EGL_GREEN_SIZE, 5, |
| 490 | EGL_BLUE_SIZE, 5, |
| 491 | EGL_ALPHA_SIZE, 0, |
| 492 | EGL_NONE, |
| 493 | }; |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 494 | static const EGLint conf_att_gles[] = { |
| 495 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 496 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 497 | EGL_RED_SIZE, 5, |
| 498 | EGL_GREEN_SIZE, 5, |
| 499 | EGL_BLUE_SIZE, 5, |
| 500 | EGL_ALPHA_SIZE, 0, |
| 501 | EGL_NONE, |
| 502 | }; |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 503 | EGLint major, minor; |
| 504 | EGLBoolean b; |
| 505 | EGLint n; |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 506 | bool gles = (mode == DISPLAYGL_MODE_ES); |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 507 | |
Gerd Hoffmann | e1913db | 2017-05-05 12:40:58 +0200 | [diff] [blame] | 508 | qemu_egl_display = qemu_egl_get_display(dpy, platform); |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 509 | if (qemu_egl_display == EGL_NO_DISPLAY) { |
Marc-André Lureau | 044ca4b | 2023-06-06 15:56:38 +0400 | [diff] [blame] | 510 | error_report("egl: eglGetDisplay failed: %s", qemu_egl_get_error_string()); |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 511 | return -1; |
| 512 | } |
| 513 | |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 514 | b = eglInitialize(qemu_egl_display, &major, &minor); |
| 515 | if (b == EGL_FALSE) { |
Marc-André Lureau | 044ca4b | 2023-06-06 15:56:38 +0400 | [diff] [blame] | 516 | error_report("egl: eglInitialize failed: %s", qemu_egl_get_error_string()); |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 517 | return -1; |
| 518 | } |
| 519 | |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 520 | b = eglBindAPI(gles ? EGL_OPENGL_ES_API : EGL_OPENGL_API); |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 521 | if (b == EGL_FALSE) { |
Marc-André Lureau | 1f086ef | 2023-02-14 15:46:58 +0400 | [diff] [blame] | 522 | error_report("egl: eglBindAPI failed (%s mode): %s", |
Marc-André Lureau | 044ca4b | 2023-06-06 15:56:38 +0400 | [diff] [blame] | 523 | gles ? "gles" : "core", qemu_egl_get_error_string()); |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 524 | return -1; |
| 525 | } |
| 526 | |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 527 | b = eglChooseConfig(qemu_egl_display, |
| 528 | gles ? conf_att_gles : conf_att_core, |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 529 | &qemu_egl_config, 1, &n); |
| 530 | if (b == EGL_FALSE || n != 1) { |
Marc-André Lureau | 1f086ef | 2023-02-14 15:46:58 +0400 | [diff] [blame] | 531 | error_report("egl: eglChooseConfig failed (%s mode): %s", |
Marc-André Lureau | 044ca4b | 2023-06-06 15:56:38 +0400 | [diff] [blame] | 532 | gles ? "gles" : "core", qemu_egl_get_error_string()); |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 533 | return -1; |
| 534 | } |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 535 | |
| 536 | qemu_egl_mode = gles ? DISPLAYGL_MODE_ES : DISPLAYGL_MODE_CORE; |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 537 | return 0; |
| 538 | } |
| 539 | |
Marc-André Lureau | 39324b4 | 2023-06-06 15:56:49 +0400 | [diff] [blame] | 540 | #endif |
| 541 | |
| 542 | #if defined(CONFIG_X11) || defined(CONFIG_GBM) |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 543 | int qemu_egl_init_dpy_x11(EGLNativeDisplayType dpy, DisplayGLMode mode) |
Gerd Hoffmann | e1913db | 2017-05-05 12:40:58 +0200 | [diff] [blame] | 544 | { |
| 545 | #ifdef EGL_KHR_platform_x11 |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 546 | return qemu_egl_init_dpy(dpy, EGL_PLATFORM_X11_KHR, mode); |
Gerd Hoffmann | e1913db | 2017-05-05 12:40:58 +0200 | [diff] [blame] | 547 | #else |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 548 | return qemu_egl_init_dpy(dpy, 0, mode); |
Gerd Hoffmann | e1913db | 2017-05-05 12:40:58 +0200 | [diff] [blame] | 549 | #endif |
| 550 | } |
| 551 | |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 552 | int qemu_egl_init_dpy_mesa(EGLNativeDisplayType dpy, DisplayGLMode mode) |
Gerd Hoffmann | e1913db | 2017-05-05 12:40:58 +0200 | [diff] [blame] | 553 | { |
| 554 | #ifdef EGL_MESA_platform_gbm |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 555 | return qemu_egl_init_dpy(dpy, EGL_PLATFORM_GBM_MESA, mode); |
Gerd Hoffmann | e1913db | 2017-05-05 12:40:58 +0200 | [diff] [blame] | 556 | #else |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 557 | return qemu_egl_init_dpy(dpy, 0, mode); |
Gerd Hoffmann | e1913db | 2017-05-05 12:40:58 +0200 | [diff] [blame] | 558 | #endif |
| 559 | } |
Marc-André Lureau | 39324b4 | 2023-06-06 15:56:49 +0400 | [diff] [blame] | 560 | #endif |
Gerd Hoffmann | e1913db | 2017-05-05 12:40:58 +0200 | [diff] [blame] | 561 | |
Marc-André Lureau | 39324b4 | 2023-06-06 15:56:49 +0400 | [diff] [blame] | 562 | |
| 563 | #ifdef WIN32 |
| 564 | int qemu_egl_init_dpy_win32(EGLNativeDisplayType dpy, DisplayGLMode mode) |
| 565 | { |
Marc-André Lureau | afe8e0b | 2023-06-06 15:56:50 +0400 | [diff] [blame] | 566 | /* prefer GL ES, as that's what ANGLE supports */ |
| 567 | if (mode == DISPLAYGL_MODE_ON) { |
| 568 | mode = DISPLAYGL_MODE_ES; |
| 569 | } |
Marc-André Lureau | 06c63a3 | 2023-06-06 15:56:55 +0400 | [diff] [blame] | 570 | |
| 571 | if (qemu_egl_init_dpy(dpy, 0, mode) < 0) { |
| 572 | return -1; |
| 573 | } |
| 574 | |
| 575 | #ifdef EGL_D3D11_DEVICE_ANGLE |
| 576 | if (epoxy_has_egl_extension(qemu_egl_display, "EGL_EXT_device_query")) { |
| 577 | EGLDeviceEXT device; |
| 578 | void *d3d11_device; |
| 579 | |
| 580 | if (!eglQueryDisplayAttribEXT(qemu_egl_display, |
| 581 | EGL_DEVICE_EXT, |
| 582 | (EGLAttrib *)&device)) { |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | if (!eglQueryDeviceAttribEXT(device, |
| 587 | EGL_D3D11_DEVICE_ANGLE, |
| 588 | (EGLAttrib *)&d3d11_device)) { |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | trace_egl_init_d3d11_device(device); |
| 593 | qemu_egl_angle_d3d = device != NULL; |
| 594 | } |
| 595 | #endif |
| 596 | |
| 597 | return 0; |
Marc-André Lureau | 39324b4 | 2023-06-06 15:56:49 +0400 | [diff] [blame] | 598 | } |
Akihiko Odaki | bc6a356 | 2021-02-23 15:03:07 +0900 | [diff] [blame] | 599 | #endif |
| 600 | |
Marc-André Lureau | 0df5c72 | 2021-02-04 14:52:26 +0400 | [diff] [blame] | 601 | bool qemu_egl_has_dmabuf(void) |
| 602 | { |
| 603 | if (qemu_egl_display == EGL_NO_DISPLAY) { |
| 604 | return false; |
| 605 | } |
| 606 | |
| 607 | return epoxy_has_egl_extension(qemu_egl_display, |
| 608 | "EGL_EXT_image_dma_buf_import"); |
| 609 | } |
| 610 | |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 611 | EGLContext qemu_egl_init_ctx(void) |
| 612 | { |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 613 | static const EGLint ctx_att_core[] = { |
Gerd Hoffmann | bc8c946 | 2017-05-05 12:41:00 +0200 | [diff] [blame] | 614 | EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT, |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 615 | EGL_NONE |
| 616 | }; |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 617 | static const EGLint ctx_att_gles[] = { |
| 618 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 619 | EGL_NONE |
| 620 | }; |
| 621 | bool gles = (qemu_egl_mode == DISPLAYGL_MODE_ES); |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 622 | EGLContext ectx; |
| 623 | EGLBoolean b; |
| 624 | |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 625 | ectx = eglCreateContext(qemu_egl_display, qemu_egl_config, EGL_NO_CONTEXT, |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 626 | gles ? ctx_att_gles : ctx_att_core); |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 627 | if (ectx == EGL_NO_CONTEXT) { |
Cole Robinson | 38a55bd | 2016-05-18 12:40:49 -0400 | [diff] [blame] | 628 | error_report("egl: eglCreateContext failed"); |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 629 | return NULL; |
| 630 | } |
| 631 | |
| 632 | b = eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, ectx); |
| 633 | if (b == EGL_FALSE) { |
Cole Robinson | 38a55bd | 2016-05-18 12:40:49 -0400 | [diff] [blame] | 634 | error_report("egl: eglMakeCurrent failed"); |
Gerd Hoffmann | 7ced9e9 | 2015-01-06 15:40:00 +0100 | [diff] [blame] | 635 | return NULL; |
| 636 | } |
| 637 | |
| 638 | return ectx; |
| 639 | } |
Marc-André Lureau | 0e1be59 | 2023-02-14 16:11:24 +0400 | [diff] [blame] | 640 | |
| 641 | bool egl_init(const char *rendernode, DisplayGLMode mode, Error **errp) |
| 642 | { |
| 643 | ERRP_GUARD(); |
| 644 | |
| 645 | if (mode == DISPLAYGL_MODE_OFF) { |
| 646 | error_setg(errp, "egl: turning off GL doesn't make sense"); |
| 647 | return false; |
| 648 | } |
| 649 | |
Marc-André Lureau | 39324b4 | 2023-06-06 15:56:49 +0400 | [diff] [blame] | 650 | #ifdef WIN32 |
| 651 | if (qemu_egl_init_dpy_win32(EGL_DEFAULT_DISPLAY, mode) < 0) { |
| 652 | error_setg(errp, "egl: init failed"); |
| 653 | return false; |
| 654 | } |
| 655 | qemu_egl_rn_ctx = qemu_egl_init_ctx(); |
| 656 | if (!qemu_egl_rn_ctx) { |
| 657 | error_setg(errp, "egl: egl_init_ctx failed"); |
| 658 | return false; |
| 659 | } |
| 660 | #elif defined(CONFIG_GBM) |
Marc-André Lureau | 0e1be59 | 2023-02-14 16:11:24 +0400 | [diff] [blame] | 661 | if (egl_rendernode_init(rendernode, mode) < 0) { |
| 662 | error_setg(errp, "egl: render node init failed"); |
| 663 | return false; |
| 664 | } |
Marc-André Lureau | 39324b4 | 2023-06-06 15:56:49 +0400 | [diff] [blame] | 665 | #endif |
| 666 | |
| 667 | if (!qemu_egl_rn_ctx) { |
| 668 | error_setg(errp, "egl: not available on this platform"); |
| 669 | return false; |
| 670 | } |
| 671 | |
Marc-André Lureau | 0e1be59 | 2023-02-14 16:11:24 +0400 | [diff] [blame] | 672 | display_opengl = 1; |
| 673 | return true; |
Marc-André Lureau | 0e1be59 | 2023-02-14 16:11:24 +0400 | [diff] [blame] | 674 | } |