Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 8df69a7

Browse files
authored
Move html window.devicePixelRatio access to EngineWindow and fix WebOS issue (#15315)
1 parent 4979039 commit 8df69a7

File tree

6 files changed

+22
-15
lines changed

6 files changed

+22
-15
lines changed

lib/web_ui/lib/src/engine/bitmap_canvas.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class BitmapCanvas extends EngineCanvas {
6969

7070
/// Keeps track of what device pixel ratio was used when this [BitmapCanvas]
7171
/// was created.
72-
final double _devicePixelRatio = html.window.devicePixelRatio;
72+
final double _devicePixelRatio = EngineWindow.browserDevicePixelRatio;
7373

7474
// Compensation for [_initializeViewport] snapping canvas position to 1 pixel.
7575
int _canvasPositionX, _canvasPositionY;
@@ -136,13 +136,13 @@ class BitmapCanvas extends EngineCanvas {
136136

137137
static int _widthToPhysical(double width) {
138138
final double boundsWidth = width + 1;
139-
return (boundsWidth * html.window.devicePixelRatio).ceil() +
139+
return (boundsWidth * EngineWindow.browserDevicePixelRatio).ceil() +
140140
2 * kPaddingPixels;
141141
}
142142

143143
static int _heightToPhysical(double height) {
144144
final double boundsHeight = height + 1;
145-
return (boundsHeight * html.window.devicePixelRatio).ceil() +
145+
return (boundsHeight * EngineWindow.browserDevicePixelRatio).ceil() +
146146
2 * kPaddingPixels;
147147
}
148148

@@ -180,7 +180,7 @@ class BitmapCanvas extends EngineCanvas {
180180
/// * [PersistedStandardPicture._recycleCanvas] which also uses this method
181181
/// for the same reason.
182182
bool isReusable() {
183-
return _devicePixelRatio == html.window.devicePixelRatio;
183+
return _devicePixelRatio == EngineWindow.browserDevicePixelRatio;
184184
}
185185

186186
/// Returns a data URI containing a representation of the image in this

lib/web_ui/lib/src/engine/canvas_pool.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ class _CanvasPool extends _SaveStackTracking {
8686
// * To make sure that when we scale the canvas by devicePixelRatio (see
8787
// _initializeViewport below) the pixels line up.
8888
final double cssWidth =
89-
_widthInBitmapPixels / html.window.devicePixelRatio;
89+
_widthInBitmapPixels / EngineWindow.browserDevicePixelRatio;
9090
final double cssHeight =
91-
_heightInBitmapPixels / html.window.devicePixelRatio;
91+
_heightInBitmapPixels / EngineWindow.browserDevicePixelRatio;
9292
_canvas = html.CanvasElement(
9393
width: _widthInBitmapPixels,
9494
height: _heightInBitmapPixels,
@@ -227,7 +227,7 @@ class _CanvasPool extends _SaveStackTracking {
227227

228228
// This scale makes sure that 1 CSS pixel is translated to the correct
229229
// number of bitmap pixels.
230-
ctx.scale(html.window.devicePixelRatio, html.window.devicePixelRatio);
230+
ctx.scale(EngineWindow.browserDevicePixelRatio, EngineWindow.browserDevicePixelRatio);
231231
}
232232

233233
void resetTransform() {

lib/web_ui/lib/src/engine/compositor/embedded_views.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ class HtmlViewEmbedder {
289289
//
290290
// HTML elements use logical (CSS) pixels, but we have been using physical
291291
// pixels, so scale down the head element to match the logical resolution.
292-
final double scale = html.window.devicePixelRatio;
292+
final double scale = EngineWindow.browserDevicePixelRatio;
293293
final double inverseScale = 1 / scale;
294294
final Matrix4 scaleMatrix =
295295
Matrix4.diagonal3Values(inverseScale, inverseScale, 1);

lib/web_ui/lib/src/engine/render_vertices.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,8 +617,8 @@ class _OffscreenCanvas {
617617
height: heightInPixels,
618618
);
619619
_glCanvas.className = 'gl-canvas';
620-
final double cssWidth = widthInPixels / html.window.devicePixelRatio;
621-
final double cssHeight = heightInPixels / html.window.devicePixelRatio;
620+
final double cssWidth = widthInPixels / EngineWindow.browserDevicePixelRatio;
621+
final double cssHeight = heightInPixels / EngineWindow.browserDevicePixelRatio;
622622
_glCanvas.style
623623
..position = 'absolute'
624624
..width = '${cssWidth}px'

lib/web_ui/lib/src/engine/surface/surface.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ void _debugRepaintSurfaceStatsOverlay(PersistedScene scene) {
231231
..fill();
232232

233233
final double physicalScreenWidth =
234-
html.window.innerWidth * html.window.devicePixelRatio;
234+
html.window.innerWidth * EngineWindow.browserDevicePixelRatio;
235235
final double physicalScreenHeight =
236-
html.window.innerHeight * html.window.devicePixelRatio;
236+
html.window.innerHeight * EngineWindow.browserDevicePixelRatio;
237237
final double physicsScreenPixelCount =
238238
physicalScreenWidth * physicalScreenHeight;
239239

@@ -402,9 +402,9 @@ void _debugPrintSurfaceStats(PersistedScene scene, int frameNumber) {
402402
return pixels;
403403
}).fold(0, (int total, int pixels) => total + pixels);
404404
final double physicalScreenWidth =
405-
html.window.innerWidth * html.window.devicePixelRatio;
405+
html.window.innerWidth * EngineWindow.browserDevicePixelRatio;
406406
final double physicalScreenHeight =
407-
html.window.innerHeight * html.window.devicePixelRatio;
407+
html.window.innerHeight * EngineWindow.browserDevicePixelRatio;
408408
final double physicsScreenPixelCount =
409409
physicalScreenWidth * physicalScreenHeight;
410410
final double screenPixelRatio = pixelCount / physicsScreenPixelCount;

lib/web_ui/lib/src/engine/window.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@ class EngineWindow extends ui.Window {
2020
}
2121

2222
if (experimentalUseSkia) {
23-
return html.window.devicePixelRatio;
23+
return browserDevicePixelRatio;
2424
} else {
2525
return 1.0;
2626
}
2727
}
2828

29+
/// Returns device pixel ratio returns by browser.
30+
static double get browserDevicePixelRatio {
31+
double ratio = html.window.devicePixelRatio;
32+
// Guard against WebOS returning 0.
33+
return (ratio == null || ratio == 0.0) ? 1.0 : ratio;
34+
}
35+
2936
/// Overrides the default device pixel ratio.
3037
///
3138
/// This is useful in tests to emulate screens of different dimensions.

0 commit comments

Comments
 (0)