Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 1 | /* |
| 2 | * GTK UI -- egl opengl code. |
| 3 | * |
| 4 | * Note that gtk 3.16+ (released 2015-03-23) has a GtkGLArea widget, |
| 5 | * which is GtkDrawingArea like widget with opengl rendering support. |
| 6 | * |
| 7 | * This code handles opengl support on older gtk versions, using egl |
| 8 | * to get a opengl context for the X11 window. |
| 9 | * |
| 10 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 11 | * See the COPYING file in the top-level directory. |
| 12 | */ |
| 13 | |
Peter Maydell | e16f4c8 | 2016-01-29 17:49:51 +0000 | [diff] [blame] | 14 | #include "qemu/osdep.h" |
Vivek Kasireddy | 65b847d | 2021-09-14 14:18:35 -0700 | [diff] [blame] | 15 | #include "qemu/main-loop.h" |
Marc-André Lureau | 1d48c9f | 2023-06-06 15:56:39 +0400 | [diff] [blame] | 16 | #include "qemu/error-report.h" |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 17 | |
| 18 | #include "trace.h" |
| 19 | |
| 20 | #include "ui/console.h" |
| 21 | #include "ui/gtk.h" |
| 22 | #include "ui/egl-helpers.h" |
Gerd Hoffmann | f1bd313 | 2018-03-06 10:09:51 +0100 | [diff] [blame] | 23 | #include "ui/shader.h" |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 24 | |
Philippe Mathieu-Daudé | 32cad1f | 2024-12-03 15:20:13 +0100 | [diff] [blame] | 25 | #include "system/system.h" |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 26 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 27 | static void gtk_egl_set_scanout_mode(VirtualConsole *vc, bool scanout) |
| 28 | { |
| 29 | if (vc->gfx.scanout_mode == scanout) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | vc->gfx.scanout_mode = scanout; |
| 34 | if (!vc->gfx.scanout_mode) { |
Dongwon Kim | 83b4b23 | 2023-06-28 12:15:04 -0700 | [diff] [blame] | 35 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 36 | vc->gfx.esurface, vc->gfx.ectx); |
Gerd Hoffmann | a4f113f | 2017-06-14 10:41:49 +0200 | [diff] [blame] | 37 | egl_fb_destroy(&vc->gfx.guest_fb); |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 38 | if (vc->gfx.surface) { |
| 39 | surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds); |
| 40 | surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 45 | /** DisplayState Callbacks (opengl version) **/ |
| 46 | |
| 47 | void gd_egl_init(VirtualConsole *vc) |
| 48 | { |
| 49 | GdkWindow *gdk_window = gtk_widget_get_window(vc->gfx.drawing_area); |
| 50 | if (!gdk_window) { |
| 51 | return; |
| 52 | } |
| 53 | |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 54 | Window x11_window = gdk_x11_window_get_xid(gdk_window); |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 55 | if (!x11_window) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | vc->gfx.ectx = qemu_egl_init_ctx(); |
Alexander Kanavin | fbd57c7 | 2019-01-16 12:37:51 +0100 | [diff] [blame] | 60 | vc->gfx.esurface = qemu_egl_init_surface_x11 |
| 61 | (vc->gfx.ectx, (EGLNativeWindowType)x11_window); |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 62 | |
| 63 | assert(vc->gfx.esurface); |
| 64 | } |
| 65 | |
| 66 | void gd_egl_draw(VirtualConsole *vc) |
| 67 | { |
| 68 | GdkWindow *window; |
Dongwon Kim | 55f4b76 | 2021-09-24 15:51:05 -0700 | [diff] [blame] | 69 | #ifdef CONFIG_GBM |
| 70 | QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf; |
Cédric Le Goater | 3dba3c0 | 2024-05-15 12:05:20 +0200 | [diff] [blame] | 71 | int fence_fd; |
Dongwon Kim | 55f4b76 | 2021-09-24 15:51:05 -0700 | [diff] [blame] | 72 | #endif |
Dongwon Kim | 47fd6ab | 2023-10-12 15:26:43 -0700 | [diff] [blame] | 73 | int ww, wh, ws; |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 74 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 75 | if (!vc->gfx.gls) { |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 76 | return; |
| 77 | } |
| 78 | |
Gerd Hoffmann | f1aba96 | 2018-11-07 08:49:49 +0100 | [diff] [blame] | 79 | window = gtk_widget_get_window(vc->gfx.drawing_area); |
Dongwon Kim | 47fd6ab | 2023-10-12 15:26:43 -0700 | [diff] [blame] | 80 | ws = gdk_window_get_scale_factor(window); |
| 81 | ww = gdk_window_get_width(window) * ws; |
| 82 | wh = gdk_window_get_height(window) * ws; |
Gerd Hoffmann | f1aba96 | 2018-11-07 08:49:49 +0100 | [diff] [blame] | 83 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 84 | if (vc->gfx.scanout_mode) { |
Dongwon Kim | 55f4b76 | 2021-09-24 15:51:05 -0700 | [diff] [blame] | 85 | #ifdef CONFIG_GBM |
| 86 | if (dmabuf) { |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 87 | if (!qemu_dmabuf_get_draw_submitted(dmabuf)) { |
Dongwon Kim | 55f4b76 | 2021-09-24 15:51:05 -0700 | [diff] [blame] | 88 | return; |
| 89 | } else { |
Dongwon Kim | fa64268 | 2024-05-08 10:54:01 -0700 | [diff] [blame] | 90 | qemu_dmabuf_set_draw_submitted(dmabuf, false); |
Dongwon Kim | 55f4b76 | 2021-09-24 15:51:05 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | #endif |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 94 | gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h); |
Gerd Hoffmann | f1aba96 | 2018-11-07 08:49:49 +0100 | [diff] [blame] | 95 | |
Erico Nunes | f8a951b | 2023-03-20 17:08:56 +0100 | [diff] [blame] | 96 | vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds); |
| 97 | vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds); |
Dongwon Kim | 55f4b76 | 2021-09-24 15:51:05 -0700 | [diff] [blame] | 98 | |
| 99 | glFlush(); |
| 100 | #ifdef CONFIG_GBM |
| 101 | if (dmabuf) { |
| 102 | egl_dmabuf_create_fence(dmabuf); |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 103 | fence_fd = qemu_dmabuf_get_fence_fd(dmabuf); |
| 104 | if (fence_fd >= 0) { |
| 105 | qemu_set_fd_handler(fence_fd, gd_hw_gl_flushed, NULL, vc); |
Dongwon Kim | 55f4b76 | 2021-09-24 15:51:05 -0700 | [diff] [blame] | 106 | return; |
| 107 | } |
| 108 | graphic_hw_gl_block(vc->gfx.dcl.con, false); |
| 109 | } |
| 110 | #endif |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 111 | } else { |
| 112 | if (!vc->gfx.ds) { |
| 113 | return; |
| 114 | } |
| 115 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 116 | vc->gfx.esurface, vc->gfx.ectx); |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 117 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 118 | surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh); |
| 119 | surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds); |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 120 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 121 | eglSwapBuffers(qemu_egl_display, vc->gfx.esurface); |
Gerd Hoffmann | f1aba96 | 2018-11-07 08:49:49 +0100 | [diff] [blame] | 122 | |
| 123 | vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds); |
| 124 | vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds); |
Dongwon Kim | 55f4b76 | 2021-09-24 15:51:05 -0700 | [diff] [blame] | 125 | |
| 126 | glFlush(); |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 127 | } |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void gd_egl_update(DisplayChangeListener *dcl, |
| 131 | int x, int y, int w, int h) |
| 132 | { |
| 133 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 134 | |
| 135 | if (!vc->gfx.gls || !vc->gfx.ds) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 140 | vc->gfx.esurface, vc->gfx.ectx); |
| 141 | surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h); |
| 142 | vc->gfx.glupdates++; |
Dongwon Kim | 83b4b23 | 2023-06-28 12:15:04 -0700 | [diff] [blame] | 143 | eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, |
| 144 | EGL_NO_SURFACE, EGL_NO_CONTEXT); |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void gd_egl_refresh(DisplayChangeListener *dcl) |
| 148 | { |
| 149 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 150 | |
Akihiko Odaki | aeffd07 | 2022-02-26 20:55:15 +0900 | [diff] [blame] | 151 | gd_update_monitor_refresh_rate( |
| 152 | vc, vc->window ? vc->window : vc->gfx.drawing_area); |
Nikola Pavlica | cab8242 | 2021-01-14 15:01:53 +0100 | [diff] [blame] | 153 | |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 154 | if (vc->gfx.guest_fb.dmabuf && |
| 155 | qemu_dmabuf_get_draw_submitted(vc->gfx.guest_fb.dmabuf)) { |
Dongwon Kim | 77bf310 | 2024-04-26 15:50:59 -0700 | [diff] [blame] | 156 | gd_egl_draw(vc); |
Dongwon Kim | 1be878e | 2023-07-06 11:33:55 -0700 | [diff] [blame] | 157 | return; |
| 158 | } |
| 159 | |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 160 | if (!vc->gfx.esurface) { |
| 161 | gd_egl_init(vc); |
| 162 | if (!vc->gfx.esurface) { |
| 163 | return; |
| 164 | } |
Gerd Hoffmann | 46e19e1 | 2017-10-10 15:54:49 +0200 | [diff] [blame] | 165 | vc->gfx.gls = qemu_gl_init_shader(); |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 166 | if (vc->gfx.ds) { |
Dongwon Kim | 01eb474 | 2021-11-03 23:51:49 -0700 | [diff] [blame] | 167 | surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds); |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 168 | surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds); |
| 169 | } |
Philippe Mathieu-Daudé | 8b4ed0d | 2021-11-08 09:31:29 +0100 | [diff] [blame] | 170 | #ifdef CONFIG_GBM |
Dongwon Kim | 4872a02 | 2021-11-03 23:51:50 -0700 | [diff] [blame] | 171 | if (vc->gfx.guest_fb.dmabuf) { |
| 172 | egl_dmabuf_release_texture(vc->gfx.guest_fb.dmabuf); |
| 173 | gd_egl_scanout_dmabuf(dcl, vc->gfx.guest_fb.dmabuf); |
| 174 | } |
Philippe Mathieu-Daudé | 8b4ed0d | 2021-11-08 09:31:29 +0100 | [diff] [blame] | 175 | #endif |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | graphic_hw_update(dcl->con); |
| 179 | |
| 180 | if (vc->gfx.glupdates) { |
| 181 | vc->gfx.glupdates = 0; |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 182 | gtk_egl_set_scanout_mode(vc, false); |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 183 | gd_egl_draw(vc); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void gd_egl_switch(DisplayChangeListener *dcl, |
| 188 | DisplaySurface *surface) |
| 189 | { |
| 190 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 191 | bool resized = true; |
| 192 | |
| 193 | trace_gd_switch(vc->label, surface_width(surface), surface_height(surface)); |
| 194 | |
| 195 | if (vc->gfx.ds && |
| 196 | surface_width(vc->gfx.ds) == surface_width(surface) && |
| 197 | surface_height(vc->gfx.ds) == surface_height(surface)) { |
| 198 | resized = false; |
| 199 | } |
Dongwon Kim | 01eb474 | 2021-11-03 23:51:49 -0700 | [diff] [blame] | 200 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 201 | vc->gfx.esurface, vc->gfx.ectx); |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 202 | |
| 203 | surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds); |
| 204 | vc->gfx.ds = surface; |
| 205 | if (vc->gfx.gls) { |
| 206 | surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds); |
| 207 | } |
| 208 | |
| 209 | if (resized) { |
| 210 | gd_update_windowsize(vc); |
| 211 | } |
Dongwon Kim | 604a868 | 2022-09-28 14:58:05 -0700 | [diff] [blame] | 212 | |
| 213 | eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, |
| 214 | EGL_NO_CONTEXT); |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 215 | } |
| 216 | |
Marc-André Lureau | 5e79d51 | 2021-10-09 23:48:46 +0400 | [diff] [blame] | 217 | QEMUGLContext gd_egl_create_context(DisplayGLCtx *dgc, |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 218 | QEMUGLParams *params) |
| 219 | { |
Marc-André Lureau | 5e79d51 | 2021-10-09 23:48:46 +0400 | [diff] [blame] | 220 | VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc); |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 221 | |
| 222 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 223 | vc->gfx.esurface, vc->gfx.ectx); |
Marc-André Lureau | 5e79d51 | 2021-10-09 23:48:46 +0400 | [diff] [blame] | 224 | return qemu_egl_create_context(dgc, params); |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Gerd Hoffmann | 543a7a1 | 2017-02-21 10:37:21 +0100 | [diff] [blame] | 227 | void gd_egl_scanout_disable(DisplayChangeListener *dcl) |
| 228 | { |
| 229 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 230 | |
| 231 | vc->gfx.w = 0; |
| 232 | vc->gfx.h = 0; |
Gerd Hoffmann | 543a7a1 | 2017-02-21 10:37:21 +0100 | [diff] [blame] | 233 | gtk_egl_set_scanout_mode(vc, false); |
| 234 | } |
| 235 | |
Gerd Hoffmann | f4c36bd | 2017-02-21 10:37:16 +0100 | [diff] [blame] | 236 | void gd_egl_scanout_texture(DisplayChangeListener *dcl, |
| 237 | uint32_t backing_id, bool backing_y_0_top, |
| 238 | uint32_t backing_width, uint32_t backing_height, |
| 239 | uint32_t x, uint32_t y, |
Marc-André Lureau | bf41ab6 | 2023-06-06 15:56:56 +0400 | [diff] [blame] | 240 | uint32_t w, uint32_t h, |
| 241 | void *d3d_tex2d) |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 242 | { |
| 243 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 244 | |
| 245 | vc->gfx.x = x; |
| 246 | vc->gfx.y = y; |
| 247 | vc->gfx.w = w; |
| 248 | vc->gfx.h = h; |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 249 | vc->gfx.y0_top = backing_y_0_top; |
| 250 | |
Antonio Caggiano | 6f189a0 | 2023-10-16 14:32:15 +0200 | [diff] [blame] | 251 | if (!vc->gfx.esurface) { |
| 252 | gd_egl_init(vc); |
| 253 | if (!vc->gfx.esurface) { |
| 254 | return; |
| 255 | } |
Antonio Caggiano | 6f189a0 | 2023-10-16 14:32:15 +0200 | [diff] [blame] | 256 | } |
Volker Rümelin | 53a939f | 2023-11-11 11:40:20 +0100 | [diff] [blame] | 257 | |
| 258 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 259 | vc->gfx.esurface, vc->gfx.ectx); |
| 260 | |
| 261 | gtk_egl_set_scanout_mode(vc, true); |
| 262 | egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height, |
| 263 | backing_id, false); |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 264 | } |
| 265 | |
Gerd Hoffmann | 70763fe | 2018-03-06 10:09:50 +0100 | [diff] [blame] | 266 | void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl, |
| 267 | QemuDmaBuf *dmabuf) |
| 268 | { |
Akihiko Odaki | bc6a356 | 2021-02-23 15:03:07 +0900 | [diff] [blame] | 269 | #ifdef CONFIG_GBM |
Vivek Kasireddy | 65b847d | 2021-09-14 14:18:35 -0700 | [diff] [blame] | 270 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 271 | uint32_t x, y, width, height, backing_width, backing_height, texture; |
| 272 | bool y0_top; |
Vivek Kasireddy | 65b847d | 2021-09-14 14:18:35 -0700 | [diff] [blame] | 273 | |
Dongwon Kim | 01eb474 | 2021-11-03 23:51:49 -0700 | [diff] [blame] | 274 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 275 | vc->gfx.esurface, vc->gfx.ectx); |
| 276 | |
Gerd Hoffmann | 70763fe | 2018-03-06 10:09:50 +0100 | [diff] [blame] | 277 | egl_dmabuf_import_texture(dmabuf); |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 278 | texture = qemu_dmabuf_get_texture(dmabuf); |
| 279 | if (!texture) { |
Gerd Hoffmann | 70763fe | 2018-03-06 10:09:50 +0100 | [diff] [blame] | 280 | return; |
| 281 | } |
| 282 | |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 283 | x = qemu_dmabuf_get_x(dmabuf); |
| 284 | y = qemu_dmabuf_get_y(dmabuf); |
| 285 | width = qemu_dmabuf_get_width(dmabuf); |
| 286 | height = qemu_dmabuf_get_height(dmabuf); |
| 287 | backing_width = qemu_dmabuf_get_backing_width(dmabuf); |
| 288 | backing_height = qemu_dmabuf_get_backing_height(dmabuf); |
| 289 | y0_top = qemu_dmabuf_get_y0_top(dmabuf); |
Vivek Kasireddy | 65b847d | 2021-09-14 14:18:35 -0700 | [diff] [blame] | 290 | |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 291 | gd_egl_scanout_texture(dcl, texture, y0_top, backing_width, backing_height, |
| 292 | x, y, width, height, NULL); |
| 293 | |
| 294 | if (qemu_dmabuf_get_allow_fences(dmabuf)) { |
Vivek Kasireddy | 65b847d | 2021-09-14 14:18:35 -0700 | [diff] [blame] | 295 | vc->gfx.guest_fb.dmabuf = dmabuf; |
| 296 | } |
Gerd Hoffmann | 70763fe | 2018-03-06 10:09:50 +0100 | [diff] [blame] | 297 | #endif |
| 298 | } |
| 299 | |
Gerd Hoffmann | f1bd313 | 2018-03-06 10:09:51 +0100 | [diff] [blame] | 300 | void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl, |
| 301 | QemuDmaBuf *dmabuf, bool have_hot, |
| 302 | uint32_t hot_x, uint32_t hot_y) |
| 303 | { |
Akihiko Odaki | bc6a356 | 2021-02-23 15:03:07 +0900 | [diff] [blame] | 304 | #ifdef CONFIG_GBM |
Gerd Hoffmann | f1bd313 | 2018-03-06 10:09:51 +0100 | [diff] [blame] | 305 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 306 | uint32_t backing_width, backing_height, texture; |
Gerd Hoffmann | f1bd313 | 2018-03-06 10:09:51 +0100 | [diff] [blame] | 307 | |
| 308 | if (dmabuf) { |
| 309 | egl_dmabuf_import_texture(dmabuf); |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 310 | texture = qemu_dmabuf_get_texture(dmabuf); |
| 311 | if (!texture) { |
Gerd Hoffmann | f1bd313 | 2018-03-06 10:09:51 +0100 | [diff] [blame] | 312 | return; |
| 313 | } |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 314 | |
| 315 | backing_width = qemu_dmabuf_get_backing_width(dmabuf); |
| 316 | backing_height = qemu_dmabuf_get_backing_height(dmabuf); |
| 317 | egl_fb_setup_for_tex(&vc->gfx.cursor_fb, backing_width, backing_height, |
| 318 | texture, false); |
Gerd Hoffmann | f1bd313 | 2018-03-06 10:09:51 +0100 | [diff] [blame] | 319 | } else { |
| 320 | egl_fb_destroy(&vc->gfx.cursor_fb); |
| 321 | } |
| 322 | #endif |
| 323 | } |
| 324 | |
| 325 | void gd_egl_cursor_position(DisplayChangeListener *dcl, |
| 326 | uint32_t pos_x, uint32_t pos_y) |
| 327 | { |
| 328 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 329 | |
Gerd Hoffmann | f1aba96 | 2018-11-07 08:49:49 +0100 | [diff] [blame] | 330 | vc->gfx.cursor_x = pos_x * vc->gfx.scale_x; |
| 331 | vc->gfx.cursor_y = pos_y * vc->gfx.scale_y; |
Gerd Hoffmann | f1bd313 | 2018-03-06 10:09:51 +0100 | [diff] [blame] | 332 | } |
| 333 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 334 | void gd_egl_scanout_flush(DisplayChangeListener *dcl, |
| 335 | uint32_t x, uint32_t y, uint32_t w, uint32_t h) |
| 336 | { |
| 337 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 338 | GdkWindow *window; |
Dongwon Kim | 47fd6ab | 2023-10-12 15:26:43 -0700 | [diff] [blame] | 339 | int ww, wh, ws; |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 340 | |
| 341 | if (!vc->gfx.scanout_mode) { |
| 342 | return; |
| 343 | } |
Gerd Hoffmann | a4f113f | 2017-06-14 10:41:49 +0200 | [diff] [blame] | 344 | if (!vc->gfx.guest_fb.framebuffer) { |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 345 | return; |
| 346 | } |
| 347 | |
| 348 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 349 | vc->gfx.esurface, vc->gfx.ectx); |
| 350 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 351 | window = gtk_widget_get_window(vc->gfx.drawing_area); |
Dongwon Kim | 47fd6ab | 2023-10-12 15:26:43 -0700 | [diff] [blame] | 352 | ws = gdk_window_get_scale_factor(window); |
| 353 | ww = gdk_window_get_width(window) * ws; |
| 354 | wh = gdk_window_get_height(window) * ws; |
Gerd Hoffmann | a4f113f | 2017-06-14 10:41:49 +0200 | [diff] [blame] | 355 | egl_fb_setup_default(&vc->gfx.win_fb, ww, wh); |
Gerd Hoffmann | f1bd313 | 2018-03-06 10:09:51 +0100 | [diff] [blame] | 356 | if (vc->gfx.cursor_fb.texture) { |
| 357 | egl_texture_blit(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.guest_fb, |
| 358 | vc->gfx.y0_top); |
| 359 | egl_texture_blend(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.cursor_fb, |
| 360 | vc->gfx.y0_top, |
Chen Zhang | 051a0cd | 2019-01-25 15:47:23 +0800 | [diff] [blame] | 361 | vc->gfx.cursor_x, vc->gfx.cursor_y, |
| 362 | vc->gfx.scale_x, vc->gfx.scale_y); |
Gerd Hoffmann | f1bd313 | 2018-03-06 10:09:51 +0100 | [diff] [blame] | 363 | } else { |
| 364 | egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top); |
| 365 | } |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 366 | |
Vivek Kasireddy | 65b847d | 2021-09-14 14:18:35 -0700 | [diff] [blame] | 367 | #ifdef CONFIG_GBM |
| 368 | if (vc->gfx.guest_fb.dmabuf) { |
| 369 | egl_dmabuf_create_sync(vc->gfx.guest_fb.dmabuf); |
| 370 | } |
| 371 | #endif |
| 372 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 373 | eglSwapBuffers(qemu_egl_display, vc->gfx.esurface); |
| 374 | } |
| 375 | |
Vivek Kasireddy | ab971f8 | 2021-09-14 14:18:36 -0700 | [diff] [blame] | 376 | void gd_egl_flush(DisplayChangeListener *dcl, |
| 377 | uint32_t x, uint32_t y, uint32_t w, uint32_t h) |
| 378 | { |
| 379 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 380 | GtkWidget *area = vc->gfx.drawing_area; |
| 381 | |
Dongwon Kim | 6779a30 | 2024-05-08 10:54:00 -0700 | [diff] [blame] | 382 | if (vc->gfx.guest_fb.dmabuf && |
| 383 | !qemu_dmabuf_get_draw_submitted(vc->gfx.guest_fb.dmabuf)) { |
Vivek Kasireddy | ab971f8 | 2021-09-14 14:18:36 -0700 | [diff] [blame] | 384 | graphic_hw_gl_block(vc->gfx.dcl.con, true); |
Dongwon Kim | fa64268 | 2024-05-08 10:54:01 -0700 | [diff] [blame] | 385 | qemu_dmabuf_set_draw_submitted(vc->gfx.guest_fb.dmabuf, true); |
Dongwon Kim | 92b5815 | 2023-07-06 11:33:54 -0700 | [diff] [blame] | 386 | gtk_egl_set_scanout_mode(vc, true); |
Vivek Kasireddy | ab971f8 | 2021-09-14 14:18:36 -0700 | [diff] [blame] | 387 | gtk_widget_queue_draw_area(area, x, y, w, h); |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | gd_egl_scanout_flush(&vc->gfx.dcl, x, y, w, h); |
| 392 | } |
| 393 | |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 394 | void gtk_egl_init(DisplayGLMode mode) |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 395 | { |
| 396 | GdkDisplay *gdk_display = gdk_display_get_default(); |
| 397 | Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display); |
| 398 | |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 399 | if (qemu_egl_init_dpy_x11(x11_display, mode) < 0) { |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 400 | return; |
| 401 | } |
| 402 | |
| 403 | display_opengl = 1; |
| 404 | } |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 405 | |
Marc-André Lureau | 5e79d51 | 2021-10-09 23:48:46 +0400 | [diff] [blame] | 406 | int gd_egl_make_current(DisplayGLCtx *dgc, |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 407 | QEMUGLContext ctx) |
| 408 | { |
Marc-André Lureau | 5e79d51 | 2021-10-09 23:48:46 +0400 | [diff] [blame] | 409 | VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc); |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 410 | |
Marc-André Lureau | 1d48c9f | 2023-06-06 15:56:39 +0400 | [diff] [blame] | 411 | if (!eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 412 | vc->gfx.esurface, ctx)) { |
| 413 | error_report("egl: eglMakeCurrent failed: %s", qemu_egl_get_error_string()); |
| 414 | return -1; |
| 415 | } |
| 416 | |
| 417 | return 0; |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 418 | } |