Skip to contentSkip to conversation

spice: Add an option to forward the dmabuf directly to the encoder (v2)

RFC In review Vivek Kasireddy 3 participants 10 messages Jan 23, 2023 – Feb 1, 2023

lore
Share & tools
lore
#0 lore raw

v2 cover spice: Add an option to forward the dmabuf directly to the encoder (v2)

This patch series adds options to select a preferred codec and also to forward a dmabuf directly to the encoder module that is part of the Spice server. Currently, gstreamer:h264 is the only combination tested but additional work is ongoing to test other combinations.

Tested with: -device virtio-gpu-pci,max_outputs=1,blob=true,xres=1920,yres=1080
-spice port=3001,gl=on,disable-ticketing=on,dmabuf-encode=on,
preferred-codec=gstreamer:h264

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

Associated Spice server patches (v1) can be found here:
https://lists.freedesktop.org/archives/spice-devel/2023-January/052927.html

v2:
- Used the already available gl_scanout and gl_draw_async APIs instead of adding new ones to Spice.
- Improved the commit message of the second patch

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Dongwon Kim <[email protected]>

Vivek Kasireddy (2):
spice: Add an option for users to provide a preferred codec
spice: Add an option to forward the dmabuf directly to the encoder

    (v2)
 include/ui/spice-display.h |  1 +
 qemu-options.hx            | 11 +++++-
 ui/spice-core.c            | 36 ++++++++++++++++--
 ui/spice-display.c         | 75 ++++++++++++++++++++++++++++----------
 4 files changed, 100 insertions(+), 23 deletions(-)
Signature · 1 line
2.37.2
#1 lore raw

RFC v2 1/2 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 Dongwon Kim <[email protected]>
---
 qemu-options.hx |  5 +++++
 ui/spice-core.c | 14 ++++++++++++++
 2 files changed, 19 insertions(+)
qemu-options.hx +5 -0
index 3aa3a2f5a3..aab8df0922 100644
@@ -2142,6 +2142,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"
+    "       [,preferred-codec=<encoder>:<codec>\n"
     "       [,gl=[on|off]][,rendernode=<file>]\n"
     "   enable spice\n"
     "   at least one of {port, tls-port} is mandatory\n",
@@ -2237,6 +2238,10 @@ SRST
     ``seamless-migration=[on|off]``
         Enable/disable spice seamless migration. Default is off.
 
+    ``preferred-codec=<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 +14 -0
index 72f8f1681c..6e00211e3a 100644
@@ -469,6 +469,9 @@ static QemuOptsList qemu_spice_opts = {
         },{
             .name = "streaming-video",
             .type = QEMU_OPT_STRING,
+        },{
+            .name = "preferred-codec",
+            .type = QEMU_OPT_STRING,
         },{
             .name = "agent-mouse",
             .type = QEMU_OPT_BOOL,
@@ -644,6 +647,7 @@ static void qemu_spice_init(void)
     char *x509_key_file = NULL,
         *x509_cert_file = NULL,
         *x509_cacert_file = NULL;
+    const char *preferred_codec = NULL;
     int port, tls_port, addr_flags;
     spice_image_compression_t compression;
     spice_wan_compression_t wan_compr;
@@ -795,6 +799,16 @@ static void qemu_spice_init(void)
         spice_server_set_streaming_video(spice_server, SPICE_STREAM_VIDEO_OFF);
     }
 
+    preferred_codec = qemu_opt_get(opts, "preferred-codec");
+    if (preferred_codec) {
+        if (spice_server_set_video_codecs(spice_server, preferred_codec)) {
+            error_report("Preferred codec name is not valid");
+            exit(1);
+        }
+    } else {
+        spice_server_set_video_codecs(spice_server, "spice:mjpeg");
+    }
+
     spice_server_set_agent_mouse
         (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1));
     spice_server_set_playback_compression
Signature · 1 line
2.37.2
#2 lore raw

Re: [RFC v2 1/2] spice: Add an option for users to provide a preferred codec

Il giorno lun 23 gen 2023 alle ore 08:37 Vivek Kasireddy <[email protected]> ha scritto:

Quoted 61 lines

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 Dongwon Kim <[email protected]>
---
 qemu-options.hx |  5 +++++
 ui/spice-core.c | 14 ++++++++++++++
 2 files changed, 19 insertions(+)
