Skip to contentSkip to conversation

spice: add opengl/virgl/dmabuf support

PATCH Superseded Gerd Hoffmann 3 participants 16 messages Feb 18, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover spice: add opengl/virgl/dmabuf support

Hi,

This patch series adds support for opengl and dma-buf passing to spice. With this in place you can use virtio-gpu in 3d (virgl) mode with spice. Not fully clear whenever the last patch (tagged as RfC) is actually needed.

please review,
Gerd

Gerd Hoffmann (7):
configure: add dma-buf support detection.
egl-helpers: add functions for render nodes and dma-buf passing
console: track gl_block state in QemuConsole
spice: add opengl/virgl/dmabuf support
spice: tweak debug messages.
spice/gl: render DisplaySurface via opengl
spice/gl: create dummy primary surface (RfC)

Marc-André Lureau (1):
spice: reset cursor on resize

 configure                  |  20 +++-
 include/ui/console.h       |   1 +
 include/ui/egl-helpers.h   |  13 +++
 include/ui/spice-display.h |  20 ++++
 qemu-options.hx            |   4 +
 ui/console.c               |  15 ++-
 ui/egl-helpers.c           | 129 +++++++++++++++++++++++++
 ui/spice-core.c            |  18 +++-
 ui/spice-display.c         | 230 +++++++++++++++++++++++++++++++++++++++++++--
 9 files changed, 437 insertions(+), 13 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/8 configure: add dma-buf support detection.

Set CONFIG_OPENGL_DMABUF in case both mesa and libepoxy are new enough to have support for dma-buf import/export.

---
 configure | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
configure +19 -1
index 0aa249b..0c0472a 100755
@@ -279,6 +279,7 @@ smartcard=""
 libusb=""
 usb_redir=""
 opengl=""
+opengl_dmabuf="no"
 zlib="yes"
 lzo=""
 snappy=""
@@ -3274,7 +3275,7 @@ libs_softmmu="$libs_softmmu $fdt_libs"
 # opengl probe (for sdl2, gtk, milkymist-tmu2)
 
 if test "$opengl" != "no" ; then
-  opengl_pkgs="epoxy"
+  opengl_pkgs="epoxy libdrm gbm"
   if $pkg_config $opengl_pkgs x11; then
     opengl_cflags="$($pkg_config --cflags $opengl_pkgs) $x11_cflags"
     opengl_libs="$($pkg_config --libs $opengl_pkgs) $x11_libs"
@@ -3292,6 +3293,18 @@ if test "$opengl" != "no" ; then
   fi
 fi
 
+if test "$opengl" = "yes"; then
+  cat > $TMPC << EOF
+#include <epoxy/egl.h>
+#ifndef EGL_MESA_image_dma_buf_export
+# error mesa/epoxy lacks support for dmabufs (mesa 10.6+)
+#endif
+int main(void) { return 0; }
+EOF
+  if compile_prog "" "" ; then
+    opengl_dmabuf=yes
+  fi
+fi
 
 ##########################################
 # archipelago probe
@@ -4752,6 +4765,7 @@ echo "smartcard support $smartcard"
 echo "libusb            $libusb"
 echo "usb net redir     $usb_redir"
 echo "OpenGL support    $opengl"
+echo "OpenGL dmabufs    $opengl_dmabuf"
 echo "libiscsi support  $libiscsi"
 echo "libnfs support    $libnfs"
 echo "build guest agent $guest_agent"
@@ -5050,6 +5064,7 @@ if test "$gtk" = "yes" ; then
   echo "CONFIG_GTK=y" >> $config_host_mak
   echo "CONFIG_GTKABI=$gtkabi" >> $config_host_mak
   echo "GTK_CFLAGS=$gtk_cflags" >> $config_host_mak
+  echo "GTK_LIBS=$gtk_libs" >> $config_host_mak
   if test "$gtk_gl" = "yes" ; then
     echo "CONFIG_GTK_GL=y" >> $config_host_mak
   fi
@@ -5158,6 +5173,9 @@ if test "$opengl" = "yes" ; then
   echo "CONFIG_OPENGL=y" >> $config_host_mak
   echo "OPENGL_CFLAGS=$opengl_cflags" >> $config_host_mak
   echo "OPENGL_LIBS=$opengl_libs" >> $config_host_mak
+  if test "$opengl_dmabuf" = "yes" ; then
+    echo "CONFIG_OPENGL_DMABUF=y" >> $config_host_mak
+  fi
 fi
 
 if test "$lzo" = "yes" ; then
Signature · 1 line
1.8.3.1
Reviewed-by on v1 1/8
#2 lore raw

Re: [Qemu-devel] [PATCH 1/8] configure: add dma-buf support detection.

Hi

Quoted 57 lines · Gerd Hoffmann

Set CONFIG_OPENGL_DMABUF in case both mesa and libepoxy are new enough to have support for dma-buf import/export.

---
 configure | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
configure +15 -1
index 0aa249b..0c0472a 100755
@@ -279,6 +279,7 @@ smartcard=""
 libusb=""
 usb_redir=""
 opengl=""
+opengl_dmabuf="no"
 zlib="yes"
 lzo=""
 snappy=""
@@ -3274,7 +3275,7 @@ libs_softmmu="$libs_softmmu $fdt_libs"
 # opengl probe (for sdl2, gtk, milkymist-tmu2)
 
 if test "$opengl" != "no" ; then
-  opengl_pkgs="epoxy"
+  opengl_pkgs="epoxy libdrm gbm"
   if $pkg_config $opengl_pkgs x11; then
     opengl_cflags="$($pkg_config --cflags $opengl_pkgs) $x11_cflags"
     opengl_libs="$($pkg_config --libs $opengl_pkgs) $x11_libs"
@@ -3292,6 +3293,18 @@ if test "$opengl" != "no" ; then
   fi
 fi
 
