ui/spice: Enable gl=on option for non-local or remote clients
v6 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 to encode the Guest FB via the Gstreamer framework.
v5 -> v6:
- Collect Marc-André's RB tag
- Instead of having the user provide both encoder and codec options, just have a single option to collect the preferred codec that would only be used with the Gstreamer encoder (Daniel)
- Use gstreamer:h264 as the default codec if the user did not provide any codecs and if gl=on is enabled
v4 -> v5 (suggestions from Marc-André):
- Fix the errors (mostly 80 chars limit violations) identified by scripts/checkpatch.pl
- Rename the globals to have a spice_ prefix for consistency
- Rename MAX_REFRESH_RATE to DEFAULT_MAX_REFRESH_RATE
- Added comments to explain how/when the gl_draw request is submitted to spice server in the remote clients case
- Fix the mem_obj leak that would occur when the associated texture is destroyed or when an error is encountered while creating a texture from an fd (Dmitry and Michael)
- Merged Michael's patch to fix the mem_obj leak into this series and added his Co-developed-by tag to the relevant patches
v3 -> v4 (suggestions from Marc-André):
- Add a new parameter to make max_refresh_rate configurable
- Have surface_gl_create_texture_from_fd() return bool after checking for errors
- Remove the check for PIXMAN_r5g6b5() in spice_gl_replace_fd_texture()
- Report errors in spice_gl_replace_fd_texture() when someting fails
- Use glGetError() correctly by adding an additional (dummy) call before checking for actual errors (Dmitry)
- Add a new patch to check fd values in egl_dmabuf_export_texture()
- Rebase on Qemu master
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]>
Cc: Daniel P. Berrangé <[email protected]>
Vivek Kasireddy (7):
ui/egl-helpers: Error check the fds in egl_dmabuf_export_texture() ui/spice: Enable gl=on option for non-local or remote clients ui/spice: Add an option for users to provide a preferred video codec ui/spice: Add an option to submit gl_draw requests at fixed rate 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
specified
ui/spice: Blit the scanout texture if its memory layout is not linear
include/ui/console.h | 3 + include/ui/spice-display.h | 5 + include/ui/surface.h | 1 + qemu-options.hx | 13 +++ ui/console-gl.c | 54 +++++++++ ui/egl-helpers.c | 6 + ui/spice-core.c | 31 +++++ ui/spice-display.c | 226 ++++++++++++++++++++++++++++++++++--- 8 files changed, 323 insertions(+), 16 deletions(-)
Signature · 1 line
2.49.0
PATCH v6 1/7 ui/egl-helpers: Error check the fds in egl_dmabuf_export_texture()
While trying to export and obtain fds associated with a texture, it is possible that the fds returned after eglExportDMABUFImageMESA() call have error values. Therefore, we need to evaluate the value of all fds and return false if any of them are negative.
--- ui/egl-helpers.c | 6 ++++++ 1 file changed, 6 insertions(+)
ui/egl-helpers.c +6 -0
@@ -295,6 +295,7 @@ bool egl_dmabuf_export_texture(uint32_t tex_id, int *fd, EGLint *offset, {EGLImageKHR image;EGLuint64KHR modifiers[DMABUF_MAX_PLANES ]; +int i; image = eglCreateImageKHR(qemu_egl_display, eglGetCurrentContext(), EGL_GL_TEXTURE_2D_KHR,
@@ -314,6 +315,11 @@ bool egl_dmabuf_export_texture(uint32_t tex_id, int *fd, EGLint *offset, *modifier = modifiers[0 ]; } +for (i =0 ; i < *num_planes; i++) { +if (fd[i] <0 ) { +return false; + } + }return true; }
Signature · 1 line
2.49.0
PATCH v6 2/7 ui/spice: Enable gl=on option for non-local or remote clients
In reply to Vivek KasireddyNewer 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.
--- 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
@@ -151,6 +151,7 @@ struct SimpleSpiceCursor { };extern bool spice_opengl; +extern bool spice_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
@@ -836,9 +836,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 */ + spice_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
@@ -31,6 +31,7 @@#include "standard-headers/drm/drm_fourcc.h" bool spice_opengl; +bool spice_remote_client;int qemu_spice_rect_is_empty (const QXLRect * r) {
Signature · 1 line
2.49.0
PATCH v6 3/7 ui/spice: Add an option for users to provide a preferred video codec
In reply to Vivek KasireddyGiving users an option to choose a particular codec will enable them to make an appropriate decision based on their hardware and use-case. Note that, the Spice server would use this codec with Gstreamer encoder and only when gl=on is specified.
If no codec is provided, then the codec gstreamer:h264 would be used as default. And, for the case where gl=off, the default codec to be used is determined by the Spice server.
--- qemu-options.hx | 8 ++++++++ ui/spice-core.c | 15 +++++++++++++++ 2 files changed, 23 insertions(+)
qemu-options.hx +8 -0
@@ -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-codec=<codec>\n" " [,gl=[on|off]][,rendernode=<file>]\n" " enable spice\n" " at least one of {port, tls-port} is mandatory\n",
@@ -2369,6 +2370,13 @@ SRST ``seamless-migration=[on|off]`` Enable/disable spice seamless migration. Default is off. + ``video-codec=<codec>`` + Provide the preferred codec the Spice server should use with the + Gstreamer encoder. This option is only relevant when gl=on is + specified. If no codec is provided, then the codec gstreamer:h264 + would be used as default. And, for the case where gl=off, the + default codec to be used is determined by the Spice server. + ``gl=[on|off]`` Enable/disable OpenGL context. Default is off.
ui/spice-core.c +15 -0
@@ -488,6 +488,9 @@ static QemuOptsList qemu_spice_opts = { },{ .name = "streaming-video", .type = QEMU_OPT_STRING, + },{ + .name = "video-codec", + .type = QEMU_OPT_STRING, },{ .name = "agent-mouse", .type = QEMU_OPT_BOOL,
@@ -662,6 +665,8 @@ static void qemu_spice_init(void)char *x509_key_file =NULL , *x509_cert_file =NULL , *x509_cacert_file =NULL ; +const char *video_codec =NULL ; + g_autofree char *enc_codec = NULL;int port, tls_port, addr_flags;spice_image_compression_t compression;spice_wan_compression_t wan_compr;
@@ -838,6 +843,16 @@ static void qemu_spice_init(void)if ((port !=0 ) || (tls_port !=0 )) {#if SPICE_SERVER_VERSION >=0x000f03 /* release 0.15.3 */ spice_remote_client =1 ; + + video_codec =qemu_opt_get (opts,"video-codec" ); +if (video_codec) { + enc_codec =g_strconcat ("gstreamer:" , video_codec,NULL ); + } +if (spice_server_set_video_codecs (spice_server, + enc_codec ?:"gstreamer:h264" )) { +error_report ("invalid video codec" ); +exit (1 ); + }#else error_report ("SPICE GL support is local-only for now and " "incompatible with -spice port/tls-port" );
Signature · 1 line
2.49.0
PATCH v6 4/7 ui/spice: Add an option to submit gl_draw requests at fixed rate
In reply to Vivek KasireddyIn 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 (refresh rate) to a fixed value using the GUI refresh timer. Otherwise, the updates or gl_draw requests would be sent as soon as the Guest submits a new frame which is not optimal as it would lead to increased network traffic and wastage of GPU cycles if the frames get dropped.
--- include/ui/spice-display.h | 1 + qemu-options.hx | 5 +++ ui/spice-core.c | 12 ++++++++ ui/spice-display.c | 62 ++++++++++++++++++++++++++++++++------ 4 files changed, 70 insertions(+), 10 deletions(-)
include/ui/spice-display.h +1 -0
@@ -152,6 +152,7 @@ struct SimpleSpiceCursor {extern bool spice_opengl;extern bool spice_remote_client; +extern int spice_max_refresh_rate;int qemu_spice_rect_is_empty (const QXLRect * r);void qemu_spice_rect_union (QXLRect *dest,const QXLRect *r);
qemu-options.hx +5 -0
@@ -2282,6 +2282,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" " [,video-codec=<codec>\n" + " [,max-refresh-rate=rate\n" " [,gl=[on|off]][,rendernode=<file>]\n" " enable spice\n" " at least one of {port, tls-port} is mandatory\n",
@@ -2377,6 +2378,10 @@ SRST would be used as default. And, for the case where gl=off, the default codec to be used is determined by the Spice server. + ``max-refresh-rate=rate`` + Provide the maximum refresh rate (or FPS) at which the encoding + requests should be sent to the Spice server. Default would be 30. + ``gl=[on|off]`` Enable/disable OpenGL context. Default is off.
ui/spice-core.c +12 -0
@@ -56,6 +56,8 @@ struct SpiceTimer {QEMUTimer *timer; }; +#define DEFAULT_MAX_REFRESH_RATE 30 +static SpiceTimer *timer_add (SpiceTimerFunc func,void *opaque) {SpiceTimer *timer;
@@ -491,6 +493,9 @@ static QemuOptsList qemu_spice_opts = { },{ .name = "video-codec", .type = QEMU_OPT_STRING, + },{ + .name = "max-refresh-rate", + .type = QEMU_OPT_NUMBER, },{ .name = "agent-mouse", .type = QEMU_OPT_BOOL,
@@ -806,6 +811,13 @@ static void qemu_spice_init(void)spice_server_set_streaming_video (spice_server,SPICE_STREAM_VIDEO_OFF ); } + spice_max_refresh_rate =qemu_opt_get_number (opts,"max-refresh-rate" , +DEFAULT_MAX_REFRESH_RATE ); +if (spice_max_refresh_rate <=0 ) { +error_report ("max refresh rate/fps is invalid" ); +exit (1 ); + } +spice_server_set_agent_mouse (spice_server,qemu_opt_get_bool (opts,"agent-mouse" ,1 )); spice_server_set_playback_compression
ui/spice-display.c +52 -10
102 lines not on this page · raw message
Signature · 1 line
2.49.0
PATCH v6 5/7 ui/console-gl: Add a helper to create a texture with linear memory layout
In reply to Vivek KasireddyThere 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.
--- include/ui/console.h | 3 +++ ui/console-gl.c | 48 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+)
include/ui/console.h +3 -0
@@ -422,6 +422,9 @@ bool console_gl_check_format(DisplayChangeListener *dcl, pixman_format_code_t format);void surface_gl_create_texture (QemuGLShader *gls,DisplaySurface *surface); +bool surface_gl_create_texture_from_fd (DisplaySurface *surface, +int fd,GLuint *texture, +GLuint *mem_obj);void surface_gl_update_texture (QemuGLShader *gls,DisplaySurface *surface,int x,int y,int w,int h);
ui/console-gl.c +48 -0
60 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [PATCH v6 5/7] ui/console-gl: Add a helper to create a texture with linear memory layout
Quoted 89 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]>Reviewed-by Marc-André Lureau <[email protected]>Co-developed-by Michael Scherle <[email protected]>Signed-off-by Vivek Kasireddy <[email protected]>--- include/ui/console.h | 3 +++ ui/console-gl.c | 48 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+)include/ui/console.h +3
-0@@ -422,6 +422,9 @@ bool console_gl_check_format(DisplayChangeListener *dcl, pixman_format_code_t format);void surface_gl_create_texture (QemuGLShader *gls,DisplaySurface *surface); +bool surface_gl_create_texture_from_fd (DisplaySurface *surface, +int fd,GLuint *texture, +GLuint *mem_obj);void surface_gl_update_texture (QemuGLShader *gls,DisplaySurface *surface,int x,int y,int w,int h);ui/console-gl.c +41
-0@@ -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,53 @@ void surface_gl_create_texture(QemuGLShader *gls,glTexParameteri (GL_TEXTURE_2D ,GL_TEXTURE_MIN_FILTER ,GL_LINEAR ); } +bool surface_gl_create_texture_from_fd (DisplaySurface *surface, +int fd,GLuint *texture, +GLuint *mem_obj) +{ +unsigned long size =surface_stride (surface) *surface_height (surface); +GLenum err =glGetError (); + *texture =0 ; + *mem_obj =0 ; + +if (!epoxy_has_gl_extension ("GL_EXT_memory_object" ) || + !epoxy_has_gl_extension ("GL_EXT_memory_object_fd" )) { +error_report ("spice: required OpenGL extensions not supported: " +"GL_EXT_memory_object and GL_EXT_memory_object_fd" ); +return false; + } + +#ifdef GL_EXT_memory_object_fd +glCreateMemoryObjectsEXT (1 , mem_obj); +glImportMemoryFdEXT (*mem_obj, size,GL_HANDLE_TYPE_OPAQUE_FD_EXT , fd); + + err =glGetError (); +if (err !=GL_NO_ERROR ) { +error_report ("spice: cannot import memory object from fd" ); + goto cleanup_mem; + } + +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 ); + err =glGetError (); +if (err !=GL_NO_ERROR ) { +error_report ("spice: cannot create texture from memory object" ); + goto cleanup_tex_and_mem; + } +return true; + +cleanup_tex_and_mem: +glDeleteTextures (1 , texture);
ui/console-gl.c void surface_gl_create_texture(QemuGLShader *gls, · line 149
Quoted 10 lines
ui/console-gl.c +7
-0+cleanup_mem: +glDeleteMemoryObjectsEXT (1 , mem_obj); + +#endif +return false; +} +void surface_gl_update_texture (QemuGLShader *gls,DisplaySurface *surface,int x,int y,int w,int h)
Signature · 2 lines
Best regards, Dmitry
PATCH v6 6/7 ui/spice: Create a new texture with linear layout when gl=on is specified
In reply to Vivek KasireddySince most encoders/decoders (invoked by Spice) may not work properly 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.
--- include/ui/surface.h | 1 + ui/console-gl.c | 6 ++++ ui/spice-display.c | 82 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+)
include/ui/surface.h +1 -0
@@ -22,6 +22,7 @@ typedef struct DisplaySurface {GLenum glformat;GLenum gltype;GLuint texture; +GLuint mem_obj;#endif qemu_pixman_shareable share_handle;uint32_t share_handle_offset;
ui/console-gl.c +6 -0
@@ -184,6 +184,12 @@ void surface_gl_destroy_texture(QemuGLShader *gls, }glDeleteTextures (1 , &surface->texture); surface->texture =0 ; +#ifdef GL_EXT_memory_object_fd +if (surface->mem_obj) { +glDeleteMemoryObjectsEXT (1 , &surface->mem_obj); + surface->mem_obj =0 ; + } +#endif } void surface_gl_setup_viewport(QemuGLShader *gls,
ui/spice-display.c +82 -0
100 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [PATCH v6 6/7] ui/spice: Create a new texture with linear layout when gl=on is specified
ui/console-gl.c void surface_gl_destroy_texture(QemuGLShader *gls, · line 191
Quoted 13 lines · Vivek Kasireddy
ui/console-gl.c +5
-0@@ -184,6 +184,12 @@ void surface_gl_destroy_texture(QemuGLShader *gls, }glDeleteTextures (1 , &surface->texture); surface->texture =0 ; +#ifdef GL_EXT_memory_object_fd +if (surface->mem_obj) { +glDeleteMemoryObjectsEXT (1 , &surface->mem_obj); + surface->mem_obj =0 ; + }
Nit: spec says that deleting surface->mem_obj=0 is ignored, hence the "if (surface->mem_obj)" check shouldn't be needed, like in a case with glDeleteTextures() above. That would make code look more consistent.
Signature · 2 lines
Best regards, Dmitry
Dmitry Osipenko
Reviewed-byRe: [PATCH v6 6/7] ui/spice: Create a new texture with linear layout when gl=on is specified
Quoted 10 lines · Dmitry Osipenko
Quoted 9 lines · Vivek Kasireddy
ui/console-gl.c +1
-0@@ -184,6 +184,12 @@ void surface_gl_destroy_texture(QemuGLShader *gls, }glDeleteTextures (1 , &surface->texture); surface->texture =0 ; +#ifdef GL_EXT_memory_object_fd
Quoted 9 lines
ui/console-gl.c void surface_gl_destroy_texture(QemuGLShader *gls, · line 191
Quoted 4 lines
ui/console-gl.c +4
-0+if (surface->mem_obj) { +glDeleteMemoryObjectsEXT (1 , &surface->mem_obj); + surface->mem_obj =0 ; + }Nit: spec says that deleting surface->mem_obj=0 is ignored, hence the "if (
surface->mem_obj)" check shouldn't be needed, like in a case withglDeleteTextures()above. That would make code look more consistent.
Otherwise looks fine
Signature · 2 lines
Best regards, Dmitry
PATCH v6 7/7 ui/spice: Blit the scanout texture if its memory layout is not linear
In reply to Vivek KasireddyIn 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.
--- include/ui/spice-display.h | 3 ++ ui/spice-display.c | 81 +++++++++++++++++++++++++++++++++++--- 2 files changed, 78 insertions(+), 6 deletions(-)
include/ui/spice-display.h +3 -0
@@ -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 +75 -6
111 lines not on this page · raw message
Signature · 1 line
2.49.0