From 213bd88b4c0b9b36bf3b6faacd65929e37edb12e Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Thu, 21 Oct 2021 15:02:31 +0200 Subject: [PATCH] linux-drm-syncobj-v1: new protocol implementation References: https://gitlab.freedesktop.org/wayland/wayland-protocols/-/merge_requests/90 --- include/wlr/types/wlr_linux_drm_syncobj_v1.h | 46 ++ protocol/meson.build | 1 + types/meson.build | 1 + types/wlr_linux_drm_syncobj_v1.c | 469 +++++++++++++++++++ 4 files changed, 517 insertions(+) create mode 100644 include/wlr/types/wlr_linux_drm_syncobj_v1.h create mode 100644 types/wlr_linux_drm_syncobj_v1.c diff --git a/include/wlr/types/wlr_linux_drm_syncobj_v1.h b/include/wlr/types/wlr_linux_drm_syncobj_v1.h new file mode 100644 index 00000000..c6e0617b --- /dev/null +++ b/include/wlr/types/wlr_linux_drm_syncobj_v1.h @@ -0,0 +1,46 @@ +/* + * This an unstable interface of wlroots. No guarantees are made regarding the + * future consistency of this API. + */ +#ifndef WLR_USE_UNSTABLE +#error "Add -DWLR_USE_UNSTABLE to enable unstable wlroots features" +#endif + +#ifndef WLR_TYPES_WLR_LINUX_DRM_SYNCOBJ_V1_H +#define WLR_TYPES_WLR_LINUX_DRM_SYNCOBJ_V1_H + +#include +#include + +struct wlr_linux_drm_syncobj_surface_v1_state { + struct wlr_drm_syncobj_timeline *acquire_timeline; + uint64_t acquire_point; + + struct wlr_drm_syncobj_timeline *release_timeline; + uint64_t release_point; +}; + +struct wlr_linux_drm_syncobj_manager_v1 { + struct wl_global *global; + + // private state + + int drm_fd; + + struct wl_listener display_destroy; +}; + +/** + * Advertise explicit synchronization support to clients. + * + * The compositor must be prepared to handle fences coming from clients and to + * send release fences correctly. In particular, both the renderer and the + * backend need to support explicit synchronization. + */ +struct wlr_linux_drm_syncobj_manager_v1 *wlr_linux_drm_syncobj_manager_v1_create( + struct wl_display *display, uint32_t version, int drm_fd); + +struct wlr_linux_drm_syncobj_surface_v1_state *wlr_linux_drm_syncobj_v1_get_surface_state( + struct wlr_surface *surface); + +#endif diff --git a/protocol/meson.build b/protocol/meson.build index c60f2b9a..a424366d 100644 --- a/protocol/meson.build +++ b/protocol/meson.build @@ -27,6 +27,7 @@ protocols = { 'ext-idle-notify-v1': wl_protocol_dir / 'staging/ext-idle-notify/ext-idle-notify-v1.xml', 'ext-session-lock-v1': wl_protocol_dir / 'staging/ext-session-lock/ext-session-lock-v1.xml', 'fractional-scale-v1': wl_protocol_dir / 'staging/fractional-scale/fractional-scale-v1.xml', + 'linux-drm-syncobj-v1': wl_protocol_dir / 'staging/linux-drm-syncobj/linux-drm-syncobj-v1.xml', 'security-context-v1': wl_protocol_dir / 'staging/security-context/security-context-v1.xml', 'single-pixel-buffer-v1': wl_protocol_dir / 'staging/single-pixel-buffer/single-pixel-buffer-v1.xml', 'xdg-activation-v1': wl_protocol_dir / 'staging/xdg-activation/xdg-activation-v1.xml', diff --git a/types/meson.build b/types/meson.build index 9ea0d540..ec70d4b7 100644 --- a/types/meson.build +++ b/types/meson.build @@ -55,6 +55,7 @@ wlr_files += files( 'wlr_keyboard_shortcuts_inhibit_v1.c', 'wlr_layer_shell_v1.c', 'wlr_linux_dmabuf_v1.c', + 'wlr_linux_drm_syncobj_v1.c', 'wlr_matrix.c', 'wlr_output_layer.c', 'wlr_output_layout.c', diff --git a/types/wlr_linux_drm_syncobj_v1.c b/types/wlr_linux_drm_syncobj_v1.c new file mode 100644 index 00000000..82ace2d7 --- /dev/null +++ b/types/wlr_linux_drm_syncobj_v1.c @@ -0,0 +1,469 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "config.h" +#include "linux-drm-syncobj-v1-protocol.h" + +#define LINUX_DRM_SYNCOBJ_V1_VERSION 1 + +struct wlr_linux_drm_syncobj_surface_v1 { + struct wl_resource *resource; + struct wlr_surface *surface; + struct wlr_linux_drm_syncobj_surface_v1_state pending, current; + + struct wlr_addon addon; + struct wlr_surface_synced synced; + + struct wl_listener client_commit; +}; + +struct wlr_linux_drm_syncobj_surface_v1_commit { + struct wlr_linux_drm_syncobj_surface_v1 *surface; + struct wlr_drm_syncobj_timeline_waiter waiter; + uint32_t cached_seq; + + struct wl_listener waiter_ready; + struct wl_listener surface_destroy; +}; + +static const struct wp_linux_drm_syncobj_manager_v1_interface manager_impl; +static const struct wp_linux_drm_syncobj_timeline_v1_interface timeline_impl; +static const struct wp_linux_drm_syncobj_surface_v1_interface surface_impl; + +static struct wlr_linux_drm_syncobj_manager_v1 *manager_from_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, + &wp_linux_drm_syncobj_manager_v1_interface, &manager_impl)); + return wl_resource_get_user_data(resource); +} + +static struct wlr_drm_syncobj_timeline *timeline_from_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, + &wp_linux_drm_syncobj_timeline_v1_interface, &timeline_impl)); + return wl_resource_get_user_data(resource); +} + +// Returns NULL if the syncobj surface is inert +static struct wlr_linux_drm_syncobj_surface_v1 *surface_from_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, + &wp_linux_drm_syncobj_surface_v1_interface, &surface_impl)); + return wl_resource_get_user_data(resource); +} + +static void timeline_handle_resource_destroy(struct wl_resource *resource) { + struct wlr_drm_syncobj_timeline *timeline = timeline_from_resource(resource); + wlr_drm_syncobj_timeline_unref(timeline); +} + +static void timeline_handle_destroy(struct wl_client *client, + struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static const struct wp_linux_drm_syncobj_timeline_v1_interface timeline_impl = { + .destroy = timeline_handle_destroy, +}; + +static void surface_destroy(struct wlr_linux_drm_syncobj_surface_v1 *surface) { + if (surface == NULL) { + return; + } + wl_list_remove(&surface->client_commit.link); + wlr_addon_finish(&surface->addon); + wlr_surface_synced_finish(&surface->synced); + wl_resource_set_user_data(surface->resource, NULL); + free(surface); +} + +static void surface_handle_destroy(struct wl_client *client, + struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static void surface_handle_set_acquire_point(struct wl_client *client, + struct wl_resource *resource, struct wl_resource *timeline_resource, + uint32_t point_hi, uint32_t point_lo) { + struct wlr_linux_drm_syncobj_surface_v1 *surface = + surface_from_resource(resource); + if (surface == NULL) { + wl_resource_post_error(resource, + WP_LINUX_DRM_SYNCOBJ_SURFACE_V1_ERROR_NO_SURFACE, + "The surface has been destroyed"); + return; + } + + struct wlr_drm_syncobj_timeline *timeline = + timeline_from_resource(timeline_resource); + uint64_t point = (uint64_t)point_hi << 32 | point_lo; + + wlr_drm_syncobj_timeline_unref(surface->pending.acquire_timeline); + surface->pending.acquire_timeline = wlr_drm_syncobj_timeline_ref(timeline); + surface->pending.acquire_point = point; +} + +static void surface_handle_set_release_point(struct wl_client *client, + struct wl_resource *resource, struct wl_resource *timeline_resource, + uint32_t point_hi, uint32_t point_lo) { + struct wlr_linux_drm_syncobj_surface_v1 *surface = + surface_from_resource(resource); + if (surface == NULL) { + wl_resource_post_error(resource, + WP_LINUX_DRM_SYNCOBJ_SURFACE_V1_ERROR_NO_SURFACE, + "The surface has been destroyed"); + return; + } + + struct wlr_drm_syncobj_timeline *timeline = + timeline_from_resource(timeline_resource); + uint64_t point = (uint64_t)point_hi << 32 | point_lo; + + wlr_drm_syncobj_timeline_unref(surface->pending.release_timeline); + surface->pending.release_timeline = wlr_drm_syncobj_timeline_ref(timeline); + surface->pending.release_point = point; +} + +static const struct wp_linux_drm_syncobj_surface_v1_interface surface_impl = { + .destroy = surface_handle_destroy, + .set_acquire_point = surface_handle_set_acquire_point, + .set_release_point = surface_handle_set_release_point, +}; + +static void surface_handle_resource_destroy(struct wl_resource *resource) { + struct wlr_linux_drm_syncobj_surface_v1 *surface = + surface_from_resource(resource); + surface_destroy(surface); +} + +static void surface_addon_handle_surface_destroy(struct wlr_addon *addon) { + struct wlr_linux_drm_syncobj_surface_v1 *surface = + wl_container_of(addon, surface, addon); + surface_destroy(surface); +} + +static const struct wlr_addon_interface surface_addon_impl = { + .name = "wp_linux_drm_syncobj_surface_v1", + .destroy = surface_addon_handle_surface_destroy, +}; + +static void surface_synced_finish_state(void *_state) { + struct wlr_linux_drm_syncobj_surface_v1_state *state = _state; + wlr_drm_syncobj_timeline_unref(state->acquire_timeline); + wlr_drm_syncobj_timeline_unref(state->release_timeline); +} + +static void surface_synced_move_state(void *_dst, void *_src) { + struct wlr_linux_drm_syncobj_surface_v1_state *dst = _dst, *src = _src; + // TODO: immediately signal dst.release_timeline if necessary + surface_synced_finish_state(dst); + *dst = *src; + *src = (struct wlr_linux_drm_syncobj_surface_v1_state){0}; +} + +static const struct wlr_surface_synced_impl surface_synced_impl = { + .state_size = sizeof(struct wlr_linux_drm_syncobj_surface_v1_state), + .finish_state = surface_synced_finish_state, + .move_state = surface_synced_move_state, +}; + +static void manager_handle_destroy(struct wl_client *client, + struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static struct wlr_linux_drm_syncobj_surface_v1 *surface_from_wlr_surface( + struct wlr_surface *wlr_surface) { + struct wlr_addon *addon = + wlr_addon_find(&wlr_surface->addons, NULL, &surface_addon_impl); + if (addon == NULL) { + return NULL; + } + struct wlr_linux_drm_syncobj_surface_v1 *surface = + wl_container_of(addon, surface, addon); + return surface; +} + +static void surface_commit_destroy(struct wlr_linux_drm_syncobj_surface_v1_commit *commit) { + wlr_surface_unlock_cached(commit->surface->surface, commit->cached_seq); + wl_list_remove(&commit->surface_destroy.link); + wl_list_remove(&commit->waiter_ready.link); + wlr_drm_syncobj_timeline_waiter_finish(&commit->waiter); + free(commit); +} + +static void surface_commit_handle_waiter_ready(struct wl_listener *listener, void *data) { + struct wlr_linux_drm_syncobj_surface_v1_commit *commit = + wl_container_of(listener, commit, waiter_ready); + surface_commit_destroy(commit); +} + +static void surface_commit_handle_surface_destroy(struct wl_listener *listener, + void *data) { + struct wlr_linux_drm_syncobj_surface_v1_commit *commit = + wl_container_of(listener, commit, surface_destroy); + surface_commit_destroy(commit); +} + +// Block the surface commit until the fence materializes +static bool lock_surface_commit(struct wlr_linux_drm_syncobj_surface_v1 *surface, + struct wlr_drm_syncobj_timeline *timeline, uint64_t point) { + uint32_t flags = DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE; + + bool already_materialized = false; + if (!wlr_drm_syncobj_timeline_check(timeline, point, flags, &already_materialized)) { + return false; + } else if (already_materialized) { + return true; + } + + struct wlr_linux_drm_syncobj_surface_v1_commit *commit = calloc(1, sizeof(*commit)); + if (commit == NULL) { + return false; + } + + struct wl_client *client = wl_resource_get_client(surface->resource); + struct wl_display *display = wl_client_get_display(client); + struct wl_event_loop *loop = wl_display_get_event_loop(display); + if (!wlr_drm_syncobj_timeline_waiter_init(&commit->waiter, timeline, point, + flags, loop)) { + free(commit); + return false; + } + + commit->surface = surface; + commit->cached_seq = wlr_surface_lock_pending(surface->surface); + + commit->waiter_ready.notify = surface_commit_handle_waiter_ready; + wl_signal_add(&commit->waiter.events.ready, &commit->waiter_ready); + + commit->surface_destroy.notify = surface_commit_handle_surface_destroy; + wl_signal_add(&surface->surface->events.destroy, &commit->surface_destroy); + + return true; +} + +static void surface_handle_client_commit(struct wl_listener *listener, + void *data) { + struct wlr_linux_drm_syncobj_surface_v1 *surface = + wl_container_of(listener, surface, client_commit); + + if (surface->pending.acquire_timeline != NULL && + surface->surface->pending.buffer == NULL) { + wlr_surface_reject_pending(surface->surface, surface->resource, + WP_LINUX_DRM_SYNCOBJ_SURFACE_V1_ERROR_NO_BUFFER, + "Acquire point set but no buffer attached"); + return; + } + if (surface->pending.release_timeline != NULL && + surface->surface->pending.buffer == NULL) { + wlr_surface_reject_pending(surface->surface, surface->resource, + WP_LINUX_DRM_SYNCOBJ_SURFACE_V1_ERROR_NO_BUFFER, + "Release point set but no buffer attached"); + return; + } + + if (surface->pending.acquire_timeline == NULL && + surface->surface->pending.buffer != NULL) { + wlr_surface_reject_pending(surface->surface, surface->resource, + WP_LINUX_DRM_SYNCOBJ_SURFACE_V1_ERROR_NO_ACQUIRE_POINT, + "Buffer attached but no acquire point set"); + return; + } + if (surface->pending.release_timeline == NULL && + surface->surface->pending.buffer != NULL) { + wlr_surface_reject_pending(surface->surface, surface->resource, + WP_LINUX_DRM_SYNCOBJ_SURFACE_V1_ERROR_NO_RELEASE_POINT, + "Buffer attached but no release point set"); + return; + } + + if (surface->pending.acquire_timeline != NULL && + surface->pending.release_timeline != NULL && + surface->pending.acquire_timeline == surface->pending.release_timeline && + surface->pending.acquire_point >= surface->pending.release_point) { + wlr_surface_reject_pending(surface->surface, surface->resource, + WP_LINUX_DRM_SYNCOBJ_SURFACE_V1_ERROR_CONFLICTING_POINTS, + "Acquire and release points conflict"); + return; + } + + if (surface->pending.acquire_timeline != NULL && !lock_surface_commit( + surface, surface->pending.acquire_timeline, surface->pending.acquire_point)) { + wl_resource_post_no_memory(surface->resource); + return; + } +} + +static void manager_handle_get_surface(struct wl_client *client, + struct wl_resource *resource, uint32_t id, + struct wl_resource *surface_resource) { + struct wlr_surface *wlr_surface = wlr_surface_from_resource(surface_resource); + + if (surface_from_wlr_surface(wlr_surface) != NULL) { + wl_resource_post_error(resource, + WP_LINUX_DRM_SYNCOBJ_MANAGER_V1_ERROR_SURFACE_EXISTS, + "wp_linux_drm_syncobj_surface_v1 already created for this surface"); + return; + } + + struct wlr_linux_drm_syncobj_surface_v1 *surface = calloc(1, sizeof(*surface)); + if (surface == NULL) { + wl_resource_post_no_memory(resource); + return; + } + + if (!wlr_surface_synced_init(&surface->synced, wlr_surface, + &surface_synced_impl, &surface->pending, &surface->current)) { + goto error_surface; + } + + uint32_t version = wl_resource_get_version(resource); + surface->resource = wl_resource_create(client, + &wp_linux_drm_syncobj_surface_v1_interface, version, id); + if (surface->resource == NULL) { + goto error_surface_synced; + } + wl_resource_set_implementation(surface->resource, + &surface_impl, surface, surface_handle_resource_destroy); + + surface->surface = wlr_surface; + + surface->client_commit.notify = surface_handle_client_commit; + wl_signal_add(&wlr_surface->events.client_commit, &surface->client_commit); + + wlr_addon_init(&surface->addon, &wlr_surface->addons, NULL, &surface_addon_impl); + + return; + +error_surface_synced: + wlr_surface_synced_finish(&surface->synced); +error_surface: + free(surface); + wl_resource_post_no_memory(resource); +} + +static void manager_handle_import_timeline(struct wl_client *client, + struct wl_resource *resource, uint32_t id, int drm_syncobj_fd) { + struct wlr_linux_drm_syncobj_manager_v1 *manager = + manager_from_resource(resource); + + struct wlr_drm_syncobj_timeline *timeline = + wlr_drm_syncobj_timeline_import(manager->drm_fd, drm_syncobj_fd); + close(drm_syncobj_fd); + if (timeline == NULL) { + wl_resource_post_error(resource, + WP_LINUX_DRM_SYNCOBJ_MANAGER_V1_ERROR_INVALID_TIMELINE, + "Failed to import drm_syncobj timeline"); + return; + } + + uint32_t version = wl_resource_get_version(resource); + struct wl_resource *timeline_resource = wl_resource_create(client, + &wp_linux_drm_syncobj_timeline_v1_interface, version, id); + if (timeline_resource == NULL) { + wl_resource_post_no_memory(resource); + return; + } + wl_resource_set_implementation(timeline_resource, + &timeline_impl, timeline, timeline_handle_resource_destroy); +} + +static const struct wp_linux_drm_syncobj_manager_v1_interface manager_impl = { + .destroy = manager_handle_destroy, + .get_surface = manager_handle_get_surface, + .import_timeline = manager_handle_import_timeline, +}; + +static void manager_bind(struct wl_client *client, void *data, + uint32_t version, uint32_t id) { + struct wlr_linux_drm_syncobj_manager_v1 *manager = data; + + struct wl_resource *resource = wl_resource_create(client, + &wp_linux_drm_syncobj_manager_v1_interface, version, id); + if (resource == NULL) { + wl_client_post_no_memory(client); + return; + } + wl_resource_set_implementation(resource, &manager_impl, manager, NULL); +} + +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_linux_drm_syncobj_manager_v1 *manager = + wl_container_of(listener, manager, display_destroy); + wl_list_remove(&manager->display_destroy.link); + wl_global_destroy(manager->global); + close(manager->drm_fd); + free(manager); +} + +static bool check_syncobj_eventfd(int drm_fd) { + if (!HAVE_EVENTFD) { + return false; + } + + uint64_t value = 0; + if (drmGetCap(drm_fd, DRM_CAP_SYNCOBJ_TIMELINE, &value) != 0 || value == 0) { + return false; + } + + // Try waiting with invalid syncobj should fail with ENOENT + struct drm_syncobj_eventfd syncobj_eventfd = { + .handle = 0, + .fd = -1, + }; + return drmIoctl(drm_fd, DRM_IOCTL_SYNCOBJ_EVENTFD, &syncobj_eventfd) != 0 && errno == ENOENT; +} + +struct wlr_linux_drm_syncobj_manager_v1 *wlr_linux_drm_syncobj_manager_v1_create( + struct wl_display *display, uint32_t version, int drm_fd) { + if (!check_syncobj_eventfd(drm_fd)) { + wlr_log(WLR_INFO, "DRM syncobj eventfd unavailable, disabling linux-drm-syncobj-v1"); + return NULL; + } + + struct wlr_linux_drm_syncobj_manager_v1 *manager = calloc(1, sizeof(*manager)); + if (manager == NULL) { + return NULL; + } + + manager->drm_fd = fcntl(drm_fd, F_DUPFD_CLOEXEC, 0); + if (manager->drm_fd < 0) { + goto error_manager; + } + + manager->global = wl_global_create(display, + &wp_linux_drm_syncobj_manager_v1_interface, + LINUX_DRM_SYNCOBJ_V1_VERSION, manager, manager_bind); + if (manager->global == NULL) { + goto error_drm_fd; + } + + manager->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &manager->display_destroy); + + return manager; + +error_drm_fd: + close(manager->drm_fd); +error_manager: + free(manager); + return NULL; +} + +struct wlr_linux_drm_syncobj_surface_v1_state * +wlr_linux_drm_syncobj_v1_get_surface_state(struct wlr_surface *wlr_surface) { + struct wlr_linux_drm_syncobj_surface_v1 *surface = + surface_from_wlr_surface(wlr_surface); + if (surface == NULL) { + return NULL; + } + return &surface->current; +}