virtio-gpu: Add a default synchronization mechanism for blobs (v6)
v6 cover virtio-gpu: Add a default synchronization mechanism for blobs (v6)
When the Guest and Host are using Blob resources, there is a chance that they may use the underlying storage associated with a Blob at the same time leading to glitches such as flickering or tearing. To prevent these from happening, the Host needs to ensure that it waits until its Blit is completed by the Host GPU before letting the Guest reuse the Blob.
This should be the default behavior regardless of the type of Guest that is using Blob resources but would be particularly useful for Guests that are using frontbuffer rendering such as some X compositors, Windows compositors, etc.
The way it works is the Guest submits the resource_flush command and waits -- for example over a dma fence -- until virtio-gpu sends an ack. And, the UI will queue a new repaint request and waits until the sync object associated with the Blit is signaled. Once this is done, the UI will trigger virtio-gpu to send an ack for the resource_flush cmd.
v2:
- Added more description in the cover letter
- Removed the wait from resource_flush and included it in a gl_flushed() callback
v3:
- Instead of explicitly waiting on the sync object and stalling the thread, add the relevant fence fd to Qemu's main loop and wait for it to be signalled. (suggested by Gerd Hoffmann)
v4:
- Replace the field 'blob' with 'allow_fences' in QemuDmabuf struct. (Gerd)
v5: rebase
v6: Fixed the compilation error on platforms that do not have GBM.
Vivek Kasireddy (5):
ui/gtk: Create a common release_dmabuf helper ui/egl: Add egl helpers to help with synchronization
ui: Create sync objects and fences only for blobs ui/gtk-egl: Wait for the draw signal for dmabuf blobs
virtio-gpu: Add gl_flushed callback
hw/display/virtio-gpu-udmabuf.c | 1 + hw/display/virtio-gpu.c | 32 ++++++++++++++++++++-- include/ui/console.h | 3 +++ include/ui/egl-helpers.h | 3 +++ include/ui/gtk.h | 5 ++-- ui/egl-helpers.c | 26 ++++++++++++++++++ ui/gtk-egl.c | 48 +++++++++++++++++++++++++++------ ui/gtk-gl-area.c | 26 ++++++++++++++++++ ui/gtk.c | 26 ++++++++++++++++-- 9 files changed, 156 insertions(+), 14 deletions(-)
Signature · 1 line
2.30.2
PATCH v6 1/5 ui/gtk: Create a common release_dmabuf helper
Since the texture release mechanism is same for both gtk-egl and gtk-glarea, move the helper from gtk-egl to common gtk code so that it can be shared by both gtk backends.
--- include/ui/gtk.h | 2 -- ui/gtk-egl.c | 8 -------- ui/gtk.c | 11 ++++++++++- 3 files changed, 10 insertions(+), 11 deletions(-)
include/ui/gtk.h +0 -2
@@ -181,8 +181,6 @@ 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_scanout_flush (DisplayChangeListener *dcl,uint32_t x,uint32_t y,uint32_t w,uint32_t h);void gtk_egl_init (DisplayGLMode mode);
ui/gtk-egl.c +0 -8
@@ -249,14 +249,6 @@ void gd_egl_cursor_position(DisplayChangeListener *dcl, vc->gfx.cursor_y = pos_y * vc->gfx.scale_y; } -void gd_egl_release_dmabuf (DisplayChangeListener *dcl, -QemuDmaBuf *dmabuf) -{ -#ifdef CONFIG_GBM -egl_dmabuf_release_texture (dmabuf); -#endif -} -void gd_egl_scanout_flush (DisplayChangeListener *dcl,uint32_t x,uint32_t y,uint32_t w,uint32_t h) {
ui/gtk.c +10 -1
@@ -575,6 +575,14 @@ 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 +egl_dmabuf_release_texture (dmabuf); +#endif +} +/** DisplayState Callbacks (opengl version) **/ static const DisplayChangeListenerOps dcl_gl_area_ops = {
@@ -593,6 +601,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_has_dmabuf = gd_has_dmabuf, };
@@ -615,8 +624,8 @@ static const DisplayChangeListenerOps dcl_egl_ops = { .dpy_gl_scanout_dmabuf = gd_egl_scanout_dmabuf, .dpy_gl_cursor_dmabuf = gd_egl_cursor_dmabuf, .dpy_gl_cursor_position = gd_egl_cursor_position, - .dpy_gl_release_dmabuf = gd_egl_release_dmabuf, .dpy_gl_update = gd_egl_scanout_flush, + .dpy_gl_release_dmabuf = gd_gl_release_dmabuf, .dpy_has_dmabuf = gd_has_dmabuf, };
Signature · 1 line
2.30.2
PATCH v6 2/5 ui/egl: Add egl helpers to help with synchronization
In reply to Vivek KasireddyThese egl helpers would be used for creating and waiting on a sync object.
--- include/ui/console.h | 2 ++ include/ui/egl-helpers.h | 2 ++ ui/egl-helpers.c | 26 ++++++++++++++++++++++++++ 3 files changed, 30 insertions(+)
include/ui/console.h +2 -0
@@ -168,6 +168,8 @@ typedef struct QemuDmaBuf {uint64_t modifier;uint32_t texture;bool y0_top; +void *sync; +int fence_fd; } QemuDmaBuf;typedef struct DisplayState DisplayState ;
include/ui/egl-helpers.h +2 -0
@@ -45,6 +45,8 @@ int egl_get_fd_for_texture(uint32_t tex_id, EGLint *stride, EGLint *fourcc,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);#endif
ui/egl-helpers.c +26 -0
@@ -287,6 +287,32 @@ void egl_dmabuf_release_texture(QemuDmaBuf *dmabuf) dmabuf->texture = 0; } +void egl_dmabuf_create_sync (QemuDmaBuf *dmabuf) +{ +EGLSyncKHR sync; + +if (epoxy_has_egl_extension (qemu_egl_display, +"EGL_KHR_fence_sync" ) && +epoxy_has_egl_extension (qemu_egl_display, +"EGL_ANDROID_native_fence_sync" )) { + sync =eglCreateSyncKHR (qemu_egl_display, +EGL_SYNC_NATIVE_FENCE_ANDROID ,NULL ); +if (sync !=EGL_NO_SYNC_KHR ) { + dmabuf->sync = sync; + } + } +} + +void egl_dmabuf_create_fence (QemuDmaBuf *dmabuf) +{ +if (dmabuf->sync) { + dmabuf->fence_fd =eglDupNativeFenceFDANDROID (qemu_egl_display, + dmabuf->sync); +eglDestroySyncKHR (qemu_egl_display, dmabuf->sync); + dmabuf->sync =NULL ; + } +} +#endif /* CONFIG_GBM */ /* ---------------------------------------------------------------------- */
Signature · 1 line
2.30.2
PATCH v6 3/5 ui: Create sync objects and fences only for blobs
In reply to Vivek KasireddyCreate sync objects and fences only for dmabufs that are blobs. Once a fence is created (after glFlush) and is signalled, graphic_hw_gl_flushed() will be called and virtio-gpu cmd processing will be resumed.
--- hw/display/virtio-gpu-udmabuf.c | 1 + include/ui/console.h | 1 + include/ui/egl-helpers.h | 1 + include/ui/gtk.h | 1 + ui/gtk-egl.c | 25 +++++++++++++++++++++++++ ui/gtk-gl-area.c | 26 ++++++++++++++++++++++++++ ui/gtk.c | 13 +++++++++++++ 7 files changed, 68 insertions(+)
hw/display/virtio-gpu-udmabuf.c +1 -0
@@ -185,6 +185,7 @@ static VGPUDMABuf dmabuf->buf.stride = fb->stride; dmabuf->buf.fourcc =qemu_pixman_to_drm_format (fb->format); dmabuf->buf.fd = res->dmabuf_fd; + dmabuf->buf.allow_fences = true; dmabuf->scanout_id = scanout_id;QTAILQ_INSERT_HEAD (&g->dmabuf.bufs, dmabuf, next);
include/ui/console.h +1 -0
@@ -170,6 +170,7 @@ typedef struct QemuDmaBuf {bool y0_top;void *sync;int fence_fd; +bool allow_fences; } QemuDmaBuf;typedef struct DisplayState DisplayState ;
include/ui/egl-helpers.h +1 -0
@@ -19,6 +19,7 @@ typedef struct egl_fb {GLuint texture;GLuint framebuffer;bool delete_texture; +QemuDmaBuf *dmabuf; } egl_fb;void egl_fb_destroy (egl_fb *fb);
include/ui/gtk.h +1 -0
@@ -155,6 +155,7 @@ extern bool gtk_use_gl_area;/* ui/gtk.c */ void gd_update_windowsize (VirtualConsole *vc);int gd_monitor_update_interval (GtkWidget *widget); +void gd_hw_gl_flushed (void *vc);/* ui/gtk-egl.c */ void gd_egl_init (VirtualConsole *vc);
ui/gtk-egl.c +25 -0
@@ -12,6 +12,7 @@*/ #include "qemu/osdep.h" +#include "qemu/main-loop.h" #include "trace.h"
@@ -94,6 +95,18 @@ void gd_egl_draw(VirtualConsole *vc) }glFlush (); +#ifdef CONFIG_GBM +if (vc->gfx.guest_fb.dmabuf) { +QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf; + +egl_dmabuf_create_fence (dmabuf); +if (dmabuf->fence_fd >0 ) { +qemu_set_fd_handler (dmabuf->fence_fd, gd_hw_gl_flushed,NULL , vc); +return ; + } +graphic_hw_gl_block (vc->gfx.dcl.con, false); + } +#endif graphic_hw_gl_flushed (vc->gfx.dcl.con); }
@@ -209,6 +222,8 @@ void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl, QemuDmaBuf *dmabuf) {#ifdef CONFIG_GBM +VirtualConsole *vc =container_of (dcl, VirtualConsole, gfx.dcl); +egl_dmabuf_import_texture (dmabuf);if (!dmabuf->texture) {return ;
@@ -217,6 +232,10 @@ void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,gd_egl_scanout_texture (dcl, dmabuf->texture, false, dmabuf->width, dmabuf->height,0 ,0 , dmabuf->width, dmabuf->height); + +if (dmabuf->allow_fences) { + vc->gfx.guest_fb.dmabuf = dmabuf; + }#endif }
@@ -281,6 +300,12 @@ void gd_egl_scanout_flush(DisplayChangeListener *dcl,egl_fb_blit (&vc->gfx.win_fb, &vc->gfx.guest_fb, !vc->gfx.y0_top); } +#ifdef CONFIG_GBM +if (vc->gfx.guest_fb.dmabuf) { +egl_dmabuf_create_sync (vc->gfx.guest_fb.dmabuf); + } +#endif +eglSwapBuffers (qemu_egl_display, vc->gfx.esurface); }
ui/gtk-gl-area.c +26 -0
@@ -8,6 +8,7 @@*/ #include "qemu/osdep.h" +#include "qemu/main-loop.h" #include "trace.h"
@@ -71,7 +72,25 @@ void gd_gl_area_draw(VirtualConsole *vc)surface_gl_render_texture (vc->gfx.gls, vc->gfx.ds); } +#ifdef CONFIG_GBM +if (vc->gfx.guest_fb.dmabuf) { +egl_dmabuf_create_sync (vc->gfx.guest_fb.dmabuf); + } +#endif +glFlush (); +#ifdef CONFIG_GBM +if (vc->gfx.guest_fb.dmabuf) { +QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf; + +egl_dmabuf_create_fence (dmabuf); +if (dmabuf->fence_fd >0 ) { +qemu_set_fd_handler (dmabuf->fence_fd, gd_hw_gl_flushed,NULL , vc); +return ; + } +graphic_hw_gl_block (vc->gfx.dcl.con, false); + } +#endif graphic_hw_gl_flushed (vc->gfx.dcl.con); }
@@ -213,6 +232,9 @@ void gd_gl_area_scanout_flush(DisplayChangeListener *dcl, {VirtualConsole *vc =container_of (dcl, VirtualConsole, gfx.dcl); +if (vc->gfx.guest_fb.dmabuf) { +graphic_hw_gl_block (vc->gfx.dcl.con, true); + }gtk_gl_area_queue_render (GTK_GL_AREA (vc->gfx.drawing_area)); }
@@ -231,6 +253,10 @@ void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,gd_gl_area_scanout_texture (dcl, dmabuf->texture, false, dmabuf->width, dmabuf->height,0 ,0 , dmabuf->width, dmabuf->height); + +if (dmabuf->allow_fences) { + vc->gfx.guest_fb.dmabuf = dmabuf; + }#endif }
ui/gtk.c +13 -0
@@ -36,6 +36,7 @@#include "qapi/qapi-commands-machine.h" #include "qapi/qapi-commands-misc.h" #include "qemu/cutils.h" +#include "qemu/main-loop.h" #include "ui/console.h" #include "ui/gtk.h"
@@ -583,6 +584,18 @@ static void gd_gl_release_dmabuf(DisplayChangeListener *dcl,#endif } +void gd_hw_gl_flushed (void *vcon) +{ +VirtualConsole *vc = vcon; +QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf; + +graphic_hw_gl_block (vc->gfx.dcl.con, false); +graphic_hw_gl_flushed (vc->gfx.dcl.con); +qemu_set_fd_handler (dmabuf->fence_fd,NULL ,NULL ,NULL ); +close (dmabuf->fence_fd); + dmabuf->fence_fd =-1 ; +} +/** DisplayState Callbacks (opengl version) **/ static const DisplayChangeListenerOps dcl_gl_area_ops = {
Signature · 1 line
2.30.2
PATCH v6 4/5 ui/gtk-egl: Wait for the draw signal for dmabuf blobs
In reply to Vivek KasireddyInstead of immediately drawing and submitting, queue and wait for the draw signal if the dmabuf submitted is a blob.
--- include/ui/gtk.h | 2 ++ ui/gtk-egl.c | 15 +++++++++++++++ ui/gtk.c | 2 +- 3 files changed, 18 insertions(+), 1 deletion(-)
include/ui/gtk.h +2 -0
@@ -182,6 +182,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_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);
ui/gtk-egl.c +15 -0
@@ -309,6 +309,21 @@ void gd_egl_scanout_flush(DisplayChangeListener *dcl,eglSwapBuffers (qemu_egl_display, vc->gfx.esurface); } +void gd_egl_flush (DisplayChangeListener *dcl, +uint32_t x,uint32_t y,uint32_t w,uint32_t h) +{ +VirtualConsole *vc =container_of (dcl, VirtualConsole, gfx.dcl); +GtkWidget *area = vc->gfx.drawing_area; + +if (vc->gfx.guest_fb.dmabuf) { +graphic_hw_gl_block (vc->gfx.dcl.con, true); +gtk_widget_queue_draw_area (area, x, y, w, h); +return ; + } + +gd_egl_scanout_flush (&vc->gfx.dcl, x, y, w, h); +} +void gtk_egl_init (DisplayGLMode mode) {GdkDisplay *gdk_display =gdk_display_get_default ();
ui/gtk.c +1 -1
@@ -637,7 +637,7 @@ static const DisplayChangeListenerOps dcl_egl_ops = { .dpy_gl_scanout_dmabuf = gd_egl_scanout_dmabuf, .dpy_gl_cursor_dmabuf = gd_egl_cursor_dmabuf, .dpy_gl_cursor_position = gd_egl_cursor_position, - .dpy_gl_update = gd_egl_scanout_flush, + .dpy_gl_update = gd_egl_flush, .dpy_gl_release_dmabuf = gd_gl_release_dmabuf, .dpy_has_dmabuf = gd_has_dmabuf, };
Signature · 1 line
2.30.2
PATCH v6 5/5 virtio-gpu: Add gl_flushed callback
In reply to Vivek KasireddyAdding this callback provides a way to resume the processing of cmds in fenceq and cmdq that were not processed because the UI was waiting on a fence and blocked cmd processing.
--- hw/display/virtio-gpu.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-)
hw/display/virtio-gpu.c +30 -2
@@ -985,8 +985,10 @@ void virtio_gpu_simple_process_cmd(VirtIOGPU *g,break ; }if (!cmd->finished) { -virtio_gpu_ctrl_response_nodata (g, cmd, cmd->error ? cmd->error : -VIRTIO_GPU_RESP_OK_NODATA ); +if (!g->parent_obj.renderer_blocked) { +virtio_gpu_ctrl_response_nodata (g, cmd, cmd->error ? cmd->error : +VIRTIO_GPU_RESP_OK_NODATA ); + } } }
@@ -1042,6 +1044,30 @@ void virtio_gpu_process_cmdq(VirtIOGPU *g) g->processing_cmdq = false; } +static void virtio_gpu_process_fenceq (VirtIOGPU *g) +{ +struct virtio_gpu_ctrl_command *cmd, *tmp; + + QTAILQ_FOREACH_SAFE(cmd, &g->fenceq, next, tmp) { +trace_virtio_gpu_fence_resp (cmd->cmd_hdr.fence_id); +virtio_gpu_ctrl_response_nodata (g, cmd,VIRTIO_GPU_RESP_OK_NODATA ); +QTAILQ_REMOVE (&g->fenceq, cmd, next); +g_free (cmd); + g->inflight--; +if (virtio_gpu_stats_enabled (g->parent_obj.conf)) { +fprintf (stderr,"inflight: %3d (-)\r" , g->inflight); + } + } +} + +static void virtio_gpu_handle_gl_flushed (VirtIOGPUBase *b) +{ +VirtIOGPU *g =container_of (b, VirtIOGPU, parent_obj); + +virtio_gpu_process_fenceq (g); +virtio_gpu_process_cmdq (g); +} +static void virtio_gpu_handle_ctrl (VirtIODevice *vdev,VirtQueue *vq) {VirtIOGPU *g =VIRTIO_GPU (vdev);
@@ -1400,10 +1426,12 @@ static void virtio_gpu_class_init(ObjectClass *klass, void *data)DeviceClass *dc =DEVICE_CLASS (klass);VirtioDeviceClass *vdc =VIRTIO_DEVICE_CLASS (klass);VirtIOGPUClass *vgc =VIRTIO_GPU_CLASS (klass); +VirtIOGPUBaseClass *vgbc = &vgc->parent; vgc->handle_ctrl = virtio_gpu_handle_ctrl; vgc->process_cmd = virtio_gpu_simple_process_cmd; vgc->update_cursor_data = virtio_gpu_update_cursor_data; + vgbc->gl_flushed = virtio_gpu_handle_gl_flushed; vdc->realize = virtio_gpu_device_realize; vdc->reset = virtio_gpu_reset;
Signature · 1 line
2.30.2