Repository navigation
[web] Record end-to-end presentation duration in benchmarks - #188628
auto-submit[bot] merged 5 commits into
Conversation
3c7da4b to
1f6c738
Compare
|
@MarlonJD I'm eager to use FLUTTER_WEB_SKWASM_FORCE_MULTI_SURFACE_RASTERIZER, will this PR be re-opened? |
|
An existing Git SHA, To re-trigger presubmits after closing or re-opeing a PR, or pushing a HEAD commit (i.e. with |
|
@MichealReed Yes, I've reopened it as a draft and updated the branch against master. The benchmarks show lower raster times on desktop Safari, a physical iPhone, and Firefox. Chrome improved in two workloads, with mixed card-scrolling results. I've added the results to the PR description. |
|
@MichealReed I tested the attached repro on macOS Chrome 152 against this branch. In both single- and multi-threaded mode, the default path gave the primary canvas the popup's dimensions in all 120 samples; with |
|
@MarlonJD this is still marked as a draft. What do you think is remaining for the flutter team to review? |
There was a problem hiding this comment.
Code Review
This pull request introduces support for forcing a multi-surface rasterizer in Skwasm when the browser supports transferring canvas control to an offscreen canvas, or when running in single-threaded mode. It updates SkwasmSurface to implement OnscreenSurface, handles CSS-only resizing for transferred canvases, and adds comprehensive tests. Feedback on the changes suggests resolving a potential DOM leak by clearing _hostElement before appending a new canvas, improving type safety during canvas resizing, removing an unnecessary async keyword from SkwasmRenderer.initialize, and explicitly using pictures_pointer.get() in the C++ surface worker to avoid compiler warnings.
| void _maybeAttachCanvasToDom() { | ||
| if (_isOnscreen) { | ||
| _hostElement.appendChild(_canvas as DomHTMLCanvasElement); | ||
| _applyOverlayPositioning(); | ||
| } | ||
| } |
There was a problem hiding this comment.
When recreating the WebGL context (e.g., after a context loss event), recreateContextForCanvas is called, which updates _canvas and calls _maybeAttachCanvasToDom(). However, the old canvas element is never removed from _hostElement before the new one is appended. This will cause multiple canvas elements to accumulate inside _hostElement, leading to DOM leaks and potential rendering/clipping bugs.\n\nPlease clear any existing children from _hostElement before appending the new canvas.
void _maybeAttachCanvasToDom() {\n if (_isOnscreen) {\n while (_hostElement.firstChild != null) {\n _hostElement.removeChild(_hostElement.firstChild!);\n }\n _hostElement.appendChild(_canvas as DomHTMLCanvasElement);\n _applyOverlayPositioning();\n }\n }| } else { | ||
| _canvasProvider.resizeCanvas(_canvas, size); | ||
| } |
There was a problem hiding this comment.
For consistency and type safety, we should cast _canvasProvider to OnscreenCanvasProvider and pass the already-cast canvas variable (which is statically typed as DomHTMLCanvasElement) instead of _canvas (which is typed as DomEventTarget). This avoids relying on implicit downcasting or generic covariance at runtime.
} else {\n (_canvasProvider as OnscreenCanvasProvider).resizeCanvas(canvas, size);\n }References
- Optimize for readability: Code is read more often than it is written. (link)
| FutureOr<void> initialize() async { | ||
| rasterizer = _createRasterizer(isMultiThreaded: isMultiThreaded); | ||
| return super.initialize(); | ||
| } |
There was a problem hiding this comment.
The initialize method does not use await anywhere in its body. Marking it as async unnecessarily wraps the returned FutureOr<void> in a Future, which can introduce microtask delays and overhead on a performance-critical initialization path.
@override\n FutureOr<void> initialize() {\n rasterizer = _createRasterizer(isMultiThreaded: isMultiThreaded);\n return super.initialize();\n }References
- Suggest simplification and refactoring: Assess whether the code can be made simpler or refactored to enhance readability and maintainability. (link)
| std::unique_ptr<sk_sp<flutter::DisplayList>[]> pictures_pointer = | ||
| std::unique_ptr<sk_sp<flutter::DisplayList>[]>(pictures); | ||
| surface->RenderPicturesOnWorker(pictures, picture_count, callback_id, | ||
| raster_start); | ||
| raster_start, capture_image_bitmaps); |
There was a problem hiding this comment.
To avoid potential compiler warnings about pictures_pointer being an unused variable (since it is only declared to manage lifetime via RAII), we should pass pictures_pointer.get() to RenderPicturesOnWorker instead of the raw pictures parameter. This makes the usage of the smart pointer explicit.
std::unique_ptr<sk_sp<flutter::DisplayList>[]> pictures_pointer =\n std::unique_ptr<sk_sp<flutter::DisplayList>[]>(pictures);\n surface->RenderPicturesOnWorker(pictures_pointer.get(), picture_count, callback_id,\n raster_start, capture_image_bitmaps);|
@kevmoo @harryterkelsen could you guys review when you get the chance please? |
|
A few high-level comments before taking a deep look into this PR:
Thanks for your PR. Let's discuss this some more so we can decide on next steps. |
harryterkelsen
left a comment
There was a problem hiding this comment.
Please see my comment above
|
I tested a single-canvas prototype on an M2 Pro Mac. Multithreaded presentation time in milliseconds, using the median of three run means:
All 10 focused correctness tests passed across four configurations covering Safari, Firefox, and Chrome. The prototype currently covers only single-canvas rendering; transitions to additional canvases or views still need work. Would you prefer this PR to optimize single-canvas presentation while preserving existing rendering for multi-canvas and multi-view cases, or should improving those cases also be in scope? |
|
Those are some very exciting numbers! I think it's definitely worth trying to get What should we do when we go from one Surface to multiple? For example, if a platform view is rendered and we need an overlay canvas. We need to make sure there's no jank or white flashes if we go from a But, if the transition from one Surface to multiple is seamless (and the transition from multiple Surfaces back to only one), then I think this seems like a slam dunk. |
|
Thanks @harryterkelsen Agreed, the results suggest that transferControlToOffscreen is where most of the performance gain comes from. I’ll explore a hybrid approach that uses the transferred canvas for single-canvas rendering and falls back to the existing bitmaprenderer path when multiple surfaces are needed. The main challenge is keeping the previous frame visible until the new rendering path is ready. I’ll focus on making that transition seamless in both directions and testing it across Safari, Firefox, and Chrome. I’ll share the results before making larger changes to the PR. |
|
Thanks @harryterkelsen. The revised implementation uses Both transition directions retain the visible frame until the next presentation is ready; bitmap → direct also requires an independent pixel comparison. Local validation passes 318 tests across Chrome/Safari/Firefox and Wimp ST/MT, with two known Wimp shader skips (#175431). Compositor recordings at 75% CSS display scale pass 60 Hz scene-marker checks over 288 directional transitions and 48 resized exits. A Wasm Workers semaphore fix addresses the reproduced FreeType corruption and native stall, with a regression test. The original Safari hang was unsymbolized, so its attribution remains probable. The paired benchmarks below show lower median presentation time in all tested cases. Defaults remain hybrid for desktop WebKit/Firefox and Chromium ST; Chromium MT and mobile remain opt-in. The measured single-view case uses two presentation contexts instead of one. Total GPU memory and context-limit stress remain unmeasured. HTML-in-Canvas remains a separate proposal; this change retains DOM platform views. Mobile checks have not been repeated with the native fix, and fresh upstream CI is pending. Full desktop benchmarks — 72 runs across 12 casesApple M2 Pro Mac mini, macOS 27, DPR 2. Safari 27.0, Chrome 155.0.8059.40, Firefox 148.0. Both paths use the same source-built release engine with the semaphore fix. Each run has 200 warm-up samples and 100 measured samples: 7,200 measured samples overall. All values are presentation time in milliseconds; lower is better. Each run column lists three run means. The comparison uses their median, and percentage change is Safari
Chrome
Firefox
Cards: These are local presentation-time measurements, not FPS or physical display latency, and they do not isolate the semaphore fix’s performance effect. Three repeats show substantial variation in some Chrome cases; the medians are descriptive, not a statistical significance claim. Chromium MT stays opt-in because earlier results were mixed; these desktop numbers do not qualify mobile performance. |
|
autosubmit label was removed for flutter/flutter/188628, because This PR has not met approval requirements for merging. The PR author is not a member of flutter-hackers and needs 1 more review(s) in order to merge this PR.
|
|
autosubmit label was removed for flutter/flutter/188628, because This PR has not met approval requirements for merging. The PR author is not a member of flutter-hackers and needs 1 more review(s) in order to merge this PR.
|
|
CI had a failure that stopped further tests from running. We need to investigate to determine the root cause. SHA at time of execution: 45d8126. Possible causes:
A blank commit, or merging to head, will be required to resume running CI for this PR. Error Details: Stack trace: |
|
autosubmit label was removed for flutter/flutter/188628, because - The status or check suite Google testing has failed. Please fix the issues identified (or deflake) before re-applying this label. |
|
Google testing failed on the latest commit, while Dashboard Checks and flutter-gold passed. Could one of you check the internal failure and let me know whether this needs a change on my side or a rerun? Thanks! |
|
Sorry, this fell off my radar. The Google Testing failures look like flakes. I'll try re-running now |
Related: flutter#187660. Part 2 of 5, split from flutter#188628. This change can land independently of the hybrid rasterizer. Makes Skwasm callback IDs unique across surfaces and adds an explicit bitmap-capture parameter to the native render call. Existing Dart rendering still requests bitmaps; the new option allows a later onscreen path to complete without clearing its backing canvas through bitmap export. Verified locally: Skwasm and Wimp native release builds; overlapping-surface, context-loss and capture/no-capture tests in ST and MT. The matching engine prebuilts must be available before dependent Dart-only changes land. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant in-code documentation (doc comments with `///`). - [ ] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: zhongliugo <[email protected]>
Related: #187660. Part 1 of 5 from the original hybrid-rasterizer change. This PR is now limited to the independent benchmark instrumentation.
Adds
presentation_durationaround preparation and frame submission, including asynchronous raster work, and records it in the web benchmark harness. The timer stops before reporting, and benchmark listeners are cleaned up even when startup fails. The existing profile JSON schema is unchanged.Verified locally: two engine timing tests cover enabled/disabled profiling and delayed preparation/rasterization; a browser regression test covers listener cleanup after synchronous startup failures. Static analysis and formatting pass. This change is independent of the hybrid rasterizer.
The remaining pieces are separate drafts:
The duplicate semaphore and codec changes were dropped on rebase because master already includes #194139 and #193187.
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.