ui/console: Introduce helpers for creating and
v4 cover ui/console: Introduce helpers for creating and
From: Dongwon Kim <[email protected]>
QemuDmaBuf struct is defined and primarily used by ui/console/gl so it is better to handle its creation, initialization and access within ui/console rather than within hw modules such as hw/display/virtio-gpu, hw/display/vhost-user-gpu and hw/vfio as well as ui/dbus-listener.
v4: refactored patches in the previous series
made ui/dbus-listener and hw/display/vhost-user-gpu to use the new helpers
Dongwon Kim (3):
ui/console: Introduce dpy_gl_dmabuf_get_height/width() helpers ui/console: Introduce dpy_gl_dmabuf_get_fd() helper ui/console: Introduce dpy_gl_create_dmabuf() helper
include/hw/vfio/vfio-common.h | 2 +- include/hw/virtio/virtio-gpu.h | 2 +- include/ui/console.h | 9 ++++++ hw/display/vhost-user-gpu.c | 12 +++---- hw/display/virtio-gpu-udmabuf.c | 26 ++++++---------- hw/vfio/display.c | 36 +++++++++++---------- ui/console.c | 55 +++++++++++++++++++++++++++++++++ ui/dbus-listener.c | 22 ++++++------- 8 files changed, 109 insertions(+), 55 deletions(-)
Signature · 1 line
2.34.1
PATCH v4 1/3 ui/console: Introduce dpy_gl_dmabuf_get_height/width() helpers
From: Dongwon Kim <[email protected]>
dpy_gl_dmabuf_get_height() and dpy_gl_dmabuf_get_width() are helpers for retrieving width and height fields from QemuDmaBuf struct.
--- include/ui/console.h | 2 ++ hw/display/virtio-gpu-udmabuf.c | 7 ++++--- hw/vfio/display.c | 9 ++++++--- ui/console.c | 18 ++++++++++++++++++ 4 files changed, 30 insertions(+), 6 deletions(-)
include/ui/console.h +2 -0
@@ -358,6 +358,8 @@ void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf, bool have_hot, uint32_t hot_x, uint32_t hot_y);void dpy_gl_cursor_position (QemuConsole *con,uint32_t pos_x,uint32_t pos_y); +uint32_t dpy_gl_dmabuf_get_width (QemuDmaBuf *dmabuf); +uint32_t dpy_gl_dmabuf_get_height (QemuDmaBuf *dmabuf);void dpy_gl_release_dmabuf (QemuConsole *con,QemuDmaBuf *dmabuf); void dpy_gl_update(QemuConsole *con,
hw/display/virtio-gpu-udmabuf.c +4 -3
@@ -206,6 +206,7 @@ int virtio_gpu_update_dmabuf(VirtIOGPU *g, {struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];VGPUDMABuf *new_primary, *old_primary =NULL ; +uint32_t width, height; new_primary =virtio_gpu_create_dmabuf (g, scanout_id, res, fb, r);if (!new_primary) {
@@ -216,10 +217,10 @@ int virtio_gpu_update_dmabuf(VirtIOGPU *g, old_primary = g->dmabuf.primary[scanout_id]; } + width =dpy_gl_dmabuf_get_width (&new_primary->buf); + height =dpy_gl_dmabuf_get_height (&new_primary->buf); g->dmabuf.primary[scanout_id] = new_primary; -qemu_console_resize (scanout->con, - new_primary->buf.width, - new_primary->buf.height); +qemu_console_resize (scanout->con, width, height);dpy_gl_scanout_dmabuf (scanout->con, &new_primary->buf);if (old_primary) {
hw/vfio/display.c +6 -3
@@ -286,6 +286,7 @@ static void vfio_display_dmabuf_update(void *opaque)VFIOPCIDevice *vdev = opaque;VFIODisplay *dpy = vdev->dpy;VFIODMABuf *primary, *cursor; +uint32_t width, height;bool free_bufs = false, new_cursor = false; primary =vfio_display_get_dmabuf (vdev,DRM_PLANE_TYPE_PRIMARY );
@@ -296,10 +297,12 @@ static void vfio_display_dmabuf_update(void *opaque)return ; } + width =dpy_gl_dmabuf_get_width (&primary->buf); + height =dpy_gl_dmabuf_get_height (&primary->buf); +if (dpy->dmabuf.primary != primary) { dpy->dmabuf.primary = primary; -qemu_console_resize (dpy->con, - primary->buf.width, primary->buf.height); +qemu_console_resize (dpy->con, width, height);dpy_gl_scanout_dmabuf (dpy->con, &primary->buf); free_bufs = true; }
@@ -328,7 +331,7 @@ static void vfio_display_dmabuf_update(void *opaque) cursor->pos_updates =0 ; } -dpy_gl_update (dpy->con,0 ,0 , primary->buf.width, primary->buf.height); +dpy_gl_update (dpy->con,0 ,0 , width, height);if (free_bufs) {vfio_display_free_dmabufs (vdev);
ui/console.c +18 -0
@@ -1132,6 +1132,24 @@ void dpy_gl_cursor_position(QemuConsole *con, } } +uint32_t dpy_gl_dmabuf_get_width (QemuDmaBuf *dmabuf) +{ +if (dmabuf) { +return dmabuf->width; + } + +return 0 ; +} + +uint32_t dpy_gl_dmabuf_get_height (QemuDmaBuf *dmabuf) +{ +if (dmabuf) { +return dmabuf->height; + } + +return 0 ; +} +void dpy_gl_release_dmabuf (QemuConsole *con,QemuDmaBuf *dmabuf) {
Signature · 1 line
2.34.1
Re: [PATCH v4 1/3] ui/console: Introduce dpy_gl_dmabuf_get_height/width() helpers
Hi Kim
Quoted 6 lines · On Fri, Mar 22, 2024 at 3:45 AM <[email protected]> wrote:
From: Dongwon Kim <[email protected]>
dpy_gl_dmabuf_get_height()anddpy_gl_dmabuf_get_width()are helpers for retrieving width and height fields from QemuDmaBuf struct.
There are many places left where width/height fields are still accessed directly.
If we want to make the whole structure private, we will probably need setters.
I don't see why the function should silently return 0 when given NULL. Imho an assert(dmabuf != NULL) is appropriate (or g_return_val_if_fail).
Quoted 116 lines
Cc Philippe Mathieu-Daudé <[email protected]>Cc Marc-André Lureau <[email protected]>Cc Vivek Kasireddy <[email protected]>Signed-off-by Dongwon Kim <[email protected]>--- include/ui/console.h | 2 ++ hw/display/virtio-gpu-udmabuf.c | 7 ++++--- hw/vfio/display.c | 9 ++++++--- ui/console.c | 18 ++++++++++++++++++ 4 files changed, 30 insertions(+), 6 deletions(-)include/ui/console.h +2
-0@@ -358,6 +358,8 @@ void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf, bool have_hot, uint32_t hot_x, uint32_t hot_y);void dpy_gl_cursor_position (QemuConsole *con,uint32_t pos_x,uint32_t pos_y); +uint32_t dpy_gl_dmabuf_get_width (QemuDmaBuf *dmabuf); +uint32_t dpy_gl_dmabuf_get_height (QemuDmaBuf *dmabuf);void dpy_gl_release_dmabuf (QemuConsole *con,QemuDmaBuf *dmabuf); void dpy_gl_update(QemuConsole *con,hw/display/virtio-gpu-udmabuf.c +4
-3@@ -206,6 +206,7 @@ int virtio_gpu_update_dmabuf(VirtIOGPU *g, {struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];VGPUDMABuf *new_primary, *old_primary =NULL ; +uint32_t width, height; new_primary =virtio_gpu_create_dmabuf (g, scanout_id, res, fb, r);if (!new_primary) {@@ -216,10 +217,10 @@ int virtio_gpu_update_dmabuf(VirtIOGPU *g, old_primary = g->dmabuf.primary[scanout_id]; } + width =dpy_gl_dmabuf_get_width (&new_primary->buf); + height =dpy_gl_dmabuf_get_height (&new_primary->buf); g->dmabuf.primary[scanout_id] = new_primary; -qemu_console_resize (scanout->con, - new_primary->buf.width, - new_primary->buf.height); +qemu_console_resize (scanout->con, width, height);dpy_gl_scanout_dmabuf (scanout->con, &new_primary->buf);if (old_primary) {hw/vfio/display.c +6
-3@@ -286,6 +286,7 @@ static void vfio_display_dmabuf_update(void *opaque)VFIOPCIDevice *vdev = opaque;VFIODisplay *dpy = vdev->dpy;VFIODMABuf *primary, *cursor; +uint32_t width, height;bool free_bufs = false, new_cursor = false; primary =vfio_display_get_dmabuf (vdev,DRM_PLANE_TYPE_PRIMARY );@@ -296,10 +297,12 @@ static void vfio_display_dmabuf_update(void *opaque)return ; } + width =dpy_gl_dmabuf_get_width (&primary->buf); + height =dpy_gl_dmabuf_get_height (&primary->buf); +if (dpy->dmabuf.primary != primary) { dpy->dmabuf.primary = primary; -qemu_console_resize (dpy->con, - primary->buf.width, primary->buf.height); +qemu_console_resize (dpy->con, width, height);dpy_gl_scanout_dmabuf (dpy->con, &primary->buf); free_bufs = true; }@@ -328,7 +331,7 @@ static void vfio_display_dmabuf_update(void *opaque) cursor->pos_updates =0 ; } -dpy_gl_update (dpy->con,0 ,0 , primary->buf.width, primary->buf.height); +dpy_gl_update (dpy->con,0 ,0 , width, height);if (free_bufs) {vfio_display_free_dmabufs (vdev);ui/console.c +18
-1@@ -1132,6 +1132,24 @@ void dpy_gl_cursor_position(QemuConsole *con, } } +uint32_t dpy_gl_dmabuf_get_width (QemuDmaBuf *dmabuf) +{ +if (dmabuf) { +return dmabuf->width; + } + +return 0 ; +} + +uint32_t dpy_gl_dmabuf_get_height (QemuDmaBuf *dmabuf) +{ +if (dmabuf) { +return dmabuf->height; + } + +return 0 ; +} +void dpy_gl_release_dmabuf (QemuConsole *con,QemuDmaBuf *dmabuf) { --
Quoted 3 lines
2.34.1
--
Marc-André Lureau
RE: [PATCH v4 1/3] ui/console: Introduce dpy_gl_dmabuf_get_height/width() helpers
Hi Marc-André,
Quoted 14 lines
-----Original Message-----
From: Marc-André Lureau <[email protected]>
Sent: Friday, March 22, 2024 2:06 AM
To: Kim, Dongwon <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v4 1/3] ui/console: Introduce dpy_gl_dmabuf_get_height/width() helpersHi Kim
Quoted 3 lines · On Fri, Mar 22, 2024 at 3:45 AM <[email protected]> wrote:
From: Dongwon Kim <[email protected]>
Quoted 7 lines
Quoted 3 lines
dpy_gl_dmabuf_get_height()anddpy_gl_dmabuf_get_width()are helpers for retrieving width and height fields from QemuDmaBuf struct.There are many places left where width/height fields are still accessed directly.
If we want to make the whole structure private, we will probably need setters.
[Kim, Dongwon] I am wondering if you are saying we need setters and getters for all individual fields and use those new functions in any places in QEMU where any of those fields are accessed including ui/* (e.g. gtk-egl.c)?
Quoted 3 lines
I don't see why the function should silently return 0 when given NULL. Imho an assert(dmabuf != NULL) is appropriate (or
g_return_val_if_fail).
[Kim, Dongwon] Yeah I can do that. I will update that part.
Quoted 131 lines
include/ui/console.h void dpy_gl_cursor_dmabuf(QemuConsole *con,
Quoted 21 lines
Cc Philippe Mathieu-Daudé <[email protected]>Cc Marc-André Lureau <[email protected]>Cc Vivek Kasireddy <[email protected]>Signed-off-by Dongwon Kim <[email protected]>--- include/ui/console.h | 2 ++ hw/display/virtio-gpu-udmabuf.c | 7 ++++--- hw/vfio/display.c | 9 ++++++--- ui/console.c | 18 ++++++++++++++++++ 4 files changed, 30 insertions(+), 6 deletions(-)include/ui/console.h index renamed from include/ui/console.h +0
-0
0bc7a00ac0..6064487fc4100644include/ui/console.h +0
-0@@ -358,6 +358,8 @@ void dpy_gl_cursor_dmabuf(QemuConsole *con,QemuDmaBuf *dmabuf,
hw/vfio/display.c static void vfio_display_dmabuf_update(void
Quoted 38 lines
include/ui/console.h +2
-0bool have_hot, uint32_t hot_x, uint32_t hot_y); void dpy_gl_cursor_position(QemuConsole *con,uint32_t pos_x,uint32_t pos_y); +uint32_t dpy_gl_dmabuf_get_width (QemuDmaBuf *dmabuf);uint32_t +dpy_gl_dmabuf_get_height (QemuDmaBuf *dmabuf);void dpy_gl_release_dmabuf (QemuConsole *con,QemuDmaBuf *dmabuf);void dpy_gl_update(QemuConsole *con, diff --git
a/hw/display/virtio-gpu-udmabuf.cb/hw/display/virtio-gpu-udmabuf.cindexd51184d658..a4ebf828ec100644hw/display/virtio-gpu-udmabuf.c +4
-3@@ -206,6 +206,7 @@ int virtio_gpu_update_dmabuf(VirtIOGPU *g, {struct virtio_gpu_scanout *scanout = &g->parent_obj.scanout[scanout_id];VGPUDMABuf *new_primary, *old_primary =NULL ; +uint32_t width, height; new_primary =virtio_gpu_create_dmabuf (g, scanout_id, res, fb, r);if (!new_primary) {@@ -216,10 +217,10 @@ int virtio_gpu_update_dmabuf(VirtIOGPU *g, old_primary = g->dmabuf.primary[scanout_id]; } + width =dpy_gl_dmabuf_get_width (&new_primary->buf); + height =dpy_gl_dmabuf_get_height (&new_primary->buf); g->dmabuf.primary[scanout_id] = new_primary; -qemu_console_resize (scanout->con, - new_primary->buf.width, - new_primary->buf.height); +qemu_console_resize (scanout->con, width, height);dpy_gl_scanout_dmabuf (scanout->con, &new_primary->buf);if (old_primary) {hw/vfio/display.c +0
-0@@ -286,6 +286,7 @@ static void vfio_display_dmabuf_update(void*opaque)
hw/vfio/display.c static void vfio_display_dmabuf_update(void
Quoted 8 lines
hw/vfio/display.c +1
-0VFIOPCIDevice *vdev = opaque;VFIODisplay *dpy = vdev->dpy;VFIODMABuf *primary, *cursor; +uint32_t width, height;bool free_bufs = false, new_cursor = false; primary =vfio_display_get_dmabuf (vdev,DRM_PLANE_TYPE_PRIMARY );@@ -296,10 +297,12 @@ static void vfio_display_dmabuf_update(void*opaque)
hw/vfio/display.c static void vfio_display_dmabuf_update(void
Quoted 15 lines
hw/vfio/display.c +4
-2return ; } + width =dpy_gl_dmabuf_get_width (&primary->buf); + height =dpy_gl_dmabuf_get_height (&primary->buf); +if (dpy->dmabuf.primary != primary) { dpy->dmabuf.primary = primary; -qemu_console_resize (dpy->con, - primary->buf.width, primary->buf.height); +qemu_console_resize (dpy->con, width, height);dpy_gl_scanout_dmabuf (dpy->con, &primary->buf); free_bufs = true; }@@ -328,7 +331,7 @@ static void vfio_display_dmabuf_update(void*opaque)
Quoted 38 lines
hw/vfio/display.c +1
-1cursor->pos_updates =0 ; } -dpy_gl_update (dpy->con,0 ,0 , primary->buf.width, primary->buf.height); +dpy_gl_update (dpy->con,0 ,0 , width, height);if (free_bufs) {vfio_display_free_dmabufs (vdev);ui/console.c +18
-1@@ -1132,6 +1132,24 @@ void dpy_gl_cursor_position(QemuConsole *con, } } +uint32_t dpy_gl_dmabuf_get_width (QemuDmaBuf *dmabuf) +{ +if (dmabuf) { +return dmabuf->width; + } + +return 0 ; +} + +uint32_t dpy_gl_dmabuf_get_height (QemuDmaBuf *dmabuf) +{ +if (dmabuf) { +return dmabuf->height; + } + +return 0 ; +} +void dpy_gl_release_dmabuf (QemuConsole *con,QemuDmaBuf *dmabuf) { --Quoted 3 lines
2.34.1
--
Marc-André Lureau
PATCH v4 2/3 ui/console: Introduce dpy_gl_dmabuf_get_fd() helper
In reply to [email protected]From: Dongwon Kim <[email protected]>
dpy_gl_dmabuf_get_fd() is a helper for retrieving FD of the dmabuf from QemuDmaBuf struct.
--- include/ui/console.h | 1 + hw/vfio/display.c | 8 +++++++- ui/console.c | 9 +++++++++ 3 files changed, 17 insertions(+), 1 deletion(-)
include/ui/console.h +1 -0
@@ -360,6 +360,7 @@ void dpy_gl_cursor_position(QemuConsole *con, uint32_t pos_x, uint32_t pos_y);uint32_t dpy_gl_dmabuf_get_width (QemuDmaBuf *dmabuf);uint32_t dpy_gl_dmabuf_get_height (QemuDmaBuf *dmabuf); +int32_t dpy_gl_dmabuf_get_fd (QemuDmaBuf *dmabuf);void dpy_gl_release_dmabuf (QemuConsole *con,QemuDmaBuf *dmabuf); void dpy_gl_update(QemuConsole *con,
hw/vfio/display.c +7 -1
@@ -259,9 +259,15 @@ static VFIODMABuf *vfio_display_get_dmabuf(VFIOPCIDevice *vdev,static void vfio_display_free_one_dmabuf (VFIODisplay *dpy,VFIODMABuf *dmabuf) { +int fd; +QTAILQ_REMOVE (&dpy->dmabuf.bufs, dmabuf, next); + fd =dpy_gl_dmabuf_get_fd (&dmabuf->buf); +if (fd >-1 ) { +close (fd); + } +dpy_gl_release_dmabuf (dpy->con, &dmabuf->buf); -close (dmabuf->buf.fd);g_free (dmabuf); }
ui/console.c +9 -0
@@ -1504,6 +1504,15 @@ int qemu_console_get_height(QemuConsole *con, int fallback) } } +int32_t dpy_gl_dmabuf_get_fd (QemuDmaBuf *dmabuf) +{ +if (dmabuf) { +return dmabuf->fd; + } + +return -1 ; +} +int qemu_invalidate_text_consoles (void ) {QemuConsole *s;
Signature · 1 line
2.34.1
Re: [PATCH v4 2/3] ui/console: Introduce dpy_gl_dmabuf_get_fd() helper
Hi
Quoted 6 lines · On Fri, Mar 22, 2024 at 3:45 AM <[email protected]> wrote:
From: Dongwon Kim <[email protected]>
dpy_gl_dmabuf_get_fd()is a helper for retrieving FD of the dmabuf from QemuDmaBuf struct.
Same remarks as previous patch regarding exhaustivity, setter, accepting NULL argument etc.
Quoted 27 lines
Cc Philippe Mathieu-Daudé <[email protected]>Cc Marc-André Lureau <[email protected]>Cc Vivek Kasireddy <[email protected]>Signed-off-by Dongwon Kim <[email protected]>--- include/ui/console.h | 1 + hw/vfio/display.c | 8 +++++++- ui/console.c | 9 +++++++++ 3 files changed, 17 insertions(+), 1 deletion(-)include/ui/console.h +1
-0@@ -360,6 +360,7 @@ void dpy_gl_cursor_position(QemuConsole *con, uint32_t pos_x, uint32_t pos_y);uint32_t dpy_gl_dmabuf_get_width (QemuDmaBuf *dmabuf);uint32_t dpy_gl_dmabuf_get_height (QemuDmaBuf *dmabuf); +int32_t dpy_gl_dmabuf_get_fd (QemuDmaBuf *dmabuf);void dpy_gl_release_dmabuf (QemuConsole *con,QemuDmaBuf *dmabuf); void dpy_gl_update(QemuConsole *con,hw/vfio/display.c +0
-0
hw/vfio/display.c static VFIODMABuf *vfio_display_get_dmabuf(VFIOPCIDevice *vdev, · line 268
Quoted 10 lines
hw/vfio/display.c +6
-0@@ -259,9 +259,15 @@ static VFIODMABuf *vfio_display_get_dmabuf(VFIOPCIDevice *vdev,static void vfio_display_free_one_dmabuf (VFIODisplay *dpy,VFIODMABuf *dmabuf) { +int fd; +QTAILQ_REMOVE (&dpy->dmabuf.bufs, dmabuf, next); + fd =dpy_gl_dmabuf_get_fd (&dmabuf->buf); +if (fd >-1 ) { +close (fd); + }
The current code assumes that fd is valid. Why should we change that?
Quoted 28 lines
hw/vfio/display.c +1
-1+dpy_gl_release_dmabuf (dpy->con, &dmabuf->buf); -close (dmabuf->buf.fd);g_free (dmabuf); }ui/console.c +9
-1@@ -1504,6 +1504,15 @@ int qemu_console_get_height(QemuConsole *con, int fallback) } } +int32_t dpy_gl_dmabuf_get_fd (QemuDmaBuf *dmabuf) +{ +if (dmabuf) { +return dmabuf->fd; + } + +return -1 ; +} +int qemu_invalidate_text_consoles (void ) {QemuConsole *s; --
Quoted 3 lines
2.34.1
--
Marc-André Lureau
PATCH v4 3/3 ui/console: Introduce dpy_gl_create_dmabuf() helper
In reply to [email protected]From: Dongwon Kim <[email protected]>
dpy_gl_create_dmabuf() allocates QemuDmaBuf and initialize fields. hw/display modules, hw/vfio and ui/dbus-listener now use this method to create QemuDmaBuf instead of declaring and initializing it on their own.
--- include/hw/vfio/vfio-common.h | 2 +- include/hw/virtio/virtio-gpu.h | 4 ++-- include/ui/console.h | 6 ++++++ hw/display/vhost-user-gpu.c | 33 ++++++++++++++++++--------------- hw/display/virtio-gpu-udmabuf.c | 23 ++++++++--------------- hw/vfio/display.c | 26 +++++++++++--------------- ui/console.c | 28 ++++++++++++++++++++++++++++ ui/dbus-listener.c | 28 ++++++++++++---------------- 8 files changed, 86 insertions(+), 64 deletions(-)
include/hw/vfio/vfio-common.h +1 -1
@@ -148,7 +148,7 @@ typedef struct VFIOGroup { } VFIOGroup; typedef struct VFIODMABuf { - QemuDmaBuf buf; + QemuDmaBuf *buf; uint32_t pos_x, pos_y, pos_updates; uint32_t hot_x, hot_y, hot_updates; int dmabuf_id;
include/hw/virtio/virtio-gpu.h +2 -2
@@ -169,7 +169,7 @@ struct VirtIOGPUBaseClass { DEFINE_PROP_UINT32("yres", _state, _conf.yres, 800)typedef struct VGPUDMABuf { -QemuDmaBuf buf; +QemuDmaBuf *buf;uint32_t scanout_id;QTAILQ_ENTRY (VGPUDMABuf) next; }VGPUDMABuf ;
@@ -238,7 +238,7 @@ struct VhostUserGPU {VhostUserBackend *vhost;int vhost_gpu_fd;/* closed by the chardev */ CharBackend vhost_chr; -QemuDmaBuf dmabuf[VIRTIO_GPU_MAX_SCANOUTS ]; +QemuDmaBuf *dmabuf[VIRTIO_GPU_MAX_SCANOUTS ];bool backend_blocked; };
include/ui/console.h +6 -0
@@ -358,6 +358,12 @@ void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf, bool have_hot, uint32_t hot_x, uint32_t hot_y);void dpy_gl_cursor_position (QemuConsole *con,uint32_t pos_x,uint32_t pos_y); +QemuDmaBuf *dpy_gl_create_dmabuf (uint32_t width,uint32_t height, +uint32_t stride,uint32_t x, +uint32_t y,uint32_t backing_width, +uint32_t backing_height,uint32_t fourcc, +uint64_t modifier,uint32_t dmabuf_fd, +bool allow_fences,bool y0_top);uint32_t dpy_gl_dmabuf_get_width (QemuDmaBuf *dmabuf);uint32_t dpy_gl_dmabuf_get_height (QemuDmaBuf *dmabuf);int32_t dpy_gl_dmabuf_get_fd (QemuDmaBuf *dmabuf);
hw/display/vhost-user-gpu.c +18 -15
@@ -249,6 +249,7 @@ vhost_user_gpu_handle_display(VhostUserGPU *g, VhostUserGpuMsg *msg)case VHOST_USER_GPU_DMABUF_SCANOUT : {VhostUserGpuDMABUFScanout *m = &msg->payload.dmabuf_scanout;int fd =qemu_chr_fe_get_msgfd (&g->vhost_chr); +uint64_t modifier =0 ;QemuDmaBuf *dmabuf;if (m->scanout_id >= g->parent_obj.conf.max_outputs) {
@@ -261,30 +262,32 @@ vhost_user_gpu_handle_display(VhostUserGPU *g, VhostUserGpuMsg *msg) g->parent_obj.enable =1 ; con = g->parent_obj.scanout[m->scanout_id].con; - dmabuf = &g->dmabuf[m->scanout_id]; -if (dmabuf->fd >=0 ) { -close (dmabuf->fd); - dmabuf->fd =-1 ; + dmabuf = g->dmabuf[m->scanout_id]; +if (dmabuf) { +int dmabuf_fd =dpy_gl_dmabuf_get_fd (dmabuf); +if (dmabuf_fd >=0 ) { +close (dmabuf_fd); + } +dpy_gl_release_dmabuf (con, dmabuf); } -dpy_gl_release_dmabuf (con, dmabuf); +if (fd ==-1 ) {dpy_gl_scanout_disable (con);break ; } - *dmabuf = (QemuDmaBuf ) { - .fd = fd, - .width = m->fd_width, - .height = m->fd_height, - .stride = m->fd_stride, - .fourcc = m->fd_drm_fourcc, - .y0_top = m->fd_flags &VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP , - }; +if (msg->request ==VHOST_USER_GPU_DMABUF_SCANOUT2 ) {VhostUserGpuDMABUFScanout2 *m2 = &msg->payload.dmabuf_scanout2; - dmabuf->modifier = m2->modifier; + modifier = m2->modifier; } -dpy_gl_scanout_dmabuf (con, dmabuf); + dmabuf =dpy_gl_create_dmabuf (m->fd_width, m->fd_height, m->fd_stride, +0 ,0 ,0 ,0 , m->fd_drm_fourcc, modifier, + fd, false, m->fd_flags & +VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP ); + +dpy_gl_scanout_dmabuf (con, g->dmabuf[m->scanout_id]); + g->dmabuf[m->scanout_id] = dmabuf;break ; }case VHOST_USER_GPU_DMABUF_UPDATE : {
hw/display/virtio-gpu-udmabuf.c +8 -15
@@ -162,7 +162,7 @@ static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf)struct virtio_gpu_scanout *scanout; scanout = &g->parent_obj.scanout[dmabuf->scanout_id]; -dpy_gl_release_dmabuf (scanout->con, &dmabuf->buf); +dpy_gl_release_dmabuf (scanout->con, dmabuf->buf);QTAILQ_REMOVE (&g->dmabuf.bufs, dmabuf, next);g_free (dmabuf); }
@@ -181,17 +181,10 @@ static VGPUDMABuf } dmabuf =g_new0 (VGPUDMABuf,1 ); - dmabuf->buf.width = r->width; - dmabuf->buf.height = r->height; - dmabuf->buf.stride = fb->stride; - dmabuf->buf.x = r->x; - dmabuf->buf.y = r->y; - dmabuf->buf.backing_width = fb->width; - dmabuf->buf.backing_height = fb->height; - dmabuf->buf.fourcc =qemu_pixman_to_drm_format (fb->format); - dmabuf->buf.fd = res->dmabuf_fd; - dmabuf->buf.allow_fences = true; - dmabuf->buf.draw_submitted = false; + dmabuf->buf =dpy_gl_create_dmabuf (r->width, r->height, fb->stride, + r->x, r->y, fb->width, fb->height, +qemu_pixman_to_drm_format (fb->format), +0 , res->dmabuf_fd, false,0 ); dmabuf->scanout_id = scanout_id;QTAILQ_INSERT_HEAD (&g->dmabuf.bufs, dmabuf, next);
@@ -217,11 +210,11 @@ int virtio_gpu_update_dmabuf(VirtIOGPU *g, old_primary = g->dmabuf.primary[scanout_id]; } - width =dpy_gl_dmabuf_get_width (&new_primary->buf); - height =dpy_gl_dmabuf_get_height (&new_primary->buf); + width =dpy_gl_dmabuf_get_width (new_primary->buf); + height =dpy_gl_dmabuf_get_height (new_primary->buf); g->dmabuf.primary[scanout_id] = new_primary;qemu_console_resize (scanout->con, width, height); -dpy_gl_scanout_dmabuf (scanout->con, &new_primary->buf); +dpy_gl_scanout_dmabuf (scanout->con, new_primary->buf);if (old_primary) {virtio_gpu_free_dmabuf (g, old_primary);
hw/vfio/display.c +11 -15
@@ -240,15 +240,11 @@ static VFIODMABuf *vfio_display_get_dmabuf(VFIOPCIDevice *vdev, } dmabuf =g_new0 (VFIODMABuf,1 ); - dmabuf->dmabuf_id = plane.dmabuf_id; - dmabuf->buf.width = plane.width; - dmabuf->buf.height = plane.height; - dmabuf->buf.backing_width = plane.width; - dmabuf->buf.backing_height = plane.height; - dmabuf->buf.stride = plane.stride; - dmabuf->buf.fourcc = plane.drm_format; - dmabuf->buf.modifier = plane.drm_format_mod; - dmabuf->buf.fd = fd; + dmabuf->dmabuf_id = plane.dmabuf_id; + dmabuf->buf =dpy_gl_create_dmabuf (plane.width, plane.height, plane.stride, +0 ,0 , plane.width, plane.height, + plane.drm_format, plane.drm_format_mod, + fd, false,0 );if (plane_type ==DRM_PLANE_TYPE_CURSOR ) {vfio_display_update_cursor (dmabuf, &plane); }
@@ -262,12 +258,12 @@ static void vfio_display_free_one_dmabuf(VFIODisplay *dpy, VFIODMABuf *dmabuf)int fd;QTAILQ_REMOVE (&dpy->dmabuf.bufs, dmabuf, next); - fd =dpy_gl_dmabuf_get_fd (&dmabuf->buf); + fd =dpy_gl_dmabuf_get_fd (dmabuf->buf);if (fd >-1 ) {close (fd); } -dpy_gl_release_dmabuf (dpy->con, &dmabuf->buf); +dpy_gl_release_dmabuf (dpy->con, dmabuf->buf);g_free (dmabuf); }
@@ -303,13 +299,13 @@ static void vfio_display_dmabuf_update(void *opaque)return ; } - width =dpy_gl_dmabuf_get_width (&primary->buf); - height =dpy_gl_dmabuf_get_height (&primary->buf); + width =dpy_gl_dmabuf_get_width (primary->buf); + height =dpy_gl_dmabuf_get_height (primary->buf);if (dpy->dmabuf.primary != primary) { dpy->dmabuf.primary = primary;qemu_console_resize (dpy->con, width, height); -dpy_gl_scanout_dmabuf (dpy->con, &primary->buf); +dpy_gl_scanout_dmabuf (dpy->con, primary->buf); free_bufs = true; }
@@ -323,7 +319,7 @@ static void vfio_display_dmabuf_update(void *opaque)if (cursor && (new_cursor || cursor->hot_updates)) {bool have_hot = (cursor->hot_x !=0xffffffff && cursor->hot_y !=0xffffffff ); -dpy_gl_cursor_dmabuf (dpy->con, &cursor->buf, have_hot, +dpy_gl_cursor_dmabuf (dpy->con, cursor->buf, have_hot, cursor->hot_x, cursor->hot_y); cursor->hot_updates =0 ; }else if (!cursor && new_cursor) {
ui/console.c +28 -0
@@ -1132,6 +1132,33 @@ void dpy_gl_cursor_position(QemuConsole *con, } } +QemuDmaBuf *dpy_gl_create_dmabuf (uint32_t width,uint32_t height, +uint32_t stride,uint32_t x, +uint32_t y,uint32_t backing_width, +uint32_t backing_height,uint32_t fourcc, +uint64_t modifier,uint32_t dmabuf_fd, +bool allow_fences,bool y0_top) { +QemuDmaBuf *dmabuf; + + dmabuf =g_new0 (QemuDmaBuf,1 ); + + dmabuf->width = width; + dmabuf->height = height; + dmabuf->stride = stride; + dmabuf->x = x; + dmabuf->y = y; + dmabuf->backing_width = backing_width; + dmabuf->backing_height = backing_height; + dmabuf->fourcc = fourcc; + dmabuf->modifier = modifier; + dmabuf->fd = dmabuf_fd; + dmabuf->allow_fences = allow_fences; + dmabuf->y0_top = y0_top; + dmabuf->fence_fd =-1 ; + +return dmabuf; +} +uint32_t dpy_gl_dmabuf_get_width (QemuDmaBuf *dmabuf) {if (dmabuf) {
@@ -1164,6 +1191,7 @@ void dpy_gl_release_dmabuf(QemuConsole *con, dcl->ops->dpy_gl_release_dmabuf (dcl, dmabuf); } } +g_free (dmabuf); } void dpy_gl_update(QemuConsole *con,
ui/dbus-listener.c +12 -16
@@ -438,28 +438,24 @@ static void dbus_scanout_texture(DisplayChangeListener *dcl,trace_dbus_scanout_texture (tex_id, backing_y_0_top, backing_width, backing_height, x, y, w, h);#ifdef CONFIG_GBM -QemuDmaBuf dmabuf = { - .width = w, - .height = h, - .y0_top = backing_y_0_top, - .x = x, - .y = y, - .backing_width = backing_width, - .backing_height = backing_height, - }; +int32_t fd; +uint32_t stride, fourcc; +uint64_t modifier; +QemuDmaBuf *dmabuf;assert (tex_id); - dmabuf.fd =egl_get_fd_for_texture ( - tex_id, (EGLint *)&dmabuf.stride, - (EGLint *)&dmabuf.fourcc, - &dmabuf.modifier); -if (dmabuf.fd <0 ) { + fd =egl_get_fd_for_texture (tex_id, (EGLint *)&stride, (EGLint *)&fourcc, + &modifier); +if (fd <0 ) {error_report ("%s: failed to get fd for texture" , __func__);return ; } + dmabuf =dpy_gl_create_dmabuf (w, h, stride, x, y, backing_width, + backing_height, fourcc, modifier, fd, + false, backing_y_0_top); -dbus_scanout_dmabuf (dcl, &dmabuf); -close (dmabuf.fd); +dbus_scanout_dmabuf (dcl, dmabuf); +close (fd);#endif #ifdef WIN32
Signature · 1 line
2.34.1
Re: [PATCH v4 3/3] ui/console: Introduce dpy_gl_create_dmabuf() helper
Hi
Quoted 256 lines · On Fri, Mar 22, 2024 at 3:45 AM <[email protected]> wrote:
From: Dongwon Kim <[email protected]>
dpy_gl_create_dmabuf()allocates QemuDmaBuf and initialize fields. hw/display modules, hw/vfio and ui/dbus-listener now use this method to create QemuDmaBuf instead of declaring and initializing it on their own.Cc Philippe Mathieu-Daudé <[email protected]>Cc Marc-André Lureau <[email protected]>Cc Vivek Kasireddy <[email protected]>Signed-off-by Dongwon Kim <[email protected]>--- include/hw/vfio/vfio-common.h | 2 +- include/hw/virtio/virtio-gpu.h | 4 ++-- include/ui/console.h | 6 ++++++ hw/display/vhost-user-gpu.c | 33 ++++++++++++++++++--------------- hw/display/virtio-gpu-udmabuf.c | 23 ++++++++--------------- hw/vfio/display.c | 26 +++++++++++--------------- ui/console.c | 28 ++++++++++++++++++++++++++++ ui/dbus-listener.c | 28 ++++++++++++---------------- 8 files changed, 86 insertions(+), 64 deletions(-)include/hw/vfio/vfio-common.h +1
-1@@ -148,7 +148,7 @@ typedef struct VFIOGroup { } VFIOGroup; typedef struct VFIODMABuf { - QemuDmaBuf buf; + QemuDmaBuf *buf; uint32_t pos_x, pos_y, pos_updates; uint32_t hot_x, hot_y, hot_updates; int dmabuf_id;include/hw/virtio/virtio-gpu.h +2
-2@@ -169,7 +169,7 @@ struct VirtIOGPUBaseClass { DEFINE_PROP_UINT32("yres", _state, _conf.yres, 800)typedef struct VGPUDMABuf { -QemuDmaBuf buf; +QemuDmaBuf *buf;uint32_t scanout_id;QTAILQ_ENTRY (VGPUDMABuf) next; }VGPUDMABuf ;@@ -238,7 +238,7 @@ struct VhostUserGPU {VhostUserBackend *vhost;int vhost_gpu_fd;/* closed by the chardev */ CharBackend vhost_chr; -QemuDmaBuf dmabuf[VIRTIO_GPU_MAX_SCANOUTS ]; +QemuDmaBuf *dmabuf[VIRTIO_GPU_MAX_SCANOUTS ];bool backend_blocked; };include/ui/console.h +6
-0@@ -358,6 +358,12 @@ void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf, bool have_hot, uint32_t hot_x, uint32_t hot_y);void dpy_gl_cursor_position (QemuConsole *con,uint32_t pos_x,uint32_t pos_y); +QemuDmaBuf *dpy_gl_create_dmabuf (uint32_t width,uint32_t height, +uint32_t stride,uint32_t x, +uint32_t y,uint32_t backing_width, +uint32_t backing_height,uint32_t fourcc, +uint64_t modifier,uint32_t dmabuf_fd, +bool allow_fences,bool y0_top);uint32_t dpy_gl_dmabuf_get_width (QemuDmaBuf *dmabuf);uint32_t dpy_gl_dmabuf_get_height (QemuDmaBuf *dmabuf);int32_t dpy_gl_dmabuf_get_fd (QemuDmaBuf *dmabuf);hw/display/vhost-user-gpu.c +18
-15@@ -249,6 +249,7 @@ vhost_user_gpu_handle_display(VhostUserGPU *g, VhostUserGpuMsg *msg)case VHOST_USER_GPU_DMABUF_SCANOUT : {VhostUserGpuDMABUFScanout *m = &msg->payload.dmabuf_scanout;int fd =qemu_chr_fe_get_msgfd (&g->vhost_chr); +uint64_t modifier =0 ;QemuDmaBuf *dmabuf;if (m->scanout_id >= g->parent_obj.conf.max_outputs) {@@ -261,30 +262,32 @@ vhost_user_gpu_handle_display(VhostUserGPU *g, VhostUserGpuMsg *msg) g->parent_obj.enable =1 ; con = g->parent_obj.scanout[m->scanout_id].con; - dmabuf = &g->dmabuf[m->scanout_id]; -if (dmabuf->fd >=0 ) { -close (dmabuf->fd); - dmabuf->fd =-1 ; + dmabuf = g->dmabuf[m->scanout_id]; +if (dmabuf) { +int dmabuf_fd =dpy_gl_dmabuf_get_fd (dmabuf); +if (dmabuf_fd >=0 ) { +close (dmabuf_fd); + } +dpy_gl_release_dmabuf (con, dmabuf); } -dpy_gl_release_dmabuf (con, dmabuf); +if (fd ==-1 ) {dpy_gl_scanout_disable (con);break ; } - *dmabuf = (QemuDmaBuf ) { - .fd = fd, - .width = m->fd_width, - .height = m->fd_height, - .stride = m->fd_stride, - .fourcc = m->fd_drm_fourcc, - .y0_top = m->fd_flags &VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP , - }; +if (msg->request ==VHOST_USER_GPU_DMABUF_SCANOUT2 ) {VhostUserGpuDMABUFScanout2 *m2 = &msg->payload.dmabuf_scanout2; - dmabuf->modifier = m2->modifier; + modifier = m2->modifier; } -dpy_gl_scanout_dmabuf (con, dmabuf); + dmabuf =dpy_gl_create_dmabuf (m->fd_width, m->fd_height, m->fd_stride, +0 ,0 ,0 ,0 , m->fd_drm_fourcc, modifier, + fd, false, m->fd_flags & +VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP ); + +dpy_gl_scanout_dmabuf (con, g->dmabuf[m->scanout_id]); + g->dmabuf[m->scanout_id] = dmabuf;break ; }case VHOST_USER_GPU_DMABUF_UPDATE : {hw/display/virtio-gpu-udmabuf.c +8
-15@@ -162,7 +162,7 @@ static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf)struct virtio_gpu_scanout *scanout; scanout = &g->parent_obj.scanout[dmabuf->scanout_id]; -dpy_gl_release_dmabuf (scanout->con, &dmabuf->buf); +dpy_gl_release_dmabuf (scanout->con, dmabuf->buf);QTAILQ_REMOVE (&g->dmabuf.bufs, dmabuf, next);g_free (dmabuf); }@@ -181,17 +181,10 @@ static VGPUDMABuf } dmabuf =g_new0 (VGPUDMABuf,1 ); - dmabuf->buf.width = r->width; - dmabuf->buf.height = r->height; - dmabuf->buf.stride = fb->stride; - dmabuf->buf.x = r->x; - dmabuf->buf.y = r->y; - dmabuf->buf.backing_width = fb->width; - dmabuf->buf.backing_height = fb->height; - dmabuf->buf.fourcc =qemu_pixman_to_drm_format (fb->format); - dmabuf->buf.fd = res->dmabuf_fd; - dmabuf->buf.allow_fences = true; - dmabuf->buf.draw_submitted = false; + dmabuf->buf =dpy_gl_create_dmabuf (r->width, r->height, fb->stride, + r->x, r->y, fb->width, fb->height, +qemu_pixman_to_drm_format (fb->format), +0 , res->dmabuf_fd, false,0 ); dmabuf->scanout_id = scanout_id;QTAILQ_INSERT_HEAD (&g->dmabuf.bufs, dmabuf, next);@@ -217,11 +210,11 @@ int virtio_gpu_update_dmabuf(VirtIOGPU *g, old_primary = g->dmabuf.primary[scanout_id]; } - width =dpy_gl_dmabuf_get_width (&new_primary->buf); - height =dpy_gl_dmabuf_get_height (&new_primary->buf); + width =dpy_gl_dmabuf_get_width (new_primary->buf); + height =dpy_gl_dmabuf_get_height (new_primary->buf); g->dmabuf.primary[scanout_id] = new_primary;qemu_console_resize (scanout->con, width, height); -dpy_gl_scanout_dmabuf (scanout->con, &new_primary->buf); +dpy_gl_scanout_dmabuf (scanout->con, new_primary->buf);if (old_primary) {virtio_gpu_free_dmabuf (g, old_primary);hw/vfio/display.c +11
-15@@ -240,15 +240,11 @@ static VFIODMABuf *vfio_display_get_dmabuf(VFIOPCIDevice *vdev, } dmabuf =g_new0 (VFIODMABuf,1 ); - dmabuf->dmabuf_id = plane.dmabuf_id; - dmabuf->buf.width = plane.width; - dmabuf->buf.height = plane.height; - dmabuf->buf.backing_width = plane.width; - dmabuf->buf.backing_height = plane.height; - dmabuf->buf.stride = plane.stride; - dmabuf->buf.fourcc = plane.drm_format; - dmabuf->buf.modifier = plane.drm_format_mod; - dmabuf->buf.fd = fd; + dmabuf->dmabuf_id = plane.dmabuf_id; + dmabuf->buf =dpy_gl_create_dmabuf (plane.width, plane.height, plane.stride, +0 ,0 , plane.width, plane.height, + plane.drm_format, plane.drm_format_mod, + fd, false,0 );if (plane_type ==DRM_PLANE_TYPE_CURSOR ) {vfio_display_update_cursor (dmabuf, &plane); }@@ -262,12 +258,12 @@ static void vfio_display_free_one_dmabuf(VFIODisplay *dpy, VFIODMABuf *dmabuf)int fd;QTAILQ_REMOVE (&dpy->dmabuf.bufs, dmabuf, next); - fd =dpy_gl_dmabuf_get_fd (&dmabuf->buf); + fd =dpy_gl_dmabuf_get_fd (dmabuf->buf);if (fd >-1 ) {close (fd); } -dpy_gl_release_dmabuf (dpy->con, &dmabuf->buf); +dpy_gl_release_dmabuf (dpy->con, dmabuf->buf);g_free (dmabuf); }@@ -303,13 +299,13 @@ static void vfio_display_dmabuf_update(void *opaque)return ; } - width =dpy_gl_dmabuf_get_width (&primary->buf); - height =dpy_gl_dmabuf_get_height (&primary->buf); + width =dpy_gl_dmabuf_get_width (primary->buf); + height =dpy_gl_dmabuf_get_height (primary->buf);if (dpy->dmabuf.primary != primary) { dpy->dmabuf.primary = primary;qemu_console_resize (dpy->con, width, height); -dpy_gl_scanout_dmabuf (dpy->con, &primary->buf); +dpy_gl_scanout_dmabuf (dpy->con, primary->buf); free_bufs = true; }@@ -323,7 +319,7 @@ static void vfio_display_dmabuf_update(void *opaque)if (cursor && (new_cursor || cursor->hot_updates)) {bool have_hot = (cursor->hot_x !=0xffffffff && cursor->hot_y !=0xffffffff ); -dpy_gl_cursor_dmabuf (dpy->con, &cursor->buf, have_hot, +dpy_gl_cursor_dmabuf (dpy->con, cursor->buf, have_hot, cursor->hot_x, cursor->hot_y); cursor->hot_updates =0 ; }else if (!cursor && new_cursor) {ui/console.c +0
-0
ui/console.c void dpy_gl_cursor_position(QemuConsole *con, · line 1140
Quoted 9 lines
ui/console.c +6
-0@@ -1132,6 +1132,33 @@ void dpy_gl_cursor_position(QemuConsole *con, } } +QemuDmaBuf *dpy_gl_create_dmabuf (uint32_t width,uint32_t height, +uint32_t stride,uint32_t x, +uint32_t y,uint32_t backing_width, +uint32_t backing_height,uint32_t fourcc, +uint64_t modifier,uint32_t dmabuf_fd, +bool allow_fences,bool y0_top) {
We usually follow glib conventions, name the function according to the type allocated, and add a "_new" suffix: qemu_dma_buf_new()
Quoted 25 lines
ui/console.c +21
-0+QemuDmaBuf *dmabuf; + + dmabuf =g_new0 (QemuDmaBuf,1 ); + + dmabuf->width = width; + dmabuf->height = height; + dmabuf->stride = stride; + dmabuf->x = x; + dmabuf->y = y; + dmabuf->backing_width = backing_width; + dmabuf->backing_height = backing_height; + dmabuf->fourcc = fourcc; + dmabuf->modifier = modifier; + dmabuf->fd = dmabuf_fd; + dmabuf->allow_fences = allow_fences; + dmabuf->y0_top = y0_top; + dmabuf->fence_fd =-1 ; + +return dmabuf; +} +uint32_t dpy_gl_dmabuf_get_width (QemuDmaBuf *dmabuf) {if (dmabuf) {
ui/console.c void dpy_gl_release_dmabuf(QemuConsole *con, · line 1194
Quoted 4 lines
ui/console.c +1
-0@@ -1164,6 +1191,7 @@ void dpy_gl_release_dmabuf(QemuConsole *con, dcl->ops->dpy_gl_release_dmabuf (dcl, dmabuf); } } +g_free (dmabuf);
This is not a good place to free dmabuf, since it's a place to notify console listeners. I suggest introducing another function, qemu_dma_buf_free(). And to use it through g_clear_pointer(&buf, qemu_dma_buf_free), to avoid dangling pointers later.
Quoted 52 lines
ui/console.c +0
-0} void dpy_gl_update(QemuConsole *con,ui/dbus-listener.c +12
-17@@ -438,28 +438,24 @@ static void dbus_scanout_texture(DisplayChangeListener *dcl,trace_dbus_scanout_texture (tex_id, backing_y_0_top, backing_width, backing_height, x, y, w, h);#ifdef CONFIG_GBM -QemuDmaBuf dmabuf = { - .width = w, - .height = h, - .y0_top = backing_y_0_top, - .x = x, - .y = y, - .backing_width = backing_width, - .backing_height = backing_height, - }; +int32_t fd; +uint32_t stride, fourcc; +uint64_t modifier; +QemuDmaBuf *dmabuf;assert (tex_id); - dmabuf.fd =egl_get_fd_for_texture ( - tex_id, (EGLint *)&dmabuf.stride, - (EGLint *)&dmabuf.fourcc, - &dmabuf.modifier); -if (dmabuf.fd <0 ) { + fd =egl_get_fd_for_texture (tex_id, (EGLint *)&stride, (EGLint *)&fourcc, + &modifier); +if (fd <0 ) {error_report ("%s: failed to get fd for texture" , __func__);return ; } + dmabuf =dpy_gl_create_dmabuf (w, h, stride, x, y, backing_width, + backing_height, fourcc, modifier, fd, + false, backing_y_0_top); -dbus_scanout_dmabuf (dcl, &dmabuf); -close (dmabuf.fd); +dbus_scanout_dmabuf (dcl, dmabuf); +close (fd);#endif #ifdef WIN32 --2.34.1
Signature · 1 line
Marc-André Lureau
RE: [PATCH v4 3/3] ui/console: Introduce dpy_gl_create_dmabuf() helper
Hi Marc-André,
Quoted 280 lines
-----Original Message-----
From: Marc-André Lureau <[email protected]>
Sent: Friday, March 22, 2024 2:06 AM
To: Kim, Dongwon <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v4 3/3] ui/console: Introducedpy_gl_create_dmabuf()helperHi
include/ui/console.h void dpy_gl_cursor_dmabuf(QemuConsole *con,
Quoted 62 lines · On Fri, Mar 22, 2024 at 3:45 AM <[email protected]> wrote:
From: Dongwon Kim <[email protected]>
dpy_gl_create_dmabuf()allocates QemuDmaBuf and initialize fields. hw/display modules, hw/vfio and ui/dbus-listener now use this method to create QemuDmaBuf instead of declaring and initializing it on their own.Cc Philippe Mathieu-Daudé <[email protected]>Cc Marc-André Lureau <[email protected]>Cc Vivek Kasireddy <[email protected]>Signed-off-by Dongwon Kim <[email protected]>--- include/hw/vfio/vfio-common.h | 2 +- include/hw/virtio/virtio-gpu.h | 4 ++-- include/ui/console.h | 6 ++++++ hw/display/vhost-user-gpu.c | 33 ++++++++++++++++++--------------- hw/display/virtio-gpu-udmabuf.c | 23 ++++++++--------------- hw/vfio/display.c | 26 +++++++++++--------------- ui/console.c | 28 ++++++++++++++++++++++++++++ ui/dbus-listener.c | 28 ++++++++++++---------------- 8 files changed, 86 insertions(+), 64 deletions(-)(quoted diff) +0
-0
b/include/hw/vfio/vfio-common.hindexb9da6c08ef..d66e27db02100644include/hw/vfio/vfio-common.h +1
-1@@ -148,7 +148,7 @@ typedef struct VFIOGroup { } VFIOGroup; typedef struct VFIODMABuf { - QemuDmaBuf buf; + QemuDmaBuf *buf; uint32_t pos_x, pos_y, pos_updates; uint32_t hot_x, hot_y, hot_updates; int dmabuf_id;(quoted diff) +0
-0
b/include/hw/virtio/virtio-gpu.hindexed44cdad6b..56d6e821bf100644include/hw/virtio/virtio-gpu.h +2
-2@@ -169,7 +169,7 @@ struct VirtIOGPUBaseClass { DEFINE_PROP_UINT32("yres", _state, _conf.yres, 800)typedef struct VGPUDMABuf { -QemuDmaBuf buf; +QemuDmaBuf *buf;uint32_t scanout_id;QTAILQ_ENTRY (VGPUDMABuf) next; }VGPUDMABuf ;@@ -238,7 +238,7 @@ struct VhostUserGPU {VhostUserBackend *vhost;int vhost_gpu_fd;/* closed by the chardev */ CharBackend vhost_chr; -QemuDmaBuf dmabuf[VIRTIO_GPU_MAX_SCANOUTS ]; +QemuDmaBuf *dmabuf[VIRTIO_GPU_MAX_SCANOUTS ];bool backend_blocked; };include/ui/console.h index renamed from include/ui/console.h +0
-0
d5334a806c..01e998264b100644include/ui/console.h +0
-0@@ -358,6 +358,12 @@ void dpy_gl_cursor_dmabuf(QemuConsole *con,QemuDmaBuf *dmabuf,
hw/display/vhost-user-gpu.c vhost_user_gpu_handle_display(VhostUserGPU *g,
Quoted 17 lines
include/ui/console.h +6
-0bool have_hot, uint32_t hot_x, uint32_t hot_y); void dpy_gl_cursor_position(QemuConsole *con,uint32_t pos_x,uint32_t pos_y); +QemuDmaBuf *dpy_gl_create_dmabuf (uint32_t width,uint32_t height, +uint32_t stride,uint32_t x, +uint32_t y,uint32_t backing_width, +uint32_t backing_height,uint32_t fourcc, +uint64_t modifier,uint32_t dmabuf_fd, +bool allow_fences,bool y0_top);uint32_t dpy_gl_dmabuf_get_width (QemuDmaBuf *dmabuf);uint32_t
dpy_gl_dmabuf_get_height(QemuDmaBuf *dmabuf);int32_tdpy_gl_dmabuf_get_fd(QemuDmaBuf *dmabuf); diff --gita/hw/display/vhost-user-gpu.cb/hw/display/vhost-user-gpu.cindex709c8a02a1..0e49a934ed100644hw/display/vhost-user-gpu.c +0
-0@@ -249,6 +249,7 @@ vhost_user_gpu_handle_display(VhostUserGPU *g,VhostUserGpuMsg *msg)
hw/display/virtio-gpu-udmabuf.c static void virtio_gpu_free_dmabuf(VirtIOGPU *g,
Quoted 64 lines
hw/display/vhost-user-gpu.c +1
-1case VHOST_USER_GPU_DMABUF_SCANOUT : {VhostUserGpuDMABUFScanout *m = &msg->payload.dmabuf_scanout;int fd =qemu_chr_fe_get_msgfd (&g->vhost_chr); +uint64_t modifier =0 ;QemuDmaBuf *dmabuf; if (m->scanout_id >= g->parent_obj.conf.max_outputs) { @@ -261,30 +262,32 @@ vhost_user_gpu_handle_display(VhostUserGPU *g,VhostUserGpuMsg *msg)
g->parent_obj.enable = 1; con = g->parent_obj.scanout[m->scanout_id].con;hw/display/vhost-user-gpu.c +9
-13- dmabuf = &g->dmabuf[m->scanout_id]; -if (dmabuf->fd >=0 ) { -close (dmabuf->fd); - dmabuf->fd =-1 ; + dmabuf = g->dmabuf[m->scanout_id]; +if (dmabuf) { +int dmabuf_fd =dpy_gl_dmabuf_get_fd (dmabuf); +if (dmabuf_fd >=0 ) { +close (dmabuf_fd); + } +dpy_gl_release_dmabuf (con, dmabuf); } -dpy_gl_release_dmabuf (con, dmabuf); +if (fd ==-1 ) {dpy_gl_scanout_disable (con);break ; } - *dmabuf = (QemuDmaBuf ) { - .fd = fd, - .width = m->fd_width, - .height = m->fd_height, - .stride = m->fd_stride, - .fourcc = m->fd_drm_fourcc, - .y0_top = m->fd_flags &VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP , - }; +if (msg->request ==VHOST_USER_GPU_DMABUF_SCANOUT2 ) { VhostUserGpuDMABUFScanout2 *m2 = &msg-payload.dmabuf_scanout2;
hw/display/vhost-user-gpu.c +2
-2- dmabuf->modifier = m2->modifier; + modifier = m2->modifier; } -dpy_gl_scanout_dmabuf (con, dmabuf); + dmabuf = dpy_gl_create_dmabuf(m->fd_width, m->fd_height, m-
fd_stride,hw/display/vhost-user-gpu.c +7
-0+0 ,0 ,0 ,0 , m->fd_drm_fourcc, modifier, + fd, false, m->fd_flags & + + VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP); + +dpy_gl_scanout_dmabuf (con, g->dmabuf[m->scanout_id]); + g->dmabuf[m->scanout_id] = dmabuf;break ; } case VHOST_USER_GPU_DMABUF_UPDATE: { diff --git
a/hw/display/virtio-gpu-udmabuf.cb/hw/display/virtio-gpu-udmabuf.cindexa4ebf828ec..2bc8697d86100644hw/display/virtio-gpu-udmabuf.c +0
-0@@ -162,7 +162,7 @@ static void virtio_gpu_free_dmabuf(VirtIOGPU *g,VGPUDMABuf *dmabuf)
hw/vfio/display.c static VFIODMABuf
Quoted 50 lines
hw/display/virtio-gpu-udmabuf.c +8
-15struct virtio_gpu_scanout *scanout; scanout = &g->parent_obj.scanout[dmabuf->scanout_id]; -dpy_gl_release_dmabuf (scanout->con, &dmabuf->buf); +dpy_gl_release_dmabuf (scanout->con, dmabuf->buf);QTAILQ_REMOVE (&g->dmabuf.bufs, dmabuf, next);g_free (dmabuf); }@@ -181,17 +181,10 @@ static VGPUDMABuf } dmabuf =g_new0 (VGPUDMABuf,1 ); - dmabuf->buf.width = r->width; - dmabuf->buf.height = r->height; - dmabuf->buf.stride = fb->stride; - dmabuf->buf.x = r->x; - dmabuf->buf.y = r->y; - dmabuf->buf.backing_width = fb->width; - dmabuf->buf.backing_height = fb->height; - dmabuf->buf.fourcc =qemu_pixman_to_drm_format (fb->format); - dmabuf->buf.fd = res->dmabuf_fd; - dmabuf->buf.allow_fences = true; - dmabuf->buf.draw_submitted = false; + dmabuf->buf =dpy_gl_create_dmabuf (r->width, r->height, fb->stride, + r->x, r->y, fb->width, fb->height, +qemu_pixman_to_drm_format (fb->format), +0 , res->dmabuf_fd, false,0 ); dmabuf->scanout_id = scanout_id;QTAILQ_INSERT_HEAD (&g->dmabuf.bufs, dmabuf, next);@@ -217,11 +210,11 @@ int virtio_gpu_update_dmabuf(VirtIOGPU *g, old_primary = g->dmabuf.primary[scanout_id]; } - width =dpy_gl_dmabuf_get_width (&new_primary->buf); - height =dpy_gl_dmabuf_get_height (&new_primary->buf); + width =dpy_gl_dmabuf_get_width (new_primary->buf); + height =dpy_gl_dmabuf_get_height (new_primary->buf); g->dmabuf.primary[scanout_id] = new_primary;qemu_console_resize (scanout->con, width, height); -dpy_gl_scanout_dmabuf (scanout->con, &new_primary->buf); +dpy_gl_scanout_dmabuf (scanout->con, new_primary->buf);if (old_primary) { virtio_gpu_free_dmabuf(g, old_primary); diff --git
a/hw/vfio/display.cb/hw/vfio/display.cindex676b2fc5f3..62f5db2d54100644hw/vfio/display.c +0
-0@@ -240,15 +240,11 @@ static VFIODMABuf*vfio_display_get_dmabuf(VFIOPCIDevice *vdev,
Quoted 4 lines
hw/vfio/display.c +0
-1} dmabuf =g_new0 (VFIODMABuf,1 ); - dmabuf->dmabuf_id = plane.dmabuf_id;hw/vfio/display.c static VFIODMABuf *vfio_display_get_dmabuf(VFIOPCIDevice *vdev,
Quoted 10 lines
hw/vfio/display.c +2
-8- dmabuf->buf.width = plane.width; - dmabuf->buf.height = plane.height; - dmabuf->buf.backing_width = plane.width; - dmabuf->buf.backing_height = plane.height; - dmabuf->buf.stride = plane.stride; - dmabuf->buf.fourcc = plane.drm_format; - dmabuf->buf.modifier = plane.drm_format_mod; - dmabuf->buf.fd = fd; + dmabuf->dmabuf_id = plane.dmabuf_id; + dmabuf->buf = dpy_gl_create_dmabuf(plane.width, plane.height,plane.stride,
hw/vfio/display.c static void
Quoted 7 lines
hw/vfio/display.c +3
-0+0 ,0 , plane.width, plane.height, + plane.drm_format, plane.drm_format_mod, + fd, false, 0);if (plane_type ==DRM_PLANE_TYPE_CURSOR ) {vfio_display_update_cursor (dmabuf, &plane); }@@ -262,12 +258,12 @@ static void
vfio_display_free_one_dmabuf(VFIODisplay *dpy, VFIODMABuf *dmabuf)hw/vfio/display.c static void vfio_display_dmabuf_update(void
Quoted 15 lines
hw/vfio/display.c +2
-2int fd;QTAILQ_REMOVE (&dpy->dmabuf.bufs, dmabuf, next); - fd =dpy_gl_dmabuf_get_fd (&dmabuf->buf); + fd =dpy_gl_dmabuf_get_fd (dmabuf->buf);if (fd >-1 ) {close (fd); } -dpy_gl_release_dmabuf (dpy->con, &dmabuf->buf); +dpy_gl_release_dmabuf (dpy->con, dmabuf->buf);g_free (dmabuf); }@@ -303,13 +299,13 @@ static void vfio_display_dmabuf_update(void*opaque)
hw/vfio/display.c static void vfio_display_dmabuf_update(void
Quoted 17 lines
hw/vfio/display.c +3
-3return ; } - width =dpy_gl_dmabuf_get_width (&primary->buf); - height =dpy_gl_dmabuf_get_height (&primary->buf); + width =dpy_gl_dmabuf_get_width (primary->buf); + height =dpy_gl_dmabuf_get_height (primary->buf);if (dpy->dmabuf.primary != primary) { dpy->dmabuf.primary = primary;qemu_console_resize (dpy->con, width, height); -dpy_gl_scanout_dmabuf (dpy->con, &primary->buf); +dpy_gl_scanout_dmabuf (dpy->con, primary->buf); free_bufs = true; }@@ -323,7 +319,7 @@ static void vfio_display_dmabuf_update(void*opaque)
Quoted 15 lines
hw/vfio/display.c +1
-1if (cursor && (new_cursor || cursor->hot_updates)) {bool have_hot = (cursor->hot_x !=0xffffffff && cursor->hot_y !=0xffffffff ); -dpy_gl_cursor_dmabuf (dpy->con, &cursor->buf, have_hot, +dpy_gl_cursor_dmabuf (dpy->con, cursor->buf, have_hot, cursor->hot_x, cursor->hot_y); cursor->hot_updates =0 ; } else if (!cursor && new_cursor) { diff --git a/ui/console.c
b/ui/console.cindex69560aac7e..f3920e7713100644ui/console.c +0
-0@@ -1132,6 +1132,33 @@ void dpy_gl_cursor_position(QemuConsole *con, } }
Quoted 9 lines
ui/console.c void dpy_gl_cursor_position(QemuConsole *con, · line 1140
Quoted 6 lines
ui/console.c +6
-0+QemuDmaBuf *dpy_gl_create_dmabuf (uint32_t width,uint32_t height, +uint32_t stride,uint32_t x, +uint32_t y,uint32_t backing_width, +uint32_t backing_height,uint32_t fourcc, +uint64_t modifier,uint32_t dmabuf_fd, +bool allow_fences,bool y0_top) {We usually follow glib conventions, name the function according to the type allocated, and add a "
_new" suffix:qemu_dma_buf_new()
[Kim, Dongwon] Yeah, I will change the names of these helpers.
Quoted 26 lines
Quoted 26 lines
ui/console.c +21
-0+QemuDmaBuf *dmabuf; + + dmabuf =g_new0 (QemuDmaBuf,1 ); + + dmabuf->width = width; + dmabuf->height = height; + dmabuf->stride = stride; + dmabuf->x = x; + dmabuf->y = y; + dmabuf->backing_width = backing_width; + dmabuf->backing_height = backing_height; + dmabuf->fourcc = fourcc; + dmabuf->modifier = modifier; + dmabuf->fd = dmabuf_fd; + dmabuf->allow_fences = allow_fences; + dmabuf->y0_top = y0_top; + dmabuf->fence_fd =-1 ; + +return dmabuf; +} +uint32_t dpy_gl_dmabuf_get_width (QemuDmaBuf *dmabuf) {if (dmabuf) {@@ -1164,6 +1191,7 @@ void dpy_gl_release_dmabuf(QemuConsole *con, dcl->ops->dpy_gl_release_dmabuf (dcl, dmabuf);
Quoted 8 lines
ui/console.c void dpy_gl_release_dmabuf(QemuConsole *con, · line 1194
Quoted 3 lines
ui/console.c +1
-0} } +g_free (dmabuf);This is not a good place to free dmabuf, since it's a place to notify console listeners. I suggest introducing another function,
qemu_dma_buf_free(). And to use it throughg_clear_pointer(&buf, qemu_dma_buf_free), to avoid dangling pointers later.
[Kim, Dongwon] Ok, I will check that.
Quoted 58 lines
ui/dbus-listener.c static void
Quoted 9 lines
ui/console.c +0
-0} void dpy_gl_update(QemuConsole *con,ui/dbus-listener.c index renamed from ui/dbus-listener.c +0
-0
4a0a5d78f9..a2145704cc100644ui/dbus-listener.c +0
-0@@ -438,28 +438,24 @@ static voiddbus_scanout_texture(DisplayChangeListener *dcl,
Quoted 41 lines
ui/dbus-listener.c +0
-0trace_dbus_scanout_texture(tex_id, backing_y_0_top, backing_width, backing_height, x, y,w, h); #ifdef
CONFIG_GBMui/dbus-listener.c +12
-17-QemuDmaBuf dmabuf = { - .width = w, - .height = h, - .y0_top = backing_y_0_top, - .x = x, - .y = y, - .backing_width = backing_width, - .backing_height = backing_height, - }; +int32_t fd; +uint32_t stride, fourcc; +uint64_t modifier; +QemuDmaBuf *dmabuf;assert (tex_id); - dmabuf.fd =egl_get_fd_for_texture ( - tex_id, (EGLint *)&dmabuf.stride, - (EGLint *)&dmabuf.fourcc, - &dmabuf.modifier); -if (dmabuf.fd <0 ) { + fd =egl_get_fd_for_texture (tex_id, (EGLint *)&stride, (EGLint *)&fourcc, + &modifier); +if (fd <0 ) {error_report ("%s: failed to get fd for texture" , __func__);return ; } + dmabuf =dpy_gl_create_dmabuf (w, h, stride, x, y, backing_width, + backing_height, fourcc, modifier, fd, + false, backing_y_0_top); -dbus_scanout_dmabuf (dcl, &dmabuf); -close (dmabuf.fd); +dbus_scanout_dmabuf (dcl, dmabuf); +close (fd);#endif #ifdef WIN32 --Quoted 3 lines
2.34.1
--
Marc-André Lureau