AtlatestRepositorysigil-ui

sigil-ui / tree / nativewayland-dmabuf-import.c

1/* Included by wayland.c. Imports are connection resources, not presentations. */
2#define DMABUF_IMPORT_LIMIT 64u
3
4struct SuiWaylandDmabufImport {
5 unsigned references;
6 Wl *owner;
7 struct SuiWaylandDmabufImport *next;
8 struct zwp_linux_buffer_params_v1 *params;
9 struct wl_buffer *buffer;
10 SuiWaylandDmabufSpec spec;
11 int status, protocol_ref, cancel_requested, busy, listening;
12 SuiWaylandDmabufRelease released;
13 void *release_data;
14 int fence_fd;
15 int64_t fence_deadline;
16};
18static void dmabuf_import_resources_cancel(SuiWaylandDmabufImport *buffer)
20 if (buffer->params) zwp_linux_buffer_params_v1_destroy(buffer->params);
21 if (buffer->buffer) wl_buffer_destroy(buffer->buffer);
22 if (buffer->fence_fd >= 0) close(buffer->fence_fd);
23 buffer->fence_fd = -1;
24 buffer->params = NULL;
25 buffer->buffer = NULL;
26 buffer->status = SUI_DMABUF_IMPORT_CANCELED;
29void sui_wayland_dmabuf_import_cancel(SuiWaylandDmabufImport *buffer)
31 if (!buffer) return;
32 buffer->cancel_requested = 1;
33 buffer->status = SUI_DMABUF_IMPORT_CANCELED;
34 /* Keep pending params alive to receive and destroy a late created
35 * wl_buffer. Dropping that listener could orphan a server allocation. */
36 if (!buffer->params && !buffer->busy) dmabuf_import_resources_cancel(buffer);
39SuiWaylandDmabufImport *sui_wayland_dmabuf_import_ref(SuiWaylandDmabufImport *buffer)
41 if (buffer) buffer->references++;
42 return buffer;
45void sui_wayland_dmabuf_import_unref(SuiWaylandDmabufImport *buffer)
47 if (!buffer) return;
48 if (--buffer->references) {
49 if (buffer->references == 1 && (buffer->protocol_ref || buffer->busy))
50 sui_wayland_dmabuf_import_cancel(buffer);
51 return;
52 }
53 Wl *owner = buffer->owner;
54 if (owner) {
55 SuiWaylandDmabufImport **link = &owner->dmabuf_imports;
56 while (*link && *link != buffer) link = &(*link)->next;
57 if (*link) *link = buffer->next;
58 }
59 dmabuf_import_resources_cancel(buffer);
60 free(buffer);
61 if (owner) sui_wayland_unref(owner);
64int sui_wayland_dmabuf_import_status(const SuiWaylandDmabufImport *buffer)
66 if (!buffer || !buffer->owner || !buffer->owner->display ||
67 buffer->owner->dead || buffer->owner->closed) return SUI_DMABUF_IMPORT_CANCELED;
68 return buffer->status;
71int sui_wayland_dmabuf_import_fence(SuiWaylandDmabufImport *buffer,
72 int fd, unsigned timeout_ms)
74 if (!buffer || buffer->busy || fd < 0 || !timeout_ms || timeout_ms > 5000 ||
75 sui_wayland_dmabuf_import_status(buffer) != SUI_DMABUF_IMPORT_READY) {
76 errno = EINVAL;
77 return 0;
78 }
79 int owned_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
80 if (owned_fd < 0) return 0;
81 buffer->fence_fd = owned_fd;
82 buffer->fence_deadline = now_ms() + timeout_ms;
83 buffer->status = SUI_DMABUF_IMPORT_FENCE_PENDING;
84 return 1;
87static int dmabuf_imports_advance_fences(Wl *owner)
89 int changed = 0;
90 for (SuiWaylandDmabufImport *buffer = owner->dmabuf_imports; buffer; buffer = buffer->next) {
91 if (buffer->fence_fd < 0) continue;
92 struct pollfd ready = {buffer->fence_fd, POLLIN, 0};
93 int status;
94 if (now_ms() >= buffer->fence_deadline) status = SUI_DMABUF_IMPORT_FENCE_TIMED_OUT;
95 else {
96 int result = poll(&ready, 1, 0);
97 if (result < 0 && errno == EINTR) continue;
98 if (result < 0 || (ready.revents & (POLLERR | POLLHUP | POLLNVAL)))
99 status = SUI_DMABUF_IMPORT_FENCE_FAILED;
100 else if (ready.revents & POLLIN) status = SUI_DMABUF_IMPORT_READY;
101 else continue;
102 }
103 close(buffer->fence_fd);
104 buffer->fence_fd = -1;
105 if (status != SUI_DMABUF_IMPORT_READY) {
106 wl_buffer_destroy(buffer->buffer);
107 buffer->buffer = NULL;
108 }
109 buffer->status = status;
110 changed++;
111 }
112 return changed;
115static void dmabuf_imports_wait_fences(Wl *owner, struct pollfd *fds,
116 nfds_t *count, int64_t *wait)
118 for (SuiWaylandDmabufImport *buffer = owner->dmabuf_imports; buffer; buffer = buffer->next) {
119 if (buffer->fence_fd < 0) continue;
120 fds[(*count)++] = (struct pollfd){buffer->fence_fd, POLLIN, 0};
121 int64_t remaining = buffer->fence_deadline - now_ms();
122 if (remaining < 0) remaining = 0;
123 if (*wait < 0 || remaining < *wait) *wait = remaining;
124 }
127static void dmabuf_import_created(void *data, struct zwp_linux_buffer_params_v1 *params,
128 struct wl_buffer *native_buffer)
130 SuiWaylandDmabufImport *buffer = data;
131 if (buffer->cancel_requested) wl_buffer_destroy(native_buffer);
132 else {
133 buffer->buffer = native_buffer;
134 buffer->status = SUI_DMABUF_IMPORT_READY;
135 }
136 buffer->params = NULL;
137 buffer->protocol_ref = 0;
138 zwp_linux_buffer_params_v1_destroy(params);
139 sui_wayland_dmabuf_import_unref(buffer);
142static void dmabuf_import_failed(void *data, struct zwp_linux_buffer_params_v1 *params)
144 SuiWaylandDmabufImport *buffer = data;
145 buffer->status = buffer->cancel_requested ? SUI_DMABUF_IMPORT_CANCELED : SUI_DMABUF_IMPORT_FAILED;
146 buffer->params = NULL;
147 buffer->protocol_ref = 0;
148 zwp_linux_buffer_params_v1_destroy(params);
149 sui_wayland_dmabuf_import_unref(buffer);
152static const struct zwp_linux_buffer_params_v1_listener dmabuf_import_listener = {
153 .created = dmabuf_import_created, .failed = dmabuf_import_failed
154};
156static void dmabuf_imports_revoke_pending(Wl *owner)
158 /* Factory removal does not invalidate already-created wl_buffer objects. */
159 for (SuiWaylandDmabufImport *buffer = owner->dmabuf_imports; buffer; buffer = buffer->next)
160 if (buffer->params) sui_wayland_dmabuf_import_cancel(buffer);
163static void dmabuf_import_release(void *data, struct wl_buffer *native_buffer)
165 SuiWaylandDmabufImport *buffer = data;
166 if (!buffer->busy) return;
167 SuiWaylandDmabufRelease notify = buffer->released;
168 void *release_data = buffer->release_data;
169 buffer->busy = 0;
170 buffer->released = NULL;
171 buffer->release_data = NULL;
172 if (buffer->cancel_requested) dmabuf_import_resources_cancel(buffer);
173 /* The submission reference survives callback-owned teardown/unref. */
174 if (notify) notify(release_data, 1);
175 sui_wayland_dmabuf_import_unref(buffer);
178static const struct wl_buffer_listener dmabuf_import_buffer_listener = {
179 .release = dmabuf_import_release
180};
182static SuiWaylandDmabufImport *dmabuf_imports_close(Wl *owner)
184 SuiWaylandDmabufImport *list = owner->dmabuf_imports;
185 owner->dmabuf_imports = NULL;
186 /* Retain the detached list until disconnect finishes. No canceled GPU
187 * release callback may run while this connection can still use a buffer. */
188 for (SuiWaylandDmabufImport *buffer = list; buffer; buffer = buffer->next) {
189 sui_wayland_dmabuf_import_ref(buffer);
190 buffer->owner = NULL;
191 dmabuf_import_resources_cancel(buffer);
192 int protocol_ref = buffer->protocol_ref;
193 buffer->protocol_ref = 0;
194 sui_wayland_unref(owner);
195 if (protocol_ref) sui_wayland_dmabuf_import_unref(buffer);
196 }
197 return list;
200static void dmabuf_imports_disconnected(SuiWaylandDmabufImport *list)
202 while (list) {
203 SuiWaylandDmabufImport *buffer = list;
204 list = buffer->next;
205 buffer->next = NULL;
206 if (buffer->busy) {
207 SuiWaylandDmabufRelease notify = buffer->released;
208 void *data = buffer->release_data;
209 buffer->busy = 0;
210 buffer->released = NULL;
211 buffer->release_data = NULL;
212 if (notify) notify(data, 0);
213 sui_wayland_dmabuf_import_unref(buffer);
214 }
215 sui_wayland_dmabuf_import_unref(buffer);
216 }
219/* Bound a plane with the same checks a compositor makes on import: a plane
220 * that fails them ends the whole connection with an out_of_bounds protocol
221 * error. Planes after the first may be subsampled, so only the first is
222 * checked against the full height. A span beyond 32 bits is refused before
223 * any size query, as compositors do; the rest is checked against the
224 * DMA-BUF's real size. A descriptor that cannot report its size (a pipe or
225 * a socket) leaves that part to the compositor, which skips it too.
226 * dma_buf_llseek never moves the file position, so the restore below only
227 * matters for other files, such as a memfd. */
228static int dmabuf_plane_fits(const SuiWaylandDmabufSpec *spec, uint32_t plane)
230 uint64_t offset = spec->planes[plane].offset, stride = spec->planes[plane].stride;
231 int fd = spec->planes[plane].fd;
232 /* offset and stride are 32-bit and height is at most 16384, so none of
233 * these sums or products can overflow 64 bits. */
234 if (offset + stride > UINT32_MAX ||
235 (plane == 0 && offset + stride * (uint64_t)spec->height > UINT32_MAX)) return 0;
236 off_t position = lseek(fd, 0, SEEK_CUR);
237 off_t end = lseek(fd, 0, SEEK_END);
238 if (end < 0) return 1;
239 lseek(fd, position < 0 ? 0 : position, SEEK_SET);
240 uint64_t size = (uint64_t)end;
241 if (offset > size || offset + stride > size) return 0;
242 return plane > 0 || offset + stride * (uint64_t)spec->height <= size;
245SuiWaylandDmabufImport *sui_wayland_dmabuf_import(SuiWaylandSurface *surface,
246 const SuiWaylandDmabufSpec *spec)
248 if (!surface || !surface->display || surface->closed || surface->dead || !spec ||
249 spec->width < 1 || spec->width > 16384 || spec->height < 1 || spec->height > 16384 ||
250 !spec->n_planes || spec->n_planes > 4 || !spec->fourcc) { errno = EINVAL; return NULL; }
251 for (uint32_t i = 0; i < spec->n_planes; i++) {
252 if (spec->planes[i].fd < 0 || !spec->planes[i].stride) { errno = EINVAL; return NULL; }
253 /* Validate all borrowed descriptors before duplicates can reuse a
254 * closed descriptor number appearing later in the input array. */
255 if (fcntl(spec->planes[i].fd, F_GETFD) < 0) return NULL;
256 if (!dmabuf_plane_fits(spec, i)) { errno = EINVAL; return NULL; }
257 }
258 Wl *owner = surface;
259 while (owner->parent) owner = owner->parent;
260 if (!owner->display || owner->closed || owner->dead) { errno = EPIPE; return NULL; }
261 if (!owner->dmabuf_global || owner->dmabuf_version < 4) { errno = ENOTSUP; return NULL; }
262 unsigned count = 0;
263 for (SuiWaylandDmabufImport *entry = owner->dmabuf_imports; entry; entry = entry->next) count++;
264 if (count >= DMABUF_IMPORT_LIMIT) { errno = ENOSPC; return NULL; }
265 int descriptors[4] = {-1, -1, -1, -1};
266 for (uint32_t i = 0; i < spec->n_planes; i++) {
267 descriptors[i] = fcntl(spec->planes[i].fd, F_DUPFD_CLOEXEC, 0);
268 if (descriptors[i] < 0) {
269 int error = errno;
270 for (uint32_t j = 0; j < i; j++) close(descriptors[j]);
271 errno = error;
272 return NULL;
273 }
274 }
275 SuiWaylandDmabufImport *buffer = calloc(1, sizeof *buffer);
276 if (buffer) {
277 buffer->fence_fd = -1;
278 buffer->references = 1;
279 buffer->spec = *spec;
280 for (unsigned i = 0; i < 4; i++) buffer->spec.planes[i].fd = -1;
281 if (!owner->dmabuf)
282 owner->dmabuf = wl_registry_bind(owner->registry, owner->dmabuf_global, &zwp_linux_dmabuf_v1_interface, 4);
283 if (owner->dmabuf) buffer->params = zwp_linux_dmabuf_v1_create_params(owner->dmabuf);
284 if (buffer->params && zwp_linux_buffer_params_v1_add_listener(buffer->params, &dmabuf_import_listener, buffer) == 0) {
285 for (uint32_t i = 0; i < spec->n_planes; i++)
286 zwp_linux_buffer_params_v1_add(buffer->params, descriptors[i], i, spec->planes[i].offset,
287 spec->planes[i].stride, (uint32_t)(spec->modifier >> 32), (uint32_t)spec->modifier);
288 zwp_linux_buffer_params_v1_create(buffer->params, spec->width, spec->height, spec->fourcc, 0);
289 buffer->owner = owner;
290 buffer->references++;
291 buffer->protocol_ref = 1;
292 owner->references++;
293 buffer->next = owner->dmabuf_imports;
294 owner->dmabuf_imports = buffer;
295 buffer->status = SUI_DMABUF_IMPORT_PENDING;
296 } else {
297 if (buffer->params) zwp_linux_buffer_params_v1_destroy(buffer->params);
298 free(buffer);
299 buffer = NULL;
300 }
301 }
302 for (uint32_t i = 0; i < spec->n_planes; i++) close(descriptors[i]);
303 if (!buffer) errno = ENOMEM;
304 return buffer;