+if test "$opengl" = "yes"; then
+  cat > $TMPC << EOF
+#include <epoxy/egl.h>
+#ifndef EGL_MESA_image_dma_buf_export
+# error mesa/epoxy lacks support for dmabufs (mesa 10.6+)
+#endif
+int main(void) { return 0; }
+EOF
+  if compile_prog "" "" ; then
+    opengl_dmabuf=yes
+  fi
+fi
 
 ##########################################
 # archipelago probe
@@ -4752,6 +4765,7 @@ echo "smartcard support $smartcard"
 echo "libusb            $libusb"
 echo "usb net redir     $usb_redir"
 echo "OpenGL support    $opengl"
+echo "OpenGL dmabufs    $opengl_dmabuf"
 echo "libiscsi support  $libiscsi"
 echo "libnfs support    $libnfs"
 echo "build guest agent $guest_agent"

configure if test "$gtk" = "yes" ; then · line 5067

Quoted 4 lines
configure +1 -0
@@ -5050,6 +5064,7 @@ if test "$gtk" = "yes" ; then
   echo "CONFIG_GTK=y" >> $config_host_mak
   echo "CONFIG_GTKABI=$gtkabi" >> $config_host_mak
   echo "GTK_CFLAGS=$gtk_cflags" >> $config_host_mak
+  echo "GTK_LIBS=$gtk_libs" >> $config_host_mak

This could be seperated

Quoted 14 lines
configure +3 -1
   if test "$gtk_gl" = "yes" ; then
     echo "CONFIG_GTK_GL=y" >> $config_host_mak
   fi
@@ -5158,6 +5173,9 @@ if test "$opengl" = "yes" ; then
   echo "CONFIG_OPENGL=y" >> $config_host_mak
   echo "OPENGL_CFLAGS=$opengl_cflags" >> $config_host_mak
   echo "OPENGL_LIBS=$opengl_libs" >> $config_host_mak
+  if test "$opengl_dmabuf" = "yes" ; then
+    echo "CONFIG_OPENGL_DMABUF=y" >> $config_host_mak
+  fi
 fi
 
 if test "$lzo" = "yes" ; then
--
Quoted 2 lines

1.8.3.1

Signature · 1 line
Marc-André Lureau
#3 lore raw

PATCH 2/8 egl-helpers: add functions for render nodes and dma-buf passing

In reply to Gerd Hoffmann

Adds helpers to open a drm render node and create a opengl context for it. Also add a helper to export a texture as dma-buf.

---
 include/ui/egl-helpers.h |  13 +++++
 ui/egl-helpers.c         | 129 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 142 insertions(+)
include/ui/egl-helpers.h +13 -0
index 8c84398..03fcf4b 100644
@@ -3,10 +3,23 @@
 
 #include <epoxy/gl.h>
 #include <epoxy/egl.h>
+#include <gbm.h>
 
 extern EGLDisplay *qemu_egl_display;
 extern EGLConfig qemu_egl_config;
 
+#ifdef CONFIG_OPENGL_DMABUF
+
+extern int qemu_egl_rn_fd;
+extern struct gbm_device *qemu_egl_rn_gbm_dev;
+extern EGLContext qemu_egl_rn_ctx;
+
+int qemu_egl_rendernode_open(void);
+int egl_rendernode_init(void);
+int egl_get_fd_for_texture(uint32_t tex_id, EGLint *stride, EGLint *fourcc);
+
+#endif
+
 EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, Window win);
 
 int qemu_egl_init_dpy(EGLNativeDisplayType dpy, bool gles, bool debug);
ui/egl-helpers.c +129 -0

141 lines not on this page · raw message

Signature · 1 line
1.8.3.1
Reviewed-by on v1 2/8
#4 lore raw

Re: [Qemu-devel] [PATCH 2/8] egl-helpers: add functions for render nodes and dma-buf passing

Hi

Quoted 186 lines · Gerd Hoffmann

Adds helpers to open a drm render node and create a opengl context for it. Also add a helper to export a texture as dma-buf.

---
 include/ui/egl-helpers.h |  13 +++++
 ui/egl-helpers.c         | 129 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 142 insertions(+)
include/ui/egl-helpers.h +13 -0
index 8c84398..03fcf4b 100644
@@ -3,10 +3,23 @@
 
 #include <epoxy/gl.h>
 #include <epoxy/egl.h>
+#include <gbm.h>
 
 extern EGLDisplay *qemu_egl_display;
 extern EGLConfig qemu_egl_config;
 
+#ifdef CONFIG_OPENGL_DMABUF
+
+extern int qemu_egl_rn_fd;
+extern struct gbm_device *qemu_egl_rn_gbm_dev;
+extern EGLContext qemu_egl_rn_ctx;
+
+int qemu_egl_rendernode_open(void);
+int egl_rendernode_init(void);
+int egl_get_fd_for_texture(uint32_t tex_id, EGLint *stride, EGLint *fourcc);
+
+#endif
+
 EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, Window win);
 
 int qemu_egl_init_dpy(EGLNativeDisplayType dpy, bool gles, bool debug);
ui/egl-helpers.c +129 -1
index 4c83834..54be44c 100644
@@ -1,6 +1,8 @@
 #include "qemu/osdep.h"
 #include <glob.h>
+#include <dirent.h>
 
+#include "config-host.h"
 #include "ui/egl-helpers.h"
 
 EGLDisplay *qemu_egl_display;
@@ -20,6 +22,133 @@ static int egl_debug;
 
 /* ---------------------------------------------------------------------- */
 
