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" |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 15 | |
| 16 | #include "trace.h" |
| 17 | |
| 18 | #include "ui/console.h" |
| 19 | #include "ui/gtk.h" |
| 20 | #include "ui/egl-helpers.h" |
Gerd Hoffmann | f1bd313 | 2018-03-06 10:09:51 +0100 | [diff] [blame] | 21 | #include "ui/shader.h" |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 22 | |
| 23 | #include "sysemu/sysemu.h" |
| 24 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 25 | static void gtk_egl_set_scanout_mode(VirtualConsole *vc, bool scanout) |
| 26 | { |
| 27 | if (vc->gfx.scanout_mode == scanout) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | vc->gfx.scanout_mode = scanout; |
| 32 | if (!vc->gfx.scanout_mode) { |
Gerd Hoffmann | a4f113f | 2017-06-14 10:41:49 +0200 | [diff] [blame] | 33 | egl_fb_destroy(&vc->gfx.guest_fb); |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 34 | if (vc->gfx.surface) { |
| 35 | surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds); |
| 36 | surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 41 | /** DisplayState Callbacks (opengl version) **/ |
| 42 | |
| 43 | void gd_egl_init(VirtualConsole *vc) |
| 44 | { |
| 45 | GdkWindow *gdk_window = gtk_widget_get_window(vc->gfx.drawing_area); |
| 46 | if (!gdk_window) { |
| 47 | return; |
| 48 | } |
| 49 | |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 50 | Window x11_window = gdk_x11_window_get_xid(gdk_window); |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 51 | if (!x11_window) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | vc->gfx.ectx = qemu_egl_init_ctx(); |
Alexander Kanavin | fbd57c7 | 2019-01-16 12:37:51 +0100 | [diff] [blame] | 56 | vc->gfx.esurface = qemu_egl_init_surface_x11 |
| 57 | (vc->gfx.ectx, (EGLNativeWindowType)x11_window); |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 58 | |
| 59 | assert(vc->gfx.esurface); |
| 60 | } |
| 61 | |
| 62 | void gd_egl_draw(VirtualConsole *vc) |
| 63 | { |
| 64 | GdkWindow *window; |
| 65 | int ww, wh; |
| 66 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 67 | if (!vc->gfx.gls) { |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 68 | return; |
| 69 | } |
| 70 | |
Gerd Hoffmann | f1aba96 | 2018-11-07 08:49:49 +0100 | [diff] [blame] | 71 | window = gtk_widget_get_window(vc->gfx.drawing_area); |
| 72 | ww = gdk_window_get_width(window); |
| 73 | wh = gdk_window_get_height(window); |
| 74 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 75 | if (vc->gfx.scanout_mode) { |
| 76 | 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] | 77 | |
| 78 | vc->gfx.scale_x = (double)ww / vc->gfx.w; |
| 79 | vc->gfx.scale_y = (double)wh / vc->gfx.h; |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 80 | } else { |
| 81 | if (!vc->gfx.ds) { |
| 82 | return; |
| 83 | } |
| 84 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 85 | vc->gfx.esurface, vc->gfx.ectx); |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 86 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 87 | surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh); |
| 88 | surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds); |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 89 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 90 | eglSwapBuffers(qemu_egl_display, vc->gfx.esurface); |
Gerd Hoffmann | f1aba96 | 2018-11-07 08:49:49 +0100 | [diff] [blame] | 91 | |
| 92 | vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds); |
| 93 | vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds); |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 94 | } |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void gd_egl_update(DisplayChangeListener *dcl, |
| 98 | int x, int y, int w, int h) |
| 99 | { |
| 100 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 101 | |
| 102 | if (!vc->gfx.gls || !vc->gfx.ds) { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 107 | vc->gfx.esurface, vc->gfx.ectx); |
| 108 | surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h); |
| 109 | vc->gfx.glupdates++; |
| 110 | } |
| 111 | |
| 112 | void gd_egl_refresh(DisplayChangeListener *dcl) |
| 113 | { |
| 114 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 115 | |
| 116 | if (!vc->gfx.esurface) { |
| 117 | gd_egl_init(vc); |
| 118 | if (!vc->gfx.esurface) { |
| 119 | return; |
| 120 | } |
Gerd Hoffmann | 46e19e1 | 2017-10-10 15:54:49 +0200 | [diff] [blame] | 121 | vc->gfx.gls = qemu_gl_init_shader(); |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 122 | if (vc->gfx.ds) { |
| 123 | surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | graphic_hw_update(dcl->con); |
| 128 | |
| 129 | if (vc->gfx.glupdates) { |
| 130 | vc->gfx.glupdates = 0; |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 131 | gtk_egl_set_scanout_mode(vc, false); |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 132 | gd_egl_draw(vc); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void gd_egl_switch(DisplayChangeListener *dcl, |
| 137 | DisplaySurface *surface) |
| 138 | { |
| 139 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 140 | bool resized = true; |
| 141 | |
| 142 | trace_gd_switch(vc->label, surface_width(surface), surface_height(surface)); |
| 143 | |
| 144 | if (vc->gfx.ds && |
| 145 | surface_width(vc->gfx.ds) == surface_width(surface) && |
| 146 | surface_height(vc->gfx.ds) == surface_height(surface)) { |
| 147 | resized = false; |
| 148 | } |
| 149 | |
| 150 | surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds); |
| 151 | vc->gfx.ds = surface; |
| 152 | if (vc->gfx.gls) { |
| 153 | surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds); |
| 154 | } |
| 155 | |
| 156 | if (resized) { |
| 157 | gd_update_windowsize(vc); |
| 158 | } |
| 159 | } |
| 160 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 161 | QEMUGLContext gd_egl_create_context(DisplayChangeListener *dcl, |
| 162 | QEMUGLParams *params) |
| 163 | { |
| 164 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 165 | |
| 166 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 167 | vc->gfx.esurface, vc->gfx.ectx); |
| 168 | return qemu_egl_create_context(dcl, params); |
| 169 | } |
| 170 | |
Gerd Hoffmann | 543a7a1 | 2017-02-21 10:37:21 +0100 | [diff] [blame] | 171 | void gd_egl_scanout_disable(DisplayChangeListener *dcl) |
| 172 | { |
| 173 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 174 | |
| 175 | vc->gfx.w = 0; |
| 176 | vc->gfx.h = 0; |
Gerd Hoffmann | 543a7a1 | 2017-02-21 10:37:21 +0100 | [diff] [blame] | 177 | gtk_egl_set_scanout_mode(vc, false); |
| 178 | } |
| 179 | |
Gerd Hoffmann | f4c36bd | 2017-02-21 10:37:16 +0100 | [diff] [blame] | 180 | void gd_egl_scanout_texture(DisplayChangeListener *dcl, |
| 181 | uint32_t backing_id, bool backing_y_0_top, |
| 182 | uint32_t backing_width, uint32_t backing_height, |
| 183 | uint32_t x, uint32_t y, |
| 184 | uint32_t w, uint32_t h) |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 185 | { |
| 186 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 187 | |
| 188 | vc->gfx.x = x; |
| 189 | vc->gfx.y = y; |
| 190 | vc->gfx.w = w; |
| 191 | vc->gfx.h = h; |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 192 | vc->gfx.y0_top = backing_y_0_top; |
| 193 | |
| 194 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 195 | vc->gfx.esurface, vc->gfx.ectx); |
| 196 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 197 | gtk_egl_set_scanout_mode(vc, true); |
Gerd Hoffmann | 74083f9 | 2017-09-27 13:50:31 +0200 | [diff] [blame] | 198 | egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height, |
| 199 | backing_id, false); |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 200 | } |
| 201 | |
Gerd Hoffmann | 70763fe | 2018-03-06 10:09:50 +0100 | [diff] [blame] | 202 | void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl, |
| 203 | QemuDmaBuf *dmabuf) |
| 204 | { |
| 205 | #ifdef CONFIG_OPENGL_DMABUF |
| 206 | egl_dmabuf_import_texture(dmabuf); |
| 207 | if (!dmabuf->texture) { |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | gd_egl_scanout_texture(dcl, dmabuf->texture, |
| 212 | false, dmabuf->width, dmabuf->height, |
| 213 | 0, 0, dmabuf->width, dmabuf->height); |
| 214 | #endif |
| 215 | } |
| 216 | |
Gerd Hoffmann | f1bd313 | 2018-03-06 10:09:51 +0100 | [diff] [blame] | 217 | void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl, |
| 218 | QemuDmaBuf *dmabuf, bool have_hot, |
| 219 | uint32_t hot_x, uint32_t hot_y) |
| 220 | { |
| 221 | #ifdef CONFIG_OPENGL_DMABUF |
| 222 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 223 | |
| 224 | if (dmabuf) { |
| 225 | egl_dmabuf_import_texture(dmabuf); |
| 226 | if (!dmabuf->texture) { |
| 227 | return; |
| 228 | } |
| 229 | egl_fb_setup_for_tex(&vc->gfx.cursor_fb, dmabuf->width, dmabuf->height, |
| 230 | dmabuf->texture, false); |
| 231 | } else { |
| 232 | egl_fb_destroy(&vc->gfx.cursor_fb); |
| 233 | } |
| 234 | #endif |
| 235 | } |
| 236 | |
| 237 | void gd_egl_cursor_position(DisplayChangeListener *dcl, |
| 238 | uint32_t pos_x, uint32_t pos_y) |
| 239 | { |
| 240 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 241 | |
Gerd Hoffmann | f1aba96 | 2018-11-07 08:49:49 +0100 | [diff] [blame] | 242 | vc->gfx.cursor_x = pos_x * vc->gfx.scale_x; |
| 243 | vc->gfx.cursor_y = pos_y * vc->gfx.scale_y; |
Gerd Hoffmann | f1bd313 | 2018-03-06 10:09:51 +0100 | [diff] [blame] | 244 | } |
| 245 | |
Gerd Hoffmann | 70763fe | 2018-03-06 10:09:50 +0100 | [diff] [blame] | 246 | void gd_egl_release_dmabuf(DisplayChangeListener *dcl, |
| 247 | QemuDmaBuf *dmabuf) |
| 248 | { |
| 249 | #ifdef CONFIG_OPENGL_DMABUF |
| 250 | egl_dmabuf_release_texture(dmabuf); |
| 251 | #endif |
| 252 | } |
| 253 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 254 | void gd_egl_scanout_flush(DisplayChangeListener *dcl, |
| 255 | uint32_t x, uint32_t y, uint32_t w, uint32_t h) |
| 256 | { |
| 257 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 258 | GdkWindow *window; |
Gerd Hoffmann | a4f113f | 2017-06-14 10:41:49 +0200 | [diff] [blame] | 259 | int ww, wh; |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 260 | |
| 261 | if (!vc->gfx.scanout_mode) { |
| 262 | return; |
| 263 | } |
Gerd Hoffmann | a4f113f | 2017-06-14 10:41:49 +0200 | [diff] [blame] | 264 | if (!vc->gfx.guest_fb.framebuffer) { |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 265 | return; |
| 266 | } |
| 267 | |
| 268 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 269 | vc->gfx.esurface, vc->gfx.ectx); |
| 270 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 271 | window = gtk_widget_get_window(vc->gfx.drawing_area); |
Daniel P. Berrangé | 89d85cd | 2018-08-22 14:15:52 +0100 | [diff] [blame] | 272 | ww = gdk_window_get_width(window); |
| 273 | wh = gdk_window_get_height(window); |
Gerd Hoffmann | a4f113f | 2017-06-14 10:41:49 +0200 | [diff] [blame] | 274 | egl_fb_setup_default(&vc->gfx.win_fb, ww, wh); |
Gerd Hoffmann | f1bd313 | 2018-03-06 10:09:51 +0100 | [diff] [blame] | 275 | if (vc->gfx.cursor_fb.texture) { |
| 276 | egl_texture_blit(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.guest_fb, |
| 277 | vc->gfx.y0_top); |
| 278 | egl_texture_blend(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.cursor_fb, |
| 279 | vc->gfx.y0_top, |
Chen Zhang | 051a0cd | 2019-01-25 15:47:23 +0800 | [diff] [blame] | 280 | vc->gfx.cursor_x, vc->gfx.cursor_y, |
| 281 | vc->gfx.scale_x, vc->gfx.scale_y); |
Gerd Hoffmann | f1bd313 | 2018-03-06 10:09:51 +0100 | [diff] [blame] | 282 | } else { |
| 283 | egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top); |
| 284 | } |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 285 | |
| 286 | eglSwapBuffers(qemu_egl_display, vc->gfx.esurface); |
| 287 | } |
| 288 | |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 289 | void gtk_egl_init(DisplayGLMode mode) |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 290 | { |
| 291 | GdkDisplay *gdk_display = gdk_display_get_default(); |
| 292 | Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display); |
| 293 | |
Gerd Hoffmann | 54d208f | 2018-06-18 13:21:41 +0200 | [diff] [blame] | 294 | if (qemu_egl_init_dpy_x11(x11_display, mode) < 0) { |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 295 | return; |
| 296 | } |
| 297 | |
| 298 | display_opengl = 1; |
| 299 | } |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 300 | |
| 301 | int gd_egl_make_current(DisplayChangeListener *dcl, |
| 302 | QEMUGLContext ctx) |
| 303 | { |
| 304 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 305 | |
| 306 | return eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 307 | vc->gfx.esurface, ctx); |
| 308 | } |