qemu-options.hx +5 -0
index 3aa3a2f5a3..aab8df0922 100644
@@ -2142,6 +2142,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"
+    "       [,preferred-codec=<encoder>:<codec>\n"
     "       [,gl=[on|off]][,rendernode=<file>]\n"
     "   enable spice\n"
     "   at least one of {port, tls-port} is mandatory\n",
@@ -2237,6 +2238,10 @@ SRST
     ``seamless-migration=[on|off]``
         Enable/disable spice seamless migration. Default is off.
 
+    ``preferred-codec=<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 +4 -0
index 72f8f1681c..6e00211e3a 100644
@@ -469,6 +469,9 @@ static QemuOptsList qemu_spice_opts = {
         },{
             .name = "streaming-video",
             .type = QEMU_OPT_STRING,
+        },{
+            .name = "preferred-codec",
+            .type = QEMU_OPT_STRING,
         },{
             .name = "agent-mouse",
             .type = QEMU_OPT_BOOL,
@@ -644,6 +647,7 @@ static void qemu_spice_init(void)
     char *x509_key_file = NULL,
         *x509_cert_file = NULL,
         *x509_cacert_file = NULL;
+    const char *preferred_codec = NULL;
     int port, tls_port, addr_flags;
     spice_image_compression_t compression;
     spice_wan_compression_t wan_compr;
@@ -795,6 +799,16 @@ static void qemu_spice_init(void)
         spice_server_set_streaming_video(spice_server, SPICE_STREAM_VIDEO_OFF);

ui/spice-core.c static void qemu_spice_init(void) · line 809

Quoted 10 lines
ui/spice-core.c +8 -0
     }
 
+    preferred_codec = qemu_opt_get(opts, "preferred-codec");
+    if (preferred_codec) {
+        if (spice_server_set_video_codecs(spice_server, preferred_codec)) {
+            error_report("Preferred codec name is not valid");
+            exit(1);
+        }
+    } else {
+        spice_server_set_video_codecs(spice_server, "spice:mjpeg");

Why overriding the compiled in Spice default?
Apart from that the commit seems good to me.

ui/spice-core.c static void qemu_spice_init(void) · line 814

Quoted 5 lines
ui/spice-core.c +2 -0
+    }
+
     spice_server_set_agent_mouse
         (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1));
     spice_server_set_playback_compression

Frediano

#3 lore raw

RFC v2 2/2 spice: Add an option to forward the dmabuf directly to the encoder (v2)

In reply to Vivek Kasireddy

This patch adds support for gl=on and port != 0. In other words, with this option enabled, it should be possible to stream the content associated with the dmabuf to a remote client.

Here is the flow of things from the Qemu side:
- Call gl_scanout (to update the fd) and gl_draw_async just like in the local display case.
- Additionally, create an update with the cmd set to QXL_CMD_DRAW to trigger the creation of a new drawable (associated with the fd) by the Spice server.
- Wait (or block) until the Encoder is done encoding the content.
- Unblock the pipeline once the async completion cookie is received.

v2:
- Use the existing gl_scanout and gl_draw_async APIs instead of adding new ones.

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Dongwon Kim <[email protected]>
---
 include/ui/spice-display.h |  1 +
 qemu-options.hx            |  6 ++-
 ui/spice-core.c            | 22 +++++++++--
 ui/spice-display.c         | 75 ++++++++++++++++++++++++++++----------
 4 files changed, 81 insertions(+), 23 deletions(-)
include/ui/spice-display.h +1 -0
index e271e011da..df74f5ee9b 100644
@@ -153,6 +153,7 @@ struct SimpleSpiceCursor {
 };
 
 extern bool spice_opengl;
+extern bool spice_dmabuf_encode;
 
 int qemu_spice_rect_is_empty(const QXLRect* r);
 void qemu_spice_rect_union(QXLRect *dest, const QXLRect *r);
qemu-options.hx +5 -1
index aab8df0922..3016f8a6f7 100644
@@ -2143,7 +2143,7 @@ DEF("spice", HAS_ARG, QEMU_OPTION_spice,
     "       [,disable-agent-file-xfer=on|off][,agent-mouse=[on|off]]\n"
     "       [,playback-compression=[on|off]][,seamless-migration=[on|off]]\n"
     "       [,preferred-codec=<encoder>:<codec>\n"
-    "       [,gl=[on|off]][,rendernode=<file>]\n"
+    "       [,gl=[on|off]][,rendernode=<file>][,dmabuf-encode=[on|off]]\n"
     "   enable spice\n"
     "   at least one of {port, tls-port} is mandatory\n",
     QEMU_ARCH_ALL)
