From cd1e9dde15e30c898bba7260ec17eaeb62ef83c5 Mon Sep 17 00:00:00 2001 From: LunaDL <91587464+EnzoDLofi@users.noreply.github.com> Date: Sun, 29 Nov 2026 00:47:47 -0400 Subject: [PATCH 1/9] Fix zero-copy layer transition callback failures --- app/src/main/cpp/waylandcomp/src/sc_layer.c | 37 +++++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/app/src/main/cpp/waylandcomp/src/sc_layer.c b/app/src/main/cpp/waylandcomp/src/sc_layer.c index 74cd5d6b45..f966abd644 100644 --- a/app/src/main/cpp/waylandcomp/src/sc_layer.c +++ b/app/src/main/cpp/waylandcomp/src/sc_layer.c @@ -397,12 +397,16 @@ static void retire_sc(struct layer *l) { ASurfaceTransaction *tx = api.txCreate(); if (tx) { retire_ops(tx, l->sc); - if (add_complete(tx, l, l->cur_slot, l->cur_token, 1) != 0) api.release(l->sc); + if (add_complete(tx, l, l->cur_slot, l->cur_token, 1) != 0) { + api.txDelete(tx); + droiddeck_log("layer", "%s: retirement deferred because its release callback could not be allocated", l->name); + return; + } api.txApply(tx); api.txDelete(tx); } else { - if (l->cur_token) ahb_swapchain_layer_released(l->cur_token, -1); - api.release(l->sc); + droiddeck_log("layer", "%s: retirement deferred because a transaction could not be allocated", l->name); + return; } droiddeck_log("layer", "%s: SurfaceControl retired (window %p)", l->name, (void *)l->win); l->sc = NULL; l->win = NULL; @@ -832,6 +836,16 @@ static ASurfaceControl *swap_sc_begin(struct layer *l) { return old; } +static void rollback_swap_sc(struct layer *l, ASurfaceControl *old) { + if (!old) return; + ASurfaceControl *fresh = l->sc; + l->sc = old; + l->shown = 1; + l->recreate_pending = 1; + l->geo_valid = 0; + if (fresh) api.release(fresh); +} + static int present_slot(struct layer *l, int idx, const int r[8], const struct droiddeck_color *color, int translucent) { struct slot *s = &l->slots[idx]; @@ -848,8 +862,12 @@ static int present_slot(struct layer *l, int idx, const int r[8], const struct d apply_colour(tx, l, color); if (!l->shown) api.setVisibility(tx, l->sc, ASC_VISIBILITY_SHOW); if (old) retire_ops(tx, old); - if (add_complete_on(tx, l, old ? old : l->sc, l->cur_slot, l->cur_token, old ? 1 : 0) != 0 && old) - api.release(old); + if (add_complete_on(tx, l, old ? old : l->sc, l->cur_slot, l->cur_token, old ? 1 : 0) != 0) { + api.txDelete(tx); + rollback_swap_sc(l, old); + droiddeck_log("layer", "%s: frame transition skipped because its release callback could not be allocated", l->name); + return -1; + } pthread_mutex_lock(&g_lock); s->busy = 1; pthread_mutex_unlock(&g_lock); @@ -917,8 +935,9 @@ int sc_layer_present_ahb(AHardwareBuffer *ahb, int w, int h, int acquire_fd, voi if (add_complete_on(tx, l, old ? old : l->sc, l->cur_slot, l->cur_token == token ? NULL : l->cur_token, old ? 1 : 0) != 0) { api.txDelete(tx); - if (old) api.release(old); + rollback_swap_sc(l, old); l->ds_applied = -1; l->md_applied = 0; l->hr_applied = -1.0f; + droiddeck_log("layer", "zero-copy: frame transition skipped because its release callback could not be allocated"); return -1; } apply_present_time(tx, 1); @@ -1173,7 +1192,11 @@ static void hide_layer(struct layer *l) { if (!tx) return; replace_with_blank(tx, l); api.setVisibility(tx, l->sc, ASC_VISIBILITY_HIDE); - add_complete(tx, l, l->cur_slot, l->cur_token, 0); + if (add_complete(tx, l, l->cur_slot, l->cur_token, 0) != 0) { + api.txDelete(tx); + droiddeck_log("layer", "%s: hide deferred because its release callback could not be allocated", l->name); + return; + } api.txApply(tx); api.txDelete(tx); l->shown = 0; l->cur_slot = -1; l->cur_token = NULL; } From 3bef9a420007bd5242319d2fc12875a32aad476e Mon Sep 17 00:00:00 2001 From: LunaDL <91587464+EnzoDLofi@users.noreply.github.com> Date: Sun, 29 Nov 2026 01:03:25 -0400 Subject: [PATCH 2/9] Fix deferred zero-copy layer retirement and rollback --- app/src/main/cpp/waylandcomp/src/sc_layer.c | 95 +++++++++++++++------ 1 file changed, 70 insertions(+), 25 deletions(-) diff --git a/app/src/main/cpp/waylandcomp/src/sc_layer.c b/app/src/main/cpp/waylandcomp/src/sc_layer.c index f966abd644..bea1c4676e 100644 --- a/app/src/main/cpp/waylandcomp/src/sc_layer.c +++ b/app/src/main/cpp/waylandcomp/src/sc_layer.c @@ -148,6 +148,7 @@ struct slot { uint32_t fmt; int release_fd; int busy; + uint64_t generation; }; struct layer { @@ -222,6 +223,7 @@ struct complete_ctx { ASurfaceControl *sc; int layer; int prev_slot; + uint64_t prev_generation; void *prev_token; int retire; }; @@ -288,10 +290,12 @@ static void on_complete(void *context, ASurfaceTransactionStats *stats) { pthread_mutex_lock(&g_lock); if (ctx->layer >= 0 && ctx->layer < SC_LAYER_COUNT && ctx->prev_slot >= 0 && ctx->prev_slot < POOL_MAX) { struct slot *s = &g_layers[ctx->layer].slots[ctx->prev_slot]; - int fd = (stats && ctx->sc) ? api.prevReleaseFence(stats, ctx->sc) : -1; - if (s->release_fd >= 0) close(s->release_fd); - s->release_fd = fd; - s->busy = 0; + if (s->generation == ctx->prev_generation) { + int fd = (stats && ctx->sc) ? api.prevReleaseFence(stats, ctx->sc) : -1; + if (s->release_fd >= 0) close(s->release_fd); + s->release_fd = fd; + s->busy = 0; + } } if (g_pending_cb > 0) g_pending_cb--; pthread_mutex_unlock(&g_lock); @@ -308,6 +312,11 @@ static int add_complete_on(ASurfaceTransaction *tx, struct layer *l, ASurfaceCon struct complete_ctx *ctx = calloc(1, sizeof(*ctx)); if (!ctx) return -1; ctx->sc = sc; ctx->layer = (int)(l - g_layers); ctx->prev_slot = prev_slot; + if (prev_slot >= 0 && prev_slot < POOL_MAX) { + pthread_mutex_lock(&g_lock); + ctx->prev_generation = l->slots[prev_slot].generation; + pthread_mutex_unlock(&g_lock); + } ctx->prev_token = prev_token; ctx->retire = retire; pthread_mutex_lock(&g_lock); g_pending_cb++; @@ -439,6 +448,26 @@ static void drain_and_free_pools(void) { g_layers[i].logged_w = g_layers[i].logged_h = 0; } pthread_mutex_unlock(&g_lock); + for (int i = 0; i < SC_LAYER_COUNT; i++) { + if (g_layers[i].sc) { + droiddeck_log("layer", "pool drain deferred while %s still owns a SurfaceControl", g_layers[i].name); + return; + } + } + pthread_mutex_lock(&g_lock); + for (int i = 0; i < SC_LAYER_COUNT; i++) { + for (int j = 0; j < POOL_MAX; j++) { + struct slot *s = &g_layers[i].slots[j]; + if (s->release_fd >= 0) { struct pollfd p = {.fd = s->release_fd, .events = POLLIN}; poll(&p, 1, 100); close(s->release_fd); } + if (s->img) vkp_image_destroy(s->img); + if (s->ahb) AHardwareBuffer_release(s->ahb); + memset(s, 0, sizeof(*s)); + s->release_fd = -1; + } + g_layers[i].first_logged = 0; + g_layers[i].logged_w = g_layers[i].logged_h = 0; + } + pthread_mutex_unlock(&g_lock); } static int sniff_modifier(const struct droiddeck_native_handle *h, uint64_t *mod) { @@ -616,7 +645,10 @@ static int take_free_slot(struct layer *l, int w, int h, uint32_t fmt, int *wait if (pending < 0) pending = i; } if (idx < 0) idx = pending; - if (idx >= 0) { fd = l->slots[idx].release_fd; l->slots[idx].release_fd = -1; } + if (idx >= 0) { + fd = l->slots[idx].release_fd; l->slots[idx].release_fd = -1; + l->slots[idx].generation++; + } pthread_mutex_unlock(&g_lock); if (idx < 0) return -1; if (fd >= 0 && fence_signalled(fd)) { close(fd); fd = -1; } @@ -663,7 +695,13 @@ static int ensure_sc(struct layer *l) { if (load_api() != 0) return -1; ANativeWindow *win = vkp_window(); if (!win) return -1; - if (l->sc && l->win != win) retire_sc(l); + if (l->sc && l->win != win) { + retire_sc(l); + if (l->sc) { + droiddeck_log("layer", "%s: keeping the old SurfaceControl until retirement completes", l->name); + return -1; + } + } if (!l->sc) { l->sc = api.createFromWindow(win, l->name); if (!l->sc) { droiddeck_log("error", "layer: ASurfaceControl_createFromWindow(%s) failed", l->name); return -1; } @@ -836,13 +874,15 @@ static ASurfaceControl *swap_sc_begin(struct layer *l) { return old; } -static void rollback_swap_sc(struct layer *l, ASurfaceControl *old) { +static void rollback_swap_sc(struct layer *l, ASurfaceControl *old, int was_shown) { if (!old) return; ASurfaceControl *fresh = l->sc; l->sc = old; - l->shown = 1; + l->shown = was_shown; l->recreate_pending = 1; l->geo_valid = 0; + l->fps_applied = -1.0f; + l->ds_applied = -1; l->md_applied = 0; l->hr_applied = -1.0f; if (fresh) api.release(fresh); } @@ -851,23 +891,24 @@ static int present_slot(struct layer *l, int idx, const int r[8], const struct d struct slot *s = &l->slots[idx]; ASurfaceTransaction *tx = api.txCreate(); if (!tx) return -1; + const int was_shown = l->shown; ASurfaceControl *old = swap_sc_begin(l); if (old) api.setZOrder(tx, l->sc, l->z); api.setBuffer(tx, l->sc, s->ahb, -1); if (api.setEnableBackPressure) api.setEnableBackPressure(tx, l->sc, true); api.setBufferTransparency(tx, l->sc, translucent ? ASC_TRANSPARENCY_TRANSLUCENT : ASC_TRANSPARENCY_OPAQUE); - apply_geometry(tx, l, r); - apply_frame_rate(tx, l); - apply_colour(tx, l, color); if (!l->shown) api.setVisibility(tx, l->sc, ASC_VISIBILITY_SHOW); if (old) retire_ops(tx, old); if (add_complete_on(tx, l, old ? old : l->sc, l->cur_slot, l->cur_token, old ? 1 : 0) != 0) { api.txDelete(tx); - rollback_swap_sc(l, old); + rollback_swap_sc(l, old, was_shown); droiddeck_log("layer", "%s: frame transition skipped because its release callback could not be allocated", l->name); return -1; } + apply_geometry(tx, l, r); + apply_frame_rate(tx, l); + apply_colour(tx, l, color); pthread_mutex_lock(&g_lock); s->busy = 1; pthread_mutex_unlock(&g_lock); @@ -922,24 +963,24 @@ int sc_layer_present_ahb(AHardwareBuffer *ahb, int w, int h, int acquire_fd, voi } ASurfaceTransaction *tx = api.txCreate(); if (!tx) goto unavailable; + const int was_shown = l->shown; ASurfaceControl *old = swap_sc_begin(l); if (old) api.setZOrder(tx, l->sc, l->z); api.setBuffer(tx, l->sc, ahb, acquire_fd); if (api.setEnableBackPressure) api.setEnableBackPressure(tx, l->sc, true); api.setBufferTransparency(tx, l->sc, ASC_TRANSPARENCY_OPAQUE); - apply_geometry(tx, l, r); - apply_frame_rate(tx, l); - apply_colour(tx, l, color); if (!l->shown) api.setVisibility(tx, l->sc, ASC_VISIBILITY_SHOW); if (old) retire_ops(tx, old); if (add_complete_on(tx, l, old ? old : l->sc, l->cur_slot, l->cur_token == token ? NULL : l->cur_token, old ? 1 : 0) != 0) { api.txDelete(tx); - rollback_swap_sc(l, old); - l->ds_applied = -1; l->md_applied = 0; l->hr_applied = -1.0f; + rollback_swap_sc(l, old, was_shown); droiddeck_log("layer", "zero-copy: frame transition skipped because its release callback could not be allocated"); return -1; } + apply_geometry(tx, l, r); + apply_frame_rate(tx, l); + apply_colour(tx, l, color); apply_present_time(tx, 1); api.txApply(tx); api.txDelete(tx); @@ -1186,19 +1227,20 @@ int sc_layer_present_overlay(struct vkp_image *src, const int geo[8], int transl return 0; } -static void hide_layer(struct layer *l) { - if (!l->sc || !l->shown || api.state != 1) return; +static int hide_layer(struct layer *l) { + if (!l->sc || !l->shown || api.state != 1) return 0; ASurfaceTransaction *tx = api.txCreate(); - if (!tx) return; + if (!tx) return -1; replace_with_blank(tx, l); api.setVisibility(tx, l->sc, ASC_VISIBILITY_HIDE); if (add_complete(tx, l, l->cur_slot, l->cur_token, 0) != 0) { api.txDelete(tx); droiddeck_log("layer", "%s: hide deferred because its release callback could not be allocated", l->name); - return; + return -1; } api.txApply(tx); api.txDelete(tx); l->shown = 0; l->cur_slot = -1; l->cur_token = NULL; + return 0; } void sc_layer_hide(void) { @@ -1206,17 +1248,20 @@ void sc_layer_hide(void) { int said = g_layers[SC_LAYER_GAME].shown || g_layers[SC_LAYER_OVERLAY].sc; if (g_layers[SC_LAYER_OVERLAY].sc) sc_layer_hide_overlay(); if (g_layers[SC_LAYER_GAME].shown) hide_layer(&g_layers[SC_LAYER_GAME]); - if (said) droiddeck_log("layer", "layers hidden (scene is not a single fullscreen window)"); + if (said && !g_layers[SC_LAYER_GAME].shown && !g_layers[SC_LAYER_OVERLAY].shown) + droiddeck_log("layer", "layers hidden (scene is not a single fullscreen window)"); } void sc_layer_hide_overlay(void) { layers_init(); struct layer *l = &g_layers[SC_LAYER_OVERLAY]; if (!l->sc) return; - if (l->shown) hide_layer(l); + if (l->shown && hide_layer(l) != 0) return; retire_sc(l); - droiddeck_log("layer", "%s: gone (nothing is above the game any more)", l->name); - if (g_layers[SC_LAYER_GAME].sc) g_layers[SC_LAYER_GAME].recreate_pending = 1; + if (!l->sc) { + droiddeck_log("layer", "%s: gone (nothing is above the game any more)", l->name); + if (g_layers[SC_LAYER_GAME].sc) g_layers[SC_LAYER_GAME].recreate_pending = 1; + } } void sc_layer_window_gone(void) { From 689ecc40ad836b38fd7733cd3db71834e298cea9 Mon Sep 17 00:00:00 2001 From: LunaDL <91587464+EnzoDLofi@users.noreply.github.com> Date: Sun, 29 Nov 2026 01:03:36 -0400 Subject: [PATCH 3/9] Guard pool drain against pending layer callbacks --- app/src/main/cpp/waylandcomp/src/sc_layer.c | 26 +++++++-------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/app/src/main/cpp/waylandcomp/src/sc_layer.c b/app/src/main/cpp/waylandcomp/src/sc_layer.c index bea1c4676e..ee0e52cdc2 100644 --- a/app/src/main/cpp/waylandcomp/src/sc_layer.c +++ b/app/src/main/cpp/waylandcomp/src/sc_layer.c @@ -427,27 +427,18 @@ static void retire_sc(struct layer *l) { static void drain_and_free_pools(void) { int64_t deadline = now_ns() + 300000000LL; - for (;;) { + int pending; + do { pthread_mutex_lock(&g_lock); - int pending = g_pending_cb; + pending = g_pending_cb; pthread_mutex_unlock(&g_lock); if (!pending || now_ns() > deadline) break; usleep(2000); + } while (1); + if (pending) { + droiddeck_log("layer", "pool drain deferred while %d SurfaceControl callbacks are pending", pending); + return; } - pthread_mutex_lock(&g_lock); - for (int i = 0; i < SC_LAYER_COUNT; i++) { - for (int j = 0; j < POOL_MAX; j++) { - struct slot *s = &g_layers[i].slots[j]; - if (s->release_fd >= 0) { struct pollfd p = {.fd = s->release_fd, .events = POLLIN}; poll(&p, 1, 100); close(s->release_fd); } - if (s->img) vkp_image_destroy(s->img); - if (s->ahb) AHardwareBuffer_release(s->ahb); - memset(s, 0, sizeof(*s)); - s->release_fd = -1; - } - g_layers[i].first_logged = 0; - g_layers[i].logged_w = g_layers[i].logged_h = 0; - } - pthread_mutex_unlock(&g_lock); for (int i = 0; i < SC_LAYER_COUNT; i++) { if (g_layers[i].sc) { droiddeck_log("layer", "pool drain deferred while %s still owns a SurfaceControl", g_layers[i].name); @@ -656,7 +647,8 @@ static int take_free_slot(struct layer *l, int w, int h, uint32_t fmt, int *wait if (s->ahb && (s->w != w || s->h != h || s->fmt != fmt)) { if (fd >= 0) { close(fd); fd = -1; } vkp_image_destroy(s->img); AHardwareBuffer_release(s->ahb); - memset(s, 0, sizeof(*s)); s->release_fd = -1; + const uint64_t generation = s->generation; + memset(s, 0, sizeof(*s)); s->release_fd = -1; s->generation = generation; } if (fd >= 0 && !vkp_can_wait_sync_fd()) { if (fence_wait(fd, 100) == 0) { slot_keep_fence(s, fd); return -1; } From 31ee95250cac7a1a8598c5099fdf8dbad7823095 Mon Sep 17 00:00:00 2001 From: LunaDL <91587464+EnzoDLofi@users.noreply.github.com> Date: Sun, 29 Nov 2026 01:03:58 -0400 Subject: [PATCH 4/9] Keep layer recovery pending when surface creation fails --- app/src/main/cpp/waylandcomp/src/sc_layer.c | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/cpp/waylandcomp/src/sc_layer.c b/app/src/main/cpp/waylandcomp/src/sc_layer.c index ee0e52cdc2..0e8892eaa3 100644 --- a/app/src/main/cpp/waylandcomp/src/sc_layer.c +++ b/app/src/main/cpp/waylandcomp/src/sc_layer.c @@ -852,6 +852,7 @@ static ASurfaceControl *swap_sc_begin(struct layer *l) { if (!win || win != l->win) return NULL; ASurfaceControl *fresh = api.createFromWindow(win, l->name); if (!fresh) { + l->recreate_pending = 1; droiddeck_log("error", "layer: %s: composition recovery could not create a new SurfaceControl", l->name); return NULL; } From 97a998a3b93147489d86d20a2f9b455e7dae6d5c Mon Sep 17 00:00:00 2001 From: LunaDL <91587464+EnzoDLofi@users.noreply.github.com> Date: Sun, 29 Nov 2026 01:04:11 -0400 Subject: [PATCH 5/9] Defer pool cleanup only for layers still in use --- app/src/main/cpp/waylandcomp/src/sc_layer.c | 35 +++++++++++---------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/app/src/main/cpp/waylandcomp/src/sc_layer.c b/app/src/main/cpp/waylandcomp/src/sc_layer.c index 0e8892eaa3..4848072337 100644 --- a/app/src/main/cpp/waylandcomp/src/sc_layer.c +++ b/app/src/main/cpp/waylandcomp/src/sc_layer.c @@ -176,6 +176,7 @@ struct layer { int votes_rate; float fps_applied; int recreate_pending; + int pending_callbacks; int32_t ds_applied; @@ -298,6 +299,8 @@ static void on_complete(void *context, ASurfaceTransactionStats *stats) { } } if (g_pending_cb > 0) g_pending_cb--; + if (ctx->layer >= 0 && ctx->layer < SC_LAYER_COUNT && g_layers[ctx->layer].pending_callbacks > 0) + g_layers[ctx->layer].pending_callbacks--; pthread_mutex_unlock(&g_lock); if (ctx->prev_token) { int fd = (stats && ctx->sc) ? api.prevReleaseFence(stats, ctx->sc) : -1; @@ -320,6 +323,7 @@ static int add_complete_on(ASurfaceTransaction *tx, struct layer *l, ASurfaceCon ctx->prev_token = prev_token; ctx->retire = retire; pthread_mutex_lock(&g_lock); g_pending_cb++; + l->pending_callbacks++; pthread_mutex_unlock(&g_lock); api.setOnComplete(tx, ctx, on_complete); return 0; @@ -427,38 +431,35 @@ static void retire_sc(struct layer *l) { static void drain_and_free_pools(void) { int64_t deadline = now_ns() + 300000000LL; - int pending; - do { + for (;;) { pthread_mutex_lock(&g_lock); - pending = g_pending_cb; + int pending = g_pending_cb; pthread_mutex_unlock(&g_lock); if (!pending || now_ns() > deadline) break; usleep(2000); - } while (1); - if (pending) { - droiddeck_log("layer", "pool drain deferred while %d SurfaceControl callbacks are pending", pending); - return; } for (int i = 0; i < SC_LAYER_COUNT; i++) { - if (g_layers[i].sc) { - droiddeck_log("layer", "pool drain deferred while %s still owns a SurfaceControl", g_layers[i].name); - return; + struct layer *l = &g_layers[i]; + pthread_mutex_lock(&g_lock); + const int pending = l->pending_callbacks; + if (pending || l->sc) { + pthread_mutex_unlock(&g_lock); + droiddeck_log("layer", "pool drain deferred for %s (%s)", l->name, + pending ? "SurfaceControl callbacks are pending" : "SurfaceControl retirement is deferred"); + continue; } - } - pthread_mutex_lock(&g_lock); - for (int i = 0; i < SC_LAYER_COUNT; i++) { for (int j = 0; j < POOL_MAX; j++) { - struct slot *s = &g_layers[i].slots[j]; + struct slot *s = &l->slots[j]; if (s->release_fd >= 0) { struct pollfd p = {.fd = s->release_fd, .events = POLLIN}; poll(&p, 1, 100); close(s->release_fd); } if (s->img) vkp_image_destroy(s->img); if (s->ahb) AHardwareBuffer_release(s->ahb); memset(s, 0, sizeof(*s)); s->release_fd = -1; } - g_layers[i].first_logged = 0; - g_layers[i].logged_w = g_layers[i].logged_h = 0; + l->first_logged = 0; + l->logged_w = l->logged_h = 0; + pthread_mutex_unlock(&g_lock); } - pthread_mutex_unlock(&g_lock); } static int sniff_modifier(const struct droiddeck_native_handle *h, uint64_t *mod) { From c833514757970638cce1f540962bc4a2ed9d9437 Mon Sep 17 00:00:00 2001 From: LunaDL <91587464+EnzoDLofi@users.noreply.github.com> Date: Sun, 29 Nov 2026 01:17:14 -0400 Subject: [PATCH 6/9] Restore HDR caches when rolling back layer swap --- app/src/main/cpp/waylandcomp/src/sc_layer.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/app/src/main/cpp/waylandcomp/src/sc_layer.c b/app/src/main/cpp/waylandcomp/src/sc_layer.c index 4848072337..a0e3df4941 100644 --- a/app/src/main/cpp/waylandcomp/src/sc_layer.c +++ b/app/src/main/cpp/waylandcomp/src/sc_layer.c @@ -868,7 +868,8 @@ static ASurfaceControl *swap_sc_begin(struct layer *l) { return old; } -static void rollback_swap_sc(struct layer *l, ASurfaceControl *old, int was_shown) { +static void rollback_swap_sc(struct layer *l, ASurfaceControl *old, int was_shown, + int32_t ds_applied, uint32_t md_applied, float hr_applied) { if (!old) return; ASurfaceControl *fresh = l->sc; l->sc = old; @@ -876,7 +877,7 @@ static void rollback_swap_sc(struct layer *l, ASurfaceControl *old, int was_show l->recreate_pending = 1; l->geo_valid = 0; l->fps_applied = -1.0f; - l->ds_applied = -1; l->md_applied = 0; l->hr_applied = -1.0f; + l->ds_applied = ds_applied; l->md_applied = md_applied; l->hr_applied = hr_applied; if (fresh) api.release(fresh); } @@ -886,6 +887,9 @@ static int present_slot(struct layer *l, int idx, const int r[8], const struct d ASurfaceTransaction *tx = api.txCreate(); if (!tx) return -1; const int was_shown = l->shown; + const int32_t ds_applied = l->ds_applied; + const uint32_t md_applied = l->md_applied; + const float hr_applied = l->hr_applied; ASurfaceControl *old = swap_sc_begin(l); if (old) api.setZOrder(tx, l->sc, l->z); api.setBuffer(tx, l->sc, s->ahb, -1); @@ -896,7 +900,7 @@ static int present_slot(struct layer *l, int idx, const int r[8], const struct d if (old) retire_ops(tx, old); if (add_complete_on(tx, l, old ? old : l->sc, l->cur_slot, l->cur_token, old ? 1 : 0) != 0) { api.txDelete(tx); - rollback_swap_sc(l, old, was_shown); + rollback_swap_sc(l, old, was_shown, ds_applied, md_applied, hr_applied); droiddeck_log("layer", "%s: frame transition skipped because its release callback could not be allocated", l->name); return -1; } @@ -958,6 +962,9 @@ int sc_layer_present_ahb(AHardwareBuffer *ahb, int w, int h, int acquire_fd, voi ASurfaceTransaction *tx = api.txCreate(); if (!tx) goto unavailable; const int was_shown = l->shown; + const int32_t ds_applied = l->ds_applied; + const uint32_t md_applied = l->md_applied; + const float hr_applied = l->hr_applied; ASurfaceControl *old = swap_sc_begin(l); if (old) api.setZOrder(tx, l->sc, l->z); api.setBuffer(tx, l->sc, ahb, acquire_fd); @@ -968,7 +975,7 @@ int sc_layer_present_ahb(AHardwareBuffer *ahb, int w, int h, int acquire_fd, voi if (add_complete_on(tx, l, old ? old : l->sc, l->cur_slot, l->cur_token == token ? NULL : l->cur_token, old ? 1 : 0) != 0) { api.txDelete(tx); - rollback_swap_sc(l, old, was_shown); + rollback_swap_sc(l, old, was_shown, ds_applied, md_applied, hr_applied); droiddeck_log("layer", "zero-copy: frame transition skipped because its release callback could not be allocated"); return -1; } From d2031ba3849ec712a9cf2a99676f53d8679de4a5 Mon Sep 17 00:00:00 2001 From: LunaDL <91587464+EnzoDLofi@users.noreply.github.com> Date: Sun, 29 Nov 2026 01:43:57 -0400 Subject: [PATCH 7/9] Fix deferred layer hide when callback allocation fails --- app/src/main/cpp/waylandcomp/src/sc_layer.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/src/main/cpp/waylandcomp/src/sc_layer.c b/app/src/main/cpp/waylandcomp/src/sc_layer.c index a0e3df4941..546ea82f40 100644 --- a/app/src/main/cpp/waylandcomp/src/sc_layer.c +++ b/app/src/main/cpp/waylandcomp/src/sc_layer.c @@ -1235,9 +1235,16 @@ static int hide_layer(struct layer *l) { replace_with_blank(tx, l); api.setVisibility(tx, l->sc, ASC_VISIBILITY_HIDE); if (add_complete(tx, l, l->cur_slot, l->cur_token, 0) != 0) { + /* + * Hiding does not require a release callback. Apply the visibility + * change now, but keep the current slot/token pinned until a later + * presentation or retirement can attach a completion callback. + */ + api.txApply(tx); api.txDelete(tx); - droiddeck_log("layer", "%s: hide deferred because its release callback could not be allocated", l->name); - return -1; + l->shown = 0; + droiddeck_log("layer", "%s: hidden; buffer release deferred because its callback could not be allocated", l->name); + return 0; } api.txApply(tx); api.txDelete(tx); l->shown = 0; l->cur_slot = -1; l->cur_token = NULL; From 9b7ce457a5c72f36c15c7bf01e83d18b5f75be44 Mon Sep 17 00:00:00 2001 From: LunaDL <91587464+EnzoDLofi@users.noreply.github.com> Date: Sun, 29 Nov 2026 01:51:24 -0400 Subject: [PATCH 8/9] Keep current buffer attached when hide callback allocation fails --- app/src/main/cpp/waylandcomp/src/sc_layer.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/src/main/cpp/waylandcomp/src/sc_layer.c b/app/src/main/cpp/waylandcomp/src/sc_layer.c index 546ea82f40..018664f9c3 100644 --- a/app/src/main/cpp/waylandcomp/src/sc_layer.c +++ b/app/src/main/cpp/waylandcomp/src/sc_layer.c @@ -1232,13 +1232,11 @@ static int hide_layer(struct layer *l) { if (!l->sc || !l->shown || api.state != 1) return 0; ASurfaceTransaction *tx = api.txCreate(); if (!tx) return -1; - replace_with_blank(tx, l); api.setVisibility(tx, l->sc, ASC_VISIBILITY_HIDE); if (add_complete(tx, l, l->cur_slot, l->cur_token, 0) != 0) { /* - * Hiding does not require a release callback. Apply the visibility - * change now, but keep the current slot/token pinned until a later - * presentation or retirement can attach a completion callback. + * Keep the current buffer attached when its release callback cannot + * be allocated. The hide can still apply without releasing the slot. */ api.txApply(tx); api.txDelete(tx); @@ -1246,6 +1244,7 @@ static int hide_layer(struct layer *l) { droiddeck_log("layer", "%s: hidden; buffer release deferred because its callback could not be allocated", l->name); return 0; } + replace_with_blank(tx, l); api.txApply(tx); api.txDelete(tx); l->shown = 0; l->cur_slot = -1; l->cur_token = NULL; return 0; From fe4f619973e1401682a889221a523bd9873ea285 Mon Sep 17 00:00:00 2001 From: LunaDL <91587464+EnzoDLofi@users.noreply.github.com> Date: Sun, 29 Nov 2026 12:51:13 -0400 Subject: [PATCH 9/9] chore: trigger CI rerun