Skip to content

Commit be32f06

Browse files
Rusinomdebbar
andauthored
[WebParagraph] Fix text pixelation, glyph clipping, and snapping offsets to avoid subpixel raster caching (flutter#191736)
## 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 [flutter#188323](flutter#188323) --------- Co-authored-by: Mouad Debbar <[email protected]>
1 parent 3b3abd4 commit be32f06

5 files changed

Lines changed: 931 additions & 116 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/web_paragraph_painter.dart‎

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,78 @@
44

55
import 'dart:typed_data';
66

7+
import 'package:meta/meta.dart';
78
import 'package:ui/ui.dart' as ui;
89

910
import '../primitives/image.dart';
1011
import '../web_paragraph/painter.dart';
1112
import 'canvaskit_api.dart';
1213
import 'image.dart';
1314

15+
/// Represents a cached rasterized paragraph image along with the effective scale
16+
/// (combining device pixel ratio and canvas matrix scale) it was rendered at.
17+
class _ParagraphCacheEntry {
18+
_ParagraphCacheEntry({
19+
required this.image,
20+
required this.effectiveScaleX,
21+
required this.effectiveScaleY,
22+
});
23+
24+
static const double _scaleEpsilon = 0.001;
25+
26+
final EngineImage image;
27+
final double effectiveScaleX;
28+
final double effectiveScaleY;
29+
30+
/// Checks if the cached image scale matches the requested effective scales within [_scaleEpsilon].
31+
bool matchesScale({required double effectiveScaleX, required double effectiveScaleY}) {
32+
return (this.effectiveScaleX - effectiveScaleX).abs() < _scaleEpsilon &&
33+
(this.effectiveScaleY - effectiveScaleY).abs() < _scaleEpsilon;
34+
}
35+
}
36+
1437
class CanvasKitPainter extends WebParagraphPainter {
1538
CanvasKitPainter(super.paragraph);
1639

17-
EngineImage? _singleImageCache;
40+
_ParagraphCacheEntry? _cacheEntry;
41+
42+
/// For a specific paragraph, how many times it was rasterized in the current test
43+
@override
44+
@visibleForTesting
45+
int debugRasterizeCount = 0;
1846

1947
@override
20-
bool get hasCache => _singleImageCache != null;
48+
bool get hasCache => _cacheEntry != null;
2149

2250
@override
2351
void clearCache() {
24-
_singleImageCache?.dispose();
25-
_singleImageCache = null;
52+
_cacheEntry?.image.dispose();
53+
_cacheEntry = null;
2654
}
2755

28-
double? _lastDevicePixelRatio;
29-
3056
@override
3157
void paintParagraphText(
3258
ui.Canvas canvas,
3359
ui.Rect sourceRect,
3460
ui.Rect targetRect, {
3561
required ParagraphImageGenerator generateParagraphImage,
62+
required double effectiveScaleX,
63+
required double effectiveScaleY,
3664
}) {
37-
final double dpr = ui.window.devicePixelRatio;
38-
if (_lastDevicePixelRatio != dpr) {
65+
final _ParagraphCacheEntry? cacheEntry = _cacheEntry;
66+
if (cacheEntry != null &&
67+
!cacheEntry.matchesScale(
68+
effectiveScaleX: effectiveScaleX,
69+
effectiveScaleY: effectiveScaleY,
70+
)) {
3971
clearCache();
4072
}
41-
_lastDevicePixelRatio = dpr;
4273

4374
if (!hasCache) {
75+
assert(() {
76+
debugRasterizeCount++;
77+
return true;
78+
}());
4479
final imageInfo = SkImageInfo(
4580
alphaType: canvasKit.AlphaType.Unpremul,
4681
colorType: canvasKit.ColorType.RGBA_8888,
@@ -58,15 +93,19 @@ class CanvasKitPainter extends WebParagraphPainter {
5893
if (skImage == null) {
5994
throw Exception('Failed to convert text image bitmap to an SkImage.');
6095
}
61-
_singleImageCache = EngineImage(
62-
CkImageDelegate(skImage),
63-
skImage.width().toInt(),
64-
skImage.height().toInt(),
96+
_cacheEntry = _ParagraphCacheEntry(
97+
image: EngineImage(
98+
CkImageDelegate(skImage),
99+
skImage.width().toInt(),
100+
skImage.height().toInt(),
101+
),
102+
effectiveScaleX: effectiveScaleX,
103+
effectiveScaleY: effectiveScaleY,
65104
);
66105
}
67106

68107
canvas.drawImageRect(
69-
_singleImageCache!,
108+
_cacheEntry!.image,
70109
sourceRect,
71110
targetRect,
72111
ui.Paint()..filterQuality = ui.FilterQuality.none,

0 commit comments

Comments
 (0)