+#ifdef CONFIG_OPENGL_DMABUF
+
+int qemu_egl_rn_fd;
+struct gbm_device *qemu_egl_rn_gbm_dev;
+EGLContext qemu_egl_rn_ctx;
+
+int qemu_egl_rendernode_open(void)
+{
+    DIR *dir;
+    struct dirent *e;
+    int r, fd;
+    char *p;
+
+    dir = opendir("/dev/dri");
+    if (!dir) {
+        return -1;
+    }
+
+    fd = -1;
+    while ((e = readdir(dir))) {
+        if (e->d_type != DT_CHR) {
+            continue;
+        }
+
+        if (strncmp(e->d_name, "renderD", 7)) {
+            continue;
+        }
+
+        r = asprintf(&p, "/dev/dri/%s", e->d_name);
+        if (r < 0) {
+            return -1;
+        }
+
+        r = open(p, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
+        if (r < 0) {
+            free(p);
+            continue;
+        }
+        fd = r;
+        free(p);
+        break;
+    }
+
+    closedir(dir);
+    if (fd < 0) {
+        return -1;
+    }
+    return fd;
+}
+
+int egl_rendernode_init(void)
+{
+    qemu_egl_rn_fd = -1;
+
+    qemu_egl_rn_fd = qemu_egl_rendernode_open();
+    if (qemu_egl_rn_fd == -1) {
+        fprintf(stderr, "egl: no drm render node available\n");
+        goto err;
+    }
+
+    qemu_egl_rn_gbm_dev = gbm_create_device(qemu_egl_rn_fd);
+    if (!qemu_egl_rn_gbm_dev) {
+        fprintf(stderr, "egl: gbm_create_device failed\n");
+        goto err;
+    }
+
+    qemu_egl_init_dpy((EGLNativeDisplayType)qemu_egl_rn_gbm_dev, false, false);
+
+    if (!epoxy_has_egl_extension(qemu_egl_display,
+                                 "EGL_KHR_surfaceless_context")) {
+        fprintf(stderr, "egl: EGL_KHR_surfaceless_context not supported\n");
+        goto err;
+    }
+    if (!epoxy_has_egl_extension(qemu_egl_display,
+                                 "EGL_MESA_image_dma_buf_export")) {
+        fprintf(stderr, "egl: EGL_MESA_image_dma_buf_export not supported\n");
+        goto err;
+    }
+
+    qemu_egl_rn_ctx = qemu_egl_init_ctx();
+    if (!qemu_egl_rn_ctx) {
+        fprintf(stderr, "egl: egl_init_ctx failed\n");
+        goto err;
+    }
+
+    return 0;
+
+err:
+    if (qemu_egl_rn_gbm_dev) {
+        gbm_device_destroy(qemu_egl_rn_gbm_dev);
+    }
+    if (qemu_egl_rn_fd != -1) {
+        close(qemu_egl_rn_fd);
+    }
+
+    return -1;
+}
+
+int egl_get_fd_for_texture(uint32_t tex_id, EGLint *stride, EGLint *fourcc)
+{
+    EGLImageKHR image;
+    EGLint num_planes, fd;
+
+    image = eglCreateImageKHR(qemu_egl_display, eglGetCurrentContext(),
+                              EGL_GL_TEXTURE_2D_KHR,
+                              (EGLClientBuffer)(unsigned long)tex_id,
+                              NULL);
+    if (!image) {
+        return -1;
+    }
+
+    eglExportDMABUFImageQueryMESA(qemu_egl_display, image, fourcc,
+                                  &num_planes, NULL);
+    if (num_planes != 1) {
+        eglDestroyImageKHR(qemu_egl_display, image);
+        return -1;
+    }
+    eglExportDMABUFImageMESA(qemu_egl_display, image, &fd, stride, NULL);
+    eglDestroyImageKHR(qemu_egl_display, image);
+
+    return fd;
+}
+
+#endif /* CONFIG_OPENGL_DMABUF */
+
+/* ---------------------------------------------------------------------- */
+
 EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, Window win)
 {
     EGLSurface esurface;
--
Quoted 2 lines

1.8.3.1

Signature · 1 line
Marc-André Lureau
#5 lore raw

PATCH 3/8 console: track gl_block state in QemuConsole

In reply to Gerd Hoffmann

Keep track of gl_block state (added in bba19b8 console: block rendering until client is done) in QemuConsole and allow to query it. This way we can avoid state inconsistencies in case different code paths make use of this.

---
 include/ui/console.h |  1 +
 ui/console.c         | 15 +++++++++++----
 2 files changed, 12 insertions(+), 4 deletions(-)
include/ui/console.h +1 -0
index c8f9f7e..9672ad9 100644
@@ -382,6 +382,7 @@ QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head);
 bool qemu_console_is_visible(QemuConsole *con);
 bool qemu_console_is_graphic(QemuConsole *con);
 bool qemu_console_is_fixedsize(QemuConsole *con);
+bool qemu_console_is_gl_blocked(QemuConsole *con);
 char *qemu_console_get_label(QemuConsole *con);
 int qemu_console_get_index(QemuConsole *con);
 uint32_t qemu_console_get_head(QemuConsole *con);