@@ -2248,6 +2248,10 @@ SRST
     ``rendernode=<file>``
         DRM render node for OpenGL rendering. If not specified, it will
         pick the first available. (Since 2.9)
+
+    ``dmabuf-encode=[on|off]``
+        Forward the dmabuf directly to the encoder (Gstreamer).
+        Default is off.
 ERST
 
 DEF("portrait", 0, QEMU_OPTION_portrait,
ui/spice-core.c +19 -3
index 6e00211e3a..c9b856b056 100644
@@ -494,6 +494,9 @@ static QemuOptsList qemu_spice_opts = {
         },{
             .name = "rendernode",
             .type = QEMU_OPT_STRING,
+        },{
+            .name = "dmabuf-encode",
+            .type = QEMU_OPT_BOOL,
 #endif
         },
         { /* end of list */ }
@@ -843,11 +846,24 @@ static void qemu_spice_init(void)
     g_free(password);
 
 #ifdef HAVE_SPICE_GL
+    if (qemu_opt_get_bool(opts, "dmabuf-encode", 0)) {
+        spice_dmabuf_encode = 1;
+    }
     if (qemu_opt_get_bool(opts, "gl", 0)) {
-        if ((port != 0) || (tls_port != 0)) {
-            error_report("SPICE GL support is local-only for now and "
-                         "incompatible with -spice port/tls-port");
+        if (((port != 0) || (tls_port != 0)) && !spice_dmabuf_encode) {
+            error_report("Add dmabuf-encode=on option to enable GL streaming");
             exit(1);
+        } else if (spice_dmabuf_encode) {
+            if (port == 0 && tls_port == 0) {
+                error_report("dmabuf-encode=on is only meant to be used for "
+                             "non-local displays");
+                exit(1);
+            }
+            if (g_strcmp0(preferred_codec, "gstreamer:h264")) {
+                error_report("dmabuf-encode=on currently only works and tested"
+                             "with gstreamer:h264");
+                exit(1);
+            }
         }
         if (egl_rendernode_init(qemu_opt_get(opts, "rendernode"),
                                 DISPLAYGL_MODE_ON) != 0) {
ui/spice-display.c +56 -19

168 lines not on this page · raw message

Signature · 1 line
2.37.2
#4 lore raw

Hi,

Quoted 3 lines

Here is the flow of things from the Qemu side:
- Call gl_scanout (to update the fd) and gl_draw_async just like in the local display case.

Ok.

Quoted 5 lines

- Additionally, create an update with the cmd set to QXL_CMD_DRAW to trigger the creation of a new drawable (associated with the fd) by the Spice server.
- Wait (or block) until the Encoder is done encoding the content.
- Unblock the pipeline once the async completion cookie is received.

Care to explain? For qemu it should make a difference what spice-server does with the dma-bufs passed (local display / encode video + send to remote).

ui/spice-core.c static void qemu_spice_init(void) · line 866

Quoted 7 lines

#ifdef HAVE_SPICE_GL

ui/spice-core.c +6 -0
+        } else if (spice_dmabuf_encode) {
+            if (g_strcmp0(preferred_codec, "gstreamer:h264")) {
+                error_report("dmabuf-encode=on currently only works and tested"
+                             "with gstreamer:h264");
+                exit(1);
+            }

IMHO we should not hard-code todays spice-server capabilities like this. For starters this isn't true for spice-server versions which don't (yet) have your patches. Also the capability might depend on hardware support. IMHO we need some feature negotiation between qemu and spice here.

take care,
Gerd

#5 lore raw

+ Frediano

Hi Gerd,

Quoted 15 lines

Hi,

Quoted 3 lines

Here is the flow of things from the Qemu side:
- Call gl_scanout (to update the fd) and gl_draw_async just like in the local display case.

Ok.

Quoted 5 lines

- Additionally, create an update with the cmd set to QXL_CMD_DRAW to trigger the creation of a new drawable (associated with the fd) by the Spice server.
- Wait (or block) until the Encoder is done encoding the content.
- Unblock the pipeline once the async completion cookie is received.

Quoted 3 lines

Care to explain? For qemu it should make a difference what spice-server does with the dma-bufs passed (local display / encode video + send to remote).

[Kasireddy, Vivek] I agree that Qemu shouldn't care what the spice-server does with the dmabuf fds but somehow a drawable has to be created in the remote client case. This is needed as most of the core functions in the server (associated with display-channel, video-stream, encoder, etc) operate on drawables. Therefore, I figured since Qemu already tells the server to create a drawable in the non-gl case (by creating an update that includes a QXL_CMD_DRAW cmd), the same thing can be done in the gl + remote client case as well.

Alternatively, we could make the server create a drawable as a response to gl_scanout, when it detects a remote client. IIUC, I think this can be done but seems rather messy given that currently, the server only creates a drawable (inside red_process_display) in the case of QXL_CMD_DRAW sent by Qemu/applications:

        switch (ext_cmd.cmd.type) {
        case QXL_CMD_DRAW: {
            auto red_drawable = red_drawable_new(worker->qxl, &worker->mem_slots,
                                                 ext_cmd.group_id, ext_cmd.cmd.data,
                                                 ext_cmd.flags); // returns with 1 ref

            if (red_drawable) {
                display_channel_process_draw(worker->display_channel, std::move(red_drawable),
                                             worker->process_display_generation);
            }

The other option I can think of is to just not deal with drawables at all and somehow directly share the dmabuf fd with the Encoder. This solution also seems very messy and invasive to me as we'd not be able to leverage the existing APIs (in display-channel, video-stream, etc) that create and manage streams efficiently.

Quoted 9 lines

ui/spice-core.c static void qemu_spice_init(void) · line 866

Quoted 8 lines

#ifdef HAVE_SPICE_GL

ui/spice-core.c +6 -0
+        } else if (spice_dmabuf_encode) {
+            if (g_strcmp0(preferred_codec, "gstreamer:h264")) {
+                error_report("dmabuf-encode=on currently only works and tested"
+                             "with gstreamer:h264");
+                exit(1);
+            }
Quoted 5 lines

IMHO we should not hard-code todays spice-server capabilities like this. For starters this isn't true for spice-server versions which don't (yet) have your patches. Also the capability might depend on hardware support. IMHO we need some feature negotiation between qemu and spice here.

[Kasireddy, Vivek] Ok, I can get rid of this chunk in v3. However, given the numerous features supported by the Spice server, I suspect implementing feature negotiation might get really challenging. Is there any other way around this that you can think of?

Thanks,
Vivek

Quoted 3 lines

take care,
Gerd

#6 lore raw

Hi,

Quoted 2 lines

The other option I can think of is to just not deal with drawables at all and somehow directly share the dmabuf fd with the Encoder.

This is what I expected to happen. This also the case when using dma-bufs for local display.

Quoted 8 lines
Quoted 5 lines

IMHO we should not hard-code todays spice-server capabilities like this. For starters this isn't true for spice-server versions which don't (yet) have your patches. Also the capability might depend on hardware support. IMHO we need some feature negotiation between qemu and spice here.

[Kasireddy, Vivek] Ok, I can get rid of this chunk in v3. However, given the numerous features supported by the Spice server, I suspect implementing feature negotiation might get really challenging. Is there any other way around this that you can think of?

I'm thinking about a simple bool, i.e. replace the current hard failure for the remote case with a query whenever the server supports dma-buf video encoding or not.

take care,
Gerd

#7 lore raw
In reply to Kasireddy, Vivek

Il giorno mar 24 gen 2023 alle ore 06:41 Kasireddy, Vivek <[email protected]> ha scritto:

Quoted 23 lines

+ Frediano

Hi Gerd,

Quoted 15 lines

Hi,

Quoted 3 lines

Here is the flow of things from the Qemu side:
- Call gl_scanout (to update the fd) and gl_draw_async just like in the local display case.

Ok.

Quoted 5 lines

- Additionally, create an update with the cmd set to QXL_CMD_DRAW to trigger the creation of a new drawable (associated with the fd) by the Spice server.
- Wait (or block) until the Encoder is done encoding the content.
- Unblock the pipeline once the async completion cookie is received.

Quoted 3 lines

Care to explain? For qemu it should make a difference what spice-server does with the dma-bufs passed (local display / encode video + send to remote).

Quoted 8 lines

[Kasireddy, Vivek] I agree that Qemu shouldn't care what the spice-server does with the dmabuf fds but somehow a drawable has to be created in the remote client case. This is needed as most of the core functions in the server (associated with display-channel, video-stream, encoder, etc) operate on drawables. Therefore, I figured since Qemu already tells the server to create a drawable in the non-gl case (by creating an update that includes a QXL_CMD_DRAW cmd), the same thing can be done in the gl + remote client case as well.

This is a hack. It's combining an invalid command in order to cause some side effects on spice server but it also needs a change in spice server to detect and handle this hack. QXL_CMD_DRAW is mainly bound to 2D commands and should come with valid bitmap data.

Quoted 4 lines

Alternatively, we could make the server create a drawable as a response to gl_scanout, when it detects a remote client. IIUC, I think this can be done but seems rather messy given that currently, the server only creates a drawable (inside red_process_display) in the case of QXL_CMD_DRAW sent by Qemu/applications:

Quoted 11 lines
        switch (ext_cmd.cmd.type) {
        case QXL_CMD_DRAW: {
            auto red_drawable = red_drawable_new(worker->qxl, &worker->mem_slots,
                                                 ext_cmd.group_id, ext_cmd.cmd.data,
                                                 ext_cmd.flags); // returns with 1 ref

            if (red_drawable) {
                display_channel_process_draw(worker->display_channel, std::move(red_drawable),
                                             worker->process_display_generation);
            }

Yes, it sounds a bit long but surely better than hacking Qemu, spice server and defining a new hacky ABI that will be hard to maintain and understand.

Quoted 5 lines

The other option I can think of is to just not deal with drawables at all and somehow directly share the dmabuf fd with the Encoder. This solution also seems very messy and invasive to me as we'd not be able to leverage the existing APIs (in display-channel, video-stream, etc) that create and manage streams efficiently.

Yes, that currently seems pretty long as a change but possibly the most clean in future, it's up to some refactory. The Encoder does not either need technically a RedDrawable, Drawable but frame data encoded in a format it can manage (either raw memory data or dmabuf at the moment).

Quoted 14 lines
Quoted 9 lines

ui/spice-core.c static void qemu_spice_init(void) · line 866

Quoted 8 lines

#ifdef HAVE_SPICE_GL

ui/spice-core.c +6 -0
+        } else if (spice_dmabuf_encode) {
+            if (g_strcmp0(preferred_codec, "gstreamer:h264")) {
+                error_report("dmabuf-encode=on currently only works and tested"
+                             "with gstreamer:h264");
+                exit(1);
+            }
Quoted 5 lines

IMHO we should not hard-code todays spice-server capabilities like this. For starters this isn't true for spice-server versions which don't (yet) have your patches. Also the capability might depend on hardware support. IMHO we need some feature negotiation between qemu and spice here.

Quoted 11 lines

[Kasireddy, Vivek] Ok, I can get rid of this chunk in v3. However, given the numerous features supported by the Spice server, I suspect implementing feature negotiation might get really challenging. Is there any other way around this that you can think of?

Thanks,
Vivek

Quoted 3 lines

take care,
Gerd

Frediano

#8 lore raw

Hi Frediano, Gerd,

Quoted 73 lines

Il giorno mar 24 gen 2023 alle ore 06:41 Kasireddy, Vivek <[email protected]> ha scritto:

Quoted 20 lines

+ Frediano

Hi Gerd,

Quoted 15 lines

Hi,

Quoted 3 lines

Here is the flow of things from the Qemu side:
- Call gl_scanout (to update the fd) and gl_draw_async just like in the local display case.

Ok.

Quoted 5 lines

- Additionally, create an update with the cmd set to QXL_CMD_DRAW to trigger the creation of a new drawable (associated with the fd) by the Spice server.
- Wait (or block) until the Encoder is done encoding the content.
- Unblock the pipeline once the async completion cookie is received.

Quoted 5 lines
Quoted 3 lines

Care to explain? For qemu it should make a difference what spice-server does with the dma-bufs passed (local display / encode video + send to remote).

[Kasireddy, Vivek] I agree that Qemu shouldn't care what the spice-server does with the dmabuf fds but somehow a drawable has to be created in the remote

client

Quoted 3 lines

case. This is needed as most of the core functions in the server (associated with display-channel, video-stream, encoder, etc) operate on drawables. Therefore, I figured since Qemu already tells the server to create a drawable in the non-gl

case

Quoted 3 lines

(by creating an update that includes a QXL_CMD_DRAW cmd), the same thing can be done in the gl + remote client case as well.

This is a hack. It's combining an invalid command in order to cause some side effects on spice server but it also needs a change in spice server to detect and handle this hack. QXL_CMD_DRAW is mainly bound to 2D commands and should come with valid bitmap data.

Quoted 1 line

Alternatively, we could make the server create a drawable as a response to

gl_scanout,

Quoted 1 line

when it detects a remote client. IIUC, I think this can be done but seems rather

messy

Quoted 1 line

given that currently, the server only creates a drawable (inside

red_process_display)

Quoted 10 lines

in the case of QXL_CMD_DRAW sent by Qemu/applications:

        switch (ext_cmd.cmd.type) {
        case QXL_CMD_DRAW: {
            auto red_drawable = red_drawable_new(worker->qxl, &worker-
mem_slots,
                                                 ext_cmd.group_id, ext_cmd.cmd.data,
                                                 ext_cmd.flags); // returns with 1 ref

            if (red_drawable) {
                display_channel_process_draw(worker->display_channel,

std::move(red_drawable),

Quoted 3 lines
                                             worker->process_display_generation);
            }

Yes, it sounds a bit long but surely better than hacking Qemu, spice server and defining a new hacky ABI that will be hard to maintain and understand.

Quoted 1 line

The other option I can think of is to just not deal with drawables at all and

somehow

Quoted 1 line

directly share the dmabuf fd with the Encoder. This solution also seems very

messy

Quoted 1 line

and invasive to me as we'd not be able to leverage the existing APIs (in display-

channel,

Quoted 8 lines
Quoted 2 lines

video-stream, etc) that create and manage streams efficiently.

Yes, that currently seems pretty long as a change but possibly the most clean in future, it's up to some refactory. The Encoder does not either need technically a RedDrawable, Drawable but frame data encoded in a format it can manage (either raw memory data or dmabuf at the moment).

[Kasireddy, Vivek] Ok, I'll work on refactoring Spice server code to pass the dmabuf fd directly to the Encoder without having to creating drawables.

Thanks,
Vivek

Quoted 31 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

#ifdef HAVE_SPICE_GL

ui/spice-core.c +6 -0
+        } else if (spice_dmabuf_encode) {
+            if (g_strcmp0(preferred_codec, "gstreamer:h264")) {
+                error_report("dmabuf-encode=on currently only works and tested"
+                             "with gstreamer:h264");
+                exit(1);
+            }
Quoted 6 lines
Quoted 5 lines

IMHO we should not hard-code todays spice-server capabilities like this. For starters this isn't true for spice-server versions which don't (yet) have your patches. Also the capability might depend on hardware support. IMHO we need some feature negotiation between qemu and spice here.

[Kasireddy, Vivek] Ok, I can get rid of this chunk in v3. However, given the

numerous

Quoted 1 line

features supported by the Spice server, I suspect implementing feature

negotiation

Quoted 1 line

might get really challenging. Is there any other way around this that you can

think of?

Quoted 8 lines

Thanks,
Vivek

Quoted 3 lines

take care,
Gerd

Frediano

#9 lore raw

Il giorno lun 30 gen 2023 alle ore 02:24 Kasireddy, Vivek <[email protected]> ha scritto:

Quoted 84 lines

Hi Frediano, Gerd,

Quoted 73 lines

Il giorno mar 24 gen 2023 alle ore 06:41 Kasireddy, Vivek <[email protected]> ha scritto:

Quoted 20 lines

+ Frediano

Hi Gerd,

Quoted 15 lines

Hi,

Quoted 3 lines

Here is the flow of things from the Qemu side:
- Call gl_scanout (to update the fd) and gl_draw_async just like in the local display case.

Ok.

Quoted 5 lines

- Additionally, create an update with the cmd set to QXL_CMD_DRAW to trigger the creation of a new drawable (associated with the fd) by the Spice server.
- Wait (or block) until the Encoder is done encoding the content.
- Unblock the pipeline once the async completion cookie is received.

Quoted 5 lines
Quoted 3 lines

Care to explain? For qemu it should make a difference what spice-server does with the dma-bufs passed (local display / encode video + send to remote).

[Kasireddy, Vivek] I agree that Qemu shouldn't care what the spice-server does with the dmabuf fds but somehow a drawable has to be created in the remote

client

Quoted 3 lines

case. This is needed as most of the core functions in the server (associated with display-channel, video-stream, encoder, etc) operate on drawables. Therefore, I figured since Qemu already tells the server to create a drawable in the non-gl

case

Quoted 3 lines

(by creating an update that includes a QXL_CMD_DRAW cmd), the same thing can be done in the gl + remote client case as well.

This is a hack. It's combining an invalid command in order to cause some side effects on spice server but it also needs a change in spice server to detect and handle this hack. QXL_CMD_DRAW is mainly bound to 2D commands and should come with valid bitmap data.

Quoted 1 line

Alternatively, we could make the server create a drawable as a response to

gl_scanout,

Quoted 1 line

when it detects a remote client. IIUC, I think this can be done but seems rather

messy

Quoted 1 line

given that currently, the server only creates a drawable (inside

red_process_display)

Quoted 10 lines

in the case of QXL_CMD_DRAW sent by Qemu/applications:

        switch (ext_cmd.cmd.type) {
        case QXL_CMD_DRAW: {
            auto red_drawable = red_drawable_new(worker->qxl, &worker-
mem_slots,
                                                 ext_cmd.group_id, ext_cmd.cmd.data,
                                                 ext_cmd.flags); // returns with 1 ref

            if (red_drawable) {
                display_channel_process_draw(worker->display_channel,

std::move(red_drawable),

Quoted 3 lines
                                             worker->process_display_generation);
            }

Yes, it sounds a bit long but surely better than hacking Qemu, spice server and defining a new hacky ABI that will be hard to maintain and understand.

Quoted 1 line

The other option I can think of is to just not deal with drawables at all and

somehow

Quoted 1 line

directly share the dmabuf fd with the Encoder. This solution also seems very

messy

Quoted 1 line

and invasive to me as we'd not be able to leverage the existing APIs (in display-

channel,

Quoted 8 lines
Quoted 2 lines

video-stream, etc) that create and manage streams efficiently.

Yes, that currently seems pretty long as a change but possibly the most clean in future, it's up to some refactory. The Encoder does not either need technically a RedDrawable, Drawable but frame data encoded in a format it can manage (either raw memory data or dmabuf at the moment).

Quoted 6 lines

[Kasireddy, Vivek] Ok, I'll work on refactoring Spice server code to pass the dmabuf fd directly to the Encoder without having to creating drawables.

Thanks,
Vivek

Hi Vivek,
thanks for the effort. I'll try to help although I'll be pretty
busy for a while.

I'd try (consider them only as suggestions) to edit display_channel_gl_scanout, dcc_gl_scanout_item_new, display_channel_gl_draw, dcc_gl_draw_item_new to handle requests from Qemu. Specifically in dcc_gl_scanout_item_new and dcc_gl_draw_item_new you probably want to remove the check and error for Unix sockets. As code you wrote in Qemu you will need to create a new surface (if not already there or if size changed) handling a new scanout. Also probably better to create a VideoStream specifically for GL surfaces. How to have some code shortcut to deliver the GL surface directly to the stream? No much suggestions.
It would probably be nice to add an additional method in VideoEncoder (video-encoder.h) to accept a GL surface instead of a SpiceBitmap.

Frediano

Quoted 31 lines
Quoted 31 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

#ifdef HAVE_SPICE_GL

ui/spice-core.c +6 -0
+        } else if (spice_dmabuf_encode) {
+            if (g_strcmp0(preferred_codec, "gstreamer:h264")) {
+                error_report("dmabuf-encode=on currently only works and tested"
+                             "with gstreamer:h264");
+                exit(1);
+            }
Quoted 6 lines
Quoted 5 lines

IMHO we should not hard-code todays spice-server capabilities like this. For starters this isn't true for spice-server versions which don't (yet) have your patches. Also the capability might depend on hardware support. IMHO we need some feature negotiation between qemu and spice here.

[Kasireddy, Vivek] Ok, I can get rid of this chunk in v3. However, given the

numerous

Quoted 1 line

features supported by the Spice server, I suspect implementing feature

negotiation

Quoted 1 line

might get really challenging. Is there any other way around this that you can

think of?

Quoted 8 lines

Thanks,
Vivek

Quoted 3 lines

take care,
Gerd

Frediano