aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVivek Kasireddy <vivek.kasireddy@intel.com>2025-06-16 21:32:30 -0700
committerMarc-André Lureau <marcandre.lureau@redhat.com>2025-07-15 10:22:33 +0400
commit2103690b1a7d98f88f7c150f48fcd951d3ee8b36 (patch)
tree5bc89ebf0ce9b8e18c1d37be90074c7ac04faaf7
parente6f0fe8f7c058af6e95e8f845c25e90453b2aec6 (diff)
downloadqemu-2103690b1a7d98f88f7c150f48fcd951d3ee8b36.zip
qemu-2103690b1a7d98f88f7c150f48fcd951d3ee8b36.tar.gz
qemu-2103690b1a7d98f88f7c150f48fcd951d3ee8b36.tar.bz2
ui/spice: Create a new texture with linear layout when gl=on is specified
Since 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. 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> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Co-developed-by: Michael Scherle <michael.scherle@rz.uni-freiburg.de> Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com> Reviewed-by: Dmitry Osipenko <dmitry.osipenko@collabora.com> Message-Id: <20250617043546.1022779-7-vivek.kasireddy@intel.com>
-rw-r--r--include/ui/surface.h1
-rw-r--r--ui/console-gl.c6
-rw-r--r--ui/spice-display.c82
3 files changed, 89 insertions, 0 deletions
diff --git a/include/ui/surface.h b/include/ui/surface.h
index f16f7be..006b198 100644
--- a/include/ui/surface.h
+++ b/include/ui/surface.h
@@ -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;
diff --git a/ui/console-gl.c b/ui/console-gl.c
index afb36db..403fc36 100644
--- a/ui/console-gl.c
+++ b/ui/console-gl.c
@@ -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,
diff --git a/ui/spice-display.c b/ui/spice-display.c
index e409b6b..854a97c 100644
--- a/ui/spice-display.c
+++ b/ui/spice-display.c
@@ -893,6 +893,81 @@ static void spice_gl_update(DisplayChangeListener *dcl,
ssd->gl_updates++;
}
+static bool spice_gl_replace_fd_texture(SimpleSpiceDisplay *ssd,
+ int *fds, uint64_t *modifier,
+ int *num_planes)
+{
+ uint32_t offsets[DMABUF_MAX_PLANES], strides[DMABUF_MAX_PLANES];
+ GLuint texture;
+ GLuint mem_obj;
+ int fourcc;
+ bool ret;
+
+ if (!spice_remote_client) {
+ return true;
+ }
+
+ if (*modifier == DRM_FORMAT_MOD_LINEAR) {
+ return true;
+ }
+
+ if (*num_planes > 1) {
+ error_report("spice: cannot replace texture with multiple planes");
+ return false;
+ }
+
+ /*
+ * We really want to ensure that the memory layout of the texture
+ * is linear; otherwise, the encoder's output may show corruption.
+ */
+ if (!surface_gl_create_texture_from_fd(ssd->ds, fds[0], &texture,
+ &mem_obj)) {
+ error_report("spice: cannot create new texture from fd");
+ return false;
+ }
+
+ /*
+ * 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.
+ */
+ ret = egl_dmabuf_export_texture(texture,
+ fds,
+ (EGLint *)offsets,
+ (EGLint *)strides,
+ &fourcc,
+ num_planes,
+ modifier);
+ if (!ret) {
+ glDeleteTextures(1, &texture);
+#ifdef GL_EXT_memory_object_fd
+ glDeleteMemoryObjectsEXT(1, &mem_obj);
+#endif
+
+ /*
+ * Since we couldn't export our newly create texture (or create,
+ * an fd associated with it) we need to backtrack and try to
+ * recreate the fd associated with the original texture.
+ */
+ ret = egl_dmabuf_export_texture(ssd->ds->texture,
+ fds,
+ (EGLint *)offsets,
+ (EGLint *)strides,
+ &fourcc,
+ num_planes,
+ modifier);
+ if (!ret) {
+ surface_gl_destroy_texture(ssd->gls, ssd->ds);
+ warn_report("spice: no texture available to display");
+ }
+ } else {
+ surface_gl_destroy_texture(ssd->gls, ssd->ds);
+ ssd->ds->texture = texture;
+ ssd->ds->mem_obj = mem_obj;
+ }
+ return ret;
+}
+
static void spice_server_gl_scanout(QXLInstance *qxl,
const int *fd,
uint32_t width, uint32_t height,
@@ -917,6 +992,7 @@ static void spice_gl_switch(DisplayChangeListener *dcl,
struct DisplaySurface *new_surface)
{
SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
+ bool ret;
if (ssd->ds) {
surface_gl_destroy_texture(ssd->gls, ssd->ds);
@@ -939,6 +1015,12 @@ static void spice_gl_switch(DisplayChangeListener *dcl,
return;
}
+ ret = spice_gl_replace_fd_texture(ssd, fd, &modifier, &num_planes);
+ 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),