跪拜 Guibai
← All articles
Frontend · Android · Flutter

Flutter Rotation ANR After Hybrid Composition: The Deadlock and the Fix

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

Developers shipping Flutter on Android can now avoid a UI hang that occurs when rotating after a Hybrid Composition view has closed. With Android 16/17 dropping forced portrait lock, rotation is no longer a rare edge case, so this fix matters for real-world apps.

Summary

After an HC PlatformView leaves the page, the Raster thread splits back from the Android main thread, but SurfacePool can retain overlay layers from the old frame size. On the next resize (rotation, fold, split-screen), Raster calls DestroySurfaces() and synchronously waits for the Platform thread, while the Platform thread may be waiting on Raster for getBitmap() or surfaceChanged. The resulting two-way wait freezes the app.

PR #196255 decouples the two sides: Raster releases its GPU surfaces and Vulkan swapchain directly through ReleaseOverlayLayers() without waiting, while the Java ImageReader teardown is posted asynchronously with a pending_java_destroy_ flag. Tests on a Pixel 8 (Android 14/API 34, Impeller Vulkan) show the resize+getBitmap() deadlock drops from 20/20 to 0/20, and Raster's p50 draw time falls from 81.76ms to 15.51ms.

The same deadlock pattern hit HCPP, fixed earlier in #190638 by resizing persistent overlays in place. Flutter 3.47.9 remains vulnerable on both paths; 3.50.0 addresses HCPP only, and legacy HC gets the fix on master. Android 16/17's move to ignore portrait lock makes rotation more common, raising the stakes for this fix.

Takeaways
— Rotation ANR occurs when Hybrid Composition PlatformView exits but cached OverlayLayers remain in SurfacePool; a subsequent frame size change triggers a synchronous cross-thread wait between Raster and Platform threads.
— PR #196255 introduces ReleaseOverlayLayers() which lets the Raster thread release GPU surfaces and Vulkan swapchain without waiting on the Platform thread; Java ImageReader destruction is posted asynchronously via DestroyPendingLayers().
— With the fix, the resize+getBitmap() deadlock test went from 20/20 failures to 0/20 on Pixel 8 with Android 14/API 34/Impeller Vulkan.
— Raster GPURasterizer::Draw p50 improved from 81.76ms to 15.51ms, and Raster sleep during resize p50 from 60.80ms to 0.37ms, after the fix.
— HCPP had a similar rotation deadlock fixed in #190638 by resizing persistent overlays in place instead of synchronous destruction; the fix is present in Flutter 3.50.0 but legacy HC only gets it on master.
— Flutter 3.47.9 remains vulnerable for both HC and HCPP; upgrading to at least 3.50.0 addresses HCPP, and master's #196255 addresses legacy HC.
— Android 16 and 17 begin ignoring user-forced portrait lock, increasing the chance that rotation-triggered ANRs like this appear on newer devices.
Conclusions

The deadlock arises because the engine treats surface destruction as a synchronous cross-thread operation; decoupling GPU and Java resource lifetimes removes the dependency that lets the two threads wait on each other.

The benchmark numbers show that before the fix, Raster spent most of its time during resize waiting for the platform thread's latch; after the fix, draw time dropped by over 80%, revealing that the synchronization overhead dwarfed actual GPU work.

The same root cause appearing in both legacy HC and HCPP indicates a systemic design flaw in the external view embedder, not an isolated platform quirk.

Android 16/17's willingness to ignore user portrait lock means any Flutter app still on 3.47.9 is just one rotation away from this ANR on newer devices.

Concepts & terms
Hybrid Composition (HC)
A Flutter Android embedding mode that renders native views (e.g., WebView) together with Flutter content by temporarily merging the Raster thread into the Platform thread. This ensures correct z-ordering but adds thread synchronization overhead.
Hybrid Composition++ (HCPP)
A newer embedding mode introduced in Flutter 3.44 that uses SurfaceControl transactions and SurfaceFlinger to composite Flutter and native views, avoiding the thread merge. It requires Android API 34+ and Impeller Vulkan.
SurfacePool
A cache that holds OverlayLayer objects used by HC to maintain front/back buffer surfaces for native views. After a view exits, these overlays can remain stale and cause issues on subsequent resize.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