qemu/ui/gtk-gl-area.c
<<
>>
Prefs
   1/*
   2 * GTK UI -- glarea opengl code.
   3 *
   4 * Requires 3.16+ (GtkGLArea widget).
   5 *
   6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
   7 * See the COPYING file in the top-level directory.
   8 */
   9
  10#include "qemu/osdep.h"
  11#include "qemu-common.h"
  12
  13#include "trace.h"
  14
  15#include "ui/console.h"
  16#include "ui/gtk.h"
  17#include "ui/egl-helpers.h"
  18
  19#include "sysemu/sysemu.h"
  20
  21static void gtk_gl_area_set_scanout_mode(VirtualConsole *vc, bool scanout)
  22{
  23    if (vc->gfx.scanout_mode == scanout) {
  24        return;
  25    }
  26
  27    vc->gfx.scanout_mode = scanout;
  28    if (!vc->gfx.scanout_mode) {
  29        egl_fb_destroy(&vc->gfx.guest_fb);
  30        if (vc->gfx.surface) {
  31            surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
  32            surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
  33        }
  34    }
  35}
  36
  37/** DisplayState Callbacks (opengl version) **/
  38
  39void gd_gl_area_draw(VirtualConsole *vc)
  40{
  41    int ww, wh, y1, y2;
  42
  43    if (!vc->gfx.gls) {
  44        return;
  45    }
  46
  47    gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
  48    ww = gtk_widget_get_allocated_width(vc->gfx.drawing_area);
  49    wh = gtk_widget_get_allocated_height(vc->gfx.drawing_area);
  50
  51    if (vc->gfx.scanout_mode) {
  52        if (!vc->gfx.guest_fb.framebuffer) {
  53            return;
  54        }
  55
  56        glBindFramebuffer(GL_READ_FRAMEBUFFER, vc->gfx.guest_fb.framebuffer);
  57        /* GtkGLArea sets GL_DRAW_FRAMEBUFFER for us */
  58
  59        glViewport(0, 0, ww, wh);
  60        y1 = vc->gfx.y0_top ? 0 : vc->gfx.h;
  61        y2 = vc->gfx.y0_top ? vc->gfx.h : 0;
  62        glBlitFramebuffer(0, y1, vc->gfx.w, y2,
  63                          0, 0, ww, wh,
  64                          GL_COLOR_BUFFER_BIT, GL_NEAREST);
  65    } else {
  66        if (!vc->gfx.ds) {
  67            return;
  68        }
  69        gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
  70
  71        surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, ww, wh);
  72        surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
  73    }
  74}
  75
  76void gd_gl_area_update(DisplayChangeListener *dcl,
  77                   int x, int y, int w, int h)
  78{
  79    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
  80
  81    if (!vc->gfx.gls || !vc->gfx.ds) {
  82        return;
  83    }
  84
  85    gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
  86    surface_gl_update_texture(vc->gfx.gls, vc->gfx.ds, x, y, w, h);
  87    vc->gfx.glupdates++;
  88}
  89
  90void gd_gl_area_refresh(DisplayChangeListener *dcl)
  91{
  92    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
  93
  94    if (!vc->gfx.gls) {
  95        if (!gtk_widget_get_realized(vc->gfx.drawing_area)) {
  96            return;
  97        }
  98        gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
  99        vc->gfx.gls = qemu_gl_init_shader();
 100        if (vc->gfx.ds) {
 101            surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
 102        }
 103    }
 104
 105    graphic_hw_update(dcl->con);
 106
 107    if (vc->gfx.glupdates) {
 108        vc->gfx.glupdates = 0;
 109        gtk_gl_area_set_scanout_mode(vc, false);
 110        gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
 111    }
 112}
 113
 114void gd_gl_area_switch(DisplayChangeListener *dcl,
 115                       DisplaySurface *surface)
 116{
 117    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
 118    bool resized = true;
 119
 120    trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
 121
 122    if (vc->gfx.ds &&
 123        surface_width(vc->gfx.ds) == surface_width(surface) &&
 124        surface_height(vc->gfx.ds) == surface_height(surface)) {
 125        resized = false;
 126    }
 127
 128    if (vc->gfx.gls) {
 129        gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
 130        surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
 131        surface_gl_create_texture(vc->gfx.gls, surface);
 132    }
 133    vc->gfx.ds = surface;
 134
 135    if (resized) {
 136        gd_update_windowsize(vc);
 137    }
 138}
 139
 140QEMUGLContext gd_gl_area_create_context(DisplayChangeListener *dcl,
 141                                        QEMUGLParams *params)
 142{
 143    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
 144    GdkWindow *window;
 145    GdkGLContext *ctx;
 146    GError *err = NULL;
 147
 148    gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
 149    window = gtk_widget_get_window(vc->gfx.drawing_area);
 150    ctx = gdk_window_create_gl_context(window, &err);
 151    gdk_gl_context_set_required_version(ctx,
 152                                        params->major_ver,
 153                                        params->minor_ver);
 154    gdk_gl_context_realize(ctx, &err);
 155    return ctx;
 156}
 157
 158void gd_gl_area_destroy_context(DisplayChangeListener *dcl, QEMUGLContext ctx)
 159{
 160    /* FIXME */
 161}
 162
 163void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
 164                                uint32_t backing_id,
 165                                bool backing_y_0_top,
 166                                uint32_t backing_width,
 167                                uint32_t backing_height,
 168                                uint32_t x, uint32_t y,
 169                                uint32_t w, uint32_t h)
 170{
 171    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
 172
 173    vc->gfx.x = x;
 174    vc->gfx.y = y;
 175    vc->gfx.w = w;
 176    vc->gfx.h = h;
 177    vc->gfx.y0_top = backing_y_0_top;
 178
 179    gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
 180
 181    if (backing_id == 0 || vc->gfx.w == 0 || vc->gfx.h == 0) {
 182        gtk_gl_area_set_scanout_mode(vc, false);
 183        return;
 184    }
 185
 186    gtk_gl_area_set_scanout_mode(vc, true);
 187    egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
 188                         backing_id, false);
 189}
 190
 191void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
 192                          uint32_t x, uint32_t y, uint32_t w, uint32_t h)
 193{
 194    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
 195
 196    gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
 197}
 198
 199void gtk_gl_area_init(void)
 200{
 201    display_opengl = 1;
 202}
 203
 204QEMUGLContext gd_gl_area_get_current_context(DisplayChangeListener *dcl)
 205{
 206    return gdk_gl_context_get_current();
 207}
 208
 209int gd_gl_area_make_current(DisplayChangeListener *dcl,
 210                            QEMUGLContext ctx)
 211{
 212    gdk_gl_context_make_current(ctx);
 213    return 0;
 214}
 215