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

Vivek Kasireddy posted 6 patches 6 months, 2 weeks ago
There is a newer version of this series
[PATCH v3 5/6] ui/spice: Create a new texture with linear layout when gl=on is enabled
Posted by Vivek Kasireddy 6 months, 2 weeks ago
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 <kraxel@redhat.com>
Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
Cc: Dmitry Osipenko <dmitry.osipenko@collabora.com>
Cc: Frediano Ziglio <freddy77@gmail.com>
Cc: Dongwon Kim <dongwon.kim@intel.com>
Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
---
 ui/spice-display.c | 63 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 62 insertions(+), 1 deletion(-)

diff --git a/ui/spice-display.c b/ui/spice-display.c
index 2c4daa0707..047d453a0b 100644
--- a/ui/spice-display.c
+++ b/ui/spice-display.c
@@ -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),
-- 
2.49.0


Re: [PATCH v3 5/6] ui/spice: Create a new texture with linear layout when gl=on is enabled
Posted by Marc-André Lureau 6 months, 2 weeks ago
Hi

On Tue, Apr 29, 2025 at 10:13 AM Vivek Kasireddy
<vivek.kasireddy@intel.com> wrote:
>
> 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 <kraxel@redhat.com>
> Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
> Cc: Dmitry Osipenko <dmitry.osipenko@collabora.com>
> Cc: Frediano Ziglio <freddy77@gmail.com>
> Cc: Dongwon Kim <dongwon.kim@intel.com>
> Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
> ---
>  ui/spice-display.c | 63 +++++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 62 insertions(+), 1 deletion(-)
>
> diff --git a/ui/spice-display.c b/ui/spice-display.c
> index 2c4daa0707..047d453a0b 100644
> --- a/ui/spice-display.c
> +++ b/ui/spice-display.c
> @@ -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;
> +    }

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

> +
> +    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;
> +        }
> +    }

If it failed, it does nothing and continues?


> +    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),
> --
> 2.49.0
>
>


--
Marc-André Lureau
RE: [PATCH v3 5/6] ui/spice: Create a new texture with linear layout when gl=on is enabled
Posted by Kasireddy, Vivek 6 months, 2 weeks ago
Hi Marc-Andre,

> Subject: Re: [PATCH v3 5/6] ui/spice: Create a new texture with linear layout
> when gl=on is enabled
> 
> Hi
> 
> On Tue, Apr 29, 2025 at 10:13 AM Vivek Kasireddy
> <vivek.kasireddy@intel.com> wrote:
> >
> > 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 <kraxel@redhat.com>
> > Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
> > Cc: Dmitry Osipenko <dmitry.osipenko@collabora.com>
> > Cc: Frediano Ziglio <freddy77@gmail.com>
> > Cc: Dongwon Kim <dongwon.kim@intel.com>
> > Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
> > ---
> >  ui/spice-display.c | 63
> +++++++++++++++++++++++++++++++++++++++++++++-
> >  1 file changed, 62 insertions(+), 1 deletion(-)
> >
> > diff --git a/ui/spice-display.c b/ui/spice-display.c
> > index 2c4daa0707..047d453a0b 100644
> > --- a/ui/spice-display.c
> > +++ b/ui/spice-display.c
> > @@ -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;
> > +    }
> 
> 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.

> 
> > +
> > +    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;
> > +        }
> > +    }
> 
> 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

> 
> 
> > +    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),
> > --
> > 2.49.0
> >
> >
> 
> 
> --
> Marc-André Lureau