Skip to content

[WebParagraph] Fix text pixelation, glyph clipping, and snapping offsets to avoid subpixel raster caching - #191736

Merged
auto-submit[bot] merged 25 commits into
flutter:masterfrom
Rusino:fraction
Sep 12, 2026
Merged

auto-submit[bot] merged 25 commits into
flutter:masterfrom
Rusino:fraction

Conversation

@Rusino

@Rusino Rusino commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary of Changes

This PR improves WebParagraph text rendering quality and caching on CanvasKit:

  1. Fixes text blurriness / fractional distortion: Snaps physical screen offsets to whole integer device pixels and aligns the Canvas2D context translation to integer coordinates. This guarantees a 1:1 physical device pixel blit with FilterQuality.none.
  2. Eliminates glyph clipping: Adds 2 physical pixels of safety antialiasing padding so glyph descenders/ascenders (e.g. g, j, y, _) are no longer clipped at the paint boundary.
  3. Preserves fractional font sizes: Removes .floor() when generating CSS font strings (cssFontSize.toStringAsFixed(2)), allowing fractional font sizes to render accurately.
  4. Simplifies caching (100% cache hit during scrolling): By snapping physical offsets to integer coordinates, text rendered on Canvas2D is always at phase (0.0, 0.0). As a result, the cached bitmap is valid across all translations and scrolling movements. All complex motion heuristics, frame sequence tracking, settle redraws, and 4-bin quantization state machines have been removed in favor of pure scale-based cache invalidation.

Fixes #188323

Rusino added 4 commits August 24, 2026 11:05
to make sure we have fractional number for anti aliasing correctly.
Also, make target rect physical size equal to source rect, not logical.
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Aug 25, 2026
@Rusino Rusino self-assigned this Aug 25, 2026
@Rusino
Rusino requested a review from mdebbar August 25, 2026 15:50
@github-actions github-actions Bot added engine flutter/engine related. See also e: labels. platform-web Web applications specifically team-web Owned by Web platform team labels Aug 25, 2026

@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 introduces subpixel alignment for web paragraphs to render 1:1 in physical pixels and avoid pixelation, updating the caching mechanism to include scale and canvas translation shifts. Feedback highlights a mathematical bug in the physical offset and target rect calculations when handling non-unity scales and translations. Additionally, a performance regression was identified where the paragraph cache is invalidated on every frame during translation animations due to changes in the canvas shift.

Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/painter.dart Outdated
Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/web_paragraph_painter.dart Outdated
@flutter-dashboard

Copy link
Copy Markdown

Golden file changes have been found for this pull request. Click here to view and triage (e.g. because this is an intentional change).

If you are still iterating on this change and are not ready to resolve the images on the Flutter Gold dashboard, consider marking this PR as a draft pull request above. You will still be able to view image results on the dashboard, commenting will be silenced, and the check will not try to resolve itself until marked ready for review.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

Changes reported for pull request #191736 at sha 1dcb1b0

