Skip to content

Commit 442c241

Browse files
[canvaskit] Use visualViewport.scale to determine device pixel ratio. (flutter#163688)
When using Chrome for Android with "Request Desktop Site" checked will cause the browser to set the `visualViewport` to have a very large size and a scale of less than 1. If we do not account for the scale, then we will create a huge bitmap (on a Pixel 4 emulator with "Request Desktop Site" checked the viewport size was ~2500 x ~5000). Attempting to create a too-large bitmap can silently fail and create a bitmap that is slightly too small. This fixes Flutter Web to take the visual viewport scale into account so we don't create enormous (and potentially flaky) bitmaps on every frame. BEFORE: ![Screenshot 2025-02-18 at 10 30 59 AM](https://github.com/user-attachments/assets/0bc5194a-59d7-4dcd-9240-b60c85f5d119) AFTER: ![Screenshot 2025-02-19 at 4 44 53 PM](https://github.com/user-attachments/assets/ac62e141-da87-4463-a5f9-d15477951bb7) Fixes flutter#136319 Fixes flutter#154162 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [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 [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
1 parent d0e2fc7 commit 442c241

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

engine/src/flutter/lib/web_ui/lib/src/engine/display.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,16 @@ class EngineFlutterDisplay extends ui.Display {
4040
///
4141
/// This value cannot be overriden by tests, for example.
4242
double get browserDevicePixelRatio {
43-
final double ratio = domWindow.devicePixelRatio;
43+
double ratio = domWindow.devicePixelRatio;
4444
// Guard against WebOS returning 0.
45-
return (ratio == 0.0) ? 1.0 : ratio;
45+
ratio = (ratio == 0.0) ? 1.0 : ratio;
46+
47+
// The device pixel ratio is also affected by the scale factor of the
48+
// viewport. For example, on Chrome for Android, if the page is requested
49+
// with "Request Desktop Site" enabled, then the viewport size will be
50+
// very large, with the viewport scale less than 1.
51+
final double scale = domWindow.visualViewport?.scale ?? 1.0;
52+
return ratio * scale;
4653
}
4754

4855
/// Overrides the default device pixel ratio.

engine/src/flutter/lib/web_ui/lib/src/engine/dom.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,6 +2139,10 @@ extension DomVisualViewportExtension on DomVisualViewport {
21392139
@JS('width')
21402140
external JSNumber? get _width;
21412141
double? get width => _width?.toDartDouble;
2142+
2143+
@JS('scale')
2144+
external JSNumber? get _scale;
2145+
double? get scale => _scale?.toDartDouble;
21422146
}
21432147

21442148
@JS()

engine/src/flutter/lib/web_ui/lib/src/engine/util.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,9 @@ class BitmapSize {
904904
@override
905905
int get hashCode => Object.hash(width, height);
906906

907+
@override
908+
String toString() => 'BitmapSize($width, $height)';
909+
907910
ui.Size toSize() {
908911
return ui.Size(width.toDouble(), height.toDouble());
909912
}

engine/src/flutter/lib/web_ui/test/engine/display_test.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,16 @@ void testMain() {
2727
display.debugOverrideDevicePixelRatio(null);
2828
expect(display.devicePixelRatio, originalDevicePixelRatio);
2929
});
30+
31+
test('computes device pixel ratio using window.devicePixelRatio and visualViewport.scale', () {
32+
final EngineFlutterDisplay display = EngineFlutterDisplay(
33+
id: 0,
34+
size: const ui.Size(100.0, 100.0),
35+
refreshRate: 60.0,
36+
);
37+
final double windowDpr = domWindow.devicePixelRatio;
38+
final double visualViewportScale = domWindow.visualViewport!.scale!;
39+
expect(display.browserDevicePixelRatio, windowDpr * visualViewportScale);
40+
});
3041
});
3142
}

0 commit comments

Comments
 (0)