Skip to content

[Impeller] Support multi-surface frames in GPUTracerVK #196834

Open

Description

@andywolff

When Android renders platform views using Hybrid Composition or HCPP, the root Flutter surface and any overlay surfaces share a single GPU context and tracer. The Vulkan GPU tracer assumes only one frame can be active at a time, so acquiring and presenting multiple surfaces within a single Flutter frame corrupts the traced frame boundaries.

Under HCPP, the overlay surface starts and finishes its traced frame before the root surface finishes encoding and submitting its work, which triggers a debug assertion on overlapping frame starts and drops the root surface's GPU work from the trace. Under Hybrid Composition, each overlay surface emits its own GPU frame time event that dilutes the reported average, and frames that acquire an overlay surface without submitting it leave the tracer stuck mid-frame and hit the debug assertion on the next frame.

Specifically, AndroidSurfaceVKImpeller shares a single ContextVK and GPUTracerVK across surfaces, and GPUTracerVK gates frame tracking with a single in_frame_ boolean toggled by MarkFrameStart and MarkFrameEnd. In #196822, GPU frame time metrics were disabled for the affected HCPP and Hybrid Composition benchmarks (createAndroidHCPPScrollPerfTest and createAndroidViewScrollPerfTest in dev/devicelab/lib/tasks/perf_tests.dart). Once GPUTracerVK supports multi-surface frames, re-enabling those metrics is in scope for this issue.

Related:

Activity

  1. added
    engine
    e: impeller
    team-engine
    on Nov 18, 2026
  2. andywolff commented on Nov 18, 2026

    @andywolff
    ContributorAuthor

    While planning the fix for this issue, I uncovered two additional problems beyond overlapping frame starts and premature frame ends.

    First, frames that acquire a surface without presenting it (such as the first platform-view frame or retried frames) never close the tracer's frame scope, leaving it stuck mid-frame. Specifically, AcquireNextDrawable calls GPUTracerVK::MarkFrameStart(), and when SurfaceFrame::Submit() is skipped (for example on RasterStatus::kSkipAndRetry or previous_frame_view_count_ == 0), Present() never runs to call MarkFrameEnd(), leaving in_frame_ set to true.

    Second, overlay swapchains hold onto their last onscreen command buffer in a ring buffer until that slot is reused two presents later on the same swapchain. When an overlay goes idle while the root surface keeps rendering, those retained probes keep pending buffer counts non-zero on the shared trace state, dropping metrics and hitting the pending-buffer assertion once the trace state ring wraps around. Specifically, AHBSwapchainImplVK and KHRSwapchainImplVK keep sync->final_cmd_buffer (and its GPUProbe) alive until the swapchain reuses that frame slot, rather than releasing it on GPU fence completion via FenceWaiterVK.

    To keep reviews small, I plan to split this into three focused engine PRs before re-enabling the benchmarks:

    1. Release final_cmd_buffer probes via FenceWaiterVK on GPU completion so idle swapchains do not retain pending_buffers, and update GPUTracerVK::RecordCmdBufferEnd to index *probe.index_ instead of current_state_.
    2. Replace bool in_frame_ with a reference-counted active_frames_ counter, an RAII frame-scope token owned by SurfaceVK / the swap callback, and explicit GPUTraceState::frame_ended coordination, fixing HCPP and unsubmitted/retried frames.
    3. Wire frame_boundary = false through GPUSurfaceVulkanImpeller and AndroidExternalViewEmbedder::SubmitFlutterView followed by GPUTracerVK::MarkFrameBoundary(), combining Hybrid Composition root and overlay surfaces into one traced frame.

    There is also a metrics semantics decision to make for Hybrid Composition: whether the merged GPU frame time should measure the wall-clock span across the whole frame or only the time the GPU is actively busy. Because Hybrid Composition submits the root surface, performs CPU, JNI, and fence-wait work, and then submits overlay surfaces, a single max - min span includes that idle gap between submits, while summing per-surface work excludes it. To preserve both views, I propose keeping FrameTimeMS as the wall-clock max - min span (consistent with GpuSumarizer and existing single-surface expectations) while also recording per-command-buffer (start_index, end_index) intervals and emitting a supplemental ActiveTimeMS counter value on the same GPUTracer event.

  3. added
    P2
    triaged-engine
    on Nov 23, 2026
  4. andywolff commented on Nov 24, 2026

    @andywolff
    ContributorAuthor

    I was talking to @gaaclarke about this, and we agreed that the complexity required to support everything I mentioned in my previous comment is likely not worth it. For now, we'll add frame time measurement to HCPP test cases only.

    To summarize the reasoning:

    In a platform-view frame, there are multiple Flutter surfaces sharing one Vulkan context and frame time tracker: the root surface (Flutter UI below the platform view) and one or more overlay surfaces (Flutter UI on top of the platform view; a single shared overlay in HCPP).

    What do we actually want to measure? Frame time, which in the case of multiple surfaces is fine to measure as the union of the surfaces (latest end minus earliest start across the frame). That is the closest thing to a whole frame. It loses some detail when there are idle gaps between submits, which is why I previously suggested adding a secondary "active time" metric, though it is not clear we need that extra complexity.

    Do we actually need to measure both surfaces independently? Not necessarily, though the answer depends on HCPP versus Hybrid Composition because their lifecycles differ. In HCPP, the overlay surface lifecycle is nested inside the root surface, so we do not need to track the overlay as its own frame: ignoring nested frame start and end markers still captures the overlay's command buffers inside the root frame and gives us the union automatically. In Hybrid Composition, the surfaces run sequentially (root surface, then JNI glue work, then overlay surfaces). Skipping the overlays there would only measure the root surface, missing whatever Flutter UI is drawn on top of the platform view. On the other hand, combining them into a union for Hybrid Composition requires plumbing a frame-boundary signal across multiple layers and still includes that JNI gap unless we also build out active-time interval tracking.

    Looking at the options in order of complexity:

    1. Status quo: Leave GPU frame time measurement disabled on multi-surface platform-view benchmarks.
    2. HCPP only (chosen): Ignore nested overlay frame markers so HCPP measures the full-frame union, and leave Hybrid Composition disabled. This still needs a small amount of lifecycle cleanup so idle overlays and unsubmitted frames do not break the tracer.
    3. HCPP and Hybrid Composition (union only): Plumb a frame-boundary flag across the surface and view-embedder layers so Hybrid Composition closes its frame after the last overlay submits, accepting that the union includes JNI glue and idle time.
    4. Full multi-surface support: All of the above plus per-command-buffer interval tracking and merging so we can report both union frame time and active GPU time.

    Even though HCPP is not the default yet, it is the path forward on modern Android, and the jump in maintenance complexity from option 2 to options 3 and 4 is large. Proceeding with option 2 gives us a clean, maintainable path in three smaller PRs:

    1. PR 1 ([Impeller] Release swapchain GPUProbe on fence completion #197024): Release onscreen timestamp probes on GPU fence completion rather than waiting for swapchain slot reuse (so idle overlay swapchains do not stall the tracer ring buffer), and ensure command buffer end timestamps always record into their own frame's state.
    2. PR 2: Ignore nested frame start and end markers so only the outermost frame owns frame boundaries, clean up frame scopes automatically when a surface is acquired without being presented, and coordinate explicit frame-end state with background GPU fence completion.
    3. PR 3: Re-enable Vulkan GPU tracing and GPU frame time metrics for the HCPP scroll benchmark, leaving the legacy Hybrid Composition benchmark disabled for now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2e: impellerengineteam-enginetriaged-engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions