Skip to contentSkip to conversation

Various virtio-gpu/dmabuf related fixes

PATCH In review Marc-André Lureau 2 participants 34 messages Sep 10

lore
Share & tools
lore
#0 lore raw

v4 cover Various virtio-gpu/dmabuf related fixes

Hi,

While working on CVE-2026-66020 (which had a few revision on list), I kept finding several places where virtio-gpu scanout and dmabuf ownership was unclear.

The CVE comes from using a blob after RESOURCE_DETACH_BACKING has unmapped its memory. Following that path also found stale dmabuf references, duplicated scanout bookkeeping, and inconsistent cleanup between display backends.

Those changes touch a lot of area and I have done basic testing with the various backends. I would not recommend backporting them all to stable. The first 2 patches address CVE-2026-66020 in the general case and should be safe to backport.

thanks

---
Changes in v4:
- replaced "ui: make GL context current before releasing DMA-BUF
textures" with more complete "ui: make GL context current in display
backend callbacks"
- rebase, drop eof line leftover, collect rb trailer
- Link to v3: https://lore.kernel.org/qemu-devel/[email protected]

Changes in v3:
- updated "virtio-gpu: fix use-after-free on blob cursor", now "clear res->blob on mapping cleanup"
- always disable GL when clearning scanout in "virtio-gpu: consolidate disabling scanout"
- drop "vhost-user-gpu: release DMA-BUFs on scanout disable" for now
- rebased, collect rb trailers
- Link to v2: https://lore.kernel.org/qemu-devel/[email protected]

Changes in v2: addresses Akihiko review
- reorder to have CVE fix first, easily back-portable (but also more
limited fix)
- include virtio_gpu_{simple,virgl}_resource_new() patch, and other sent
separately
- move draw_submitted from QemuDmaBuf to VirtualGfxConsole
- contrib/vhost-user-gpu: disable scanouts on resource unref
- drop rfc, collect rb, adjust commit messages
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]

---
Marc-André Lureau (24):
virtio-gpu: disable blob scanouts on mapping cleanup
virtio-gpu: clear res->blob on mapping cleanup hw/display/virtio-gpu: introduce virtio_gpu_{simple,virgl}_resource_new() virtio-gpu/virgl: free BHs and timers on renderer reset hw/display/virtio-gpu: set share_handle for udmabuf blob resources ui/gtk: move GL fence tracking from QemuDmaBuf to VirtualGfxConsole ui/gtk: move draw_submitted from QemuDmaBuf to VirtualGfxConsole ui/dbus: disable scanout iff dmabuf is current
virtio-gpu: store the scanout DMABUF in virtio_gpu_scanout ui/console: disable GL scanout when dmabuf is the active one ui/sdl2: clear guest_fb.dmabuf on release
ui/dmabuf: own and close fds on free
ui: make GL context current in display backend callbacks
virtio-gpu: release the dmabuf when a scanout is disabled contrib/vhost-user-gpu: drop scanout_bitmask bookkeeping
virtio-gpu-rutabaga: fix scanout handling
virtio-gpu: drop scanout_bitmask bookkeeping
virtio-gpu: release dmabuf when switching to a surface virtio-gpu/virgl: release dmabuf when a texture replaces a scanout
virtio-gpu: consolidate disabling scanout
virtio-gpu: minor refactoring
virtio-gpu/virgl: disable scanouts on resource unref
virtio-gpu: extract virtio_gpu_disable_scanout_for_resource helper contrib/vhost-user-gpu: disable scanouts on resource unref

 contrib/vhost-user-gpu/vhost-user-gpu.c |  28 ++----
 contrib/vhost-user-gpu/virgl.c          |  12 +++
 hw/display/vhost-user-gpu.c             |   1 -
 hw/display/virtio-gpu-rutabaga.c        |  38 ++++----
 hw/display/virtio-gpu-udmabuf-stubs.c   |   2 +-
 hw/display/virtio-gpu-udmabuf.c         |  89 ++++++-------------
 hw/display/virtio-gpu-virgl.c           | 135 +++++++++++++++++-----------
 hw/display/virtio-gpu.c                 | 150 +++++++++++++++++++++-----------
 hw/vfio/display.c                       |   1 -
 include/hw/virtio/virtio-gpu.h          |  31 ++++---
 include/ui/dmabuf.h                     |   7 --
 include/ui/egl-helpers.h                |   4 +-
 include/ui/gtk.h                        |  14 ++-
 ui/console.c                            |   3 +
 ui/dbus-listener.c                      |  69 ++++++++++-----
 ui/dmabuf.c                             |  65 ++------------
 ui/egl-headless.c                       |  17 ++++
 ui/egl-helpers.c                        |  15 ++--
 ui/gtk-egl.c                            |  55 +++++++-----
 ui/gtk-gl-area.c                        |  33 ++++---
 ui/gtk.c                                |  43 +++++----
 ui/sdl2-gl.c                            |   7 ++
 ui/spice-display.c                      |   2 +
 23 files changed, 452 insertions(+), 369 deletions(-)

---
base-commit: 1df256f5968e9f7c3c4533a1383b071c044a36d6
change-id: 20260825-virtio-gpu-cleanup-dfb2ba185936

Best regards,

Signature · 1 line
Marc-André Lureau <[email protected]>
#1 lore raw

PATCH v4 1/24 virtio-gpu: disable blob scanouts on mapping cleanup

When a blob resource backing is cleaned up (for ex via detach_backing), any scanouts referencing it must be disabled first to prevent the dmabuf from outliving its backing memory.

Fixes CVE-2026-66020
Fixes 32db3c63ae11 ("virtio-gpu: Add virtio_gpu_set_scanout_blob")
---
 hw/display/virtio-gpu.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
hw/display/virtio-gpu.c +10 -0
index 55a1c7f80fb8..5b63a292933a 100644
@@ -1022,6 +1022,16 @@ void virtio_gpu_cleanup_mapping_iov(VirtIOGPU *g,
 void virtio_gpu_cleanup_mapping(VirtIOGPU *g,
                                 struct virtio_gpu_simple_resource *res)
 {
+    if (res->blob) {
+        int i, max_outputs = g->parent_obj.conf.max_outputs;
+
+        for (i = 0; i < max_outputs; i++) {
+            if (g->parent_obj.scanout[i].resource_id == res->resource_id) {
+                virtio_gpu_disable_scanout(g, i);
+            }
+        }
+    }
+
     virtio_gpu_cleanup_mapping_iov(g, res->iov, res->iov_cnt);
     res->iov = NULL;
     res->iov_cnt = 0;
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#2 lore raw

PATCH v4 2/24 virtio-gpu: clear res->blob on mapping cleanup

In reply to Marc-André Lureau

d3c2da174ca6 ("hw/display/virtio-gpu: Check cursor data presence") already prevents the use-after-free on blob cursor after detach.

Clear res->blob in virtio_gpu_destroy_udmabuf() to keep init/fini symmetric, and add LOG_GUEST_ERROR messages in virtio_gpu_update_cursor_data() for missing or undersized blobs.

Fixes bdd53f739273 ("virtio-gpu: Update cursor data using blob")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3881

---
 hw/display/virtio-gpu-udmabuf.c |  1 +
 hw/display/virtio-gpu.c         | 12 ++++++++++--
 2 files changed, 11 insertions(+), 2 deletions(-)
hw/display/virtio-gpu-udmabuf.c +1 -0
index c230509852ff..03f0b29754e1 100644
@@ -92,6 +92,7 @@ static void virtio_gpu_destroy_udmabuf(struct virtio_gpu_simple_resource *res)
         close(res->dmabuf_fd);
         res->dmabuf_fd = -1;
     }
+    res->blob = NULL;
 }
 
 static int find_memory_backend_type(Object *obj, void *opaque)
hw/display/virtio-gpu.c +10 -2
index 5b63a292933a..3c3539e337fb 100644
@@ -61,8 +61,16 @@ void virtio_gpu_update_cursor_data(VirtIOGPU *g,
         }
         data = pixman_image_get_data(res->image);
     } else {
-        if (!res->iov || res->blob_size < (s->current_cursor->width *
-                                           s->current_cursor->height * 4)) {
+        if (!res->blob) {
+            qemu_log_mask(LOG_GUEST_ERROR, "%s: resource %d has no blob\n",
+                          __func__, resource_id);
+            return;
+        }
+        if (res->blob_size < (s->current_cursor->width *
+                              s->current_cursor->height * 4)) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: blob size too small for resource %d\n",
+                          __func__, resource_id);
             return;
         }
         data = res->blob;
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#3 lore raw

PATCH v4 3/24 hw/display/virtio-gpu: introduce virtio_gpu_{simple,virgl}_resource_new()

In reply to Marc-André Lureau

All allocation sites used g_new0() which zero-initializes fields. However share_handle must be SHAREABLE_NONE (-1 on Unix, NULL on Windows) and dmabuf_fd must be -1, not 0. Centralize allocation and field initialization in new constructors fix this and reduce code duplication.

This fixes -display dbus with virtio-gpu blob resources. The other end is currently receiving qemu fd 0.

Fixes 5f899c34af1d (“virtio-gpu: allocate shareable 2d resources on !win32”)
---
 hw/display/virtio-gpu-rutabaga.c | 21 +++----------
 hw/display/virtio-gpu-virgl.c    | 48 +++++++++++++++++++----------
 hw/display/virtio-gpu.c          | 66 +++++++++++++++++++++++++++++-----------
 include/hw/virtio/virtio-gpu.h   | 11 +++++++
 4 files changed, 97 insertions(+), 49 deletions(-)
hw/display/virtio-gpu-rutabaga.c +5 -16
index 041216a10d04..b8f8a2068d0b 100644
@@ -100,12 +100,8 @@ rutabaga_cmd_create_resource_2d(VirtIOGPU *g,
     result = rutabaga_resource_create_3d(vr->rutabaga, c2d.resource_id, &rc_3d);
     CHECK(!result, cmd);
 
-    res = g_new0(struct virtio_gpu_simple_resource, 1);
-    res->width = c2d.width;
-    res->height = c2d.height;
-    res->format = c2d.format;
-    res->resource_id = c2d.resource_id;
-
+    res = virtio_gpu_simple_resource_new(c2d.resource_id, c2d.width,
+                                         c2d.height, c2d.format);
     QTAILQ_INSERT_HEAD(&g->reslist, res, next);
 }
 
@@ -139,12 +135,8 @@ rutabaga_cmd_create_resource_3d(VirtIOGPU *g,
     result = rutabaga_resource_create_3d(vr->rutabaga, c3d.resource_id, &rc_3d);
     CHECK(!result, cmd);
 
-    res = g_new0(struct virtio_gpu_simple_resource, 1);
-    res->width = c3d.width;
-    res->height = c3d.height;
-    res->format = c3d.format;
-    res->resource_id = c3d.resource_id;
-
+    res = virtio_gpu_simple_resource_new(c3d.resource_id, c3d.width,
+                                         c3d.height, c3d.format);
     QTAILQ_INSERT_HEAD(&g->reslist, res, next);
 }
 
