Skip to contentSkip to conversation

ui/spice: Enable gl=on option for non-local or remote clients

PATCH Superseded Vivek Kasireddy 3 participants 17 messages Apr 29, 2025 – May 1, 2025

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover ui/spice: Enable gl=on option for non-local or remote clients

To address the limitation that this option is incompatible with remote clients, this patch series adds an option to select a preferred codec and also enable gl=on option for clients that are connected via the network. In other words, with this option enabled (and the below linked Spice series merged), it would be possible to have Qemu share a dmabuf fd with Spice, which would then forward it to a hardware or software based encoder and eventually send the data associated with the fd to a client that could be located on a different machine.

Essentially, this patch series provides a hardware accelerated, opensource VDI option for users using Qemu and Spice by leveraging the iGPU/dGPU on the host machine via the Gstreamer framework.

v2 -> v3:
- Check for errors after invoking glImportMemoryFdEXT() using glGetError() and report the error to user (Dmitry)

v1 -> v2:
- Replace the option name preferred-codec with video-codecs (Marc-André)
- Add a warning when an fd cannot be created from texture (Marc-André)
- Add a new patch to blit the scanout texture into a linear one to make it work with virgl
- Rebased and tested against the latest Spice master

Tested with the following Qemu parameters:
-device virtio-vga,max_outputs=1,xres=1920,yres=1080,blob=true -spice port=3001,gl=on,disable-ticketing=on,video-codecs=gstreamer:h264

and remote-viewer --spice-debug spice://x.x.x.x:3001 on the client side.

Associated Spice server MR (merged):
https://gitlab.freedesktop.org/spice/spice/-/merge_requests/229

---
Cc: Gerd Hoffmann <[email protected]>
Cc: Marc-André Lureau <[email protected]>
Cc: Dmitry Osipenko <[email protected]>
Cc: Frediano Ziglio <[email protected]>
Cc: Michael Scherle <[email protected]>
Cc: Dongwon Kim <[email protected]>
Cc: Alex Bennée <[email protected]>

Vivek Kasireddy (6):
ui/spice: Add an option for users to provide a preferred codec ui/spice: Enable gl=on option for non-local or remote clients ui/spice: Submit the gl_draw requests at 60 FPS for remote clients ui/console-gl: Add a helper to create a texture with linear memory
layout
ui/spice: Create a new texture with linear layout when gl=on is
enabled
ui/spice: Blit the scanout texture if its memory layout is not linear

 include/ui/console.h       |   2 +
 include/ui/spice-display.h |   4 +
 qemu-options.hx            |   5 +
 ui/console-gl.c            |  28 ++++++
 ui/spice-core.c            |  16 ++++
 ui/spice-display.c         | 184 +++++++++++++++++++++++++++++++++----
 6 files changed, 223 insertions(+), 16 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

PATCH v3 1/6 ui/spice: Add an option for users to provide a preferred codec

Giving users an option to choose a particular codec will enable them to make an appropriate decision based on their hardware and use-case.

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Frediano Ziglio <[email protected]>
Cc Dongwon Kim <[email protected]>
---
 qemu-options.hx |  5 +++++
 ui/spice-core.c | 12 ++++++++++++
 2 files changed, 17 insertions(+)
qemu-options.hx +5 -0
index dc694a99a3..608a07cfc1 100644
@@ -2281,6 +2281,7 @@ DEF("spice", HAS_ARG, QEMU_OPTION_spice,
     "       [,streaming-video=[off|all|filter]][,disable-copy-paste=on|off]\n"
     "       [,disable-agent-file-xfer=on|off][,agent-mouse=[on|off]]\n"
     "       [,playback-compression=[on|off]][,seamless-migration=[on|off]]\n"
+    "       [,video-codecs=<encoder>:<codec>\n"
     "       [,gl=[on|off]][,rendernode=<file>]\n"
     "                enable spice\n"
     "                at least one of {port, tls-port} is mandatory\n",
@@ -2369,6 +2370,10 @@ SRST
     ``seamless-migration=[on|off]``
         Enable/disable spice seamless migration. Default is off.
 
+    ``video-codecs=<encoder>:<codec>``
+        Provide the preferred codec the Spice server should use.
+        Default would be spice:mjpeg.
+
     ``gl=[on|off]``
         Enable/disable OpenGL context. Default is off.
 
ui/spice-core.c +12 -0
index 0326c63bec..907b0e9a81 100644
@@ -488,6 +488,9 @@ static QemuOptsList qemu_spice_opts = {
         },{
             .name = "streaming-video",
             .type = QEMU_OPT_STRING,
+        },{
+            .name = "video-codecs",
+            .type = QEMU_OPT_STRING,
         },{
             .name = "agent-mouse",
             .type = QEMU_OPT_BOOL,
@@ -662,6 +665,7 @@ static void qemu_spice_init(void)
     char *x509_key_file = NULL,
         *x509_cert_file = NULL,
         *x509_cacert_file = NULL;
+    const char *video_codecs = NULL;
     int port, tls_port, addr_flags;
     spice_image_compression_t compression;
     spice_wan_compression_t wan_compr;
@@ -801,6 +805,14 @@ static void qemu_spice_init(void)
         spice_server_set_streaming_video(spice_server, SPICE_STREAM_VIDEO_OFF);
     }
 
+    video_codecs = qemu_opt_get(opts, "video-codecs");
+    if (video_codecs) {
+        if (spice_server_set_video_codecs(spice_server, video_codecs)) {
+            error_report("invalid video codecs");
+            exit(1);
+        }
+    }
+
     spice_server_set_agent_mouse
         (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1));
     spice_server_set_playback_compression
Signature · 1 line
2.49.0
#2 lore raw

PATCH v3 2/6 ui/spice: Enable gl=on option for non-local or remote clients

In reply to Vivek Kasireddy

Newer versions of Spice server should be able to accept dmabuf fds from Qemu for clients that are connected via the network. In other words, when this option is enabled, Qemu would share a dmabuf fd with Spice which would encode and send the data associated with the fd to a client that could be located on a different machine.

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Frediano Ziglio <[email protected]>
Cc Dongwon Kim <[email protected]>
---
 include/ui/spice-display.h | 1 +
 ui/spice-core.c            | 4 ++++
 ui/spice-display.c         | 1 +
 3 files changed, 6 insertions(+)
include/ui/spice-display.h +1 -0
index e1a9b36185..f4922dd74b 100644
@@ -151,6 +151,7 @@ struct SimpleSpiceCursor {
 };
 
 extern bool spice_opengl;
+extern bool remote_client;
 
 int qemu_spice_rect_is_empty(const QXLRect* r);
 void qemu_spice_rect_union(QXLRect *dest, const QXLRect *r);
