[canvaskit] Make the surface size exactly the window size so filters work correctly at the edges (flutter/engine#26887)
This commit is contained in:
@@ -448,6 +448,7 @@ class HtmlViewEmbedder {
|
||||
// If there's an active overlay for the view ID, continue using it.
|
||||
Surface? overlay = _overlays[viewId];
|
||||
if (overlay != null && !_viewsUsingBackupSurface.contains(viewId)) {
|
||||
overlay.createOrUpdateSurfaces(_frameSize);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -462,6 +463,7 @@ class HtmlViewEmbedder {
|
||||
if (overlay == SurfaceFactory.instance.backupSurface) {
|
||||
_viewsUsingBackupSurface.add(viewId);
|
||||
}
|
||||
overlay.createOrUpdateSurfaces(_frameSize);
|
||||
_overlays[viewId] = overlay;
|
||||
}
|
||||
|
||||
|
||||
@@ -53,6 +53,8 @@ class LayerTree {
|
||||
for (int i = 0; i < overlayCanvases.length; i++) {
|
||||
internalNodesCanvas.addCanvas(overlayCanvases[i]);
|
||||
}
|
||||
// Clear the canvases before painting
|
||||
internalNodesCanvas.clear(ui.Color(0x00000000));
|
||||
final PaintContext context = PaintContext(
|
||||
internalNodesCanvas,
|
||||
frame.canvas,
|
||||
|
||||
@@ -70,6 +70,13 @@ class CkNWayCanvas {
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls [clear] on all canvases.
|
||||
void clear(ui.Color color) {
|
||||
for (int i = 0; i < _canvases.length; i++) {
|
||||
_canvases[i].clear(color);
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls [clipPath] on all canvases.
|
||||
void clipPath(CkPath path, bool doAntiAlias) {
|
||||
for (int i = 0; i < _canvases.length; i++) {
|
||||
|
||||
@@ -33,8 +33,6 @@ class Rasterizer {
|
||||
SurfaceFactory.instance.baseSurface.acquireFrame(layerTree.frameSize);
|
||||
HtmlViewEmbedder.instance.frameSize = layerTree.frameSize;
|
||||
final CkCanvas canvas = frame.skiaCanvas;
|
||||
// Clear the canvas before trying to draw to it again.
|
||||
canvas.clear(ui.Color(0x00000000));
|
||||
final Frame compositorFrame =
|
||||
context.acquireFrame(canvas, HtmlViewEmbedder.instance);
|
||||
|
||||
|
||||
@@ -74,6 +74,7 @@ class Surface {
|
||||
void Function(html.Event)? _cachedContextRestoredListener;
|
||||
|
||||
SkGrContext? _grContext;
|
||||
int? _glContext;
|
||||
int? _skiaCacheBytes;
|
||||
|
||||
/// The root HTML element for this surface.
|
||||
@@ -110,7 +111,7 @@ class Surface {
|
||||
///
|
||||
/// The given [size] is in physical pixels.
|
||||
SurfaceFrame acquireFrame(ui.Size size) {
|
||||
final CkSurface surface = _createOrUpdateSurfaces(size);
|
||||
final CkSurface surface = createOrUpdateSurfaces(size);
|
||||
|
||||
if (surface.context != null) {
|
||||
canvasKit.setCurrentContext(surface.context!);
|
||||
@@ -130,21 +131,23 @@ class Surface {
|
||||
_addedToScene = true;
|
||||
}
|
||||
|
||||
ui.Size? _currentSize;
|
||||
ui.Size? _currentCanvasPhysicalSize;
|
||||
ui.Size? _currentSurfaceSize;
|
||||
double _currentDevicePixelRatio = -1;
|
||||
|
||||
CkSurface _createOrUpdateSurfaces(ui.Size size) {
|
||||
/// Creates a <canvas> and SkSurface for the given [size].
|
||||
CkSurface createOrUpdateSurfaces(ui.Size size) {
|
||||
if (size.isEmpty) {
|
||||
throw CanvasKitError('Cannot create surfaces of empty size.');
|
||||
}
|
||||
|
||||
// Check if the window is shrinking in size, and if so, don't allocate a
|
||||
// new canvas as the previous canvas is big enough to fit everything.
|
||||
final ui.Size? previousSize = _currentSize;
|
||||
// Check if the window is the same size as before, and if so, don't allocate
|
||||
// a new canvas as the previous canvas is big enough to fit everything.
|
||||
final ui.Size? previousSurfaceSize = _currentSurfaceSize;
|
||||
if (!_forceNewContext &&
|
||||
previousSize != null &&
|
||||
size.width <= previousSize.width &&
|
||||
size.height <= previousSize.height) {
|
||||
previousSurfaceSize != null &&
|
||||
size.width == previousSurfaceSize.width &&
|
||||
size.height == previousSurfaceSize.height) {
|
||||
// The existing surface is still reusable.
|
||||
if (window.devicePixelRatio != _currentDevicePixelRatio) {
|
||||
_updateLogicalHtmlCanvasSize();
|
||||
@@ -153,19 +156,37 @@ class Surface {
|
||||
}
|
||||
|
||||
_currentDevicePixelRatio = window.devicePixelRatio;
|
||||
_currentSize = _currentSize == null
|
||||
// First frame. Allocate a canvas of the exact size as the window. The
|
||||
// window is frequently never resized, particularly on mobile, so using
|
||||
// the exact size is most optimal.
|
||||
? size
|
||||
// The window is growing. Overallocate to prevent frequent reallocations.
|
||||
: size * 1.4;
|
||||
|
||||
_surface?.dispose();
|
||||
_surface = null;
|
||||
_addedToScene = false;
|
||||
// If the current canvas size is smaller than the requested size then create
|
||||
// a new, larger, canvas. Then update the GR context so we can create a new
|
||||
// SkSurface.
|
||||
final ui.Size? previousCanvasSize = _currentCanvasPhysicalSize;
|
||||
if (_forceNewContext ||
|
||||
previousCanvasSize == null ||
|
||||
size.width > previousCanvasSize.width ||
|
||||
size.height > previousCanvasSize.height) {
|
||||
// Initialize a new, larger, canvas. If the size is growing, then make the
|
||||
// new canvas larger than required to avoid many canvas creations.
|
||||
final ui.Size newSize = previousCanvasSize == null ? size : size * 1.4;
|
||||
|
||||
return _surface = _createNewSurface(_currentSize!);
|
||||
// Only resources from the current context can be disposed.
|
||||
if (_glContext != null && _glContext != 0) {
|
||||
canvasKit.setCurrentContext(_glContext!);
|
||||
}
|
||||
_surface?.dispose();
|
||||
_surface = null;
|
||||
_addedToScene = false;
|
||||
_grContext?.releaseResourcesAndAbandonContext();
|
||||
_grContext?.delete();
|
||||
_grContext = null;
|
||||
|
||||
_createNewCanvas(newSize);
|
||||
_currentCanvasPhysicalSize = newSize;
|
||||
}
|
||||
|
||||
_currentSurfaceSize = size;
|
||||
_translateCanvas();
|
||||
return _surface = _createNewSurface(size);
|
||||
}
|
||||
|
||||
/// Sets the CSS size of the canvas so that canvas pixels are 1:1 with device
|
||||
@@ -178,13 +199,28 @@ class Surface {
|
||||
/// match the size of the window precisely we use the most precise floating
|
||||
/// point value we can get.
|
||||
void _updateLogicalHtmlCanvasSize() {
|
||||
final double logicalWidth = _pixelWidth / ui.window.devicePixelRatio;
|
||||
final double logicalHeight = _pixelHeight / ui.window.devicePixelRatio;
|
||||
final double logicalWidth = _pixelWidth / window.devicePixelRatio;
|
||||
final double logicalHeight = _pixelHeight / window.devicePixelRatio;
|
||||
htmlCanvas!.style
|
||||
..width = '${logicalWidth}px'
|
||||
..height = '${logicalHeight}px';
|
||||
}
|
||||
|
||||
/// Translate the canvas so the surface covers the visible portion of the
|
||||
/// screen.
|
||||
///
|
||||
/// The <canvas> may be larger than the visible screen, but the SkSurface is
|
||||
/// exactly the size of the visible screen. Unfortunately, the SkSurface is
|
||||
/// drawn in the lower left corner of the <canvas>, and without translation,
|
||||
/// only the top left of the <canvas> is visible. So we shift the canvas up so
|
||||
/// the bottom left corner is visible.
|
||||
void _translateCanvas() {
|
||||
final int surfaceHeight = _currentSurfaceSize!.height.ceil();
|
||||
final double offset =
|
||||
(_pixelHeight - surfaceHeight) / window.devicePixelRatio;
|
||||
htmlCanvas!.style..transform = 'translate(0, -${offset}px)';
|
||||
}
|
||||
|
||||
void _contextRestoredListener(html.Event event) {
|
||||
assert(
|
||||
_contextLost,
|
||||
@@ -212,8 +248,8 @@ class Surface {
|
||||
|
||||
/// This function is expensive.
|
||||
///
|
||||
/// It's better to reuse surface if possible.
|
||||
CkSurface _createNewSurface(ui.Size physicalSize) {
|
||||
/// It's better to reuse canvas if possible.
|
||||
void _createNewCanvas(ui.Size physicalSize) {
|
||||
// Clear the container, if it's not empty. We're going to create a new <canvas>.
|
||||
if (this.htmlCanvas != null) {
|
||||
this.htmlCanvas!.removeEventListener(
|
||||
@@ -264,16 +300,7 @@ class Surface {
|
||||
_forceNewContext = false;
|
||||
_contextLost = false;
|
||||
|
||||
htmlElement.append(htmlCanvas);
|
||||
|
||||
if (webGLVersion == -1) {
|
||||
return _makeSoftwareCanvasSurface(
|
||||
htmlCanvas, 'WebGL support not detected');
|
||||
} else if (canvasKitForceCpuOnly) {
|
||||
return _makeSoftwareCanvasSurface(
|
||||
htmlCanvas, 'CPU rendering forced by application');
|
||||
} else {
|
||||
// Try WebGL first.
|
||||
if (webGLVersion != -1 && !canvasKitForceCpuOnly) {
|
||||
final int glContext = canvasKit.GetWebGLContext(
|
||||
htmlCanvas,
|
||||
SkWebGLContextOptions(
|
||||
@@ -284,35 +311,49 @@ class Surface {
|
||||
),
|
||||
);
|
||||
|
||||
if (glContext == 0) {
|
||||
return _makeSoftwareCanvasSurface(
|
||||
htmlCanvas, 'Failed to initialize WebGL context');
|
||||
_glContext = glContext;
|
||||
|
||||
if (_glContext != 0) {
|
||||
_grContext = canvasKit.MakeGrContext(glContext);
|
||||
if (_grContext == null) {
|
||||
throw CanvasKitError('Failed to initialize CanvasKit. '
|
||||
'CanvasKit.MakeGrContext returned null.');
|
||||
}
|
||||
// Set the cache byte limit for this grContext, if not specified it will
|
||||
// use CanvasKit's default.
|
||||
_syncCacheBytes();
|
||||
}
|
||||
}
|
||||
|
||||
_grContext = canvasKit.MakeGrContext(glContext);
|
||||
|
||||
if (_grContext == null) {
|
||||
throw CanvasKitError(
|
||||
'Failed to initialize CanvasKit. CanvasKit.MakeGrContext returned null.');
|
||||
}
|
||||
|
||||
// Set the cache byte limit for this grContext, if not specified it will use
|
||||
// CanvasKit's default.
|
||||
_syncCacheBytes();
|
||||
htmlElement.append(htmlCanvas);
|
||||
}
|
||||
|
||||
CkSurface _createNewSurface(ui.Size size) {
|
||||
assert(htmlCanvas != null);
|
||||
if (webGLVersion == -1) {
|
||||
return _makeSoftwareCanvasSurface(
|
||||
htmlCanvas!, 'WebGL support not detected');
|
||||
} else if (canvasKitForceCpuOnly) {
|
||||
return _makeSoftwareCanvasSurface(
|
||||
htmlCanvas!, 'CPU rendering forced by application');
|
||||
} else if (_glContext == 0) {
|
||||
return _makeSoftwareCanvasSurface(
|
||||
htmlCanvas!, 'Failed to initialize WebGL context');
|
||||
} else {
|
||||
canvasKit.setCurrentContext(_glContext!);
|
||||
SkSurface? skSurface = canvasKit.MakeOnScreenGLSurface(
|
||||
_grContext!,
|
||||
_pixelWidth,
|
||||
_pixelHeight,
|
||||
size.width.ceil(),
|
||||
size.height.ceil(),
|
||||
SkColorSpaceSRGB,
|
||||
);
|
||||
|
||||
if (skSurface == null) {
|
||||
return _makeSoftwareCanvasSurface(
|
||||
htmlCanvas, 'Failed to initialize WebGL surface');
|
||||
htmlCanvas!, 'Failed to initialize WebGL surface');
|
||||
}
|
||||
|
||||
return CkSurface(skSurface, _grContext, glContext);
|
||||
return CkSurface(skSurface, _glContext);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -327,7 +368,6 @@ class Surface {
|
||||
return CkSurface(
|
||||
canvasKit.MakeSWCanvasSurface(htmlCanvas),
|
||||
null,
|
||||
null,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -354,10 +394,9 @@ class Surface {
|
||||
/// A Dart wrapper around Skia's CkSurface.
|
||||
class CkSurface {
|
||||
final SkSurface _surface;
|
||||
final SkGrContext? _grContext;
|
||||
final int? _glContext;
|
||||
|
||||
CkSurface(this._surface, this._grContext, this._glContext);
|
||||
CkSurface(this._surface, this._glContext);
|
||||
|
||||
CkCanvas getCanvas() {
|
||||
assert(!_isDisposed, 'Attempting to use the canvas of a disposed surface');
|
||||
@@ -378,17 +417,7 @@ class CkSurface {
|
||||
if (_isDisposed) {
|
||||
return;
|
||||
}
|
||||
// Only resources from the current context can be disposed.
|
||||
if (_glContext != null) {
|
||||
canvasKit.setCurrentContext(_glContext!);
|
||||
}
|
||||
_surface.dispose();
|
||||
|
||||
// In CPU-only mode there's no graphics context.
|
||||
if (_grContext != null) {
|
||||
_grContext!.releaseResourcesAndAbandonContext();
|
||||
_grContext!.delete();
|
||||
}
|
||||
_isDisposed = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:test/bootstrap/browser.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:ui/src/engine.dart';
|
||||
import 'package:ui/ui.dart' as ui;
|
||||
|
||||
import 'package:web_engine_tester/golden_tester.dart';
|
||||
|
||||
import 'common.dart';
|
||||
|
||||
void main() {
|
||||
internalBootstrapBrowserTest(() => testMain);
|
||||
}
|
||||
|
||||
const ui.Rect region = const ui.Rect.fromLTRB(0, 0, 500, 500);
|
||||
|
||||
void testMain() {
|
||||
group('BackdropFilter', () {
|
||||
setUpCanvasKitTest();
|
||||
window.debugOverrideDevicePixelRatio(1.0);
|
||||
|
||||
test('blur renders to the edges', () async {
|
||||
// Make a checkerboard picture so we can see the blur.
|
||||
final CkPictureRecorder recorder = CkPictureRecorder();
|
||||
final CkCanvas canvas = recorder.beginRecording(region);
|
||||
canvas.drawColor(ui.Color(0xffffffff), ui.BlendMode.srcOver);
|
||||
final double sideLength = region.width / 20;
|
||||
final int rows = (region.height / sideLength).ceil();
|
||||
|
||||
for (int row = 0; row < rows; row++) {
|
||||
for (int column = 0; column < 10; column++) {
|
||||
final ui.Rect rect = ui.Rect.fromLTWH(
|
||||
row.isEven
|
||||
? (column * 2) * sideLength
|
||||
: (column * 2 + 1) * sideLength,
|
||||
row * sideLength,
|
||||
sideLength,
|
||||
sideLength,
|
||||
);
|
||||
canvas.drawRect(rect, CkPaint()..color = ui.Color(0xffff0000));
|
||||
}
|
||||
}
|
||||
final CkPicture checkerboard = recorder.endRecording();
|
||||
|
||||
final LayerSceneBuilder builder = LayerSceneBuilder();
|
||||
builder.pushOffset(0, 0);
|
||||
builder.addPicture(ui.Offset.zero, checkerboard);
|
||||
builder.pushBackdropFilter(ui.ImageFilter.blur(sigmaX: 10, sigmaY: 10));
|
||||
EnginePlatformDispatcher.instance.rasterizer!
|
||||
.draw(builder.build().layerTree);
|
||||
await matchGoldenFile('canvaskit_backdropfilter_blur_edges.png',
|
||||
region: region);
|
||||
});
|
||||
// TODO: https://github.com/flutter/flutter/issues/60040
|
||||
// TODO: https://github.com/flutter/flutter/issues/71520
|
||||
}, skip: isIosSafari || isFirefox);
|
||||
}
|
||||
@@ -18,56 +18,88 @@ void main() {
|
||||
void testMain() {
|
||||
group('CanvasKit', () {
|
||||
setUpCanvasKitTest();
|
||||
setUp(() {
|
||||
window.debugOverrideDevicePixelRatio(1.0);
|
||||
});
|
||||
|
||||
test('Surface allocates canvases efficiently', () {
|
||||
final Surface surface = SurfaceFactory.instance.getSurface();
|
||||
final CkSurface original =
|
||||
CkSurface originalSurface =
|
||||
surface.acquireFrame(ui.Size(9, 19)).skiaSurface;
|
||||
final html.CanvasElement original = surface.htmlCanvas!;
|
||||
|
||||
// Expect exact requested dimensions.
|
||||
expect(original.width(), 9);
|
||||
expect(original.height(), 19);
|
||||
expect(surface.htmlCanvas!.style.width, '9px');
|
||||
expect(surface.htmlCanvas!.style.height, '19px');
|
||||
expect(original.width, 9);
|
||||
expect(original.height, 19);
|
||||
expect(original.style.width, '9px');
|
||||
expect(original.style.height, '19px');
|
||||
expect(originalSurface.width(), 9);
|
||||
expect(originalSurface.height(), 19);
|
||||
|
||||
// Shrinking reuses the existing surface straight-up.
|
||||
final CkSurface shrunk = surface.acquireFrame(ui.Size(5, 15)).skiaSurface;
|
||||
// Shrinking reuses the existing canvas straight-up.
|
||||
CkSurface shrunkSurface =
|
||||
surface.acquireFrame(ui.Size(5, 15)).skiaSurface;
|
||||
final html.CanvasElement shrunk = surface.htmlCanvas!;
|
||||
expect(shrunk, same(original));
|
||||
expect(surface.htmlCanvas!.style.width, '9px');
|
||||
expect(surface.htmlCanvas!.style.height, '19px');
|
||||
expect(shrunk.style.width, '9px');
|
||||
expect(shrunk.style.height, '19px');
|
||||
expect(shrunkSurface, isNot(same(original)));
|
||||
expect(shrunkSurface.width(), 5);
|
||||
expect(shrunkSurface.height(), 15);
|
||||
|
||||
// The first increase will allocate a new surface, but will overallocate
|
||||
// The first increase will allocate a new canvas, but will overallocate
|
||||
// by 40% to accommodate future increases.
|
||||
final CkSurface firstIncrease =
|
||||
CkSurface firstIncreaseSurface =
|
||||
surface.acquireFrame(ui.Size(10, 20)).skiaSurface;
|
||||
final html.CanvasElement firstIncrease = surface.htmlCanvas!;
|
||||
expect(firstIncrease, isNot(same(original)));
|
||||
expect(firstIncreaseSurface, isNot(same(shrunkSurface)));
|
||||
|
||||
// Expect overallocated dimensions
|
||||
expect(firstIncrease.width(), 14);
|
||||
expect(firstIncrease.height(), 28);
|
||||
expect(surface.htmlCanvas!.style.width, '14px');
|
||||
expect(surface.htmlCanvas!.style.height, '28px');
|
||||
expect(firstIncrease.width, 14);
|
||||
expect(firstIncrease.height, 28);
|
||||
expect(firstIncrease.style.width, '14px');
|
||||
expect(firstIncrease.style.height, '28px');
|
||||
expect(firstIncreaseSurface.width(), 10);
|
||||
expect(firstIncreaseSurface.height(), 20);
|
||||
|
||||
// Subsequent increases within 40% reuse the old surface.
|
||||
final CkSurface secondIncrease =
|
||||
// Subsequent increases within 40% reuse the old canvas.
|
||||
CkSurface secondIncreaseSurface =
|
||||
surface.acquireFrame(ui.Size(11, 22)).skiaSurface;
|
||||
final html.CanvasElement secondIncrease = surface.htmlCanvas!;
|
||||
expect(secondIncrease, same(firstIncrease));
|
||||
expect(secondIncreaseSurface, isNot(same(firstIncreaseSurface)));
|
||||
expect(secondIncreaseSurface.width(), 11);
|
||||
expect(secondIncreaseSurface.height(), 22);
|
||||
|
||||
// Increases beyond the 40% limit will cause a new allocation.
|
||||
final CkSurface huge = surface.acquireFrame(ui.Size(20, 40)).skiaSurface;
|
||||
expect(huge, isNot(same(firstIncrease)));
|
||||
CkSurface hugeSurface = surface.acquireFrame(ui.Size(20, 40)).skiaSurface;
|
||||
final html.CanvasElement huge = surface.htmlCanvas!;
|
||||
expect(huge, isNot(same(secondIncrease)));
|
||||
expect(hugeSurface, isNot(same(secondIncreaseSurface)));
|
||||
|
||||
// Also over-allocated
|
||||
expect(huge.width(), 28);
|
||||
expect(huge.height(), 56);
|
||||
expect(surface.htmlCanvas!.style.width, '28px');
|
||||
expect(surface.htmlCanvas!.style.height, '56px');
|
||||
expect(huge.width, 28);
|
||||
expect(huge.height, 56);
|
||||
expect(huge.style.width, '28px');
|
||||
expect(huge.style.height, '56px');
|
||||
expect(hugeSurface.width(), 20);
|
||||
expect(hugeSurface.height(), 40);
|
||||
|
||||
// Shrink again. Reuse the last allocated surface.
|
||||
final CkSurface shrunk2 =
|
||||
CkSurface shrunkSurface2 =
|
||||
surface.acquireFrame(ui.Size(5, 15)).skiaSurface;
|
||||
final html.CanvasElement shrunk2 = surface.htmlCanvas!;
|
||||
expect(shrunk2, same(huge));
|
||||
});
|
||||
expect(shrunkSurface2, isNot(same(hugeSurface)));
|
||||
expect(shrunkSurface2.width(), 5);
|
||||
expect(shrunkSurface2.height(), 15);
|
||||
// Skipping on Firefox for now since Firefox headless doesn't support WebGL
|
||||
// This causes issues in the test since we create a Canvas-backed surface,
|
||||
// which cannot be a different size from the canvas.
|
||||
// TODO(hterkelsen): See if we can give a custom size for software
|
||||
// surfaces.
|
||||
}, skip: isFirefox || isIosSafari);
|
||||
|
||||
test(
|
||||
'Surface creates new context when WebGL context is restored',
|
||||
|
||||
Reference in New Issue
Block a user