1925a0400SGerd Hoffmann /* 2925a0400SGerd Hoffmann * GTK UI -- glarea opengl code. 3925a0400SGerd Hoffmann * 4925a0400SGerd Hoffmann * Requires 3.16+ (GtkGLArea widget). 5925a0400SGerd Hoffmann * 6925a0400SGerd Hoffmann * This work is licensed under the terms of the GNU GPL, version 2 or later. 7925a0400SGerd Hoffmann * See the COPYING file in the top-level directory. 8925a0400SGerd Hoffmann */ 9925a0400SGerd Hoffmann 10e16f4c87SPeter Maydell #include "qemu/osdep.h" 1165b847d2SVivek Kasireddy #include "qemu/main-loop.h" 12925a0400SGerd Hoffmann 13925a0400SGerd Hoffmann #include "trace.h" 14925a0400SGerd Hoffmann 15925a0400SGerd Hoffmann #include "ui/console.h" 16925a0400SGerd Hoffmann #include "ui/gtk.h" 17925a0400SGerd Hoffmann #include "ui/egl-helpers.h" 18925a0400SGerd Hoffmann 19925a0400SGerd Hoffmann #include "sysemu/sysemu.h" 20925a0400SGerd Hoffmann 21925a0400SGerd Hoffmann static void gtk_gl_area_set_scanout_mode(VirtualConsole *vc, bool scanout) 22925a0400SGerd Hoffmann { 23925a0400SGerd Hoffmann if (vc->gfx.scanout_mode == scanout) { 24925a0400SGerd Hoffmann return; 25925a0400SGerd Hoffmann } 26925a0400SGerd Hoffmann 27925a0400SGerd Hoffmann vc->gfx.scanout_mode = scanout; 28925a0400SGerd Hoffmann if (!vc->gfx.scanout_mode) { 2983b4b236SDongwon Kim gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area)); 30a4f113fdSGerd Hoffmann egl_fb_destroy(&vc->gfx.guest_fb); 31925a0400SGerd Hoffmann if (vc->gfx.surface) { 32925a0400SGerd Hoffmann surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds); 33925a0400SGerd Hoffmann surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds); 34925a0400SGerd Hoffmann } 35925a0400SGerd Hoffmann } 36925a0400SGerd Hoffmann } 37925a0400SGerd Hoffmann 38925a0400SGerd Hoffmann /** DisplayState Callbacks (opengl version) **/ 39925a0400SGerd Hoffmann 40925a0400SGerd Hoffmann void gd_gl_area_draw(VirtualConsole *vc) 41925a0400SGerd Hoffmann { 4255f4b767SDongwon Kim #ifdef CONFIG_GBM 4355f4b767SDongwon Kim QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf; 4455f4b767SDongwon Kim #endif 454323118cSAlexander Orzechowski int ww, wh, ws, y1, y2; 46925a0400SGerd Hoffmann 47925a0400SGerd Hoffmann if (!vc->gfx.gls) { 48925a0400SGerd Hoffmann return; 49925a0400SGerd Hoffmann } 50925a0400SGerd Hoffmann 51925a0400SGerd Hoffmann gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area)); 524323118cSAlexander Orzechowski ws = gdk_window_get_scale_factor(gtk_widget_get_window(vc->gfx.drawing_area)); 534323118cSAlexander Orzechowski ww = gtk_widget_get_allocated_width(vc->gfx.drawing_area) * ws; 544323118cSAlexander Orzechowski wh = gtk_widget_get_allocated_height(vc->gfx.drawing_area) * ws; 55925a0400SGerd Hoffmann 56925a0400SGerd Hoffmann if (vc->gfx.scanout_mode) { 57a4f113fdSGerd Hoffmann if (!vc->gfx.guest_fb.framebuffer) { 58925a0400SGerd Hoffmann return; 59925a0400SGerd Hoffmann } 60925a0400SGerd Hoffmann 6155f4b767SDongwon Kim #ifdef CONFIG_GBM 6255f4b767SDongwon Kim if (dmabuf) { 63*6779a307SDongwon Kim if (!qemu_dmabuf_get_draw_submitted(dmabuf)) { 6455f4b767SDongwon Kim return; 6555f4b767SDongwon Kim } else { 6655f4b767SDongwon Kim dmabuf->draw_submitted = false; 6755f4b767SDongwon Kim } 6855f4b767SDongwon Kim } 6955f4b767SDongwon Kim #endif 7055f4b767SDongwon Kim 71a4f113fdSGerd Hoffmann glBindFramebuffer(GL_READ_FRAMEBUFFER, vc->gfx.guest_fb.framebuffer); 72925a0400SGerd Hoffmann /* GtkGLArea sets GL_DRAW_FRAMEBUFFER for us */ 73925a0400SGerd Hoffmann 74925a0400SGerd Hoffmann glViewport(0, 0, ww, wh); 75925a0400SGerd Hoffmann y1 = vc->gfx.y0_top ? 0 : vc->gfx.h; 76925a0400SGerd Hoffmann y2 = vc->gfx.y0_top ? vc->gfx.h : 0; 77925a0400SGerd Hoffmann glBlitFramebuffer(0, y1, vc->gfx.w, y2, 78925a0400SGerd Hoffmann 0, 0, ww, wh, 79925a0400SGerd Hoffmann GL_COLOR_BUFFER_BIT, GL_NEAREST); 8055f4b767SDongwon Kim #ifdef CONFIG_GBM 8155f4b767SDongwon Kim if (dmabuf) { 8255f4b767SDongwon Kim egl_dmabuf_create_sync(dmabuf); 8355f4b767SDongwon Kim } 8455f4b767SDongwon Kim #endif 8555f4b767SDongwon Kim glFlush(); 8655f4b767SDongwon Kim #ifdef CONFIG_GBM 8755f4b767SDongwon Kim if (dmabuf) { 88*6779a307SDongwon Kim int fence_fd; 8955f4b767SDongwon Kim egl_dmabuf_create_fence(dmabuf); 90*6779a307SDongwon Kim fence_fd = qemu_dmabuf_get_fence_fd(dmabuf); 91*6779a307SDongwon Kim if (fence_fd >= 0) { 92*6779a307SDongwon Kim qemu_set_fd_handler(fence_fd, gd_hw_gl_flushed, NULL, vc); 9355f4b767SDongwon Kim return; 9455f4b767SDongwon Kim } 9555f4b767SDongwon Kim graphic_hw_gl_block(vc->gfx.dcl.con, false); 9655f4b767SDongwon Kim } 9755f4b767SDongwon Kim #endif 98925a0400SGerd Hoffmann } else { 99925a0400SGerd Hoffmann if (!vc->gfx.ds) { 100925a0400SGerd Hoffmann return; 101925a0400SGerd Hoffmann } 102925a0400SGerd Hoffmann gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area)); 103925a0400SGerd Hoffmann 104925a0400SGerd Hoffmann surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh); 105925a0400SGerd Hoffmann surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds); 106925a0400SGerd Hoffmann } 107925a0400SGerd Hoffmann } 108925a0400SGerd Hoffmann 109925a0400SGerd Hoffmann void gd_gl_area_update(DisplayChangeListener *dcl, 110925a0400SGerd Hoffmann int x, int y, int w, int h) 111925a0400SGerd Hoffmann { 112925a0400SGerd Hoffmann VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 113925a0400SGerd Hoffmann 114925a0400SGerd Hoffmann if (!vc->gfx.gls || !vc->gfx.ds) { 115925a0400SGerd Hoffmann return; 116925a0400SGerd Hoffmann } 117925a0400SGerd Hoffmann 118925a0400SGerd Hoffmann gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area)); 119925a0400SGerd Hoffmann surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h); 120925a0400SGerd Hoffmann vc->gfx.glupdates++; 12183b4b236SDongwon Kim gdk_gl_context_clear_current(); 122925a0400SGerd Hoffmann } 123925a0400SGerd Hoffmann 124925a0400SGerd Hoffmann void gd_gl_area_refresh(DisplayChangeListener *dcl) 125925a0400SGerd Hoffmann { 126925a0400SGerd Hoffmann VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 127925a0400SGerd Hoffmann 128aeffd071SAkihiko Odaki gd_update_monitor_refresh_rate(vc, vc->window ? vc->window : vc->gfx.drawing_area); 129760deab3SNikola Pavlica 130*6779a307SDongwon Kim if (vc->gfx.guest_fb.dmabuf && 131*6779a307SDongwon Kim qemu_dmabuf_get_draw_submitted(vc->gfx.guest_fb.dmabuf)) { 13277bf3100SDongwon Kim gd_gl_area_draw(vc); 1331be878ebSDongwon Kim return; 1341be878ebSDongwon Kim } 1351be878ebSDongwon Kim 136925a0400SGerd Hoffmann if (!vc->gfx.gls) { 137925a0400SGerd Hoffmann if (!gtk_widget_get_realized(vc->gfx.drawing_area)) { 138925a0400SGerd Hoffmann return; 139925a0400SGerd Hoffmann } 140925a0400SGerd Hoffmann gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area)); 14146e19e14SGerd Hoffmann vc->gfx.gls = qemu_gl_init_shader(); 142925a0400SGerd Hoffmann if (vc->gfx.ds) { 143925a0400SGerd Hoffmann surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds); 144925a0400SGerd Hoffmann } 145925a0400SGerd Hoffmann } 146925a0400SGerd Hoffmann 147925a0400SGerd Hoffmann graphic_hw_update(dcl->con); 148925a0400SGerd Hoffmann 149925a0400SGerd Hoffmann if (vc->gfx.glupdates) { 150925a0400SGerd Hoffmann vc->gfx.glupdates = 0; 151925a0400SGerd Hoffmann gtk_gl_area_set_scanout_mode(vc, false); 152925a0400SGerd Hoffmann gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area)); 153925a0400SGerd Hoffmann } 154925a0400SGerd Hoffmann } 155925a0400SGerd Hoffmann 156925a0400SGerd Hoffmann void gd_gl_area_switch(DisplayChangeListener *dcl, 157925a0400SGerd Hoffmann DisplaySurface *surface) 158925a0400SGerd Hoffmann { 159925a0400SGerd Hoffmann VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 160925a0400SGerd Hoffmann bool resized = true; 161925a0400SGerd Hoffmann 162925a0400SGerd Hoffmann trace_gd_switch(vc->label, surface_width(surface), surface_height(surface)); 163925a0400SGerd Hoffmann 164925a0400SGerd Hoffmann if (vc->gfx.ds && 165925a0400SGerd Hoffmann surface_width(vc->gfx.ds) == surface_width(surface) && 166925a0400SGerd Hoffmann surface_height(vc->gfx.ds) == surface_height(surface)) { 167925a0400SGerd Hoffmann resized = false; 168925a0400SGerd Hoffmann } 169925a0400SGerd Hoffmann 170925a0400SGerd Hoffmann if (vc->gfx.gls) { 171925a0400SGerd Hoffmann gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area)); 172925a0400SGerd Hoffmann surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds); 173925a0400SGerd Hoffmann surface_gl_create_texture(vc->gfx.gls, surface); 174925a0400SGerd Hoffmann } 175925a0400SGerd Hoffmann vc->gfx.ds = surface; 176925a0400SGerd Hoffmann 177925a0400SGerd Hoffmann if (resized) { 178925a0400SGerd Hoffmann gd_update_windowsize(vc); 179925a0400SGerd Hoffmann } 180925a0400SGerd Hoffmann } 181925a0400SGerd Hoffmann 18209053670SVolker Rümelin static int gd_cmp_gl_context_version(int major, int minor, QEMUGLParams *params) 18309053670SVolker Rümelin { 18409053670SVolker Rümelin if (major > params->major_ver) { 18509053670SVolker Rümelin return 1; 18609053670SVolker Rümelin } 18709053670SVolker Rümelin if (major < params->major_ver) { 18809053670SVolker Rümelin return -1; 18909053670SVolker Rümelin } 19009053670SVolker Rümelin if (minor > params->minor_ver) { 19109053670SVolker Rümelin return 1; 19209053670SVolker Rümelin } 19309053670SVolker Rümelin if (minor < params->minor_ver) { 19409053670SVolker Rümelin return -1; 19509053670SVolker Rümelin } 19609053670SVolker Rümelin return 0; 19709053670SVolker Rümelin } 19809053670SVolker Rümelin 1995e79d516SMarc-André Lureau QEMUGLContext gd_gl_area_create_context(DisplayGLCtx *dgc, 200925a0400SGerd Hoffmann QEMUGLParams *params) 201925a0400SGerd Hoffmann { 2025e79d516SMarc-André Lureau VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc); 203925a0400SGerd Hoffmann GdkWindow *window; 204925a0400SGerd Hoffmann GdkGLContext *ctx; 205925a0400SGerd Hoffmann GError *err = NULL; 20609053670SVolker Rümelin int major, minor; 207925a0400SGerd Hoffmann 208925a0400SGerd Hoffmann window = gtk_widget_get_window(vc->gfx.drawing_area); 209925a0400SGerd Hoffmann ctx = gdk_window_create_gl_context(window, &err); 2102cd1e3f9SPan Nengyuan if (err) { 2112cd1e3f9SPan Nengyuan g_printerr("Create gdk gl context failed: %s\n", err->message); 2122cd1e3f9SPan Nengyuan g_error_free(err); 2132cd1e3f9SPan Nengyuan return NULL; 2142cd1e3f9SPan Nengyuan } 215925a0400SGerd Hoffmann gdk_gl_context_set_required_version(ctx, 216925a0400SGerd Hoffmann params->major_ver, 217925a0400SGerd Hoffmann params->minor_ver); 218925a0400SGerd Hoffmann gdk_gl_context_realize(ctx, &err); 2192cd1e3f9SPan Nengyuan if (err) { 2202cd1e3f9SPan Nengyuan g_printerr("Realize gdk gl context failed: %s\n", err->message); 2212cd1e3f9SPan Nengyuan g_error_free(err); 2222cd1e3f9SPan Nengyuan g_clear_object(&ctx); 2232cd1e3f9SPan Nengyuan return NULL; 2242cd1e3f9SPan Nengyuan } 22509053670SVolker Rümelin 22609053670SVolker Rümelin gdk_gl_context_make_current(ctx); 22709053670SVolker Rümelin gdk_gl_context_get_version(ctx, &major, &minor); 22809053670SVolker Rümelin gdk_gl_context_clear_current(); 22909053670SVolker Rümelin gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area)); 23009053670SVolker Rümelin 23109053670SVolker Rümelin if (gd_cmp_gl_context_version(major, minor, params) == -1) { 23209053670SVolker Rümelin /* created ctx version < requested version */ 23309053670SVolker Rümelin g_clear_object(&ctx); 23409053670SVolker Rümelin } 23509053670SVolker Rümelin 23609053670SVolker Rümelin trace_gd_gl_area_create_context(ctx, params->major_ver, params->minor_ver); 237925a0400SGerd Hoffmann return ctx; 238925a0400SGerd Hoffmann } 239925a0400SGerd Hoffmann 2405e79d516SMarc-André Lureau void gd_gl_area_destroy_context(DisplayGLCtx *dgc, QEMUGLContext ctx) 241925a0400SGerd Hoffmann { 242e561b3b7SVolker Rümelin GdkGLContext *current_ctx = gdk_gl_context_get_current(); 243e561b3b7SVolker Rümelin 244e561b3b7SVolker Rümelin trace_gd_gl_area_destroy_context(ctx, current_ctx); 245e561b3b7SVolker Rümelin if (ctx == current_ctx) { 246e561b3b7SVolker Rümelin gdk_gl_context_clear_current(); 247e561b3b7SVolker Rümelin } 248e561b3b7SVolker Rümelin g_clear_object(&ctx); 249925a0400SGerd Hoffmann } 250925a0400SGerd Hoffmann 251f4c36bdaSGerd Hoffmann void gd_gl_area_scanout_texture(DisplayChangeListener *dcl, 252f4c36bdaSGerd Hoffmann uint32_t backing_id, 253f4c36bdaSGerd Hoffmann bool backing_y_0_top, 254f4c36bdaSGerd Hoffmann uint32_t backing_width, 255f4c36bdaSGerd Hoffmann uint32_t backing_height, 256925a0400SGerd Hoffmann uint32_t x, uint32_t y, 257bf41ab61SMarc-André Lureau uint32_t w, uint32_t h, 258bf41ab61SMarc-André Lureau void *d3d_tex2d) 259925a0400SGerd Hoffmann { 260925a0400SGerd Hoffmann VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 261925a0400SGerd Hoffmann 262925a0400SGerd Hoffmann vc->gfx.x = x; 263925a0400SGerd Hoffmann vc->gfx.y = y; 264925a0400SGerd Hoffmann vc->gfx.w = w; 265925a0400SGerd Hoffmann vc->gfx.h = h; 266925a0400SGerd Hoffmann vc->gfx.y0_top = backing_y_0_top; 267925a0400SGerd Hoffmann 268925a0400SGerd Hoffmann gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area)); 269925a0400SGerd Hoffmann 2702ff408deSAnthoine Bourgeois if (backing_id == 0 || vc->gfx.w == 0 || vc->gfx.h == 0) { 271925a0400SGerd Hoffmann gtk_gl_area_set_scanout_mode(vc, false); 272925a0400SGerd Hoffmann return; 273925a0400SGerd Hoffmann } 274925a0400SGerd Hoffmann 27558ea90f8SDongwon Kim gtk_gl_area_set_scanout_mode(vc, true); 27674083f9cSGerd Hoffmann egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height, 27774083f9cSGerd Hoffmann backing_id, false); 278925a0400SGerd Hoffmann } 279925a0400SGerd Hoffmann 280568b12fcSMarc-André Lureau void gd_gl_area_scanout_disable(DisplayChangeListener *dcl) 281568b12fcSMarc-André Lureau { 282568b12fcSMarc-André Lureau VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 283568b12fcSMarc-André Lureau 284568b12fcSMarc-André Lureau gtk_gl_area_set_scanout_mode(vc, false); 285568b12fcSMarc-André Lureau } 286568b12fcSMarc-André Lureau 287925a0400SGerd Hoffmann void gd_gl_area_scanout_flush(DisplayChangeListener *dcl, 288925a0400SGerd Hoffmann uint32_t x, uint32_t y, uint32_t w, uint32_t h) 289925a0400SGerd Hoffmann { 290925a0400SGerd Hoffmann VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 291925a0400SGerd Hoffmann 292*6779a307SDongwon Kim if (vc->gfx.guest_fb.dmabuf && 293*6779a307SDongwon Kim !qemu_dmabuf_get_draw_submitted(vc->gfx.guest_fb.dmabuf)) { 29465b847d2SVivek Kasireddy graphic_hw_gl_block(vc->gfx.dcl.con, true); 29555f4b767SDongwon Kim vc->gfx.guest_fb.dmabuf->draw_submitted = true; 29692b58156SDongwon Kim gtk_gl_area_set_scanout_mode(vc, true); 29765b847d2SVivek Kasireddy } 298925a0400SGerd Hoffmann gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area)); 299925a0400SGerd Hoffmann } 300925a0400SGerd Hoffmann 3012606519bSMarc-André Lureau void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl, 3022606519bSMarc-André Lureau QemuDmaBuf *dmabuf) 3032606519bSMarc-André Lureau { 304bc6a3565SAkihiko Odaki #ifdef CONFIG_GBM 3052606519bSMarc-André Lureau VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl); 306*6779a307SDongwon Kim uint32_t x, y, width, height, backing_width, backing_height, texture; 307*6779a307SDongwon Kim bool y0_top; 3082606519bSMarc-André Lureau 3092606519bSMarc-André Lureau gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area)); 3102606519bSMarc-André Lureau egl_dmabuf_import_texture(dmabuf); 311*6779a307SDongwon Kim texture = qemu_dmabuf_get_texture(dmabuf); 312*6779a307SDongwon Kim if (!texture) { 3132606519bSMarc-André Lureau return; 3142606519bSMarc-André Lureau } 3152606519bSMarc-André Lureau 316*6779a307SDongwon Kim x = qemu_dmabuf_get_x(dmabuf); 317*6779a307SDongwon Kim y = qemu_dmabuf_get_y(dmabuf); 318*6779a307SDongwon Kim width = qemu_dmabuf_get_width(dmabuf); 319*6779a307SDongwon Kim height = qemu_dmabuf_get_height(dmabuf); 320*6779a307SDongwon Kim backing_width = qemu_dmabuf_get_backing_width(dmabuf); 321*6779a307SDongwon Kim backing_height = qemu_dmabuf_get_backing_height(dmabuf); 322*6779a307SDongwon Kim y0_top = qemu_dmabuf_get_y0_top(dmabuf); 32365b847d2SVivek Kasireddy 324*6779a307SDongwon Kim gd_gl_area_scanout_texture(dcl, texture, y0_top, 325*6779a307SDongwon Kim backing_width, backing_height, 326*6779a307SDongwon Kim x, y, width, height, NULL); 327*6779a307SDongwon Kim 328*6779a307SDongwon Kim if (qemu_dmabuf_get_allow_fences(dmabuf)) { 32965b847d2SVivek Kasireddy vc->gfx.guest_fb.dmabuf = dmabuf; 33065b847d2SVivek Kasireddy } 3312606519bSMarc-André Lureau #endif 3322606519bSMarc-André Lureau } 3332606519bSMarc-André Lureau 334925a0400SGerd Hoffmann void gtk_gl_area_init(void) 335925a0400SGerd Hoffmann { 336925a0400SGerd Hoffmann display_opengl = 1; 337925a0400SGerd Hoffmann } 338925a0400SGerd Hoffmann 3395e79d516SMarc-André Lureau int gd_gl_area_make_current(DisplayGLCtx *dgc, 340925a0400SGerd Hoffmann QEMUGLContext ctx) 341925a0400SGerd Hoffmann { 342925a0400SGerd Hoffmann gdk_gl_context_make_current(ctx); 343925a0400SGerd Hoffmann return 0; 344925a0400SGerd Hoffmann } 345