ui/spice-core.c +4 -0
index 907b0e9a81..6c3bfe1d0f 100644
@@ -848,9 +848,13 @@ static void qemu_spice_init(void)
 #ifdef HAVE_SPICE_GL
     if (qemu_opt_get_bool(opts, "gl", 0)) {
         if ((port != 0) || (tls_port != 0)) {
+#if SPICE_SERVER_VERSION >= 0x000f03 /* release 0.15.3 */
+            remote_client = 1;
+#else
             error_report("SPICE GL support is local-only for now and "
                          "incompatible with -spice port/tls-port");
             exit(1);
+#endif
         }
         egl_init(qemu_opt_get(opts, "rendernode"), DISPLAY_GL_MODE_ON, &error_fatal);
         spice_opengl = 1;
ui/spice-display.c +1 -0
index c794ae0649..bf4caf0d1b 100644
@@ -29,6 +29,7 @@
 #include "ui/spice-display.h"
 
 bool spice_opengl;
+bool remote_client;
 
 int qemu_spice_rect_is_empty(const QXLRect* r)
 {
Signature · 1 line
2.49.0
#3 lore raw

PATCH v3 3/6 ui/spice: Submit the gl_draw requests at 60 FPS for remote clients

In reply to Vivek Kasireddy

In the specific case where the display layer (virtio-gpu) is using dmabuf, and if remote clients are enabled (-spice gl=on,port=xxxx), it makes sense to limit the maximum (streaming) rate to 60 FPS using the GUI timer. This matches the behavior of GTK UI where the display updates are submitted at 60 FPS (assuming the underlying mode is WxY@60).

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Frediano Ziglio <[email protected]>
Cc Dongwon Kim <[email protected]>
---
 ui/spice-display.c | 53 +++++++++++++++++++++++++++++++++++++---------
 1 file changed, 43 insertions(+), 10 deletions(-)
ui/spice-display.c +43 -10
index bf4caf0d1b..2c4daa0707 100644
@@ -842,12 +842,32 @@ static void qemu_spice_gl_block_timer(void *opaque)
     warn_report("spice: no gl-draw-done within one second");
 }
 
+static void spice_gl_draw(SimpleSpiceDisplay *ssd,
+                           uint32_t x, uint32_t y, uint32_t w, uint32_t h)
+{
+    uint64_t cookie;
+
+    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 void spice_gl_refresh(DisplayChangeListener *dcl)
 {
     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
-    uint64_t cookie;
 
-    if (!ssd->ds || qemu_console_is_gl_blocked(ssd->dcl.con)) {
+    if (!ssd->ds) {
+        return;
+    }
+
+    if (qemu_console_is_gl_blocked(ssd->dcl.con)) {
+        if (remote_client && ssd->gl_updates && ssd->have_scanout) {
+            glFlush();
+            spice_gl_draw(ssd, 0, 0,
+                          surface_width(ssd->ds), surface_height(ssd->ds));
+            ssd->gl_updates = 0;
+            /* To stream at 60 FPS, the (GUI) timer delay needs to be ~17 ms */
+            dcl->update_interval = 1000 / (2 * GUI_REFRESH_INTERVAL_DEFAULT) + 1;
+        }
         return;
     }
 
@@ -855,11 +875,8 @@ static void spice_gl_refresh(DisplayChangeListener *dcl)
     if (ssd->gl_updates && ssd->have_surface) {
         qemu_spice_gl_block(ssd, true);
         glFlush();
-        cookie = (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0);
-        spice_qxl_gl_draw_async(&ssd->qxl, 0, 0,
-                                surface_width(ssd->ds),
-                                surface_height(ssd->ds),
-                                cookie);
+        spice_gl_draw(ssd, 0, 0,
+                      surface_width(ssd->ds), surface_height(ssd->ds));
         ssd->gl_updates = 0;
     }
 }
@@ -926,6 +943,20 @@ static void qemu_spice_gl_scanout_disable(DisplayChangeListener *dcl)
     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
 
     trace_qemu_spice_gl_scanout_disable(ssd->qxl.id);
+
+    /*
+     * We need to check for the case of "lost" updates, where a gl_draw
+     * was not submitted because the timer did not get a chance to run.
+     * One case where this happens is when the Guest VM is getting
+     * rebooted. If the console is blocked in this situation, we need
+     * to unblock it. Otherwise, newer updates would not take effect.
+     */
+    if (qemu_console_is_gl_blocked(ssd->dcl.con)) {
+        if (remote_client && ssd->gl_updates && ssd->have_scanout) {
+            ssd->gl_updates = 0;
+            qemu_spice_gl_block(ssd, false);
+        }
+    }
     spice_qxl_gl_scanout(&ssd->qxl, -1, 0, 0, 0, 0, false);
     qemu_spice_gl_monitor_config(ssd, 0, 0, 0, 0);
     ssd->have_surface = false;
@@ -1029,7 +1060,6 @@ static void qemu_spice_gl_update(DisplayChangeListener *dcl,
     EGLint stride = 0, fourcc = 0;
     bool render_cursor = false;
     bool y_0_top = false; /* FIXME */
-    uint64_t cookie;
     int fd;
     uint32_t width, height, texture;
 
@@ -1107,8 +1137,11 @@ static void qemu_spice_gl_update(DisplayChangeListener *dcl,
     trace_qemu_spice_gl_update(ssd->qxl.id, w, h, x, y);
     qemu_spice_gl_block(ssd, true);
     glFlush();
-    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);
+    if (remote_client) {
+        ssd->gl_updates++;
+    } else {
+        spice_gl_draw(ssd, x, y, w, h);
+    }
 }
 
 static const DisplayChangeListenerOps display_listener_gl_ops = {
Signature · 1 line
2.49.0
#4 lore raw

Re: [PATCH v3 3/6] ui/spice: Submit the gl_draw requests at 60 FPS for remote clients

Hi Vivek

Quoted 7 lines · Vivek Kasireddy

In the specific case where the display layer (virtio-gpu) is using dmabuf, and if remote clients are enabled (-spice gl=on,port=xxxx), it makes sense to limit the maximum (streaming) rate to 60 FPS using the GUI timer. This matches the behavior of GTK UI where the display updates are submitted at 60 FPS (assuming the underlying mode is WxY@60).

I guess it would make sense to make it configurable, for any UI/remote protocol.

For some UI, refresh rate is set via dpy_set_ui_info(). Unfortunately, none of the vnc, spice or dbus protocols provide the refresh rate.

I wonder if it would make sense to set it on the GPU.. Perhaps a "max-refresh-rate" device property?

Quoted 36 lines
Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Frediano Ziglio <[email protected]>
Cc Dongwon Kim <[email protected]>
---
 ui/spice-display.c | 53 +++++++++++++++++++++++++++++++++++++---------
 1 file changed, 43 insertions(+), 10 deletions(-)
ui/spice-display.c +11 -2
index bf4caf0d1b..2c4daa0707 100644
@@ -842,12 +842,32 @@ static void qemu_spice_gl_block_timer(void *opaque)
     warn_report("spice: no gl-draw-done within one second");
 }
 
+static void spice_gl_draw(SimpleSpiceDisplay *ssd,
+                           uint32_t x, uint32_t y, uint32_t w, uint32_t h)
+{
+    uint64_t cookie;
+
+    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 void spice_gl_refresh(DisplayChangeListener *dcl)
 {
     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
-    uint64_t cookie;
 
-    if (!ssd->ds || qemu_console_is_gl_blocked(ssd->dcl.con)) {
+    if (!ssd->ds) {
+        return;

ui/spice-display.c static void qemu_spice_gl_block_timer(void *opaque) · line 869

Quoted 10 lines
ui/spice-display.c +10 -0
+    }
+
+    if (qemu_console_is_gl_blocked(ssd->dcl.con)) {
+        if (remote_client && ssd->gl_updates && ssd->have_scanout) {
+            glFlush();
+            spice_gl_draw(ssd, 0, 0,
+                          surface_width(ssd->ds), surface_height(ssd->ds));
+            ssd->gl_updates = 0;
+            /* To stream at 60 FPS, the (GUI) timer delay needs to be ~17 ms */
+            dcl->update_interval = 1000 / (2 * GUI_REFRESH_INTERVAL_DEFAULT) + 1;

That expression doesn't make much sense to me.

"update_interval" is in ms. GUI_REFRESH_INTERVAL_DEFAULT is 30ms. (iow, it's not 30fps)

If you need 60fps, just add a new constant/macro value instead?

Quoted 65 lines
ui/spice-display.c +22 -9
+        }
         return;
     }
 
@@ -855,11 +875,8 @@ static void spice_gl_refresh(DisplayChangeListener *dcl)
     if (ssd->gl_updates && ssd->have_surface) {
         qemu_spice_gl_block(ssd, true);
         glFlush();
-        cookie = (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0);
-        spice_qxl_gl_draw_async(&ssd->qxl, 0, 0,
-                                surface_width(ssd->ds),
-                                surface_height(ssd->ds),
-                                cookie);
+        spice_gl_draw(ssd, 0, 0,
+                      surface_width(ssd->ds), surface_height(ssd->ds));
         ssd->gl_updates = 0;
     }
 }
@@ -926,6 +943,20 @@ static void qemu_spice_gl_scanout_disable(DisplayChangeListener *dcl)
     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
 
     trace_qemu_spice_gl_scanout_disable(ssd->qxl.id);
+
+    /*
+     * We need to check for the case of "lost" updates, where a gl_draw
+     * was not submitted because the timer did not get a chance to run.
+     * One case where this happens is when the Guest VM is getting
+     * rebooted. If the console is blocked in this situation, we need
+     * to unblock it. Otherwise, newer updates would not take effect.
+     */
+    if (qemu_console_is_gl_blocked(ssd->dcl.con)) {
+        if (remote_client && ssd->gl_updates && ssd->have_scanout) {
+            ssd->gl_updates = 0;
+            qemu_spice_gl_block(ssd, false);
+        }
+    }
     spice_qxl_gl_scanout(&ssd->qxl, -1, 0, 0, 0, 0, false);
     qemu_spice_gl_monitor_config(ssd, 0, 0, 0, 0);
     ssd->have_surface = false;
@@ -1029,7 +1060,6 @@ static void qemu_spice_gl_update(DisplayChangeListener *dcl,
     EGLint stride = 0, fourcc = 0;
     bool render_cursor = false;
     bool y_0_top = false; /* FIXME */
-    uint64_t cookie;
     int fd;
     uint32_t width, height, texture;
 
@@ -1107,8 +1137,11 @@ static void qemu_spice_gl_update(DisplayChangeListener *dcl,
     trace_qemu_spice_gl_update(ssd->qxl.id, w, h, x, y);
     qemu_spice_gl_block(ssd, true);
     glFlush();
-    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);
+    if (remote_client) {
+        ssd->gl_updates++;
+    } else {
+        spice_gl_draw(ssd, x, y, w, h);
+    }
 }
 
 static const DisplayChangeListenerOps display_listener_gl_ops = {
--

2.49.0

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

RE: [PATCH v3 3/6] ui/spice: Submit the gl_draw requests at 60 FPS for remote clients

Hi Marc-Andre,

Quoted 15 lines

Subject: Re: [PATCH v3 3/6] ui/spice: Submit the gl_draw requests at 60 FPS for remote clients

Hi Vivek

Quoted 7 lines · Vivek Kasireddy

In the specific case where the display layer (virtio-gpu) is using dmabuf, and if remote clients are enabled (-spice gl=on,port=xxxx), it makes sense to limit the maximum (streaming) rate to 60 FPS using the GUI timer. This matches the behavior of GTK UI where the display updates are submitted at 60 FPS (assuming the underlying mode is WxY@60).

Quoted 2 lines

I guess it would make sense to make it configurable, for any UI/remote protocol.

Sure, I'll add a new parameter for this.

Quoted 3 lines

For some UI, refresh rate is set via dpy_set_ui_info(). Unfortunately, none of the vnc, spice or dbus protocols provide the refresh rate.

AFAIU, dpy_set_ui_info() sets the refresh rate for the Guest updates but what I am trying to do in this patch is increase the submission/update rate (to Host) from 30 FPS to 60 FPS. This is only needed for specific use-cases where the update submission to Host (encode request) is done from the refresh timer callback.

Quoted 3 lines

I wonder if it would make sense to set it on the GPU.. Perhaps a "max-refresh-rate" device property?

Not sure if a generic one is needed as not all UIs do Host submission (encode or draw) from the refresh timer callback.

Quoted 49 lines

ui/spice-display.c static void qemu_spice_gl_block_timer(void

Quoted 17 lines
Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Frediano Ziglio <[email protected]>
Cc Dongwon Kim <[email protected]>
---
 ui/spice-display.c | 53 +++++++++++++++++++++++++++++++++++++---------
 1 file changed, 43 insertions(+), 10 deletions(-)
ui/spice-display.c +0 -0
index bf4caf0d1b..2c4daa0707 100644
@@ -842,12 +842,32 @@ static void qemu_spice_gl_block_timer(void

*opaque)

ui/spice-display.c static void qemu_spice_gl_block_timer(void *opaque)

Quoted 9 lines
ui/spice-display.c +6 -0
     warn_report("spice: no gl-draw-done within one second");
 }
 
+static void spice_gl_draw(SimpleSpiceDisplay *ssd,
+                           uint32_t x, uint32_t y, uint32_t w, uint32_t h)
+{
+    uint64_t cookie;
+
+    cookie =

(uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0);

Quoted 10 lines
ui/spice-display.c +4 -2
+    spice_qxl_gl_draw_async(&ssd->qxl, x, y, w, h, cookie);
+}
+
 static void spice_gl_refresh(DisplayChangeListener *dcl)
 {
     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
-    uint64_t cookie;
 
-    if (!ssd->ds || qemu_console_is_gl_blocked(ssd->dcl.con)) {
+    if (!ssd->ds) {

ui/spice-display.c static void qemu_spice_gl_block_timer(void *opaque)

Quoted 10 lines
ui/spice-display.c +10 -0
+        return;
+    }
+
+    if (qemu_console_is_gl_blocked(ssd->dcl.con)) {
+        if (remote_client && ssd->gl_updates && ssd->have_scanout) {
+            glFlush();
+            spice_gl_draw(ssd, 0, 0,
+                          surface_width(ssd->ds), surface_height(ssd->ds));
+            ssd->gl_updates = 0;
+            /* To stream at 60 FPS, the (GUI) timer delay needs to be ~17 ms

*/

Quoted 7 lines

ui/spice-display.c static void qemu_spice_gl_block_timer(void · line 869

Quoted 1 line
ui/spice-display.c +1 -0
+            dcl->update_interval = 1000 / (2 *

GUI_REFRESH_INTERVAL_DEFAULT) + 1;

That expression doesn't make much sense to me.

"update_interval" is in ms. GUI_REFRESH_INTERVAL_DEFAULT is 30ms. (iow, it's not 30fps)

IIUC, update_interval needs to be ~17ms (16.66 to be more accurate) for the refresh timer callback to get invoked 60 times per sec.

Quoted 2 lines

If you need 60fps, just add a new constant/macro value instead?

Yeah, I'll do that as it would make it more clear.

Thanks,
Vivek

Quoted 76 lines

ui/spice-display.c static void

Quoted 6 lines
ui/spice-display.c +1 -0
+        }
         return;
     }
 
@@ -855,11 +875,8 @@ static void

spice_gl_refresh(DisplayChangeListener *dcl)

ui/spice-display.c static void spice_gl_refresh(DisplayChangeListener *dcl)

Quoted 4 lines
ui/spice-display.c +0 -1
     if (ssd->gl_updates && ssd->have_surface) {
         qemu_spice_gl_block(ssd, true);
         glFlush();
-        cookie =

(uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0);

ui/spice-display.c static void

Quoted 10 lines
ui/spice-display.c +2 -4
-        spice_qxl_gl_draw_async(&ssd->qxl, 0, 0,
-                                surface_width(ssd->ds),
-                                surface_height(ssd->ds),
-                                cookie);
+        spice_gl_draw(ssd, 0, 0,
+                      surface_width(ssd->ds), surface_height(ssd->ds));
         ssd->gl_updates = 0;
     }
 }
@@ -926,6 +943,20 @@ static void

qemu_spice_gl_scanout_disable(DisplayChangeListener *dcl)

ui/spice-display.c static void

Quoted 21 lines
ui/spice-display.c +14 -0
     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
 
     trace_qemu_spice_gl_scanout_disable(ssd->qxl.id);
+
+    /*
+     * We need to check for the case of "lost" updates, where a gl_draw
+     * was not submitted because the timer did not get a chance to run.
+     * One case where this happens is when the Guest VM is getting
+     * rebooted. If the console is blocked in this situation, we need
+     * to unblock it. Otherwise, newer updates would not take effect.
+     */
+    if (qemu_console_is_gl_blocked(ssd->dcl.con)) {
+        if (remote_client && ssd->gl_updates && ssd->have_scanout) {
+            ssd->gl_updates = 0;
+            qemu_spice_gl_block(ssd, false);
+        }
+    }
     spice_qxl_gl_scanout(&ssd->qxl, -1, 0, 0, 0, 0, false);
     qemu_spice_gl_monitor_config(ssd, 0, 0, 0, 0);
     ssd->have_surface = false;
@@ -1029,7 +1060,6 @@ static void

qemu_spice_gl_update(DisplayChangeListener *dcl,

ui/spice-display.c static void

Quoted 8 lines
ui/spice-display.c +0 -1
     EGLint stride = 0, fourcc = 0;
     bool render_cursor = false;
     bool y_0_top = false; /* FIXME */
-    uint64_t cookie;
     int fd;
     uint32_t width, height, texture;
 
@@ -1107,8 +1137,11 @@ static void

qemu_spice_gl_update(DisplayChangeListener *dcl,

ui/spice-display.c static void qemu_spice_gl_update(DisplayChangeListener *dcl,

Quoted 4 lines
ui/spice-display.c +0 -1
     trace_qemu_spice_gl_update(ssd->qxl.id, w, h, x, y);
     qemu_spice_gl_block(ssd, true);
     glFlush();
-    cookie =

(uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0);

Quoted 10 lines
ui/spice-display.c +5 -2
-    spice_qxl_gl_draw_async(&ssd->qxl, x, y, w, h, cookie);
+    if (remote_client) {
+        ssd->gl_updates++;
+    } else {
+        spice_gl_draw(ssd, x, y, w, h);
+    }
 }
 
 static const DisplayChangeListenerOps display_listener_gl_ops = {
--
Quoted 3 lines

2.49.0

--
Marc-André Lureau

#6 lore raw

PATCH v3 4/6 ui/console-gl: Add a helper to create a texture with linear memory layout

In reply to Vivek Kasireddy

There are cases where we do not want the memory layout of a texture to be tiled as the component processing the texture would not know how to de-tile either via software or hardware. Therefore, ensuring that the memory backing the texture has a linear layout is absolutely necessary in these situations.

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Frediano Ziglio <[email protected]>
Cc Dongwon Kim <[email protected]>
---
 include/ui/console.h |  2 ++
 ui/console-gl.c      | 28 ++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
include/ui/console.h +2 -0
index 46b3128185..fa2dd53e2e 100644
@@ -422,6 +422,8 @@ bool console_gl_check_format(DisplayChangeListener *dcl,
                              pixman_format_code_t format);
 void surface_gl_create_texture(QemuGLShader *gls,
                                DisplaySurface *surface);
+void surface_gl_create_texture_from_fd(DisplaySurface *surface,
+                                       int fd, GLuint *texture);
 void surface_gl_update_texture(QemuGLShader *gls,
                                DisplaySurface *surface,
                                int x, int y, int w, int h);
ui/console-gl.c +28 -0
index 103b954017..f83012fed4 100644
@@ -25,6 +25,7 @@
  * THE SOFTWARE.
  */
 #include "qemu/osdep.h"
+#include "qemu/error-report.h"
 #include "ui/console.h"
 #include "ui/shader.h"
 
@@ -96,6 +97,33 @@ void surface_gl_create_texture(QemuGLShader *gls,
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 }
 
+void surface_gl_create_texture_from_fd(DisplaySurface *surface,
+                                       int fd, GLuint *texture)
+{
+    unsigned long size = surface_stride(surface) * surface_height(surface);
+    GLuint mem_obj;
+
+    if (!epoxy_has_gl_extension("GL_EXT_memory_object") ||
+        !epoxy_has_gl_extension("GL_EXT_memory_object_fd")) {
+        return;
+    }
+
+#ifdef GL_EXT_memory_object_fd
+    glCreateMemoryObjectsEXT(1, &mem_obj);
+    glImportMemoryFdEXT(mem_obj, size, GL_HANDLE_TYPE_OPAQUE_FD_EXT, fd);
+    if (glGetError() != GL_NO_ERROR) {
+        error_report("spice: cannot import memory object from fd");
+        return;
+    }
+
+    glGenTextures(1, texture);
+    glBindTexture(GL_TEXTURE_2D, *texture);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_TILING_EXT, GL_LINEAR_TILING_EXT);
+    glTexStorageMem2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8, surface_width(surface),
+                         surface_height(surface), mem_obj, 0);
+#endif
+}
+
 void surface_gl_update_texture(QemuGLShader *gls,
                                DisplaySurface *surface,
                                int x, int y, int w, int h)
Signature · 1 line
2.49.0
#7 lore raw

Re: [PATCH v3 4/6] ui/console-gl: Add a helper to create a texture with linear memory layout

Hi

Quoted 62 lines · Vivek Kasireddy

There are cases where we do not want the memory layout of a texture to be tiled as the component processing the texture would not know how to de-tile either via software or hardware. Therefore, ensuring that the memory backing the texture has a linear layout is absolutely necessary in these situations.

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Frediano Ziglio <[email protected]>
Cc Dongwon Kim <[email protected]>
---
 include/ui/console.h |  2 ++
 ui/console-gl.c      | 28 ++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
include/ui/console.h +2 -0
index 46b3128185..fa2dd53e2e 100644
@@ -422,6 +422,8 @@ bool console_gl_check_format(DisplayChangeListener *dcl,
                              pixman_format_code_t format);
 void surface_gl_create_texture(QemuGLShader *gls,
                                DisplaySurface *surface);
+void surface_gl_create_texture_from_fd(DisplaySurface *surface,
+                                       int fd, GLuint *texture);
 void surface_gl_update_texture(QemuGLShader *gls,
                                DisplaySurface *surface,
                                int x, int y, int w, int h);
ui/console-gl.c +16 -0
index 103b954017..f83012fed4 100644
@@ -25,6 +25,7 @@
  * THE SOFTWARE.
  */
 #include "qemu/osdep.h"
+#include "qemu/error-report.h"
 #include "ui/console.h"
 #include "ui/shader.h"
 
@@ -96,6 +97,33 @@ void surface_gl_create_texture(QemuGLShader *gls,
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 }
 
+void surface_gl_create_texture_from_fd(DisplaySurface *surface,
+                                       int fd, GLuint *texture)
+{
+    unsigned long size = surface_stride(surface) * surface_height(surface);
+    GLuint mem_obj;
+
+    if (!epoxy_has_gl_extension("GL_EXT_memory_object") ||
+        !epoxy_has_gl_extension("GL_EXT_memory_object_fd")) {
+        return;
+    }
+
+#ifdef GL_EXT_memory_object_fd
+    glCreateMemoryObjectsEXT(1, &mem_obj);
+    glImportMemoryFdEXT(mem_obj, size, GL_HANDLE_TYPE_OPAQUE_FD_EXT, fd);
+    if (glGetError() != GL_NO_ERROR) {

ui/console-gl.c void surface_gl_create_texture(QemuGLShader *gls, · line 124

Quoted 10 lines
ui/console-gl.c +10 -0
+        error_report("spice: cannot import memory object from fd");
+        return;
+    }
+
+    glGenTextures(1, texture);
+    glBindTexture(GL_TEXTURE_2D, *texture);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_TILING_EXT, GL_LINEAR_TILING_EXT);
+    glTexStorageMem2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8, surface_width(surface),
+                         surface_height(surface), mem_obj, 0);
+#endif

I suggest making the function return a "bool" for success. return *texture > 0 && glGetError() == GL_NO_ERROR for example.

Quoted 9 lines
ui/console-gl.c +2 -1
+}
+
 void surface_gl_update_texture(QemuGLShader *gls,
                                DisplaySurface *surface,
                                int x, int y, int w, int h)
--

2.49.0

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

RE: [PATCH v3 4/6] ui/console-gl: Add a helper to create a texture with linear memory layout

Hi Marc-Andre,

Quoted 75 lines

include/ui/console.h bool

Quoted 23 lines · Vivek Kasireddy

There are cases where we do not want the memory layout of a texture to be tiled as the component processing the texture would not know how to de-tile either via software or hardware. Therefore, ensuring that the memory backing the texture has a linear layout is absolutely necessary in these situations.

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Frediano Ziglio <[email protected]>
Cc Dongwon Kim <[email protected]>
---
 include/ui/console.h |  2 ++
 ui/console-gl.c      | 28 ++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
include/ui/console.h +0 -0
index 46b3128185..fa2dd53e2e 100644
@@ -422,6 +422,8 @@ bool

console_gl_check_format(DisplayChangeListener *dcl,

Quoted 21 lines
include/ui/console.h +2 -0
                              pixman_format_code_t format);
 void surface_gl_create_texture(QemuGLShader *gls,
                                DisplaySurface *surface);
+void surface_gl_create_texture_from_fd(DisplaySurface *surface,
+                                       int fd, GLuint *texture);
 void surface_gl_update_texture(QemuGLShader *gls,
                                DisplaySurface *surface,
                                int x, int y, int w, int h);
ui/console-gl.c +1 -0
index 103b954017..f83012fed4 100644
@@ -25,6 +25,7 @@
  * THE SOFTWARE.
  */
 #include "qemu/osdep.h"
+#include "qemu/error-report.h"
 #include "ui/console.h"
 #include "ui/shader.h"
 

ui/console-gl.c void surface_gl_create_texture(QemuGLShader *gls, · line 97

Quoted 1 line
ui/console-gl.c +0 -0
@@ -96,6 +97,33 @@ void surface_gl_create_texture(QemuGLShader *gls,
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,

GL_LINEAR);

Quoted 6 lines
ui/console-gl.c +4 -0
 }
 
+void surface_gl_create_texture_from_fd(DisplaySurface *surface,
+                                       int fd, GLuint *texture)
+{
+    unsigned long size = surface_stride(surface) * surface_height(surface);

ui/console-gl.c void surface_gl_create_texture(QemuGLShader *gls,

Quoted 10 lines
ui/console-gl.c +10 -0
+    GLuint mem_obj;
+
+    if (!epoxy_has_gl_extension("GL_EXT_memory_object") ||
+        !epoxy_has_gl_extension("GL_EXT_memory_object_fd")) {
+        return;
+    }
+
+#ifdef GL_EXT_memory_object_fd
+    glCreateMemoryObjectsEXT(1, &mem_obj);
+    glImportMemoryFdEXT(mem_obj, size,

GL_HANDLE_TYPE_OPAQUE_FD_EXT, fd);

ui/console-gl.c void surface_gl_create_texture(QemuGLShader *gls,

Quoted 8 lines
ui/console-gl.c +8 -0
+    if (glGetError() != GL_NO_ERROR) {
+        error_report("spice: cannot import memory object from fd");
+        return;
+    }
+
+    glGenTextures(1, texture);
+    glBindTexture(GL_TEXTURE_2D, *texture);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_TILING_EXT,
Quoted 8 lines

GL_LINEAR_TILING_EXT);

ui/console-gl.c void surface_gl_create_texture(QemuGLShader *gls, · line 122

Quoted 1 line
ui/console-gl.c +1 -0
+    glTexStorageMem2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8,

surface_width(surface),

ui/console-gl.c void surface_gl_create_texture(QemuGLShader *gls, · line 124

Quoted 2 lines
ui/console-gl.c +2 -0
+                         surface_height(surface), mem_obj, 0);
+#endif

I suggest making the function return a "bool" for success. return *texture > 0 && glGetError() == GL_NO_ERROR for example.

Sure, will do that in the next version.

Thanks,
Vivek

Quoted 14 lines
Quoted 10 lines
ui/console-gl.c +2 -1
+}
+
 void surface_gl_update_texture(QemuGLShader *gls,
                                DisplaySurface *surface,
                                int x, int y, int w, int h)
--

2.49.0

--
Marc-André Lureau

#9 lore raw

Re: [PATCH v3 4/6] ui/console-gl: Add a helper to create a texture with linear memory layout

In reply to Vivek Kasireddy

ui/console-gl.c void surface_gl_create_texture(QemuGLShader *gls, · line 117

Quoted 7 lines · Vivek Kasireddy
ui/console-gl.c +7 -0
+#ifdef GL_EXT_memory_object_fd
+    glCreateMemoryObjectsEXT(1, &mem_obj);
+    glImportMemoryFdEXT(mem_obj, size, GL_HANDLE_TYPE_OPAQUE_FD_EXT, fd);
+    if (glGetError() != GL_NO_ERROR) {
+        error_report("spice: cannot import memory object from fd");
+        return;
+    }

glGetError() retains error once error occurred. Calling glGetError() returns last error code and clears the error state. Hence you should add a dummy glGetError() call before glCreateMemoryObjectsEXT() to clear the potential stale GL errors, otherwise your code will handle unrelated GL errors and report them as memfd import failures.

Signature · 2 lines
Best regards,
Dmitry
#10 lore raw

RE: [PATCH v3 4/6] ui/console-gl: Add a helper to create a texture with linear memory layout

Hi Dmitry,

Quoted 13 lines

Subject: Re: [PATCH v3 4/6] ui/console-gl: Add a helper to create a texture with linear memory layout

ui/console-gl.c void surface_gl_create_texture(QemuGLShader *gls,

Quoted 3 lines · Vivek Kasireddy
ui/console-gl.c +3 -0
+#ifdef GL_EXT_memory_object_fd
+    glCreateMemoryObjectsEXT(1, &mem_obj);
+    glImportMemoryFdEXT(mem_obj, size,

GL_HANDLE_TYPE_OPAQUE_FD_EXT, fd);

ui/console-gl.c void surface_gl_create_texture(QemuGLShader *gls, · line 117

Quoted 4 lines
ui/console-gl.c +4 -0
+    if (glGetError() != GL_NO_ERROR) {
+        error_report("spice: cannot import memory object from fd");
+        return;
+    }
Quoted 5 lines

glGetError() retains error once error occurred. Calling glGetError() returns last error code and clears the error state. Hence you should add a dummy glGetError() call before glCreateMemoryObjectsEXT() to clear the potential stale GL errors, otherwise your code will handle unrelated GL errors and report them as memfd import failures.

Thank you for explaining how glGetError() works. I'll fix this in the next version.

Thanks,
Vivek

Quoted 4 lines

--
Best regards,
Dmitry

#11 lore raw

PATCH v3 5/6 ui/spice: Create a new texture with linear layout when gl=on is enabled

In reply to Vivek Kasireddy

Since most encoders/decoders (invoked by Spice) may not work with tiled memory associated with a texture, we need to create another texture that has linear memory layout and use that instead.

Note that, there does not seem to be a direct way to indicate to the GL implementation that a texture's backing memory needs to be linear. Instead, we have to do it in a roundabout way where we need to first create a tiled texture and import that as a memory object to create a new texture that has a linear memory layout.

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Frediano Ziglio <[email protected]>
Cc Dongwon Kim <[email protected]>
---
 ui/spice-display.c | 63 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 62 insertions(+), 1 deletion(-)
ui/spice-display.c +62 -1
index 2c4daa0707..047d453a0b 100644
@@ -26,6 +26,7 @@
 #include "ui/console.h"
 #include "trace.h"
 
+#include "standard-headers/drm/drm_fourcc.h"
 #include "ui/spice-display.h"
 
 bool spice_opengl;
@@ -890,11 +891,65 @@ static void spice_gl_update(DisplayChangeListener *dcl,
     ssd->gl_updates++;
 }
 
+static bool spice_gl_replace_fd_texture(SimpleSpiceDisplay *ssd,
+                                        EGLint *stride, EGLint *fourcc,
+                                        EGLuint64KHR *modifier,
+                                        int *fd)
+{
+    GLuint texture = 0;
+
+    if (!remote_client) {
+        return true;
+    }
+
+    if (surface_format(ssd->ds) == PIXMAN_r5g6b5) {
+        return true;
+    }
+
+    if (*modifier == DRM_FORMAT_MOD_LINEAR) {
+        return true;
+    }
+
+    /*
+     * We really want to ensure that the memory layout of the texture
+     * is linear; otherwise, the encoder's output may show corruption.
+     */
+    surface_gl_create_texture_from_fd(ssd->ds, *fd, &texture);
+
+    /*
+     * A successful return after glImportMemoryFdEXT() means that
+     * the ownership of fd has been passed to GL. In other words,
+     * the fd we got above should not be used anymore.
+     */
+    if (texture > 0) {
+        *fd = egl_get_fd_for_texture(texture,
+                                     stride, fourcc,
+                                     NULL);
+        if (*fd < 0) {
+            glDeleteTextures(1, &texture);
+            *fd = egl_get_fd_for_texture(ssd->ds->texture,
+                                         stride, fourcc,
+                                         NULL);
+            if (*fd < 0) {
+                surface_gl_destroy_texture(ssd->gls, ssd->ds);
+                warn_report("spice: no texture available to display");
+                return false;
+            }
+        } else {
+            surface_gl_destroy_texture(ssd->gls, ssd->ds);
+            ssd->ds->texture = texture;
+        }
+    }
+    return true;
+}
+
 static void spice_gl_switch(DisplayChangeListener *dcl,
                             struct DisplaySurface *new_surface)
 {
     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
     EGLint stride, fourcc;
+    EGLuint64KHR modifier;
+    bool ret;
     int fd;
 
     if (ssd->ds) {
@@ -905,12 +960,18 @@ static void spice_gl_switch(DisplayChangeListener *dcl,
         surface_gl_create_texture(ssd->gls, ssd->ds);
         fd = egl_get_fd_for_texture(ssd->ds->texture,
                                     &stride, &fourcc,
-                                    NULL);
+                                    &modifier);
         if (fd < 0) {
             surface_gl_destroy_texture(ssd->gls, ssd->ds);
             return;
         }
 
+        ret = spice_gl_replace_fd_texture(ssd, &stride, &fourcc, &modifier, &fd);
+        if (!ret) {
+            surface_gl_destroy_texture(ssd->gls, ssd->ds);
+            return;
+        }
+
         trace_qemu_spice_gl_surface(ssd->qxl.id,
                                     surface_width(ssd->ds),
                                     surface_height(ssd->ds),
Signature · 1 line
2.49.0
#12 lore raw

Re: [PATCH v3 5/6] ui/spice: Create a new texture with linear layout when gl=on is enabled

Hi

Quoted 41 lines · Vivek Kasireddy

Since most encoders/decoders (invoked by Spice) may not work with tiled memory associated with a texture, we need to create another texture that has linear memory layout and use that instead.

Note that, there does not seem to be a direct way to indicate to the GL implementation that a texture's backing memory needs to be linear. Instead, we have to do it in a roundabout way where we need to first create a tiled texture and import that as a memory object to create a new texture that has a linear memory layout.

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Frediano Ziglio <[email protected]>
Cc Dongwon Kim <[email protected]>
---
 ui/spice-display.c | 63 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 62 insertions(+), 1 deletion(-)
ui/spice-display.c +5 -0
index 2c4daa0707..047d453a0b 100644
@@ -26,6 +26,7 @@
 #include "ui/console.h"
 #include "trace.h"
 
+#include "standard-headers/drm/drm_fourcc.h"
 #include "ui/spice-display.h"
 
 bool spice_opengl;
@@ -890,11 +891,65 @@ static void spice_gl_update(DisplayChangeListener *dcl,
     ssd->gl_updates++;
 }
 
+static bool spice_gl_replace_fd_texture(SimpleSpiceDisplay *ssd,
+                                        EGLint *stride, EGLint *fourcc,
+                                        EGLuint64KHR *modifier,
+                                        int *fd)

ui/spice-display.c static void spice_gl_update(DisplayChangeListener *dcl, · line 907

Quoted 10 lines
ui/spice-display.c +10 -0
+{
+    GLuint texture = 0;
+
+    if (!remote_client) {
+        return true;
+    }
+
+    if (surface_format(ssd->ds) == PIXMAN_r5g6b5) {
+        return true;
+    }

Please explain why this particular format is handled differently with a comment.

Quoted 25 lines
ui/spice-display.c +25 -0
+
+    if (*modifier == DRM_FORMAT_MOD_LINEAR) {
+        return true;
+    }
+
+    /*
+     * We really want to ensure that the memory layout of the texture
+     * is linear; otherwise, the encoder's output may show corruption.
+     */
+    surface_gl_create_texture_from_fd(ssd->ds, *fd, &texture);
+
+    /*
+     * A successful return after glImportMemoryFdEXT() means that
+     * the ownership of fd has been passed to GL. In other words,
+     * the fd we got above should not be used anymore.
+     */
+    if (texture > 0) {
+        *fd = egl_get_fd_for_texture(texture,
+                                     stride, fourcc,
+                                     NULL);
+        if (*fd < 0) {
+            glDeleteTextures(1, &texture);
+            *fd = egl_get_fd_for_texture(ssd->ds->texture,
+                                         stride, fourcc,
+                                         NULL);

ui/spice-display.c static void spice_gl_update(DisplayChangeListener *dcl, · line 942

Quoted 10 lines
ui/spice-display.c +10 -0
+            if (*fd < 0) {
+                surface_gl_destroy_texture(ssd->gls, ssd->ds);
+                warn_report("spice: no texture available to display");
+                return false;
+            }
+        } else {
+            surface_gl_destroy_texture(ssd->gls, ssd->ds);
+            ssd->ds->texture = texture;
+        }
+    }

If it failed, it does nothing and continues?

Quoted 34 lines
ui/spice-display.c +12 -2
+    return true;
+}
+
 static void spice_gl_switch(DisplayChangeListener *dcl,
                             struct DisplaySurface *new_surface)
 {
     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
     EGLint stride, fourcc;
+    EGLuint64KHR modifier;
+    bool ret;
     int fd;
 
     if (ssd->ds) {
@@ -905,12 +960,18 @@ static void spice_gl_switch(DisplayChangeListener *dcl,
         surface_gl_create_texture(ssd->gls, ssd->ds);
         fd = egl_get_fd_for_texture(ssd->ds->texture,
                                     &stride, &fourcc,
-                                    NULL);
+                                    &modifier);
         if (fd < 0) {
             surface_gl_destroy_texture(ssd->gls, ssd->ds);
             return;
         }
 
+        ret = spice_gl_replace_fd_texture(ssd, &stride, &fourcc, &modifier, &fd);
+        if (!ret) {
+            surface_gl_destroy_texture(ssd->gls, ssd->ds);
+            return;
+        }
+
         trace_qemu_spice_gl_surface(ssd->qxl.id,
                                     surface_width(ssd->ds),
                                     surface_height(ssd->ds),
--
Quoted 3 lines

2.49.0

--
Marc-André Lureau

#13 lore raw

RE: [PATCH v3 5/6] ui/spice: Create a new texture with linear layout when gl=on is enabled

Hi Marc-Andre,

Quoted 55 lines

Subject: Re: [PATCH v3 5/6] ui/spice: Create a new texture with linear layout when gl=on is enabled

Hi

Quoted 11 lines · Vivek Kasireddy

Since most encoders/decoders (invoked by Spice) may not work with tiled memory associated with a texture, we need to create another texture that has linear memory layout and use that instead.

Note that, there does not seem to be a direct way to indicate to the GL implementation that a texture's backing memory needs to be linear. Instead, we have to do it in a roundabout way where we need to first create a tiled texture and import that as a memory object to create a new texture that has a linear memory layout.

Quoted 8 lines
Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Frediano Ziglio <[email protected]>
Cc Dongwon Kim <[email protected]>
---
 ui/spice-display.c | 63

+++++++++++++++++++++++++++++++++++++++++++++-

ui/spice-display.c static void

Quoted 15 lines
 1 file changed, 62 insertions(+), 1 deletion(-)
ui/spice-display.c +1 -0
index 2c4daa0707..047d453a0b 100644
@@ -26,6 +26,7 @@
 #include "ui/console.h"
 #include "trace.h"
 
+#include "standard-headers/drm/drm_fourcc.h"
 #include "ui/spice-display.h"
 
 bool spice_opengl;
@@ -890,11 +891,65 @@ static void

spice_gl_update(DisplayChangeListener *dcl,

Quoted 12 lines
ui/spice-display.c +9 -0
     ssd->gl_updates++;
 }
 
+static bool spice_gl_replace_fd_texture(SimpleSpiceDisplay *ssd,
+                                        EGLint *stride, EGLint *fourcc,
+                                        EGLuint64KHR *modifier,
+                                        int *fd)
+{
+    GLuint texture = 0;
+
+    if (!remote_client) {
+        return true;
Quoted 8 lines

ui/spice-display.c static void spice_gl_update(DisplayChangeListener *dcl, · line 907

Quoted 5 lines
ui/spice-display.c +5 -0
+    }
+
+    if (surface_format(ssd->ds) == PIXMAN_r5g6b5) {
+        return true;
+    }

Please explain why this particular format is handled differently with a comment.

I guess I'll remove this check as I don't remember why I added it in the first place.

Quoted 30 lines
Quoted 30 lines
ui/spice-display.c +29 -0
+
+    if (*modifier == DRM_FORMAT_MOD_LINEAR) {
+        return true;
+    }
+
+    /*
+     * We really want to ensure that the memory layout of the texture
+     * is linear; otherwise, the encoder's output may show corruption.
+     */
+    surface_gl_create_texture_from_fd(ssd->ds, *fd, &texture);
+
+    /*
+     * A successful return after glImportMemoryFdEXT() means that
+     * the ownership of fd has been passed to GL. In other words,
+     * the fd we got above should not be used anymore.
+     */
+    if (texture > 0) {
+        *fd = egl_get_fd_for_texture(texture,
+                                     stride, fourcc,
+                                     NULL);
+        if (*fd < 0) {
+            glDeleteTextures(1, &texture);
+            *fd = egl_get_fd_for_texture(ssd->ds->texture,
+                                         stride, fourcc,
+                                         NULL);
+            if (*fd < 0) {
+                surface_gl_destroy_texture(ssd->gls, ssd->ds);
+                warn_report("spice: no texture available to display");
+                return false;
Quoted 8 lines

ui/spice-display.c static void spice_gl_update(DisplayChangeListener *dcl, · line 942

Quoted 6 lines
ui/spice-display.c +6 -0
+            }
+        } else {
+            surface_gl_destroy_texture(ssd->gls, ssd->ds);
+            ssd->ds->texture = texture;
+        }
+    }

If it failed, it does nothing and continues?

An error gets printed when a failure is encountered in different places but printing one here would also be ok.

Thanks,
Vivek

Quoted 45 lines

ui/spice-display.c static void spice_gl_switch(DisplayChangeListener

Quoted 16 lines
ui/spice-display.c +5 -0
+    return true;
+}
+
 static void spice_gl_switch(DisplayChangeListener *dcl,
                             struct DisplaySurface *new_surface)
 {
     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
     EGLint stride, fourcc;
+    EGLuint64KHR modifier;
+    bool ret;
     int fd;
 
     if (ssd->ds) {
@@ -905,12 +960,18 @@ static void spice_gl_switch(DisplayChangeListener

*dcl,

ui/spice-display.c static void spice_gl_switch(DisplayChangeListener *dcl,

Quoted 11 lines
ui/spice-display.c +2 -1
         surface_gl_create_texture(ssd->gls, ssd->ds);
         fd = egl_get_fd_for_texture(ssd->ds->texture,
                                     &stride, &fourcc,
-                                    NULL);
+                                    &modifier);
         if (fd < 0) {
             surface_gl_destroy_texture(ssd->gls, ssd->ds);
             return;
         }
 
+        ret = spice_gl_replace_fd_texture(ssd, &stride, &fourcc, &modifier,

&fd);

Quoted 9 lines
ui/spice-display.c +5 -1
+        if (!ret) {
+            surface_gl_destroy_texture(ssd->gls, ssd->ds);
+            return;
+        }
+
         trace_qemu_spice_gl_surface(ssd->qxl.id,
                                     surface_width(ssd->ds),
                                     surface_height(ssd->ds),
--
Quoted 3 lines

2.49.0

--
Marc-André Lureau

#14 lore raw

PATCH v3 6/6 ui/spice: Blit the scanout texture if its memory layout is not linear

In reply to Vivek Kasireddy

In cases where the scanout buffer is provided as a texture (e.g. Virgl) we need to check to see if it has a linear memory layout or not. If it doesn't have a linear layout, then blitting it onto the texture associated with the display surface (which already has a linear layout) seems to ensure that there is no corruption seen regardless of which encoder or decoder is used.

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Frediano Ziglio <[email protected]>
Cc Dongwon Kim <[email protected]>
---
 include/ui/spice-display.h |  3 ++
 ui/spice-display.c         | 67 +++++++++++++++++++++++++++++++++++---
 2 files changed, 65 insertions(+), 5 deletions(-)
include/ui/spice-display.h +3 -0
index f4922dd74b..d087d7198a 100644
@@ -132,6 +132,9 @@ struct SimpleSpiceDisplay {
     egl_fb guest_fb;
     egl_fb blit_fb;
     egl_fb cursor_fb;
+    bool backing_y_0_top;
+    bool blit_scanout_texture;
+    bool new_scanout_texture;
     bool have_hot;
 #endif
 };
ui/spice-display.c +62 -5

100 lines not on this page · raw message

Signature · 1 line
2.49.0
#15 lore raw
In reply to Vivek Kasireddy

Hi Vivek

Quoted 11 lines · Vivek Kasireddy

To address the limitation that this option is incompatible with remote clients, this patch series adds an option to select a preferred codec and also enable gl=on option for clients that are connected via the network. In other words, with this option enabled (and the below linked Spice series merged), it would be possible to have Qemu share a dmabuf fd with Spice, which would then forward it to a hardware or software based encoder and eventually send the data associated with the fd to a client that could be located on a different machine.

Quoted 4 lines

Essentially, this patch series provides a hardware accelerated, opensource VDI option for users using Qemu and Spice by leveraging the iGPU/dGPU on the host machine via the Gstreamer framework.

Could you explain why Spice or GStreamer can't handle a non-linear texture layout? It feels like a limitation that is not under QEMU responsibility, I am not sure it should have to do it.

Btw, the series will likely have conflicts with "ui: support multi plane texture", which I think we should merge first.
(https://patchew.org/QEMU/[email protected]/)

Quoted 51 lines

v2 -> v3:
- Check for errors after invoking glImportMemoryFdEXT() using glGetError() and report the error to user (Dmitry)

v1 -> v2:
- Replace the option name preferred-codec with video-codecs (Marc-André)
- Add a warning when an fd cannot be created from texture (Marc-André)
- Add a new patch to blit the scanout texture into a linear one to make it work with virgl
- Rebased and tested against the latest Spice master

Tested with the following Qemu parameters:

(quoted diff) +0 -2
-device virtio-vga,max_outputs=1,xres=1920,yres=1080,blob=true
-spice port=3001,gl=on,disable-ticketing=on,video-codecs=gstreamer:h264

and remote-viewer --spice-debug spice://x.x.x.x:3001 on the client side.

Associated Spice server MR (merged):
https://gitlab.freedesktop.org/spice/spice/-/merge_requests/229

---
Cc: Gerd Hoffmann <[email protected]>
Cc: Marc-André Lureau <[email protected]>
Cc: Dmitry Osipenko <[email protected]>
Cc: Frediano Ziglio <[email protected]>
Cc: Michael Scherle <[email protected]>
Cc: Dongwon Kim <[email protected]>
Cc: Alex Bennée <[email protected]>

Vivek Kasireddy (6):
ui/spice: Add an option for users to provide a preferred codec ui/spice: Enable gl=on option for non-local or remote clients ui/spice: Submit the gl_draw requests at 60 FPS for remote clients ui/console-gl: Add a helper to create a texture with linear memory
layout
ui/spice: Create a new texture with linear layout when gl=on is
enabled
ui/spice: Blit the scanout texture if its memory layout is not linear

 include/ui/console.h       |   2 +
 include/ui/spice-display.h |   4 +
 qemu-options.hx            |   5 +
 ui/console-gl.c            |  28 ++++++
 ui/spice-core.c            |  16 ++++
 ui/spice-display.c         | 184 +++++++++++++++++++++++++++++++++----
 6 files changed, 223 insertions(+), 16 deletions(-)

--
2.49.0

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

Hi Marc-Andre,

Quoted 18 lines

Subject: Re: [PATCH v3 0/6] ui/spice: Enable gl=on option for non-local or remote clients

Hi Vivek

Quoted 11 lines · Vivek Kasireddy

To address the limitation that this option is incompatible with remote clients, this patch series adds an option to select a preferred codec and also enable gl=on option for clients that are connected via the network. In other words, with this option enabled (and the below linked Spice series merged), it would be possible to have Qemu share a dmabuf fd with Spice, which would then forward it to a hardware or software based encoder and eventually send the data associated with the fd to a client that could be located on a different machine.

Quoted 8 lines
Quoted 4 lines

Essentially, this patch series provides a hardware accelerated, opensource VDI option for users using Qemu and Spice by leveraging the iGPU/dGPU on the host machine via the Gstreamer framework.

Could you explain why Spice or GStreamer can't handle a non-linear texture layout? It feels like a limitation that is not under QEMU responsibility, I am not sure it should have to do it.

AFAIU, there are several things to consider for handling non-linear/tiled layouts (aka DRM modifiers) in this use-case:
- The choice of which encoder to use (hardware vs software) is decided dynamically and is internal to Spice at this point. And, since there is no easy way to know from Qemu whether an encoder chosen by Spice would support any given tiling format, I chose to always use linear layout since it is mostly guaranteed to work with all types of encoders. However, it makes sense to add some kind of encoder/codec caps exchange between Qemu and Spice in the future.

- There does not appear to be a way to do the conversion from tiled to linear at the encoder level with Gstreamer. In other words, if Spice were to pick a software encoder such as x264enc, then it would become necessary do the conversion in Qemu (using GL) where this situation can be easily detected and handled.

- And, last time I checked, DRM modifiers support in Gstreamer was still not very mature and not all the key elements/plugins supported them. So, it may not be guaranteed to work in different use-cases.

Given all the above, I figured it makes sense to start small and stick with linear layout for now as it works in all cases and then improve by adding modifier support later to address the scenarios where they can be supported.

Quoted 4 lines

Btw, the series will likely have conflicts with "ui: support multi plane texture", which I think we should merge first.
(https://patchew.org/QEMU/[email protected]/)

Ok, for the next version, I'll rebase my patches on top of the above series.

Thanks,
Vivek

Quoted 56 lines
Quoted 48 lines

v2 -> v3:
- Check for errors after invoking glImportMemoryFdEXT() using glGetError() and report the error to user (Dmitry)

v1 -> v2:
- Replace the option name preferred-codec with video-codecs (Marc-André)
- Add a warning when an fd cannot be created from texture (Marc-André)
- Add a new patch to blit the scanout texture into a linear one to make it work with virgl
- Rebased and tested against the latest Spice master

Tested with the following Qemu parameters:

(quoted diff) +0 -2
-device virtio-vga,max_outputs=1,xres=1920,yres=1080,blob=true
-spice port=3001,gl=on,disable-ticketing=on,video-codecs=gstreamer:h264

and remote-viewer --spice-debug spice://x.x.x.x:3001 on the client side.

Associated Spice server MR (merged):
https://gitlab.freedesktop.org/spice/spice/-/merge_requests/229

---
Cc: Gerd Hoffmann <[email protected]>
Cc: Marc-André Lureau <[email protected]>
Cc: Dmitry Osipenko <[email protected]>
Cc: Frediano Ziglio <[email protected]>
Cc: Michael Scherle <[email protected]>
Cc: Dongwon Kim <[email protected]>
Cc: Alex Bennée <[email protected]>

Vivek Kasireddy (6):
ui/spice: Add an option for users to provide a preferred codec ui/spice: Enable gl=on option for non-local or remote clients ui/spice: Submit the gl_draw requests at 60 FPS for remote clients ui/console-gl: Add a helper to create a texture with linear memory
layout
ui/spice: Create a new texture with linear layout when gl=on is
enabled
ui/spice: Blit the scanout texture if its memory layout is not linear

 include/ui/console.h       |   2 +
 include/ui/spice-display.h |   4 +
 qemu-options.hx            |   5 +
 ui/console-gl.c            |  28 ++++++
 ui/spice-core.c            |  16 ++++
 ui/spice-display.c         | 184 +++++++++++++++++++++++++++++++++----
 6 files changed, 223 insertions(+), 16 deletions(-)
Quoted 4 lines

--
2.49.0

--
Marc-André Lureau