Modern Apps git

← Modern-Apps.git

library: bound the swapchain image wait at 100 ms (#maprender)

Commit
57b0e937bb784afe63e5e33d98450457fb92b449
Author
Vayun Mathur <[email protected]>
When
2026-11-13 20:07 UTC (22 days ago)
Parents
de779a6
Owner report: "I sometimes get 'Maps isn't responding'". An ANR is the
main thread blocked for 5 s; this removes one unbounded block.

Root cause: both frame paths (Earth `Renderer::render` and the Moon
`render_moon`) called `vkAcquireNextImageKHR` with a `u64::MAX`
timeout, on the Choreographer callback, i.e. the main thread. The map
draws into a TextureView's SurfaceTexture: its buffers return to the
producer only when the UI thread composites the next frame. Once every
swapchain image is queued (2 frames in flight, FIFO when MAILBOX is not
offered, a UI thread busy with a recomposition, GC or a binder call),
acquire waits for a buffer that only the UI thread can release, while
the UI thread is the one waiting. That is a self-stall with no upper
bound and a direct ANR path.

What changed: acquire waits at most `ACQUIRE_TIMEOUT_NANOS` (100 ms,
six vsyncs). `VK_TIMEOUT`/`VK_NOT_READY` now mean "frame skipped":
`render` returns Ok(false), the same "not drawn" answer an out-of-date
swapchain gives, and the host's existing loop asks again on the next
vsync. Nothing has been recorded or submitted at that point, so no
state needs unwinding (it is the same early return the out-of-date
path already takes). The GPU fence wait stays unbounded: it completes
on the GPU's own progress, not the UI thread's.

Measured effect: worst-case main-thread block inside one acquire goes
from unbounded to 100 ms. A healthy present loop is unaffected (the
acquire step on the frame-stats overlay reads ~0 ms there). This also
makes the render-thread move in the next commit deadlock-free: a
detach waiting on the render thread can no longer sit behind an
acquire that needs the waiting UI thread.

Verified: `cargo check --target aarch64-linux-android` (the Vulkan
layer is android-only and has no host tests).

Diff

library/map/src/main/rust/src/vulkan/renderer/frame.rs
diff --git a/library/map/src/main/rust/src/vulkan/renderer/frame.rs b/library/map/src/main/rust/src/vulkan/renderer/frame.rsindex 6044d7058b..c861b1be98 100644--- a/library/map/src/main/rust/src/vulkan/renderer/frame.rs+++ b/library/map/src/main/rust/src/vulkan/renderer/frame.rs@@ -370,7 +370,7 @@ impl Renderer {         let acquired = unsafe {             self.swapchain.loader.acquire_next_image(                 self.swapchain.swapchain,-                u64::MAX,+                super::ACQUIRE_TIMEOUT_NANOS,                 frame.image_available,                 vk::Fence::null(),             )@@ -384,6 +384,8 @@ impl Renderer {                 self.needs_rebuild = true;                 return Ok(false);             }+            // No image within the bound: skipped, not failed; the host asks again.+            Err(vk::Result::TIMEOUT | vk::Result::NOT_READY) => return Ok(false),             Err(e) => return Err(format!("acquire_next_image {e:?}")),         }; 
library/map/src/main/rust/src/vulkan/renderer/mod.rs
diff --git a/library/map/src/main/rust/src/vulkan/renderer/mod.rs b/library/map/src/main/rust/src/vulkan/renderer/mod.rsindex be3dcbcf0c..0af7e90b8e 100644--- a/library/map/src/main/rust/src/vulkan/renderer/mod.rs+++ b/library/map/src/main/rust/src/vulkan/renderer/mod.rs@@ -45,6 +45,17 @@ pub(crate) use quad::{Quad, QUAD_INDICES, QUAD_VERTICES}; /// without adding latency the user can feel when panning. const FRAMES_IN_FLIGHT: usize = 2; +/// How long a frame may wait for a swapchain image before it is skipped, in nanoseconds.+///+/// Was `u64::MAX`. The map draws into a `TextureView`'s `SurfaceTexture`, whose buffers are+/// only released when the UI thread composites the next frame; an image that cannot come back+/// until the UI thread runs must never be waited on without a bound, or a busy UI thread and a+/// waiting frame stall each other — on the main thread, that was a frozen app and the "Maps+/// isn't responding" dialog. 100 ms is six vsyncs: a healthy present loop never comes close,+/// and a starved one skips the frame (`render` answers "not drawn", the host asks again on the+/// next vsync) instead of blocking its thread.+const ACQUIRE_TIMEOUT_NANOS: u64 = 100_000_000;+ /// One layer's geometry, resident on the GPU. /// /// The mesh lives in its format pool on the tile ([`ResidentTile::flat`] for fills,
library/map/src/main/rust/src/vulkan/renderer/record_moon.rs
diff --git a/library/map/src/main/rust/src/vulkan/renderer/record_moon.rs b/library/map/src/main/rust/src/vulkan/renderer/record_moon.rsindex 13e56fe332..58c8010cf8 100644--- a/library/map/src/main/rust/src/vulkan/renderer/record_moon.rs+++ b/library/map/src/main/rust/src/vulkan/renderer/record_moon.rs@@ -39,7 +39,7 @@ impl Renderer {             let frame = &self.frames[self.frame_index];             match self.swapchain.loader.acquire_next_image(                 self.swapchain.swapchain,-                u64::MAX,+                super::ACQUIRE_TIMEOUT_NANOS,                 frame.image_available,                 vk::Fence::null(),             ) {@@ -48,6 +48,7 @@ impl Renderer {                     self.needs_rebuild = true;                     return Ok(false);                 }+                Err(vk::Result::TIMEOUT | vk::Result::NOT_READY) => return Ok(false),                 Err(e) => return Err(format!("acquire_next_image {e:?}")),             }         };