@@ -634,10 +626,7 @@ rutabaga_cmd_resource_create_blob(VirtIOGPU *g,
 
     CHECK(cblob.resource_id != 0, cmd);
 
-    res = g_new0(struct virtio_gpu_simple_resource, 1);
-
-    res->resource_id = cblob.resource_id;
-    res->blob_size = cblob.size;
+    res = virtio_gpu_simple_resource_new_blob(cblob.resource_id, cblob.size);
 
     if (cblob.blob_mem != VIRTIO_GPU_BLOB_MEM_HOST3D) {
         result = virtio_gpu_create_mapping_iov(g, cblob.nr_entries,
hw/display/virtio-gpu-virgl.c +32 -16

72 lines not on this page · raw message

hw/display/virtio-gpu.c +49 -17

106 lines not on this page · raw message

include/hw/virtio/virtio-gpu.h +11 -0
index 69b5ee2e382f..8c0df60b3b7e 100644
@@ -64,6 +64,17 @@ struct virtio_gpu_simple_resource {
     QTAILQ_ENTRY(virtio_gpu_simple_resource) next;
 };
 
+void
+virtio_gpu_simple_resource_init(struct virtio_gpu_simple_resource *res,
+                               uint32_t resource_id);
+
+struct virtio_gpu_simple_resource *
+virtio_gpu_simple_resource_new(uint32_t resource_id, uint32_t width,
+                               uint32_t height, uint32_t format);
+
+struct virtio_gpu_simple_resource *
+virtio_gpu_simple_resource_new_blob(uint32_t resource_id, uint64_t blob_size);
+
 struct virtio_gpu_framebuffer {
     pixman_format_code_t format;
     uint32_t width, height;
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#4 lore raw

PATCH v4 4/24 virtio-gpu/virgl: free BHs and timers on renderer reset

In reply to Marc-André Lureau

virtio_gpu_virgl_reset() does not free the bottom halves or timers allocated by the previous virtio_gpu_virgl_init() call. A subsequent virtio_gpu_virgl_init() overwrites the old pointers, leaking the previous allocations.

Fixes 7e688d1bf515 ("virtio-gpu: Handle virtio_gpu_virgl_init() failure")
---
 hw/display/virtio-gpu-virgl.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/display/virtio-gpu-virgl.c +6 -0
index 742d9dde4a32..7f1d1e0c3276 100644
@@ -1430,6 +1430,7 @@ void virtio_gpu_virgl_reset_scanout(VirtIOGPU *g)
 
 static bool virtio_gpu_virgl_reset(VirtIOGPU *g)
 {
+    VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
     struct virtio_gpu_simple_resource *res, *tmp;
 
     /*
@@ -1448,6 +1449,11 @@ static bool virtio_gpu_virgl_reset(VirtIOGPU *g)
 
     virtio_gpu_virgl_reset_async_fences(g);
 
+    g_clear_pointer(&gl->cmdq_resume_bh, qemu_bh_delete);
+    g_clear_pointer(&gl->async_fence_bh, qemu_bh_delete);
+    g_clear_pointer(&gl->print_stats, timer_free);
+    g_clear_pointer(&gl->fence_poll, timer_free);
+
     return true;
 }
 
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#5 lore raw

PATCH v4 5/24 hw/display/virtio-gpu: set share_handle for udmabuf blob resources

In reply to Marc-André Lureau

Set share_handle to the dmabuf fd after successful udmabuf creation, and reset it to SHAREABLE_NONE on teardown.

Fixes 5f899c34af1d ("virtio-gpu: allocate shareable 2d resources on !win32")
---
 hw/display/virtio-gpu-udmabuf.c | 3 +++
 1 file changed, 3 insertions(+)
hw/display/virtio-gpu-udmabuf.c +3 -0
index 03f0b29754e1..fc63f2c4ec21 100644
@@ -91,6 +91,7 @@ static void virtio_gpu_destroy_udmabuf(struct virtio_gpu_simple_resource *res)
     if (res->dmabuf_fd >= 0) {
         close(res->dmabuf_fd);
         res->dmabuf_fd = -1;
+        res->share_handle = SHAREABLE_NONE;
     }
     res->blob = NULL;
 }
@@ -150,6 +151,7 @@ bool virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
             virtio_gpu_destroy_udmabuf(res);
             return false;
         }
+        res->share_handle = res->dmabuf_fd;
         pdata = res->remapped;
     }
 
@@ -183,6 +185,7 @@ void virtio_gpu_fini_udmabuf(VirtIOGPU *g, struct virtio_gpu_simple_resource *re
             res->dmabuf_fd != -1) {
             qemu_dmabuf_close(dmabuf->buf);
             res->dmabuf_fd = -1;
+            res->share_handle = SHAREABLE_NONE;
         }
     }
 
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#6 lore raw

PATCH v4 6/24 ui/gtk: move GL fence tracking from QemuDmaBuf to VirtualGfxConsole

In reply to Marc-André Lureau

The sync and fence_fd fields on QemuDmaBuf are only used by the GTK display backend for GPU fence synchronization during draw. They are not intrinsic properties of the DMA-BUF itself but transient GL state tied to the display listener.

Move fence_fd to VirtualGfxConsole and keep the EGL sync local to the draw operation. Change egl_dmabuf_create_sync() and egl_dmabuf_create_fence() to return values instead of storing them on the dmabuf. This makes the fence lifecycle independent of the dmabuf lifetime.

---
 include/ui/dmabuf.h      |  4 ----
 include/ui/egl-helpers.h |  4 ++--
 include/ui/gtk.h         |  8 +++++---
 ui/dmabuf.c              | 29 -----------------------------
 ui/egl-helpers.c         | 15 ++++++++-------
 ui/gtk-egl.c             | 30 ++++++++++++++++--------------
 ui/gtk-gl-area.c         | 12 +++---------
 ui/gtk.c                 | 31 +++++++++++++++++++++++--------
 8 files changed, 57 insertions(+), 76 deletions(-)
include/ui/dmabuf.h +0 -4
index 3decdca4979c..381583bd536c 100644
@@ -41,13 +41,9 @@ uint32_t qemu_dmabuf_get_y(QemuDmaBuf *dmabuf);
 uint32_t qemu_dmabuf_get_backing_width(QemuDmaBuf *dmabuf);
 uint32_t qemu_dmabuf_get_backing_height(QemuDmaBuf *dmabuf);
 bool qemu_dmabuf_get_y0_top(QemuDmaBuf *dmabuf);
-void *qemu_dmabuf_get_sync(QemuDmaBuf *dmabuf);
-int32_t qemu_dmabuf_get_fence_fd(QemuDmaBuf *dmabuf);
 bool qemu_dmabuf_get_allow_fences(QemuDmaBuf *dmabuf);
 bool qemu_dmabuf_get_draw_submitted(QemuDmaBuf *dmabuf);
 void qemu_dmabuf_set_texture(QemuDmaBuf *dmabuf, uint32_t texture);
-void qemu_dmabuf_set_fence_fd(QemuDmaBuf *dmabuf, int32_t fence_fd);
-void qemu_dmabuf_set_sync(QemuDmaBuf *dmabuf, void *sync);
 void qemu_dmabuf_set_draw_submitted(QemuDmaBuf *dmabuf, bool draw_submitted);
 
 #endif
include/ui/egl-helpers.h +2 -2
index 679c79eacd54..533d9afc37ba 100644
@@ -54,8 +54,8 @@ bool egl_dmabuf_export_texture(uint32_t tex_id, int *fd, EGLint *offset,
 
 void egl_dmabuf_import_texture(QemuDmaBuf *dmabuf);
 void egl_dmabuf_release_texture(QemuDmaBuf *dmabuf);
-void egl_dmabuf_create_sync(QemuDmaBuf *dmabuf);
-void egl_dmabuf_create_fence(QemuDmaBuf *dmabuf);
+EGLSyncKHR egl_create_sync(void);
+int egl_create_fence(EGLSyncKHR sync);
 
 #endif
 
include/ui/gtk.h +5 -3
index 5c9c34a069ea..4b4ef50e3701 100644
@@ -58,6 +58,7 @@ typedef struct VirtualGfxConsole {
     bool y0_top;
     bool scanout_mode;
     bool has_dmabuf;
+    int gl_fence_fd;
 #endif
 } VirtualGfxConsole;
 
@@ -160,7 +161,7 @@ extern bool gtk_use_gl_area;
 /* ui/gtk.c */
 void gd_update_windowsize(VirtualConsole *vc);
 void gd_update_monitor_refresh_rate(VirtualConsole *vc, GtkWidget *widget);
-void gd_hw_gl_flushed(void *vc);
+void gd_gl_wait_sync(VirtualConsole *vc, void *sync);
 
 /* ui/gtk-egl.c */
 void gd_egl_init(VirtualConsole *vc);
@@ -190,8 +191,9 @@ void gd_egl_cursor_position(DisplayChangeListener *dcl,
                             uint32_t pos_x, uint32_t pos_y);
 void gd_egl_flush(DisplayChangeListener *dcl,
                   uint32_t x, uint32_t y, uint32_t w, uint32_t h);
-void gd_egl_scanout_flush(DisplayChangeListener *dcl,
-                          uint32_t x, uint32_t y, uint32_t w, uint32_t h);
+void *gd_egl_scanout_flush(DisplayChangeListener *dcl,
+                           uint32_t x, uint32_t y,
+                           uint32_t w, uint32_t h);
 void gtk_egl_init(DisplayGLMode mode);
 int gd_egl_make_current(DisplayGLCtx *dgc,
                         QEMUGLContext ctx);
ui/dmabuf.c +0 -29
index 7433a268f0df..b61dc7ad2783 100644
@@ -25,8 +25,6 @@ struct QemuDmaBuf {
     uint32_t  backing_width;
     uint32_t  backing_height;
     bool      y0_top;
-    void      *sync;
-    int       fence_fd;
     bool      allow_fences;
     bool      draw_submitted;
 };
@@ -57,7 +55,6 @@ QemuDmaBuf *qemu_dmabuf_new(uint32_t width, uint32_t height,
     memcpy(dmabuf->fd, dmabuf_fd, num_planes * sizeof(*dmabuf_fd));
     dmabuf->allow_fences = allow_fences;
     dmabuf->y0_top = y0_top;
-    dmabuf->fence_fd = -1;
     dmabuf->num_planes = num_planes;
 
     return dmabuf;
@@ -208,20 +205,6 @@ bool qemu_dmabuf_get_y0_top(QemuDmaBuf *dmabuf)
     return dmabuf->y0_top;
 }
 
-void *qemu_dmabuf_get_sync(QemuDmaBuf *dmabuf)
-{
-    assert(dmabuf != NULL);
-
-    return dmabuf->sync;
-}
-
-int32_t qemu_dmabuf_get_fence_fd(QemuDmaBuf *dmabuf)
-{
-    assert(dmabuf != NULL);
-
-    return dmabuf->fence_fd;
-}
-
 bool qemu_dmabuf_get_allow_fences(QemuDmaBuf *dmabuf)
 {
     assert(dmabuf != NULL);
@@ -242,18 +225,6 @@ void qemu_dmabuf_set_texture(QemuDmaBuf *dmabuf, uint32_t texture)
     dmabuf->texture = texture;
 }
 
-void qemu_dmabuf_set_fence_fd(QemuDmaBuf *dmabuf, int32_t fence_fd)
-{
-    assert(dmabuf != NULL);
-    dmabuf->fence_fd = fence_fd;
-}
-
-void qemu_dmabuf_set_sync(QemuDmaBuf *dmabuf, void *sync)
-{
-    assert(dmabuf != NULL);
-    dmabuf->sync = sync;
-}
-
 void qemu_dmabuf_set_draw_submitted(QemuDmaBuf *dmabuf, bool draw_submitted)
 {
     assert(dmabuf != NULL);
ui/egl-helpers.c +8 -7
index 8e3729a54d15..cb9c1e839cf9 100644
@@ -432,7 +432,7 @@ void egl_dmabuf_release_texture(QemuDmaBuf *dmabuf)
     qemu_dmabuf_set_texture(dmabuf, 0);
 }
 
-void egl_dmabuf_create_sync(QemuDmaBuf *dmabuf)
+EGLSyncKHR egl_create_sync(void)
 {
     EGLSyncKHR sync;
 
@@ -443,23 +443,24 @@ void egl_dmabuf_create_sync(QemuDmaBuf *dmabuf)
         sync = eglCreateSyncKHR(qemu_egl_display,
                                 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
         if (sync != EGL_NO_SYNC_KHR) {
-            qemu_dmabuf_set_sync(dmabuf, sync);
+            return sync;
         }
     }
+
+    return NULL;
 }
 
-void egl_dmabuf_create_fence(QemuDmaBuf *dmabuf)
+int egl_create_fence(EGLSyncKHR sync)
 {
-    void *sync = qemu_dmabuf_get_sync(dmabuf);
-    int fence_fd;
+    int fence_fd = -1;
 
     if (sync) {
         fence_fd = eglDupNativeFenceFDANDROID(qemu_egl_display,
                                               sync);
-        qemu_dmabuf_set_fence_fd(dmabuf, fence_fd);
         eglDestroySyncKHR(qemu_egl_display, sync);
-        qemu_dmabuf_set_sync(dmabuf, NULL);
     }
+
+    return fence_fd;
 }
 
 #endif /* CONFIG_GBM */
ui/gtk-egl.c +16 -14
index adc81f34b1c8..c671aabd11b3 100644
@@ -68,7 +68,7 @@ void gd_egl_draw(VirtualConsole *vc)
     GdkWindow *window;
 #ifdef CONFIG_GBM
     QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
-    int fence_fd;
+    EGLSyncKHR sync;
 #endif
     int ww, wh, pw, ph, gs;
 
@@ -94,7 +94,11 @@ void gd_egl_draw(VirtualConsole *vc)
             qemu_console_hw_gl_block(vc->gfx.dcl.con, true);
         }
 #endif
-        gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0, vc->gfx.w, vc->gfx.h);
+#ifdef CONFIG_GBM
+        sync =
+#endif
+            gd_egl_scanout_flush(&vc->gfx.dcl, 0, 0,
+                                 vc->gfx.w, vc->gfx.h);
 
         gd_update_scale(vc, ww, wh,
                         surface_width(vc->gfx.ds),
@@ -103,13 +107,7 @@ void gd_egl_draw(VirtualConsole *vc)
         glFlush();
 #ifdef CONFIG_GBM
         if (dmabuf) {
-            egl_dmabuf_create_fence(dmabuf);
-            fence_fd = qemu_dmabuf_get_fence_fd(dmabuf);
-            if (fence_fd >= 0) {
-                qemu_set_fd_handler(fence_fd, gd_hw_gl_flushed, NULL, vc);
-                return;
-            }
-            qemu_console_hw_gl_block(vc->gfx.dcl.con, false);
+            gd_gl_wait_sync(vc, sync);
         }
 #endif
     } else {
@@ -330,10 +328,12 @@ void gd_egl_cursor_position(DisplayChangeListener *dcl,
     vc->gfx.cursor_y = pos_y * vc->gfx.scale_y;
 }
 
-void gd_egl_scanout_flush(DisplayChangeListener *dcl,
-                          uint32_t x, uint32_t y, uint32_t w, uint32_t h)
+EGLSyncKHR gd_egl_scanout_flush(DisplayChangeListener *dcl,
+                                uint32_t x, uint32_t y,
+                                uint32_t w, uint32_t h)
 {
     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
+    EGLSyncKHR sync = EGL_NO_SYNC_KHR;
     GdkWindow *window;
     int px_offset, py_offset;
     int gs;
@@ -342,10 +342,10 @@ void gd_egl_scanout_flush(DisplayChangeListener *dcl,
     int fbw, fbh;
 
     if (!vc->gfx.scanout_mode) {
-        return;
+        return sync;
     }
     if (!vc->gfx.guest_fb.framebuffer) {
-        return;
+        return sync;
     }
 
     eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
@@ -391,11 +391,13 @@ void gd_egl_scanout_flush(DisplayChangeListener *dcl,
 
 #ifdef CONFIG_GBM
     if (vc->gfx.guest_fb.dmabuf) {
-        egl_dmabuf_create_sync(vc->gfx.guest_fb.dmabuf);
+        sync = egl_create_sync();
     }
 #endif
 
     eglSwapBuffers(qemu_egl_display, vc->gfx.esurface);
+
+    return sync;
 }
 
 void gd_egl_flush(DisplayChangeListener *dcl,
ui/gtk-gl-area.c +3 -9
index 29497019ee46..29d1b1e4116f 100644
@@ -41,6 +41,7 @@ void gd_gl_area_draw(VirtualConsole *vc)
 {
 #ifdef CONFIG_GBM
     QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
+    EGLSyncKHR sync = EGL_NO_SYNC_KHR;
 #endif
     int pw, ph, gs, y1, y2;
     int ww, wh;
@@ -119,20 +120,13 @@ void gd_gl_area_draw(VirtualConsole *vc)
                           GL_COLOR_BUFFER_BIT, GL_NEAREST);
 #ifdef CONFIG_GBM
         if (dmabuf) {
-            egl_dmabuf_create_sync(dmabuf);
+            sync = egl_create_sync();
         }
 #endif
         glFlush();
 #ifdef CONFIG_GBM
         if (dmabuf) {
-            int fence_fd;
-            egl_dmabuf_create_fence(dmabuf);
-            fence_fd = qemu_dmabuf_get_fence_fd(dmabuf);
-            if (fence_fd >= 0) {
-                qemu_set_fd_handler(fence_fd, gd_hw_gl_flushed, NULL, vc);
-                return;
-            }
-            qemu_console_hw_gl_block(vc->gfx.dcl.con, false);
+            gd_gl_wait_sync(vc, sync);
         }
 #endif
     } else {
ui/gtk.c +23 -8
index 7078d89d6797..1c75b9ea3c4d 100644
@@ -597,21 +597,32 @@ static void gd_gl_release_dmabuf(DisplayChangeListener *dcl,
 #endif
 }
 
-void gd_hw_gl_flushed(void *vcon)
+#ifdef CONFIG_GBM
+static void gd_gl_fence_cb(void *vcon)
 {
     VirtualConsole *vc = vcon;
-    QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
-    int fence_fd;
 
-    fence_fd = qemu_dmabuf_get_fence_fd(dmabuf);
-    if (fence_fd >= 0) {
-        qemu_set_fd_handler(fence_fd, NULL, NULL, NULL);
-        close(fence_fd);
-        qemu_dmabuf_set_fence_fd(dmabuf, -1);
+    if (vc->gfx.gl_fence_fd >= 0) {
+        qemu_set_fd_handler(vc->gfx.gl_fence_fd, NULL, NULL, NULL);
+        g_clear_fd(&vc->gfx.gl_fence_fd, NULL);
         qemu_console_hw_gl_block(vc->gfx.dcl.con, false);
     }
 }
 
+void gd_gl_wait_sync(VirtualConsole *vc, EGLSyncKHR sync)
+{
+    assert(vc->gfx.gl_fence_fd < 0);
+
+    vc->gfx.gl_fence_fd = egl_create_fence(sync);
+    if (vc->gfx.gl_fence_fd >= 0) {
+        qemu_set_fd_handler(vc->gfx.gl_fence_fd,
+                            gd_gl_fence_cb, NULL, vc);
+    } else {
+        qemu_console_hw_gl_block(vc->gfx.dcl.con, false);
+    }
+}
+#endif
+
 /** DisplayState Callbacks (opengl version) **/
 
 static const DisplayChangeListenerOps dcl_gl_area_ops = {
@@ -2355,6 +2366,7 @@ add_gfx_console(GtkDisplayState *s, QemuConsole *con)
     vc->gfx.scale_y = vc->gfx.preferred_scale;
 
 #if defined(CONFIG_OPENGL)
+    vc->gfx.gl_fence_fd = -1;
     if (display_opengl) {
         if (gtk_use_gl_area) {
             vc->gfx.drawing_area = gtk_gl_area_new();
@@ -2647,6 +2659,9 @@ static void gd_vc_free(void *p)
 
     switch (vc->type) {
     case GD_VC_GFX:
+#if defined(CONFIG_OPENGL) && defined(CONFIG_GBM)
+        gd_gl_fence_cb(vc);
+#endif
         qemu_console_unregister_listener(&vc->gfx.dcl);
 #if defined(CONFIG_OPENGL)
         if (display_opengl) {
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#7 lore raw

PATCH v4 7/24 ui/gtk: move draw_submitted from QemuDmaBuf to VirtualGfxConsole

In reply to Marc-André Lureau

The draw_submitted flag on QemuDmaBuf is only used by the GTK display backend to gate the draw pipeline and prevent gl_block refcount imbalance when the guest submits frames faster than GTK draws them. It is not an intrinsic property of the DMA-BUF but transient display listener state.

Move it to VirtualGfxConsole, replacing the getter/setter calls with direct struct member access. Reset the flag when the active dmabuf is released in gd_gl_release_dmabuf().

---
 include/ui/dmabuf.h |  2 --
 include/ui/gtk.h    |  1 +
 ui/dmabuf.c         | 14 --------------
 ui/gtk-egl.c        | 13 ++++++-------
 ui/gtk-gl-area.c    | 10 +++++-----
 ui/gtk.c            |  1 +
 6 files changed, 13 insertions(+), 28 deletions(-)
include/ui/dmabuf.h +0 -2
index 381583bd536c..b972099859f2 100644
@@ -42,8 +42,6 @@ uint32_t qemu_dmabuf_get_backing_width(QemuDmaBuf *dmabuf);
 uint32_t qemu_dmabuf_get_backing_height(QemuDmaBuf *dmabuf);
 bool qemu_dmabuf_get_y0_top(QemuDmaBuf *dmabuf);
 bool qemu_dmabuf_get_allow_fences(QemuDmaBuf *dmabuf);
-bool qemu_dmabuf_get_draw_submitted(QemuDmaBuf *dmabuf);
 void qemu_dmabuf_set_texture(QemuDmaBuf *dmabuf, uint32_t texture);
-void qemu_dmabuf_set_draw_submitted(QemuDmaBuf *dmabuf, bool draw_submitted);
 
 #endif
include/ui/gtk.h +1 -0
index 4b4ef50e3701..b66b9ca5c519 100644
@@ -59,6 +59,7 @@ typedef struct VirtualGfxConsole {
     bool scanout_mode;
     bool has_dmabuf;
     int gl_fence_fd;
+    bool draw_submitted;
 #endif
 } VirtualGfxConsole;
 
ui/dmabuf.c +0 -14
index b61dc7ad2783..495270f29bd6 100644
@@ -26,7 +26,6 @@ struct QemuDmaBuf {
     uint32_t  backing_height;
     bool      y0_top;
     bool      allow_fences;
-    bool      draw_submitted;
 };
 
 QemuDmaBuf *qemu_dmabuf_new(uint32_t width, uint32_t height,
@@ -212,21 +211,8 @@ bool qemu_dmabuf_get_allow_fences(QemuDmaBuf *dmabuf)
     return dmabuf->allow_fences;
 }
 
-bool qemu_dmabuf_get_draw_submitted(QemuDmaBuf *dmabuf)
-{
-    assert(dmabuf != NULL);
-
-    return dmabuf->draw_submitted;
-}
-
 void qemu_dmabuf_set_texture(QemuDmaBuf *dmabuf, uint32_t texture)
 {
     assert(dmabuf != NULL);
     dmabuf->texture = texture;
 }
-
-void qemu_dmabuf_set_draw_submitted(QemuDmaBuf *dmabuf, bool draw_submitted)
-{
-    assert(dmabuf != NULL);
-    dmabuf->draw_submitted = draw_submitted;
-}
ui/gtk-egl.c +6 -7
index c671aabd11b3..8623181336e9 100644
@@ -86,10 +86,10 @@ void gd_egl_draw(VirtualConsole *vc)
     if (vc->gfx.scanout_mode) {
 #ifdef CONFIG_GBM
         if (dmabuf) {
-            if (!qemu_dmabuf_get_draw_submitted(dmabuf)) {
+            if (!vc->gfx.draw_submitted) {
                 return;
             } else {
-                qemu_dmabuf_set_draw_submitted(dmabuf, false);
+                vc->gfx.draw_submitted = false;
             }
             qemu_console_hw_gl_block(vc->gfx.dcl.con, true);
         }
@@ -169,8 +169,7 @@ void gd_egl_refresh(DisplayChangeListener *dcl)
 #endif
     }
 
-    if (vc->gfx.guest_fb.dmabuf &&
-        qemu_dmabuf_get_draw_submitted(vc->gfx.guest_fb.dmabuf)) {
+    if (vc->gfx.guest_fb.dmabuf && vc->gfx.draw_submitted) {
         gd_egl_draw(vc);
         return;
     }
@@ -290,6 +289,7 @@ void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
 
     if (qemu_dmabuf_get_allow_fences(dmabuf)) {
         vc->gfx.guest_fb.dmabuf = dmabuf;
+        vc->gfx.draw_submitted = false;
     }
 #endif
 }
@@ -406,9 +406,8 @@ void gd_egl_flush(DisplayChangeListener *dcl,
     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
     GtkWidget *area = vc->gfx.drawing_area;
 
-    if (vc->gfx.guest_fb.dmabuf &&
-        !qemu_dmabuf_get_draw_submitted(vc->gfx.guest_fb.dmabuf)) {
-        qemu_dmabuf_set_draw_submitted(vc->gfx.guest_fb.dmabuf, true);
+    if (vc->gfx.guest_fb.dmabuf && !vc->gfx.draw_submitted) {
+        vc->gfx.draw_submitted = true;
         gtk_egl_set_scanout_mode(vc, true);
     }
 
ui/gtk-gl-area.c +5 -5
index 29d1b1e4116f..45c0c8c81a11 100644
@@ -82,10 +82,10 @@ void gd_gl_area_draw(VirtualConsole *vc)
 
 #ifdef CONFIG_GBM
         if (dmabuf) {
-            if (!qemu_dmabuf_get_draw_submitted(dmabuf)) {
+            if (!vc->gfx.draw_submitted) {
                 return;
             } else {
-                qemu_dmabuf_set_draw_submitted(dmabuf, false);
+                vc->gfx.draw_submitted = false;
             }
             qemu_console_hw_gl_block(vc->gfx.dcl.con, true);
         }
@@ -319,9 +319,8 @@ void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
 {
     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
 
-    if (vc->gfx.guest_fb.dmabuf &&
-        !qemu_dmabuf_get_draw_submitted(vc->gfx.guest_fb.dmabuf)) {
-        qemu_dmabuf_set_draw_submitted(vc->gfx.guest_fb.dmabuf, true);
+    if (vc->gfx.guest_fb.dmabuf && !vc->gfx.draw_submitted) {
+        vc->gfx.draw_submitted = true;
         gtk_gl_area_set_scanout_mode(vc, true);
     }
     gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
@@ -356,6 +355,7 @@ void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
 
     if (qemu_dmabuf_get_allow_fences(dmabuf)) {
         vc->gfx.guest_fb.dmabuf = dmabuf;
+        vc->gfx.draw_submitted = false;
     }
 #endif
 }
ui/gtk.c +1 -0
index 1c75b9ea3c4d..55e45e8445bc 100644
@@ -593,6 +593,7 @@ static void gd_gl_release_dmabuf(DisplayChangeListener *dcl,
     egl_dmabuf_release_texture(dmabuf);
     if (vc->gfx.guest_fb.dmabuf == dmabuf) {
         vc->gfx.guest_fb.dmabuf = NULL;
+        vc->gfx.draw_submitted = false;
     }
 #endif
 }
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#8 lore raw

PATCH v4 8/24 ui/dbus: disable scanout iff dmabuf is current

In reply to Marc-André Lureau

Narrows release_dmabuf to the active scanout, as the device & console may interleave new scanouts before releasing old ones.

Fixes 142ca628a733 ("ui: add a D-Bus display backend")
---
 ui/dbus-listener.c | 68 ++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 48 insertions(+), 20 deletions(-)
ui/dbus-listener.c +48 -20
index 2e2f6ba4183a..5a72c7eeae59 100644
@@ -91,6 +91,8 @@ struct _DBusDisplayListener {
     guint dbus_filter;
     guint32 display_serial_to_discard;
     guint32 cursor_serial_to_discard;
+
+    QemuDmaBuf *scanout_dmabuf;
 };
 
 G_DEFINE_TYPE(DBusDisplayListener, dbus_display_listener, G_TYPE_OBJECT)
@@ -119,6 +121,7 @@ static void dbus_scanout_disable(DisplayChangeListener *dcl)
 {
     DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
 
+    ddl->scanout_dmabuf = NULL;
     ddl_discard_display_messages(ddl);
 
     qemu_dbus_display1_listener_call_disable(
@@ -298,9 +301,9 @@ static void dbus_call_update_gl(DisplayChangeListener *dcl,
 
 #ifdef CONFIG_GBM
 static void dbus_scanout_dmabuf_v1(DBusDisplayListener *ddl,
-                                   QemuDmaBuf *dmabuf)
+                                   QemuDmaBuf *dmabuf,
+                                   GError **err)
 {
-    g_autoptr(GError) err = NULL;
     g_autoptr(GUnixFDList) fd_list = NULL;
     int fd;
     uint32_t width, height, stride, fourcc;
@@ -309,8 +312,7 @@ static void dbus_scanout_dmabuf_v1(DBusDisplayListener *ddl,
 
     fd = qemu_dmabuf_get_fds(dmabuf, NULL)[0];
     fd_list = g_unix_fd_list_new();
-    if (g_unix_fd_list_append(fd_list, fd, &err) != 0) {
-        error_report("Failed to setup dmabuf fdlist: %s", err->message);
+    if (g_unix_fd_list_append(fd_list, fd, err) != 0) {
         return;
     }
 
@@ -332,9 +334,9 @@ static void dbus_scanout_dmabuf_v1(DBusDisplayListener *ddl,
 }
 
 static void dbus_scanout_dmabuf_v2(DBusDisplayListener *ddl,
-                                   QemuDmaBuf *dmabuf)
+                                   QemuDmaBuf *dmabuf,
+                                   GError **err)
 {
-    g_autoptr(GError) err = NULL;
     g_autoptr(GUnixFDList) fd_list = NULL;
     int i, fd_index[DMABUF_MAX_PLANES], num_fds;
     uint32_t x, y, width, height, fourcc, backing_width, backing_height;
@@ -360,9 +362,8 @@ static void dbus_scanout_dmabuf_v2(DBusDisplayListener *ddl,
             break;
         }
 
-        fd_index[num_fds] = g_unix_fd_list_append(fd_list, plane_fd, &err);
+        fd_index[num_fds] = g_unix_fd_list_append(fd_list, plane_fd, err);
         if (fd_index[num_fds] < 0) {
-            error_report("Failed to setup dmabuf fdlist: %s", err->message);
             return;
         }
     }
@@ -395,20 +396,36 @@ static void dbus_scanout_dmabuf_v2(DBusDisplayListener *ddl,
         G_DBUS_CALL_FLAGS_NONE, -1, fd_list, NULL, NULL, NULL);
 }
 
+static bool dbus_call_scanout_dmabuf(DBusDisplayListener *ddl,
+                                     QemuDmaBuf *dmabuf)
+{
+    g_autoptr(GError) err = NULL;
+
+    if (ddl->scanout_dmabuf_v2_proxy) {
+        dbus_scanout_dmabuf_v2(ddl, dmabuf, &err);
+    } else {
+        if (qemu_dmabuf_get_num_planes(dmabuf) > 1) {
+            error_report("Peer does not support multi plane dmabuf");
+            return false;
+        }
+        dbus_scanout_dmabuf_v1(ddl, dmabuf, &err);
+    }
+
+    if (err) {
+        error_report("Failed to scanout dmabuf: %s", err->message);
+        return false;
+    }
+
+    return true;
+}
+
 static void dbus_scanout_dmabuf(DisplayChangeListener *dcl,
                                 QemuDmaBuf *dmabuf)
 {
     DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
 
-    if (ddl->scanout_dmabuf_v2_proxy) {
-        dbus_scanout_dmabuf_v2(ddl, dmabuf);
-    } else {
-        if (qemu_dmabuf_get_num_planes(dmabuf) > 1) {
-            g_debug("org.qemu.Display1.Listener.ScanoutDMABUF "
-                    "does not support mutli plane");
-            return;
-        }
-        dbus_scanout_dmabuf_v1(ddl, dmabuf);
+    if (dbus_call_scanout_dmabuf(ddl, dmabuf)) {
+        ddl->scanout_dmabuf = dmabuf;
     }
 }
 #endif /* GBM */
@@ -589,6 +606,10 @@ static void dbus_scanout_texture(DisplayChangeListener *dcl,
                                  uint32_t w, uint32_t h,
                                  void *d3d_tex2d)
 {
+#if defined(CONFIG_GBM) || defined(WIN32)
+    DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
+#endif
+
     trace_dbus_scanout_texture(tex_id, backing_y_0_top,
                                backing_width, backing_height, x, y, w, h);
 #ifdef CONFIG_GBM
@@ -607,13 +628,13 @@ static void dbus_scanout_texture(DisplayChangeListener *dcl,
                              backing_height, fourcc, modifier, fd, num_planes,
                              false, backing_y_0_top);
 
-    dbus_scanout_dmabuf(dcl, dmabuf);
+    if (dbus_call_scanout_dmabuf(ddl, dmabuf)) {
+        ddl->scanout_dmabuf = NULL;
+    }
     qemu_dmabuf_close(dmabuf);
 #endif
 
 #ifdef WIN32
-    DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
-
     /* there must be a matching gfx_switch before */
     assert(surface_width(ddl->ds) == w);
     assert(surface_height(ddl->ds) == h);
@@ -686,7 +707,14 @@ static void dbus_cursor_dmabuf(DisplayChangeListener *dcl,
 static void dbus_release_dmabuf(DisplayChangeListener *dcl,
                                 QemuDmaBuf *dmabuf)
 {
+    DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
+
+    if (ddl->scanout_dmabuf != dmabuf) {
+        return;
+    }
+
     dbus_scanout_disable(dcl);
+    ddl->scanout_dmabuf = NULL;
 }
 #endif /* GBM */
 
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#9 lore raw

PATCH v4 9/24 virtio-gpu: store the scanout DMABUF in virtio_gpu_scanout

In reply to Marc-André Lureau

VirtIOGPU tracks presented dmabufs both through a per-scanout primary pointer and a device-wide list of VGPUDMABuf wrappers. Only the current DMA-BUF for a scanout can ever be presented, so the list and the wrapper struct are redundant bookkeeping.

Store the QemuDmaBuf directly in virtio_gpu_scanout and drop VGPUDMABuf and the device-wide list. The wrapper only carried the buffer and its scanout id; the id is now implicit in the owning scanout.

No functional change intended.

---
 hw/display/virtio-gpu-udmabuf.c | 68 +++++++++++++++--------------------------
 include/hw/virtio/virtio-gpu.h  | 12 +-------
 2 files changed, 25 insertions(+), 55 deletions(-)
hw/display/virtio-gpu-udmabuf.c +24 -44
index fc63f2c4ec21..a38afb24c71f 100644
@@ -160,30 +160,19 @@ bool virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
     return true;
 }
 
-static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf)
-{
-    struct virtio_gpu_scanout *scanout;
-
-    scanout = &g->parent_obj.scanout[dmabuf->scanout_id];
-    qemu_console_gl_release_dmabuf(scanout->con, dmabuf->buf);
-    g_clear_pointer(&dmabuf->buf, qemu_dmabuf_free);
-    QTAILQ_REMOVE(&g->dmabuf.bufs, dmabuf, next);
-    g_free(dmabuf);
-}
-
 void virtio_gpu_fini_udmabuf(VirtIOGPU *g, struct virtio_gpu_simple_resource *res)
 {
     int max_outputs = g->parent_obj.conf.max_outputs;
     int i;
 
     for (i = 0; i < max_outputs; i++) {
-        VGPUDMABuf *dmabuf = g->dmabuf.primary[i];
+        struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[i];
 
-        if (dmabuf &&
-            qemu_dmabuf_get_num_planes(dmabuf->buf) > 0 &&
-            qemu_dmabuf_get_fds(dmabuf->buf, NULL)[0] == res->dmabuf_fd &&
+        if (scanout->dmabuf &&
+            qemu_dmabuf_get_num_planes(scanout->dmabuf) > 0 &&
+            qemu_dmabuf_get_fds(scanout->dmabuf, NULL)[0] == res->dmabuf_fd &&
             res->dmabuf_fd != -1) {
-            qemu_dmabuf_close(dmabuf->buf);
+            qemu_dmabuf_close(scanout->dmabuf);
             res->dmabuf_fd = -1;
             res->share_handle = SHAREABLE_NONE;
         }
@@ -192,31 +181,23 @@ void virtio_gpu_fini_udmabuf(VirtIOGPU *g, struct virtio_gpu_simple_resource *re
     virtio_gpu_destroy_udmabuf(res);
 }
 
-static VGPUDMABuf
-*virtio_gpu_create_dmabuf(VirtIOGPU *g,
-                          uint32_t scanout_id,
-                          struct virtio_gpu_simple_resource *res,
-                          struct virtio_gpu_framebuffer *fb,
-                          struct virtio_gpu_rect *r)
+static QemuDmaBuf *
+virtio_gpu_create_dmabuf(struct virtio_gpu_simple_resource *res,
+                         struct virtio_gpu_framebuffer *fb,
+                         struct virtio_gpu_rect *r)
 {
-    VGPUDMABuf *dmabuf;
     uint32_t offset = 0;
 
     if (res->dmabuf_fd < 0) {
         return NULL;
     }
 
-    dmabuf = g_new0(VGPUDMABuf, 1);
-    dmabuf->buf = qemu_dmabuf_new(r->width, r->height,
-                                  &offset, &fb->stride,
-                                  r->x, r->y, fb->width, fb->height,
-                                  qemu_pixman_to_drm_format(fb->format),
-                                  DRM_FORMAT_MOD_INVALID, &res->dmabuf_fd,
-                                  1, true, false);
-    dmabuf->scanout_id = scanout_id;
-    QTAILQ_INSERT_HEAD(&g->dmabuf.bufs, dmabuf, next);
-
-    return dmabuf;
+    return qemu_dmabuf_new(r->width, r->height,
+                           &offset, &fb->stride,
+                           r->x, r->y, fb->width, fb->height,
+                           qemu_pixman_to_drm_format(fb->format),
+                           DRM_FORMAT_MOD_INVALID, &res->dmabuf_fd,
+                           1, true, false);
 }
 
 int virtio_gpu_update_dmabuf(VirtIOGPU *g,
@@ -226,26 +207,25 @@ int virtio_gpu_update_dmabuf(VirtIOGPU *g,
                              struct virtio_gpu_rect *r)
 {
     struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
-    VGPUDMABuf *new_primary, *old_primary = NULL;
+    QemuDmaBuf *new_primary, *old_primary;
     uint32_t width, height;
 
-    new_primary = virtio_gpu_create_dmabuf(g, scanout_id, res, fb, r);
+    new_primary = virtio_gpu_create_dmabuf(res, fb, r);
     if (!new_primary) {
         return -EINVAL;
     }
 
-    if (g->dmabuf.primary[scanout_id]) {
-        old_primary = g->dmabuf.primary[scanout_id];
-    }
+    old_primary = scanout->dmabuf;
 
-    width = qemu_dmabuf_get_width(new_primary->buf);
-    height = qemu_dmabuf_get_height(new_primary->buf);
-    g->dmabuf.primary[scanout_id] = new_primary;
+    width = qemu_dmabuf_get_width(new_primary);
+    height = qemu_dmabuf_get_height(new_primary);
+    scanout->dmabuf = new_primary;
     qemu_console_resize(scanout->con, width, height);
-    qemu_console_gl_scanout_dmabuf(scanout->con, new_primary->buf);
+    qemu_console_gl_scanout_dmabuf(scanout->con, new_primary);
 
     if (old_primary) {
-        virtio_gpu_free_dmabuf(g, old_primary);
+        qemu_console_gl_release_dmabuf(scanout->con, old_primary);
+        qemu_dmabuf_free(old_primary);
     }
 
     return 0;
include/hw/virtio/virtio-gpu.h +1 -11
index 8c0df60b3b7e..ee5f6f2f2375 100644
@@ -85,6 +85,7 @@ struct virtio_gpu_framebuffer {
 struct virtio_gpu_scanout {
     QemuConsole *con;
     DisplaySurface *ds;
+    QemuDmaBuf *dmabuf;
     uint32_t width, height;
     int x, y;
     int invalidate;
@@ -189,12 +190,6 @@ struct VirtIOGPUBaseClass {
     DEFINE_PROP_UINT32("xres", _state, _conf.xres, 1280), \
     DEFINE_PROP_UINT32("yres", _state, _conf.yres, 800)
 
-typedef struct VGPUDMABuf {
-    QemuDmaBuf *buf;
-    uint32_t scanout_id;
-    QTAILQ_ENTRY(VGPUDMABuf) next;
-} VGPUDMABuf;
-
 struct VirtIOGPU {
     VirtIOGPUBase parent_obj;
 
@@ -226,11 +221,6 @@ struct VirtIOGPU {
         uint32_t bytes_3d;
     } stats;
 
-    struct {
-        QTAILQ_HEAD(, VGPUDMABuf) bufs;
-        VGPUDMABuf *primary[VIRTIO_GPU_MAX_SCANOUTS];
-    } dmabuf;
-
     GArray *capset_ids;
 };
 
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#10 lore raw

PATCH v4 10/24 ui/console: disable GL scanout when dmabuf is the active one

In reply to Marc-André Lureau

Disable the GL scanout when the actively-displayed dmabuf is released, so the console does not retain a stale reference.

The VFIO commit changes to call qemu_console_gl_release_dmabuf() before qemu_graphic_console_close(), which dereferences the active scanout.

Fixes 239b7c5705a4 ("vfio/pci: close display console during unrealize, not finalize")
---
 ui/console.c | 3 +++
 1 file changed, 3 insertions(+)
ui/console.c +3 -0
index a8a2a247d8f4..14f148118e37 100644
@@ -1049,6 +1049,9 @@ void qemu_console_gl_release_dmabuf(QemuConsole *con,
             dcl->ops->dpy_gl_release_dmabuf(dcl, dmabuf);
         }
     }
+    if (con->scanout.kind == SCANOUT_DMABUF && dmabuf == con->scanout.dmabuf) {
+        qemu_console_gl_scanout_disable(con);
+    }
 }
 
 void qemu_console_gl_update(QemuConsole *con,
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#11 lore raw

PATCH v4 11/24 ui/sdl2: clear guest_fb.dmabuf on release

In reply to Marc-André Lureau

sdl2_gl_release_dmabuf() releases the texture but leaves the guest_fb.dmabuf pointer dangling. Clear it to prevent stale references after the dmabuf is freed.

Fixes 52053b7e0a0e ("ui/sdl2: Implement dpy dmabuf functions")
---
 ui/sdl2-gl.c | 5 +++++
 1 file changed, 5 insertions(+)
ui/sdl2-gl.c +5 -0
index 1547ad2f6f80..46eff768c968 100644
@@ -286,7 +286,12 @@ void sdl2_gl_scanout_dmabuf(DisplayChangeListener *dcl,
 void sdl2_gl_release_dmabuf(DisplayChangeListener *dcl,
                             QemuDmaBuf *dmabuf)
 {
+    struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
+
     egl_dmabuf_release_texture(dmabuf);
+    if (scon->guest_fb.dmabuf == dmabuf) {
+        scon->guest_fb.dmabuf = NULL;
+    }
 }
 
 bool sdl2_gl_has_dmabuf(DisplayChangeListener *dcl)
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#12 lore raw

PATCH v4 12/24 ui/dmabuf: own and close fds on free

In reply to Marc-André Lureau

Make qemu_dmabuf_free() call qemu_dmabuf_close(), so the dmabuf always owns and closes its fds. This removes the need for callers to explicitly close fds before freeing and simplify the code.

To support this, virtio_gpu_create_dmabuf() now dup()s the resource fd so each QemuDmaBuf has an independent copy. The scanout-matching loop in virtio_gpu_fini_udmabuf() is no longer needed and is removed.

---
 hw/display/vhost-user-gpu.c           |  1 -
 hw/display/virtio-gpu-udmabuf-stubs.c |  2 +-
 hw/display/virtio-gpu-udmabuf.c       | 33 +++++++++------------------------
 hw/display/virtio-gpu.c               |  2 +-
 hw/vfio/display.c                     |  1 -
 include/hw/virtio/virtio-gpu.h        |  3 +--
 include/ui/dmabuf.h                   |  1 -
 ui/dbus-listener.c                    |  1 -
 ui/dmabuf.c                           | 22 ++++++++--------------
 9 files changed, 20 insertions(+), 46 deletions(-)
hw/display/vhost-user-gpu.c +0 -1
index cd684d63639e..c09aca041135 100644
@@ -293,7 +293,6 @@ vhost_user_gpu_handle_display(VhostUserGPU *g, VhostUserGpuMsg *msg)
         dmabuf = g->dmabuf[m->scanout_id];
 
         if (dmabuf) {
-            qemu_dmabuf_close(dmabuf);
             qemu_console_gl_release_dmabuf(con, dmabuf);
             g_clear_pointer(&dmabuf, qemu_dmabuf_free);
         }
hw/display/virtio-gpu-udmabuf-stubs.c +1 -1
index 0883bf05fac1..1a9dfbdec109 100644
@@ -13,7 +13,7 @@ bool virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
     return false;
 }
 
-void virtio_gpu_fini_udmabuf(VirtIOGPU *g, struct virtio_gpu_simple_resource *res)
+void virtio_gpu_fini_udmabuf(struct virtio_gpu_simple_resource *res)
 {
     /* nothing (stub) */
 }
hw/display/virtio-gpu-udmabuf.c +9 -24
index a38afb24c71f..6d2bc8d9771f 100644
@@ -82,7 +82,7 @@ static void virtio_gpu_remap_udmabuf(struct virtio_gpu_simple_resource *res)
     }
 }
 
-static void virtio_gpu_destroy_udmabuf(struct virtio_gpu_simple_resource *res)
+void virtio_gpu_fini_udmabuf(struct virtio_gpu_simple_resource *res)
 {
     if (res->remapped) {
         munmap(res->remapped, res->blob_size);
@@ -148,7 +148,7 @@ bool virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
         }
         virtio_gpu_remap_udmabuf(res);
         if (!res->remapped) {
-            virtio_gpu_destroy_udmabuf(res);
+            virtio_gpu_fini_udmabuf(res);
             return false;
         }
         res->share_handle = res->dmabuf_fd;
@@ -160,43 +160,28 @@ bool virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
     return true;
 }
 
-void virtio_gpu_fini_udmabuf(VirtIOGPU *g, struct virtio_gpu_simple_resource *res)
-{
-    int max_outputs = g->parent_obj.conf.max_outputs;
-    int i;
-
-    for (i = 0; i < max_outputs; i++) {
-        struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[i];
-
-        if (scanout->dmabuf &&
-            qemu_dmabuf_get_num_planes(scanout->dmabuf) > 0 &&
-            qemu_dmabuf_get_fds(scanout->dmabuf, NULL)[0] == res->dmabuf_fd &&
-            res->dmabuf_fd != -1) {
-            qemu_dmabuf_close(scanout->dmabuf);
-            res->dmabuf_fd = -1;
-            res->share_handle = SHAREABLE_NONE;
-        }
-    }
-
-    virtio_gpu_destroy_udmabuf(res);
-}
-
 static QemuDmaBuf *
 virtio_gpu_create_dmabuf(struct virtio_gpu_simple_resource *res,
                          struct virtio_gpu_framebuffer *fb,
                          struct virtio_gpu_rect *r)
 {
     uint32_t offset = 0;
+    int fd;
 
     if (res->dmabuf_fd < 0) {
         return NULL;
     }
 
+    fd = qemu_dup(res->dmabuf_fd);
+    if (fd < 0) {
+        return NULL;
+    }
+
     return qemu_dmabuf_new(r->width, r->height,
                            &offset, &fb->stride,
                            r->x, r->y, fb->width, fb->height,
                            qemu_pixman_to_drm_format(fb->format),
-                           DRM_FORMAT_MOD_INVALID, &res->dmabuf_fd,
+                           DRM_FORMAT_MOD_INVALID, &fd,
                            1, true, false);
 }
 
hw/display/virtio-gpu.c +1 -1
index cb45360e769e..ecf3cbc4893c 100644
@@ -1077,7 +1077,7 @@ void virtio_gpu_cleanup_mapping(VirtIOGPU *g,
     res->addrs = NULL;
 
     if (res->blob) {
-        virtio_gpu_fini_udmabuf(g, res);
+        virtio_gpu_fini_udmabuf(res);
     }
 }
 
hw/vfio/display.c +0 -1
index cb83d98e9af6..89854eceb43c 100644
@@ -263,7 +263,6 @@ static void vfio_display_free_one_dmabuf(VFIODisplay *dpy, VFIODMABuf *dmabuf)
 {
     QTAILQ_REMOVE(&dpy->dmabuf.bufs, dmabuf, next);
 
-    qemu_dmabuf_close(dmabuf->buf);
     qemu_console_gl_release_dmabuf(dpy->con, dmabuf->buf);
     g_clear_pointer(&dmabuf->buf, qemu_dmabuf_free);
     g_free(dmabuf);
include/hw/virtio/virtio-gpu.h +1 -2
index ee5f6f2f2375..c2018b41056f 100644
@@ -390,8 +390,7 @@ bool virtio_gpu_scanout_blob_to_fb(struct virtio_gpu_framebuffer *fb,
 /* virtio-gpu-udmabuf.c */
 bool virtio_gpu_have_udmabuf(void);
 bool virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res);
-void virtio_gpu_fini_udmabuf(VirtIOGPU *g,
-                             struct virtio_gpu_simple_resource *res);
+void virtio_gpu_fini_udmabuf(struct virtio_gpu_simple_resource *res);
 int virtio_gpu_update_dmabuf(VirtIOGPU *g,
                              uint32_t scanout_id,
                              struct virtio_gpu_simple_resource *res,
include/ui/dmabuf.h +0 -1
index b972099859f2..968f3f9c8d48 100644
@@ -27,7 +27,6 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(QemuDmaBuf, qemu_dmabuf_free);
 
 const int *qemu_dmabuf_get_fds(QemuDmaBuf *dmabuf, int *nfds);
 void qemu_dmabuf_dup_fds(QemuDmaBuf *dmabuf, int *fds, int nfds);
-void qemu_dmabuf_close(QemuDmaBuf *dmabuf);
 uint32_t qemu_dmabuf_get_width(QemuDmaBuf *dmabuf);
 uint32_t qemu_dmabuf_get_height(QemuDmaBuf *dmabuf);
 const uint32_t *qemu_dmabuf_get_offsets(QemuDmaBuf *dmabuf, int *noffsets);
ui/dbus-listener.c +0 -1
index 5a72c7eeae59..c1e86648384c 100644
@@ -631,7 +631,6 @@ static void dbus_scanout_texture(DisplayChangeListener *dcl,
     if (dbus_call_scanout_dmabuf(ddl, dmabuf)) {
         ddl->scanout_dmabuf = NULL;
     }
-    qemu_dmabuf_close(dmabuf);
 #endif
 
 #ifdef WIN32
ui/dmabuf.c +8 -14
index 495270f29bd6..497f82cc61f2 100644
@@ -61,10 +61,18 @@ QemuDmaBuf *qemu_dmabuf_new(uint32_t width, uint32_t height,
 
 void qemu_dmabuf_free(QemuDmaBuf *dmabuf)
 {
+    int i;
+
     if (dmabuf == NULL) {
         return;
     }
 
+    for (i = 0; i < dmabuf->num_planes; i++) {
+        if (dmabuf->fd[i] >= 0) {
+            close(dmabuf->fd[i]);
+            dmabuf->fd[i] = -1;
+        }
+    }
     g_free(dmabuf);
 }
 
@@ -91,20 +99,6 @@ void qemu_dmabuf_dup_fds(QemuDmaBuf *dmabuf, int *fds, int nfds)
     }
 }
 
-void qemu_dmabuf_close(QemuDmaBuf *dmabuf)
-{
-    int i;
-
-    assert(dmabuf != NULL);
-
-    for (i = 0; i < dmabuf->num_planes; i++) {
-        if (dmabuf->fd[i] >= 0) {
-            close(dmabuf->fd[i]);
-            dmabuf->fd[i] = -1;
-        }
-    }
-}
-
 uint32_t qemu_dmabuf_get_width(QemuDmaBuf *dmabuf)
 {
     assert(dmabuf != NULL);
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#13 lore raw

PATCH v4 13/24 ui: make GL context current in display backend callbacks

In reply to Marc-André Lureau

Display backend GL callbacks (scanout, flush, release, etc.) may be invoked from a different GL context, like virgl. Since framebuffer objects are per-context and textures require a valid context for deletion, each callback must ensure the correct context is active before performing GL operations.

Make each backend's own GL context current at the entry of every callback that performs GL work.

For GTK, the previously shared gd_gl_release_dmabuf is split into backend-specific gd_egl_release_dmabuf and gd_gl_area_release_dmabuf, matching the existing pattern where each backend manages its own context.

On the virgl side, restore ctx0 with virgl_renderer_force_ctx_0() after SET_SCANOUT, RESOURCE_FLUSH, and SET_SCANOUT_BLOB command handlers, since their display callbacks may have changed the current GL context.

---
 hw/display/virtio-gpu-virgl.c |  4 ++++
 include/ui/gtk.h              |  5 +++++
 ui/egl-headless.c             | 17 +++++++++++++++++
 ui/gtk-egl.c                  | 12 ++++++++++++
 ui/gtk-gl-area.c              | 11 +++++++++++
 ui/gtk.c                      | 13 ++++---------
 ui/sdl2-gl.c                  |  2 ++
 ui/spice-display.c            |  2 ++
 8 files changed, 57 insertions(+), 9 deletions(-)
hw/display/virtio-gpu-virgl.c +4 -0
index 7f1d1e0c3276..0e43eacca406 100644
@@ -1091,9 +1091,12 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
         break;
     case VIRTIO_GPU_CMD_SET_SCANOUT:
         virgl_cmd_set_scanout(g, cmd);
+        /* Display callbacks may change the current GL context. */
+        virgl_renderer_force_ctx_0();
         break;
     case VIRTIO_GPU_CMD_RESOURCE_FLUSH:
         virgl_cmd_resource_flush(g, cmd);
+        virgl_renderer_force_ctx_0();
         break;
     case VIRTIO_GPU_CMD_RESOURCE_UNREF:
         virgl_cmd_resource_unref(g, cmd, &cmd_suspended);
@@ -1130,6 +1133,7 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
         break;
     case VIRTIO_GPU_CMD_SET_SCANOUT_BLOB:
         virgl_cmd_set_scanout_blob(g, cmd);
+        virgl_renderer_force_ctx_0();
         break;
 #endif
     default:
include/ui/gtk.h +5 -0
index b66b9ca5c519..6b62e9758813 100644
@@ -163,6 +163,7 @@ extern bool gtk_use_gl_area;
 void gd_update_windowsize(VirtualConsole *vc);
 void gd_update_monitor_refresh_rate(VirtualConsole *vc, GtkWidget *widget);
 void gd_gl_wait_sync(VirtualConsole *vc, void *sync);
+void gd_release_dmabuf(VirtualConsole *vc, QemuDmaBuf *dmabuf);
 
 /* ui/gtk-egl.c */
 void gd_egl_init(VirtualConsole *vc);
@@ -190,6 +191,8 @@ void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
                           uint32_t hot_x, uint32_t hot_y);
 void gd_egl_cursor_position(DisplayChangeListener *dcl,
                             uint32_t pos_x, uint32_t pos_y);
+void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
+                           QemuDmaBuf *dmabuf);
 void gd_egl_flush(DisplayChangeListener *dcl,
                   uint32_t x, uint32_t y, uint32_t w, uint32_t h);
 void *gd_egl_scanout_flush(DisplayChangeListener *dcl,
@@ -221,6 +224,8 @@ void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                 uint32_t x, uint32_t y,
                                 uint32_t w, uint32_t h,
                                 void *d3d_tex2d);
+void gd_gl_area_release_dmabuf(DisplayChangeListener *dcl,
+                               QemuDmaBuf *dmabuf);
 void gd_gl_area_scanout_disable(DisplayChangeListener *dcl);
 void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
                               uint32_t x, uint32_t y, uint32_t w, uint32_t h);
ui/egl-headless.c +17 -0
index ba27efcf4c3e..c3f319c0960b 100644
@@ -52,6 +52,8 @@ static void egl_scanout_disable(DisplayChangeListener *dcl)
 {
     egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
     egl_fb_destroy(&edpy->guest_fb);
     egl_fb_destroy(&edpy->blit_fb);
 }
@@ -69,6 +71,9 @@ static void egl_scanout_texture(DisplayChangeListener *dcl,
 
     edpy->y_0_top = backing_y_0_top;
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
+
     /* source framebuffer */
     egl_fb_setup_for_tex(&edpy->guest_fb,
                          backing_width, backing_height, backing_id, false);
@@ -88,6 +93,8 @@ static void egl_scanout_dmabuf(DisplayChangeListener *dcl,
 {
     uint32_t width, height, texture;
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
     egl_dmabuf_import_texture(dmabuf);
     texture = qemu_dmabuf_get_texture(dmabuf);
     if (!texture) {
@@ -108,6 +115,8 @@ static void egl_cursor_dmabuf(DisplayChangeListener *dcl,
     uint32_t width, height, texture;
     egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
     if (dmabuf) {
         egl_dmabuf_import_texture(dmabuf);
         texture = qemu_dmabuf_get_texture(dmabuf);
@@ -126,6 +135,8 @@ static void egl_cursor_dmabuf(DisplayChangeListener *dcl,
 static void egl_release_dmabuf(DisplayChangeListener *dcl,
                                QemuDmaBuf *dmabuf)
 {
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
     egl_dmabuf_release_texture(dmabuf);
 }
 
@@ -151,6 +162,8 @@ static void egl_scanout_flush(DisplayChangeListener *dcl,
     }
     assert(surface_format(edpy->ds) == PIXMAN_x8r8g8b8);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
     if (edpy->cursor_fb.texture) {
         /* have cursor -> render using textures */
         egl_texture_blit(edpy->gls, &edpy->blit_fb, &edpy->guest_fb,
@@ -225,6 +238,8 @@ static void egl_headless_init(DisplayState *ds, DisplayOptions *opts)
 
     egl_dpys = g_ptr_array_new();
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
     for (idx = 0;; idx++) {
         DisplayGLCtx *ctx;
 
@@ -250,6 +265,8 @@ static void egl_headless_cleanup(void)
         return;
     }
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
     for (guint i = 0; i < egl_dpys->len; i++) {
         egl_dpy *edpy = g_ptr_array_index(egl_dpys, i);
 
ui/gtk-egl.c +12 -0
index 8623181336e9..d595916476d9 100644
@@ -426,6 +426,18 @@ void gtk_egl_init(DisplayGLMode mode)
     display_opengl = 1;
 }
 
+void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
+                           QemuDmaBuf *dmabuf)
+{
+#ifdef CONFIG_GBM
+    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
+
+    eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
+                   vc->gfx.esurface, vc->gfx.ectx);
+    gd_release_dmabuf(vc, dmabuf);
+#endif
+}
+
 int gd_egl_make_current(DisplayGLCtx *dgc,
                         QEMUGLContext ctx)
 {
ui/gtk-gl-area.c +11 -0
index 45c0c8c81a11..effd88f28721 100644
@@ -360,6 +360,17 @@ void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
 #endif
 }
 
+void gd_gl_area_release_dmabuf(DisplayChangeListener *dcl,
+                               QemuDmaBuf *dmabuf)
+{
+#ifdef CONFIG_GBM
+    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
+
+    gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
+    gd_release_dmabuf(vc, dmabuf);
+#endif
+}
+
 void gtk_gl_area_init(void)
 {
     display_opengl = 1;
ui/gtk.c +4 -9
index 55e45e8445bc..c615d35451b6 100644
@@ -584,21 +584,16 @@ static bool gd_has_dmabuf(DisplayChangeListener *dcl)
     return vc->gfx.has_dmabuf;
 }
 
-static void gd_gl_release_dmabuf(DisplayChangeListener *dcl,
-                                 QemuDmaBuf *dmabuf)
-{
 #ifdef CONFIG_GBM
-    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
-
+void gd_release_dmabuf(VirtualConsole *vc, QemuDmaBuf *dmabuf)
+{
     egl_dmabuf_release_texture(dmabuf);
     if (vc->gfx.guest_fb.dmabuf == dmabuf) {
         vc->gfx.guest_fb.dmabuf = NULL;
         vc->gfx.draw_submitted = false;
     }
-#endif
 }
 
-#ifdef CONFIG_GBM
 static void gd_gl_fence_cb(void *vcon)
 {
     VirtualConsole *vc = vcon;
@@ -639,7 +634,7 @@ static const DisplayChangeListenerOps dcl_gl_area_ops = {
     .dpy_gl_scanout_disable  = gd_gl_area_scanout_disable,
     .dpy_gl_update           = gd_gl_area_scanout_flush,
     .dpy_gl_scanout_dmabuf   = gd_gl_area_scanout_dmabuf,
-    .dpy_gl_release_dmabuf   = gd_gl_release_dmabuf,
+    .dpy_gl_release_dmabuf   = gd_gl_area_release_dmabuf,
     .dpy_has_dmabuf          = gd_has_dmabuf,
 };
 
@@ -673,7 +668,7 @@ static const DisplayChangeListenerOps dcl_egl_ops = {
     .dpy_gl_cursor_dmabuf    = gd_egl_cursor_dmabuf,
     .dpy_gl_cursor_position  = gd_egl_cursor_position,
     .dpy_gl_update           = gd_egl_flush,
-    .dpy_gl_release_dmabuf   = gd_gl_release_dmabuf,
+    .dpy_gl_release_dmabuf   = gd_egl_release_dmabuf,
     .dpy_has_dmabuf          = gd_has_dmabuf,
 };
 
ui/sdl2-gl.c +2 -0
index 46eff768c968..f41fc8bb9634 100644
@@ -200,6 +200,7 @@ void sdl2_gl_scanout_disable(DisplayChangeListener *dcl)
     struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
 
     assert(scon->opengl);
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
     scon->w = 0;
     scon->h = 0;
     sdl2_set_scanout_mode(scon, false);
@@ -288,6 +289,7 @@ void sdl2_gl_release_dmabuf(DisplayChangeListener *dcl,
 {
     struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
 
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
     egl_dmabuf_release_texture(dmabuf);
     if (scon->guest_fb.dmabuf == dmabuf) {
         scon->guest_fb.dmabuf = NULL;
ui/spice-display.c +2 -0
index 75c7df7bb5e0..22cd1e266caa 100644
@@ -1176,6 +1176,8 @@ static void qemu_spice_gl_release_dmabuf(DisplayChangeListener *dcl,
         ssd->guest_dmabuf = NULL;
         ssd->guest_dmabuf_refresh = false;
     }
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
     egl_dmabuf_release_texture(dmabuf);
 }
 
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#14 lore raw

Re: [PATCH v4 13/24] ui: make GL context current in display backend callbacks

Quoted 49 lines · Marc-André Lureau

Display backend GL callbacks (scanout, flush, release, etc.) may be invoked from a different GL context, like virgl. Since framebuffer objects are per-context and textures require a valid context for deletion, each callback must ensure the correct context is active before performing GL operations.

Make each backend's own GL context current at the entry of every callback that performs GL work.

For GTK, the previously shared gd_gl_release_dmabuf is split into backend-specific gd_egl_release_dmabuf and gd_gl_area_release_dmabuf, matching the existing pattern where each backend manages its own context.

On the virgl side, restore ctx0 with virgl_renderer_force_ctx_0() after SET_SCANOUT, RESOURCE_FLUSH, and SET_SCANOUT_BLOB command handlers, since their display callbacks may have changed the current GL context.

---
  hw/display/virtio-gpu-virgl.c |  4 ++++
  include/ui/gtk.h              |  5 +++++
  ui/egl-headless.c             | 17 +++++++++++++++++
  ui/gtk-egl.c                  | 12 ++++++++++++
  ui/gtk-gl-area.c              | 11 +++++++++++
  ui/gtk.c                      | 13 ++++---------
  ui/sdl2-gl.c                  |  2 ++
  ui/spice-display.c            |  2 ++
  8 files changed, 57 insertions(+), 9 deletions(-)
hw/display/virtio-gpu-virgl.c +3 -0
index 7f1d1e0c3276..0e43eacca406 100644
@@ -1091,9 +1091,12 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
          break;
      case VIRTIO_GPU_CMD_SET_SCANOUT:
          virgl_cmd_set_scanout(g, cmd);
+        /* Display callbacks may change the current GL context. */
+        virgl_renderer_force_ctx_0();
          break;
      case VIRTIO_GPU_CMD_RESOURCE_FLUSH:
          virgl_cmd_resource_flush(g, cmd);
+        virgl_renderer_force_ctx_0();
          break;
      case VIRTIO_GPU_CMD_RESOURCE_UNREF:
          virgl_cmd_resource_unref(g, cmd, &cmd_suspended);

hw/display/virtio-gpu-virgl.c void virtio_gpu_virgl_process_cmd(VirtIOGPU *g, · line 1136

Quoted 4 lines
hw/display/virtio-gpu-virgl.c +1 -0
@@ -1130,6 +1133,7 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
          break;
      case VIRTIO_GPU_CMD_SET_SCANOUT_BLOB:
          virgl_cmd_set_scanout_blob(g, cmd);
+        virgl_renderer_force_ctx_0();

I doubt that adding these virgl_renderer_force_ctx_0() calls is correct here. Since scanout and resource flush actually happen in the UI context, we actually need to use the UI context to fence those operations.

Regards,
Akihiko Odaki

Quoted 235 lines
hw/display/virtio-gpu-virgl.c +0 -0
          break;
  #endif
      default:
include/ui/gtk.h +5 -0
index b66b9ca5c519..6b62e9758813 100644
@@ -163,6 +163,7 @@ extern bool gtk_use_gl_area;
  void gd_update_windowsize(VirtualConsole *vc);
  void gd_update_monitor_refresh_rate(VirtualConsole *vc, GtkWidget *widget);
  void gd_gl_wait_sync(VirtualConsole *vc, void *sync);
+void gd_release_dmabuf(VirtualConsole *vc, QemuDmaBuf *dmabuf);
 
  /* ui/gtk-egl.c */
  void gd_egl_init(VirtualConsole *vc);
@@ -190,6 +191,8 @@ void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
                            uint32_t hot_x, uint32_t hot_y);
  void gd_egl_cursor_position(DisplayChangeListener *dcl,
                              uint32_t pos_x, uint32_t pos_y);
+void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
+                           QemuDmaBuf *dmabuf);
  void gd_egl_flush(DisplayChangeListener *dcl,
                    uint32_t x, uint32_t y, uint32_t w, uint32_t h);
  void *gd_egl_scanout_flush(DisplayChangeListener *dcl,
@@ -221,6 +224,8 @@ void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                  uint32_t x, uint32_t y,
                                  uint32_t w, uint32_t h,
                                  void *d3d_tex2d);
+void gd_gl_area_release_dmabuf(DisplayChangeListener *dcl,
+                               QemuDmaBuf *dmabuf);
  void gd_gl_area_scanout_disable(DisplayChangeListener *dcl);
  void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
                                uint32_t x, uint32_t y, uint32_t w, uint32_t h);
ui/egl-headless.c +17 -0
index ba27efcf4c3e..c3f319c0960b 100644
@@ -52,6 +52,8 @@ static void egl_scanout_disable(DisplayChangeListener *dcl)
  {
      egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
      egl_fb_destroy(&edpy->guest_fb);
      egl_fb_destroy(&edpy->blit_fb);
  }
@@ -69,6 +71,9 @@ static void egl_scanout_texture(DisplayChangeListener *dcl,
 
      edpy->y_0_top = backing_y_0_top;
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
+
      /* source framebuffer */
      egl_fb_setup_for_tex(&edpy->guest_fb,
                           backing_width, backing_height, backing_id, false);
@@ -88,6 +93,8 @@ static void egl_scanout_dmabuf(DisplayChangeListener *dcl,
  {
      uint32_t width, height, texture;
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
      egl_dmabuf_import_texture(dmabuf);
      texture = qemu_dmabuf_get_texture(dmabuf);
      if (!texture) {
@@ -108,6 +115,8 @@ static void egl_cursor_dmabuf(DisplayChangeListener *dcl,
      uint32_t width, height, texture;
      egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
      if (dmabuf) {
          egl_dmabuf_import_texture(dmabuf);
          texture = qemu_dmabuf_get_texture(dmabuf);
@@ -126,6 +135,8 @@ static void egl_cursor_dmabuf(DisplayChangeListener *dcl,
  static void egl_release_dmabuf(DisplayChangeListener *dcl,
                                 QemuDmaBuf *dmabuf)
  {
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
      egl_dmabuf_release_texture(dmabuf);
  }
 
@@ -151,6 +162,8 @@ static void egl_scanout_flush(DisplayChangeListener *dcl,
      }
      assert(surface_format(edpy->ds) == PIXMAN_x8r8g8b8);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
      if (edpy->cursor_fb.texture) {
          /* have cursor -> render using textures */
          egl_texture_blit(edpy->gls, &edpy->blit_fb, &edpy->guest_fb,
@@ -225,6 +238,8 @@ static void egl_headless_init(DisplayState *ds, DisplayOptions *opts)
 
      egl_dpys = g_ptr_array_new();
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
      for (idx = 0;; idx++) {
          DisplayGLCtx *ctx;
 
@@ -250,6 +265,8 @@ static void egl_headless_cleanup(void)
          return;
      }
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
      for (guint i = 0; i < egl_dpys->len; i++) {
          egl_dpy *edpy = g_ptr_array_index(egl_dpys, i);
 
ui/gtk-egl.c +12 -0
index 8623181336e9..d595916476d9 100644
@@ -426,6 +426,18 @@ void gtk_egl_init(DisplayGLMode mode)
      display_opengl = 1;
  }
 
+void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
+                           QemuDmaBuf *dmabuf)
+{
+#ifdef CONFIG_GBM
+    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
+
+    eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
+                   vc->gfx.esurface, vc->gfx.ectx);
+    gd_release_dmabuf(vc, dmabuf);
+#endif
+}
+
  int gd_egl_make_current(DisplayGLCtx *dgc,
                          QEMUGLContext ctx)
  {
ui/gtk-gl-area.c +11 -0
index 45c0c8c81a11..effd88f28721 100644
@@ -360,6 +360,17 @@ void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
  #endif
  }
 
+void gd_gl_area_release_dmabuf(DisplayChangeListener *dcl,
+                               QemuDmaBuf *dmabuf)
+{
+#ifdef CONFIG_GBM
+    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
+
+    gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
+    gd_release_dmabuf(vc, dmabuf);
+#endif
+}
+
  void gtk_gl_area_init(void)
  {
      display_opengl = 1;
ui/gtk.c +4 -9
index 55e45e8445bc..c615d35451b6 100644
@@ -584,21 +584,16 @@ static bool gd_has_dmabuf(DisplayChangeListener *dcl)
      return vc->gfx.has_dmabuf;
  }
 
-static void gd_gl_release_dmabuf(DisplayChangeListener *dcl,
-                                 QemuDmaBuf *dmabuf)
-{
  #ifdef CONFIG_GBM
-    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
-
+void gd_release_dmabuf(VirtualConsole *vc, QemuDmaBuf *dmabuf)
+{
      egl_dmabuf_release_texture(dmabuf);
      if (vc->gfx.guest_fb.dmabuf == dmabuf) {
          vc->gfx.guest_fb.dmabuf = NULL;
          vc->gfx.draw_submitted = false;
      }
-#endif
  }
 
-#ifdef CONFIG_GBM
  static void gd_gl_fence_cb(void *vcon)
  {
      VirtualConsole *vc = vcon;
@@ -639,7 +634,7 @@ static const DisplayChangeListenerOps dcl_gl_area_ops = {
      .dpy_gl_scanout_disable  = gd_gl_area_scanout_disable,
      .dpy_gl_update           = gd_gl_area_scanout_flush,
      .dpy_gl_scanout_dmabuf   = gd_gl_area_scanout_dmabuf,
-    .dpy_gl_release_dmabuf   = gd_gl_release_dmabuf,
+    .dpy_gl_release_dmabuf   = gd_gl_area_release_dmabuf,
      .dpy_has_dmabuf          = gd_has_dmabuf,
  };
 
@@ -673,7 +668,7 @@ static const DisplayChangeListenerOps dcl_egl_ops = {
      .dpy_gl_cursor_dmabuf    = gd_egl_cursor_dmabuf,
      .dpy_gl_cursor_position  = gd_egl_cursor_position,
      .dpy_gl_update           = gd_egl_flush,
-    .dpy_gl_release_dmabuf   = gd_gl_release_dmabuf,
+    .dpy_gl_release_dmabuf   = gd_egl_release_dmabuf,
      .dpy_has_dmabuf          = gd_has_dmabuf,
  };
 
ui/sdl2-gl.c +2 -0
index 46eff768c968..f41fc8bb9634 100644
@@ -200,6 +200,7 @@ void sdl2_gl_scanout_disable(DisplayChangeListener *dcl)
      struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
 
      assert(scon->opengl);
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
      scon->w = 0;
      scon->h = 0;
      sdl2_set_scanout_mode(scon, false);
@@ -288,6 +289,7 @@ void sdl2_gl_release_dmabuf(DisplayChangeListener *dcl,
  {
      struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
 
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
      egl_dmabuf_release_texture(dmabuf);
      if (scon->guest_fb.dmabuf == dmabuf) {
          scon->guest_fb.dmabuf = NULL;
ui/spice-display.c +2 -0
index 75c7df7bb5e0..22cd1e266caa 100644
@@ -1176,6 +1176,8 @@ static void qemu_spice_gl_release_dmabuf(DisplayChangeListener *dcl,
          ssd->guest_dmabuf = NULL;
          ssd->guest_dmabuf_refresh = false;
      }
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
      egl_dmabuf_release_texture(dmabuf);
  }
 
#15 lore raw

Re: [PATCH v4 13/24] ui: make GL context current in display backend callbacks

Hi

Quoted 16 lines · Akihiko Odaki
Quoted 14 lines · Marc-André Lureau

Display backend GL callbacks (scanout, flush, release, etc.) may be invoked from a different GL context, like virgl. Since framebuffer objects are per-context and textures require a valid context for deletion, each callback must ensure the correct context is active before performing GL operations.

Make each backend's own GL context current at the entry of every callback that performs GL work.

For GTK, the previously shared gd_gl_release_dmabuf is split into backend-specific gd_egl_release_dmabuf and gd_gl_area_release_dmabuf, matching the existing pattern where each backend manages its own context.

Quoted 4 lines
Quoted 4 lines

On the virgl side, restore ctx0 with virgl_renderer_force_ctx_0() after SET_SCANOUT, RESOURCE_FLUSH, and SET_SCANOUT_BLOB command handlers, since their display callbacks may have changed the current GL context.

Ok, it sounds like we need to save/restore context at each callback for now. Later on we should try to change the code to avoid that dance and defer it. Right?

Quoted 279 lines
Quoted 31 lines
---
  hw/display/virtio-gpu-virgl.c |  4 ++++
  include/ui/gtk.h              |  5 +++++
  ui/egl-headless.c             | 17 +++++++++++++++++
  ui/gtk-egl.c                  | 12 ++++++++++++
  ui/gtk-gl-area.c              | 11 +++++++++++
  ui/gtk.c                      | 13 ++++---------
  ui/sdl2-gl.c                  |  2 ++
  ui/spice-display.c            |  2 ++
  8 files changed, 57 insertions(+), 9 deletions(-)
hw/display/virtio-gpu-virgl.c +3 -0
index 7f1d1e0c3276..0e43eacca406 100644
@@ -1091,9 +1091,12 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
          break;
      case VIRTIO_GPU_CMD_SET_SCANOUT:
          virgl_cmd_set_scanout(g, cmd);
+        /* Display callbacks may change the current GL context. */
+        virgl_renderer_force_ctx_0();
          break;
      case VIRTIO_GPU_CMD_RESOURCE_FLUSH:
          virgl_cmd_resource_flush(g, cmd);
+        virgl_renderer_force_ctx_0();
          break;
      case VIRTIO_GPU_CMD_RESOURCE_UNREF:
          virgl_cmd_resource_unref(g, cmd, &cmd_suspended);

hw/display/virtio-gpu-virgl.c void virtio_gpu_virgl_process_cmd(VirtIOGPU *g, · line 1136

Quoted 4 lines
hw/display/virtio-gpu-virgl.c +1 -0
@@ -1130,6 +1133,7 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
          break;
      case VIRTIO_GPU_CMD_SET_SCANOUT_BLOB:
          virgl_cmd_set_scanout_blob(g, cmd);
+        virgl_renderer_force_ctx_0();

I doubt that adding these virgl_renderer_force_ctx_0() calls is correct here. Since scanout and resource flush actually happen in the UI context, we actually need to use the UI context to fence those operations.

Regards,
Akihiko Odaki

Quoted 235 lines
hw/display/virtio-gpu-virgl.c +0 -0
          break;
  #endif
      default:
include/ui/gtk.h +5 -0
index b66b9ca5c519..6b62e9758813 100644
@@ -163,6 +163,7 @@ extern bool gtk_use_gl_area;
  void gd_update_windowsize(VirtualConsole *vc);
  void gd_update_monitor_refresh_rate(VirtualConsole *vc, GtkWidget *widget);
  void gd_gl_wait_sync(VirtualConsole *vc, void *sync);
+void gd_release_dmabuf(VirtualConsole *vc, QemuDmaBuf *dmabuf);
 
  /* ui/gtk-egl.c */
  void gd_egl_init(VirtualConsole *vc);
@@ -190,6 +191,8 @@ void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
                            uint32_t hot_x, uint32_t hot_y);
  void gd_egl_cursor_position(DisplayChangeListener *dcl,
                              uint32_t pos_x, uint32_t pos_y);
+void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
+                           QemuDmaBuf *dmabuf);
  void gd_egl_flush(DisplayChangeListener *dcl,
                    uint32_t x, uint32_t y, uint32_t w, uint32_t h);
  void *gd_egl_scanout_flush(DisplayChangeListener *dcl,
@@ -221,6 +224,8 @@ void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                  uint32_t x, uint32_t y,
                                  uint32_t w, uint32_t h,
                                  void *d3d_tex2d);
+void gd_gl_area_release_dmabuf(DisplayChangeListener *dcl,
+                               QemuDmaBuf *dmabuf);
  void gd_gl_area_scanout_disable(DisplayChangeListener *dcl);
  void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
                                uint32_t x, uint32_t y, uint32_t w, uint32_t h);
ui/egl-headless.c +17 -0
index ba27efcf4c3e..c3f319c0960b 100644
@@ -52,6 +52,8 @@ static void egl_scanout_disable(DisplayChangeListener *dcl)
  {
      egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
      egl_fb_destroy(&edpy->guest_fb);
      egl_fb_destroy(&edpy->blit_fb);
  }
@@ -69,6 +71,9 @@ static void egl_scanout_texture(DisplayChangeListener *dcl,
 
      edpy->y_0_top = backing_y_0_top;
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
+
      /* source framebuffer */
      egl_fb_setup_for_tex(&edpy->guest_fb,
                           backing_width, backing_height, backing_id, false);
@@ -88,6 +93,8 @@ static void egl_scanout_dmabuf(DisplayChangeListener *dcl,
  {
      uint32_t width, height, texture;
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
      egl_dmabuf_import_texture(dmabuf);
      texture = qemu_dmabuf_get_texture(dmabuf);
      if (!texture) {
@@ -108,6 +115,8 @@ static void egl_cursor_dmabuf(DisplayChangeListener *dcl,
      uint32_t width, height, texture;
      egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
      if (dmabuf) {
          egl_dmabuf_import_texture(dmabuf);
          texture = qemu_dmabuf_get_texture(dmabuf);
@@ -126,6 +135,8 @@ static void egl_cursor_dmabuf(DisplayChangeListener *dcl,
  static void egl_release_dmabuf(DisplayChangeListener *dcl,
                                 QemuDmaBuf *dmabuf)
  {
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
      egl_dmabuf_release_texture(dmabuf);
  }
 
@@ -151,6 +162,8 @@ static void egl_scanout_flush(DisplayChangeListener *dcl,
      }
      assert(surface_format(edpy->ds) == PIXMAN_x8r8g8b8);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
      if (edpy->cursor_fb.texture) {
          /* have cursor -> render using textures */
          egl_texture_blit(edpy->gls, &edpy->blit_fb, &edpy->guest_fb,
@@ -225,6 +238,8 @@ static void egl_headless_init(DisplayState *ds, DisplayOptions *opts)
 
      egl_dpys = g_ptr_array_new();
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
      for (idx = 0;; idx++) {
          DisplayGLCtx *ctx;
 
@@ -250,6 +265,8 @@ static void egl_headless_cleanup(void)
          return;
      }
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
      for (guint i = 0; i < egl_dpys->len; i++) {
          egl_dpy *edpy = g_ptr_array_index(egl_dpys, i);
 
ui/gtk-egl.c +12 -0
index 8623181336e9..d595916476d9 100644
@@ -426,6 +426,18 @@ void gtk_egl_init(DisplayGLMode mode)
      display_opengl = 1;
  }
 
+void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
+                           QemuDmaBuf *dmabuf)
+{
+#ifdef CONFIG_GBM
+    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
+
+    eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
+                   vc->gfx.esurface, vc->gfx.ectx);
+    gd_release_dmabuf(vc, dmabuf);
+#endif
+}
+
  int gd_egl_make_current(DisplayGLCtx *dgc,
                          QEMUGLContext ctx)
  {
ui/gtk-gl-area.c +11 -0
index 45c0c8c81a11..effd88f28721 100644
@@ -360,6 +360,17 @@ void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
  #endif
  }
 
+void gd_gl_area_release_dmabuf(DisplayChangeListener *dcl,
+                               QemuDmaBuf *dmabuf)
+{
+#ifdef CONFIG_GBM
+    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
+
+    gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
+    gd_release_dmabuf(vc, dmabuf);
+#endif
+}
+
  void gtk_gl_area_init(void)
  {
      display_opengl = 1;
ui/gtk.c +4 -9
index 55e45e8445bc..c615d35451b6 100644
@@ -584,21 +584,16 @@ static bool gd_has_dmabuf(DisplayChangeListener *dcl)
      return vc->gfx.has_dmabuf;
  }
 
-static void gd_gl_release_dmabuf(DisplayChangeListener *dcl,
-                                 QemuDmaBuf *dmabuf)
-{
  #ifdef CONFIG_GBM
-    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
-
+void gd_release_dmabuf(VirtualConsole *vc, QemuDmaBuf *dmabuf)
+{
      egl_dmabuf_release_texture(dmabuf);
      if (vc->gfx.guest_fb.dmabuf == dmabuf) {
          vc->gfx.guest_fb.dmabuf = NULL;
          vc->gfx.draw_submitted = false;
      }
-#endif
  }
 
-#ifdef CONFIG_GBM
  static void gd_gl_fence_cb(void *vcon)
  {
      VirtualConsole *vc = vcon;
@@ -639,7 +634,7 @@ static const DisplayChangeListenerOps dcl_gl_area_ops = {
      .dpy_gl_scanout_disable  = gd_gl_area_scanout_disable,
      .dpy_gl_update           = gd_gl_area_scanout_flush,
      .dpy_gl_scanout_dmabuf   = gd_gl_area_scanout_dmabuf,
-    .dpy_gl_release_dmabuf   = gd_gl_release_dmabuf,
+    .dpy_gl_release_dmabuf   = gd_gl_area_release_dmabuf,
      .dpy_has_dmabuf          = gd_has_dmabuf,
  };
 
@@ -673,7 +668,7 @@ static const DisplayChangeListenerOps dcl_egl_ops = {
      .dpy_gl_cursor_dmabuf    = gd_egl_cursor_dmabuf,
      .dpy_gl_cursor_position  = gd_egl_cursor_position,
      .dpy_gl_update           = gd_egl_flush,
-    .dpy_gl_release_dmabuf   = gd_gl_release_dmabuf,
+    .dpy_gl_release_dmabuf   = gd_egl_release_dmabuf,
      .dpy_has_dmabuf          = gd_has_dmabuf,
  };
 
ui/sdl2-gl.c +2 -0
index 46eff768c968..f41fc8bb9634 100644
@@ -200,6 +200,7 @@ void sdl2_gl_scanout_disable(DisplayChangeListener *dcl)
      struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
 
      assert(scon->opengl);
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
      scon->w = 0;
      scon->h = 0;
      sdl2_set_scanout_mode(scon, false);
@@ -288,6 +289,7 @@ void sdl2_gl_release_dmabuf(DisplayChangeListener *dcl,
  {
      struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
 
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
      egl_dmabuf_release_texture(dmabuf);
      if (scon->guest_fb.dmabuf == dmabuf) {
          scon->guest_fb.dmabuf = NULL;
ui/spice-display.c +2 -0
index 75c7df7bb5e0..22cd1e266caa 100644
@@ -1176,6 +1176,8 @@ static void qemu_spice_gl_release_dmabuf(DisplayChangeListener *dcl,
          ssd->guest_dmabuf = NULL;
          ssd->guest_dmabuf_refresh = false;
      }
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
      egl_dmabuf_release_texture(dmabuf);
  }
 
Reviewed-by
#16 lore raw

Re: [PATCH v4 13/24] ui: make GL context current in display backend callbacks

Quoted 20 lines · Marc-André Lureau

Hi

Quoted 16 lines · Akihiko Odaki
Quoted 14 lines · Marc-André Lureau

Display backend GL callbacks (scanout, flush, release, etc.) may be invoked from a different GL context, like virgl. Since framebuffer objects are per-context and textures require a valid context for deletion, each callback must ensure the correct context is active before performing GL operations.

Make each backend's own GL context current at the entry of every callback that performs GL work.

For GTK, the previously shared gd_gl_release_dmabuf is split into backend-specific gd_egl_release_dmabuf and gd_gl_area_release_dmabuf, matching the existing pattern where each backend manages its own context.

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

On the virgl side, restore ctx0 with virgl_renderer_force_ctx_0() after SET_SCANOUT, RESOURCE_FLUSH, and SET_SCANOUT_BLOB command handlers, since their display callbacks may have changed the current GL context.

Ok, it sounds like we need to save/restore context at each callback for now. Later on we should try to change the code to avoid that dance and defer it. Right?

We rather need to let callbacks make the UI context current instead of keeping the virgl context current to wait for the UI to finish processing those commands.

But now I have a second thought: actually the situation is way more complicated because theoretically one resource flush can update multiple scanouts with different contexts, not just one context. Using the last UI context for fence creation does not solve that problem.

Making the virgl context current will ensure predictable behavior at least. Fence will not work with those commands properly, but it is just yet another silent long-standing bug.

So this patch should have:

Regards,
Akihiko Odaki

Quoted 281 lines
Quoted 281 lines
Quoted 33 lines
---
   hw/display/virtio-gpu-virgl.c |  4 ++++
   include/ui/gtk.h              |  5 +++++
   ui/egl-headless.c             | 17 +++++++++++++++++
   ui/gtk-egl.c                  | 12 ++++++++++++
   ui/gtk-gl-area.c              | 11 +++++++++++
   ui/gtk.c                      | 13 ++++---------
   ui/sdl2-gl.c                  |  2 ++
   ui/spice-display.c            |  2 ++
   8 files changed, 57 insertions(+), 9 deletions(-)
hw/display/virtio-gpu-virgl.c +3 -0
index 7f1d1e0c3276..0e43eacca406 100644
@@ -1091,9 +1091,12 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
           break;
       case VIRTIO_GPU_CMD_SET_SCANOUT:
           virgl_cmd_set_scanout(g, cmd);
+        /* Display callbacks may change the current GL context. */
+        virgl_renderer_force_ctx_0();
           break;
       case VIRTIO_GPU_CMD_RESOURCE_FLUSH:
           virgl_cmd_resource_flush(g, cmd);
+        virgl_renderer_force_ctx_0();
           break;
       case VIRTIO_GPU_CMD_RESOURCE_UNREF:
           virgl_cmd_resource_unref(g, cmd, &cmd_suspended);

hw/display/virtio-gpu-virgl.c void virtio_gpu_virgl_process_cmd(VirtIOGPU *g, · line 1136

Quoted 4 lines
hw/display/virtio-gpu-virgl.c +1 -0
@@ -1130,6 +1133,7 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
           break;
       case VIRTIO_GPU_CMD_SET_SCANOUT_BLOB:
           virgl_cmd_set_scanout_blob(g, cmd);
+        virgl_renderer_force_ctx_0();

I doubt that adding these virgl_renderer_force_ctx_0() calls is correct here. Since scanout and resource flush actually happen in the UI context, we actually need to use the UI context to fence those operations.

Regards,
Akihiko Odaki

Quoted 235 lines
hw/display/virtio-gpu-virgl.c +0 -0
           break;
   #endif
       default:
include/ui/gtk.h +5 -0
index b66b9ca5c519..6b62e9758813 100644
@@ -163,6 +163,7 @@ extern bool gtk_use_gl_area;
   void gd_update_windowsize(VirtualConsole *vc);
   void gd_update_monitor_refresh_rate(VirtualConsole *vc, GtkWidget *widget);
   void gd_gl_wait_sync(VirtualConsole *vc, void *sync);
+void gd_release_dmabuf(VirtualConsole *vc, QemuDmaBuf *dmabuf);
 
   /* ui/gtk-egl.c */
   void gd_egl_init(VirtualConsole *vc);
@@ -190,6 +191,8 @@ void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
                             uint32_t hot_x, uint32_t hot_y);
   void gd_egl_cursor_position(DisplayChangeListener *dcl,
                               uint32_t pos_x, uint32_t pos_y);
+void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
+                           QemuDmaBuf *dmabuf);
   void gd_egl_flush(DisplayChangeListener *dcl,
                     uint32_t x, uint32_t y, uint32_t w, uint32_t h);
   void *gd_egl_scanout_flush(DisplayChangeListener *dcl,
@@ -221,6 +224,8 @@ void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                   uint32_t x, uint32_t y,
                                   uint32_t w, uint32_t h,
                                   void *d3d_tex2d);
+void gd_gl_area_release_dmabuf(DisplayChangeListener *dcl,
+                               QemuDmaBuf *dmabuf);
   void gd_gl_area_scanout_disable(DisplayChangeListener *dcl);
   void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
                                 uint32_t x, uint32_t y, uint32_t w, uint32_t h);
ui/egl-headless.c +17 -0
index ba27efcf4c3e..c3f319c0960b 100644
@@ -52,6 +52,8 @@ static void egl_scanout_disable(DisplayChangeListener *dcl)
   {
       egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
       egl_fb_destroy(&edpy->guest_fb);
       egl_fb_destroy(&edpy->blit_fb);
   }
@@ -69,6 +71,9 @@ static void egl_scanout_texture(DisplayChangeListener *dcl,
 
       edpy->y_0_top = backing_y_0_top;
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
+
       /* source framebuffer */
       egl_fb_setup_for_tex(&edpy->guest_fb,
                            backing_width, backing_height, backing_id, false);
@@ -88,6 +93,8 @@ static void egl_scanout_dmabuf(DisplayChangeListener *dcl,
   {
       uint32_t width, height, texture;
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
       egl_dmabuf_import_texture(dmabuf);
       texture = qemu_dmabuf_get_texture(dmabuf);
       if (!texture) {
@@ -108,6 +115,8 @@ static void egl_cursor_dmabuf(DisplayChangeListener *dcl,
       uint32_t width, height, texture;
       egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
       if (dmabuf) {
           egl_dmabuf_import_texture(dmabuf);
           texture = qemu_dmabuf_get_texture(dmabuf);
@@ -126,6 +135,8 @@ static void egl_cursor_dmabuf(DisplayChangeListener *dcl,
   static void egl_release_dmabuf(DisplayChangeListener *dcl,
                                  QemuDmaBuf *dmabuf)
   {
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
       egl_dmabuf_release_texture(dmabuf);
   }
 
@@ -151,6 +162,8 @@ static void egl_scanout_flush(DisplayChangeListener *dcl,
       }
       assert(surface_format(edpy->ds) == PIXMAN_x8r8g8b8);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
       if (edpy->cursor_fb.texture) {
           /* have cursor -> render using textures */
           egl_texture_blit(edpy->gls, &edpy->blit_fb, &edpy->guest_fb,
@@ -225,6 +238,8 @@ static void egl_headless_init(DisplayState *ds, DisplayOptions *opts)
 
       egl_dpys = g_ptr_array_new();
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
       for (idx = 0;; idx++) {
           DisplayGLCtx *ctx;
 
@@ -250,6 +265,8 @@ static void egl_headless_cleanup(void)
           return;
       }
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
       for (guint i = 0; i < egl_dpys->len; i++) {
           egl_dpy *edpy = g_ptr_array_index(egl_dpys, i);
 
ui/gtk-egl.c +12 -0
index 8623181336e9..d595916476d9 100644
@@ -426,6 +426,18 @@ void gtk_egl_init(DisplayGLMode mode)
       display_opengl = 1;
   }
 
+void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
+                           QemuDmaBuf *dmabuf)
+{
+#ifdef CONFIG_GBM
+    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
+
+    eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
+                   vc->gfx.esurface, vc->gfx.ectx);
+    gd_release_dmabuf(vc, dmabuf);
+#endif
+}
+
   int gd_egl_make_current(DisplayGLCtx *dgc,
                           QEMUGLContext ctx)
   {
ui/gtk-gl-area.c +11 -0
index 45c0c8c81a11..effd88f28721 100644
@@ -360,6 +360,17 @@ void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
   #endif
   }
 
+void gd_gl_area_release_dmabuf(DisplayChangeListener *dcl,
+                               QemuDmaBuf *dmabuf)
+{
+#ifdef CONFIG_GBM
+    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
+
+    gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
+    gd_release_dmabuf(vc, dmabuf);
+#endif
+}
+
   void gtk_gl_area_init(void)
   {
       display_opengl = 1;
ui/gtk.c +4 -9
index 55e45e8445bc..c615d35451b6 100644
@@ -584,21 +584,16 @@ static bool gd_has_dmabuf(DisplayChangeListener *dcl)
       return vc->gfx.has_dmabuf;
   }
 
-static void gd_gl_release_dmabuf(DisplayChangeListener *dcl,
-                                 QemuDmaBuf *dmabuf)
-{
   #ifdef CONFIG_GBM
-    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
-
+void gd_release_dmabuf(VirtualConsole *vc, QemuDmaBuf *dmabuf)
+{
       egl_dmabuf_release_texture(dmabuf);
       if (vc->gfx.guest_fb.dmabuf == dmabuf) {
           vc->gfx.guest_fb.dmabuf = NULL;
           vc->gfx.draw_submitted = false;
       }
-#endif
   }
 
-#ifdef CONFIG_GBM
   static void gd_gl_fence_cb(void *vcon)
   {
       VirtualConsole *vc = vcon;
@@ -639,7 +634,7 @@ static const DisplayChangeListenerOps dcl_gl_area_ops = {
       .dpy_gl_scanout_disable  = gd_gl_area_scanout_disable,
       .dpy_gl_update           = gd_gl_area_scanout_flush,
       .dpy_gl_scanout_dmabuf   = gd_gl_area_scanout_dmabuf,
-    .dpy_gl_release_dmabuf   = gd_gl_release_dmabuf,
+    .dpy_gl_release_dmabuf   = gd_gl_area_release_dmabuf,
       .dpy_has_dmabuf          = gd_has_dmabuf,
   };
 
@@ -673,7 +668,7 @@ static const DisplayChangeListenerOps dcl_egl_ops = {
       .dpy_gl_cursor_dmabuf    = gd_egl_cursor_dmabuf,
       .dpy_gl_cursor_position  = gd_egl_cursor_position,
       .dpy_gl_update           = gd_egl_flush,
-    .dpy_gl_release_dmabuf   = gd_gl_release_dmabuf,
+    .dpy_gl_release_dmabuf   = gd_egl_release_dmabuf,
       .dpy_has_dmabuf          = gd_has_dmabuf,
   };
 
ui/sdl2-gl.c +2 -0
index 46eff768c968..f41fc8bb9634 100644
@@ -200,6 +200,7 @@ void sdl2_gl_scanout_disable(DisplayChangeListener *dcl)
       struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
 
       assert(scon->opengl);
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
       scon->w = 0;
       scon->h = 0;
       sdl2_set_scanout_mode(scon, false);
@@ -288,6 +289,7 @@ void sdl2_gl_release_dmabuf(DisplayChangeListener *dcl,
   {
       struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
 
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
       egl_dmabuf_release_texture(dmabuf);
       if (scon->guest_fb.dmabuf == dmabuf) {
           scon->guest_fb.dmabuf = NULL;
ui/spice-display.c +2 -0
index 75c7df7bb5e0..22cd1e266caa 100644
@@ -1176,6 +1176,8 @@ static void qemu_spice_gl_release_dmabuf(DisplayChangeListener *dcl,
           ssd->guest_dmabuf = NULL;
           ssd->guest_dmabuf_refresh = false;
       }
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
       egl_dmabuf_release_texture(dmabuf);
   }
 
#17 lore raw

Re: [PATCH v4 13/24] ui: make GL context current in display backend callbacks

Hi

Quoted 40 lines · Akihiko Odaki
Quoted 20 lines · Marc-André Lureau

Hi

Quoted 16 lines · Akihiko Odaki
Quoted 14 lines · Marc-André Lureau

Display backend GL callbacks (scanout, flush, release, etc.) may be invoked from a different GL context, like virgl. Since framebuffer objects are per-context and textures require a valid context for deletion, each callback must ensure the correct context is active before performing GL operations.

Make each backend's own GL context current at the entry of every callback that performs GL work.

For GTK, the previously shared gd_gl_release_dmabuf is split into backend-specific gd_egl_release_dmabuf and gd_gl_area_release_dmabuf, matching the existing pattern where each backend manages its own context.

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

On the virgl side, restore ctx0 with virgl_renderer_force_ctx_0() after SET_SCANOUT, RESOURCE_FLUSH, and SET_SCANOUT_BLOB command handlers, since their display callbacks may have changed the current GL context.

Ok, it sounds like we need to save/restore context at each callback for now. Later on we should try to change the code to avoid that dance and defer it. Right?

We rather need to let callbacks make the UI context current instead of keeping the virgl context current to wait for the UI to finish processing those commands.

But now I have a second thought: actually the situation is way more complicated because theoretically one resource flush can update multiple scanouts with different contexts, not just one context. Using the last UI context for fence creation does not solve that problem.

Quoted 7 lines

Making the virgl context current will ensure predictable behavior at least. Fence will not work with those commands properly, but it is just yet another silent long-standing bug.

So this patch should have:

Ok, I assume I can send a PR and I can count on you for a quick follow up ? thanks!

Quoted 287 lines

Regards,
Akihiko Odaki

Quoted 281 lines
Quoted 281 lines
Quoted 33 lines
---
   hw/display/virtio-gpu-virgl.c |  4 ++++
   include/ui/gtk.h              |  5 +++++
   ui/egl-headless.c             | 17 +++++++++++++++++
   ui/gtk-egl.c                  | 12 ++++++++++++
   ui/gtk-gl-area.c              | 11 +++++++++++
   ui/gtk.c                      | 13 ++++---------
   ui/sdl2-gl.c                  |  2 ++
   ui/spice-display.c            |  2 ++
   8 files changed, 57 insertions(+), 9 deletions(-)
hw/display/virtio-gpu-virgl.c +3 -0
index 7f1d1e0c3276..0e43eacca406 100644
@@ -1091,9 +1091,12 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
           break;
       case VIRTIO_GPU_CMD_SET_SCANOUT:
           virgl_cmd_set_scanout(g, cmd);
+        /* Display callbacks may change the current GL context. */
+        virgl_renderer_force_ctx_0();
           break;
       case VIRTIO_GPU_CMD_RESOURCE_FLUSH:
           virgl_cmd_resource_flush(g, cmd);
+        virgl_renderer_force_ctx_0();
           break;
       case VIRTIO_GPU_CMD_RESOURCE_UNREF:
           virgl_cmd_resource_unref(g, cmd, &cmd_suspended);

hw/display/virtio-gpu-virgl.c void virtio_gpu_virgl_process_cmd(VirtIOGPU *g, · line 1136

Quoted 4 lines
hw/display/virtio-gpu-virgl.c +1 -0
@@ -1130,6 +1133,7 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
           break;
       case VIRTIO_GPU_CMD_SET_SCANOUT_BLOB:
           virgl_cmd_set_scanout_blob(g, cmd);
+        virgl_renderer_force_ctx_0();

I doubt that adding these virgl_renderer_force_ctx_0() calls is correct here. Since scanout and resource flush actually happen in the UI context, we actually need to use the UI context to fence those operations.

Regards,
Akihiko Odaki

Quoted 235 lines
hw/display/virtio-gpu-virgl.c +0 -0
           break;
   #endif
       default:
include/ui/gtk.h +5 -0
index b66b9ca5c519..6b62e9758813 100644
@@ -163,6 +163,7 @@ extern bool gtk_use_gl_area;
   void gd_update_windowsize(VirtualConsole *vc);
   void gd_update_monitor_refresh_rate(VirtualConsole *vc, GtkWidget *widget);
   void gd_gl_wait_sync(VirtualConsole *vc, void *sync);
+void gd_release_dmabuf(VirtualConsole *vc, QemuDmaBuf *dmabuf);
 
   /* ui/gtk-egl.c */
   void gd_egl_init(VirtualConsole *vc);
@@ -190,6 +191,8 @@ void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
                             uint32_t hot_x, uint32_t hot_y);
   void gd_egl_cursor_position(DisplayChangeListener *dcl,
                               uint32_t pos_x, uint32_t pos_y);
+void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
+                           QemuDmaBuf *dmabuf);
   void gd_egl_flush(DisplayChangeListener *dcl,
                     uint32_t x, uint32_t y, uint32_t w, uint32_t h);
   void *gd_egl_scanout_flush(DisplayChangeListener *dcl,
@@ -221,6 +224,8 @@ void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                   uint32_t x, uint32_t y,
                                   uint32_t w, uint32_t h,
                                   void *d3d_tex2d);
+void gd_gl_area_release_dmabuf(DisplayChangeListener *dcl,
+                               QemuDmaBuf *dmabuf);
   void gd_gl_area_scanout_disable(DisplayChangeListener *dcl);
   void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
                                 uint32_t x, uint32_t y, uint32_t w, uint32_t h);
ui/egl-headless.c +17 -0
index ba27efcf4c3e..c3f319c0960b 100644
@@ -52,6 +52,8 @@ static void egl_scanout_disable(DisplayChangeListener *dcl)
   {
       egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
       egl_fb_destroy(&edpy->guest_fb);
       egl_fb_destroy(&edpy->blit_fb);
   }
@@ -69,6 +71,9 @@ static void egl_scanout_texture(DisplayChangeListener *dcl,
 
       edpy->y_0_top = backing_y_0_top;
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
+
       /* source framebuffer */
       egl_fb_setup_for_tex(&edpy->guest_fb,
                            backing_width, backing_height, backing_id, false);
@@ -88,6 +93,8 @@ static void egl_scanout_dmabuf(DisplayChangeListener *dcl,
   {
       uint32_t width, height, texture;
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
       egl_dmabuf_import_texture(dmabuf);
       texture = qemu_dmabuf_get_texture(dmabuf);
       if (!texture) {
@@ -108,6 +115,8 @@ static void egl_cursor_dmabuf(DisplayChangeListener *dcl,
       uint32_t width, height, texture;
       egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
       if (dmabuf) {
           egl_dmabuf_import_texture(dmabuf);
           texture = qemu_dmabuf_get_texture(dmabuf);
@@ -126,6 +135,8 @@ static void egl_cursor_dmabuf(DisplayChangeListener *dcl,
   static void egl_release_dmabuf(DisplayChangeListener *dcl,
                                  QemuDmaBuf *dmabuf)
   {
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
       egl_dmabuf_release_texture(dmabuf);
   }
 
@@ -151,6 +162,8 @@ static void egl_scanout_flush(DisplayChangeListener *dcl,
       }
       assert(surface_format(edpy->ds) == PIXMAN_x8r8g8b8);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
       if (edpy->cursor_fb.texture) {
           /* have cursor -> render using textures */
           egl_texture_blit(edpy->gls, &edpy->blit_fb, &edpy->guest_fb,
@@ -225,6 +238,8 @@ static void egl_headless_init(DisplayState *ds, DisplayOptions *opts)
 
       egl_dpys = g_ptr_array_new();
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
       for (idx = 0;; idx++) {
           DisplayGLCtx *ctx;
 
@@ -250,6 +265,8 @@ static void egl_headless_cleanup(void)
           return;
       }
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
       for (guint i = 0; i < egl_dpys->len; i++) {
           egl_dpy *edpy = g_ptr_array_index(egl_dpys, i);
 
ui/gtk-egl.c +12 -0
index 8623181336e9..d595916476d9 100644
@@ -426,6 +426,18 @@ void gtk_egl_init(DisplayGLMode mode)
       display_opengl = 1;
   }
 
+void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
+                           QemuDmaBuf *dmabuf)
+{
+#ifdef CONFIG_GBM
+    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
+
+    eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
+                   vc->gfx.esurface, vc->gfx.ectx);
+    gd_release_dmabuf(vc, dmabuf);
+#endif
+}
+
   int gd_egl_make_current(DisplayGLCtx *dgc,
                           QEMUGLContext ctx)
   {
ui/gtk-gl-area.c +11 -0
index 45c0c8c81a11..effd88f28721 100644
@@ -360,6 +360,17 @@ void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
   #endif
   }
 
+void gd_gl_area_release_dmabuf(DisplayChangeListener *dcl,
+                               QemuDmaBuf *dmabuf)
+{
+#ifdef CONFIG_GBM
+    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
+
+    gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
+    gd_release_dmabuf(vc, dmabuf);
+#endif
+}
+
   void gtk_gl_area_init(void)
   {
       display_opengl = 1;
ui/gtk.c +4 -9
index 55e45e8445bc..c615d35451b6 100644
@@ -584,21 +584,16 @@ static bool gd_has_dmabuf(DisplayChangeListener *dcl)
       return vc->gfx.has_dmabuf;
   }
 
-static void gd_gl_release_dmabuf(DisplayChangeListener *dcl,
-                                 QemuDmaBuf *dmabuf)
-{
   #ifdef CONFIG_GBM
-    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
-
+void gd_release_dmabuf(VirtualConsole *vc, QemuDmaBuf *dmabuf)
+{
       egl_dmabuf_release_texture(dmabuf);
       if (vc->gfx.guest_fb.dmabuf == dmabuf) {
           vc->gfx.guest_fb.dmabuf = NULL;
           vc->gfx.draw_submitted = false;
       }
-#endif
   }
 
-#ifdef CONFIG_GBM
   static void gd_gl_fence_cb(void *vcon)
   {
       VirtualConsole *vc = vcon;
@@ -639,7 +634,7 @@ static const DisplayChangeListenerOps dcl_gl_area_ops = {
       .dpy_gl_scanout_disable  = gd_gl_area_scanout_disable,
       .dpy_gl_update           = gd_gl_area_scanout_flush,
       .dpy_gl_scanout_dmabuf   = gd_gl_area_scanout_dmabuf,
-    .dpy_gl_release_dmabuf   = gd_gl_release_dmabuf,
+    .dpy_gl_release_dmabuf   = gd_gl_area_release_dmabuf,
       .dpy_has_dmabuf          = gd_has_dmabuf,
   };
 
@@ -673,7 +668,7 @@ static const DisplayChangeListenerOps dcl_egl_ops = {
       .dpy_gl_cursor_dmabuf    = gd_egl_cursor_dmabuf,
       .dpy_gl_cursor_position  = gd_egl_cursor_position,
       .dpy_gl_update           = gd_egl_flush,
-    .dpy_gl_release_dmabuf   = gd_gl_release_dmabuf,
+    .dpy_gl_release_dmabuf   = gd_egl_release_dmabuf,
       .dpy_has_dmabuf          = gd_has_dmabuf,
   };
 
ui/sdl2-gl.c +2 -0
index 46eff768c968..f41fc8bb9634 100644
@@ -200,6 +200,7 @@ void sdl2_gl_scanout_disable(DisplayChangeListener *dcl)
       struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
 
       assert(scon->opengl);
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
       scon->w = 0;
       scon->h = 0;
       sdl2_set_scanout_mode(scon, false);
@@ -288,6 +289,7 @@ void sdl2_gl_release_dmabuf(DisplayChangeListener *dcl,
   {
       struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
 
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
       egl_dmabuf_release_texture(dmabuf);
       if (scon->guest_fb.dmabuf == dmabuf) {
           scon->guest_fb.dmabuf = NULL;
ui/spice-display.c +2 -0
index 75c7df7bb5e0..22cd1e266caa 100644
@@ -1176,6 +1176,8 @@ static void qemu_spice_gl_release_dmabuf(DisplayChangeListener *dcl,
           ssd->guest_dmabuf = NULL;
           ssd->guest_dmabuf_refresh = false;
       }
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
       egl_dmabuf_release_texture(dmabuf);
   }
 
#18 lore raw

Re: [PATCH v4 13/24] ui: make GL context current in display backend callbacks

Quoted 48 lines · Marc-André Lureau

Hi

Quoted 44 lines · Akihiko Odaki
Quoted 20 lines · Marc-André Lureau

Hi

Quoted 16 lines · Akihiko Odaki
Quoted 14 lines · Marc-André Lureau

Display backend GL callbacks (scanout, flush, release, etc.) may be invoked from a different GL context, like virgl. Since framebuffer objects are per-context and textures require a valid context for deletion, each callback must ensure the correct context is active before performing GL operations.

Make each backend's own GL context current at the entry of every callback that performs GL work.

For GTK, the previously shared gd_gl_release_dmabuf is split into backend-specific gd_egl_release_dmabuf and gd_gl_area_release_dmabuf, matching the existing pattern where each backend manages its own context.

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

On the virgl side, restore ctx0 with virgl_renderer_force_ctx_0() after SET_SCANOUT, RESOURCE_FLUSH, and SET_SCANOUT_BLOB command handlers, since their display callbacks may have changed the current GL context.

Ok, it sounds like we need to save/restore context at each callback for now. Later on we should try to change the code to avoid that dance and defer it. Right?

We rather need to let callbacks make the UI context current instead of keeping the virgl context current to wait for the UI to finish processing those commands.

But now I have a second thought: actually the situation is way more complicated because theoretically one resource flush can update multiple scanouts with different contexts, not just one context. Using the last UI context for fence creation does not solve that problem.

Making the virgl context current will ensure predictable behavior at least. Fence will not work with those commands properly, but it is just yet another silent long-standing bug.

Quoted 6 lines
Quoted 3 lines

So this patch should have:

Ok, I assume I can send a PR and I can count on you for a quick follow up ? thanks!

Probably I won't "quickly" provide a fix for that issue, I have to say. This is part of an issue I have been suspecting for long time.

I discussed the completion fence in this thread, but we also need to synchronize the starting timing of operations. In summary, we need to ensure the following happens in order:

1. The guest writes the texture in the virgl context.
2. The host reads the texture for scanout in the UI context.
3. The guest gets notified for scanout completion (and may start overwriting the texture).

What I said is synchronization between 2 and 3 is missing, and leaving the UI context current could solve it (and I was wrong because there could be multiple UI contexts).

But the fact is that we almost completely lack synchronization at all i.e., we also lack synchronization between 1 and 2. We actually need to make use of glFenceSync() but QEMU just doesn't use it at all. Fixing it is less trivial because we need to audit all OpenGL-capable UI implementations.

Fortunately (or maybe unfortunate because that made us miss the potential bug), OpenGL implementations often tolerate the lack of synchronization and things just work. Unlike the CPU-side C code, catastrophic consequences of use-after-free and deadlocks are unlikely to happen either. It is true that we still rely on undefined behavior and may have glitches in some case.

It is more likely that I would simply acknowledge the presence of the issue with some analysis at the upcoming KVM Forum:
https://pretalx.com/kvm-forum-2026/talk/KDRWGB/

Regards,
Akihiko Odaki

Quoted 288 lines
Quoted 288 lines

Regards,
Akihiko Odaki

Quoted 281 lines
Quoted 281 lines
Quoted 33 lines
---
    hw/display/virtio-gpu-virgl.c |  4 ++++
    include/ui/gtk.h              |  5 +++++
    ui/egl-headless.c             | 17 +++++++++++++++++
    ui/gtk-egl.c                  | 12 ++++++++++++
    ui/gtk-gl-area.c              | 11 +++++++++++
    ui/gtk.c                      | 13 ++++---------
    ui/sdl2-gl.c                  |  2 ++
    ui/spice-display.c            |  2 ++
    8 files changed, 57 insertions(+), 9 deletions(-)
hw/display/virtio-gpu-virgl.c +3 -0
index 7f1d1e0c3276..0e43eacca406 100644
@@ -1091,9 +1091,12 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
            break;
        case VIRTIO_GPU_CMD_SET_SCANOUT:
            virgl_cmd_set_scanout(g, cmd);
+        /* Display callbacks may change the current GL context. */
+        virgl_renderer_force_ctx_0();
            break;
        case VIRTIO_GPU_CMD_RESOURCE_FLUSH:
            virgl_cmd_resource_flush(g, cmd);
+        virgl_renderer_force_ctx_0();
            break;
        case VIRTIO_GPU_CMD_RESOURCE_UNREF:
            virgl_cmd_resource_unref(g, cmd, &cmd_suspended);

hw/display/virtio-gpu-virgl.c void virtio_gpu_virgl_process_cmd(VirtIOGPU *g, · line 1136

Quoted 4 lines
hw/display/virtio-gpu-virgl.c +1 -0
@@ -1130,6 +1133,7 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
            break;
        case VIRTIO_GPU_CMD_SET_SCANOUT_BLOB:
            virgl_cmd_set_scanout_blob(g, cmd);
+        virgl_renderer_force_ctx_0();

I doubt that adding these virgl_renderer_force_ctx_0() calls is correct here. Since scanout and resource flush actually happen in the UI context, we actually need to use the UI context to fence those operations.

Regards,
Akihiko Odaki

Quoted 235 lines
hw/display/virtio-gpu-virgl.c +0 -0
            break;
    #endif
        default:
include/ui/gtk.h +5 -0
index b66b9ca5c519..6b62e9758813 100644
@@ -163,6 +163,7 @@ extern bool gtk_use_gl_area;
    void gd_update_windowsize(VirtualConsole *vc);
    void gd_update_monitor_refresh_rate(VirtualConsole *vc, GtkWidget *widget);
    void gd_gl_wait_sync(VirtualConsole *vc, void *sync);
+void gd_release_dmabuf(VirtualConsole *vc, QemuDmaBuf *dmabuf);
 
    /* ui/gtk-egl.c */
    void gd_egl_init(VirtualConsole *vc);
@@ -190,6 +191,8 @@ void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
                              uint32_t hot_x, uint32_t hot_y);
    void gd_egl_cursor_position(DisplayChangeListener *dcl,
                                uint32_t pos_x, uint32_t pos_y);
+void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
+                           QemuDmaBuf *dmabuf);
    void gd_egl_flush(DisplayChangeListener *dcl,
                      uint32_t x, uint32_t y, uint32_t w, uint32_t h);
    void *gd_egl_scanout_flush(DisplayChangeListener *dcl,
@@ -221,6 +224,8 @@ void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                    uint32_t x, uint32_t y,
                                    uint32_t w, uint32_t h,
                                    void *d3d_tex2d);
+void gd_gl_area_release_dmabuf(DisplayChangeListener *dcl,
+                               QemuDmaBuf *dmabuf);
    void gd_gl_area_scanout_disable(DisplayChangeListener *dcl);
    void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
                                  uint32_t x, uint32_t y, uint32_t w, uint32_t h);
ui/egl-headless.c +17 -0
index ba27efcf4c3e..c3f319c0960b 100644
@@ -52,6 +52,8 @@ static void egl_scanout_disable(DisplayChangeListener *dcl)
    {
        egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
        egl_fb_destroy(&edpy->guest_fb);
        egl_fb_destroy(&edpy->blit_fb);
    }
@@ -69,6 +71,9 @@ static void egl_scanout_texture(DisplayChangeListener *dcl,
 
        edpy->y_0_top = backing_y_0_top;
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
+
        /* source framebuffer */
        egl_fb_setup_for_tex(&edpy->guest_fb,
                             backing_width, backing_height, backing_id, false);
@@ -88,6 +93,8 @@ static void egl_scanout_dmabuf(DisplayChangeListener *dcl,
    {
        uint32_t width, height, texture;
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
        egl_dmabuf_import_texture(dmabuf);
        texture = qemu_dmabuf_get_texture(dmabuf);
        if (!texture) {
@@ -108,6 +115,8 @@ static void egl_cursor_dmabuf(DisplayChangeListener *dcl,
        uint32_t width, height, texture;
        egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
        if (dmabuf) {
            egl_dmabuf_import_texture(dmabuf);
            texture = qemu_dmabuf_get_texture(dmabuf);
@@ -126,6 +135,8 @@ static void egl_cursor_dmabuf(DisplayChangeListener *dcl,
    static void egl_release_dmabuf(DisplayChangeListener *dcl,
                                   QemuDmaBuf *dmabuf)
    {
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
        egl_dmabuf_release_texture(dmabuf);
    }
 
@@ -151,6 +162,8 @@ static void egl_scanout_flush(DisplayChangeListener *dcl,
        }
        assert(surface_format(edpy->ds) == PIXMAN_x8r8g8b8);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
        if (edpy->cursor_fb.texture) {
            /* have cursor -> render using textures */
            egl_texture_blit(edpy->gls, &edpy->blit_fb, &edpy->guest_fb,
@@ -225,6 +238,8 @@ static void egl_headless_init(DisplayState *ds, DisplayOptions *opts)
 
        egl_dpys = g_ptr_array_new();
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
        for (idx = 0;; idx++) {
            DisplayGLCtx *ctx;
 
@@ -250,6 +265,8 @@ static void egl_headless_cleanup(void)
            return;
        }
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
        for (guint i = 0; i < egl_dpys->len; i++) {
            egl_dpy *edpy = g_ptr_array_index(egl_dpys, i);
 
ui/gtk-egl.c +12 -0
index 8623181336e9..d595916476d9 100644
@@ -426,6 +426,18 @@ void gtk_egl_init(DisplayGLMode mode)
        display_opengl = 1;
    }
 
+void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
+                           QemuDmaBuf *dmabuf)
+{
+#ifdef CONFIG_GBM
+    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
+
+    eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
+                   vc->gfx.esurface, vc->gfx.ectx);
+    gd_release_dmabuf(vc, dmabuf);
+#endif
+}
+
    int gd_egl_make_current(DisplayGLCtx *dgc,
                            QEMUGLContext ctx)
    {
ui/gtk-gl-area.c +11 -0
index 45c0c8c81a11..effd88f28721 100644
@@ -360,6 +360,17 @@ void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
    #endif
    }
 
+void gd_gl_area_release_dmabuf(DisplayChangeListener *dcl,
+                               QemuDmaBuf *dmabuf)
+{
+#ifdef CONFIG_GBM
+    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
+
+    gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
+    gd_release_dmabuf(vc, dmabuf);
+#endif
+}
+
    void gtk_gl_area_init(void)
    {
        display_opengl = 1;
ui/gtk.c +4 -9
index 55e45e8445bc..c615d35451b6 100644
@@ -584,21 +584,16 @@ static bool gd_has_dmabuf(DisplayChangeListener *dcl)
        return vc->gfx.has_dmabuf;
    }
 
-static void gd_gl_release_dmabuf(DisplayChangeListener *dcl,
-                                 QemuDmaBuf *dmabuf)
-{
    #ifdef CONFIG_GBM
-    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
-
+void gd_release_dmabuf(VirtualConsole *vc, QemuDmaBuf *dmabuf)
+{
        egl_dmabuf_release_texture(dmabuf);
        if (vc->gfx.guest_fb.dmabuf == dmabuf) {
            vc->gfx.guest_fb.dmabuf = NULL;
            vc->gfx.draw_submitted = false;
        }
-#endif
    }
 
-#ifdef CONFIG_GBM
    static void gd_gl_fence_cb(void *vcon)
    {
        VirtualConsole *vc = vcon;
@@ -639,7 +634,7 @@ static const DisplayChangeListenerOps dcl_gl_area_ops = {
        .dpy_gl_scanout_disable  = gd_gl_area_scanout_disable,
        .dpy_gl_update           = gd_gl_area_scanout_flush,
        .dpy_gl_scanout_dmabuf   = gd_gl_area_scanout_dmabuf,
-    .dpy_gl_release_dmabuf   = gd_gl_release_dmabuf,
+    .dpy_gl_release_dmabuf   = gd_gl_area_release_dmabuf,
        .dpy_has_dmabuf          = gd_has_dmabuf,
    };
 
@@ -673,7 +668,7 @@ static const DisplayChangeListenerOps dcl_egl_ops = {
        .dpy_gl_cursor_dmabuf    = gd_egl_cursor_dmabuf,
        .dpy_gl_cursor_position  = gd_egl_cursor_position,
        .dpy_gl_update           = gd_egl_flush,
-    .dpy_gl_release_dmabuf   = gd_gl_release_dmabuf,
+    .dpy_gl_release_dmabuf   = gd_egl_release_dmabuf,
        .dpy_has_dmabuf          = gd_has_dmabuf,
    };
 
ui/sdl2-gl.c +2 -0
index 46eff768c968..f41fc8bb9634 100644
@@ -200,6 +200,7 @@ void sdl2_gl_scanout_disable(DisplayChangeListener *dcl)
        struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
 
        assert(scon->opengl);
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
        scon->w = 0;
        scon->h = 0;
        sdl2_set_scanout_mode(scon, false);
@@ -288,6 +289,7 @@ void sdl2_gl_release_dmabuf(DisplayChangeListener *dcl,
    {
        struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
 
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
        egl_dmabuf_release_texture(dmabuf);
        if (scon->guest_fb.dmabuf == dmabuf) {
            scon->guest_fb.dmabuf = NULL;
ui/spice-display.c +2 -0
index 75c7df7bb5e0..22cd1e266caa 100644
@@ -1176,6 +1176,8 @@ static void qemu_spice_gl_release_dmabuf(DisplayChangeListener *dcl,
            ssd->guest_dmabuf = NULL;
            ssd->guest_dmabuf_refresh = false;
        }
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
        egl_dmabuf_release_texture(dmabuf);
    }
 
#19 lore raw

Re: [PATCH v4 13/24] ui: make GL context current in display backend callbacks

Quoted 49 lines · Akihiko Odaki
Quoted 48 lines · Marc-André Lureau

Hi

Quoted 44 lines · Akihiko Odaki
Quoted 20 lines · Marc-André Lureau

Hi

Quoted 16 lines · Akihiko Odaki
Quoted 14 lines · Marc-André Lureau

Display backend GL callbacks (scanout, flush, release, etc.) may be invoked from a different GL context, like virgl. Since framebuffer objects are per-context and textures require a valid context for deletion, each callback must ensure the correct context is active before performing GL operations.

Make each backend's own GL context current at the entry of every callback that performs GL work.

For GTK, the previously shared gd_gl_release_dmabuf is split into backend-specific gd_egl_release_dmabuf and gd_gl_area_release_dmabuf, matching the existing pattern where each backend manages its own context.

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

On the virgl side, restore ctx0 with virgl_renderer_force_ctx_0() after SET_SCANOUT, RESOURCE_FLUSH, and SET_SCANOUT_BLOB command handlers, since their display callbacks may have changed the current GL context.

Ok, it sounds like we need to save/restore context at each callback for now. Later on we should try to change the code to avoid that dance and defer it. Right?

We rather need to let callbacks make the UI context current instead of keeping the virgl context current to wait for the UI to finish processing those commands.

But now I have a second thought: actually the situation is way more complicated because theoretically one resource flush can update multiple scanouts with different contexts, not just one context. Using the last UI context for fence creation does not solve that problem.

Making the virgl context current will ensure predictable behavior at least. Fence will not work with those commands properly, but it is just yet another silent long-standing bug.

Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

So this patch should have:

Ok, I assume I can send a PR and I can count on you for a quick follow up ? thanks!

I forgot to tell this: the PR looks good to me.

Quoted 329 lines

Probably I won't "quickly" provide a fix for that issue, I have to say. This is part of an issue I have been suspecting for long time.

I discussed the completion fence in this thread, but we also need to synchronize the starting timing of operations. In summary, we need to ensure the following happens in order:

1. The guest writes the texture in the virgl context.
2. The host reads the texture for scanout in the UI context.
3. The guest gets notified for scanout completion (and may start overwriting the texture).

What I said is synchronization between 2 and 3 is missing, and leaving the UI context current could solve it (and I was wrong because there could be multiple UI contexts).

But the fact is that we almost completely lack synchronization at all i.e., we also lack synchronization between 1 and 2. We actually need to make use of glFenceSync() but QEMU just doesn't use it at all. Fixing it is less trivial because we need to audit all OpenGL-capable UI implementations.

Fortunately (or maybe unfortunate because that made us miss the potential bug), OpenGL implementations often tolerate the lack of synchronization and things just work. Unlike the CPU-side C code, catastrophic consequences of use-after-free and deadlocks are unlikely to happen either. It is true that we still rely on undefined behavior and may have glitches in some case.

It is more likely that I would simply acknowledge the presence of the issue with some analysis at the upcoming KVM Forum:
https://pretalx.com/kvm-forum-2026/talk/KDRWGB/

Regards,
Akihiko Odaki

Quoted 306 lines
Quoted 306 lines

Regards,
Akihiko Odaki

Quoted 299 lines
Quoted 299 lines
Quoted 35 lines

---
    hw/display/virtio-gpu-virgl.c |  4 ++++ include/ui/gtk.h              |  5 +++++ ui/egl-headless.c             | 17 +++++++++++++++++ ui/gtk-egl.c                  | 12 ++++++++++++ ui/gtk-gl-area.c              | 11 +++++++++++ ui/gtk.c                      | 13 ++++--------- ui/sdl2-gl.c                  |  2 ++ ui/spice-display.c            |  2 ++

    8 files changed, 57 insertions(+), 9 deletions(-)
hw/display/virtio-gpu-virgl.c +0 -0
index 7f1d1e0c3276..0e43eacca406 100644
@@ -1091,9 +1091,12 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU 
 *g,
             break;

        case VIRTIO_GPU_CMD_SET_SCANOUT:
            virgl_cmd_set_scanout(g, cmd);
+        /* Display callbacks may change the current GL context. */ +        virgl_renderer_force_ctx_0();
            break;
        case VIRTIO_GPU_CMD_RESOURCE_FLUSH:
            virgl_cmd_resource_flush(g, cmd);
+        virgl_renderer_force_ctx_0();
            break;
        case VIRTIO_GPU_CMD_RESOURCE_UNREF:
            virgl_cmd_resource_unref(g, cmd, &cmd_suspended);

hw/display/virtio-gpu-virgl.c +0 -0
@@ -1130,6 +1133,7 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
             break;
Quoted 3 lines

        case VIRTIO_GPU_CMD_SET_SCANOUT_BLOB:
            virgl_cmd_set_scanout_blob(g, cmd);
+        virgl_renderer_force_ctx_0();

I doubt that adding these virgl_renderer_force_ctx_0() calls is correct here. Since scanout and resource flush actually happen in the UI context, we actually need to use the UI context to fence those operations.

Regards,
Akihiko Odaki

Quoted 252 lines

            break;
    #endif
        default:

include/ui/gtk.h +3 -0
index b66b9ca5c519..6b62e9758813 100644
@@ -163,6 +163,7 @@ extern bool gtk_use_gl_area;
     void gd_update_windowsize(VirtualConsole *vc);
     void gd_update_monitor_refresh_rate(VirtualConsole *vc, GtkWidget *widget);
     void gd_gl_wait_sync(VirtualConsole *vc, void *sync);
+void gd_release_dmabuf(VirtualConsole *vc, QemuDmaBuf *dmabuf);
 
     /* ui/gtk-egl.c */
     void gd_egl_init(VirtualConsole *vc);
@@ -190,6 +191,8 @@ void 
 gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
                               uint32_t hot_x, uint32_t hot_y);
     void gd_egl_cursor_position(DisplayChangeListener *dcl,
                                 uint32_t pos_x, uint32_t pos_y);
+void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
+                           QemuDmaBuf *dmabuf);
     void gd_egl_flush(DisplayChangeListener *dcl,
                       uint32_t x, uint32_t y, uint32_t w, uint32_t h);

    void *gd_egl_scanout_flush(DisplayChangeListener *dcl,

include/ui/gtk.h +2 -0
@@ -221,6 +224,8 @@ void 
 gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                     uint32_t x, uint32_t y,
                                     uint32_t w, uint32_t h,
                                     void *d3d_tex2d);
+void gd_gl_area_release_dmabuf(DisplayChangeListener *dcl,
+                               QemuDmaBuf *dmabuf);
     void gd_gl_area_scanout_disable(DisplayChangeListener *dcl);
     void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,

                                  uint32_t x, uint32_t y, uint32_t w, uint32_t h);

ui/egl-headless.c +2 -0
index ba27efcf4c3e..c3f319c0960b 100644
@@ -52,6 +52,8 @@ static void 
 egl_scanout_disable(DisplayChangeListener *dcl)
     {
         egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
         egl_fb_destroy(&edpy->guest_fb);
         egl_fb_destroy(&edpy->blit_fb);

    }

ui/egl-headless.c +3 -0
@@ -69,6 +71,9 @@ static void 
 egl_scanout_texture(DisplayChangeListener *dcl,
 
         edpy->y_0_top = backing_y_0_top;
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
+
         /* source framebuffer */
         egl_fb_setup_for_tex(&edpy->guest_fb,

                             backing_width, backing_height, backing_id, false);

ui/egl-headless.c +2 -0
@@ -88,6 +93,8 @@ static void 
 egl_scanout_dmabuf(DisplayChangeListener *dcl,
     {
         uint32_t width, height, texture;
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
         egl_dmabuf_import_texture(dmabuf);
         texture = qemu_dmabuf_get_texture(dmabuf);

        if (!texture) {

ui/egl-headless.c +2 -0
@@ -108,6 +115,8 @@ static void 
 egl_cursor_dmabuf(DisplayChangeListener *dcl,
         uint32_t width, height, texture;
         egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
         if (dmabuf) {
             egl_dmabuf_import_texture(dmabuf);

            texture = qemu_dmabuf_get_texture(dmabuf);

ui/egl-headless.c +4 -0
@@ -126,6 +135,8 @@ static void 
 egl_cursor_dmabuf(DisplayChangeListener *dcl,
     static void egl_release_dmabuf(DisplayChangeListener *dcl,
                                    QemuDmaBuf *dmabuf)
     {
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
         egl_dmabuf_release_texture(dmabuf);
     }
@@ -151,6 +162,8 @@ static void 
 egl_scanout_flush(DisplayChangeListener *dcl,
         }
         assert(surface_format(edpy->ds) == PIXMAN_x8r8g8b8);
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
         if (edpy->cursor_fb.texture) {
             /* have cursor -> render using textures */

            egl_texture_blit(edpy->gls, &edpy->blit_fb, &edpy-

Quoted 1 line

guest_fb,

ui/egl-headless.c +4 -0
@@ -225,6 +238,8 @@ static void egl_headless_init(DisplayState 
 *ds, DisplayOptions *opts)
 
         egl_dpys = g_ptr_array_new();
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
         for (idx = 0;; idx++) {
             DisplayGLCtx *ctx;
@@ -250,6 +265,8 @@ static void egl_headless_cleanup(void)
             return;
         }
 
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
         for (guint i = 0; i < egl_dpys->len; i++) {
             egl_dpy *edpy = g_ptr_array_index(egl_dpys, i);
 
ui/gtk-egl.c +12 -0
index 8623181336e9..d595916476d9 100644
@@ -426,6 +426,18 @@ void gtk_egl_init(DisplayGLMode mode)
         display_opengl = 1;
     }
 
+void gd_egl_release_dmabuf(DisplayChangeListener *dcl,
+                           QemuDmaBuf *dmabuf)
+{
+#ifdef CONFIG_GBM
+    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
+
+    eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
+                   vc->gfx.esurface, vc->gfx.ectx);
+    gd_release_dmabuf(vc, dmabuf);
+#endif
+}
+
     int gd_egl_make_current(DisplayGLCtx *dgc,
                             QEMUGLContext ctx)
     {
ui/gtk-gl-area.c +11 -0
index 45c0c8c81a11..effd88f28721 100644
@@ -360,6 +360,17 @@ void 
 gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
     #endif
     }
 
+void gd_gl_area_release_dmabuf(DisplayChangeListener *dcl,
+                               QemuDmaBuf *dmabuf)
+{
+#ifdef CONFIG_GBM
+    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
+
+    gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
+    gd_release_dmabuf(vc, dmabuf);
+#endif
+}
+
     void gtk_gl_area_init(void)
     {

        display_opengl = 1;

ui/gtk.c +2 -7
index 55e45e8445bc..c615d35451b6 100644
@@ -584,21 +584,16 @@ static bool 
 gd_has_dmabuf(DisplayChangeListener *dcl)
         return vc->gfx.has_dmabuf;
     }
 
-static void gd_gl_release_dmabuf(DisplayChangeListener *dcl,
-                                 QemuDmaBuf *dmabuf)
-{
     #ifdef CONFIG_GBM
-    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
-
+void gd_release_dmabuf(VirtualConsole *vc, QemuDmaBuf *dmabuf)
+{
         egl_dmabuf_release_texture(dmabuf);
         if (vc->gfx.guest_fb.dmabuf == dmabuf) {
             vc->gfx.guest_fb.dmabuf = NULL;
             vc->gfx.draw_submitted = false;
         }
-#endif
     }
 
-#ifdef CONFIG_GBM
     static void gd_gl_fence_cb(void *vcon)
     {

        VirtualConsole *vc = vcon;

ui/gtk.c +2 -2
@@ -639,7 +634,7 @@ static const DisplayChangeListenerOps 
 dcl_gl_area_ops = {
         .dpy_gl_scanout_disable  = gd_gl_area_scanout_disable,
         .dpy_gl_update           = gd_gl_area_scanout_flush,
         .dpy_gl_scanout_dmabuf   = gd_gl_area_scanout_dmabuf,
-    .dpy_gl_release_dmabuf   = gd_gl_release_dmabuf,
+    .dpy_gl_release_dmabuf   = gd_gl_area_release_dmabuf,
         .dpy_has_dmabuf          = gd_has_dmabuf,
     };
@@ -673,7 +668,7 @@ static const DisplayChangeListenerOps 
 dcl_egl_ops = {
         .dpy_gl_cursor_dmabuf    = gd_egl_cursor_dmabuf,
         .dpy_gl_cursor_position  = gd_egl_cursor_position,
         .dpy_gl_update           = gd_egl_flush,
-    .dpy_gl_release_dmabuf   = gd_gl_release_dmabuf,
+    .dpy_gl_release_dmabuf   = gd_egl_release_dmabuf,
         .dpy_has_dmabuf          = gd_has_dmabuf,
     };
ui/sdl2-gl.c +1 -0
index 46eff768c968..f41fc8bb9634 100644
@@ -200,6 +200,7 @@ void 
 sdl2_gl_scanout_disable(DisplayChangeListener *dcl)
         struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
 
         assert(scon->opengl);
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
         scon->w = 0;
         scon->h = 0;

        sdl2_set_scanout_mode(scon, false);

ui/sdl2-gl.c +1 -0
@@ -288,6 +289,7 @@ void 
 sdl2_gl_release_dmabuf(DisplayChangeListener *dcl,
     {
         struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
 
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
         egl_dmabuf_release_texture(dmabuf);
         if (scon->guest_fb.dmabuf == dmabuf) {

            scon->guest_fb.dmabuf = NULL;

ui/spice-display.c +2 -0
index 75c7df7bb5e0..22cd1e266caa 100644
@@ -1176,6 +1176,8 @@ static void 
 qemu_spice_gl_release_dmabuf(DisplayChangeListener *dcl,
             ssd->guest_dmabuf = NULL;
             ssd->guest_dmabuf_refresh = false;
         }
+    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE,
+                   EGL_NO_SURFACE, qemu_egl_rn_ctx);
         egl_dmabuf_release_texture(dmabuf);
     }
#20 lore raw

PATCH v4 14/24 virtio-gpu: release the dmabuf when a scanout is disabled

In reply to Marc-André Lureau

Rather than waiting for the next set_scanout()/update_dmabuf() to replace it.

---
 hw/display/virtio-gpu.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
hw/display/virtio-gpu.c +15 -4
index ecf3cbc4893c..bcde65b50349 100644
@@ -418,20 +418,31 @@ static void virtio_gpu_resource_create_blob(VirtIOGPU *g,
     QTAILQ_INSERT_HEAD(&g->reslist, res, next);
 }
 
+static void virtio_gpu_release_scanout_dmabuf(VirtIOGPU *g, int scanout_id)
+{
+    struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
+    g_autoptr(QemuDmaBuf) dmabuf = scanout->dmabuf;
+
+    if (!dmabuf) {
+        return;
+    }
+
+    scanout->dmabuf = NULL;
+    qemu_console_gl_release_dmabuf(scanout->con, dmabuf);
+}
+
 void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id)
 {
     struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
     struct virtio_gpu_simple_resource *res;
 
-    if (scanout->resource_id == 0) {
-        return;
-    }
-
     res = virtio_gpu_find_resource(g, scanout->resource_id);
     if (res) {
         res->scanout_bitmask &= ~(1 << scanout_id);
     }
 
+    virtio_gpu_release_scanout_dmabuf(g, scanout_id);
+
     qemu_console_set_surface(scanout->con, NULL);
     scanout->resource_id = 0;
     scanout->ds = NULL;
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#21 lore raw

PATCH v4 15/24 contrib/vhost-user-gpu: drop scanout_bitmask bookkeeping

In reply to Marc-André Lureau

The per-resource scanout_bitmask is a reverse index of which scanouts reference a given resource. The information is already available from scanout->resource_id. Performance impact should be negligible.

---
 contrib/vhost-user-gpu/vhost-user-gpu.c | 28 +++++++---------------------
 1 file changed, 7 insertions(+), 21 deletions(-)
contrib/vhost-user-gpu/vhost-user-gpu.c +7 -21
index 933bdbb671c0..f70b34c179f3 100644
@@ -36,7 +36,6 @@ struct virtio_gpu_simple_resource {
     uint32_t format;
     struct iovec *iov;
     unsigned int iov_cnt;
-    uint32_t scanout_bitmask;
     pixman_image_t *image;
     struct vugbm_buffer buffer;
     QTAILQ_ENTRY(virtio_gpu_simple_resource) next;
@@ -416,17 +415,12 @@ static void
 vg_disable_scanout(VuGpu *g, int scanout_id)
 {
     struct virtio_gpu_scanout *scanout = &g->scanout[scanout_id];
-    struct virtio_gpu_simple_resource *res;
 
     if (scanout->resource_id == 0) {
         return;
     }
 
-    res = virtio_gpu_find_resource(g, scanout->resource_id);
-    if (res) {
-        res->scanout_bitmask &= ~(1 << scanout_id);
-    }
-
+    scanout->resource_id = 0;
     scanout->width = 0;
     scanout->height = 0;
 
@@ -446,11 +440,9 @@ vg_resource_destroy(VuGpu *g,
 {
     int i;
 
-    if (res->scanout_bitmask) {
-        for (i = 0; i < VIRTIO_GPU_MAX_SCANOUTS; i++) {
-            if (res->scanout_bitmask & (1 << i)) {
-                vg_disable_scanout(g, i);
-            }
+    for (i = 0; i < VIRTIO_GPU_MAX_SCANOUTS; i++) {
+        if (g->scanout[i].resource_id == res->resource_id) {
+            vg_disable_scanout(g, i);
         }
     }
 
@@ -662,7 +654,7 @@ static void
 vg_set_scanout(VuGpu *g,
                struct virtio_gpu_ctrl_command *cmd)
 {
-    struct virtio_gpu_simple_resource *res, *ores;
+    struct virtio_gpu_simple_resource *res;
     struct virtio_gpu_scanout *scanout;
     struct virtio_gpu_set_scanout ss;
     int fd;
@@ -707,12 +699,6 @@ vg_set_scanout(VuGpu *g,
 
     scanout = &g->scanout[ss.scanout_id];
 
-    ores = virtio_gpu_find_resource(g, scanout->resource_id);
-    if (ores) {
-        ores->scanout_bitmask &= ~(1 << ss.scanout_id);
-    }
-
-    res->scanout_bitmask |= (1 << ss.scanout_id);
     scanout->resource_id = ss.resource_id;
     scanout->x = ss.r.x;
     scanout->y = ss.r.y;
@@ -797,10 +783,10 @@ vg_resource_flush(VuGpu *g,
         pixman_region16_t region, finalregion;
         pixman_box16_t *extents;
 
-        if (!(res->scanout_bitmask & (1 << i))) {
+        scanout = &g->scanout[i];
+        if (scanout->resource_id != res->resource_id) {
             continue;
         }
-        scanout = &g->scanout[i];
 
         pixman_region_init(&finalregion);
         pixman_region_init_rect(&region, scanout->x, scanout->y,
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#22 lore raw

PATCH v4 16/24 virtio-gpu-rutabaga: fix scanout handling

In reply to Marc-André Lureau

The rutabaga backend was misusing the scanout_bitmask field as a plain scanout ID instead of a bitmask.

Fix the scanout lookup to use scanout->resource_id directly. Also fix the missing resource_id clear when disabling a scanout, and flush all matching scanouts instead of just the first one.

Fixes 1dcc6adbc168 ("gfxstream + rutabaga: add initial support for gfxstream")
---
 hw/display/virtio-gpu-rutabaga.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
hw/display/virtio-gpu-rutabaga.c +17 -3
index b8f8a2068d0b..8a82e96b7461 100644
@@ -146,8 +146,15 @@ virtio_gpu_rutabaga_resource_unref(VirtIOGPU *g,
                                    Error **errp)
 {
     int32_t result;
+    int i;
     VirtIOGPURutabaga *vr = VIRTIO_GPU_RUTABAGA(g);
 
+    for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
+        if (g->parent_obj.scanout[i].resource_id == res->resource_id) {
+            virtio_gpu_disable_scanout(g, i);
+        }
+    }
+
     result = rutabaga_resource_unref(vr->rutabaga, res->resource_id);
     if (result) {
         error_setg_errno(errp,
@@ -251,7 +258,7 @@ rutabaga_cmd_resource_flush(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
 
     for (i = 0; i < vb->conf.max_outputs; i++) {
         scanout = &vb->scanout[i];
-        if (i == res->scanout_bitmask) {
+        if (scanout->resource_id == res->resource_id) {
             found = true;
             break;
         }
@@ -275,7 +282,13 @@ rutabaga_cmd_resource_flush(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
                                              rf.resource_id, &transfer,
                                              &transfer_iovec);
     CHECK(!result, cmd);
-    qemu_console_update_full(scanout->con);
+
+    for (i = 0; i < vb->conf.max_outputs; i++) {
+        scanout = &vb->scanout[i];
+        if (scanout->resource_id == res->resource_id) {
+            qemu_console_update_full(scanout->con);
+        }
+    }
 }
 
 static void
@@ -299,6 +312,7 @@ rutabaga_cmd_set_scanout(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
     scanout = &vb->scanout[ss.scanout_id];
 
     if (ss.resource_id == 0) {
+        scanout->resource_id = 0;
         qemu_console_set_surface(scanout->con, NULL);
         qemu_console_gl_scanout_disable(scanout->con);
         return;
@@ -332,7 +346,7 @@ rutabaga_cmd_set_scanout(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
     scanout->ds = qemu_create_displaysurface_pixman(res->image);
     qemu_console_set_surface(scanout->con, NULL);
     qemu_console_set_surface(scanout->con, scanout->ds);
-    res->scanout_bitmask = ss.scanout_id;
+    scanout->resource_id = ss.resource_id;
 }
 
 static void
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#23 lore raw

PATCH v4 17/24 virtio-gpu: drop scanout_bitmask bookkeeping

In reply to Marc-André Lureau

The per-resource scanout_bitmask is a reverse index of which scanouts reference a given resource. The information is already available from scanout->resource_id. Performance impact should be negligible.

---
 hw/display/virtio-gpu.c        | 26 +++++---------------------
 include/hw/virtio/virtio-gpu.h |  1 -
 2 files changed, 5 insertions(+), 22 deletions(-)
hw/display/virtio-gpu.c +5 -21
index bcde65b50349..6e021b78b890 100644
@@ -434,12 +434,6 @@ static void virtio_gpu_release_scanout_dmabuf(VirtIOGPU *g, int scanout_id)
 void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id)
 {
     struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
-    struct virtio_gpu_simple_resource *res;
-
-    res = virtio_gpu_find_resource(g, scanout->resource_id);
-    if (res) {
-        res->scanout_bitmask &= ~(1 << scanout_id);
-    }
 
     virtio_gpu_release_scanout_dmabuf(g, scanout_id);
 
@@ -456,11 +450,9 @@ static void virtio_gpu_resource_destroy(VirtIOGPU *g,
 {
     int i;
 
-    if (res->scanout_bitmask) {
-        for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
-            if (res->scanout_bitmask & (1 << i)) {
-                virtio_gpu_disable_scanout(g, i);
-            }
+    for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
+        if (g->parent_obj.scanout[i].resource_id == res->resource_id) {
+            virtio_gpu_disable_scanout(g, i);
         }
     }
 
@@ -639,10 +631,10 @@ static void virtio_gpu_resource_flush(VirtIOGPU *g,
     for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
         QemuRect rect;
 
-        if (!(res->scanout_bitmask & (1 << i))) {
+        scanout = &g->parent_obj.scanout[i];
+        if (scanout->resource_id != res->resource_id) {
             continue;
         }
-        scanout = &g->parent_obj.scanout[i];
 
         qemu_rect_init(&rect, scanout->x, scanout->y,
                        scanout->width, scanout->height);
@@ -667,16 +659,9 @@ void virtio_gpu_update_scanout(VirtIOGPU *g,
                                struct virtio_gpu_framebuffer *fb,
                                struct virtio_gpu_rect *r)
 {
-    struct virtio_gpu_simple_resource *ores;
     struct virtio_gpu_scanout *scanout;
 
     scanout = &g->parent_obj.scanout[scanout_id];
-    ores = virtio_gpu_find_resource(g, scanout->resource_id);
-    if (ores) {
-        ores->scanout_bitmask &= ~(1 << scanout_id);
-    }
-
-    res->scanout_bitmask |= (1 << scanout_id);
     scanout->resource_id = res->resource_id;
     scanout->x = r->x;
     scanout->y = r->y;
@@ -1707,7 +1692,6 @@ static int virtio_gpu_post_load(void *opaque, int version_id)
         if (scanout->cursor.resource_id) {
             update_cursor(g, &scanout->cursor);
         }
-        res->scanout_bitmask |= (1 << i);
     }
 
     return 0;
include/hw/virtio/virtio-gpu.h +0 -1
index c2018b41056f..071cbbb6113e 100644
@@ -51,7 +51,6 @@ struct virtio_gpu_simple_resource {
     uint64_t *addrs;
     struct iovec *iov;
     unsigned int iov_cnt;
-    uint32_t scanout_bitmask;
     pixman_image_t *image;
     qemu_pixman_shareable share_handle;
     uint64_t hostmem;
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#24 lore raw

PATCH v4 18/24 virtio-gpu: release dmabuf when switching to a surface

In reply to Marc-André Lureau

A scanout may hold an active QemuDmaBuf when a SET_SCANOUT switches to CPU-backed display data. Force surface recreation and release the old buffer in this case.

Fixes 32db3c63ae11 ("virtio-gpu: Add virtio_gpu_set_scanout_blob")
---
 hw/display/virtio-gpu.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
hw/display/virtio-gpu.c +9 -4
index 6e021b78b890..b2cba26405c5 100644
@@ -704,9 +704,11 @@ static bool virtio_gpu_do_set_scanout(VirtIOGPU *g,
 {
     struct virtio_gpu_scanout *scanout;
     uint32_t bytes_pp = virtio_gpu_format_bytes_pp(fb->format);
+    bool was_dmabuf;
     uint8_t *data;
 
     scanout = &g->parent_obj.scanout[scanout_id];
+    was_dmabuf = scanout->dmabuf != NULL;
 
     if (!virtio_gpu_check_scanout_bounds(scanout_id, res->resource_id,
                                          fb->width, fb->height, r, error)) {
@@ -733,12 +735,11 @@ static bool virtio_gpu_do_set_scanout(VirtIOGPU *g,
 
     if (res->blob) {
         if (qemu_console_has_gl(scanout->con)) {
-            if (!virtio_gpu_update_dmabuf(g, scanout_id, res, fb, r)) {
-                virtio_gpu_update_scanout(g, scanout_id, res, fb, r);
-            } else {
+            if (virtio_gpu_update_dmabuf(g, scanout_id, res, fb, r)) {
                 *error = VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY;
                 return false;
             }
+            virtio_gpu_update_scanout(g, scanout_id, res, fb, r);
             return true;
         }
 
@@ -748,7 +749,8 @@ static bool virtio_gpu_do_set_scanout(VirtIOGPU *g,
     }
 
     /* create a surface for this scanout */
-    if ((res->blob && !qemu_console_has_gl(scanout->con)) ||
+    if (was_dmabuf ||
+        (res->blob && !qemu_console_has_gl(scanout->con)) ||
         !scanout->ds ||
         surface_data(scanout->ds) != data + fb->offset ||
         scanout->width != r->width ||
@@ -773,6 +775,9 @@ static bool virtio_gpu_do_set_scanout(VirtIOGPU *g,
         qemu_displaysurface_set_share_handle(scanout->ds, res->share_handle, fb->offset);
 
         pixman_image_unref(rect);
+        if (was_dmabuf) {
+            virtio_gpu_release_scanout_dmabuf(g, scanout_id);
+        }
         qemu_console_set_surface(g->parent_obj.scanout[scanout_id].con,
                                 scanout->ds);
     }
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#25 lore raw

PATCH v4 19/24 virtio-gpu/virgl: release dmabuf when a texture replaces a scanout

In reply to Marc-André Lureau

Release the scanout dmabuf before installing the texture. A full virtio_gpu_disable_scanout() is not used because the scanout is reassigned immediately afterwards.

Fixes 7c092f17ccee ("virtio-gpu: Handle resource blob commands")
---
 hw/display/virtio-gpu-virgl.c  | 3 +++
 hw/display/virtio-gpu.c        | 2 +-
 include/hw/virtio/virtio-gpu.h | 2 ++
 3 files changed, 6 insertions(+), 1 deletion(-)
hw/display/virtio-gpu-virgl.c +3 -0
index 0e43eacca406..07349c5c5502 100644
@@ -605,6 +605,9 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
                                              &cmd->error)) {
             return;
         }
+
+        virtio_gpu_release_scanout_dmabuf(g, ss.scanout_id);
+
         qemu_console_resize(g->parent_obj.scanout[ss.scanout_id].con,
                             ss.r.width, ss.r.height);
         virgl_renderer_force_ctx_0();
hw/display/virtio-gpu.c +1 -1
index b2cba26405c5..f9083e9577c3 100644
@@ -418,7 +418,7 @@ static void virtio_gpu_resource_create_blob(VirtIOGPU *g,
     QTAILQ_INSERT_HEAD(&g->reslist, res, next);
 }
 
-static void virtio_gpu_release_scanout_dmabuf(VirtIOGPU *g, int scanout_id)
+void virtio_gpu_release_scanout_dmabuf(VirtIOGPU *g, int scanout_id)
 {
     struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
     g_autoptr(QemuDmaBuf) dmabuf = scanout->dmabuf;
include/hw/virtio/virtio-gpu.h +2 -0
index 071cbbb6113e..2f18d839ccd3 100644
@@ -371,6 +371,8 @@ bool virtio_gpu_check_scanout_bounds(uint32_t scanout_id, uint32_t resource_id,
                                      const struct virtio_gpu_rect *r,
                                      uint32_t *error);
 
+void virtio_gpu_release_scanout_dmabuf(VirtIOGPU *g, int scanout_id);
+
 /**
  * virtio_gpu_scanout_blob_to_fb() - fill out fb based on scanout data
  * fb: the frame-buffer descriptor to fill out
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#26 lore raw

PATCH v4 20/24 virtio-gpu: consolidate disabling scanout

In reply to Marc-André Lureau

Use the common virtio_gpu_disable_scanout() to reset the scanout state, including GL state if any.

Fixes 7c092f17ccee ("virtio-gpu: Handle resource blob commands")
---
 hw/display/virtio-gpu-rutabaga.c | 4 +---
 hw/display/virtio-gpu-virgl.c    | 6 ++----
 hw/display/virtio-gpu.c          | 6 ++++--
 3 files changed, 7 insertions(+), 9 deletions(-)
hw/display/virtio-gpu-rutabaga.c +1 -3
index 8a82e96b7461..8697dd6936ba 100644
@@ -312,9 +312,7 @@ rutabaga_cmd_set_scanout(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
     scanout = &vb->scanout[ss.scanout_id];
 
     if (ss.resource_id == 0) {
-        scanout->resource_id = 0;
-        qemu_console_set_surface(scanout->con, NULL);
-        qemu_console_gl_scanout_disable(scanout->con);
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
         return;
     }
 
hw/display/virtio-gpu-virgl.c +2 -4
index 07349c5c5502..9ca7b946ac65 100644
@@ -618,8 +618,7 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
             ss.r.x, ss.r.y, ss.r.width, ss.r.height,
             d3d_tex2d);
     } else {
-        qemu_console_set_surface(g->parent_obj.scanout[ss.scanout_id].con, NULL);
-        qemu_console_gl_scanout_disable(g->parent_obj.scanout[ss.scanout_id].con);
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
     }
     g->parent_obj.scanout[ss.scanout_id].resource_id = ss.resource_id;
 }
@@ -1430,8 +1429,7 @@ void virtio_gpu_virgl_reset_scanout(VirtIOGPU *g)
     int i;
 
     for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
-        qemu_console_set_surface(g->parent_obj.scanout[i].con, NULL);
-        qemu_console_gl_scanout_disable(g->parent_obj.scanout[i].con);
+        virtio_gpu_disable_scanout(g, i);
     }
 }
 
hw/display/virtio-gpu.c +4 -2
index f9083e9577c3..dc5b2da9a43f 100644
@@ -435,8 +435,10 @@ void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id)
 {
     struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
 
-    virtio_gpu_release_scanout_dmabuf(g, scanout_id);
-
+    if (scanout->dmabuf) {
+        virtio_gpu_release_scanout_dmabuf(g, scanout_id);
+    }
+    qemu_console_gl_scanout_disable(scanout->con);
     qemu_console_set_surface(scanout->con, NULL);
     scanout->resource_id = 0;
     scanout->ds = NULL;
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#27 lore raw

Re: [PATCH v4 20/24] virtio-gpu: consolidate disabling scanout

Quoted 55 lines · Marc-André Lureau

Use the common virtio_gpu_disable_scanout() to reset the scanout state, including GL state if any.

Fixes 7c092f17ccee ("virtio-gpu: Handle resource blob commands")
---
  hw/display/virtio-gpu-rutabaga.c | 4 +---
  hw/display/virtio-gpu-virgl.c    | 6 ++----
  hw/display/virtio-gpu.c          | 6 ++++--
  3 files changed, 7 insertions(+), 9 deletions(-)
hw/display/virtio-gpu-rutabaga.c +1 -3
index 8a82e96b7461..8697dd6936ba 100644
@@ -312,9 +312,7 @@ rutabaga_cmd_set_scanout(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
      scanout = &vb->scanout[ss.scanout_id];
 
      if (ss.resource_id == 0) {
-        scanout->resource_id = 0;
-        qemu_console_set_surface(scanout->con, NULL);
-        qemu_console_gl_scanout_disable(scanout->con);
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
          return;
      }
 
hw/display/virtio-gpu-virgl.c +2 -4
index 07349c5c5502..9ca7b946ac65 100644
@@ -618,8 +618,7 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
              ss.r.x, ss.r.y, ss.r.width, ss.r.height,
              d3d_tex2d);
      } else {
-        qemu_console_set_surface(g->parent_obj.scanout[ss.scanout_id].con, NULL);
-        qemu_console_gl_scanout_disable(g->parent_obj.scanout[ss.scanout_id].con);
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
      }
      g->parent_obj.scanout[ss.scanout_id].resource_id = ss.resource_id;
  }
@@ -1430,8 +1429,7 @@ void virtio_gpu_virgl_reset_scanout(VirtIOGPU *g)
      int i;
 
      for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
-        qemu_console_set_surface(g->parent_obj.scanout[i].con, NULL);
-        qemu_console_gl_scanout_disable(g->parent_obj.scanout[i].con);
+        virtio_gpu_disable_scanout(g, i);
      }
  }
 
hw/display/virtio-gpu.c +0 -0
index f9083e9577c3..dc5b2da9a43f 100644

hw/display/virtio-gpu.c void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id) · line 440

Quoted 8 lines
hw/display/virtio-gpu.c +3 -2
@@ -435,8 +435,10 @@ void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id)
  {
      struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
 
-    virtio_gpu_release_scanout_dmabuf(g, scanout_id);
-
+    if (scanout->dmabuf) {
+        virtio_gpu_release_scanout_dmabuf(g, scanout_id);
+    }

I'm not sure why adding an if branch here. It looks simpler without one.

Regards,
Akihiko Odaki

Quoted 5 lines
hw/display/virtio-gpu.c +1 -0
+    qemu_console_gl_scanout_disable(scanout->con);
      qemu_console_set_surface(scanout->con, NULL);
      scanout->resource_id = 0;
      scanout->ds = NULL;
#28 lore raw

Re: [PATCH v4 20/24] virtio-gpu: consolidate disabling scanout

Hi

Quoted 60 lines · Akihiko Odaki
Quoted 58 lines · Marc-André Lureau

Use the common virtio_gpu_disable_scanout() to reset the scanout state, including GL state if any.

Fixes 7c092f17ccee ("virtio-gpu: Handle resource blob commands")
---
  hw/display/virtio-gpu-rutabaga.c | 4 +---
  hw/display/virtio-gpu-virgl.c    | 6 ++----
  hw/display/virtio-gpu.c          | 6 ++++--
  3 files changed, 7 insertions(+), 9 deletions(-)
hw/display/virtio-gpu-rutabaga.c +1 -3
index 8a82e96b7461..8697dd6936ba 100644
@@ -312,9 +312,7 @@ rutabaga_cmd_set_scanout(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
      scanout = &vb->scanout[ss.scanout_id];
 
      if (ss.resource_id == 0) {
-        scanout->resource_id = 0;
-        qemu_console_set_surface(scanout->con, NULL);
-        qemu_console_gl_scanout_disable(scanout->con);
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
          return;
      }
 
hw/display/virtio-gpu-virgl.c +2 -4
index 07349c5c5502..9ca7b946ac65 100644
@@ -618,8 +618,7 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
              ss.r.x, ss.r.y, ss.r.width, ss.r.height,
              d3d_tex2d);
      } else {
-        qemu_console_set_surface(g->parent_obj.scanout[ss.scanout_id].con, NULL);
-        qemu_console_gl_scanout_disable(g->parent_obj.scanout[ss.scanout_id].con);
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
      }
      g->parent_obj.scanout[ss.scanout_id].resource_id = ss.resource_id;
  }
@@ -1430,8 +1429,7 @@ void virtio_gpu_virgl_reset_scanout(VirtIOGPU *g)
      int i;
 
      for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
-        qemu_console_set_surface(g->parent_obj.scanout[i].con, NULL);
-        qemu_console_gl_scanout_disable(g->parent_obj.scanout[i].con);
+        virtio_gpu_disable_scanout(g, i);
      }
  }
 
hw/display/virtio-gpu.c +0 -0
index f9083e9577c3..dc5b2da9a43f 100644
@@ -435,8 +435,10 @@ void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id)
  {
      struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
 
Quoted 7 lines

hw/display/virtio-gpu.c void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id) · line 440

Quoted 5 lines
hw/display/virtio-gpu.c +3 -2
-    virtio_gpu_release_scanout_dmabuf(g, scanout_id);
-
+    if (scanout->dmabuf) {
+        virtio_gpu_release_scanout_dmabuf(g, scanout_id);
+    }

I'm not sure why adding an if branch here. It looks simpler without one.

yep, let's drop it.
ack with that change?

Quoted 9 lines

Regards,
Akihiko Odaki

Quoted 5 lines
hw/display/virtio-gpu.c +1 -0
+    qemu_console_gl_scanout_disable(scanout->con);
      qemu_console_set_surface(scanout->con, NULL);
      scanout->resource_id = 0;
      scanout->ds = NULL;
Reviewed-by
#29 lore raw

Re: [PATCH v4 20/24] virtio-gpu: consolidate disabling scanout

Quoted 72 lines · Marc-André Lureau

Hi

Quoted 60 lines · Akihiko Odaki
Quoted 58 lines · Marc-André Lureau

Use the common virtio_gpu_disable_scanout() to reset the scanout state, including GL state if any.

Fixes 7c092f17ccee ("virtio-gpu: Handle resource blob commands")
---
   hw/display/virtio-gpu-rutabaga.c | 4 +---
   hw/display/virtio-gpu-virgl.c    | 6 ++----
   hw/display/virtio-gpu.c          | 6 ++++--
   3 files changed, 7 insertions(+), 9 deletions(-)
hw/display/virtio-gpu-rutabaga.c +1 -3
index 8a82e96b7461..8697dd6936ba 100644
@@ -312,9 +312,7 @@ rutabaga_cmd_set_scanout(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
       scanout = &vb->scanout[ss.scanout_id];
 
       if (ss.resource_id == 0) {
-        scanout->resource_id = 0;
-        qemu_console_set_surface(scanout->con, NULL);
-        qemu_console_gl_scanout_disable(scanout->con);
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
           return;
       }
 
hw/display/virtio-gpu-virgl.c +2 -4
index 07349c5c5502..9ca7b946ac65 100644
@@ -618,8 +618,7 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
               ss.r.x, ss.r.y, ss.r.width, ss.r.height,
               d3d_tex2d);
       } else {
-        qemu_console_set_surface(g->parent_obj.scanout[ss.scanout_id].con, NULL);
-        qemu_console_gl_scanout_disable(g->parent_obj.scanout[ss.scanout_id].con);
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
       }
       g->parent_obj.scanout[ss.scanout_id].resource_id = ss.resource_id;
   }
@@ -1430,8 +1429,7 @@ void virtio_gpu_virgl_reset_scanout(VirtIOGPU *g)
       int i;
 
       for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
-        qemu_console_set_surface(g->parent_obj.scanout[i].con, NULL);
-        qemu_console_gl_scanout_disable(g->parent_obj.scanout[i].con);
+        virtio_gpu_disable_scanout(g, i);
       }
   }
 
hw/display/virtio-gpu.c +0 -0
index f9083e9577c3..dc5b2da9a43f 100644
@@ -435,8 +435,10 @@ void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id)
   {
       struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];
 
Quoted 7 lines

hw/display/virtio-gpu.c void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id) · line 440

Quoted 5 lines
hw/display/virtio-gpu.c +3 -2
-    virtio_gpu_release_scanout_dmabuf(g, scanout_id);
-
+    if (scanout->dmabuf) {
+        virtio_gpu_release_scanout_dmabuf(g, scanout_id);
+    }

I'm not sure why adding an if branch here. It looks simpler without one.

Quoted 2 lines

yep, let's drop it.
ack with that change?

Yes, with it dropped you can have:

Regards,
Akihiko Odaki

Quoted 11 lines
Quoted 10 lines

Regards,
Akihiko Odaki

Quoted 5 lines
hw/display/virtio-gpu.c +1 -0
+    qemu_console_gl_scanout_disable(scanout->con);
       qemu_console_set_surface(scanout->con, NULL);
       scanout->resource_id = 0;
       scanout->ds = NULL;
#30 lore raw

PATCH v4 21/24 virtio-gpu: minor refactoring

In reply to Marc-André Lureau

Move disable_scanout() branch earlier, return early. (scanout->resource_id is cleared there)

---
 hw/display/virtio-gpu-virgl.c | 71 ++++++++++++++++++++++---------------------
 1 file changed, 36 insertions(+), 35 deletions(-)
hw/display/virtio-gpu-virgl.c +36 -35
index 9ca7b946ac65..439a6e1c1cff 100644
@@ -565,6 +565,8 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
                                   struct virtio_gpu_ctrl_command *cmd)
 {
     struct virtio_gpu_set_scanout ss;
+    struct virgl_renderer_resource_info info;
+    void *d3d_tex2d = NULL;
     int ret;
 
     VIRTIO_GPU_FILL_CMD(ss);
@@ -579,47 +581,46 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
     }
     g->parent_obj.enable = 1;
 
-    if (ss.resource_id) {
-        struct virgl_renderer_resource_info info;
-        void *d3d_tex2d = NULL;
+    if (!ss.resource_id) {
+        virtio_gpu_disable_scanout(g, ss.scanout_id);
+        return;
+    }
 
 #if VIRGL_VERSION_MAJOR >= 1
-        struct virgl_renderer_resource_info_ext ext;
-        memset(&ext, 0, sizeof(ext));
-        ret = virgl_renderer_resource_get_info_ext(ss.resource_id, &ext);
-        info = ext.base;
-        d3d_tex2d = ext.d3d_tex2d;
+    struct virgl_renderer_resource_info_ext ext;
+    memset(&ext, 0, sizeof(ext));
+    ret = virgl_renderer_resource_get_info_ext(ss.resource_id, &ext);
+    info = ext.base;
+    d3d_tex2d = ext.d3d_tex2d;
 #else
-        memset(&info, 0, sizeof(info));
-        ret = virgl_renderer_resource_get_info(ss.resource_id, &info);
+    memset(&info, 0, sizeof(info));
+    ret = virgl_renderer_resource_get_info(ss.resource_id, &info);
 #endif
-        if (ret) {
-            qemu_log_mask(LOG_GUEST_ERROR,
-                          "%s: illegal resource specified %d\n",
-                          __func__, ss.resource_id);
-            cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
-            return;
-        }
-        if (!virtio_gpu_check_scanout_bounds(ss.scanout_id, ss.resource_id,
-                                             info.width, info.height, &ss.r,
-                                             &cmd->error)) {
-            return;
-        }
 
-        virtio_gpu_release_scanout_dmabuf(g, ss.scanout_id);
-
-        qemu_console_resize(g->parent_obj.scanout[ss.scanout_id].con,
-                            ss.r.width, ss.r.height);
-        virgl_renderer_force_ctx_0();
-        qemu_console_gl_scanout_texture(
-            g->parent_obj.scanout[ss.scanout_id].con, info.tex_id,
-            info.flags & VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP,
-            info.width, info.height,
-            ss.r.x, ss.r.y, ss.r.width, ss.r.height,
-            d3d_tex2d);
-    } else {
-        virtio_gpu_disable_scanout(g, ss.scanout_id);
+    if (ret) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: illegal resource specified %d\n",
+                      __func__, ss.resource_id);
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
+        return;
     }
+    if (!virtio_gpu_check_scanout_bounds(ss.scanout_id, ss.resource_id,
+                                         info.width, info.height, &ss.r,
+                                         &cmd->error)) {
+        return;
+    }
+
+    virtio_gpu_release_scanout_dmabuf(g, ss.scanout_id);
+
+    qemu_console_resize(g->parent_obj.scanout[ss.scanout_id].con,
+                        ss.r.width, ss.r.height);
+    virgl_renderer_force_ctx_0();
+    qemu_console_gl_scanout_texture(
+        g->parent_obj.scanout[ss.scanout_id].con, info.tex_id,
+        info.flags & VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP,
+        info.width, info.height,
+        ss.r.x, ss.r.y, ss.r.width, ss.r.height,
+        d3d_tex2d);
     g->parent_obj.scanout[ss.scanout_id].resource_id = ss.resource_id;
 }
 
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#31 lore raw

PATCH v4 22/24 virtio-gpu/virgl: disable scanouts on resource unref

In reply to Marc-André Lureau

Disable scanouts referencing a resource being unreferenced.

The scanout disable callbacks may change the current GL context, so restore the renderer context before detaching and unreferencing the resource.

Fixes 9d9e152136bd ("virtio-gpu: add 3d mode and virgl rendering support.")
---
 hw/display/virtio-gpu-virgl.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/display/virtio-gpu-virgl.c +8 -0
index 439a6e1c1cff..494be3fb4f8d 100644
@@ -431,6 +431,7 @@ virtio_gpu_virgl_resource_unref(VirtIOGPU *g,
 {
     struct iovec *res_iovs = NULL;
     int num_iovs = 0;
+    int i;
 #if VIRGL_VERSION_MAJOR >= 1
     int ret;
 
@@ -443,6 +444,13 @@ virtio_gpu_virgl_resource_unref(VirtIOGPU *g,
     }
 #endif
 
+    for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
+        if (g->parent_obj.scanout[i].resource_id == res->base.resource_id) {
+            virtio_gpu_disable_scanout(g, i);
+        }
+    }
+
+    virgl_renderer_force_ctx_0();
     virgl_renderer_resource_detach_iov(res->base.resource_id,
                                        &res_iovs,
                                        &num_iovs);
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#32 lore raw

PATCH v4 23/24 virtio-gpu: extract virtio_gpu_disable_scanout_for_resource helper

In reply to Marc-André Lureau

The same loop iterating scanouts and disabling those matching a given resource_id was duplicated across four call sites in three backends. Factor it into a shared helper.

---
 hw/display/virtio-gpu-rutabaga.c |  7 +------
 hw/display/virtio-gpu-virgl.c    |  7 +------
 hw/display/virtio-gpu.c          | 26 ++++++++++++--------------
 include/hw/virtio/virtio-gpu.h   |  2 ++
 4 files changed, 16 insertions(+), 26 deletions(-)
hw/display/virtio-gpu-rutabaga.c +1 -6
index 8697dd6936ba..3aafa4dc66cd 100644
@@ -146,14 +146,9 @@ virtio_gpu_rutabaga_resource_unref(VirtIOGPU *g,
                                    Error **errp)
 {
     int32_t result;
-    int i;
     VirtIOGPURutabaga *vr = VIRTIO_GPU_RUTABAGA(g);
 
-    for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
-        if (g->parent_obj.scanout[i].resource_id == res->resource_id) {
-            virtio_gpu_disable_scanout(g, i);
-        }
-    }
+    virtio_gpu_disable_scanout_for_resource(g, res->resource_id);
 
     result = rutabaga_resource_unref(vr->rutabaga, res->resource_id);
     if (result) {
hw/display/virtio-gpu-virgl.c +1 -6
index 494be3fb4f8d..1c9380e2a6d2 100644
@@ -431,7 +431,6 @@ virtio_gpu_virgl_resource_unref(VirtIOGPU *g,
 {
     struct iovec *res_iovs = NULL;
     int num_iovs = 0;
-    int i;
 #if VIRGL_VERSION_MAJOR >= 1
     int ret;
 
@@ -444,11 +443,7 @@ virtio_gpu_virgl_resource_unref(VirtIOGPU *g,
     }
 #endif
 
-    for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
-        if (g->parent_obj.scanout[i].resource_id == res->base.resource_id) {
-            virtio_gpu_disable_scanout(g, i);
-        }
-    }
+    virtio_gpu_disable_scanout_for_resource(g, res->base.resource_id);
 
     virgl_renderer_force_ctx_0();
     virgl_renderer_resource_detach_iov(res->base.resource_id,
hw/display/virtio-gpu.c +12 -14
index dc5b2da9a43f..7a6eca944310 100644
@@ -446,17 +446,21 @@ void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id)
     scanout->height = 0;
 }
 
+void virtio_gpu_disable_scanout_for_resource(VirtIOGPU *g,
+                                             uint32_t resource_id)
+{
+    for (int i = 0; i < g->parent_obj.conf.max_outputs; i++) {
+        if (g->parent_obj.scanout[i].resource_id == resource_id) {
+            virtio_gpu_disable_scanout(g, i);
+        }
+    }
+}
+
 static void virtio_gpu_resource_destroy(VirtIOGPU *g,
                                         struct virtio_gpu_simple_resource *res,
                                         Error **errp)
 {
-    int i;
-
-    for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
-        if (g->parent_obj.scanout[i].resource_id == res->resource_id) {
-            virtio_gpu_disable_scanout(g, i);
-        }
-    }
+    virtio_gpu_disable_scanout_for_resource(g, res->resource_id);
 
     qemu_pixman_image_unref(res->image);
     virtio_gpu_cleanup_mapping(g, res);
@@ -1064,13 +1068,7 @@ void virtio_gpu_cleanup_mapping(VirtIOGPU *g,
                                 struct virtio_gpu_simple_resource *res)
 {
     if (res->blob) {
-        int i, max_outputs = g->parent_obj.conf.max_outputs;
-
-        for (i = 0; i < max_outputs; i++) {
-            if (g->parent_obj.scanout[i].resource_id == res->resource_id) {
-                virtio_gpu_disable_scanout(g, i);
-            }
-        }
+        virtio_gpu_disable_scanout_for_resource(g, res->resource_id);
     }
 
     virtio_gpu_cleanup_mapping_iov(g, res->iov, res->iov_cnt);
include/hw/virtio/virtio-gpu.h +2 -0
index 2f18d839ccd3..631dfc63a4d4 100644
@@ -404,6 +404,8 @@ void virtio_gpu_update_scanout(VirtIOGPU *g,
                                struct virtio_gpu_framebuffer *fb,
                                struct virtio_gpu_rect *r);
 void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id);
+void virtio_gpu_disable_scanout_for_resource(VirtIOGPU *g,
+                                             uint32_t resource_id);
 
 /* virtio-gpu-3d.c */
 void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
#33 lore raw

PATCH v4 24/24 contrib/vhost-user-gpu: disable scanouts on resource unref

In reply to Marc-André Lureau

virgl_cmd_resource_unref() destroys the virgl renderer resource without first disabling any scanouts that reference it. The QEMU frontend then holds a stale DMA-BUF until another DMABUF_SCANOUT message arrives.

Send a DMABUF_SCANOUT disable message (fd=-1) for each affected scanout before destroying the resource.

Fixes d52c454aadcd ("contrib: add vhost-user-gpu")
---
 contrib/vhost-user-gpu/virgl.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
contrib/vhost-user-gpu/virgl.c +12 -0
index 20bae57d0fe4..5a5f9f14c80c 100644
@@ -113,6 +113,18 @@ virgl_cmd_resource_unref(VuGpu *g,
 
     VUGPU_FILL_CMD(unref);
 
+    for (int i = 0; i < VIRTIO_GPU_MAX_SCANOUTS; i++) {
+        if (g->scanout[i].resource_id == unref.resource_id) {
+            VhostUserGpuMsg msg = {
+                .request = VHOST_USER_GPU_DMABUF_SCANOUT,
+                .size = sizeof(VhostUserGpuDMABUFScanout),
+                .payload.dmabuf_scanout.scanout_id = i,
+            };
+            vg_send_msg(g, &msg, -1);
+            g->scanout[i].resource_id = 0;
+        }
+    }
+
     virgl_renderer_resource_detach_iov(unref.resource_id,
                                        &res_iovs,
                                        &num_iovs);
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8