tests/wayland/dma-buf-scanout: Use WaylandBuffer

WaylandBuffer supports dma-bufs now, even ones which can be scanned out.

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/3350>
This commit is contained in:
Sebastian Wick 2023-12-12 16:49:05 +01:00 committed by Marge Bot
parent 6f4da83b36
commit d4b9e72440

View File

@ -42,164 +42,47 @@
#include "config.h" #include "config.h"
#include <drm_fourcc.h>
#include <fcntl.h> #include <fcntl.h>
#include <gbm.h>
#include <glib.h> #include <glib.h>
#include <unistd.h> #include <unistd.h>
#include <wayland-client.h> #include <wayland-client.h>
#include "wayland-test-client-utils.h" #include "wayland-test-client-utils.h"
#include "linux-dmabuf-unstable-v1-client-protocol.h"
typedef enum typedef enum
{ {
WINDOW_STATE_NONE, WINDOW_STATE_NONE,
WINDOW_STATE_FULLSCREEN, WINDOW_STATE_FULLSCREEN,
} WindowState; } WindowState;
typedef struct _Buffer
{
struct wl_buffer *buffer;
gboolean busy;
gboolean recreate;
int dmabuf_fds[4];
struct gbm_bo *bo;
int n_planes;
uint32_t width, height, strides[4], offsets[4];
uint32_t format;
uint64_t modifier;
} Buffer;
static WaylandDisplay *display; static WaylandDisplay *display;
static struct wl_registry *registry;
static struct zwp_linux_dmabuf_v1 *dmabuf;
static struct wl_surface *surface; static struct wl_surface *surface;
static struct xdg_surface *xdg_surface; static struct xdg_surface *xdg_surface;
static struct xdg_toplevel *xdg_toplevel; static struct xdg_toplevel *xdg_toplevel;
struct gbm_device *gbm_device;
static GList *active_buffers; static GList *active_buffers;
static int prev_width; static int prev_width;
static int prev_height; static int prev_height;
static WindowState window_state; static WindowState window_state;
static struct
{
uint32_t format;
uint64_t *modifiers;
int n_modifiers;
} format_state = {
.format = DRM_FORMAT_XRGB8888,
};
static gboolean running; static gboolean running;
static void
buffer_free (Buffer *buffer)
{
int i;
g_clear_pointer (&buffer->buffer, wl_buffer_destroy);
g_clear_pointer (&buffer->bo, gbm_bo_destroy);
for (i = 0; i < buffer->n_planes; i++)
close (buffer->dmabuf_fds[i]);
g_free (buffer);
}
static void static void
handle_buffer_release (void *user_data, handle_buffer_release (void *user_data,
struct wl_buffer *buffer_resource) struct wl_buffer *buffer_resource)
{ {
Buffer *buffer = user_data; WaylandBuffer *buffer = user_data;
active_buffers = g_list_remove (active_buffers, buffer); active_buffers = g_list_remove (active_buffers, buffer);
buffer_free (buffer); g_object_unref (buffer);
} }
static const struct wl_buffer_listener buffer_listener = { static const struct wl_buffer_listener buffer_listener = {
handle_buffer_release handle_buffer_release
}; };
static Buffer *
create_dma_buf_buffer (uint32_t width,
uint32_t height,
uint32_t format,
unsigned int n_modifiers,
uint64_t *modifiers)
{
Buffer *buffer;
static uint32_t flags = 0;
struct zwp_linux_buffer_params_v1 *params;
int i;
buffer = g_new0 (Buffer, 1);
buffer->width = width;
buffer->height = height;
buffer->format = format;
if (n_modifiers > 0)
{
buffer->bo = gbm_bo_create_with_modifiers2 (gbm_device,
buffer->width, buffer->height,
format, modifiers,
n_modifiers,
GBM_BO_USE_RENDERING |
GBM_BO_USE_SCANOUT);
if (buffer->bo)
buffer->modifier = gbm_bo_get_modifier (buffer->bo);
}
if (!buffer->bo)
{
buffer->bo = gbm_bo_create (gbm_device, buffer->width,
buffer->height, buffer->format,
GBM_BO_USE_RENDERING |
GBM_BO_USE_SCANOUT);
buffer->modifier = DRM_FORMAT_MOD_INVALID;
}
g_assert_nonnull (buffer->bo);
buffer->n_planes = gbm_bo_get_plane_count (buffer->bo);
params = zwp_linux_dmabuf_v1_create_params (dmabuf);
for (i = 0; i < buffer->n_planes; i++)
{
buffer->dmabuf_fds[i] = gbm_bo_get_fd_for_plane (buffer->bo, i);
buffer->strides[i] = gbm_bo_get_stride_for_plane (buffer->bo, i);
buffer->offsets[i] = gbm_bo_get_offset (buffer->bo, i);
g_assert_cmpint (buffer->dmabuf_fds[i], >=, 0);
g_assert_cmpint (buffer->strides[i], >, 0);
zwp_linux_buffer_params_v1_add (params, buffer->dmabuf_fds[i], i,
buffer->offsets[i], buffer->strides[i],
buffer->modifier >> 32,
buffer->modifier & 0xffffffff);
}
buffer->buffer =
zwp_linux_buffer_params_v1_create_immed(params,
buffer->width,
buffer->height,
buffer->format,
flags);
g_assert_nonnull (buffer->buffer);
wl_buffer_add_listener (buffer->buffer, &buffer_listener, buffer);
return buffer;
}
static void static void
init_surface (void) init_surface (void)
{ {
@ -212,16 +95,26 @@ static void
draw_main (int width, draw_main (int width,
int height) int height)
{ {
Buffer *buffer; WaylandBuffer *buffer;
DmaBufFormat *format;
buffer = create_dma_buf_buffer (width, height, format = g_hash_table_lookup (display->formats,
format_state.format, GUINT_TO_POINTER (DRM_FORMAT_XRGB8888));
format_state.n_modifiers, g_assert_nonnull (format);
format_state.modifiers);
buffer = wayland_buffer_create (display,
&buffer_listener,
width, height,
format->format,
format->modifiers,
format->n_modifiers,
GBM_BO_USE_RENDERING |
GBM_BO_USE_SCANOUT);
g_assert_nonnull (buffer);
active_buffers = g_list_prepend (active_buffers, buffer); active_buffers = g_list_prepend (active_buffers, buffer);
wl_surface_attach (surface, buffer->buffer, 0, 0); wl_surface_attach (surface, wayland_buffer_get_wl_buffer (buffer), 0, 0);
} }
static WindowState static WindowState
@ -317,69 +210,6 @@ static const struct xdg_surface_listener xdg_surface_listener = {
handle_xdg_surface_configure, handle_xdg_surface_configure,
}; };
static void
dmabuf_modifiers (void *user_data,
struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf,
uint32_t format,
uint32_t modifier_hi,
uint32_t modifier_lo)
{
uint64_t modifier;
modifier = ((uint64_t)modifier_hi << 32) | modifier_lo;
if (format != format_state.format)
return;
if (modifier != DRM_FORMAT_MOD_INVALID)
{
format_state.n_modifiers++;
format_state.modifiers = g_realloc_n (format_state.modifiers,
format_state.n_modifiers,
sizeof (uint64_t));
format_state.modifiers[format_state.n_modifiers - 1] = modifier;
}
}
static void
dmabuf_format (void *user_data,
struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf,
uint32_t format)
{
}
static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
dmabuf_format,
dmabuf_modifiers
};
static void
handle_registry_global (void *user_data,
struct wl_registry *registry,
uint32_t id,
const char *interface,
uint32_t version)
{
if (strcmp (interface, "zwp_linux_dmabuf_v1") == 0)
{
g_assert_cmpuint (version, >=, 3);
dmabuf = wl_registry_bind (registry, id,
&zwp_linux_dmabuf_v1_interface, 3);
zwp_linux_dmabuf_v1_add_listener (dmabuf, &dmabuf_listener, NULL);
}
}
static void
handle_registry_global_remove (void *user_data,
struct wl_registry *registry,
uint32_t name)
{
}
static const struct wl_registry_listener registry_listener = {
handle_registry_global,
handle_registry_global_remove
};
static void static void
on_sync_event (WaylandDisplay *display, on_sync_event (WaylandDisplay *display,
uint32_t serial) uint32_t serial)
@ -393,29 +223,10 @@ int
main (int argc, main (int argc,
char **argv) char **argv)
{ {
const char *gpu_path;
int fd;
display = wayland_display_new (WAYLAND_DISPLAY_CAPABILITY_TEST_DRIVER); display = wayland_display_new (WAYLAND_DISPLAY_CAPABILITY_TEST_DRIVER);
g_signal_connect (display, "sync-event", G_CALLBACK (on_sync_event), NULL); g_signal_connect (display, "sync-event", G_CALLBACK (on_sync_event), NULL);
registry = wl_display_get_registry (display->display);
wl_registry_add_listener (registry, &registry_listener, NULL);
wl_display_roundtrip (display->display); wl_display_roundtrip (display->display);
wl_display_roundtrip (display->display); g_assert_nonnull (display->gbm_device);
g_assert_nonnull (dmabuf);
gpu_path = lookup_property_value (display, "gpu-path");
g_assert_nonnull (gpu_path);
fd = open (gpu_path, O_RDWR);
if (fd < 0)
{
g_error ("Failed to open drm render node %s: %s",
gpu_path, g_strerror (errno));
}
gbm_device = gbm_create_device (fd);
surface = wl_compositor_create_surface (display->compositor); surface = wl_compositor_create_surface (display->compositor);
xdg_surface = xdg_wm_base_get_xdg_surface (display->xdg_wm_base, surface); xdg_surface = xdg_wm_base_get_xdg_surface (display->xdg_wm_base, surface);
@ -432,7 +243,7 @@ main (int argc,
return EXIT_FAILURE; return EXIT_FAILURE;
} }
g_list_free_full (active_buffers, (GDestroyNotify) buffer_free); g_list_free_full (active_buffers, (GDestroyNotify) g_object_unref);
g_object_unref (display); g_object_unref (display);
return EXIT_SUCCESS; return EXIT_SUCCESS;