diff --git a/backend/libinput/switch.c b/backend/libinput/switch.c index 1e0071255e31859b87e76990100882d3d6ee066e..088d986589073e4698961717d98f4f3adf6b717d 100644 --- a/backend/libinput/switch.c +++ b/backend/libinput/switch.c @@ -38,6 +38,8 @@ static bool switch_type_from_libinput(enum libinput_switch type, enum wlr_switch case LIBINPUT_SWITCH_KEYPAD_SLIDE: *out = WLR_SWITCH_TYPE_KEYPAD_SLIDE; return true; + default: + return; #endif } return false; diff --git a/debian/changelog b/debian/changelog index 6a666418353e8b0e29fb53416243100031d0926c..3ee0614513baedbd22d7fddf5f36f9b033d420fe 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,8 +1,8 @@ -wlroots (0.17.4-ok23) huanghe; urgency=medium +wlroots (0.17.4-ok24) huanghe; urgency=medium - * Update + * feat: 从主线V0.20+回填部分功能 - -- Morose Thu, 20 Aug 2026 15:17:53 +0800 + -- gfc Thu, 24 Sep 2026 18:17:53 +0800 wlroots (0.17.4-ok22) huanghe; urgency=medium diff --git a/include/render/drm_syncobj_merger.h b/include/render/drm_syncobj_merger.h new file mode 100644 index 0000000000000000000000000000000000000000..397c5ce807273e248ccc3073eca4543902911599 --- /dev/null +++ b/include/render/drm_syncobj_merger.h @@ -0,0 +1,64 @@ +#ifndef WLR_RENDER_DRM_SYNCOBJ_MERGER_H +#define WLR_RENDER_DRM_SYNCOBJ_MERGER_H + +#include + +struct wlr_buffer; + +/** + * Accumulate timeline points, to have a destination timeline point be + * signalled when all inputs are + */ +struct wlr_drm_syncobj_merger { + int n_ref; + struct wlr_drm_syncobj_timeline *dst_timeline; + uint64_t dst_point; + int sync_fd; +}; + +/** + * Create a new merger. + * + * The given timeline point will be signalled when all input points are + * signalled and the merger is destroyed. + */ +struct wlr_drm_syncobj_merger *wlr_drm_syncobj_merger_create( + struct wlr_drm_syncobj_timeline *dst_timeline, uint64_t dst_point); + +struct wlr_drm_syncobj_merger *wlr_drm_syncobj_merger_ref( + struct wlr_drm_syncobj_merger *merger); + +/** + * Target timeline point is materialized when all inputs are, and the merger is + * destroyed. + */ +void wlr_drm_syncobj_merger_unref(struct wlr_drm_syncobj_merger *merger); +/** + * Add a new timeline point to wait for. + * + * If the point is not materialized, the supplied event loop is used to schedule + * a wait. + */ +bool wlr_drm_syncobj_merger_add(struct wlr_drm_syncobj_merger *merger, + struct wlr_drm_syncobj_timeline *dst_timeline, uint64_t dst_point, + struct wl_event_loop *loop); + +/** + * Add a new sync file to wait for. + * + * Ownership of fd is transferred to the merger. + */ +bool wlr_drm_syncobj_merger_add_sync_file(struct wlr_drm_syncobj_merger *merger, + int fd); + +/** + * Add a new DMA-BUF release to wait for. + * + * Waits for write access. + * If the platform does not support DMA-BUF<->sync file interop, the supplied + * event_loop is used to schedule a wait. + */ +bool wlr_drm_syncobj_merger_add_dmabuf(struct wlr_drm_syncobj_merger *merger, + struct wlr_buffer *buffer, struct wl_event_loop *event_loop); + +#endif diff --git a/include/wlr/render/drm_syncobj.h b/include/wlr/render/drm_syncobj.h new file mode 100644 index 0000000000000000000000000000000000000000..c7dd3b34cbce99fea84c12f19a00f3cc80cc6809 --- /dev/null +++ b/include/wlr/render/drm_syncobj.h @@ -0,0 +1,138 @@ +#ifndef WLR_RENDER_DRM_SYNCOBJ_H +#define WLR_RENDER_DRM_SYNCOBJ_H + +#include +#include +#include +#include + +/** + * A synchronization timeline. + * + * Timelines are used to synchronize accesses to buffers. Given a producer + * (writing contents to a buffer) and a consumer (reading from the buffer), the + * compositor needs to synchronize back-and-forth between these two users. The + * consumer needs to wait for the producer to signal that they're done with the + * writes, and the producer needs to wait for the consumer to signal that + * they're done with the reads. + * + * Timelines provide synchronization points in the form of monotonically + * increasing 64-bit integer values. + * + * wlroots timelines are designed after Vulkan timeline semaphores. For more + * information on the Vulkan APIs, see: + * https://www.khronos.org/blog/vulkan-timeline-semaphores + * + * wlroots timelines are powered by DRM synchronization objects (drm_syncobj): + * https://dri.freedesktop.org/docs/drm/gpu/drm-mm.html#drm-sync-objects + */ +struct wlr_drm_syncobj_timeline { + int drm_fd; + uint32_t handle; + + struct wlr_addon_set addons; + + struct { + size_t n_refs; + } WLR_PRIVATE; +}; + +struct wlr_drm_syncobj_timeline_waiter; + +typedef void (*wlr_drm_syncobj_timeline_ready_callback)( + struct wlr_drm_syncobj_timeline_waiter *waiter); + +struct wlr_drm_syncobj_timeline_waiter { + struct { + int ev_fd; + struct wl_event_source *event_source; + wlr_drm_syncobj_timeline_ready_callback callback; + } WLR_PRIVATE; +}; + +/** + * Create a new synchronization timeline. + */ +struct wlr_drm_syncobj_timeline *wlr_drm_syncobj_timeline_create(int drm_fd); +/** + * Import a timeline from a drm_syncobj FD. + */ +struct wlr_drm_syncobj_timeline *wlr_drm_syncobj_timeline_import(int drm_fd, + int drm_syncobj_fd); +/** + * Reference a synchronization timeline. + */ +struct wlr_drm_syncobj_timeline *wlr_drm_syncobj_timeline_ref(struct wlr_drm_syncobj_timeline *timeline); +/** + * Unreference a synchronization timeline. + */ +void wlr_drm_syncobj_timeline_unref(struct wlr_drm_syncobj_timeline *timeline); +/** + * Export a drm_syncobj FD from a timeline. + */ +int wlr_drm_syncobj_timeline_export(struct wlr_drm_syncobj_timeline *timeline); +/** + * Transfer a point from a timeline to another. + * + * Both timelines must have been created with the same DRM FD. + */ +bool wlr_drm_syncobj_timeline_transfer(struct wlr_drm_syncobj_timeline *dst, + uint64_t dst_point, struct wlr_drm_syncobj_timeline *src, uint64_t src_point); +/** + * Check if a timeline point has been signalled or has materialized. + * + * Flags can be: + * + * - DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT to wait for the point to be + * signalled + * - DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE to only wait for a fence to + * materialize + */ +bool wlr_drm_syncobj_timeline_check(struct wlr_drm_syncobj_timeline *timeline, + uint64_t point, uint32_t flags, bool *result); +/** + * Signals a timeline point. + */ +bool wlr_drm_syncobj_timeline_signal(struct wlr_drm_syncobj_timeline *timeline, uint64_t point); +/** + * Asynchronously wait for a timeline point. + * + * Flags can be: + * + * - 0 to wait for the point to be signalled + * - DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE to only wait for a fence to + * materialize + * + * A callback must be provided that will be invoked when the waiter has finished. + */ +bool wlr_drm_syncobj_timeline_waiter_init(struct wlr_drm_syncobj_timeline_waiter *waiter, + struct wlr_drm_syncobj_timeline *timeline, uint64_t point, uint32_t flags, + struct wl_event_loop *loop, wlr_drm_syncobj_timeline_ready_callback callback); +/** + * Cancel a timeline waiter. + */ +void wlr_drm_syncobj_timeline_waiter_finish(struct wlr_drm_syncobj_timeline_waiter *waiter); +/** + * Export a timeline point as a sync_file FD. + * + * The returned sync_file will be signalled when the provided point is + * signalled on the timeline. + * + * This allows inter-operation with other APIs which don't support drm_syncobj + * yet. The synchronization point needs to have already materialized: + * wait-before-signal is not supported. + */ +int wlr_drm_syncobj_timeline_export_sync_file(struct wlr_drm_syncobj_timeline *timeline, + uint64_t src_point); +/** + * Import a timeline point from a sync_file FD. + * + * The provided timeline point will be signalled when the provided sync_file is. + * + * This allows inter-operation with other APIs which don't support drm_syncobj + * yet. + */ +bool wlr_drm_syncobj_timeline_import_sync_file(struct wlr_drm_syncobj_timeline *timeline, + uint64_t dst_point, int sync_file_fd); + +#endif 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 0000000000000000000000000000000000000000..dfcbc4d43c80b664782d257393fb78090a7d6d07 --- /dev/null +++ b/include/wlr/types/wlr_linux_drm_syncobj_v1.h @@ -0,0 +1,94 @@ +/* + * 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_buffer; +struct wlr_surface; + +struct wlr_linux_drm_syncobj_surface_v1_state { + struct wlr_drm_syncobj_timeline *acquire_timeline; + uint64_t acquire_point; + + struct { + bool committed; + struct wlr_drm_syncobj_timeline *release_timeline; + uint64_t release_point; + struct wlr_drm_syncobj_merger *release_merger; + } WLR_PRIVATE; +}; + +struct wlr_linux_drm_syncobj_manager_v1 { + struct wl_global *global; + + struct { + int drm_fd; + + struct wl_listener display_destroy; + } WLR_PRIVATE; +}; + +/** + * 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); + +/** + * Signal the release point when wlr_buffer.events.release is emitted. + * + * Compositors unwilling to track fine-grained commit release can call this + * helper on surface commit. + */ +bool wlr_linux_drm_syncobj_v1_state_signal_release_with_buffer( + struct wlr_linux_drm_syncobj_surface_v1_state *state, struct wlr_buffer *buffer); + +/** + * Register a release point for buffer usage. + * + * This function may be called multiple times for the same commit. The client's + * release point will be signalled when all registered points are signalled, and + * a new buffer has been committed. + * + * Because the given release point may not be materialized, a wl_event_loop must + * be supplied to schedule a wait internally, if needed + */ +bool wlr_linux_drm_syncobj_v1_state_add_release_point( + struct wlr_linux_drm_syncobj_surface_v1_state *state, + struct wlr_drm_syncobj_timeline *release_timeline, uint64_t release_point, + struct wl_event_loop *event_loop); + +/** + * Register the DMA-BUF release of a buffer for buffer usage. + * Non-dmabuf buffers are considered to be immediately available (no wait). + * + * This function may be called multiple times for the same commit. The client's + * release point will be signalled when all registered points are signalled, and + * a new buffer has been committed. + * + * Because the platform may not support DMA-BUF fence merges, a wl_event_loop + * must be supplied to schedule a wait internally, if needed + * + * Waits for write access + */ +bool wlr_linux_drm_syncobj_v1_state_add_release_from_implicit_sync( + struct wlr_linux_drm_syncobj_surface_v1_state *state, + struct wlr_buffer *buffer, struct wl_event_loop *event_loop); + +#endif diff --git a/meson.build b/meson.build index 53df65493250a3c8541e4fb28a3de6534b533005..490f5e9d6fed79c787b1468d5c75d95397fa081e 100644 --- a/meson.build +++ b/meson.build @@ -22,6 +22,7 @@ big_endian = target_machine.endian() == 'big' add_project_arguments([ '-DWLR_USE_UNSTABLE', + '-DWLR_PRIVATE=', '-DWLR_LITTLE_ENDIAN=@0@'.format(little_endian.to_int()), '-DWLR_BIG_ENDIAN=@0@'.format(big_endian.to_int()), ], language: 'c') @@ -99,6 +100,13 @@ internal_features = { 'xcb-errors': false, 'egl': false, } + +# Backported for wlr_linux_drm_syncobj_v1 +# 对wlr_linux_drm_syncobj_v1的backport +internal_features += { + 'eventfd': cc.has_function('eventfd', prefix: '#include '), + 'linux_sync_file': cc.has_header('linux/sync_file.h'), +} internal_config = configuration_data() wayland_project_options = ['tests=false', 'documentation=false'] diff --git a/protocol/meson.build b/protocol/meson.build index af0ae03f688b3396639624fbed9f39863a55a77f..4c684c0782d3f0534a19b85393aeab392d8e97a3 100644 --- a/protocol/meson.build +++ b/protocol/meson.build @@ -29,7 +29,8 @@ protocols = { 'xdg-activation-v1': wl_protocol_dir / 'staging/xdg-activation/xdg-activation-v1.xml', 'xwayland-shell-v1': wl_protocol_dir / 'staging/xwayland-shell/xwayland-shell-v1.xml', 'tearing-control-v1': wl_protocol_dir / 'staging/tearing-control/tearing-control-v1.xml', - + 'linux-drm-syncobj-v1': wl_protocol_dir / 'staging/linux-drm-syncobj/linux-drm-syncobj-v1.xml', + # Unstable upstream protocols 'fullscreen-shell-unstable-v1': wl_protocol_dir / 'unstable/fullscreen-shell/fullscreen-shell-unstable-v1.xml', 'idle-inhibit-unstable-v1': wl_protocol_dir / 'unstable/idle-inhibit/idle-inhibit-unstable-v1.xml', diff --git a/render/drm_syncobj.c b/render/drm_syncobj.c new file mode 100644 index 0000000000000000000000000000000000000000..15f71c536fe8eb878b3314c426e71f4504944b83 --- /dev/null +++ b/render/drm_syncobj.c @@ -0,0 +1,249 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "config.h" + +#if HAVE_EVENTFD +#include +#endif + +static struct wlr_drm_syncobj_timeline *timeline_create(int drm_fd, uint32_t handle) { + struct wlr_drm_syncobj_timeline *timeline = calloc(1, sizeof(*timeline)); + if (timeline == NULL) { + return NULL; + } + + timeline->drm_fd = drm_fd; + timeline->n_refs = 1; + timeline->handle = handle; + + wlr_addon_set_init(&timeline->addons); + + return timeline; +} + +struct wlr_drm_syncobj_timeline *wlr_drm_syncobj_timeline_create(int drm_fd) { + uint32_t handle = 0; + if (drmSyncobjCreate(drm_fd, 0, &handle) != 0) { + wlr_log_errno(WLR_ERROR, "drmSyncobjCreate failed"); + return NULL; + } + + struct wlr_drm_syncobj_timeline *timeline = timeline_create(drm_fd, handle); + if (timeline == NULL) { + drmSyncobjDestroy(drm_fd, handle); + } + + return timeline; +} + +struct wlr_drm_syncobj_timeline *wlr_drm_syncobj_timeline_import(int drm_fd, + int drm_syncobj_fd) { + uint32_t handle = 0; + if (drmSyncobjFDToHandle(drm_fd, drm_syncobj_fd, &handle) != 0) { + wlr_log_errno(WLR_ERROR, "drmSyncobjFDToHandle failed"); + return NULL; + } + + struct wlr_drm_syncobj_timeline *timeline = timeline_create(drm_fd, handle); + if (timeline == NULL) { + drmSyncobjDestroy(drm_fd, handle); + } + + return timeline; +} + +struct wlr_drm_syncobj_timeline *wlr_drm_syncobj_timeline_ref(struct wlr_drm_syncobj_timeline *timeline) { + timeline->n_refs++; + return timeline; +} + +void wlr_drm_syncobj_timeline_unref(struct wlr_drm_syncobj_timeline *timeline) { + if (timeline == NULL) { + return; + } + + assert(timeline->n_refs > 0); + timeline->n_refs--; + if (timeline->n_refs > 0) { + return; + } + + wlr_addon_set_finish(&timeline->addons); + drmSyncobjDestroy(timeline->drm_fd, timeline->handle); + free(timeline); +} + +int wlr_drm_syncobj_timeline_export(struct wlr_drm_syncobj_timeline *timeline) { + int drm_syncobj_fd = -1; + if (drmSyncobjHandleToFD(timeline->drm_fd, timeline->handle, &drm_syncobj_fd) != 0) { + wlr_log_errno(WLR_ERROR, "drmSyncobjHandleToFD failed"); + return -1; + } + return drm_syncobj_fd; +} + +bool wlr_drm_syncobj_timeline_transfer(struct wlr_drm_syncobj_timeline *dst, + uint64_t dst_point, struct wlr_drm_syncobj_timeline *src, uint64_t src_point) { + assert(dst->drm_fd == src->drm_fd); + + if (drmSyncobjTransfer(dst->drm_fd, dst->handle, dst_point, + src->handle, src_point, 0) != 0) { + wlr_log_errno(WLR_ERROR, "drmSyncobjTransfer failed"); + return false; + } + + return true; +} + +int wlr_drm_syncobj_timeline_export_sync_file(struct wlr_drm_syncobj_timeline *timeline, + uint64_t src_point) { + int sync_file_fd = -1; + + uint32_t syncobj_handle; + if (drmSyncobjCreate(timeline->drm_fd, 0, &syncobj_handle) != 0) { + wlr_log_errno(WLR_ERROR, "drmSyncobjCreate failed"); + return -1; + } + + if (drmSyncobjTransfer(timeline->drm_fd, syncobj_handle, 0, + timeline->handle, src_point, 0) != 0) { + wlr_log_errno(WLR_ERROR, "drmSyncobjTransfer failed"); + goto out; + } + + if (drmSyncobjExportSyncFile(timeline->drm_fd, + syncobj_handle, &sync_file_fd) != 0) { + wlr_log_errno(WLR_ERROR, "drmSyncobjExportSyncFile failed"); + goto out; + } + +out: + drmSyncobjDestroy(timeline->drm_fd, syncobj_handle); + return sync_file_fd; +} + +bool wlr_drm_syncobj_timeline_import_sync_file(struct wlr_drm_syncobj_timeline *timeline, + uint64_t dst_point, int sync_file_fd) { + bool ok = false; + + uint32_t syncobj_handle; + if (drmSyncobjCreate(timeline->drm_fd, 0, &syncobj_handle) != 0) { + wlr_log_errno(WLR_ERROR, "drmSyncobjCreate failed"); + return false; + } + + if (drmSyncobjImportSyncFile(timeline->drm_fd, syncobj_handle, + sync_file_fd) != 0) { + wlr_log_errno(WLR_ERROR, "drmSyncobjImportSyncFile failed"); + goto out; + } + + if (drmSyncobjTransfer(timeline->drm_fd, timeline->handle, dst_point, + syncobj_handle, 0, 0) != 0) { + wlr_log_errno(WLR_ERROR, "drmSyncobjTransfer failed"); + goto out; + } + + ok = true; + +out: + drmSyncobjDestroy(timeline->drm_fd, syncobj_handle); + return ok; +} + +bool wlr_drm_syncobj_timeline_check(struct wlr_drm_syncobj_timeline *timeline, + uint64_t point, uint32_t flags, bool *result) { + int etime; +#if defined(__FreeBSD__) + etime = ETIMEDOUT; +#else + etime = ETIME; +#endif + + int ret = drmSyncobjTimelineWait(timeline->drm_fd, &timeline->handle, &point, 1, 0, flags, NULL); + if (ret != 0 && ret != -etime) { + wlr_log_errno(WLR_ERROR, "drmSyncobjTimelineWait() failed"); + return false; + } + + *result = ret == 0; + return true; +} + +bool wlr_drm_syncobj_timeline_signal(struct wlr_drm_syncobj_timeline *timeline, uint64_t point) { + if (drmSyncobjTimelineSignal(timeline->drm_fd, &timeline->handle, &point, 1) != 0) { + wlr_log(WLR_ERROR, "drmSyncobjTimelineSignal() failed"); + return false; + } + return true; +} + +static int handle_eventfd_ready(int ev_fd, uint32_t mask, void *data) { + struct wlr_drm_syncobj_timeline_waiter *waiter = data; + + if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) { + wlr_log(WLR_ERROR, "Failed to wait for render timeline: eventfd error"); + } + + if (mask & WL_EVENT_READABLE) { + uint64_t ev_fd_value; + if (read(ev_fd, &ev_fd_value, sizeof(ev_fd_value)) <= 0) { + wlr_log(WLR_ERROR, "Failed to wait for render timeline: read() failed"); + } + } + + waiter->callback(waiter); + return 0; +} + +bool wlr_drm_syncobj_timeline_waiter_init(struct wlr_drm_syncobj_timeline_waiter *waiter, + struct wlr_drm_syncobj_timeline *timeline, uint64_t point, uint32_t flags, + struct wl_event_loop *loop, wlr_drm_syncobj_timeline_ready_callback callback) { + assert(callback); + + int ev_fd; +#if HAVE_EVENTFD + ev_fd = eventfd(0, EFD_CLOEXEC); + if (ev_fd < 0) { + wlr_log_errno(WLR_ERROR, "eventfd() failed"); + } +#else + ev_fd = -1; + wlr_log(WLR_ERROR, "eventfd() is unavailable"); +#endif + if (ev_fd < 0) { + return false; + } + + if (drmSyncobjEventfd(timeline->drm_fd, timeline->handle, point, ev_fd, flags) != 0) { + wlr_log_errno(WLR_ERROR, "drmSyncobjEventfd() failed"); + close(ev_fd); + return false; + } + + struct wl_event_source *source = wl_event_loop_add_fd(loop, ev_fd, WL_EVENT_READABLE, handle_eventfd_ready, waiter); + if (source == NULL) { + wlr_log(WLR_ERROR, "Failed to add FD to event loop"); + close(ev_fd); + return false; + } + + *waiter = (struct wlr_drm_syncobj_timeline_waiter){ + .ev_fd = ev_fd, + .event_source = source, + .callback = callback, + }; + return true; +} + +void wlr_drm_syncobj_timeline_waiter_finish(struct wlr_drm_syncobj_timeline_waiter *waiter) { + wl_event_source_remove(waiter->event_source); + close(waiter->ev_fd); +} diff --git a/render/drm_syncobj_merger.c b/render/drm_syncobj_merger.c new file mode 100644 index 0000000000000000000000000000000000000000..66657b2e14f1e9dbca0b44dd664ff92bd32f61a0 --- /dev/null +++ b/render/drm_syncobj_merger.c @@ -0,0 +1,197 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "render/dmabuf.h" +#include "render/drm_syncobj_merger.h" + +#include "config.h" + +#if HAVE_LINUX_SYNC_FILE + +#include +#include + +static int sync_file_merge(int fd1, int fd2) { + // The kernel will automatically prune signalled fences + struct sync_merge_data merge_data = { .fd2 = fd2 }; + if (ioctl(fd1, SYNC_IOC_MERGE, &merge_data) < 0) { + wlr_log_errno(WLR_ERROR, "ioctl(SYNC_IOC_MERGE) failed"); + return -1; + } + + return merge_data.fence; +} + +#else + +static int sync_file_merge(int fd1, int fd2) { + wlr_log(WLR_ERROR, "sync_file support is unavailable"); + return -1; +} + +#endif + +struct wlr_drm_syncobj_merger *wlr_drm_syncobj_merger_create( + struct wlr_drm_syncobj_timeline *dst_timeline, uint64_t dst_point) { + struct wlr_drm_syncobj_merger *merger = calloc(1, sizeof(*merger)); + if (merger == NULL) { + return NULL; + } + merger->n_ref = 1; + merger->dst_timeline = wlr_drm_syncobj_timeline_ref(dst_timeline); + merger->dst_point = dst_point; + merger->sync_fd = -1; + return merger; +} + +struct wlr_drm_syncobj_merger *wlr_drm_syncobj_merger_ref( + struct wlr_drm_syncobj_merger *merger) { + assert(merger->n_ref > 0); + merger->n_ref++; + return merger; +} + +void wlr_drm_syncobj_merger_unref(struct wlr_drm_syncobj_merger *merger) { + if (merger == NULL) { + return; + } + assert(merger->n_ref > 0); + merger->n_ref--; + if (merger->n_ref > 0) { + return; + } + + if (merger->sync_fd != -1) { + wlr_drm_syncobj_timeline_import_sync_file(merger->dst_timeline, + merger->dst_point, merger->sync_fd); + close(merger->sync_fd); + } else { + wlr_drm_syncobj_timeline_signal(merger->dst_timeline, merger->dst_point); + } + wlr_drm_syncobj_timeline_unref(merger->dst_timeline); + free(merger); +} + +static bool merger_add_exportable(struct wlr_drm_syncobj_merger *merger, + struct wlr_drm_syncobj_timeline *src_timeline, uint64_t src_point) { + int new_sync = wlr_drm_syncobj_timeline_export_sync_file(src_timeline, src_point); + return wlr_drm_syncobj_merger_add_sync_file(merger, new_sync); +} + +struct export_waiter { + struct wlr_drm_syncobj_timeline_waiter waiter; + struct wlr_drm_syncobj_merger *merger; + struct wlr_drm_syncobj_timeline *src_timeline; + uint64_t src_point; +}; + +static void export_waiter_handle_ready(struct wlr_drm_syncobj_timeline_waiter *waiter) { + struct export_waiter *add = wl_container_of(waiter, add, waiter); + merger_add_exportable(add->merger, add->src_timeline, add->src_point); + wlr_drm_syncobj_merger_unref(add->merger); + wlr_drm_syncobj_timeline_unref(add->src_timeline); + wlr_drm_syncobj_timeline_waiter_finish(&add->waiter); + free(add); +} + +bool wlr_drm_syncobj_merger_add(struct wlr_drm_syncobj_merger *merger, + struct wlr_drm_syncobj_timeline *src_timeline, uint64_t src_point, + struct wl_event_loop *loop) { + assert(loop != NULL); + bool exportable = false; + int flags = DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE; + if (!wlr_drm_syncobj_timeline_check(src_timeline, src_point, flags, &exportable)) { + return false; + } + if (exportable) { + return merger_add_exportable(merger, src_timeline, src_point); + } + struct export_waiter *add = calloc(1, sizeof(*add)); + if (add == NULL) { + return false; + } + if (!wlr_drm_syncobj_timeline_waiter_init(&add->waiter, src_timeline, src_point, + flags, loop, export_waiter_handle_ready)) { + free(add); + return false; + } + add->merger = merger; + add->src_timeline = wlr_drm_syncobj_timeline_ref(src_timeline); + add->src_point = src_point; + merger->n_ref++; + return true; +} + +bool wlr_drm_syncobj_merger_add_sync_file(struct wlr_drm_syncobj_merger *merger, + int fd) { + int new_sync = fd; + if (merger->sync_fd != -1) { + new_sync = sync_file_merge(merger->sync_fd, fd); + close(fd); + if (new_sync < 0) { + return false; + } + close(merger->sync_fd); + } + merger->sync_fd = new_sync; + return merger->sync_fd != -1; +} + +struct poll_waiter { + struct wl_event_source *event_source; + struct wlr_drm_syncobj_merger *merger; +}; + +static int poll_waiter_handle_done(int fd, uint32_t mask, void *data) { + struct poll_waiter *waiter = data; + wlr_drm_syncobj_merger_unref(waiter->merger); + wl_event_source_remove(waiter->event_source); + free(waiter); + return 0; +} + +bool wlr_drm_syncobj_merger_add_dmabuf(struct wlr_drm_syncobj_merger *merger, + struct wlr_buffer *buffer, struct wl_event_loop *event_loop) { + struct wlr_dmabuf_attributes dmabuf_attributes; + if (!wlr_buffer_get_dmabuf(buffer, &dmabuf_attributes)) { + return true; + } + + bool res = true; + for (int i = 0; i < dmabuf_attributes.n_planes; ++i) { + int sync_fd = dmabuf_export_sync_file(dmabuf_attributes.fd[i], DMA_BUF_SYNC_WRITE); + if (sync_fd == -1) { + res = false; + break; + } + if (!wlr_drm_syncobj_merger_add_sync_file(merger, sync_fd)) { + return false; + } + } + + if (res) { + return true; + } + + for (int i = 0; i < dmabuf_attributes.n_planes; ++i) { + struct poll_waiter *waiter = calloc(1, sizeof(*waiter)); + if (waiter == NULL) { + return false; + } + waiter->merger = wlr_drm_syncobj_merger_ref(merger); + waiter->event_source = wl_event_loop_add_fd(event_loop, + dmabuf_attributes.fd[i], WL_EVENT_WRITABLE, poll_waiter_handle_done, waiter); + if (waiter->event_source == NULL) { + wlr_drm_syncobj_merger_unref(waiter->merger); + free(waiter); + return false; + } + } + return true; +} diff --git a/render/meson.build b/render/meson.build index f09905c716e35cd53370fa6565e38892767d5e2b..87db3494ec72f95785f05dec0b8be1d320994859 100644 --- a/render/meson.build +++ b/render/meson.build @@ -7,6 +7,8 @@ endif wlr_files += files( 'dmabuf.c', + 'drm_syncobj.c', + 'drm_syncobj_merger.c', 'drm_format_set.c', 'pass.c', 'pixel_format.c', diff --git a/types/meson.build b/types/meson.build index ba41e3e27f882bfd8b6bac17d2f8cd3925fa1384..6de04a10a7994bd69a18cd49b9dd63f909ffd0dc 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 0000000000000000000000000000000000000000..d44f80357d82637e60781abea1f96fc73b429865 --- /dev/null +++ b/types/wlr_linux_drm_syncobj_v1.c @@ -0,0 +1,462 @@ +/** + * Backported from upstream wlroots mainline for GXDE's forked version of + * Open Kylin wlroots 0.17.4-ok*. + * + * Things adapted is that 0.17 has NO wlr_surface_synced framework so the + * pending -> current state is now moved MANUALLY in a surface 「commit」 + * listener. Wait-before-signal blocking (lock_surface_commit) is dropped here. + * + * The acquire fence is waited on at sample time by the compositor instead. + * + * wlr_surface_reject_pending -> post_error. + * ---------------------------------------------------------------------------- + * 从上游wlroots主线的回填,用于GXDE版本的Open Kylin wlroots 0.17.4-ok*分支。 + * + * 适配内容:0.17中*没有* wlr_surface_synced框架,因此pending -> current的状态迁移 + * 现在改为在surface的commit listener中手动完成。 + * + * 此处移除了wait-before-signal的阻塞逻辑 (lock_surface_commit). + * + * acquire fence改为由合成器在采样时等待。 + * + * wlr_surface_reject_pending改为post_error。 + */ + +#define _POSIX_C_SOURCE 200809L + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "config.h" +#include "linux-drm-syncobj-v1-protocol.h" +#include "render/dmabuf.h" +#include "render/drm_syncobj_merger.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 wl_listener surface_commit; + 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 state_finish(struct wlr_linux_drm_syncobj_surface_v1_state *state) { + wlr_drm_syncobj_timeline_unref(state->acquire_timeline); + wlr_drm_syncobj_timeline_unref(state->release_timeline); + wlr_drm_syncobj_merger_unref(state->release_merger); + *state = (struct wlr_linux_drm_syncobj_surface_v1_state){0}; +} + +static void surface_destroy(struct wlr_linux_drm_syncobj_surface_v1 *surface) { + if (surface == NULL) { + return; + } + wl_list_remove(&surface->surface_commit.link); + wl_list_remove(&surface->surface_destroy.link); + wlr_addon_finish(&surface->addon); + state_finish(&surface->pending); + state_finish(&surface->current); + 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 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; +} + +// 0.17 has no wlr_surface_synced; move pending->current manually on commit. +static void surface_handle_commit(struct wl_listener *listener, void *data) { + struct wlr_linux_drm_syncobj_surface_v1 *surface = + wl_container_of(listener, surface, surface_commit); + + // Ignore commits that did not set an acquire point (i.e. did not attach a + // buffer through explicit sync this commit). + if (surface->pending.acquire_timeline == NULL) { + state_finish(&surface->pending); + return; + } + + state_finish(&surface->current); + surface->current = surface->pending; + surface->pending = (struct wlr_linux_drm_syncobj_surface_v1_state){0}; + + if (surface->current.release_timeline != NULL) { + surface->current.release_merger = wlr_drm_syncobj_merger_create( + surface->current.release_timeline, surface->current.release_point); + } + + // The acquire fence is waited on GPU-side by the renderer right before it + // samples this surface's buffer (see ky_scene get_texture path), not here. + // Release-point signalling is handled after the compositor's GPU read. +} + +static void surface_handle_surface_destroy(struct wl_listener *listener, void *data) { + struct wlr_linux_drm_syncobj_surface_v1 *surface = + wl_container_of(listener, surface, surface_destroy); + surface_destroy(surface); +} + +static void manager_handle_destroy(struct wl_client *client, + struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +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; + } + + 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) { + free(surface); + wl_resource_post_no_memory(resource); + return; + } + wl_resource_set_implementation(surface->resource, + &surface_impl, surface, surface_handle_resource_destroy); + + surface->surface = wlr_surface; + + wlr_log(WLR_INFO, "KYDBG syncobj: client created syncobj surface for %p", + (void *)wlr_surface); + + surface->surface_commit.notify = surface_handle_commit; + wl_signal_add(&wlr_surface->events.precommit, &surface->surface_commit); + + surface->surface_destroy.notify = surface_handle_surface_destroy; + wl_signal_add(&wlr_surface->events.destroy, &surface->surface_destroy); + + wlr_addon_init(&surface->addon, &wlr_surface->addons, NULL, &surface_addon_impl); +} + +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) { + wlr_drm_syncobj_timeline_unref(timeline); + 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) { + assert(version <= LINUX_DRM_SYNCOBJ_V1_VERSION); + + if (!HAVE_LINUX_SYNC_FILE) { + wlr_log(WLR_INFO, "Linux sync_file unavailable, disabling linux-drm-syncobj-v1"); + return NULL; + } + + 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, + 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); + static int nq = 0; + if (nq++ < 60) + wlr_log(WLR_INFO, "KYDBG get_surface_state surf=%p found=%p", + (void*)wlr_surface, (void*)surface); + if (surface == NULL) { + return NULL; + } + return &surface->current; +} + +struct release_signaller { + struct wlr_drm_syncobj_merger *merger; + struct wl_listener buffer_release; +}; + +static void release_signaller_handle_buffer_release(struct wl_listener *listener, void *data) { + struct release_signaller *signaller = wl_container_of(listener, signaller, buffer_release); + + wlr_drm_syncobj_merger_unref(signaller->merger); + wl_list_remove(&signaller->buffer_release.link); + free(signaller); +} + +bool wlr_linux_drm_syncobj_v1_state_signal_release_with_buffer( + struct wlr_linux_drm_syncobj_surface_v1_state *state, struct wlr_buffer *buffer) { + assert(buffer->n_locks > 0); + if (state->release_merger == NULL) { + return true; + } + + struct release_signaller *signaller = calloc(1, sizeof(*signaller)); + if (signaller == NULL) { + return false; + } + + signaller->merger = wlr_drm_syncobj_merger_ref(state->release_merger); + signaller->buffer_release.notify = release_signaller_handle_buffer_release; + wl_signal_add(&buffer->events.release, &signaller->buffer_release); + + return true; +} + +bool wlr_linux_drm_syncobj_v1_state_add_release_point( + struct wlr_linux_drm_syncobj_surface_v1_state *state, + struct wlr_drm_syncobj_timeline *release_timeline, uint64_t release_point, + struct wl_event_loop *event_loop) { + if (state->release_merger == NULL) { + return true; + } + return wlr_drm_syncobj_merger_add(state->release_merger, + release_timeline, release_point, event_loop); +} + +bool wlr_linux_drm_syncobj_v1_state_add_release_from_implicit_sync( + struct wlr_linux_drm_syncobj_surface_v1_state *state, + struct wlr_buffer *buffer, struct wl_event_loop *event_loop) { + if (state->release_merger == NULL) { + return true; + } + return wlr_drm_syncobj_merger_add_dmabuf(state->release_merger, buffer, event_loop); +}