From 484a0ab7944924c464605f0137010c76eefb05e5 Mon Sep 17 00:00:00 2001 From: The412Banner <205237651+The412Banner@users.noreply.github.com> Date: Sun, 22 Nov 2026 09:32:20 +0000 Subject: [PATCH] fix(compositor): keep a single-pixel buffer until the surface replaces it --- app/src/main/cpp/waylandcomp/src/compositor.c | 34 ++++++++- tools/tests/subsurface_release_harness.c | 76 ++++++++++++++++++- tools/tests/test_subsurface_release.py | 16 +++- tools/tests/vk_terminal_callers_test.c | 13 ++-- 4 files changed, 125 insertions(+), 14 deletions(-) diff --git a/app/src/main/cpp/waylandcomp/src/compositor.c b/app/src/main/cpp/waylandcomp/src/compositor.c index 461a0d6798..e7fe69d4df 100644 --- a/app/src/main/cpp/waylandcomp/src/compositor.c +++ b/app/src/main/cpp/waylandcomp/src/compositor.c @@ -224,6 +224,8 @@ struct surface { struct wl_resource *dmabuf; struct dmabuf_buffer *dmabuf_buf; struct wl_listener dmabuf_destroy; + struct wl_resource *pixel; + struct wl_listener pixel_destroy; const void *last_dmabuf; int buf_w, buf_h, has_content; int buf_alpha; @@ -444,7 +446,7 @@ static int buffer_shown_elsewhere(struct wl_resource *buffer, const struct surfa struct surface *o; if (!buffer) return 0; wl_list_for_each(o, &g_surfaces, link) - if (o != except && o->dmabuf == buffer) return 1; + if (o != except && (o->dmabuf == buffer || o->pixel == buffer)) return 1; return 0; } @@ -816,6 +818,27 @@ static void drop_dmabuf(struct surface *s, int paced) { s->dmabuf_buf = NULL; } +static void on_pixel_destroyed(struct wl_listener *l, void *data) { + struct surface *s = wl_container_of(l, s, pixel_destroy); + wl_list_remove(&s->pixel_destroy.link); + s->pixel = NULL; +} + +static void hold_pixel(struct surface *s, struct wl_resource *buffer) { + if (s->pixel == buffer) return; + s->pixel = buffer; + s->pixel_destroy.notify = on_pixel_destroyed; + wl_resource_add_destroy_listener(buffer, &s->pixel_destroy); +} + +static void drop_pixel(struct surface *s) { + struct wl_resource *buffer = s->pixel; + if (!buffer) return; + wl_list_remove(&s->pixel_destroy.link); + s->pixel = NULL; + if (!buffer_shown_elsewhere(buffer, s)) send_buffer_release(buffer); +} + static void on_dmabuf_destroyed(struct wl_listener *l, void *data) { struct surface *s = wl_container_of(l, s, dmabuf_destroy); wl_list_remove(&s->dmabuf_destroy.link); @@ -961,14 +984,13 @@ static void take_shm(struct surface *s, struct wl_shm_buffer *shm, struct wl_res droiddeck_on_compositor_frame(); } -static void take_pixel(struct surface *s, const uint8_t bgra[4], struct wl_resource *buffer) { +static void take_pixel(struct surface *s, const uint8_t bgra[4]) { if (s->shm_img && (vkp_image_width(s->shm_img) != 1 || vkp_image_height(s->shm_img) != 1)) { vkp_image_destroy(s->shm_img); s->shm_img = NULL; } if (!s->shm_img) s->shm_img = vkp_image_create_shm(1, 1); if (s->shm_img) vkp_image_upload_shm(s->shm_img, bgra, 4); - send_buffer_release(buffer); s->buf_w = 1; s->buf_h = 1; s->buf_alpha = bgra[3] != 0xff; @@ -1384,6 +1406,7 @@ static void surface_commit(struct wl_client *c, struct wl_resource *r) { surface_drop_idle(s); buf_trace_attach(buffer); + if (s->pixel != buffer) drop_pixel(s); if (s->role == ROLE_CURSOR) { if (!buffer) { cursor_publish_hidden(); @@ -1400,7 +1423,8 @@ static void surface_commit(struct wl_client *c, struct wl_resource *r) { take_shm(s, shm, buffer); } else if (single_pixel_buffer_get(buffer, pixel)) { drop_dmabuf(s, 1); - take_pixel(s, pixel, buffer); + take_pixel(s, pixel); + hold_pixel(s, buffer); } else { drop_dmabuf(s, 1); s->has_content = 0; @@ -1493,6 +1517,7 @@ static void surface_resource_destroy(struct wl_resource *r) { if (s->pending_buffer) wl_list_remove(&s->pending_buffer_destroy.link); pending_releases_forget_surface(s); drop_dmabuf(s, 0); + drop_pixel(s); ahb_swapchain_surface_gone(s); vkp_image_destroy(s->shm_img); wl_resource_for_each_safe(cb, cbtmp, &s->pending_frames) wl_resource_destroy(cb); @@ -1629,6 +1654,7 @@ static void subsurface_resource_destroy(struct wl_resource *r) { s->role = ROLE_NONE; s->has_content = 0; drop_dmabuf(s, 1); + drop_pixel(s); schedule_render(); } diff --git a/tools/tests/subsurface_release_harness.c b/tools/tests/subsurface_release_harness.c index 0921666d45..047b4003f4 100644 --- a/tools/tests/subsurface_release_harness.c +++ b/tools/tests/subsurface_release_harness.c @@ -22,7 +22,7 @@ void wl_list_remove(struct wl_list *elm) { int wl_list_empty(const struct wl_list *list) { return list->next == list; } struct wl_listener { struct wl_list link; void (*notify)(struct wl_listener *, void *); }; -struct wl_resource { struct wl_list listeners; int acquired, releases, errors; }; +struct wl_resource { struct wl_list listeners; int acquired, releases, errors, in_flight; }; struct dmabuf_buffer { int unused; }; enum surface_role { ROLE_NONE, ROLE_TOPLEVEL, ROLE_SUBSURFACE }; struct surface { @@ -30,6 +30,8 @@ struct surface { struct wl_resource *dmabuf; struct dmabuf_buffer *dmabuf_buf; struct wl_listener dmabuf_destroy; + struct wl_resource *pixel; + struct wl_listener pixel_destroy; int64_t next_release_ns; int releases_pending; enum surface_role role; @@ -47,6 +49,7 @@ static int g_release_unpaced; static int64_t g_now = 1000000000LL; static int64_t g_armed; static struct dmabuf_buffer g_buf; +static int g_late_dispatch; static int64_t now_ns(void) { return g_now; } static void perf_note_release(int64_t since) { (void)since; } @@ -64,10 +67,16 @@ static int timerfd_settime(int fd, int flags, const struct itimerspec *its, stru g_armed = (int64_t)its->it_value.tv_sec * 1000000000LL + its->it_value.tv_nsec; return 0; } +static void dispatch(struct wl_resource *b) { + for (; b->in_flight; b->in_flight--) { + if (b->acquired) b->acquired = 0; + else b->errors++; + } +} static void send_buffer_release(struct wl_resource *b) { b->releases++; - if (b->acquired) b->acquired = 0; - else b->errors++; + b->in_flight++; + if (!g_late_dispatch) dispatch(b); } #include "release.inc" @@ -75,6 +84,7 @@ static void send_buffer_release(struct wl_resource *b) { static void present(struct wl_resource *b) { b->acquired = 1; } static void commit(struct surface *s, struct wl_resource *b) { + if (s->pixel != b) drop_pixel(s); if (s->dmabuf != b) { drop_dmabuf(s, 1); if (b) { @@ -87,6 +97,13 @@ static void commit(struct surface *s, struct wl_resource *b) { pending_releases_apply(s); } +static void commit_pixel(struct surface *s, struct wl_resource *b) { + if (s->pixel != b) drop_pixel(s); + drop_dmabuf(s, 1); + hold_pixel(s, b); + pending_releases_apply(s); +} + static void timer_fires(int64_t at) { if (g_now < at) g_now = at; release_due_buffers(); @@ -300,6 +317,57 @@ static void set_desync_frees_held_release(void) { check(&d, 0); } +static void backing_attached_every_frame(void) { + struct surface root, plane; + struct wl_resource black, a, b, c; + struct wl_resource *frames[3] = {&a, &b, &c}; + surface_init(&root, ROLE_TOPLEVEL, NULL); + surface_init(&plane, ROLE_SUBSURFACE, &root); + buffer_init(&black); buffer_init(&a); buffer_init(&b); buffer_init(&c); + g_late_dispatch = 1; + for (int i = 0; i < 6; i += 2) { + present(&black); present(frames[i % 3]); + present(&black); present(frames[(i + 1) % 3]); + commit(&plane, frames[i % 3]); commit_pixel(&root, &black); + commit(&plane, frames[(i + 1) % 3]); commit_pixel(&root, &black); + timer_fires(g_now + 100000000LL); + dispatch(&black); dispatch(&a); dispatch(&b); dispatch(&c); + } + check(&black, 0); + check(&a, 2); + check(&b, 2); + check(&c, 1); + present(&a); + commit(&plane, NULL); + commit(&root, &a); + timer_fires(g_now + 100000000LL); + dispatch(&black); dispatch(&a); dispatch(&b); dispatch(&c); + check(&black, 1); + check(&a, 2); + check(&c, 2); + present(&black); + commit_pixel(&root, &black); + wl_list_remove(&root.link); + drop_pixel(&root); + dispatch(&black); + check(&black, 2); +} + +static void backing_moved_to_another_surface(void) { + struct surface low, high; + struct wl_resource black; + surface_init(&low, ROLE_TOPLEVEL, NULL); + surface_init(&high, ROLE_TOPLEVEL, NULL); + buffer_init(&black); + present(&black); + commit_pixel(&low, &black); + commit_pixel(&high, &black); + commit(&low, NULL); + check(&black, 0); + commit(&high, NULL); + check(&black, 1); +} + int main(int argc, char **argv) { assert(argc == 2); wl_list_init(&g_surfaces); @@ -313,6 +381,8 @@ int main(int argc, char **argv) { else if (!strcmp(argv[1], "destroyed")) holder_destroyed(); else if (!strcmp(argv[1], "nested")) desync_parent_holds_sync_grandchild(); else if (!strcmp(argv[1], "set-desync")) set_desync_frees_held_release(); + else if (!strcmp(argv[1], "backing")) backing_attached_every_frame(); + else if (!strcmp(argv[1], "backing-moved")) backing_moved_to_another_surface(); else return 2; return 0; } diff --git a/tools/tests/test_subsurface_release.py b/tools/tests/test_subsurface_release.py index 2254eb1a71..c297cc988f 100644 --- a/tools/tests/test_subsurface_release.py +++ b/tools/tests/test_subsurface_release.py @@ -12,7 +12,7 @@ FUNCTIONS = ( 'pending_release_free', 'pending_releases_forget_surface', 'pending_releases_cancel', 'pending_releases_apply', 'sync_holder', 'pending_releases_rehold', 'on_pending_release_buffer_destroyed', 'arm_release_timer', 'release_due_buffers', 'release_buffer', 'buffer_shown_elsewhere', - 'drop_dmabuf', + 'drop_dmabuf', 'on_pixel_destroyed', 'hold_pixel', 'drop_pixel', ) @@ -83,6 +83,20 @@ class SubsurfaceReleaseTest(unittest.TestCase): def test_set_desync_releases_what_the_old_parent_held(self): self.check_case('set-desync') + def test_backing_attached_every_frame_is_released_once_when_replaced(self): + self.check_case('backing') + + def test_backing_shown_on_two_surfaces_is_released_by_the_last(self): + self.check_case('backing-moved') + + def test_compositor_holds_single_pixel_buffers_while_shown(self): + self.assertNotIn('send_buffer_release', function_body(self.source, 'take_pixel')) + commit = function_body(self.source, 'surface_commit') + self.assertLess(commit.index('if (s->pixel != buffer) drop_pixel(s);'), commit.index('if (s->role == ROLE_CURSOR)')) + self.assertLess(commit.index('take_pixel(s, pixel);'), commit.index('hold_pixel(s, buffer);')) + self.assertIn('drop_pixel(s);', function_body(self.source, 'surface_resource_destroy')) + self.assertIn('drop_pixel(s);', function_body(self.source, 'subsurface_resource_destroy')) + def test_compositor_attach_and_commit_use_the_release_hold(self): self.assertIn('pending_releases_cancel(buffer);', function_body(self.source, 'take_dmabuf')) commit = function_body(self.source, 'surface_commit') diff --git a/tools/tests/vk_terminal_callers_test.c b/tools/tests/vk_terminal_callers_test.c index 374c049a6f..3ee2429319 100644 --- a/tools/tests/vk_terminal_callers_test.c +++ b/tools/tests/vk_terminal_callers_test.c @@ -234,28 +234,29 @@ static void upload_timeout(int single_pixel) { struct wl_shm_buffer b = {.w = img->w, .h = img->h, .stride = img->w * 4, .data = data}; struct wl_resource resource = {0}; expected_release = &resource; - if (single_pixel) take_pixel(&s, data, &resource); else take_shm(&s, &b, &resource); - assert(bytes[3] == 0x3c && bytes[0] == 0xa5 && releases == 1); + const unsigned per_take = single_pixel ? 0 : 1; + if (single_pixel) take_pixel(&s, data); else take_shm(&s, &b, &resource); + assert(bytes[3] == 0x3c && bytes[0] == 0xa5 && releases == per_take); memcpy(saved, bytes, sizeof(saved)); struct vkp_image dst = {.image = (VkImage)(uintptr_t)9, .blit_dst = 1, .w = img->w, .h = img->h}; fence_answer = VK_TIMEOUT; assert(vkp_blit_image(img, &dst, -1) == -1); assert(g_dev_state == -3 && submits == 1 && waits == 1); memset(data, 0x7e, sizeof(data)); - if (single_pixel) take_pixel(&s, data, &resource); else take_shm(&s, &b, &resource); - assert(s.shm_img == img && memcmp(saved, bytes, sizeof(saved)) == 0 && releases == 2); + if (single_pixel) take_pixel(&s, data); else take_shm(&s, &b, &resource); + assert(s.shm_img == img && memcmp(saved, bytes, sizeof(saved)) == 0 && releases == 2 * per_take); struct wl_resource dmabuf = {.dmabuf = 1}; expected_release = &dmabuf; assert(buffer_release_blocked(&dmabuf)); send_buffer_release(&dmabuf); - assert(releases == 2); + assert(releases == 2 * per_take); vkp_image_destroy(img); assert(destroys == 0 && img->map == bytes && img->image && img->mem); assert_stopped(); g_dev_state = 1; assert(!buffer_release_blocked(&dmabuf)); send_buffer_release(&dmabuf); - assert(releases == 3); + assert(releases == 2 * per_take + 1); free(img); free(g_images); /* fake resources only, not production cleanup */ }