diff mbox series

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

Message ID 20250325234115.1332883-6-vivek.kasireddy@intel.com (mailing list archive)
State New
Headers show
Series ui/spice: Enable gl=on option for non-local or remote clients | expand

Commit Message

Kasireddy, Vivek March 25, 2025, 11:39 p.m. UTC
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 mbox series

Patch

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