Skip to contentSkip to conversation

ui/gtk: flush display pipeline before saving vmstate when blob=true

PATCH In review Dongwon Kim 5 participants 17 messages Dec 5, 2023 – Mar 6, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ui/gtk: flush display pipeline before saving vmstate when blob=true

If the guest state is paused before it gets a response for the current scanout frame submission (resource-flush), it won't start submitting new frames after being restored as it still waits for the old response, which is accepted as a scanout render done signal. So it's needed to unblock the current scanout render pipeline before the run state is changed to make sure the guest receives the response for the current frame submission.

Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 ui/gtk.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
ui/gtk.c +12 -0
index 810d7fc796..0f6237dd2f 100644
@@ -678,6 +678,18 @@ static const DisplayGLCtxOps egl_ctx_ops = {
 static void gd_change_runstate(void *opaque, bool running, RunState state)
 {
     GtkDisplayState *s = opaque;
+    int i;
+
+    if (state == RUN_STATE_SAVE_VM) {
+        for (i = 0; i < s->nb_vcs; i++) {
+            VirtualConsole *vc = &s->vc[i];
+
+            if (vc->gfx.guest_fb.dmabuf) {
+                /* force flushing current scanout blob rendering process */
+                gd_hw_gl_flushed(vc);
+            }
+        }
+    }
 
     gd_update_caption(s);
 }
Signature · 1 line
2.34.1
#1 lore raw

Hi

Quoted 21 lines · Dongwon Kim

If the guest state is paused before it gets a response for the current scanout frame submission (resource-flush), it won't start submitting new frames after being restored as it still waits for the old response, which is accepted as a scanout render done signal. So it's needed to unblock the current scanout render pipeline before the run state is changed to make sure the guest receives the response for the current frame submission.

Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 ui/gtk.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
ui/gtk.c +0 -0
index 810d7fc796..0f6237dd2f 100644

ui/gtk.c static const DisplayGLCtxOps egl_ctx_ops = { · line 687

Quoted 10 lines
ui/gtk.c +7 -0
@@ -678,6 +678,18 @@ static const DisplayGLCtxOps egl_ctx_ops = {
 static void gd_change_runstate(void *opaque, bool running, RunState state)
 {
     GtkDisplayState *s = opaque;
+    int i;
+
+    if (state == RUN_STATE_SAVE_VM) {
+        for (i = 0; i < s->nb_vcs; i++) {
+            VirtualConsole *vc = &s->vc[i];
+
+            if (vc->gfx.guest_fb.dmabuf) {

&& ..dmabuf->fence_fd >= 0

ui/gtk.c static const DisplayGLCtxOps egl_ctx_ops = { · line 689

Quoted 2 lines
ui/gtk.c +2 -0
+                /* force flushing current scanout blob rendering process */
+                gd_hw_gl_flushed(vc);

This defeats the purpose of the fence, maybe we should wait for it to be signaled first. At worse, I suppose the client can have some glitches. Although since the guest is stopped, this is unlikely.

#2 lore raw

Hi Marc-André,

Quoted 46 lines

-----Original Message-----
From: Marc-André Lureau <[email protected]>
Sent: Monday, December 4, 2023 11:15 PM
To: Kim, Dongwon <[email protected]>
Cc: [email protected]; Kasireddy, Vivek <[email protected]>
Subject: Re: [PATCH] ui/gtk: flush display pipeline before saving vmstate when blob=true

Hi

Quoted 24 lines · Dongwon Kim

If the guest state is paused before it gets a response for the current scanout frame submission (resource-flush), it won't start submitting new frames after being restored as it still waits for the old response, which is accepted as a scanout render done signal. So it's needed to unblock the current scanout render pipeline before the run state is changed to make sure the guest receives the response for the current frame submission.

Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 ui/gtk.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
ui/gtk.c +0 -0
index 810d7fc796..0f6237dd2f 100644
@@ -678,6 +678,18 @@ static const DisplayGLCtxOps egl_ctx_ops = {

static void gd_change_runstate(void *opaque, bool running, RunState

state)  {
     GtkDisplayState *s = opaque;

ui/gtk.c static const DisplayGLCtxOps egl_ctx_ops = { · line 687

Quoted 7 lines
ui/gtk.c +7 -0
+    int i;
+
+    if (state == RUN_STATE_SAVE_VM) {
+        for (i = 0; i < s->nb_vcs; i++) {
+            VirtualConsole *vc = &s->vc[i];
+
+            if (vc->gfx.guest_fb.dmabuf) {

&& ..dmabuf->fence_fd >= 0

Quoted 6 lines

ui/gtk.c static const DisplayGLCtxOps egl_ctx_ops = { · line 689

Quoted 2 lines
ui/gtk.c +2 -0
+                /* force flushing current scanout blob rendering process */
+                gd_hw_gl_flushed(vc);

This defeats the purpose of the fence, maybe we should wait for it to be signaled first. At worse, I suppose the client can have some glitches. Although since the guest is stopped, this is unlikely.

[Kim, Dongwon]
So this is the flow you are referring to?

            if (vc->gfx.guest_fb.dmabuf &&
                vc->gfx.guest_fb.dmabuf->fence_fd >= 0) {
                EGLint ret = eglClientWaitSync(qemu_egl_display,
                                               vc->gfx.guest_fb.dmabuf->sync,
                                               EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
                                               100000000); /* timeout of 100ms */

                if (ret != EGL_CONDITION_SATISFIED_KHR) {
                    /* force flushing current scanout blob rendering process */
                    gd_hw_gl_flushed(vc);
                }
            }

If yes, I will test this then create v2 based on this flow. Thanks

#3 lore raw

Hi

Quoted 71 lines · Dongwon

Hi Marc-André,

Quoted 46 lines

-----Original Message-----
From: Marc-André Lureau <[email protected]>
Sent: Monday, December 4, 2023 11:15 PM
To: Kim, Dongwon <[email protected]>
Cc: [email protected]; Kasireddy, Vivek <[email protected]>
Subject: Re: [PATCH] ui/gtk: flush display pipeline before saving vmstate when blob=true

Hi

Quoted 24 lines · Dongwon Kim

If the guest state is paused before it gets a response for the current scanout frame submission (resource-flush), it won't start submitting new frames after being restored as it still waits for the old response, which is accepted as a scanout render done signal. So it's needed to unblock the current scanout render pipeline before the run state is changed to make sure the guest receives the response for the current frame submission.

Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 ui/gtk.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
ui/gtk.c +0 -0
index 810d7fc796..0f6237dd2f 100644
@@ -678,6 +678,18 @@ static const DisplayGLCtxOps egl_ctx_ops = {

static void gd_change_runstate(void *opaque, bool running, RunState

state)  {
     GtkDisplayState *s = opaque;

ui/gtk.c static const DisplayGLCtxOps egl_ctx_ops = { · line 687

Quoted 7 lines
ui/gtk.c +7 -0
+    int i;
+
+    if (state == RUN_STATE_SAVE_VM) {
+        for (i = 0; i < s->nb_vcs; i++) {
+            VirtualConsole *vc = &s->vc[i];
+
+            if (vc->gfx.guest_fb.dmabuf) {

&& ..dmabuf->fence_fd >= 0

Quoted 6 lines

ui/gtk.c static const DisplayGLCtxOps egl_ctx_ops = { · line 689

Quoted 2 lines
ui/gtk.c +2 -0
+                /* force flushing current scanout blob rendering process */
+                gd_hw_gl_flushed(vc);

This defeats the purpose of the fence, maybe we should wait for it to be signaled first. At worse, I suppose the client can have some glitches. Although since the guest is stopped, this is unlikely.

[Kim, Dongwon]
So this is the flow you are referring to?

            if (vc->gfx.guest_fb.dmabuf &&
                vc->gfx.guest_fb.dmabuf->fence_fd >= 0) {
                EGLint ret = eglClientWaitSync(qemu_egl_display,
                                               vc->gfx.guest_fb.dmabuf->sync,
                                               EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
                                               100000000); /* timeout of 100ms */

                if (ret != EGL_CONDITION_SATISFIED_KHR) {
                    /* force flushing current scanout blob rendering process */
                    gd_hw_gl_flushed(vc);
                }
            }
Quoted 1 line

If yes, I will test this then create v2 based on this flow.

Yes, you may want to call gd_hw_gl_flushed() even on success, to avoid waiting for the main loop dispatch.

#4 lore raw

PATCH 1/3 ui/gtk: flush display pipeline before saving vmstate when blob=true

In reply to Dongwon Kim

If the guest state is paused before it gets a response for the current scanout frame submission (resource-flush), it won't flush new frames after being restored as it still waits for the old response, which is accepted as a scanout render done signal. So it's needed to unblock the current scanout render pipeline before the run state is changed to make sure the guest receives the response for the current frame submission.

v2: Giving some time for the fence to be signaled before flushing
the pipeline

Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 ui/gtk.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
ui/gtk.c +19 -0
index 810d7fc796..ea8d07833e 100644
@@ -678,6 +678,25 @@ static const DisplayGLCtxOps egl_ctx_ops = {
 static void gd_change_runstate(void *opaque, bool running, RunState state)
 {
     GtkDisplayState *s = opaque;
+    int i;
+
+    if (state == RUN_STATE_SAVE_VM) {
+        for (i = 0; i < s->nb_vcs; i++) {
+            VirtualConsole *vc = &s->vc[i];
+
+            if (vc->gfx.guest_fb.dmabuf &&
+                vc->gfx.guest_fb.dmabuf->fence_fd >= 0) {
+                eglClientWaitSync(qemu_egl_display,
+                                  vc->gfx.guest_fb.dmabuf->sync,
+                                  EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
+                                  100000000);
+
+                /* force flushing current scanout blob rendering process
+                 * just in case the fence is still not signaled */
+                gd_hw_gl_flushed(vc);
+            }
+        }
+    }
 
     gd_update_caption(s);
 }
Signature · 1 line
2.34.1
#5 lore raw

PATCH 2/3 ui/gtk: unblock pipeline only if fence hasn't been signaled yet

It is needed to unblock the pipeline only if there is an active dmabuf to be rendered and the fence for it is not yet signaled.

Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 ui/gtk.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
ui/gtk.c +10 -4
index ea8d07833e..073c9eadb8 100644
@@ -597,10 +597,16 @@ void gd_hw_gl_flushed(void *vcon)
     VirtualConsole *vc = vcon;
     QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
 
-    qemu_set_fd_handler(dmabuf->fence_fd, NULL, NULL, NULL);
-    close(dmabuf->fence_fd);
-    dmabuf->fence_fd = -1;
-    graphic_hw_gl_block(vc->gfx.dcl.con, false);
+    if (!dmabuf) {
+        return;
+    }
+
+    if (dmabuf->fence_fd > 0) {
+        qemu_set_fd_handler(dmabuf->fence_fd, NULL, NULL, NULL);
+        close(dmabuf->fence_fd);
+        dmabuf->fence_fd = -1;
+        graphic_hw_gl_block(vc->gfx.dcl.con, false);
+    }
 }
 
 /** DisplayState Callbacks (opengl version) **/
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH 2/3] ui/gtk: unblock pipeline only if fence hasn't been signaled yet

Hi

Quoted 16 lines · Dongwon Kim

It is needed to unblock the pipeline only if there is an active dmabuf to be rendered and the fence for it is not yet signaled.

Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 ui/gtk.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
ui/gtk.c +0 -0
index ea8d07833e..073c9eadb8 100644

ui/gtk.c void gd_hw_gl_flushed(void *vcon) · line 602

Quoted 10 lines
ui/gtk.c +3 -4
@@ -597,10 +597,16 @@ void gd_hw_gl_flushed(void *vcon)
     VirtualConsole *vc = vcon;
     QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
 
-    qemu_set_fd_handler(dmabuf->fence_fd, NULL, NULL, NULL);
-    close(dmabuf->fence_fd);
-    dmabuf->fence_fd = -1;
-    graphic_hw_gl_block(vc->gfx.dcl.con, false);
+    if (!dmabuf) {
+        return;
+    }

When is this function called with dmabuf == NULL or fence_fd < 0?

ui/gtk.c void gd_hw_gl_flushed(void *vcon) · line 604

Quoted 2 lines
ui/gtk.c +2 -0
+
+    if (dmabuf->fence_fd > 0) {

this should be >= 0

Quoted 12 lines
ui/gtk.c +5 -1
+        qemu_set_fd_handler(dmabuf->fence_fd, NULL, NULL, NULL);
+        close(dmabuf->fence_fd);
+        dmabuf->fence_fd = -1;
+        graphic_hw_gl_block(vc->gfx.dcl.con, false);
+    }
 }
 
 /** DisplayState Callbacks (opengl version) **/
--

2.34.1

Signature · 1 line
Marc-André Lureau
#7 lore raw

Re: [PATCH 2/3] ui/gtk: unblock pipeline only if fence hasn't been signaled yet

Quoted 32 lines · Marc-André Lureau

Hi

Quoted 16 lines · Dongwon Kim

It is needed to unblock the pipeline only if there is an active dmabuf to be rendered and the fence for it is not yet signaled.

Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 ui/gtk.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
ui/gtk.c +0 -0
index ea8d07833e..073c9eadb8 100644

ui/gtk.c void gd_hw_gl_flushed(void *vcon) · line 602

Quoted 10 lines
ui/gtk.c +3 -4
@@ -597,10 +597,16 @@ void gd_hw_gl_flushed(void *vcon)
     VirtualConsole *vc = vcon;
     QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
 
-    qemu_set_fd_handler(dmabuf->fence_fd, NULL, NULL, NULL);
-    close(dmabuf->fence_fd);
-    dmabuf->fence_fd = -1;
-    graphic_hw_gl_block(vc->gfx.dcl.con, false);
+    if (!dmabuf) {
+        return;
+    }

When is this function called with dmabuf == NULL or fence_fd < 0?

Quoted 4 lines

ui/gtk.c void gd_hw_gl_flushed(void *vcon) · line 604

Quoted 2 lines
ui/gtk.c +2 -0
+
+    if (dmabuf->fence_fd > 0) {

this should be >= 0

There's another in line 102 already and one in gtk-gl-area.c too.

Regards,
BALATON Zoltan

Quoted 15 lines
Quoted 12 lines
ui/gtk.c +5 -1
+        qemu_set_fd_handler(dmabuf->fence_fd, NULL, NULL, NULL);
+        close(dmabuf->fence_fd);
+        dmabuf->fence_fd = -1;
+        graphic_hw_gl_block(vc->gfx.dcl.con, false);
+    }
 }
 
 /** DisplayState Callbacks (opengl version) **/
--

2.34.1

#8 lore raw

RE: [PATCH 2/3] ui/gtk: unblock pipeline only if fence hasn't been signaled yet

In reply to Marc-André Lureau

Hi Marc-André,

I reviewed and realized these conditions won't be met in normal situations in given upstream code. But we've initially added those conditions in our internal code base for dev because we often had to call gd_hw_gl_flushed to forcefully unblock from HPD code (i.e. 'connectors' param. Not Upstreamed yet) when VM display is disconnected. In such cases, it is needed to make sure there is a frame in the pipeline already. Anyway, I think we can check dmabuf==NULL and fence_fd < 0 before calling gd_hw_flushed in HPD code just as in gd_change_runstate ([PATCH 1/3] ui/gtk: flush display pipeline before saving vmstate when blob=true).

Quoted 57 lines

Subject: Re: [PATCH 2/3] ui/gtk: unblock pipeline only if fence hasn't been signaled yet

Hi

Quoted 16 lines · Dongwon Kim

It is needed to unblock the pipeline only if there is an active dmabuf to be rendered and the fence for it is not yet signaled.

Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 ui/gtk.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
ui/gtk.c +0 -0
index ea8d07833e..073c9eadb8 100644

ui/gtk.c void gd_hw_gl_flushed(void *vcon) · line 602

Quoted 10 lines
ui/gtk.c +3 -4
@@ -597,10 +597,16 @@ void gd_hw_gl_flushed(void *vcon)
     VirtualConsole *vc = vcon;
     QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
 
-    qemu_set_fd_handler(dmabuf->fence_fd, NULL, NULL, NULL);
-    close(dmabuf->fence_fd);
-    dmabuf->fence_fd = -1;
-    graphic_hw_gl_block(vc->gfx.dcl.con, false);
+    if (!dmabuf) {
+        return;
+    }

When is this function called with dmabuf == NULL or fence_fd < 0?

ui/gtk.c void gd_hw_gl_flushed(void *vcon) · line 604

Quoted 2 lines
ui/gtk.c +2 -0
+
+    if (dmabuf->fence_fd > 0) {

this should be >= 0

Quoted 9 lines
ui/gtk.c +5 -1
+        qemu_set_fd_handler(dmabuf->fence_fd, NULL, NULL, NULL);
+        close(dmabuf->fence_fd);
+        dmabuf->fence_fd = -1;
+        graphic_hw_gl_block(vc->gfx.dcl.con, false);
+    }
 }
 
 /** DisplayState Callbacks (opengl version) **/
--
Quoted 3 lines

2.34.1

--
Marc-André Lureau

#9 lore raw

PATCH 3/3 virtio-gpu: first surface update with blob scanout after resumed

In reply to Dongwon Kim

The guest surface needs to be updated with a blob scanout after resumed from saved vm state if blob is enabled.

Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 hw/display/virtio-gpu.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
hw/display/virtio-gpu.c +15 -7
index b016d3bac8..34dc0ab9fc 100644
@@ -1417,19 +1417,27 @@ static int virtio_gpu_post_load(void *opaque, int version_id)
         if (!res) {
             return -EINVAL;
         }
-        scanout->ds = qemu_create_displaysurface_pixman(res->image);
-        if (!scanout->ds) {
-            return -EINVAL;
-        }
+
+        if (res->blob_size) {
+            assert(g->dmabuf.primary[i] != NULL);
+            g->dmabuf.primary[i]->buf.fd = res->dmabuf_fd;
+            dpy_gl_scanout_dmabuf(scanout->con, &g->dmabuf.primary[i]->buf);
+        } else {
+            scanout->ds = qemu_create_displaysurface_pixman(res->image);
+            if (!scanout->ds) {
+                return -EINVAL;
+            }
 #ifdef WIN32
-        qemu_displaysurface_win32_set_handle(scanout->ds, res->handle, 0);
+            qemu_displaysurface_win32_set_handle(scanout->ds, res->handle, 0);
 #endif
+            dpy_gfx_replace_surface(scanout->con, scanout->ds);
+            dpy_gfx_update_full(scanout->con);
+        }
 
-        dpy_gfx_replace_surface(scanout->con, scanout->ds);
-        dpy_gfx_update_full(scanout->con);
         if (scanout->cursor.resource_id) {
             update_cursor(g, &scanout->cursor);
         }
+
         res->scanout_bitmask |= (1 << i);
     }
 
Signature · 1 line
2.34.1
#10 lore raw

Re: [PATCH 3/3] virtio-gpu: first surface update with blob scanout after resumed

Hi Dongwon Kim

Quoted 7 lines · Dongwon Kim

The guest surface needs to be updated with a blob scanout after resumed from saved vm state if blob is enabled.

Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>

Can you resend the last version of the series?

thanks

Quoted 47 lines
---
 hw/display/virtio-gpu.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
hw/display/virtio-gpu.c +15 -8
index b016d3bac8..34dc0ab9fc 100644
@@ -1417,19 +1417,27 @@ static int virtio_gpu_post_load(void *opaque, int version_id)
         if (!res) {
             return -EINVAL;
         }
-        scanout->ds = qemu_create_displaysurface_pixman(res->image);
-        if (!scanout->ds) {
-            return -EINVAL;
-        }
+
+        if (res->blob_size) {
+            assert(g->dmabuf.primary[i] != NULL);
+            g->dmabuf.primary[i]->buf.fd = res->dmabuf_fd;
+            dpy_gl_scanout_dmabuf(scanout->con, &g->dmabuf.primary[i]->buf);
+        } else {
+            scanout->ds = qemu_create_displaysurface_pixman(res->image);
+            if (!scanout->ds) {
+                return -EINVAL;
+            }
 #ifdef WIN32
-        qemu_displaysurface_win32_set_handle(scanout->ds, res->handle, 0);
+            qemu_displaysurface_win32_set_handle(scanout->ds, res->handle, 0);
 #endif
+            dpy_gfx_replace_surface(scanout->con, scanout->ds);
+            dpy_gfx_update_full(scanout->con);
+        }
 
-        dpy_gfx_replace_surface(scanout->con, scanout->ds);
-        dpy_gfx_update_full(scanout->con);
         if (scanout->cursor.resource_id) {
             update_cursor(g, &scanout->cursor);
         }
+
         res->scanout_bitmask |= (1 << i);
     }
 
--

2.34.1

Signature · 1 line
Marc-André Lureau
#11 lore raw

PATCH 1/2 ui/gtk: flush display pipeline before saving vmstate when blob=true

From: Dongwon Kim <[email protected]>

If the guest state is paused before it gets a response for the current scanout frame submission (resource-flush), it won't flush new frames after being restored as it still waits for the old response, which is accepted as a scanout render done signal. So it's needed to unblock the current scanout render pipeline before the run state is changed to make sure the guest receives the response for the current frame submission.

v2: Giving some time for the fence to be signaled before flushing
the pipeline

v3: Prevent redundant call of gd_hw_gl_flushed by checking dmabuf
and fence_fd >= 0 in it (e.g. during and after eglClientWaitSync in gd_change_runstate).

Destroy sync object later in gd_hw_fl_flushed

Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 ui/egl-helpers.c |  2 --
 ui/gtk.c         | 31 +++++++++++++++++++++++++++----
 2 files changed, 27 insertions(+), 6 deletions(-)
ui/egl-helpers.c +0 -2
index 3d19dbe382..a77f9e57d9 100644
@@ -385,8 +385,6 @@ 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;
     }
 }
 
ui/gtk.c +27 -4
index 810d7fc796..eaca890cba 100644
@@ -597,10 +597,14 @@ void gd_hw_gl_flushed(void *vcon)
     VirtualConsole *vc = vcon;
     QemuDmaBuf *dmabuf = vc->gfx.guest_fb.dmabuf;
 
-    qemu_set_fd_handler(dmabuf->fence_fd, NULL, NULL, NULL);
-    close(dmabuf->fence_fd);
-    dmabuf->fence_fd = -1;
-    graphic_hw_gl_block(vc->gfx.dcl.con, false);
+    if (dmabuf && dmabuf->fence_fd >= 0) {
+        qemu_set_fd_handler(dmabuf->fence_fd, NULL, NULL, NULL);
+        close(dmabuf->fence_fd);
+        dmabuf->fence_fd = -1;
+        eglDestroySyncKHR(qemu_egl_display, dmabuf->sync);
+        dmabuf->sync = NULL;
+        graphic_hw_gl_block(vc->gfx.dcl.con, false);
+    }
 }
 
 /** DisplayState Callbacks (opengl version) **/
@@ -678,6 +682,25 @@ static const DisplayGLCtxOps egl_ctx_ops = {
 static void gd_change_runstate(void *opaque, bool running, RunState state)
 {
     GtkDisplayState *s = opaque;
+    int i;
+
+    if (state == RUN_STATE_SAVE_VM) {
+        for (i = 0; i < s->nb_vcs; i++) {
+            VirtualConsole *vc = &s->vc[i];
+
+            if (vc->gfx.guest_fb.dmabuf &&
+                vc->gfx.guest_fb.dmabuf->fence_fd >= 0) {
+                eglClientWaitSync(qemu_egl_display,
+                                  vc->gfx.guest_fb.dmabuf->sync,
+                                  EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
+                                  100000000);
+
+                /* force flushing current scanout blob rendering process
+                 * just in case the fence is still not signaled */
+                gd_hw_gl_flushed(vc);
+            }
+        }
+    }
 
     gd_update_caption(s);
 }
Signature · 1 line
2.34.1
#12 lore raw

PATCH 2/2 virtio-gpu: first surface update with blob scanout after resumed

From: Dongwon Kim <[email protected]>

The guest surface needs to be updated with a blob scanout after resumed from saved vm state if blob is enabled.

Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 hw/display/virtio-gpu.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
hw/display/virtio-gpu.c +14 -7
index 1c1ee230b3..01bc4f9565 100644
@@ -1422,16 +1422,23 @@ static int virtio_gpu_post_load(void *opaque, int version_id)
         if (!res) {
             return -EINVAL;
         }
-        scanout->ds = qemu_create_displaysurface_pixman(res->image);
-        if (!scanout->ds) {
-            return -EINVAL;
-        }
+
+        if (res->blob_size) {
+            assert(g->dmabuf.primary[i] != NULL);
+            g->dmabuf.primary[i]->buf.fd = res->dmabuf_fd;
+            dpy_gl_scanout_dmabuf(scanout->con, &g->dmabuf.primary[i]->buf);
+        } else {
+            scanout->ds = qemu_create_displaysurface_pixman(res->image);
+            if (!scanout->ds) {
+                return -EINVAL;
+            }
 #ifdef WIN32
-        qemu_displaysurface_win32_set_handle(scanout->ds, res->handle, 0);
+            qemu_displaysurface_win32_set_handle(scanout->ds, res->handle, 0);
 #endif
+            dpy_gfx_replace_surface(scanout->con, scanout->ds);
+            dpy_gfx_update_full(scanout->con);
+        }
 
-        dpy_gfx_replace_surface(scanout->con, scanout->ds);
-        dpy_gfx_update_full(scanout->con);
         if (scanout->cursor.resource_id) {
             update_cursor(g, &scanout->cursor);
         }
Signature · 1 line
2.34.1
#13 lore raw

Re: [PATCH 2/2] virtio-gpu: first surface update with blob scanout after resumed

Hi Dongwon Kim

Quoted 6 lines · On Wed, Mar 6, 2024 at 2:24 AM <[email protected]> wrote:

From: Dongwon Kim <[email protected]>

The guest surface needs to be updated with a blob scanout after resumed from saved vm state if blob is enabled.

Quoted 7 lines
Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 hw/display/virtio-gpu.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

The way you send patches to the ML confuses patchew, and they are not properly triaged or tested.
(https://patchew.org/search?q=project%3AQEMU+from%3Adongwon.kim%40intel.com)

Please send a series of your pending patches, with a new version. It's confusing with the various mails otherwise. And it's then easier to deal with regular patch series that are tracked by patchew.

thanks

Quoted 38 lines
hw/display/virtio-gpu.c +14 -8
index 1c1ee230b3..01bc4f9565 100644
@@ -1422,16 +1422,23 @@ static int virtio_gpu_post_load(void *opaque, int version_id)
         if (!res) {
             return -EINVAL;
         }
-        scanout->ds = qemu_create_displaysurface_pixman(res->image);
-        if (!scanout->ds) {
-            return -EINVAL;
-        }
+
+        if (res->blob_size) {
+            assert(g->dmabuf.primary[i] != NULL);
+            g->dmabuf.primary[i]->buf.fd = res->dmabuf_fd;
+            dpy_gl_scanout_dmabuf(scanout->con, &g->dmabuf.primary[i]->buf);
+        } else {
+            scanout->ds = qemu_create_displaysurface_pixman(res->image);
+            if (!scanout->ds) {
+                return -EINVAL;
+            }
 #ifdef WIN32
-        qemu_displaysurface_win32_set_handle(scanout->ds, res->handle, 0);
+            qemu_displaysurface_win32_set_handle(scanout->ds, res->handle, 0);
 #endif
+            dpy_gfx_replace_surface(scanout->con, scanout->ds);
+            dpy_gfx_update_full(scanout->con);
+        }
 
-        dpy_gfx_replace_surface(scanout->con, scanout->ds);
-        dpy_gfx_update_full(scanout->con);
         if (scanout->cursor.resource_id) {
             update_cursor(g, &scanout->cursor);
         }
--

2.34.1

Signature · 1 line
Marc-André Lureau
#14 lore raw
In reply to Dongwon Kim

Hi

Quoted 29 lines · Dongwon Kim

If the guest state is paused before it gets a response for the current scanout frame submission (resource-flush), it won't flush new frames after being restored as it still waits for the old response, which is accepted as a scanout render done signal. So it's needed to unblock the current scanout render pipeline before the run state is changed to make sure the guest receives the response for the current frame submission.

v2: Giving some time for the fence to be signaled before flushing
the pipeline

Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 ui/gtk.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
ui/gtk.c +2 -0
index 810d7fc796..ea8d07833e 100644
@@ -678,6 +678,25 @@ static const DisplayGLCtxOps egl_ctx_ops = {
 static void gd_change_runstate(void *opaque, bool running, RunState state)
 {
     GtkDisplayState *s = opaque;
+    int i;
+

ui/gtk.c static const DisplayGLCtxOps egl_ctx_ops = { · line 692

Quoted 10 lines
ui/gtk.c +10 -0
+    if (state == RUN_STATE_SAVE_VM) {
+        for (i = 0; i < s->nb_vcs; i++) {
+            VirtualConsole *vc = &s->vc[i];
+
+            if (vc->gfx.guest_fb.dmabuf &&
+                vc->gfx.guest_fb.dmabuf->fence_fd >= 0) {
+                eglClientWaitSync(qemu_egl_display,
+                                  vc->gfx.guest_fb.dmabuf->sync,
+                                  EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
+                                  100000000);

This won't work. dmabuf->sync is NULL after egl_dmabuf_create_sync.

I will let Vivek, who wrote the sync code, comment.

thanks

Signature · 1 line
Marc-André Lureau
#15 lore raw

Hi,

Quoted 29 lines
Quoted 24 lines · Dongwon Kim

If the guest state is paused before it gets a response for the current scanout frame submission (resource-flush), it won't flush new frames after being restored as it still waits for the old response, which is accepted as a scanout render done signal. So it's needed to unblock the current scanout render pipeline before the run state is changed to make sure the guest receives the response for the current frame submission.

v2: Giving some time for the fence to be signaled before flushing
the pipeline

Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 ui/gtk.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
ui/gtk.c +0 -0
index 810d7fc796..ea8d07833e 100644

ui/gtk.c static const DisplayGLCtxOps egl_ctx_ops = { · line 678

Quoted 1 line
ui/gtk.c +0 -0
@@ -678,6 +678,25 @@ static const DisplayGLCtxOps egl_ctx_ops = {
 static void gd_change_runstate(void *opaque, bool running, RunState

state)

Quoted 16 lines
Quoted 4 lines
ui/gtk.c +2 -0
 {
     GtkDisplayState *s = opaque;
+    int i;
+

ui/gtk.c static const DisplayGLCtxOps egl_ctx_ops = { · line 692

Quoted 10 lines
ui/gtk.c +10 -0
+    if (state == RUN_STATE_SAVE_VM) {
+        for (i = 0; i < s->nb_vcs; i++) {
+            VirtualConsole *vc = &s->vc[i];
+
+            if (vc->gfx.guest_fb.dmabuf &&
+                vc->gfx.guest_fb.dmabuf->fence_fd >= 0) {
+                eglClientWaitSync(qemu_egl_display,
+                                  vc->gfx.guest_fb.dmabuf->sync,
+                                  EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
+                                  100000000);

This won't work. dmabuf->sync is NULL after egl_dmabuf_create_sync.

Right, we destroy the sync object after we create the fence from it. If you want to use eglClientWaitSync() here, you either need to recreate the sync object using fence_fd or don't destroy it in egl_dmabuf_create_fence(). Either way should be ok but make sure you destroy it when the fence_fd is closed.

Thanks,
Vivek

Quoted 9 lines

I will let Vivek, who wrote the sync code, comment.

thanks

--
Marc-André Lureau

#16 lore raw

Hi,

Quoted 50 lines

Subject: RE: [PATCH 1/3] ui/gtk: flush display pipeline before saving vmstate when blob=true

Hi,

Quoted 29 lines
Quoted 24 lines · Dongwon Kim

If the guest state is paused before it gets a response for the current scanout frame submission (resource-flush), it won't flush new frames after being restored as it still waits for the old response, which is accepted as a scanout render done signal. So it's needed to unblock the current scanout render pipeline before the run state is changed to make sure the guest receives the response for the current frame submission.

v2: Giving some time for the fence to be signaled before flushing
the pipeline

Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 ui/gtk.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
ui/gtk.c +0 -0
index 810d7fc796..ea8d07833e 100644
@@ -678,6 +678,25 @@ static const DisplayGLCtxOps egl_ctx_ops = {
Quoted 1 line

static void gd_change_runstate(void *opaque, bool running, RunState

state)

Quoted 16 lines
Quoted 4 lines
 {
     GtkDisplayState *s = opaque;
ui/gtk.c +2 -0
+    int i;
+

ui/gtk.c static const DisplayGLCtxOps egl_ctx_ops = { · line 692

Quoted 10 lines
ui/gtk.c +10 -0
+    if (state == RUN_STATE_SAVE_VM) {
+        for (i = 0; i < s->nb_vcs; i++) {
+            VirtualConsole *vc = &s->vc[i];
+
+            if (vc->gfx.guest_fb.dmabuf &&
+                vc->gfx.guest_fb.dmabuf->fence_fd >= 0) {
+                eglClientWaitSync(qemu_egl_display,
+                                  vc->gfx.guest_fb.dmabuf->sync,
+                                  EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
+                                  100000000);

This won't work. dmabuf->sync is NULL after egl_dmabuf_create_sync.

Quoted 5 lines

Right, we destroy the sync object after we create the fence from it. If you want to use eglClientWaitSync() here, you either need to recreate the sync object using fence_fd or don't destroy it in egl_dmabuf_create_fence(). Either way should be ok but make sure you destroy it when the fence_fd is closed.

I guess it makes sense to destroy sync object inside gd_hw_gl_flushed if that is the case. Another thing is I mentioned that "dmabuf == NULL or fence_fd < 0" doesn't seem to be checked in gd_hw_flushed in my previous reply but now I am thinking it is needed because gd_hw_gl_flushed can be called twice with given code change - once when rendering is done and the fence is signaled during eglClientWaitSync and second after eglClientWaitSync. I will come up with a new version of patches.

Quoted 13 lines

Thanks,
Vivek

Quoted 9 lines

I will let Vivek, who wrote the sync code, comment.

thanks

--
Marc-André Lureau