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

Commit 244ae95

Browse files
committed
Clean-up CanvasKit canvas sizing.
1 parent 8780859 commit 244ae95

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ class CanvasKit {
7676
external SkGrContext MakeGrContext(int glContext);
7777
external SkSurface MakeOnScreenGLSurface(
7878
SkGrContext grContext,
79-
double width,
80-
double height,
79+
int width,
80+
int height,
8181
SkColorSpace colorSpace,
8282
);
8383
external SkSurface MakeSWCanvasSurface(html.CanvasElement canvas);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class HtmlViewEmbedder {
4949
Map<int, int> _clipCount = <int, int>{};
5050

5151
/// The size of the frame, in physical pixels.
52-
ui.Size _frameSize = _computeFrameSize();
52+
ui.Size _frameSize = ui.window.physicalSize;
5353

5454
void set frameSize(ui.Size size) {
5555
if (_frameSize == size) {

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class LayerTree {
1111
Layer? rootLayer;
1212

1313
/// The size (in physical pixels) of the frame to paint this layer tree into.
14-
final ui.Size frameSize = _computeFrameSize();
14+
final ui.Size frameSize = ui.window.physicalSize;
1515

1616
/// The devicePixelRatio of the frame to paint this layer tree into.
1717
double? devicePixelRatio;
@@ -54,14 +54,6 @@ class LayerTree {
5454
}
5555
}
5656

57-
ui.Size _computeFrameSize() {
58-
final ui.Size physicalSize = ui.window.physicalSize;
59-
return ui.Size(
60-
physicalSize.width.truncate().toDouble(),
61-
physicalSize.height.truncate().toDouble(),
62-
);
63-
}
64-
6557
/// A single frame to be rendered.
6658
class Frame {
6759
/// The canvas to render this frame to.

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,28 @@ class Surface {
113113
_surface = _wrapHtmlCanvas(size);
114114
}
115115

116-
CkSurface _wrapHtmlCanvas(ui.Size size) {
117-
final ui.Size logicalSize = size / ui.window.devicePixelRatio;
116+
CkSurface _wrapHtmlCanvas(ui.Size physicalSize) {
117+
// If `physicalSize` is not precise, use a slightly bigger canvas. This way
118+
// we ensure that the rendred picture covers the entire browser window.
119+
final int pixelWidth = physicalSize.width.ceil();
120+
final int pixelHeight = physicalSize.height.ceil();
118121
final html.CanvasElement htmlCanvas = html.CanvasElement(
119-
width: size.width.ceil(), height: size.height.ceil());
122+
width: pixelWidth,
123+
height: pixelHeight,
124+
);
125+
126+
// The logical size of the canvas is not based on the size of the window
127+
// but on the size of the canvas, which, due to `ceil()` above, may not be
128+
// the same as the window. We do not round/floor/ceil the logical size as
129+
// CSS pixels can contain more than one physical pixel and therefore to
130+
// match the size of the window precisely we use the most precise floating
131+
// point value we can get.
132+
final double logicalWidth = pixelWidth / ui.window.devicePixelRatio;
133+
final double logicalHeight = pixelHeight / ui.window.devicePixelRatio;
120134
htmlCanvas.style
121135
..position = 'absolute'
122-
..width = '${logicalSize.width.ceil()}px'
123-
..height = '${logicalSize.height.ceil()}px';
136+
..width = '${logicalWidth}px'
137+
..height = '${logicalHeight}px';
124138

125139
htmlElement = htmlCanvas;
126140
if (webGLVersion == -1 || canvasKitForceCpuOnly) {
@@ -153,8 +167,8 @@ class Surface {
153167

154168
SkSurface? skSurface = canvasKit.MakeOnScreenGLSurface(
155169
_grContext!,
156-
size.width,
157-
size.height,
170+
pixelWidth,
171+
pixelHeight,
158172
SkColorSpaceSRGB,
159173
);
160174

0 commit comments

Comments
 (0)