AtlatestRepositorysigil-ui

sigil-ui / tree / test / waylanddmabuf-present-peer.c

1/* Synthetic wire ownership controls, not GPU import or pixel qualification. */
2#define main import_ownership_main
3#include "dmabuf-import-peer.c"
4#undef main
5
6typedef struct {
7 Wl *root, *child;
8 SuiWaylandDmabufImport *buffer;
9 unsigned released, retired, frames, canceled;
10 int callback_owned, release_unref, release_close_child, frame_unref;
11 int parent_close_kind, display_fd;
12} PresentResult;
14static void close_parent_from_callback(PresentResult *result)
16 Wl *root = result->root, *child = result->child;
17 SuiWaylandDmabufImport *buffer = result->buffer;
18 sui_wayland_close(root);
19 /* The actual listener is still executing. Disconnecting here frees the
20 * libwayland display/queue before its dispatcher has finished with it. */
21 assert(root->display && root->closed);
22 if (result->parent_close_kind == 3) {
23 assert(dispatch(root, 0) >= 0);
24 assert(root->display);
25 }
26 assert(!sui_wayland_present_dmabuf(child, buffer, NULL, NULL, NULL));
27 sui_wayland_close(child);
28 result->root = result->child = NULL;
29 result->buffer = NULL;
30 sui_wayland_dmabuf_import_unref(buffer);
31 sui_wayland_unref(child);
32 sui_wayland_unref(root);
35static void present_release(void *data, int released)
37 PresentResult *result = data;
38 if (released) result->released++;
39 else {
40 if (result->root) assert(!result->root->display);
41 else assert(fcntl(result->display_fd, F_GETFD) == -1 && errno == EBADF);
42 result->retired++;
43 }
44 if (released && (result->parent_close_kind == 1 || result->parent_close_kind == 3))
45 close_parent_from_callback(result);
46 if (result->callback_owned || result->release_unref) {
47 SuiWaylandDmabufImport *buffer = result->buffer;
48 result->buffer = NULL;
49 sui_wayland_dmabuf_import_unref(buffer);
50 }
51 if (result->callback_owned) {
52 Wl *root = result->root;
53 result->root = NULL;
54 sui_wayland_unref(root);
55 }
56 if (result->release_close_child) {
57 Wl *child = result->child;
58 result->child = NULL;
59 sui_wayland_unref(child);
60 }
63static void present_frame(void *data, uint32_t time, int presented)
65 PresentResult *result = data;
66 if (presented) { assert(time == 1234); result->frames++; }
67 else { assert(time == 0); result->canceled++; }
68 if ((presented && result->parent_close_kind == 2) ||
69 (!presented && result->parent_close_kind == 4)) close_parent_from_callback(result);
70 if (result->frame_unref) {
71 SuiWaylandDmabufImport *buffer = result->buffer;
72 result->buffer = NULL;
73 sui_wayland_dmabuf_import_unref(buffer);
74 }
75 if (result->callback_owned) {
76 Wl *child = result->child;
77 result->child = NULL;
78 sui_wayland_unref(child);
79 }
82static void refused_release(void *data, int released) { abort(); }
83static void refused_frame(void *data, uint32_t time, int presented) { abort(); }
84static void close_sync_done(void *data, struct wl_callback *callback, uint32_t serial)
86 wl_callback_destroy(callback);
88static const struct wl_callback_listener close_sync_listener = {.done = close_sync_done};
90static int submit(PresentResult *result)
92 return sui_wayland_present_dmabuf(result->child, result->buffer,
93 present_release, present_frame, result);
96int main(int argc, char **argv)
98 assert(argc == 2);
99 const char *mode = argv[1];
100 struct rlimit no_core = {0, 0};
101 assert(setrlimit(RLIMIT_CORE, &no_core) == 0);
102 signal(SIGPIPE, SIG_IGN);
103 ImportStats *stats = mmap(NULL, sizeof *stats, PROT_READ | PROT_WRITE,
104 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
105 assert(stats != MAP_FAILED);
106 int sockets[2];
107 assert(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == 0);
108 pid_t peer = fork();
109 assert(peer >= 0);
110 if (!peer) {
111 close(sockets[0]);
112 int status = import_serve(sockets[1], mode, stats);
113 if (status == 2 && (errno == EPIPE || errno == ECONNRESET)) status = 0;
114 _exit(status);
115 }
116 close(sockets[1]);
117 char name[32], error[256];
118 snprintf(name, sizeof name, "%d", sockets[0]);
119 assert(setenv("WAYLAND_SOCKET", name, 1) == 0 && load_libraries(error, sizeof error));
120 Wl *root = calloc(1, sizeof *root);
121 assert(root);
122 root->references = root->configured = root->scale = 1;
123 root->width = 640;
124 root->height = 480;
125 for (unsigned i = 0; i < CLIPBOARD_WRITES; i++) root->writes[i].fd = -1;
126 root->display = wl_display_connect(NULL);
127 assert(root->display);
128 root->registry = wl_display_get_registry(root->display);
129 assert(wl_registry_add_listener(root->registry, &registry_listener, root) == 0);
130 assert(wl_display_roundtrip(root->display) >= 0);
131 root->surface = wl_compositor_create_surface(root->compositor);
132 Wl *child = sui_wayland_child(root, 0, 0, 16, 16, 0);
133 assert(child && wl_display_roundtrip(root->display) >= 0);
134 if (!strcmp(mode, "p-scale2")) child->scale = 2;
135 int size = !strcmp(mode, "p-scale2") ? 32 : 16;
136 int plane = memfd_create("present-fixture", MFD_CLOEXEC);
137 uint32_t marker = 0x22334455;
138 assert(plane >= 0 && ftruncate(plane, 4096) == 0 && pwrite(plane, &marker, 4, 0) == 4);
139 unsigned descriptors = import_fd_count();
140 SuiWaylandDmabufSpec spec = {.width = size, .height = size, .fourcc = 0x34325241,
141 .n_planes = 1, .modifier = 0x0100000000000001, .planes = {{plane, 0, (uint32_t)size * 4}}};
142 PresentResult result = {.root = root, .child = child,
143 .callback_owned = !strcmp(mode, "p-callback-owned"),
144 .release_unref = !strcmp(mode, "p-release-unref"),
145 .release_close_child = !strcmp(mode, "p-release-close-child"),
146 .frame_unref = !strcmp(mode, "p-frame-unref"),
147 .parent_close_kind = (!strcmp(mode, "p-release-close-parent") || !strcmp(mode, "p-release-child-dispatch")) ? 1 :
148 (!strcmp(mode, "p-frame-close-parent") || !strcmp(mode, "p-frame-child-dispatch")) ? 2 : !strcmp(mode, "p-parent-nested") ? 3 :
149 !strcmp(mode, "p-cancel-close-parent") ? 4 : 0,
150 .display_fd = wl_display_get_fd(root->display)};
151 result.buffer = sui_wayland_dmabuf_import(child, &spec);
152 assert(result.buffer);
153 if (!strcmp(mode, "p-reject-pending"))
154 assert(!sui_wayland_present_dmabuf(child, result.buffer, refused_release, refused_frame, NULL));
155 assert(wl_display_roundtrip(root->display) >= 0);
156 assert(sui_wayland_dmabuf_import_status(result.buffer) == SUI_DMABUF_IMPORT_READY);
157 if (!strcmp(mode, "p-reject-size")) {
158 assert(sui_wayland_geometry(child, 0, 0, 32, 32));
159 assert(!sui_wayland_present_dmabuf(child, result.buffer, refused_release, refused_frame, NULL));
160 assert(sui_wayland_geometry(child, 0, 0, 16, 16));
161 }
162 if (!strcmp(mode, "p-reject-owner")) {
163 Wl alien = {0}, other = *child;
164 other.parent = &alien;
165 assert(!sui_wayland_present_dmabuf(&other, result.buffer, refused_release, refused_frame, NULL));
166 }
167 if (!strcmp(mode, "p-reject-root"))
168 assert(!sui_wayland_present_dmabuf(root, result.buffer, refused_release, refused_frame, NULL));
169 assert(submit(&result));
170 int64_t deadline = now_ms() + 1000;
171 while (!stats->commits && now_ms() < deadline) usleep(1000);
172 assert(stats->commits == 1);
173 assert(!result.released && !result.retired && !result.frames && !result.canceled);
174 assert(result.buffer->busy && result.buffer->buffer);
175 if (!strcmp(mode, "p-child-close")) {
176 sui_wayland_close(child);
177 assert(result.canceled == 1 && !result.released && !result.retired);
178 assert(result.buffer->busy && result.buffer->buffer);
179 } else if (!strcmp(mode, "p-cancel-busy")) {
180 sui_wayland_dmabuf_import_cancel(result.buffer);
181 assert(result.buffer->busy && result.buffer->buffer);
182 assert(sui_wayland_dmabuf_import_status(result.buffer) == SUI_DMABUF_IMPORT_CANCELED);
183 } else if (!strcmp(mode, "p-unref-busy")) {
184 sui_wayland_dmabuf_import_unref(result.buffer);
185 result.buffer = NULL;
186 } else if (!strcmp(mode, "p-resize-busy")) {
187 assert(sui_wayland_geometry(child, 0, 0, 32, 32));
188 assert(result.buffer->busy && result.buffer->buffer);
189 }
190 if (result.parent_close_kind) {
191 if (result.parent_close_kind == 4) {
192 sui_wayland_close(child);
193 assert(result.canceled == 1 && result.retired == 1 && !result.frames && !result.released);
194 } else {
195 struct wl_callback *sync = wl_display_sync(root->display);
196 assert(sync && wl_callback_add_listener(sync, &close_sync_listener, NULL) == 0);
197 assert(wl_display_flush(root->display) >= 0);
198 deadline = now_ms() + 1000;
199 while (result.root && now_ms() < deadline) {
200 result.root->head = result.root->count = result.root->wake = 0;
201 Wl *target = (!strcmp(mode, "p-release-child-dispatch") || !strcmp(mode, "p-frame-child-dispatch")) ? result.child : result.root;
202 assert(dispatch(target, 50) >= 0);
203 }
204 assert(!result.root && !result.child && !result.buffer);
205 if (result.parent_close_kind == 2)
206 assert(result.frames == 1 && result.retired == 1 && !result.released && !result.canceled);
207 else assert(result.released == 1 && result.canceled == 1 && !result.frames && !result.retired);
208 }
209 } else if (!strcmp(mode, "p-parent-close") || result.callback_owned) {
210 sui_wayland_close(root);
211 assert(result.retired == 1 && result.canceled == 1 && !result.released && !result.frames);
212 if (result.callback_owned) assert(!result.root && !result.child && !result.buffer);
213 } else if (!strcmp(mode, "p-disconnect")) {
214 assert(wl_display_roundtrip(root->display) < 0 && dispatch(root, 0) < 0);
215 assert(!result.released && !result.retired);
216 sui_wayland_close(root);
217 assert(result.retired == 1 && result.canceled == 1 && !result.released);
218 } else {
219 assert(wl_display_roundtrip(root->display) >= 0);
220 int frame_first = !strcmp(mode, "p-frame-first") || !strcmp(mode, "p-busy-buffer") ||
221 !strcmp(mode, "p-frame-unref");
222 if (frame_first) assert(result.frames == 1 && !result.released);
223 else assert(result.released == 1 && !result.frames);
224 if (!strcmp(mode, "p-busy-buffer")) {
225 Wl *other = sui_wayland_child(root, 0, 0, 16, 16, 0);
226 assert(other && !sui_wayland_present_dmabuf(other, result.buffer, refused_release, refused_frame, NULL));
227 sui_wayland_close(other);
228 sui_wayland_unref(other);
229 }
230 if (!strcmp(mode, "p-frame-busy"))
231 assert(!sui_wayland_present_dmabuf(child, result.buffer, refused_release, refused_frame, NULL));
232 assert(wl_display_roundtrip(root->display) >= 0);
233 assert(result.released == 1 && !result.retired);
234 assert(result.frames + result.canceled == 1);
235 if (result.buffer) assert(!result.buffer->busy);
236 if (!strcmp(mode, "p-repeated")) {
237 assert(submit(&result));
238 assert(wl_display_roundtrip(root->display) >= 0 && wl_display_roundtrip(root->display) >= 0);
239 assert(result.released == 2 && result.frames == 2 && !result.canceled);
240 }
241 }
242 if (result.root) sui_wayland_close(result.root);
243 sui_wayland_unref(result.child);
244 sui_wayland_unref(result.root);
245 sui_wayland_dmabuf_import_unref(result.buffer);
246 assert(fcntl(plane, F_GETFD) == FD_CLOEXEC && import_fd_count() + 1 == descriptors);
247 close(plane);
248 unsetenv("WAYLAND_SOCKET");
249 int status;
250 assert(waitpid(peer, &status, 0) == peer && WIFEXITED(status) && WEXITSTATUS(status) == 0);
251 assert(stats->attaches == (!strcmp(mode, "p-repeated") ? 2u : 1u));
252 munmap(stats, sizeof *stats);
253 printf("PASS: presentation wire %s, independent frame/release and retirement\n", mode);
254 return 0;