From 04e506bc8df5d9eac368e6eaee70de88d22367a5 Mon Sep 17 00:00:00 2001 From: The412Banner <205237651+The412Banner@users.noreply.github.com> Date: Mon, 30 Nov 2026 05:08:19 +0000 Subject: [PATCH] fix(display): frames a nested compositor forwards wait for scanout before going back to their program --- .../main/cpp/waylandcomp/src/ahb_swapchain.c | 12 +- .../main/cpp/waylandcomp/src/ahb_swapchain.h | 2 +- .../cpp/waylandcomp/src/display_buffers.c | 9 +- .../tests/test_zero_copy_release_at_latch.py | 142 ++++++++++++++++-- 4 files changed, 145 insertions(+), 20 deletions(-) diff --git a/app/src/main/cpp/waylandcomp/src/ahb_swapchain.c b/app/src/main/cpp/waylandcomp/src/ahb_swapchain.c index 3ac9ec78fc..0ec9dab56c 100644 --- a/app/src/main/cpp/waylandcomp/src/ahb_swapchain.c +++ b/app/src/main/cpp/waylandcomp/src/ahb_swapchain.c @@ -39,6 +39,7 @@ struct ahb_buf { struct wl_resource *resource; struct wl_listener resource_destroy; int display; + int client_allocated; int on_layer; int release_pending; int64_t deferred_ns; @@ -133,10 +134,11 @@ static void handle_released(uint64_t id, int fd) { if (fd >= 0) { struct droiddeck_dma_buf_sync_file imp = {.flags = DROIDDECK_DMA_BUF_SYNC_READ, .fd = fd}; int dmabuf_fd = droiddeck_dmabuf_fd(ab->b); - if (dmabuf_fd >= 0 && ioctl(dmabuf_fd, DROIDDECK_DMA_BUF_IOCTL_IMPORT_SYNC_FILE, &imp) == 0) { + const int at_latch = !ab->display || ab->client_allocated; + if (at_latch && dmabuf_fd >= 0 && ioctl(dmabuf_fd, DROIDDECK_DMA_BUF_IOCTL_IMPORT_SYNC_FILE, &imp) == 0) { close(fd); } else { - if (!g_import_failed_logged) { + if (at_latch && !g_import_failed_logged) { g_import_failed_logged = 1; droiddeck_log("layer", "zero-copy: DMA_BUF_IOCTL_IMPORT_SYNC_FILE failed (%s): releases wait for the display's fence here instead", strerror(errno)); @@ -300,11 +302,13 @@ static int adopt(struct wl_client *c, struct wl_resource *buffer, struct dmabuf_ } int ahb_swapchain_adopt(struct wl_client *c, struct wl_resource *buffer, struct dmabuf_buffer *b, AHardwareBuffer *ahb, - uint64_t modifier) { + uint64_t modifier, int client_allocated) { if (!g_advertised || *droiddeck_dmabuf_ahb_slot(b)) return -1; AHardwareBuffer_acquire(ahb); if (adopt(c, buffer, b, ahb, 0, 0, modifier) != 0) { AHardwareBuffer_release(ahb); return -1; } - ((struct ahb_buf *)*droiddeck_dmabuf_ahb_slot(b))->display = 1; + struct ahb_buf *ab = *droiddeck_dmabuf_ahb_slot(b); + ab->display = 1; + ab->client_allocated = client_allocated; return 0; } diff --git a/app/src/main/cpp/waylandcomp/src/ahb_swapchain.h b/app/src/main/cpp/waylandcomp/src/ahb_swapchain.h index 9002e6fc0a..ac32d6d9c6 100644 --- a/app/src/main/cpp/waylandcomp/src/ahb_swapchain.h +++ b/app/src/main/cpp/waylandcomp/src/ahb_swapchain.h @@ -14,7 +14,7 @@ void ahb_swapchain_set_mode(int on, int live); int ahb_swapchain_last_frame_age_ms(void); int ahb_swapchain_has_ahb(const struct dmabuf_buffer *b); int ahb_swapchain_adopt(struct wl_client *c, struct wl_resource *buffer, struct dmabuf_buffer *b, AHardwareBuffer *ahb, - uint64_t modifier); + uint64_t modifier, int client_allocated); AHardwareBuffer *ahb_swapchain_display_buffer(const struct dmabuf_buffer *b); int ahb_swapchain_present(struct dmabuf_buffer *b, struct surface *s, int scene_w, int scene_h, const struct vkp_draw *draw); int ahb_swapchain_defer_release(struct dmabuf_buffer *b, struct wl_resource *buffer, struct surface *s, int paced); diff --git a/app/src/main/cpp/waylandcomp/src/display_buffers.c b/app/src/main/cpp/waylandcomp/src/display_buffers.c index 99f6993a56..2755b6458e 100644 --- a/app/src/main/cpp/waylandcomp/src/display_buffers.c +++ b/app/src/main/cpp/waylandcomp/src/display_buffers.c @@ -312,8 +312,13 @@ void display_buffers_match(struct wl_client *c, struct wl_resource *buffer, stru : (b->modifier == MOD_LINEAR || b->modifier == MOD_INVALID) && e->stride == b->stride[0]; if (laid_out && e->format == ahb_format_of(b->format, e->modifier) && b->width > 0 && b->height > 0 && - (uint32_t)b->width <= d.width && (uint32_t)b->height <= d.height) - ahb_swapchain_adopt(c, buffer, b, e->ahb, e->modifier); + (uint32_t)b->width <= d.width && (uint32_t)b->height <= d.height) { + pid_t pid = 0; + uid_t uid; + gid_t gid; + wl_client_get_credentials(c, &pid, &uid, &gid); + ahb_swapchain_adopt(c, buffer, b, e->ahb, e->modifier, pid == e->peer->pid); + } return; } } diff --git a/tools/tests/test_zero_copy_release_at_latch.py b/tools/tests/test_zero_copy_release_at_latch.py index 6d57d48897..eec03a2b24 100644 --- a/tools/tests/test_zero_copy_release_at_latch.py +++ b/tools/tests/test_zero_copy_release_at_latch.py @@ -61,20 +61,22 @@ static void send_deferred_release(struct ahb_buf *ab) { ++deferred_releases; ab- static void maybe_free(struct ahb_buf *ab) { (void)ab; ++frees; } FUNCTIONS static void reset(void) { import_ok = imports = imported_flags = deferred_releases = frees = sources = 0; } -static void run(int display, int can_import, int fence_signalled) { +static void run(int display, int client_allocated, int can_import, int fence_signalled) { reset(); wl_list_init(&g_bufs); struct dmabuf_buffer b = {.fd = 7}; struct ahb_buf ab; memset(&ab, 0, sizeof(ab)); - ab.id = 42; ab.b = &b; ab.display = display; ab.on_layer = 1; ab.release_pending = 1; ab.fence_fd = -1; + ab.id = 42; ab.b = &b; ab.display = display; ab.client_allocated = client_allocated; ab.on_layer = 1; ab.release_pending = 1; ab.fence_fd = -1; wl_list_insert(&g_bufs, &ab.link); int fds[2]; assert(pipe(fds) == 0); if (fence_signalled) assert(write(fds[1], "x", 1) == 1); import_ok = can_import; handle_released(42, fds[0]); - assert(!ab.on_layer && imports == 1 && imported_flags == DROIDDECK_DMA_BUF_SYNC_READ); - if (can_import || fence_signalled) { + const int at_latch = !display || client_allocated; + assert(!ab.on_layer && imports == at_latch); + if (at_latch) assert(imported_flags == DROIDDECK_DMA_BUF_SYNC_READ); + if ((at_latch && can_import) || fence_signalled) { assert(deferred_releases == 1 && frees == 1 && !ab.fence_src && ab.fence_fd == -1); errno = 0; assert(fcntl(fds[0], F_GETFD) == -1 && errno == EBADF); } else { @@ -84,8 +86,8 @@ static void run(int display, int can_import, int fence_signalled) { close(fds[1]); } int main(int argc, char **argv) { - assert(argc == 4); - run(atoi(argv[1]), atoi(argv[2]), atoi(argv[3])); + assert(argc == 5); + run(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), atoi(argv[4])); return 0; } ''' @@ -116,25 +118,139 @@ class ZeroCopyReleaseAtLatchTest(unittest.TestCase): if built.returncode: raise AssertionError(built.stdout + built.stderr) - def scenario(self, display, can_import, fence_signalled): - result = subprocess.run([str(self.binary), str(display), str(can_import), str(fence_signalled)], + def scenario(self, display, client_allocated, can_import, fence_signalled): + result = subprocess.run([str(self.binary), str(display), str(client_allocated), str(can_import), + str(fence_signalled)], capture_output=True, text=True, timeout=10) self.assertEqual(result.returncode, 0, result.stdout + result.stderr) def test_display_buffer_released_at_latch_when_fence_attaches(self): - self.scenario(1, 1, 0) + self.scenario(1, 1, 1, 0) def test_swapchain_buffer_released_at_latch_when_fence_attaches(self): - self.scenario(0, 1, 0) + self.scenario(0, 0, 1, 0) def test_display_buffer_waits_for_fence_when_kernel_cannot_attach(self): - self.scenario(1, 0, 0) + self.scenario(1, 1, 0, 0) def test_swapchain_buffer_waits_for_fence_when_kernel_cannot_attach(self): - self.scenario(0, 0, 0) + self.scenario(0, 0, 0, 0) def test_signalled_fence_releases_without_waiting(self): - self.scenario(1, 0, 1) + self.scenario(1, 1, 0, 1) + + def test_forwarded_display_buffer_waits_for_fence_even_when_kernel_can_attach(self): + self.scenario(1, 0, 1, 0) + + def test_forwarded_display_buffer_with_signalled_fence_releases_without_waiting(self): + self.scenario(1, 0, 1, 1) + + +MATCH_HARNESS = r''' +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#define MOD_LINEAR 0x0000000000000000ULL +#define MOD_INVALID 0x00ffffffffffffffULL +#define MOD_QCOM_COMPRESSED 0x0500000000000001ULL +struct wl_client { pid_t pid; }; +struct wl_resource { int unused; }; +struct wl_event_loop { int unused; }; +struct dmabuf_buffer { + int fd[4]; + uint32_t offset[4], stride[4]; + int n_planes; + int32_t width, height; + uint32_t format; + uint64_t modifier; +}; +typedef struct AHardwareBuffer { uint32_t width, height; } AHardwareBuffer; +typedef struct { uint32_t width, height; } AHardwareBuffer_Desc; +STRUCTS +static struct wl_list g_entries; +static struct wl_event_loop *g_loop; +static int adopted = -1, adoptions; +static void AHardwareBuffer_describe(const AHardwareBuffer *a, AHardwareBuffer_Desc *d) { + d->width = a->width; d->height = a->height; +} +static uint32_t ahb_format_of(uint32_t drm, uint64_t modifier) { (void)modifier; return drm; } +static void wl_client_get_credentials(struct wl_client *c, pid_t *pid, uid_t *uid, gid_t *gid) { + *pid = c->pid; *uid = 0; *gid = 0; +} +static int ahb_swapchain_adopt(struct wl_client *c, struct wl_resource *buffer, struct dmabuf_buffer *b, + AHardwareBuffer *ahb, uint64_t modifier, int client_allocated) { + (void)c; (void)buffer; (void)b; (void)ahb; (void)modifier; + ++adoptions; adopted = client_allocated; return 0; +} +FUNCTIONS +int main(int argc, char **argv) { + assert(argc == 4); + int fd = open(argv[1], O_RDONLY); + assert(fd >= 0); + struct stat st; + assert(fstat(fd, &st) == 0); + static struct wl_event_loop loop; + g_loop = &loop; + wl_list_init(&g_entries); + AHardwareBuffer ahb = {1920, 1080}; + struct peer allocator = {.pid = (pid_t)atoi(argv[2])}; + struct entry e = {.dev = st.st_dev, .ino = st.st_ino, .ahb = &ahb, .format = 1, .stride = 7680, + .width = 1920, .height = 1080, .modifier = MOD_LINEAR, .peer = &allocator}; + wl_list_insert(&g_entries, &e.link); + struct wl_client client = {.pid = (pid_t)atoi(argv[3])}; + struct wl_resource resource; + struct dmabuf_buffer b = {.fd = {fd}, .stride = {7680}, .n_planes = 1, .width = 1920, .height = 1080, + .format = 1, .modifier = MOD_LINEAR}; + display_buffers_match(&client, &resource, &b); + assert(adoptions == 1); + return adopted == (allocator.pid == client.pid) ? 0 : 1; +} +''' + + +class DisplayBufferAllocatorTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + cc = shutil.which(os.environ.get('CC', 'cc')) + if not cc: + raise AssertionError('C compiler required') + text = (SRC / 'display_buffers.c').read_text() + structs = '\n'.join(re.search(r'struct %s \{.*?\n\};' % n, text, re.S).group() for n in ('peer', 'entry')) + source = MATCH_HARNESS.replace('STRUCTS', structs).replace('FUNCTIONS', function(text, 'display_buffers_match')) + source = source.replace('#include ', '#include \n' + 'void wl_list_init(struct wl_list *l) { l->prev = l; l->next = l; }\n' + 'void wl_list_insert(struct wl_list *l, struct wl_list *e) {\n' + ' e->next = l->next; e->prev = l; l->next->prev = e; l->next = e;\n}\n' + 'int wl_list_empty(const struct wl_list *l) { return l->next == l; }') + cls.tmp = tempfile.TemporaryDirectory(prefix='droiddeck-display-match-') + cls.addClassCleanup(cls.tmp.cleanup) + probe = Path(cls.tmp.name) / 'match.c' + probe.write_text(source) + cls.binary = probe.with_suffix('') + cls.buffer = Path(cls.tmp.name) / 'buffer' + cls.buffer.write_bytes(b'\0' * 16) + built = subprocess.run([cc, '-std=c11', '-Wall', '-Wextra', '-Werror', '-Wno-unused-function', '-UNDEBUG', + '-I', str(SRC.parent / 'prebuilt/include'), str(probe), '-o', str(cls.binary)], + capture_output=True, text=True) + if built.returncode: + raise AssertionError(built.stdout + built.stderr) + + def adopt(self, allocator_pid, client_pid): + result = subprocess.run([str(self.binary), str(self.buffer), str(allocator_pid), str(client_pid)], + capture_output=True, text=True, timeout=10) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + + def test_buffer_the_attaching_client_allocated_is_its_own(self): + self.adopt(1200, 1200) + + def test_buffer_a_nested_compositor_forwards_from_another_process_is_not_its_own(self): + self.adopt(3400, 1200) if __name__ == '__main__':