"""A commit whose dma-buf the client's GPU is still writing is held until the buffer polls readable. The compiled functions come from compositor.c; libwayland's lists and event loop, poll and the clock are faked. """ import os from pathlib import Path import re import shutil import subprocess import tempfile import unittest SOURCE = Path(__file__).resolve().parents[2] / 'app/src/main/cpp/waylandcomp/src/compositor.c' def function(text, name): match = re.search(r'^(?:static )?[^\n;]+\b' + name + r'\([^;]*?\)\s*\{', text, re.M) if not match: raise AssertionError('Production function not found: ' + name) pos = text.index('{', match.start()) depth, end = 1, pos + 1 while depth: depth += (text[end] == '{') - (text[end] == '}') end += 1 return text[match.start():end] def block(text, start): begin = text.index(start) return text[begin:text.index('\n};\n', begin) + 4] HARNESS = r''' #include #include #include #include #include #include #define DAMAGE_RECTS 4 #define POLLIN 1 #define F_DUPFD_CLOEXEC 1030 #define WL_EVENT_READABLE 1 struct pollfd { int fd; short events, revents; }; struct wl_list { struct wl_list *prev, *next; }; struct wl_listener { struct wl_list link; void (*notify)(struct wl_listener *, void *); }; static void wl_list_init(struct wl_list *l) { l->prev = l->next = l; } static void wl_list_remove(struct wl_list *l) { l->prev->next = l->next; l->next->prev = l->prev; l->prev = l->next = NULL; } static void wl_list_insert(struct wl_list *list, struct wl_list *elm) { elm->prev = list; elm->next = list->next; list->next = elm; elm->next->prev = elm; } static void wl_list_insert_list(struct wl_list *list, struct wl_list *other) { if (other->next == other) return; other->next->prev = list; other->prev->next = list->next; list->next->prev = other->prev; list->next = other->next; } static int wl_list_length(const struct wl_list *l) { int n = 0; for (struct wl_list *e = l->next; e != l; e = e->next) n++; return n; } #define wl_container_of(ptr, sample, member) (__typeof__(sample))((char *)(ptr) - offsetof(__typeof__(*sample), member)) struct dmabuf_buffer { int fd[4]; int n_planes; int ahb; }; struct wl_resource { struct wl_list link; struct wl_listener *listener; struct dmabuf_buffer *db; int released, destroyed; }; static struct wl_resource *from_link(struct wl_list *l) { return wl_container_of(l, (struct wl_resource *)0, link); } #define wl_resource_for_each_safe(r, t, list) \ for (r = from_link((list)->next), t = from_link(r->link.next); &r->link != (list); r = t, t = from_link(t->link.next)) static void wl_resource_destroy(struct wl_resource *r) { wl_list_remove(&r->link); r->destroyed = 1; } static void wl_resource_add_destroy_listener(struct wl_resource *r, struct wl_listener *l) { r->listener = l; wl_list_init(&l->link); } static void client_destroys(struct wl_resource *r) { if (r->listener) { struct wl_listener *l = r->listener; r->listener = NULL; l->notify(l, r); } } static int ready[64], dup_of[64], next_dup = 40, closed[64]; static int poll(struct pollfd *p, unsigned n, int timeout) { if (n != 1 || timeout != 0 || p->events != POLLIN) { puts("FAIL poll args"); exit(1); } return ready[p->fd] ? 1 : 0; } static int fcntl(int fd, int cmd, int arg) { (void)cmd; (void)arg; dup_of[next_dup] = fd; return next_dup++; } static int close(int fd) { closed[fd]++; return 0; } struct dd_frame_acquire { int fd_plus_one; }; static void dd_frame_acquire_clear(struct dd_frame_acquire *f) { if (f->fd_plus_one) close(f->fd_plus_one - 1); f->fd_plus_one = 0; } struct wl_event_source { int fd; int (*fd_cb)(int, uint32_t, void *); int (*timer_cb)(void *); void *data; int armed_ms, removed; }; struct wl_event_loop; struct wl_display; static struct wl_display *g_display; static struct wl_event_source sources[32]; static int n_sources; static struct wl_event_loop *wl_display_get_event_loop(struct wl_display *d) { (void)d; return NULL; } static struct wl_event_source *wl_event_loop_add_fd(struct wl_event_loop *l, int fd, uint32_t mask, int (*cb)(int, uint32_t, void *), void *data) { (void)l; (void)mask; struct wl_event_source *s = &sources[n_sources++]; memset(s, 0, sizeof(*s)); s->fd = fd; s->fd_cb = cb; s->data = data; return s; } static struct wl_event_source *wl_event_loop_add_timer(struct wl_event_loop *l, int (*cb)(void *), void *data) { (void)l; struct wl_event_source *s = &sources[n_sources++]; memset(s, 0, sizeof(*s)); s->fd = -1; s->timer_cb = cb; s->data = data; return s; } static int wl_event_source_timer_update(struct wl_event_source *s, int ms) { s->armed_ms = ms; return 0; } static int wl_event_source_remove(struct wl_event_source *s) { s->removed = 1; return 0; } static void dispatch(int timers_expire) { for (int i = 0; i < n_sources; i++) { struct wl_event_source *s = &sources[i]; if (s->removed) continue; if (s->fd_cb && ready[dup_of[s->fd]]) s->fd_cb(s->fd, WL_EVENT_READABLE, s->data); else if (s->timer_cb && s->armed_ms && timers_expire) { s->armed_ms = 0; s->timer_cb(s->data); } } } static int64_t clock_ns; static int64_t now_ns(void) { return clock_ns; } static struct { unsigned replaced; } g_pace; static struct { unsigned commit_waits, commit_wait_limits; int64_t commit_wait_ns, commit_wait_max_ns; } g_perf; static int logged; static void droiddeck_log(const char *tag, const char *fmt, ...) { (void)tag; (void)fmt; logged++; } static void send_buffer_release(struct wl_resource *r) { r->released++; } static unsigned feedback_discard_all(struct wl_list *list) { unsigned n = 0; struct wl_resource *r, *t; wl_resource_for_each_safe(r, t, list) { wl_resource_destroy(r); n++; } return n; } static struct dmabuf_buffer *get_dmabuf(struct wl_resource *r) { return r ? r->db : NULL; } static int ahb_swapchain_has_ahb(const struct dmabuf_buffer *b) { return b->ahb; } static int64_t rect_area(const int32_t *r, int bw, int bh) { (void)bw; (void)bh; return (int64_t)r[2] * r[3]; } struct damage_list { int32_t r[DAMAGE_RECTS][4]; int n; }; struct surface { struct wl_resource *resource, *xdg_toplevel; int toplevel_committed; struct wl_resource *pending_buffer; struct dd_frame_acquire pending_frame_acquire; struct wl_listener pending_buffer_destroy; int pending_attach, pending_src_set, pending_dst_set, pending_opaque_set; float pending_src[4]; int pending_dst[2], pending_opaque[4]; struct damage_list pending_damage_buf, pending_damage_local; struct wl_list pending_frames, pending_feedback; struct surface_state *waiting; struct wl_event_source *waiting_source, *waiting_timer; int waiting_fd; int64_t waiting_since_ns; }; static void send_toplevel_configure(struct surface *s) { (void)s; } struct wl_client; static struct surface surf; STATE_STRUCT static int applies, applied_attach, applied_frames, applied_opaque, applied_damage; static struct wl_resource *applied_buffer; static void surface_state_apply(struct surface *s, struct surface_state *st); FUNCTIONS static void surface_state_apply(struct surface *s, struct surface_state *st) { (void)s; dd_frame_acquire_clear(&st->frame_acquire); applies++; applied_attach = st->attach; applied_buffer = st->attach ? st->buffer : NULL; applied_frames = wl_list_length(&st->frames); applied_opaque = st->opaque_set ? st->opaque[2] : -1; applied_damage = st->damage_buf.n; if (st->attach) { wl_list_remove(&st->buffer_destroy.link); wl_list_init(&st->buffer_destroy.link); } struct wl_resource *r, *t; wl_resource_for_each_safe(r, t, &st->frames) wl_resource_destroy(r); feedback_discard_all(&st->feedback); } static struct wl_resource frame_res[16]; static int n_frames; static void attach(struct wl_resource *b) { if (surf.pending_buffer) wl_list_remove(&surf.pending_buffer_destroy.link); wl_list_init(&surf.pending_buffer_destroy.link); surf.pending_buffer = b; surf.pending_attach = 1; } static void frame(void) { struct wl_resource *f = &frame_res[n_frames++]; wl_list_insert(surf.pending_frames.prev, &f->link); } static void damage(int w) { surf.pending_damage_buf.r[surf.pending_damage_buf.n][2] = w; surf.pending_damage_buf.r[surf.pending_damage_buf.n][3] = 1; surf.pending_damage_buf.n++; } static void commit(void) { surface_commit(NULL, NULL); } #define CHECK(c, what) do { if (!(c)) { printf("FAIL %s (line %d)\n", what, __LINE__); return 1; } } while (0) int main(void) { wl_list_init(&surf.pending_frames); wl_list_init(&surf.pending_feedback); wl_list_init(&surf.pending_buffer_destroy.link); surf.waiting_fd = -1; struct dmabuf_buffer d1 = {{10, -1}, 1, 0}, d2 = {{11, -1}, 1, 0}, d3 = {{12, -1}, 1, 0}, dz = {{13, -1}, 1, 1}; struct wl_resource b1 = {.db = &d1}, b2 = {.db = &d2}, b3 = {.db = &d3}, bz = {.db = &dz}; ready[10] = 1; attach(&b1); frame(); commit(); CHECK(applies == 1 && applied_buffer == &b1 && applied_frames == 1 && !surf.waiting && n_sources == 0, "ready buffer"); ready[11] = 0; clock_ns = 1000000; attach(&b2); frame(); damage(5); commit(); CHECK(applies == 1 && surf.waiting && surf.waiting_fd >= 40 && dup_of[surf.waiting_fd] == 11, "held"); CHECK(sources[0].armed_ms == 0 && sources[1].armed_ms == 1000, "waits on the buffer with a 1 s limit"); dispatch(0); CHECK(applies == 1, "nothing before the writes end"); clock_ns = 4000000; ready[11] = 1; dispatch(0); CHECK(applies == 2 && applied_buffer == &b2 && applied_frames == 1 && applied_damage == 1 && !surf.waiting, "applied once ready"); CHECK(closed[40] == 1 && sources[0].removed && sources[1].removed, "wait cleaned up"); CHECK(g_perf.commit_waits == 1 && g_perf.commit_wait_max_ns == 3000000, "wait counted"); ready[11] = 0; ready[12] = 0; attach(&b2); frame(); damage(7); commit(); attach(&b3); frame(); damage(9); commit(); CHECK(applies == 2 && b2.released == 1 && dup_of[surf.waiting_fd] == 12, "replaced while waiting"); ready[12] = 1; dispatch(0); CHECK(applies == 3 && applied_buffer == &b3 && applied_frames == 2 && applied_damage == 2, "newest shown with both callbacks"); CHECK(g_perf.commit_waits == 2, "merged wait counted once"); ready[11] = 0; attach(&b2); commit(); surf.pending_opaque[2] = 640; surf.pending_opaque_set = 1; frame(); commit(); CHECK(applies == 3 && b2.released == 1 && dup_of[surf.waiting_fd] == 11, "the waiting buffer is not released"); ready[11] = 1; dispatch(0); CHECK(applies == 4 && applied_buffer == &b2 && applied_opaque == 640 && applied_frames == 1, "buffer kept, opaque applied"); g_zero_copy_shown = &surf; attach(&bz); commit(); CHECK(applies == 5 && applied_buffer == &bz && !surf.waiting && g_unwaited == &surf, "zero-copy buffer"); g_zero_copy_shown = NULL; ready[13] = 0; attach(&bz); commit(); CHECK(applies == 5 && surf.waiting, "a droiddeck-ahb buffer not shown zero-copy waits"); ready[13] = 1; dispatch(0); CHECK(applies == 6 && !g_unwaited, "waited buffer clears the mark"); ready[13] = 0; ready[10] = 0; attach(&b1); frame(); commit(); client_destroys(&b1); ready[10] = 1; dispatch(0); CHECK(applies == 7 && !applied_attach && applied_frames == 1, "destroyed buffer dropped from the commit"); ready[11] = 0; attach(&b2); commit(); dispatch(1); CHECK(applies == 8 && applied_buffer == &b2 && g_perf.commit_wait_limits == 1 && logged == 1 && !surf.waiting, "limit"); ready[11] = 0; ready[12] = 0; clock_ns = 10000000000; attach(&b2); commit(); CHECK(applies == 8 && sources[n_sources - 1].armed_ms == 1000, "held again"); clock_ns += 600000000; attach(&b3); commit(); CHECK(applies == 8 && surf.waiting && sources[n_sources - 1].armed_ms == 400, "a newer commit keeps the first deadline"); clock_ns += 500000000; attach(&b2); commit(); CHECK(applies == 9 && applied_buffer == &b2 && g_perf.commit_wait_limits == 2 && !surf.waiting, "commits past the deadline are applied at once"); puts("PASS"); return 0; } ''' class CommitWaitsForClientWritesTest(unittest.TestCase): def test_commits_wait_for_the_clients_gpu_writes(self): compiler = shutil.which(os.environ.get('CC', 'cc')) if not compiler: self.skipTest('A host C compiler is required') source = SOURCE.read_text() functions = '\n'.join(re.search(pattern, source, re.M).group(0) for pattern in (r'^#define COMMIT_WAIT_LIMIT_MS .*$', r'^static struct surface \*g_zero_copy_shown, \*g_unwaited;$')) + '\n' + '\n'.join( function(source, name) for name in ('note_damage', 'on_state_buffer_destroyed', 'surface_state_take', 'state_unfinished_fd', 'surface_state_discard', 'surface_state_merge', 'surface_wait_stop', 'surface_wait_end', 'on_commit_ready', 'on_commit_wait_limit', 'surface_wait_start', 'surface_commit')) probe = HARNESS.replace('STATE_STRUCT', block(source, 'struct surface_state {')).replace('FUNCTIONS', functions) probe = probe.replace('struct surface *s = wl_resource_get_user_data(r);', 'struct surface *s = &surf; (void)r;') with tempfile.TemporaryDirectory(prefix='droiddeck-commit-wait-') as temp: folder = Path(temp) (folder / 'probe.c').write_text(probe) built = subprocess.run([compiler, '-std=gnu11', '-Wall', '-Wextra', '-Wno-unused-parameter', '-Wno-unused-function', str(folder / 'probe.c'), '-o', str(folder / 'probe')], capture_output=True, text=True) self.assertEqual(built.returncode, 0, built.stdout + built.stderr) run = subprocess.run([str(folder / 'probe')], capture_output=True, text=True, timeout=10) self.assertEqual(run.returncode, 0, run.stdout + run.stderr) self.assertEqual(run.stdout.strip(), 'PASS') def test_scenes_settle_an_unwaited_buffer_before_copying_it(self): body = function(SOURCE.read_text(), 'render_scene').splitlines() copies = [i for i, line in enumerate(body) if 'vkp_render(w, h, dl.d, dl.n)' in line] self.assertGreaterEqual(len(copies), 4) for i in copies: self.assertTrue(any('settle_unwaited();' in line for line in body[max(0, i - 3):i]), body[i]) text = '\n'.join(body) self.assertIn('if (hdr_route == 1 || hdr_route == 2) settle_unwaited();', text) self.assertIn('settle_unwaited();\n render_stream();', text) self.assertIn('if (r == 0 && !fg_pass) g_zero_copy_shown = ls;\n else settle_unwaited();', text) if __name__ == '__main__': unittest.main()