From 5e345372a3a8bb750309ef369c6be810c12c4798 Mon Sep 17 00:00:00 2001 From: maxjivi05 Date: Sun, 29 Nov 2026 14:03:35 -0500 Subject: [PATCH] Wait for the game's dma-buf before copying, and keep frames that cannot be copied on the display layer --- .../main/cpp/waylandcomp/src/ahb_swapchain.c | 12 ++++-- tools/tests/test_zero_copy_acquire_fence.py | 37 +++++++++++++------ 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/app/src/main/cpp/waylandcomp/src/ahb_swapchain.c b/app/src/main/cpp/waylandcomp/src/ahb_swapchain.c index 8153c034f5..a016696ce0 100644 --- a/app/src/main/cpp/waylandcomp/src/ahb_swapchain.c +++ b/app/src/main/cpp/waylandcomp/src/ahb_swapchain.c @@ -200,10 +200,16 @@ int ahb_swapchain_present(struct dmabuf_buffer *b, struct surface *s, int scene_ } else { if (!g_export_failed_logged) { g_export_failed_logged = 1; - droiddeck_log("layer", "zero-copy: no acquire fence from the game's dma-buf (%s): its frames are copied instead", - dmabuf_fd >= 0 ? strerror(errno) : "no dma-buf fd"); + droiddeck_log("layer", "zero-copy: no acquire fence from the game's dma-buf (%s): %s", + dmabuf_fd >= 0 ? strerror(errno) : "no dma-buf fd", + draw ? "its frames are copied instead" : "it cannot be copied, so each frame is waited for on the CPU"); } - return -1; + if (dmabuf_fd >= 0) { + struct pollfd p = {.fd = dmabuf_fd, .events = POLLIN}; + int r; + do { r = poll(&p, 1, 100); } while (r < 0 && errno == EINTR); + } + if (draw) return -1; } int r = sc_layer_present_ahb(ab->ahb, ab->w, ab->h, acquire, (void *)(uintptr_t)ab->id, scene_w, scene_h, draw, s ? droiddeck_surface_color(s) : NULL, ab->format); diff --git a/tools/tests/test_zero_copy_acquire_fence.py b/tools/tests/test_zero_copy_acquire_fence.py index 043753f1c7..5cae4586cb 100644 --- a/tools/tests/test_zero_copy_acquire_fence.py +++ b/tools/tests/test_zero_copy_acquire_fence.py @@ -38,7 +38,7 @@ static unsigned g_stat_zero_copy; static int g_export_failed_logged; static struct surface *g_announced; static volatile int64_t g_last_zero_copy_ns; -static int export_ok, exports, presents, presented_acquire, logs; +static int export_ok, exports, presents, presented_acquire, logs, polls; static int fake_ioctl(int fd, unsigned long req, void *arg) { struct droiddeck_dma_buf_sync_file *f = arg; (void)fd; @@ -49,6 +49,11 @@ static int fake_ioctl(int fd, unsigned long req, void *arg) { return 0; } #define ioctl fake_ioctl +static int fake_poll(struct pollfd *p, nfds_t n, int timeout) { + assert(n == 1 && p->events == POLLIN && timeout == 100); + ++polls; p->revents = POLLIN; return 1; +} +#define poll fake_poll static int64_t now_ns(void) { return 1; } static void **droiddeck_dmabuf_ahb_slot(struct dmabuf_buffer *b) { return &b->slot; } static int droiddeck_dmabuf_fd(struct dmabuf_buffer *b) { return b->fd; } @@ -63,26 +68,30 @@ static int sc_layer_present_ahb(AHardwareBuffer *a, int w, int h, int acquire, v return 0; } FUNCTIONS -static void run(int fd, int can_export) { +static void run(int fd, int can_export, int copyable) { struct dmabuf_buffer b = {.fd = fd}; + struct vkp_draw d; + const struct vkp_draw *draw = copyable ? &d : NULL; struct ahb_buf ab; struct surface s; memset(&ab, 0, sizeof(ab)); ab.id = 7; ab.w = 64; ab.h = 64; ab.fence_fd = -1; b.slot = &ab; export_ok = can_export; - int r = ahb_swapchain_present(&b, &s, 64, 64, NULL); + int r = ahb_swapchain_present(&b, &s, 64, 64, draw); if (fd >= 0 && can_export) { - assert(r == 0 && presents == 1 && presented_acquire >= 0 && ab.on_layer && g_stat_zero_copy == 1); - } else { - assert(r == -1 && presents == 0 && !ab.on_layer && g_stat_zero_copy == 0); - r = ahb_swapchain_present(&b, &s, 64, 64, NULL); + assert(r == 0 && presents == 1 && presented_acquire >= 0 && ab.on_layer && g_stat_zero_copy == 1 && polls == 0); + } else if (copyable) { + assert(r == -1 && presents == 0 && !ab.on_layer && g_stat_zero_copy == 0 && polls == (fd >= 0)); + r = ahb_swapchain_present(&b, &s, 64, 64, draw); assert(r == -1 && presents == 0 && logs == 1); + } else { + assert(r == 0 && presents == 1 && presented_acquire == -1 && ab.on_layer && polls == (fd >= 0)); } } int main(int argc, char **argv) { - assert(argc == 3); - run(atoi(argv[1]), atoi(argv[2])); + assert(argc == 4); + run(atoi(argv[1]), atoi(argv[2]), atoi(argv[3])); return 0; } ''' @@ -112,8 +121,8 @@ class ZeroCopyAcquireFenceTest(unittest.TestCase): if built.returncode: raise AssertionError(built.stdout + built.stderr) - def scenario(self, fd, can_export): - result = subprocess.run([str(self.binary), str(fd), str(can_export)], capture_output=True, text=True, + def scenario(self, fd, can_export, copyable=1): + result = subprocess.run([str(self.binary), str(fd), str(can_export), str(copyable)], capture_output=True, text=True, timeout=10) self.assertEqual(result.returncode, 0, result.stdout + result.stderr) @@ -126,6 +135,12 @@ class ZeroCopyAcquireFenceTest(unittest.TestCase): def test_buffer_without_dmabuf_fd_falls_back_to_copy(self): self.scenario(-1, 1) + def test_uncopyable_buffer_waits_on_the_cpu_and_stays_on_the_layer(self): + self.scenario(5, 0, 0) + + def test_uncopyable_buffer_without_dmabuf_fd_stays_on_the_layer(self): + self.scenario(-1, 1, 0) + if __name__ == '__main__': unittest.main()