ui/console.c +11 -4
index b739ae9..215c98d 100644
@@ -123,6 +123,7 @@ struct QemuConsole {
     DisplaySurface *surface;
     int dcls;
     DisplayChangeListener *gl;
+    bool gl_block;
 
     /* Graphic console state.  */
     Object *device;
@@ -264,10 +265,10 @@ void graphic_hw_update(QemuConsole *con)
 
 void graphic_hw_gl_block(QemuConsole *con, bool block)
 {
-    if (!con) {
-        con = active_console;
-    }
-    if (con && con->hw_ops->gl_block) {
+    assert(con != NULL);
+
+    con->gl_block = block;
+    if (con->hw_ops->gl_block) {
         con->hw_ops->gl_block(con->hw, block);
     }
 }
@@ -1811,6 +1812,12 @@ bool qemu_console_is_fixedsize(QemuConsole *con)
     return con && (con->console_type != TEXT_CONSOLE);
 }
 
+bool qemu_console_is_gl_blocked(QemuConsole *con)
+{
+    assert(con != NULL);
+    return con->gl_block;
+}
+
 char *qemu_console_get_label(QemuConsole *con)
 {
     if (con->console_type == GRAPHIC_CONSOLE) {
Signature · 1 line
1.8.3.1
Reviewed-by on v1 3/8
#6 lore raw

Re: [Qemu-devel] [PATCH 3/8] console: track gl_block state in QemuConsole

Hi

Quoted 6 lines · Gerd Hoffmann

Keep track of gl_block state (added in bba19b8 console: block rendering until client is done) in QemuConsole and allow to query it. This way we can avoid state inconsistencies in case different code paths make use of this.

Signature · 1 line
Marc-André Lureau
#7 lore raw

PATCH 4/8 spice: reset cursor on resize

In reply to Gerd Hoffmann

From: Marc-André Lureau <[email protected]>

Spice server will clear the cursor on resize. QXL driver reset it after resize, however, virtio and other devices do not. Teach qemu to set it back.

---
 ui/spice-display.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
ui/spice-display.c +12 -2
index 8a5b325..2a2a7c1 100644
@@ -460,6 +460,13 @@ void qemu_spice_display_switch(SimpleSpiceDisplay *ssd,
 
     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
     ssd->notify++;
+
+    qemu_mutex_lock(&ssd->lock);
+    if (ssd->cursor) {
+        g_free(ssd->ptr_define);
+        ssd->ptr_define = qemu_spice_create_cursor_update(ssd, ssd->cursor, 0);
+    }
+    qemu_mutex_unlock(&ssd->lock);
 }
 
 static void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
@@ -467,8 +474,6 @@ static void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
     if (ssd->cursor) {
         assert(ssd->dcl.con);
         dpy_cursor_define(ssd->dcl.con, ssd->cursor);
-        cursor_put(ssd->cursor);
-        ssd->cursor = NULL;
     }
     if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
         assert(ssd->dcl.con);
@@ -750,6 +755,11 @@ static void display_mouse_define(DisplayChangeListener *dcl,
     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
 
     qemu_mutex_lock(&ssd->lock);
+    if (c) {
+        cursor_get(c);
+    }
+    cursor_put(ssd->cursor);
+    ssd->cursor = c;
     ssd->hot_x = c->hot_x;
     ssd->hot_y = c->hot_y;
     g_free(ssd->ptr_move);
Signature · 1 line
1.8.3.1
#8 lore raw

PATCH 5/8 spice: add opengl/virgl/dmabuf support

In reply to Gerd Hoffmann

This adds support for dma-buf passing to spice. This makes virtio-gpu with 3d acceleration work with spice.

Workflow:
* virglrenderer renders the guest command stream into a texture.
* qemu exports the texture as dma-buf and passes on that dma-buf to spice-server.
* spice-server passes the dma-buf to spice-client, using unix socket file descriptor passing.
* spice-client asks the window systems composer to render the dma-buf to the screen.

Requires cutting edge spice (server) and spice-gtk (client) builds, from git master branch.

Also requires libvirt managing your qemu instance, and using "virt-viewer --attach $guest". libvirt will connect spice-server and spice-client using unix sockets instead of tcp sockets then, which is required for file descriptor passing.

Works for the local case (spice server and client on the same machine) only. Supporting remote too is planned (by feeding the dma-bufs into gpu-assisted video encoder), but not there yet.

gl mode is turned off by default, use "-spice gl=on,$otherargs" to enable it.

---
 include/ui/spice-display.h |  18 +++++++
 qemu-options.hx            |   4 ++
 ui/spice-core.c            |  18 ++++++-
 ui/spice-display.c         | 129 +++++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 165 insertions(+), 4 deletions(-)
include/ui/spice-display.h +18 -0
index b25328a..5c5726a 100644
@@ -23,6 +23,16 @@
 #include "ui/qemu-pixman.h"
 #include "ui/console.h"
 #include "sysemu/sysemu.h"
+#ifdef CONFIG_OPENGL_DMABUF
+#include "ui/egl-helpers.h"
+#include "ui/egl-context.h"
+#endif
+
+#if defined(CONFIG_OPENGL_DMABUF)
+# if SPICE_SERVER_VERSION >= 0x000d00 /* release 0.13.0 */
+#  define HAVE_SPICE_GL 1
+# endif
+#endif
 
 #define NUM_MEMSLOTS 8
 #define MEMSLOT_GENERATION_BITS 8
@@ -50,6 +60,7 @@ enum {
     QXL_COOKIE_TYPE_IO,
     QXL_COOKIE_TYPE_RENDER_UPDATE_AREA,
     QXL_COOKIE_TYPE_POST_LOAD_MONITORS_CONFIG,
+    QXL_COOKIE_TYPE_GL_DRAW_DONE,
 };
 
 typedef struct QXLCookie {
@@ -104,6 +115,13 @@ struct SimpleSpiceDisplay {
     QEMUCursor *cursor;
     int mouse_x, mouse_y;
     QEMUBH *cursor_bh;
+
+#ifdef HAVE_SPICE_GL
+    /* opengl rendering */
+    QEMUBH *gl_unblock_bh;
+    QEMUTimer *gl_unblock_timer;
+    int dmabuf_fd;
+#endif
 };
 
 struct SimpleSpiceUpdate {
qemu-options.hx +4 -0
index 2f0465e..8cc367e 100644
@@ -1051,6 +1051,7 @@ DEF("spice", HAS_ARG, QEMU_OPTION_spice,
     "       [,streaming-video=[off|all|filter]][,disable-copy-paste]\n"
     "       [,disable-agent-file-xfer][,agent-mouse=[on|off]]\n"
     "       [,playback-compression=[on|off]][,seamless-migration=[on|off]]\n"
+    "       [,gl=[on|off]\n"
     "   enable spice\n"
     "   at least one of {port, tls-port} is mandatory\n",
     QEMU_ARCH_ALL)
@@ -1142,6 +1143,9 @@ Enable/disable audio stream compression (using celt 0.5.1).  Default is on.
 @item seamless-migration=[on|off]
 Enable/disable spice seamless migration. Default is off.
 
+@item gl=[on|off]
+Enable/disable OpenGL context. Default is off.
+
 @end table
 ETEXI
 
ui/spice-core.c +17 -1
index 4dbd99a..2c9aba5 100644
@@ -39,6 +39,9 @@
 #include "hw/hw.h"
 #include "ui/spice-display.h"
 #include "qapi-event.h"
+#ifdef CONFIG_OPENGL_DMABUF
+#include "ui/egl-helpers.h"
+#endif
 
 /* core bits */
 
@@ -494,9 +497,14 @@ static QemuOptsList qemu_spice_opts = {
         },{
             .name = "playback-compression",
             .type = QEMU_OPT_BOOL,
-        }, {
+        },{
             .name = "seamless-migration",
             .type = QEMU_OPT_BOOL,
+#ifdef CONFIG_OPENGL_DMABUF
+        },{
+            .name = "gl",
+            .type = QEMU_OPT_BOOL,
+#endif
         },
         { /* end of list */ }
     },
@@ -819,6 +827,14 @@ void qemu_spice_init(void)
 #if SPICE_SERVER_VERSION >= 0x000c02
     qemu_spice_register_ports();
 #endif
+
+#ifdef CONFIG_OPENGL_DMABUF
+    if (qemu_opt_get_bool(opts, "gl", 0)) {
+        if (egl_rendernode_init() == 0) {
+            display_opengl = 1;
+        }
+    }
+#endif
 }
 
 int qemu_spice_add_interface(SpiceBaseInstance *sin)
ui/spice-display.c +126 -3

147 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#9 lore raw

Hi

Quoted 20 lines · Gerd Hoffmann

This adds support for dma-buf passing to spice. This makes virtio-gpu with 3d acceleration work with spice.

Workflow:
* virglrenderer renders the guest command stream into a texture.
* qemu exports the texture as dma-buf and passes on that dma-buf to spice-server.
* spice-server passes the dma-buf to spice-client, using unix socket file descriptor passing.
* spice-client asks the window systems composer to render the dma-buf to the screen.

Requires cutting edge spice (server) and spice-gtk (client) builds, from git master branch.

Also requires libvirt managing your qemu instance, and using "virt-viewer --attach $guest". libvirt will connect spice-server and spice-client using unix sockets instead of tcp sockets then, which is required for file descriptor passing.

Quoted 8 lines

Works for the local case (spice server and client on the same machine) only. Supporting remote too is planned (by feeding the dma-bufs into gpu-assisted video encoder), but not there yet.

gl mode is turned off by default, use "-spice gl=on,$otherargs" to enable it.

I did the initial implementation of this patch, and I spent considerable time testing and fixing yours. Even if you don't always take my patch or fixes, I think it'd be fair adding me as Signed-off.

Quoted 12 lines
---
 include/ui/spice-display.h |  18 +++++++
 qemu-options.hx            |   4 ++
 ui/spice-core.c            |  18 ++++++-
 ui/spice-display.c         | 129 +++++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 165 insertions(+), 4 deletions(-)
include/ui/spice-display.h +0 -0
index b25328a..5c5726a 100644

include/ui/spice-display.h line 32

Quoted 10 lines
include/ui/spice-display.h +7 -0
@@ -23,6 +23,16 @@
 #include "ui/qemu-pixman.h"
 #include "ui/console.h"
 #include "sysemu/sysemu.h"
+#ifdef CONFIG_OPENGL_DMABUF
+#include "ui/egl-helpers.h"
+#include "ui/egl-context.h"
+#endif
+
+#if defined(CONFIG_OPENGL_DMABUF)
+# if SPICE_SERVER_VERSION >= 0x000d00 /* release 0.13.0 */

It will be most likely 0.13.1, at least not 0.13.0.

Quoted 15 lines
include/ui/spice-display.h +4 -0
+#  define HAVE_SPICE_GL 1
+# endif
+#endif
 
 #define NUM_MEMSLOTS 8
 #define MEMSLOT_GENERATION_BITS 8
@@ -50,6 +60,7 @@ enum {
     QXL_COOKIE_TYPE_IO,
     QXL_COOKIE_TYPE_RENDER_UPDATE_AREA,
     QXL_COOKIE_TYPE_POST_LOAD_MONITORS_CONFIG,
+    QXL_COOKIE_TYPE_GL_DRAW_DONE,
 };
 
 typedef struct QXLCookie {

include/ui/spice-display.h struct SimpleSpiceDisplay { · line 122

Quoted 8 lines
include/ui/spice-display.h +5 -0
@@ -104,6 +115,13 @@ struct SimpleSpiceDisplay {
     QEMUCursor *cursor;
     int mouse_x, mouse_y;
     QEMUBH *cursor_bh;
+
+#ifdef HAVE_SPICE_GL
+    /* opengl rendering */
+    QEMUBH *gl_unblock_bh;
+    QEMUTimer *gl_unblock_timer;

I think it would make sense if this helper timer would be a seperate patch (we may want to revert or change it). It would make the patch easier to read.

Quoted 10 lines
include/ui/spice-display.h +2 -0
+    int dmabuf_fd;
+#endif
 };
 
 struct SimpleSpiceUpdate {
qemu-options.hx +0 -0
index 2f0465e..8cc367e 100644

qemu-options.hx DEF("spice", HAS_ARG, QEMU_OPTION_spice, · line 1054

Quoted 4 lines
qemu-options.hx +1 -0
@@ -1051,6 +1051,7 @@ DEF("spice", HAS_ARG, QEMU_OPTION_spice,
     "       [,streaming-video=[off|all|filter]][,disable-copy-paste]\n"
     "       [,disable-agent-file-xfer][,agent-mouse=[on|off]]\n"
     "       [,playback-compression=[on|off]][,seamless-migration=[on|off]]\n"
+    "       [,gl=[on|off]\n"

missing ]

Quoted 18 lines
qemu-options.hx +3 -0
     "   enable spice\n"
     "   at least one of {port, tls-port} is mandatory\n",
     QEMU_ARCH_ALL)
@@ -1142,6 +1143,9 @@ Enable/disable audio stream compression (using celt 0.5.1).  Default is on.
 @item seamless-migration=[on|off]
 Enable/disable spice seamless migration. Default is off.
 
+@item gl=[on|off]
+Enable/disable OpenGL context. Default is off.
+
 @end table
 ETEXI
 
ui/spice-core.c +0 -0
index 4dbd99a..2c9aba5 100644

ui/spice-core.c line 42

Quoted 4 lines
ui/spice-core.c +1 -0
@@ -39,6 +39,9 @@
 #include "hw/hw.h"
 #include "ui/spice-display.h"
 #include "qapi-event.h"
+#ifdef CONFIG_OPENGL_DMABUF

we may want to use HAVE_SPICE_GL to simplify things (what can we expect with dmabuf && !spice-gl here?)

Quoted 123 lines
ui/spice-core.c +16 -1
+#include "ui/egl-helpers.h"
+#endif
 
 /* core bits */
 
@@ -494,9 +497,14 @@ static QemuOptsList qemu_spice_opts = {
         },{
             .name = "playback-compression",
             .type = QEMU_OPT_BOOL,
-        }, {
+        },{
             .name = "seamless-migration",
             .type = QEMU_OPT_BOOL,
+#ifdef CONFIG_OPENGL_DMABUF
+        },{
+            .name = "gl",
+            .type = QEMU_OPT_BOOL,
+#endif
         },
         { /* end of list */ }
     },
@@ -819,6 +827,14 @@ void qemu_spice_init(void)
 #if SPICE_SERVER_VERSION >= 0x000c02
     qemu_spice_register_ports();
 #endif
+
+#ifdef CONFIG_OPENGL_DMABUF
+    if (qemu_opt_get_bool(opts, "gl", 0)) {
+        if (egl_rendernode_init() == 0) {
+            display_opengl = 1;
+        }
+    }
+#endif
 }
 
 int qemu_spice_add_interface(SpiceBaseInstance *sin)
ui/spice-display.c +69 -3
index 2a2a7c1..17a71ce 100644
@@ -650,9 +650,23 @@ static void interface_update_area_complete(QXLInstance *sin,
 /* called from spice server thread context only */
 static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
 {
-    /* should never be called, used in qxl native mode only */
-    fprintf(stderr, "%s: abort()\n", __func__);
-    abort();
+    QXLCookie *cookie = (QXLCookie *)(uintptr_t)cookie_token;
+
+    switch (cookie->type) {
+#ifdef HAVE_SPICE_GL
+    case QXL_COOKIE_TYPE_GL_DRAW_DONE:
+    {
+        SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
+        qemu_bh_schedule(ssd->gl_unblock_bh);
+        break;
+    }
+#endif
+    default:
+        /* should never be called, used in qxl native mode only */
+        fprintf(stderr, "%s: abort()\n", __func__);
+        abort();
+    }
+    g_free(cookie);
 }
 
 static void interface_set_client_capabilities(QXLInstance *sin,
@@ -779,6 +793,106 @@ static const DisplayChangeListenerOps display_listener_ops = {
     .dpy_cursor_define    = display_mouse_define,
 };
 
+#ifdef HAVE_SPICE_GL
+
+static void qemu_spice_gl_block(SimpleSpiceDisplay *ssd, bool block)
+{
+    uint64_t timeout;
+
+    if (block) {
+        timeout = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
+        timeout += 1000; /* one sec */
+        timer_mod(ssd->gl_unblock_timer, timeout);
+    } else {
+        timer_del(ssd->gl_unblock_timer);
+    }
+    graphic_hw_gl_block(ssd->dcl.con, block);
+}
+
+static void qemu_spice_gl_unblock_bh(void *opaque)
+{
+    SimpleSpiceDisplay *ssd = opaque;
+
+    qemu_spice_gl_block(ssd, false);
+}
+
+static void qemu_spice_gl_block_timer(void *opaque)
+{
+    fprintf(stderr, "WARNING: spice: no gl-draw-done within one second\n");
+}
+
+static QEMUGLContext qemu_spice_gl_create_context(DisplayChangeListener *dcl,
+                                                  QEMUGLParams *params)
+{
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
+                   qemu_egl_rn_ctx);
+    return qemu_egl_create_context(dcl, params);
+}
+
+static void qemu_spice_gl_scanout(DisplayChangeListener *dcl,
+                                  uint32_t tex_id,
+                                  bool y_0_top,
+                                  uint32_t x, uint32_t y,
+                                  uint32_t w, uint32_t h)
+{
+    SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
+    EGLint stride = 0, fourcc = 0;
+    int fd = -1;
+
+    if (tex_id) {
+        fd = egl_get_fd_for_texture(tex_id, &stride, &fourcc);
+        if (fd < 0) {
+            fprintf(stderr, "%s: failed to get fd for texture\n", __func__);
+            return;
+        }

ui/spice-display.c static const DisplayChangeListenerOps display_listener_ops = { · line 857

Quoted 10 lines
ui/spice-display.c +10 -0
+    }
+    dprint(0, "%s: %dx%d (stride %d, fourcc 0x%x)\n", __func__,
+           w, h, stride, fourcc);
+
+    assert(!tex_id || fd >= 0);
+    spice_qxl_gl_scanout(&ssd->qxl, fd,
+                         surface_width(ssd->ds),
+                         surface_height(ssd->ds),
+                         stride, fourcc, y_0_top);
+

The server and client need to know where is the monitor within the scanout. I am surprised you deliberately dropped the monitor config from previous patches. It seems it's not necessary to get things working today, but I am afraid this is not what virgl expects and it will likely break.

ui/spice-display.c static const DisplayChangeListenerOps display_listener_ops = { · line 860

Quoted 3 lines
ui/spice-display.c +3 -0
+    if (ssd->dmabuf_fd != -1) {
+        close(ssd->dmabuf_fd);
+    }

This is incompatible with Spice server API, it is responsible for closing it when no longer needed or outdated (otherwise finding out whether the fd is valid in spice server will be broken)

Quoted 55 lines
ui/spice-display.c +44 -1
+    ssd->dmabuf_fd = fd;
+}
+
+static void qemu_spice_gl_update(DisplayChangeListener *dcl,
+                                 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
+{
+    SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
+    uint64_t cookie;
+
+    dprint(1, "%s\n", __func__);
+    qemu_spice_gl_block(ssd, true);
+    cookie = (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0);
+    spice_qxl_gl_draw_async(&ssd->qxl, x, y, w, h, cookie);
+}
+
+static const DisplayChangeListenerOps display_listener_gl_ops = {
+    .dpy_name             = "spice-egl",
+    .dpy_gfx_update       = display_update,
+    .dpy_gfx_switch       = display_switch,
+    .dpy_gfx_check_format = qemu_pixman_check_format,
+    .dpy_refresh          = display_refresh,
+    .dpy_mouse_set        = display_mouse_set,
+    .dpy_cursor_define    = display_mouse_define,
+
+    .dpy_gl_ctx_create       = qemu_spice_gl_create_context,
+    .dpy_gl_ctx_destroy      = qemu_egl_destroy_context,
+    .dpy_gl_ctx_make_current = qemu_egl_make_context_current,
+    .dpy_gl_ctx_get_current  = qemu_egl_get_current_context,
+
+    .dpy_gl_scanout          = qemu_spice_gl_scanout,
+    .dpy_gl_update           = qemu_spice_gl_update,
+};
+
+#endif /* HAVE_SPICE_GL */
+
 static void qemu_spice_display_init_one(QemuConsole *con)
 {
     SimpleSpiceDisplay *ssd = g_new0(SimpleSpiceDisplay, 1);
@@ -792,6 +906,15 @@ static void qemu_spice_display_init_one(QemuConsole *con)
     qemu_spice_create_host_memslot(ssd);
 
     ssd->dcl.ops = &display_listener_ops;
+#ifdef HAVE_SPICE_GL
+    if (display_opengl) {
+        ssd->dcl.ops = &display_listener_gl_ops;
+        ssd->dmabuf_fd = -1;
+        ssd->gl_unblock_bh = qemu_bh_new(qemu_spice_gl_unblock_bh, ssd);
+        ssd->gl_unblock_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
+                                             qemu_spice_gl_block_timer, ssd);
+    }
+#endif
     ssd->dcl.con = con;
     register_displaychangelistener(&ssd->dcl);
 }
--
Quoted 2 lines

1.8.3.1

After this patch, spice+virgl works :)

Signature · 1 line
Marc-André Lureau
#10 lore raw
In reply to Gerd Hoffmann
Quoted 14 lines · Gerd Hoffmann

This adds support for dma-buf passing to spice. This makes virtio-gpu with 3d acceleration work with spice.

gl mode is turned off by default, use "-spice gl=on,$otherargs" to enable it.

ui/spice-core.c +0 -0
@@ -494,9 +497,14 @@ static QemuOptsList qemu_spice_opts = {
         },{
             .name = "playback-compression",
             .type = QEMU_OPT_BOOL,

ui/spice-core.c static QemuOptsList qemu_spice_opts = { · line 508

Quoted 10 lines
ui/spice-core.c +6 -1
-        }, {
+        },{
             .name = "seamless-migration",
             .type = QEMU_OPT_BOOL,
+#ifdef CONFIG_OPENGL_DMABUF
+        },{
+            .name = "gl",
+            .type = QEMU_OPT_BOOL,
+#endif
         },

How introspectible is this? Does QMP 'query-command-line-options' cope will with showing 'gl' as an option only for the qemu binaries where the #ifdef succeeded?

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#11 lore raw

Hi

Quoted 21 lines · Eric Blake
Quoted 20 lines · Gerd Hoffmann

This adds support for dma-buf passing to spice. This makes virtio-gpu with 3d acceleration work with spice.

gl mode is turned off by default, use "-spice gl=on,$otherargs" to enable it.

ui/spice-core.c +3 -1
@@ -494,9 +497,14 @@ static QemuOptsList qemu_spice_opts = {
         },{
             .name = "playback-compression",
             .type = QEMU_OPT_BOOL,
-        }, {
+        },{
             .name = "seamless-migration",
             .type = QEMU_OPT_BOOL,
+#ifdef CONFIG_OPENGL_DMABUF
+        },{
Quoted 8 lines

ui/spice-core.c static QemuOptsList qemu_spice_opts = { · line 508

Quoted 4 lines
ui/spice-core.c +3 -0
+            .name = "gl",
+            .type = QEMU_OPT_BOOL,
+#endif
         },

How introspectible is this? Does QMP 'query-command-line-options' cope will with showing 'gl' as an option only for the qemu binaries where the #ifdef succeeded?

{ "execute": "query-command-line-options", "arguments": { "option": "spice" } } {"return": [{"parameters": [{"name": "gl", "type": "boolean"},

(I just sent a libvirt patch that queries successfully the capability too)

Signature · 1 line
Marc-André Lureau
#12 lore raw

PATCH 6/8 spice: tweak debug messages.

In reply to Gerd Hoffmann

Adjust message levels, make messages more verbose.

---
 ui/spice-display.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
ui/spice-display.c +7 -5
index 17a71ce..264ecdb 100644
@@ -568,7 +568,7 @@ static int interface_get_command(QXLInstance *sin, QXLCommandExt *ext)
 
 static int interface_req_cmd_notification(QXLInstance *sin)
 {
-    dprint(1, "%s/%d:\n", __func__, sin->id);
+    dprint(2, "%s/%d:\n", __func__, sin->id);
     return 1;
 }
 
@@ -621,7 +621,7 @@ static int interface_get_cursor_command(QXLInstance *sin, QXLCommandExt *ext)
 
 static int interface_req_cursor_notification(QXLInstance *sin)
 {
-    dprint(1, "%s:\n", __FUNCTION__);
+    dprint(2, "%s:\n", __func__);
     return 1;
 }
 
@@ -845,9 +845,11 @@ static void qemu_spice_gl_scanout(DisplayChangeListener *dcl,
             fprintf(stderr, "%s: failed to get fd for texture\n", __func__);
             return;
         }
+        dprint(1, "%s: %dx%d (stride %d, fourcc 0x%x)\n", __func__,
+               w, h, stride, fourcc);
+    } else {
+        dprint(1, "%s: no texture (no framebuffer)\n", __func__);
     }
-    dprint(0, "%s: %dx%d (stride %d, fourcc 0x%x)\n", __func__,
-           w, h, stride, fourcc);
 
     assert(!tex_id || fd >= 0);
     spice_qxl_gl_scanout(&ssd->qxl, fd,
@@ -867,7 +869,7 @@ static void qemu_spice_gl_update(DisplayChangeListener *dcl,
     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
     uint64_t cookie;
 
-    dprint(1, "%s\n", __func__);
+    dprint(2, "%s: %dx%d+%d+%d\n", __func__, w, h, x, y);
     qemu_spice_gl_block(ssd, true);
     cookie = (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0);
     spice_qxl_gl_draw_async(&ssd->qxl, x, y, w, h, cookie);
Signature · 1 line
1.8.3.1
#13 lore raw

PATCH 7/8 spice/gl: render DisplaySurface via opengl

In reply to Gerd Hoffmann

This switches over spice (in opengl mode) to render DisplaySurface updates into a opengl texture, using the helper functions in ui/console-gl.c. With this patch applied spice (with gl=on) will stop using qxl rendering ops, it will use dma-buf passing all the time, i.e. for bios/bootloader (before virtio-gpu driver is loaded) too.

This should improve performance even using spice (with gl=on) with non-accelerated stdvga because we stop squeezing all display updates through a unix/tcp socket and basically using a shared memory transport instead.

---
 include/ui/spice-display.h |  2 ++
 ui/spice-display.c         | 81 ++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 76 insertions(+), 7 deletions(-)
include/ui/spice-display.h +2 -0
index 5c5726a..f613141 100644
@@ -121,6 +121,8 @@ struct SimpleSpiceDisplay {
     QEMUBH *gl_unblock_bh;
     QEMUTimer *gl_unblock_timer;
     int dmabuf_fd;
+    ConsoleGLState *gls;
+    int gl_updates;
 #endif
 };
 
ui/spice-display.c +74 -7

99 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#14 lore raw

PATCH 8/8 spice/gl: create dummy primary surface (RfC)

In reply to Gerd Hoffmann

Current spice client expects we create a primary surface, even if we do display updates using dma-bufs exclusively.

So just do that to get things going.

Not fully clear whenever that is intentional or a bug on the spice side, so I keep this as separate patch for now.

I think this should either be squashed into the previous patch, or dropped after fixing things on the spice side.

---
 ui/spice-display.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
ui/spice-display.c +14 -0
index b222984..54702bb 100644
@@ -856,13 +856,27 @@ static void spice_gl_switch(DisplayChangeListener *dcl,
 {
     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
     EGLint stride, fourcc;
+    bool resize = true;
     int fd;
 
+    if (ssd->ds && new_surface &&
+        surface_width(ssd->ds)  == surface_width(new_surface)  &&
+        surface_height(ssd->ds) == surface_height(new_surface) &&
+        surface_format(ssd->ds) == surface_format(new_surface)) {
+        resize = false;
+    }
+
     if (ssd->ds) {
         surface_gl_destroy_texture(ssd->gls, ssd->ds);
+        if (resize) {
+            qemu_spice_destroy_host_primary(ssd);
+        }
     }
     ssd->ds = new_surface;
     if (ssd->ds) {
+        if (resize) {
+            qemu_spice_create_host_primary(ssd);
+        }
         surface_gl_create_texture(ssd->gls, ssd->ds);
         fd = egl_get_fd_for_texture(ssd->ds->texture,
                                     &stride, &fourcc);
Signature · 1 line
1.8.3.1
#15 lore raw
In reply to Gerd Hoffmann

Hi

Quoted 19 lines · Gerd Hoffmann

Hi,

This patch series adds support for opengl and dma-buf passing to spice. With this in place you can use virtio-gpu in 3d (virgl) mode with spice. Not fully clear whenever the last patch (tagged as RfC) is actually needed.

please review,
Gerd

Gerd Hoffmann (7):
configure: add dma-buf support detection.
egl-helpers: add functions for render nodes and dma-buf passing
console: track gl_block state in QemuConsole
spice: add opengl/virgl/dmabuf support
spice: tweak debug messages.
spice/gl: render DisplaySurface via opengl
spice/gl: create dummy primary surface (RfC)

Quoted 3 lines

Marc-André Lureau (1):
spice: reset cursor on resize

Please reorder so that "track gl_block state" is closer to what it is actually needed for.

After "add opengl/virgl/dmabuf support", spice+virgl starts working. I'd like to have this ready and merged before the rest of the series, so I'll focus on that.

Quoted 14 lines
 configure                  |  20 +++-
 include/ui/console.h       |   1 +
 include/ui/egl-helpers.h   |  13 +++
 include/ui/spice-display.h |  20 ++++
 qemu-options.hx            |   4 +
 ui/console.c               |  15 ++-
 ui/egl-helpers.c           | 129 +++++++++++++++++++++++++
 ui/spice-core.c            |  18 +++-
 ui/spice-display.c         | 230 +++++++++++++++++++++++++++++++++++++++++++--
 9 files changed, 437 insertions(+), 13 deletions(-)

--
1.8.3.1

Signature · 1 line
Marc-André Lureau