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 14 #include "qemu/osdep.h" 15 #include "qemu/main-loop.h" 16 #include "qemu/error-report.h" 17 18 #include "trace.h" 19 20 #include "ui/console.h" 21 #include "ui/gtk.h" 22 #include "ui/egl-helpers.h" 23 #include "ui/shader.h" 24 25 #include "sysemu/sysemu.h" 26 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) { 35 egl_fb_destroy(&vc->gfx.guest_fb); 36 if (vc->gfx.surface) { 37 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds); 38 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds); 39 } 40 } 41 } 42 43 /** DisplayState Callbacks (opengl version) **/ 44 45 void gd_egl_init(VirtualConsole *vc) 46 { 47 GdkWindow *gdk_window = gtk_widget_get_window(vc->gfx.drawing_area); 48 if (!gdk_window) { 49 return; 50 } 51 52 Window x11_window = gdk_x11_window_get_xid(gdk_window); 53 if (!x11_window) { 54 return; 55 } 56 57 vc->gfx.ectx = qemu_egl_init_ctx(); 58 vc->gfx.esurface = qemu_egl_init_surface_x11 59 (vc->gfx.ectx, (EGLNativeWindowType)x11_window); 60 61 assert(vc->gfx.esurface); 62 } 63 64 void gd_egl_draw(VirtualConsole *vc) 65 { 66 GdkWindow *window; 67 #ifdef CONFIG_GBM 68 QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf; 69 #endif 70 int ww, wh; 71 72 if (!vc->gfx.gls) { 73 return; 74 } 75 76 window = gtk_widget_get_window(vc->gfx.drawing_area); 77 ww = gdk_window_get_width(window); 78 wh = gdk_window_get_height(window); 79 80 if (vc->gfx.scanout_mode) { 81 #ifdef CONFIG_GBM 82 if (dmabuf) { 83 if (!dmabuf->draw_submitted) { 84 return; 85 } else { 86 dmabuf->draw_submitted = false; 87 } 88 } 89 #endif 90 gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h); 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); 94 95 glFlush(); 96 #ifdef CONFIG_GBM 97 if (dmabuf) { 98 egl_dmabuf_create_fence(dmabuf); 99 if (dmabuf->fence_fd > 0) { 100 qemu_set_fd_handler(dmabuf->fence_fd, gd_hw_gl_flushed, NULL, vc); 101 return; 102 } 103 graphic_hw_gl_block(vc->gfx.dcl.con, false); 104 } 105 #endif 106 } else { 107 if (!vc->gfx.ds) { 108 return; 109 } 110 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, 111 vc->gfx.esurface, vc->gfx.ectx); 112 113 surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh); 114 surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds); 115 116 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface); 117 118 vc->gfx.scale_x = (double)ww / surface_width(vc->gfx.ds); 119 vc->gfx.scale_y = (double)wh / surface_height(vc->gfx.ds); 120 121 glFlush(); 122 } 123 } 124 125 void gd_egl_update(DisplayChangeListener *dcl, 126 int x, int y, int w, int h) 127 { 128 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 129 130 if (!vc->gfx.gls || !vc->gfx.ds) { 131 return; 132 } 133 134 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, 135 vc->gfx.esurface, vc->gfx.ectx); 136 surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h); 137 vc->gfx.glupdates++; 138 } 139 140 void gd_egl_refresh(DisplayChangeListener *dcl) 141 { 142 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 143 144 gd_update_monitor_refresh_rate( 145 vc, vc->window ? vc->window : vc->gfx.drawing_area); 146 147 if (!vc->gfx.esurface) { 148 gd_egl_init(vc); 149 if (!vc->gfx.esurface) { 150 return; 151 } 152 vc->gfx.gls = qemu_gl_init_shader(); 153 if (vc->gfx.ds) { 154 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds); 155 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds); 156 } 157 #ifdef CONFIG_GBM 158 if (vc->gfx.guest_fb.dmabuf) { 159 egl_dmabuf_release_texture(vc->gfx.guest_fb.dmabuf); 160 gd_egl_scanout_dmabuf(dcl, vc->gfx.guest_fb.dmabuf); 161 } 162 #endif 163 } 164 165 graphic_hw_update(dcl->con); 166 167 if (vc->gfx.glupdates) { 168 vc->gfx.glupdates = 0; 169 gtk_egl_set_scanout_mode(vc, false); 170 gd_egl_draw(vc); 171 } 172 } 173 174 void gd_egl_switch(DisplayChangeListener *dcl, 175 DisplaySurface *surface) 176 { 177 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 178 bool resized = true; 179 180 trace_gd_switch(vc->label, surface_width(surface), surface_height(surface)); 181 182 if (vc->gfx.ds && 183 surface_width(vc->gfx.ds) == surface_width(surface) && 184 surface_height(vc->gfx.ds) == surface_height(surface)) { 185 resized = false; 186 } 187 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, 188 vc->gfx.esurface, vc->gfx.ectx); 189 190 surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds); 191 vc->gfx.ds = surface; 192 if (vc->gfx.gls) { 193 surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds); 194 } 195 196 if (resized) { 197 gd_update_windowsize(vc); 198 } 199 200 eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, 201 EGL_NO_CONTEXT); 202 } 203 204 QEMUGLContext gd_egl_create_context(DisplayGLCtx *dgc, 205 QEMUGLParams *params) 206 { 207 VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc); 208 209 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, 210 vc->gfx.esurface, vc->gfx.ectx); 211 return qemu_egl_create_context(dgc, params); 212 } 213 214 void gd_egl_scanout_disable(DisplayChangeListener *dcl) 215 { 216 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 217 218 vc->gfx.w = 0; 219 vc->gfx.h = 0; 220 gtk_egl_set_scanout_mode(vc, false); 221 } 222 223 void gd_egl_scanout_texture(DisplayChangeListener *dcl, 224 uint32_t backing_id, bool backing_y_0_top, 225 uint32_t backing_width, uint32_t backing_height, 226 uint32_t x, uint32_t y, 227 uint32_t w, uint32_t h) 228 { 229 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 230 231 vc->gfx.x = x; 232 vc->gfx.y = y; 233 vc->gfx.w = w; 234 vc->gfx.h = h; 235 vc->gfx.y0_top = backing_y_0_top; 236 237 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, 238 vc->gfx.esurface, vc->gfx.ectx); 239 240 gtk_egl_set_scanout_mode(vc, true); 241 egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height, 242 backing_id, false); 243 } 244 245 void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl, 246 QemuDmaBuf *dmabuf) 247 { 248 #ifdef CONFIG_GBM 249 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 250 251 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, 252 vc->gfx.esurface, vc->gfx.ectx); 253 254 egl_dmabuf_import_texture(dmabuf); 255 if (!dmabuf->texture) { 256 return; 257 } 258 259 gd_egl_scanout_texture(dcl, dmabuf->texture, 260 dmabuf->y0_top, dmabuf->width, dmabuf->height, 261 dmabuf->x, dmabuf->y, dmabuf->scanout_width, 262 dmabuf->scanout_height); 263 264 if (dmabuf->allow_fences) { 265 vc->gfx.guest_fb.dmabuf = dmabuf; 266 } 267 #endif 268 } 269 270 void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl, 271 QemuDmaBuf *dmabuf, bool have_hot, 272 uint32_t hot_x, uint32_t hot_y) 273 { 274 #ifdef CONFIG_GBM 275 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 276 277 if (dmabuf) { 278 egl_dmabuf_import_texture(dmabuf); 279 if (!dmabuf->texture) { 280 return; 281 } 282 egl_fb_setup_for_tex(&vc->gfx.cursor_fb, dmabuf->width, dmabuf->height, 283 dmabuf->texture, false); 284 } else { 285 egl_fb_destroy(&vc->gfx.cursor_fb); 286 } 287 #endif 288 } 289 290 void gd_egl_cursor_position(DisplayChangeListener *dcl, 291 uint32_t pos_x, uint32_t pos_y) 292 { 293 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 294 295 vc->gfx.cursor_x = pos_x * vc->gfx.scale_x; 296 vc->gfx.cursor_y = pos_y * vc->gfx.scale_y; 297 } 298 299 void gd_egl_scanout_flush(DisplayChangeListener *dcl, 300 uint32_t x, uint32_t y, uint32_t w, uint32_t h) 301 { 302 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 303 GdkWindow *window; 304 int ww, wh; 305 306 if (!vc->gfx.scanout_mode) { 307 return; 308 } 309 if (!vc->gfx.guest_fb.framebuffer) { 310 return; 311 } 312 313 eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, 314 vc->gfx.esurface, vc->gfx.ectx); 315 316 window = gtk_widget_get_window(vc->gfx.drawing_area); 317 ww = gdk_window_get_width(window); 318 wh = gdk_window_get_height(window); 319 egl_fb_setup_default(&vc->gfx.win_fb, ww, wh); 320 if (vc->gfx.cursor_fb.texture) { 321 egl_texture_blit(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.guest_fb, 322 vc->gfx.y0_top); 323 egl_texture_blend(vc->gfx.gls, &vc->gfx.win_fb, &vc->gfx.cursor_fb, 324 vc->gfx.y0_top, 325 vc->gfx.cursor_x, vc->gfx.cursor_y, 326 vc->gfx.scale_x, vc->gfx.scale_y); 327 } else { 328 egl_fb_blit(&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top); 329 } 330 331 #ifdef CONFIG_GBM 332 if (vc->gfx.guest_fb.dmabuf) { 333 egl_dmabuf_create_sync(vc->gfx.guest_fb.dmabuf); 334 } 335 #endif 336 337 eglSwapBuffers(qemu_egl_display, vc->gfx.esurface); 338 } 339 340 void gd_egl_flush(DisplayChangeListener *dcl, 341 uint32_t x, uint32_t y, uint32_t w, uint32_t h) 342 { 343 VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 344 GtkWidget *area = vc->gfx.drawing_area; 345 346 if (vc->gfx.guest_fb.dmabuf && !vc->gfx.guest_fb.dmabuf->draw_submitted) { 347 graphic_hw_gl_block(vc->gfx.dcl.con, true); 348 vc->gfx.guest_fb.dmabuf->draw_submitted = true; 349 gtk_widget_queue_draw_area(area, x, y, w, h); 350 return; 351 } 352 353 gd_egl_scanout_flush(&vc->gfx.dcl, x, y, w, h); 354 } 355 356 void gtk_egl_init(DisplayGLMode mode) 357 { 358 GdkDisplay *gdk_display = gdk_display_get_default(); 359 Display *x11_display = gdk_x11_display_get_xdisplay(gdk_display); 360 361 if (qemu_egl_init_dpy_x11(x11_display, mode) < 0) { 362 return; 363 } 364 365 display_opengl = 1; 366 } 367 368 int gd_egl_make_current(DisplayGLCtx *dgc, 369 QEMUGLContext ctx) 370 { 371 VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc); 372 373 if (!eglMakeCurrent(qemu_egl_display, vc->gfx.esurface, 374 vc->gfx.esurface, ctx)) { 375 error_report("egl: eglMakeCurrent failed: %s", qemu_egl_get_error_string()); 376 return -1; 377 } 378 379 return 0; 380 } 381