@flutter-dashboard flutter-dashboard Bot added the will affect goldens Changes to golden files label Aug 25, 2026
Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/web_paragraph_painter.dart Outdated
Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/web_paragraph_painter.dart Outdated
Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/web_paragraph_painter.dart Outdated
ui.Rect sourceRect,
ui.Rect targetRect, {
required ParagraphImageGenerator generateParagraphImage,
required ui.Offset canvas2dShift,

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.

This is not used.

Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/painter.dart Outdated
Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/painter.dart Outdated
@mdebbar

mdebbar commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

/gemini review

@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 updates the web paragraph painting logic to support subpixel alignment and scale transforms, introducing a paragraph cache key and updating calculation logic for the source and target rectangles. The review feedback points out a copy-paste typo in the vertical target rectangle calculation and suggests including the subpixel shift (canvas2dShift) in the cache key to prevent stale cache reuse and text jittering.

Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/painter.dart Outdated
Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/web_paragraph_painter.dart Outdated
Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/web_paragraph_painter.dart Outdated
* **Unified Cache Entry ()**:
  * Consolidated the paragraph cache image and its scale metrics into a single  class, eliminating separate image and cache key fields in .
  * Replaced individual , , and  properties with  and .
  * Added an epsilon tolerance () to  to avoid unnecessary cache invalidation due to minor floating-point precision fluctuations.

* **Cleaned Up  Signature**:
  * Removed the unused  parameter from  and .

* **Upfront Transform & Scale Computation**:
  * Extracted  and  calculations directly within  and passed them down to  and .
  * Streamlined 's return signature to return .
  * Renamed / to / across canvas sizing and layout calculations.

* **Fixed Vertical Target Rect Translation**:
  * Corrected the  calculation in  to properly divide  by  when canvas transforms are present.

* **Unit Testing & Test Maintenance**:
  * Updated existing subpixel alignment tests to reflect the simplified 3-tuple return signature of .
  * Added test coverage verifying  behavior with combined scaling and translation matrices.
@Rusino

Rusino commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

1 similar comment
@Rusino

Rusino commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 updates the web paragraph painter to account for the canvas transform matrix when calculating paragraph dimensions and scaling, ensuring subpixel phase alignment. It also introduces a _ParagraphCacheKey to cache paragraphs based on device pixel ratio and scale. Feedback highlights a typo in the vertical position calculation of the target rectangle where scaleY was used instead of transformY / scaleY. Additionally, it is recommended to include canvas2dShift in _ParagraphCacheKey to prevent stale bitmap reuse when the subpixel phase changes.

Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/web_paragraph_painter.dart Outdated

@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 improves web paragraph rendering by incorporating canvas matrix scaling and translation into the subpixel alignment and caching logic. It introduces a _ParagraphCacheEntry to cache rasterized paragraph images at their effective scale and updates the paragraph layout calculations to match physical subpixel phases. Feedback on these changes suggests simplifying the mathematical formula for calculating targetRect, refactoring _resizePaintCanvas to accept pre-calculated effective scales directly, and avoiding null assertions on _cacheEntry by using a local variable.

Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/painter.dart Outdated
Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/painter.dart Outdated
Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/web_paragraph_painter.dart Outdated
- Dual-Mode Caching Strategy (CanvasKitPainter):
  - Updated  to include  for subpixel phase tracking.
  - Implemented dynamic motion detection () by tracking frame-to-frame position deltas.
  - Active Scrolling: Uses loose scale-only cache matching () to eliminate  readbacks and guarantee 60/120 FPS jank-free scrolling.
  - Stationary / Settled: Uses strict cache matching () including  so static text is always rasterized with exact subpixel alignment and crispness.

- Target Rect & Canvas Resize Simplification (painter.dart):
  - Simplified  and  calculation in  to , canceling out redundant  terms and avoiding potential division-by-zero precision issues.
  - Updated  to accept pre-calculated  and  directly.

- Code Health & Type Safety:
  - Added unit test  in  verifying static text subpixel invalidation and scroll cache preservation.
@Rusino
Rusino requested a review from mdebbar August 27, 2026 15:53
@flutter-dashboard

Copy link
Copy Markdown

Golden file changes are available for triage from new commit, Click here to view.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

Changes reported for pull request #191736 at sha fa91671

@Rusino Rusino changed the title [WebParagraph] Fixes text pixelation and cutting off glyphs vertically [WebParagraph] Fix text pixelation, glyph clipping, and implement dual-mode subpixel raster caching Sep 1, 2026
@Rusino
Rusino marked this pull request as draft September 1, 2026 17:24
@github-actions github-actions Bot added the d: examples Sample code and demos label Sep 8, 2026
@github-actions github-actions Bot removed the d: examples Sample code and demos label Sep 8, 2026

@mdebbar mdebbar 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.

Let's update the PR title since we aren't doing "dual-mode caching" anymore.

Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/web_paragraph_painter.dart Outdated
Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/painter.dart Outdated
Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/web_paragraph_painter.dart Outdated

if (!hasCache) {
debugRasterizeCount++;
debugTotalRasterizeCount++;

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.

Add a test that uses it to justify its existence.

Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/painter.dart Outdated
Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/painter.dart Outdated
final TextLayout layout = _paragraph.getLayout();
final Float64List canvasTransform = canvas.getTransform();
final double dpr = ui.window.devicePixelRatio;
final transform = _ParagraphTransform.from(canvasTransform, dpr);

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.

There's no need to create a wrapper class. You can use extension methods to attach methods to Float64List. Something like this:

extension MatrixOperations on Float64List {
  (double scaleX, double scaleY) getScale() {
    return ...
  }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

An extension would work if we only needed the scale, but ParagraphTransform also merges in the DPR, handles null transforms, and owns the coordinate snapping (snapOffset, snapRect). It also gives us a nice bundle to pass into the background/block painting methods instead of passing 5 loose arguments around.

@Rusino
Rusino requested a review from mdebbar September 9, 2026 14:56
@Rusino Rusino changed the title [WebParagraph] Fix text pixelation, glyph clipping, and implement dual-mode subpixel raster caching [WebParagraph] Fix text pixelation, glyph clipping, and snapping offsets to avoid subpixel raster caching Sep 9, 2026
Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/web_paragraph_painter.dart Outdated
Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/painter.dart Outdated
Comment thread engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/painter.dart Outdated
@Rusino
Rusino requested a review from mdebbar September 10, 2026 15:14
Comment thread engine/src/flutter/lib/web_ui/test/webparagraph/paragraph_cache_test.dart Outdated
Comment thread engine/src/flutter/lib/web_ui/test/webparagraph/paragraph_cache_test.dart Outdated
@Rusino
Rusino requested a review from mdebbar September 10, 2026 17:23
@flutter-dashboard

Copy link
Copy Markdown

Golden file changes are available for triage from new commit, Click here to view.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

Changes reported for pull request #191736 at sha 0c11d0c

@flutter-dashboard

Copy link
Copy Markdown

Golden file changes are available for triage from new commit, Click here to view.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

Changes reported for pull request #191736 at sha c72c9a2

@mdebbar mdebbar added the autosubmit Merge PR when tree becomes green via auto submit App label Sep 11, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Sep 12, 2026
Merged via the queue into flutter:master with commit be32f06 Sep 12, 2026
23 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Sep 12, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Sep 12, 2026
Roll Flutter from 63b9518f16bc to 2553f89be1ef (62 revisions)

flutter/flutter@63b9518...2553f89

2026-09-12 [email protected] Roll clang to 07d67299a15ce03b053736e2d31a668ee0576987 (flutter/flutter#192607)
2026-09-12 [email protected] Roll Dart SDK from b24b352c29db to 7863887acbfb (3 revisions) (flutter/flutter#192665)
2026-09-12 [email protected] fix(tree): et format --all (flutter/flutter#192669)
2026-09-12 [email protected] Support activity-alias in Android launcher activity detection (flutter/flutter#187251)
2026-09-12 [email protected] fix: regenerate gradle lockfiles for external_textures integration test (flutter/flutter#192657)
2026-09-12 [email protected] Roll Skia from 51aa02b2c195 to e5e96533b732 (4 revisions) (flutter/flutter#192646)
2026-09-12 [email protected] [WebParagraph] Fix text pixelation, glyph clipping, and snapping offsets to avoid subpixel raster caching (flutter/flutter#191736)
2026-09-12 [email protected] fix(tests): Add ruby dep to Mac windowing_test (flutter/flutter#192656)
2026-09-12 [email protected] Remove some `unawaited_futures` outdated ignores (flutter/flutter#192026)
2026-09-11 [email protected] Bump Dart to 3.13, reformat, re-enable formatting check (flutter/flutter#192649)
2026-09-11 [email protected] [android] Isolate API-34 high contrast listener in AccessibilityBridge behind Api34Impl (flutter/flutter#192502)
2026-09-11 [email protected] Update style guide for Dart 3.13 (flutter/flutter#192639)
2026-09-11 [email protected] ci(bringups): remove bringup from these tests (flutter/flutter#192637)
2026-09-11 [email protected] Unmark new_gallery_windows_impeller__transition_perf as bringup (flutter/flutter#192633)
2026-09-11 [email protected] Unmark new_gallery_windows_skia__transition_perf as bringup (flutter/flutter#192635)
2026-09-11 [email protected] Roll Skia from f446aec4ce9e to 51aa02b2c195 (2 revisions) (flutter/flutter#192631)
2026-09-11 [email protected] fix(ci): mac verify binaries still racing (flutter/flutter#192634)
2026-09-11 [email protected] Roll Packages from 8a35b16 to 9caa77c (8 revisions) (flutter/flutter#192630)
2026-09-11 [email protected] Roll Dart SDK from a76fee2722e0 to b24b352c29db (2 revisions) (flutter/flutter#192627)
2026-09-11 [email protected] [tool] Migrate PackagesCommand and Pub to modular dependency injection (flutter/flutter#192460)
2026-09-11 [email protected] [tool] Migrate Darwin framework commands to modular dependency injection (flutter/flutter#192252)
2026-09-11 [email protected] [tool] Migrate Upgrade, Downgrade, and UpdatePackages commands to modular dependency injection (flutter/flutter#190787)
2026-09-11 [email protected] Roll Skia from b3fa80a0e168 to f446aec4ce9e (55 revisions) (flutter/flutter#192620)
2026-09-11 [email protected] Roll Dart SDK from d20d59cfa664 to a76fee2722e0 (2 revisions) (flutter/flutter#192616)
2026-09-11 [email protected] remove deeplink integration tests from bringup (flutter/flutter#192459)
2026-09-11 [email protected] Revert: [devicelab] Clean up Chrome profile directories and trap abort signals in web benchmarks (flutter/flutter#192615)
2026-09-11 [email protected] Roll Fuchsia Linux SDK from C0j8F-_Iy-OQZ6YyI... to m69nfK-wnHaMTe8P9... (flutter/flutter#192608)
2026-09-11 [email protected] Fix Wasm build cache mismatch in ResidentWebRunner and browser 404 fallbacks (flutter/flutter#190786)
2026-09-11 [email protected] Roll Dart SDK from 3ce3116bc9ec to d20d59cfa664 (1 revision) (flutter/flutter#192588)
2026-09-11 [email protected] [Linux] Keep the frame at the top of a resizing subsurface (flutter/flutter#192408)
2026-09-10 [email protected] [devicelab] Clean up Chrome profile directories and trap abort signals in web benchmarks (flutter/flutter#192584)
2026-09-10 [email protected] [web] Keep ImageSource alive until Picture disposal (flutter/flutter#192516)
2026-09-10 [email protected] Remove the accessibility evaluations feature flag (flutter/flutter#192266)
2026-09-10 [email protected] Promote Windows gradient benchmark tests to non-bringup (flutter/flutter#192581)
2026-09-10 [email protected] Remove commented out export in `text_input.dart` since the dart compiler has the correct behavior now (flutter/flutter#191539)
2026-09-10 [email protected] [flutter_tools] Do not mistake server-initiated requests for responses to client requests in AnalysisServer (flutter/flutter#192546)
2026-09-10 [email protected] Roll Packages from 36e088a to 8a35b16 (12 revisions) (flutter/flutter#192550)
2026-09-10 [email protected] Roll pub packages (flutter/flutter#192517)
2026-09-10 [email protected] feat(web): support --web-content-hash for entrypoint files (flutter/flutter#190153)
2026-09-10 [email protected] [web] Stop iOS auto-zoom by sizing semantic inputs at 16px (flutter/flutter#192328)
2026-09-10 [email protected] Roll Dart SDK from 0d55b7a6a9ee to 3ce3116bc9ec (2 revisions) (flutter/flutter#192534)
2026-09-10 [email protected] Update more enum instances comparison to use equality == since dart has the correct behavior now (flutter/flutter#191788)
2026-09-10 [email protected] Fix specify_nonobvious_local_variable_types lints in luci_resultdb_test.dart (flutter/flutter#192543)
2026-09-10 [email protected] Roll ICU from 8cc91d9b6ab9 to 6ebb40c59477 (1 revision) (flutter/flutter#192527)
2026-09-10 [email protected] [Linux] Make the framebuffer depth/stencil buffer optional (flutter/flutter#192156)
2026-09-10 [email protected] [Linux] Cancel input when events stop being delivered (flutter/flutter#192277)
...
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-web Web applications specifically team-web Owned by Web platform team will affect goldens Changes to golden files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[WebParagraph] Visual: At certain positions, text looks pixelated and grainy

2 participants