Skip to content

[web] SkwasmRenderer.renderScene drops FrameTimingRecorder on queue #193696

Description

@kevmoo

Summary

In multi-threaded skwasm and wimp, when DedicatedWorker rasterization (_surface_renderPicturesOnWorker) spans more than one UI frame, SkwasmRenderer.renderScene (engine/src/flutter/lib/web_ui/lib/src/engine/renderer.dart:423-430) queues the incoming scene in renderQueue.next before running Frame.raster. This causes two defects:

  1. Silent FrameTimingRecorder Drop When renderQueue.next Is Overwritten:
    When a new scene arrives while renderQueue.current != null and renderQueue.next != null, SkwasmRenderer.renderScene (renderer.dart:426) calls renderQueue.next?.completer.complete() and overwrites renderQueue.next without recording raster timestamps or reporting timings for the replaced frame's FrameTimingRecorder.

  2. Premature SkwasmPicture Disposal Before Deferred Frame.raster Execution:
    When renderQueue.current == null, _kickRenderLoop runs ViewRasterizer.draw -> compositorFrame.raster(layerTree, currentFrameSize, recorder) (engine/src/flutter/lib/web_ui/lib/src/engine/layer/layer_tree.dart:83-94) synchronously before await viewEmbedder.submitFrame(recorder).
    However, when renderQueue.current != null, renderQueue.next holds the raw LayerScene without running Frame.raster (preroll -> measure -> paint). If a subsequent UI frame runs flushPaint() / compositeFrame() on the main thread before _kickRenderLoop pops renderQueue.next:

    • PictureLayer.picture = newPicture (packages/flutter/lib/src/rendering/layer.dart:844-849) calls _picture?.dispose() on the previous frame's SkwasmPicture handles.
    • Layer.engineLayer = ... (packages/flutter/lib/src/rendering/layer.dart:449-454) calls _engineLayer?.dispose() (ContainerLayer.dispose() in engine/src/flutter/lib/web_ui/lib/src/engine/layer/layer.dart:45-54), disposing direct PictureLayer children of the queued scene's OffsetEngineLayers.
    • When _kickRenderLoop subsequently pops renderQueue.next, PrerollVisitor.visitPicture (engine/src/flutter/lib/web_ui/lib/src/engine/layer/layer_visitor.dart:177-184) sees picture.picture.isDisposed == true and culls the disposed pictures (picture.paintBounds = ui.Rect.zero; picture.isCulled = true). If all pictures are culled, rootLayer.needsPainting is false, PlatformViewEmbedder._updateDomForNewComposition (embedder.dart:482-498) releases the active <canvas> overlay, and submitFrame dispatches surfaceRenderPictures(handle, pictureHandles, 0) (picture_count == 0).

Steps to Reproduce

  1. Run a multi-threaded skwasm or wimp scene where worker rasterization (_surface_renderPicturesOnWorker) takes longer than one UI frame interval (e.g., >16.6 ms during initial shader compilation or heavy scene rendering) while the main UI thread continues producing frames every requestAnimationFrame.
  2. Inspect SchedulerBinding.instance.addTimingsCallback deliveries and PrerollVisitor.visitPicture (picture.picture.isDisposed) when renderQueue.next is popped by _kickRenderLoop.

Observed Behavior

// engine/src/flutter/lib/web_ui/lib/src/engine/renderer.dart:423-430
if (renderQueue.current != null) {
  // If a scene is already queued up, drop it and queue this one up instead
  // so that the scene view always displays the most recently requested scene.
  renderQueue.next?.completer.complete();
  final completer = Completer<void>();
  renderQueue.next = (scene: scene, completer: completer, recorder: recorder);
  return completer.future;
}
  • Any FrameTimingRecorder attached to an overwritten renderQueue.next entry is discarded without reporting timings.
  • Any SkwasmPicture in renderQueue.next.scene that is replaced by a subsequent UI frame's PictureLayer.picture= or Layer.engineLayer= is already disposed (isDisposed == true) by the time _kickRenderLoop executes PrerollVisitor.visitPicture.

Expected Behavior

SkwasmRenderer.renderScene should either retain ScenePicture references while a LayerScene is queued in renderQueue.next (or execute the main-thread preroll / measure / paint phase before queuing worker rasterization) and properly close out or report FrameTimingRecorder entries when renderQueue.next is superseded.

Environment / Target

  • Target Commit / Version: Flutter 3.49.0-1.0.pre-171 (engine 1a3d11d5ab1)
  • Runtime / Platform: WebAssembly (skwasm and wimp multi-threaded under COOP/COEP)

Activity

  1. added
    engine
    platform-web
    e: wasm
    e: web_skwasm
    team-web
    on Oct 2, 2026
  2. harryterkelsen commented on Oct 29, 2026

    @harryterkelsen
    Contributor

    Thanks for the detailed report. I reproduced this on current main with an engine unit test that holds one render in flight and queues a second scene behind it. It reproduces on CanvasKit, Skwasm (both COI and single-threaded) and wimp, because the queue lives in the shared Renderer.renderScene.

    Findings:

    • Blank queued frames are real, but the trigger is different from the one described. addPicture stores a clone of the picture, so the framework's PictureLayer.picture = disposal can't reach the engine. And within a normal frame, any engineLayer= disposal is followed by a render() that replaces renderQueue.next. The actual problem is that a queued scene is not prerolled until it is dequeued, while web's Scene.dispose() / EngineLayer.dispose() destroy the picture clones directly. So:
      • scene.dispose() right after render() blanks a queued scene that has pictures directly under the root.
      • Disposing an engine layer with no newer render() following (e.g. OffsetLayer.toImage between frames) blanks the queued scene too.
    • Frame timings: a queued frame's buildFinish is stamped when it is dequeued, so the time it waits behind the in-flight raster shows up as build time (~100 ms in the test). Skwasm used to stamp it in renderScene; that regressed when the renderers were unified ([reland][web] Refactor renderers to use the same frontend code #174588 #175392). Separately, a frame that overwrites renderQueue.next drops the previous frame's timing entirely.

    The planned fix is to have the render queue retain the queued scene's pictures until it is rasterized or dropped, and to record build finish at render() time.

  3. reopened this on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

e: wasme: web_skwasmengineplatform-webteam-web

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions