Repository navigation
Description
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:
-
Silent
FrameTimingRecorderDrop WhenrenderQueue.nextIs Overwritten:
When a new scene arrives whilerenderQueue.current != nullandrenderQueue.next != null,SkwasmRenderer.renderScene(renderer.dart:426) callsrenderQueue.next?.completer.complete()and overwritesrenderQueue.nextwithout recording raster timestamps or reporting timings for the replaced frame'sFrameTimingRecorder. -
Premature
SkwasmPictureDisposal Before DeferredFrame.rasterExecution:
WhenrenderQueue.current == null,_kickRenderLooprunsViewRasterizer.draw->compositorFrame.raster(layerTree, currentFrameSize, recorder)(engine/src/flutter/lib/web_ui/lib/src/engine/layer/layer_tree.dart:83-94) synchronously beforeawait viewEmbedder.submitFrame(recorder).
However, whenrenderQueue.current != null,renderQueue.nextholds the rawLayerScenewithout runningFrame.raster(preroll->measure->paint). If a subsequent UI frame runsflushPaint()/compositeFrame()on the main thread before_kickRenderLooppopsrenderQueue.next:PictureLayer.picture = newPicture(packages/flutter/lib/src/rendering/layer.dart:844-849) calls_picture?.dispose()on the previous frame'sSkwasmPicturehandles.Layer.engineLayer = ...(packages/flutter/lib/src/rendering/layer.dart:449-454) calls_engineLayer?.dispose()(ContainerLayer.dispose()inengine/src/flutter/lib/web_ui/lib/src/engine/layer/layer.dart:45-54), disposing directPictureLayerchildren of the queued scene'sOffsetEngineLayers.- When
_kickRenderLoopsubsequently popsrenderQueue.next,PrerollVisitor.visitPicture(engine/src/flutter/lib/web_ui/lib/src/engine/layer/layer_visitor.dart:177-184) seespicture.picture.isDisposed == trueand culls the disposed pictures (picture.paintBounds = ui.Rect.zero; picture.isCulled = true). If all pictures are culled,rootLayer.needsPaintingisfalse,PlatformViewEmbedder._updateDomForNewComposition(embedder.dart:482-498) releases the active<canvas>overlay, andsubmitFramedispatchessurfaceRenderPictures(handle, pictureHandles, 0)(picture_count == 0).
Steps to Reproduce
- Run a multi-threaded
skwasmorwimpscene 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 everyrequestAnimationFrame. - Inspect
SchedulerBinding.instance.addTimingsCallbackdeliveries andPrerollVisitor.visitPicture(picture.picture.isDisposed) whenrenderQueue.nextis 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
FrameTimingRecorderattached to an overwrittenrenderQueue.nextentry is discarded without reporting timings. - Any
SkwasmPictureinrenderQueue.next.scenethat is replaced by a subsequent UI frame'sPictureLayer.picture=orLayer.engineLayer=is already disposed (isDisposed == true) by the time_kickRenderLoopexecutesPrerollVisitor.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(engine1a3d11d5ab1) - Runtime / Platform: WebAssembly (
skwasmandwimpmulti-threaded under COOP/COEP)
Activity
harryterkelsen commentedon Oct 29, 2026
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:
addPicturestores a clone of the picture, so the framework'sPictureLayer.picture =disposal can't reach the engine. And within a normal frame, anyengineLayer=disposal is followed by arender()that replacesrenderQueue.next. The actual problem is that a queued scene is not prerolled until it is dequeued, while web'sScene.dispose()/EngineLayer.dispose()destroy the picture clones directly. So:scene.dispose()right afterrender()blanks a queued scene that has pictures directly under the root.render()following (e.g.OffsetLayer.toImagebetween frames) blanks the queued scene too.buildFinishis 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 inrenderScene; that regressed when the renderers were unified ([reland][web] Refactor renderers to use the same frontend code #174588 #175392). Separately, a frame that overwritesrenderQueue.nextdrops 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.[web] Record build finish synchronously in renderScene and fix droppe…
[web] Retain queued scene pictures via ContainerLayer render pinning (f…