ui/dmabuf: extend QemuDmaBuf to support multi-plane

mesa/radeonsi is going to support explicit modifier which
may export a multi-plane texture. For example, texture with
DCC enabled (a compressed format) has two planes, one with
compressed data, the other with meta data for compression.

v2:
  * change API qemu_dmabuf_get_fd/offset/stride to
    qemu_dmabuf_get_fds/offsets/strides.
  * change API qemu_dmabuf_dup_fd to qemu_dmabuf_dup_fds.
  * add an extra arg to these API for the length of the
    array.

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Qiang Yu <yuq825@gmail.com>
[ Fix style ]
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-ID: <20250327025848.46962-2-yuq825@gmail.com>
This commit is contained in:
Qiang Yu 2025-03-27 10:58:43 +08:00 committed by Marc-André Lureau
parent 5134cf9b5d
commit bb5101aadc
8 changed files with 90 additions and 49 deletions

View file

@ -249,6 +249,8 @@ vhost_user_gpu_handle_display(VhostUserGPU *g, VhostUserGpuMsg *msg)
case VHOST_USER_GPU_DMABUF_SCANOUT: { case VHOST_USER_GPU_DMABUF_SCANOUT: {
VhostUserGpuDMABUFScanout *m = &msg->payload.dmabuf_scanout; VhostUserGpuDMABUFScanout *m = &msg->payload.dmabuf_scanout;
int fd = qemu_chr_fe_get_msgfd(&g->vhost_chr); int fd = qemu_chr_fe_get_msgfd(&g->vhost_chr);
uint32_t offset = 0;
uint32_t stride = m->fd_stride;
uint64_t modifier = 0; uint64_t modifier = 0;
QemuDmaBuf *dmabuf; QemuDmaBuf *dmabuf;
@ -282,10 +284,10 @@ vhost_user_gpu_handle_display(VhostUserGPU *g, VhostUserGpuMsg *msg)
} }
dmabuf = qemu_dmabuf_new(m->width, m->height, dmabuf = qemu_dmabuf_new(m->width, m->height,
m->fd_stride, 0, 0, &offset, &stride, 0, 0,
m->fd_width, m->fd_height, m->fd_width, m->fd_height,
m->fd_drm_fourcc, modifier, m->fd_drm_fourcc, modifier,
fd, false, m->fd_flags & &fd, 1, false, m->fd_flags &
VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP); VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP);
dpy_gl_scanout_dmabuf(con, dmabuf); dpy_gl_scanout_dmabuf(con, dmabuf);

View file

@ -176,16 +176,18 @@ static VGPUDMABuf
struct virtio_gpu_rect *r) struct virtio_gpu_rect *r)
{ {
VGPUDMABuf *dmabuf; VGPUDMABuf *dmabuf;
uint32_t offset = 0;
if (res->dmabuf_fd < 0) { if (res->dmabuf_fd < 0) {
return NULL; return NULL;
} }
dmabuf = g_new0(VGPUDMABuf, 1); dmabuf = g_new0(VGPUDMABuf, 1);
dmabuf->buf = qemu_dmabuf_new(r->width, r->height, fb->stride, dmabuf->buf = qemu_dmabuf_new(r->width, r->height,
&offset, &fb->stride,
r->x, r->y, fb->width, fb->height, r->x, r->y, fb->width, fb->height,
qemu_pixman_to_drm_format(fb->format), qemu_pixman_to_drm_format(fb->format),
0, res->dmabuf_fd, true, false); 0, &res->dmabuf_fd, 1, true, false);
dmabuf->scanout_id = scanout_id; dmabuf->scanout_id = scanout_id;
QTAILQ_INSERT_HEAD(&g->dmabuf.bufs, dmabuf, next); QTAILQ_INSERT_HEAD(&g->dmabuf.bufs, dmabuf, next);

View file

@ -213,6 +213,7 @@ static VFIODMABuf *vfio_display_get_dmabuf(VFIOPCIDevice *vdev,
struct vfio_device_gfx_plane_info plane; struct vfio_device_gfx_plane_info plane;
VFIODMABuf *dmabuf; VFIODMABuf *dmabuf;
int fd, ret; int fd, ret;
uint32_t offset = 0;
memset(&plane, 0, sizeof(plane)); memset(&plane, 0, sizeof(plane));
plane.argsz = sizeof(plane); plane.argsz = sizeof(plane);
@ -245,10 +246,10 @@ static VFIODMABuf *vfio_display_get_dmabuf(VFIOPCIDevice *vdev,
dmabuf = g_new0(VFIODMABuf, 1); dmabuf = g_new0(VFIODMABuf, 1);
dmabuf->dmabuf_id = plane.dmabuf_id; dmabuf->dmabuf_id = plane.dmabuf_id;
dmabuf->buf = qemu_dmabuf_new(plane.width, plane.height, dmabuf->buf = qemu_dmabuf_new(plane.width, plane.height, &offset,
plane.stride, 0, 0, plane.width, &plane.stride, 0, 0, plane.width,
plane.height, plane.drm_format, plane.height, plane.drm_format,
plane.drm_format_mod, fd, false, false); plane.drm_format_mod, &fd, 1, false, false);
if (plane_type == DRM_PLANE_TYPE_CURSOR) { if (plane_type == DRM_PLANE_TYPE_CURSOR) {
vfio_display_update_cursor(dmabuf, &plane); vfio_display_update_cursor(dmabuf, &plane);

View file

@ -10,24 +10,29 @@
#ifndef DMABUF_H #ifndef DMABUF_H
#define DMABUF_H #define DMABUF_H
#define DMABUF_MAX_PLANES 4
typedef struct QemuDmaBuf QemuDmaBuf; typedef struct QemuDmaBuf QemuDmaBuf;
QemuDmaBuf *qemu_dmabuf_new(uint32_t width, uint32_t height, QemuDmaBuf *qemu_dmabuf_new(uint32_t width, uint32_t height,
uint32_t stride, uint32_t x, const uint32_t *offset, const uint32_t *stride,
uint32_t y, uint32_t backing_width, uint32_t x, uint32_t y,
uint32_t backing_height, uint32_t fourcc, uint32_t backing_width, uint32_t backing_height,
uint64_t modifier, int dmabuf_fd, uint32_t fourcc, uint64_t modifier,
const int32_t *dmabuf_fd, uint32_t num_planes,
bool allow_fences, bool y0_top); bool allow_fences, bool y0_top);
void qemu_dmabuf_free(QemuDmaBuf *dmabuf); void qemu_dmabuf_free(QemuDmaBuf *dmabuf);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(QemuDmaBuf, qemu_dmabuf_free); G_DEFINE_AUTOPTR_CLEANUP_FUNC(QemuDmaBuf, qemu_dmabuf_free);
int qemu_dmabuf_get_fd(QemuDmaBuf *dmabuf); const int *qemu_dmabuf_get_fds(QemuDmaBuf *dmabuf, int *nfds);
int qemu_dmabuf_dup_fd(QemuDmaBuf *dmabuf); void qemu_dmabuf_dup_fds(QemuDmaBuf *dmabuf, int *fds, int nfds);
void qemu_dmabuf_close(QemuDmaBuf *dmabuf); void qemu_dmabuf_close(QemuDmaBuf *dmabuf);
uint32_t qemu_dmabuf_get_width(QemuDmaBuf *dmabuf); uint32_t qemu_dmabuf_get_width(QemuDmaBuf *dmabuf);
uint32_t qemu_dmabuf_get_height(QemuDmaBuf *dmabuf); uint32_t qemu_dmabuf_get_height(QemuDmaBuf *dmabuf);
uint32_t qemu_dmabuf_get_stride(QemuDmaBuf *dmabuf); const uint32_t *qemu_dmabuf_get_offsets(QemuDmaBuf *dmabuf, int *noffsets);
const uint32_t *qemu_dmabuf_get_strides(QemuDmaBuf *dmabuf, int *nstrides);
uint32_t qemu_dmabuf_get_num_planes(QemuDmaBuf *dmabuf);
uint32_t qemu_dmabuf_get_fourcc(QemuDmaBuf *dmabuf); uint32_t qemu_dmabuf_get_fourcc(QemuDmaBuf *dmabuf);
uint64_t qemu_dmabuf_get_modifier(QemuDmaBuf *dmabuf); uint64_t qemu_dmabuf_get_modifier(QemuDmaBuf *dmabuf);
uint32_t qemu_dmabuf_get_texture(QemuDmaBuf *dmabuf); uint32_t qemu_dmabuf_get_texture(QemuDmaBuf *dmabuf);
@ -44,6 +49,5 @@ void qemu_dmabuf_set_texture(QemuDmaBuf *dmabuf, uint32_t texture);
void qemu_dmabuf_set_fence_fd(QemuDmaBuf *dmabuf, int32_t fence_fd); void qemu_dmabuf_set_fence_fd(QemuDmaBuf *dmabuf, int32_t fence_fd);
void qemu_dmabuf_set_sync(QemuDmaBuf *dmabuf, void *sync); void qemu_dmabuf_set_sync(QemuDmaBuf *dmabuf, void *sync);
void qemu_dmabuf_set_draw_submitted(QemuDmaBuf *dmabuf, bool draw_submitted); void qemu_dmabuf_set_draw_submitted(QemuDmaBuf *dmabuf, bool draw_submitted);
void qemu_dmabuf_set_fd(QemuDmaBuf *dmabuf, int32_t fd);
#endif #endif

View file

@ -299,7 +299,7 @@ static void dbus_scanout_dmabuf(DisplayChangeListener *dcl,
uint64_t modifier; uint64_t modifier;
bool y0_top; bool y0_top;
fd = qemu_dmabuf_get_fd(dmabuf); fd = qemu_dmabuf_get_fds(dmabuf, NULL)[0];
fd_list = g_unix_fd_list_new(); fd_list = g_unix_fd_list_new();
if (g_unix_fd_list_append(fd_list, fd, &err) != 0) { if (g_unix_fd_list_append(fd_list, fd, &err) != 0) {
error_report("Failed to setup dmabuf fdlist: %s", err->message); error_report("Failed to setup dmabuf fdlist: %s", err->message);
@ -310,7 +310,7 @@ static void dbus_scanout_dmabuf(DisplayChangeListener *dcl,
width = qemu_dmabuf_get_width(dmabuf); width = qemu_dmabuf_get_width(dmabuf);
height = qemu_dmabuf_get_height(dmabuf); height = qemu_dmabuf_get_height(dmabuf);
stride = qemu_dmabuf_get_stride(dmabuf); stride = qemu_dmabuf_get_strides(dmabuf, NULL)[0];
fourcc = qemu_dmabuf_get_fourcc(dmabuf); fourcc = qemu_dmabuf_get_fourcc(dmabuf);
modifier = qemu_dmabuf_get_modifier(dmabuf); modifier = qemu_dmabuf_get_modifier(dmabuf);
y0_top = qemu_dmabuf_get_y0_top(dmabuf); y0_top = qemu_dmabuf_get_y0_top(dmabuf);
@ -505,7 +505,7 @@ static void dbus_scanout_texture(DisplayChangeListener *dcl,
#ifdef CONFIG_GBM #ifdef CONFIG_GBM
g_autoptr(QemuDmaBuf) dmabuf = NULL; g_autoptr(QemuDmaBuf) dmabuf = NULL;
int fd; int fd;
uint32_t stride, fourcc; uint32_t offset = 0, stride, fourcc;
uint64_t modifier; uint64_t modifier;
assert(tex_id); assert(tex_id);
@ -515,8 +515,8 @@ static void dbus_scanout_texture(DisplayChangeListener *dcl,
error_report("%s: failed to get fd for texture", __func__); error_report("%s: failed to get fd for texture", __func__);
return; return;
} }
dmabuf = qemu_dmabuf_new(w, h, stride, x, y, backing_width, dmabuf = qemu_dmabuf_new(w, h, &offset, &stride, x, y, backing_width,
backing_height, fourcc, modifier, fd, backing_height, fourcc, modifier, &fd, 1,
false, backing_y_0_top); false, backing_y_0_top);
dbus_scanout_dmabuf(dcl, dmabuf); dbus_scanout_dmabuf(dcl, dmabuf);

View file

@ -11,10 +11,12 @@
#include "ui/dmabuf.h" #include "ui/dmabuf.h"
struct QemuDmaBuf { struct QemuDmaBuf {
int fd; int fd[DMABUF_MAX_PLANES];
uint32_t width; uint32_t width;
uint32_t height; uint32_t height;
uint32_t stride; uint32_t offset[DMABUF_MAX_PLANES];
uint32_t stride[DMABUF_MAX_PLANES];
uint32_t num_planes;
uint32_t fourcc; uint32_t fourcc;
uint64_t modifier; uint64_t modifier;
uint32_t texture; uint32_t texture;
@ -30,28 +32,33 @@ struct QemuDmaBuf {
}; };
QemuDmaBuf *qemu_dmabuf_new(uint32_t width, uint32_t height, QemuDmaBuf *qemu_dmabuf_new(uint32_t width, uint32_t height,
uint32_t stride, uint32_t x, const uint32_t *offset, const uint32_t *stride,
uint32_t y, uint32_t backing_width, uint32_t x, uint32_t y,
uint32_t backing_height, uint32_t fourcc, uint32_t backing_width, uint32_t backing_height,
uint64_t modifier, int32_t dmabuf_fd, uint32_t fourcc, uint64_t modifier,
const int32_t *dmabuf_fd, uint32_t num_planes,
bool allow_fences, bool y0_top) { bool allow_fences, bool y0_top) {
QemuDmaBuf *dmabuf; QemuDmaBuf *dmabuf;
assert(num_planes > 0 && num_planes <= DMABUF_MAX_PLANES);
dmabuf = g_new0(QemuDmaBuf, 1); dmabuf = g_new0(QemuDmaBuf, 1);
dmabuf->width = width; dmabuf->width = width;
dmabuf->height = height; dmabuf->height = height;
dmabuf->stride = stride; memcpy(dmabuf->offset, offset, num_planes * sizeof(*offset));
memcpy(dmabuf->stride, stride, num_planes * sizeof(*stride));
dmabuf->x = x; dmabuf->x = x;
dmabuf->y = y; dmabuf->y = y;
dmabuf->backing_width = backing_width; dmabuf->backing_width = backing_width;
dmabuf->backing_height = backing_height; dmabuf->backing_height = backing_height;
dmabuf->fourcc = fourcc; dmabuf->fourcc = fourcc;
dmabuf->modifier = modifier; dmabuf->modifier = modifier;
dmabuf->fd = dmabuf_fd; memcpy(dmabuf->fd, dmabuf_fd, num_planes * sizeof(*dmabuf_fd));
dmabuf->allow_fences = allow_fences; dmabuf->allow_fences = allow_fences;
dmabuf->y0_top = y0_top; dmabuf->y0_top = y0_top;
dmabuf->fence_fd = -1; dmabuf->fence_fd = -1;
dmabuf->num_planes = num_planes;
return dmabuf; return dmabuf;
} }
@ -65,31 +72,40 @@ void qemu_dmabuf_free(QemuDmaBuf *dmabuf)
g_free(dmabuf); g_free(dmabuf);
} }
int qemu_dmabuf_get_fd(QemuDmaBuf *dmabuf) const int *qemu_dmabuf_get_fds(QemuDmaBuf *dmabuf, int *nfds)
{ {
assert(dmabuf != NULL); assert(dmabuf != NULL);
if (nfds) {
*nfds = ARRAY_SIZE(dmabuf->fd);
}
return dmabuf->fd; return dmabuf->fd;
} }
int qemu_dmabuf_dup_fd(QemuDmaBuf *dmabuf) void qemu_dmabuf_dup_fds(QemuDmaBuf *dmabuf, int *fds, int nfds)
{ {
assert(dmabuf != NULL); int i;
if (dmabuf->fd >= 0) { assert(dmabuf != NULL);
return dup(dmabuf->fd); assert(nfds >= dmabuf->num_planes);
} else {
return -1; for (i = 0; i < dmabuf->num_planes; i++) {
fds[i] = dmabuf->fd[i] >= 0 ? dup(dmabuf->fd[i]) : -1;
} }
} }
void qemu_dmabuf_close(QemuDmaBuf *dmabuf) void qemu_dmabuf_close(QemuDmaBuf *dmabuf)
{ {
int i;
assert(dmabuf != NULL); assert(dmabuf != NULL);
if (dmabuf->fd >= 0) { for (i = 0; i < dmabuf->num_planes; i++) {
close(dmabuf->fd); if (dmabuf->fd[i] >= 0) {
dmabuf->fd = -1; close(dmabuf->fd[i]);
dmabuf->fd[i] = -1;
}
} }
} }
@ -107,13 +123,35 @@ uint32_t qemu_dmabuf_get_height(QemuDmaBuf *dmabuf)
return dmabuf->height; return dmabuf->height;
} }
uint32_t qemu_dmabuf_get_stride(QemuDmaBuf *dmabuf) const uint32_t *qemu_dmabuf_get_offsets(QemuDmaBuf *dmabuf, int *noffsets)
{ {
assert(dmabuf != NULL); assert(dmabuf != NULL);
if (noffsets) {
*noffsets = ARRAY_SIZE(dmabuf->offset);
}
return dmabuf->offset;
}
const uint32_t *qemu_dmabuf_get_strides(QemuDmaBuf *dmabuf, int *nstrides)
{
assert(dmabuf != NULL);
if (nstrides) {
*nstrides = ARRAY_SIZE(dmabuf->stride);
}
return dmabuf->stride; return dmabuf->stride;
} }
uint32_t qemu_dmabuf_get_num_planes(QemuDmaBuf *dmabuf)
{
assert(dmabuf != NULL);
return dmabuf->num_planes;
}
uint32_t qemu_dmabuf_get_fourcc(QemuDmaBuf *dmabuf) uint32_t qemu_dmabuf_get_fourcc(QemuDmaBuf *dmabuf)
{ {
assert(dmabuf != NULL); assert(dmabuf != NULL);
@ -221,9 +259,3 @@ void qemu_dmabuf_set_draw_submitted(QemuDmaBuf *dmabuf, bool draw_submitted)
assert(dmabuf != NULL); assert(dmabuf != NULL);
dmabuf->draw_submitted = draw_submitted; dmabuf->draw_submitted = draw_submitted;
} }
void qemu_dmabuf_set_fd(QemuDmaBuf *dmabuf, int32_t fd)
{
assert(dmabuf != NULL);
dmabuf->fd = fd;
}

View file

@ -323,9 +323,9 @@ void egl_dmabuf_import_texture(QemuDmaBuf *dmabuf)
attrs[i++] = qemu_dmabuf_get_fourcc(dmabuf); attrs[i++] = qemu_dmabuf_get_fourcc(dmabuf);
attrs[i++] = EGL_DMA_BUF_PLANE0_FD_EXT; attrs[i++] = EGL_DMA_BUF_PLANE0_FD_EXT;
attrs[i++] = qemu_dmabuf_get_fd(dmabuf); attrs[i++] = qemu_dmabuf_get_fds(dmabuf, NULL)[0];
attrs[i++] = EGL_DMA_BUF_PLANE0_PITCH_EXT; attrs[i++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
attrs[i++] = qemu_dmabuf_get_stride(dmabuf); attrs[i++] = qemu_dmabuf_get_strides(dmabuf, NULL)[0];
attrs[i++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT; attrs[i++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
attrs[i++] = 0; attrs[i++] = 0;
#ifdef EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT #ifdef EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT

View file

@ -1075,10 +1075,10 @@ static void qemu_spice_gl_update(DisplayChangeListener *dcl,
stride, fourcc, false); stride, fourcc, false);
} }
} else { } else {
stride = qemu_dmabuf_get_stride(dmabuf); stride = qemu_dmabuf_get_strides(dmabuf, NULL)[0];
fourcc = qemu_dmabuf_get_fourcc(dmabuf); fourcc = qemu_dmabuf_get_fourcc(dmabuf);
y_0_top = qemu_dmabuf_get_y0_top(dmabuf); y_0_top = qemu_dmabuf_get_y0_top(dmabuf);
fd = qemu_dmabuf_dup_fd(dmabuf); qemu_dmabuf_dup_fds(dmabuf, &fd, 1);
trace_qemu_spice_gl_forward_dmabuf(ssd->qxl.id, width, height); trace_qemu_spice_gl_forward_dmabuf(ssd->qxl.id, width, height);
/* note: spice server will close the fd, so hand over a dup */ /* note: spice server will close the fd, so hand over a dup */