跪拜 Guibai
← All articles
Frontend · Android · Flutter

One Missed Latch Can Permanently Double Flutter's Raster Latency on Android

By 恋猫de小郭 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

One missed latch can permanently double Raster latency on Android's KHR swapchain path, and the usual CPU/GPU profiling shows no cause because per-frame work stays flat. Flipping on `EnableHcpp` to use the AHB swapchain keeps Raster at 2.4–2.7 ms, so Flutter apps with stutter after rotation have a clear path out.

Summary

On a Galaxy A16 running Android 17 with Flutter 3.50.1 and Impeller Vulkan, rotating the screen a few times pushes Raster p95 from ~2.7 ms to 5.0–7.2 ms, and the penalty sticks for as long as the swapchain goes unrecreated. The Raster thread's per-frame CPU time stays at 2.5–2.7 ms, so the extra latency is pure waiting, not extra work. GPU completion latency climbs from 3–7 ms to 10.9–11.0 ms, and SurfaceFlinger's time-weighted queued-buffer reading moves from ~3.3 to ~4.9.

The cause is a missed latch. In the Vulkan KHR swapchain path, when a frame misses the latch window, that buffer joins a queue. Even after the app resumes producing one buffer per vsync and SurfaceFlinger consumes one per vsync, the queue depth stays at N+1 forever because production and consumption rates are exactly matched. The swapchain's finite image count then forces Raster to wait for fences and available images, stretching its end-to-end latency. `CreateSwapchainKHR` on rotation takes 17–44 ms and can itself miss a latch, so rotation sometimes fixes the state and sometimes creates it; even first startup can land in the backlog.

The workaround is to switch away from KHR. Adding the `io.flutter.embedding.android.EnableHcpp` meta-data to true makes Flutter use the AHB/SurfaceControl swapchain. On the same device, after 11 consecutive rotations, Raster p95 stayed at 2.4–2.7 ms. The KHR path lacks any frame pacing to drain the extra buffer, which is why the backlog becomes permanent.

Takeaways
— Raster p95 jumps from about 2.7 ms to 5.0–7.2 ms and stays there after a missed latch.
— Raster's per-frame CPU time remains 2.5–2.7 ms; the extra latency is wait time.
— GPU completion latency rises from 3–7 ms to 10.9–11.0 ms.
— SurfaceFlinger's time-weighted QueuedBuffer reading goes from 3.3–3.9 to 4.8–4.9.
— The KHR swapchain uses FIFO present mode, `minImageCount + 1` images, and two in-flight sync sets with no frame pacing for an extra buffer.
— `CreateSwapchainKHR` takes 17–44 ms per screen rotation and can itself cause a missed latch.
— Enabling `io.flutter.embedding.android.EnableHcpp` to true switches to AHB/SurfaceControl swapchain; after 11 rotations Raster p95 stayed at 2.4–2.7 ms.
Conclusions

A single missed latch flips the swapchain queue from N to N+1, and because production and consumption both return to one per vsync, the extra buffer never drains.

The KHR swapchain design lacks a mechanism to shed the extra buffer once the backlog forms; the AHB/SurfaceControl path either paces differently or manages buffer count more aggressively.

Because the issue can also appear on first start, it's not a rotation-specific edge case; it's a buffer-management gap that rotation merely tends to trigger.

Concepts & terms
Latch
In Android's display pipeline, SurfaceFlinger latches a buffer when it picks it up for composition. A missed latch means a freshly rendered buffer waits for the next vsync slot, leaving it in the queue.
Swapchain
In Vulkan, a swapchain is a queue of presentable images. The app acquires an image, renders into it, and presents it; the swapchain's image count and present mode affect how frames flow.
Raster thread
Flutter's thread that records and submits GPU work (e.g., Impeller draws). Its timing is reported as 'Raster' in Flutter's frame timeline.
Impeller
Flutter's modern rendering engine that uses Vulkan or Metal directly, replacing the older Skia-based backend on Android and iOS.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