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 | #include "qemu-common.h" |
| 16 | |
| 17 | #include "trace.h" |
| 18 | |
| 19 | #include "ui/console.h" |
| 20 | #include "ui/gtk.h" |
| 21 | #include "ui/egl-helpers.h" |
| 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 | |
| 50 | #if GTK_CHECK_VERSION(3, 0, 0) |
| 51 | Window x11_window = gdk_x11_window_get_xid(gdk_window); |
| 52 | #else |
| 53 | Window x11_window = gdk_x11_drawable_get_xid(gdk_window); |
| 54 | #endif |
| 55 | if (!x11_window) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | vc->gfx.ectx = qemu_egl_init_ctx(); |
| 60 | vc->gfx.esurface = qemu_egl_init_surface_x11(vc->gfx.ectx, x11_window); |
| 61 | |
| 62 | assert(vc->gfx.esurface); |
| 63 | } |
| 64 | |
| 65 | void gd_egl_draw(VirtualConsole *vc) |
| 66 | { |
| 67 | GdkWindow *window; |
| 68 | int ww, wh; |
| 69 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 70 | if (!vc->gfx.gls) { |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 71 | return; |
| 72 | } |
| 73 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 74 | if (vc->gfx.scanout_mode) { |
| 75 | gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h); |
| 76 | } else { |
| 77 | if (!vc->gfx.ds) { |
| 78 | return; |
| 79 | } |
| 80 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 81 | vc->gfx.esurface, vc->gfx.ectx); |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 82 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 83 | window = gtk_widget_get_window(vc->gfx.drawing_area); |
| 84 | gdk_drawable_get_size(window, &ww, &wh); |
| 85 | surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh); |
| 86 | surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds); |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 87 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 88 | eglSwapBuffers(qemu_egl_display, vc->gfx.esurface); |
| 89 | } |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void gd_egl_update(DisplayChangeListener *dcl, |
| 93 | int x, int y, int w, int h) |
| 94 | { |
| 95 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 96 | |
| 97 | if (!vc->gfx.gls || !vc->gfx.ds) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 102 | vc->gfx.esurface, vc->gfx.ectx); |
| 103 | surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h); |
| 104 | vc->gfx.glupdates++; |
| 105 | } |
| 106 | |
| 107 | void gd_egl_refresh(DisplayChangeListener *dcl) |
| 108 | { |
| 109 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 110 | |
| 111 | if (!vc->gfx.esurface) { |
| 112 | gd_egl_init(vc); |
| 113 | if (!vc->gfx.esurface) { |
| 114 | return; |
| 115 | } |
Gerd Hoffmann | 46e19e1 | 2017-10-10 15:54:49 +0200 | [diff] [blame] | 116 | vc->gfx.gls = qemu_gl_init_shader(); |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 117 | if (vc->gfx.ds) { |
| 118 | surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | graphic_hw_update(dcl->con); |
| 123 | |
| 124 | if (vc->gfx.glupdates) { |
| 125 | vc->gfx.glupdates = 0; |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 126 | gtk_egl_set_scanout_mode(vc, false); |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 127 | gd_egl_draw(vc); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void gd_egl_switch(DisplayChangeListener *dcl, |
| 132 | DisplaySurface *surface) |
| 133 | { |
| 134 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 135 | bool resized = true; |
| 136 | |
| 137 | trace_gd_switch(vc->label, surface_width(surface), surface_height(surface)); |
| 138 | |
| 139 | if (vc->gfx.ds && |
| 140 | surface_width(vc->gfx.ds) == surface_width(surface) && |
| 141 | surface_height(vc->gfx.ds) == surface_height(surface)) { |
| 142 | resized = false; |
| 143 | } |
| 144 | |
| 145 | surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds); |
| 146 | vc->gfx.ds = surface; |
| 147 | if (vc->gfx.gls) { |
| 148 | surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds); |
| 149 | } |
| 150 | |
| 151 | if (resized) { |
| 152 | gd_update_windowsize(vc); |
| 153 | } |
| 154 | } |
| 155 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 156 | QEMUGLContext gd_egl_create_context(DisplayChangeListener *dcl, |
| 157 | QEMUGLParams *params) |
| 158 | { |
| 159 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 160 | |
| 161 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 162 | vc->gfx.esurface, vc->gfx.ectx); |
| 163 | return qemu_egl_create_context(dcl, params); |
| 164 | } |
| 165 | |
Gerd Hoffmann | 543a7a1 | 2017-02-21 10:37:21 +0100 | [diff] [blame] | 166 | void gd_egl_scanout_disable(DisplayChangeListener *dcl) |
| 167 | { |
| 168 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 169 | |
| 170 | vc->gfx.w = 0; |
| 171 | vc->gfx.h = 0; |
Gerd Hoffmann | 543a7a1 | 2017-02-21 10:37:21 +0100 | [diff] [blame] | 172 | gtk_egl_set_scanout_mode(vc, false); |
| 173 | } |
| 174 | |
Gerd Hoffmann | f4c36bd | 2017-02-21 10:37:16 +0100 | [diff] [blame] | 175 | void gd_egl_scanout_texture(DisplayChangeListener *dcl, |
| 176 | uint32_t backing_id, bool backing_y_0_top, |
| 177 | uint32_t backing_width, uint32_t backing_height, |
| 178 | uint32_t x, uint32_t y, |
| 179 | uint32_t w, uint32_t h) |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 180 | { |
| 181 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 182 | |
| 183 | vc->gfx.x = x; |
| 184 | vc->gfx.y = y; |
| 185 | vc->gfx.w = w; |
| 186 | vc->gfx.h = h; |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 187 | vc->gfx.y0_top = backing_y_0_top; |
| 188 | |
| 189 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 190 | vc->gfx.esurface, vc->gfx.ectx); |
| 191 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 192 | gtk_egl_set_scanout_mode(vc, true); |
Gerd Hoffmann | 74083f9 | 2017-09-27 13:50:31 +0200 | [diff] [blame] | 193 | egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height, |
| 194 | backing_id, false); |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void gd_egl_scanout_flush(DisplayChangeListener *dcl, |
| 198 | uint32_t x, uint32_t y, uint32_t w, uint32_t h) |
| 199 | { |
| 200 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 201 | GdkWindow *window; |
Gerd Hoffmann | a4f113f | 2017-06-14 10:41:49 +0200 | [diff] [blame] | 202 | int ww, wh; |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 203 | |
| 204 | if (!vc->gfx.scanout_mode) { |
| 205 | return; |
| 206 | } |
Gerd Hoffmann | a4f113f | 2017-06-14 10:41:49 +0200 | [diff] [blame] | 207 | if (!vc->gfx.guest_fb.framebuffer) { |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 208 | return; |
| 209 | } |
| 210 | |
| 211 | eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 212 | vc->gfx.esurface, vc->gfx.ectx); |
| 213 | |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 214 | window = gtk_widget_get_window(vc->gfx.drawing_area); |
| 215 | gdk_drawable_get_size(window, &ww, &wh); |
Gerd Hoffmann | a4f113f | 2017-06-14 10:41:49 +0200 | [diff] [blame] | 216 | egl_fb_setup_default(&vc->gfx.win_fb, ww, wh); |
| 217 | egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top); |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 218 | |
| 219 | eglSwapBuffers(qemu_egl_display, vc->gfx.esurface); |
| 220 | } |
| 221 | |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 222 | void gtk_egl_init(void) |
| 223 | { |
| 224 | GdkDisplay *gdk_display = gdk_display_get_default(); |
| 225 | Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display); |
| 226 | |
Gerd Hoffmann | e1913db | 2017-05-05 12:40:58 +0200 | [diff] [blame] | 227 | if (qemu_egl_init_dpy_x11(x11_display) < 0) { |
Gerd Hoffmann | 97edf3b | 2015-01-20 12:43:28 +0100 | [diff] [blame] | 228 | return; |
| 229 | } |
| 230 | |
| 231 | display_opengl = 1; |
| 232 | } |
Gerd Hoffmann | 4782aeb | 2015-05-08 11:30:51 +0200 | [diff] [blame] | 233 | |
| 234 | int gd_egl_make_current(DisplayChangeListener *dcl, |
| 235 | QEMUGLContext ctx) |
| 236 | { |
| 237 | VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); |
| 238 | |
| 239 | return eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, |
| 240 | vc->gfx.esurface, ctx); |
| 241 | } |