Skip to content

Defer TLHC PlatformViewWrapper frame post and avoid initial 1x1 buffer resize - #195861

Merged
auto-submit[bot] merged 4 commits into
flutter:masterfrom
gmackall:tlhc-pr1-defer-frame-and-offset-relayout
Nov 5, 2026
Merged

auto-submit[bot] merged 4 commits into
flutter:masterfrom
gmackall:tlhc-pr1-defer-frame-and-offset-relayout

Conversation

@gmackall

@gmackall gmackall commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

With this change, scrolling a TLHC platform view (e.g. a WebView) goes from ~70 fps to 120 fps on my Pixel 9.

Previously in TLHC, PlatformViewWrapper.draw() locked a hardware canvas from the PlatformViewRenderTarget surface, drew the child Android View hierarchy into it, and called surface.unlockCanvasAndPost(canvas) synchronously inside ViewRootImpl.performTraversals(). Because Surface.unlockCanvasAndPost() calls nSyncAndDrawFrame() on Android's shared RenderThread, calling it inline during performTraversals() queued the platform view's RenderThread frame submission ahead of the Activity window's own ThreadedRenderer frame sync, stalling the UI thread during traversal.

Additionally, when creating a TLHC platform view, PlatformViewsController.configureForTextureLayerComposition() previously constructed PlatformViewWrapper (which immediately locks and clears the render target's surface at its default 1x1 size) before resizing the render target to the platform view's actual dimensions, causing an unnecessary 1x1 ImageReader allocation that was immediately discarded and recreated.

This PR fixes both issues by:

  1. Recording the child view hierarchy synchronously into pendingFrameCanvas during PlatformViewWrapper.draw(), and deferring unlockCanvasAndPost() + renderTarget.scheduleFrame() via mainHandler.postAtFrontOfQueue(...) so it runs immediately after the current traversal's window sync finishes rather than stalling inline during traversal.
  2. Flushing any pending frame synchronously if draw() is re-entered in the same traversal, before resizeRenderTarget(), when window visibility leaves VISIBLE, and before release() (without scheduling a redundant engine frame on release).
  3. Resizing the PlatformViewRenderTarget to (physicalWidth, physicalHeight) before constructing PlatformViewWrapper in configureForTextureLayerComposition(), and removing the redundant viewWrapper.resizeRenderTarget() call afterward.

…r resize

In Texture Layer Hybrid Composition (TLHC), `PlatformViewWrapper.draw(Canvas)`
locks a hardware canvas from the offscreen `PlatformViewRenderTarget` surface,
draws the embedded Android `View` hierarchy into its display list (`super.draw`),
and previously called `surface.unlockCanvasAndPost(canvas)` synchronously inside
`ViewRootImpl.performTraversals()`.

On Android's `HardwareRenderer`, `Surface.unlockCanvasAndPost()` invokes
`nSyncAndDrawFrame()` on the shared singleton `RenderThread` to rasterize and
queue the `PlatformView`'s buffer. In observed traces on 120 Hz displays with
complex `PlatformView`s such as `WebView`, calling `unlockCanvasAndPost()`
inline during `performTraversals()` queues the platform view's `RenderThread`
submission ahead of the Activity window's own `ThreadedRenderer.draw()` ->
`nSyncAndDrawFrame()`, stalling the Activity's UI thread during traversal.

Additionally, `PlatformViewsController.configureForTextureLayerComposition()`
previously constructed `new PlatformViewWrapper(context, renderTarget)` before
calling `renderTarget.resize(physicalWidth, physicalHeight)`. When
`PlatformViewWrapper`'s constructor cleared the initial surface, `getSurface()`
lazily allocated a fallback `1x1` `ImageReader` and locked/posted a `1x1`
hardware canvas, only for `resizeRenderTarget()` to immediately close and
recreate the `ImageReader` at the real physical size.

This commit:
1. Records the child view hierarchy synchronously into `pendingFrameCanvas`
   during `PlatformViewWrapper.draw()`, and defers
   `pendingFrameSurface.unlockCanvasAndPost` + `renderTarget.scheduleFrame()`
   via `mainHandler.postAtFrontOfQueue(...)` so submission runs immediately
   after the current traversal's window sync completes rather than blocking
   inline during traversal.
2. Flushes any pending recorded frame synchronously before re-entering `draw()`,
   before `resizeRenderTarget()`, when window visibility leaves `VISIBLE`
   (`onWindowVisibilityChanged`), and before `release()` (skipping
   `renderTarget.scheduleFrame()` when releasing).
3. Calls `renderTarget.resize(physicalWidth, physicalHeight)` before constructing
   `PlatformViewWrapper` in `configureForTextureLayerComposition()`, avoiding
   the throwaway `1x1` buffer allocation and removing the redundant
   `viewWrapper.resizeRenderTarget()` call.
@github-actions github-actions Bot added platform-android Android applications specifically engine flutter/engine related. See also e: labels. team-android Owned by Android platform team labels Nov 3, 2026
@gmackall gmackall added the CICD Run CI/CD label Nov 3, 2026
@gmackall
gmackall marked this pull request as ready for review November 3, 2026 03:22
@gmackall
gmackall requested a review from a team as a code owner November 3, 2026 03:22
@gmackall
gmackall requested review from okorohelijah and removed request for a team November 3, 2026 03:22

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modifies PlatformViewWrapper to defer posting recorded frames to the render target by scheduling the unlock and post operations on the main thread queue instead of executing them synchronously during the draw traversal. It introduces tracking for pending frames and flushes them during resize, release, and window visibility changes. Additionally, PlatformViewsController is updated to resize the render target prior to wrapper initialization, and comprehensive unit tests are added to verify these behaviors. I have no feedback to provide.

@gmackall
gmackall requested a review from mboetger November 3, 2026 21:54

@mboetger mboetger left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Before and after sequence diagrams might help more than the descriptions of the code in the PR description - just a thought.

@gmackall

gmackall commented Nov 5, 2026 •

Copy link
Copy Markdown
Member Author

LGTM. Before and after sequence diagrams might help more than the descriptions of the code in the PR description - just a thought.

Yeah I could have explained this better. Android splits rendering into two phases (docs):

  • Record View#draw on the UI thread, which runs draw(Canvas) on every invalidated view, and can invoke calls into custom views or into your code.
  • DrawFrame on the RenderThread, which runs on the native RenderThread but operates based on work generated by the Record View#draw phase.

Both the app window and our offscreen platform view Surface share that single RenderThread (HardwareRenderer docs):

All HardwareRenderer instances share a common render thread.

Previously, we called surface.unlockCanvasAndPost() inside PlatformViewWrapper.draw(), which immediately starts GPU rendering for the platform view on RenderThread (syncAndDraw() docs) from within the lightweight draw. When the app window finished its View#draw walk microseconds later and tried to queue its frame to RenderThread (and wait for the return, because it uses postAndWait()), RenderThread was already busy drawing the platform view—forcing the platform/UI thread to sit blocked (~3.5 ms on a Pixel 9) waiting for the GPU (because we are waiting on a task to finish which has a heavy second task in front of it).

With this change, draw() only records the platform view, and we defer unlockCanvasAndPost() (postAtFrontOfQueue) until right after the window's View#draw pass finishes. The window handoff now completes immediately while RenderThread is idle (~1.4 ms vs ~5.1 ms on a Pixel 9), and the platform view renders on RenderThread in parallel with the platform/UI thread.

Gemini made this mermaid diagram which I can't get to display right inside of github, so here's a picture, but it accurately captures my understanding:

Screenshot 2026-11-05 at 11 25 59 AM

@gmackall gmackall added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 5, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Nov 5, 2026
@gmackall

gmackall commented Nov 5, 2026

Copy link
Copy Markdown
Member Author

Here's a picture
image

Merged via the queue into flutter:master with commit 9d41d2b Nov 5, 2026
23 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 5, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Nov 6, 2026
Roll Flutter from 3623d2d34543 to 09175cdeac89 (61 revisions)

flutter/flutter@3623d2d...09175cd

2026-11-06 [email protected] Roll Skia from ae35b7b2221f to 793b8ac73d4b (3 revisions) (flutter/flutter#196091)
2026-11-06 [email protected] [web] Rasterize scenes at the size passed to `FlutterView.render` (flutter/flutter#196052)
2026-11-06 [email protected] Roll Skia from 4aa043a81e0c to ae35b7b2221f (1 revision) (flutter/flutter#196086)
2026-11-06 [email protected] Roll Fuchsia Linux SDK from Fqe3UlLwAohsPbsCS... to KIc0XZbhLmvDpTluZ... (flutter/flutter#196085)
2026-11-06 [email protected] Allow a zero scale change in InteractiveViewer (flutter/flutter#194949)
2026-11-06 [email protected] Roll Skia from dd5c2553a856 to 4aa043a81e0c (2 revisions) (flutter/flutter#196079)
2026-11-06 [email protected] [web] Keep ImageFilter.blur bounds in equality and description (flutter/flutter#196013)
2026-11-06 [email protected] iOS: Remove unused PlatformViewIOS setup from FlutterPlatformViewsTest (flutter/flutter#196033)
2026-11-06 [email protected] Serialize LoggerTests that mutate the shared Logger (flutter/flutter#196028)
2026-11-06 [email protected] Use reverseDuration for the AnimatedCrossFade size animation (flutter/flutter#194860)
2026-11-05 [email protected] Roll Skia from a5aacd103e68 to dd5c2553a856 (8 revisions) (flutter/flutter#196073)
2026-11-05 [email protected] Fix ListWheelScrollView null check error when it reads itemIndex before content dimensions are set (flutter/flutter#194859)
2026-11-05 [email protected] Allow zero cross axis extent in SliverGridDelegateWithMaxCrossAxisExtent (flutter/flutter#194858)
2026-11-05 [email protected] Revert: Allow color delta tolerance in in-memory fuzzy image comparator (flutter/flutter#196067)
2026-11-05 [email protected] Revert: Mitigate snapshot race in 3D transform test by reducing loop density (flutter/flutter#196066)
2026-11-05 [email protected] Delegate createStream in ResizeImage and ScrollAwareImageProvider (flutter/flutter#194756)
2026-11-05 [email protected] [iOS] Introduce FlutterShell and move thread host and profiler (flutter/flutter#195388)
2026-11-05 [email protected] [web] Support FragmentShader.getImageSampler on CanvasKit and Skwasm (flutter/flutter#195854)
2026-11-05 [email protected] Deprecates setFloat API (flutter/flutter#195505)
2026-11-05 [email protected] [flutter_tools] Remove public GlobalToolContext and globals from runner (flutter/flutter#195561)
2026-11-05 [email protected] Defer TLHC PlatformViewWrapper frame post and avoid initial 1x1 buffer resize (flutter/flutter#195861)
2026-11-05 [email protected] Roll Skia from b5abed66da86 to a5aacd103e68 (17 revisions) (flutter/flutter#196054)
2026-11-05 [email protected] Made `FlutterWindowControllerSizeTest.SizedToContentNotResizable` more robust (flutter/flutter#195845)
2026-11-05 [email protected] Update docs for contributor ladder (flutter/flutter#196011)
2026-11-05 [email protected] [Impeller] Use the transfer stage for the Vulkan readback barrier (flutter/flutter#194991)
2026-11-05 [email protected] Remove Mac mac_unopt builder (flutter/flutter#196027)
2026-11-05 [email protected] Updated Instructions for Determining Gradle Version for `gradle_dists` in CIPD (flutter/flutter#193310)
2026-11-05 [email protected] Move ios_app_with_extensions_test into the tool integration shard (flutter/flutter#195609)
2026-11-05 [email protected] [web] Respect Uint8List view bounds when detecting animated WebP/GIF (flutter/flutter#196010)
2026-11-05 [email protected] Add platform_views_layout to build_tests so iOS/Android build breaks are caught in presubmit (flutter/flutter#192985)
2026-11-05 [email protected] [flutter_tools] Detect macOS TCC permission crashes and provide diagnostic guidance (flutter/flutter#192432)
2026-11-05 [email protected] Roll Packages from 670b30e to 0478162 (15 revisions) (flutter/flutter#196047)
2026-11-05 [email protected] [web] Support Skwasm rendering without bitmap capture (flutter/flutter#195103)
2026-11-05 [email protected] [flutter_tools] Reland fix for local CanvasKit asset paths on Windows (flutter/flutter#195770)
2026-11-05 [email protected] Roll Skia from f96be51af95d to b5abed66da86 (14 revisions) (flutter/flutter#196017)
2026-11-05 [email protected] Split chrome web engine test drone (flutter/flutter#195873)
2026-11-05 [email protected] Roll Dart SDK from c9153304136a to 6623b84628f3 (1 revision) (flutter/flutter#196026)
2026-11-05 [email protected] Roll ICU from 4fe960221710 to a0ea93436c52 (6 revisions) (flutter/flutter#196025)
2026-11-05 [email protected] Add more guidance in README and AGENTS for contributing to flutter_tools (flutter/flutter#195998)
2026-11-05 [email protected] Roll Fuchsia Linux SDK from p0HDXLISPIElJfSz7... to Fqe3UlLwAohsPbsCS... (flutter/flutter#196019)
2026-11-05 [email protected] Roll Dart SDK from e362a23e9738 to c9153304136a (18 revisions) (flutter/flutter#196014)
2026-11-05 [email protected] [Impeller] Fix dilation input bounds when clipping saveLayers (flutter/flutter#195652)
2026-11-05 [email protected] Add `variantOf` to the M3E migration checklist (flutter/flutter#195099)
2026-11-05 [email protected] [flutter_tools] Eliminate globals from remaining subsystem files (flutter/flutter#195555)
2026-11-05 [email protected] Fix gap flipping when dragging variable height reorderable items (flutter/flutter#194478)
2026-11-05 [email protected] [A11y] Send un-merged SemanticsNodes in the same update that first references them (flutter/flutter#195829)
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD engine flutter/engine related. See also e: labels. platform-android Android applications specifically team-android Owned by Android platform team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants