[canvaskit] Maintain a Surface dedicated to Picture.toImage (flutter/engine#36740)

This commit is contained in:
Harry Terkelsen
2022-10-13 16:46:17 -07:00
committed by GitHub
parent 9972d4ccde
commit 9efa5369ac
6 changed files with 23 additions and 14 deletions

View File

@@ -676,7 +676,7 @@ class HtmlViewEmbedder {
assert(!_overlays.containsKey(viewId));
// Try reusing a cached overlay created for another platform view.
final Surface overlay = SurfaceFactory.instance.getOverlay()!;
final Surface overlay = SurfaceFactory.instance.getSurface()!;
overlay.createOrUpdateSurface(_frameSize);
_overlays[viewId] = overlay;
}

View File

@@ -13,6 +13,7 @@ import 'canvaskit_api.dart';
import 'image.dart';
import 'skia_object_cache.dart';
import 'surface.dart';
import 'surface_factory.dart';
/// Implements [ui.Picture] on top of [SkPicture].
///
@@ -101,10 +102,11 @@ class CkPicture extends ManagedSkiaObject<SkPicture> implements ui.Picture {
@override
ui.Image toImageSync(int width, int height) {
assert(debugCheckNotDisposed('Cannot convert picture to image.'));
final Surface surface = Surface();
final Surface surface = SurfaceFactory.instance.pictureToImageSurface;
final CkSurface ckSurface =
surface.createOrUpdateSurface(ui.Size(width.toDouble(), height.toDouble()));
final CkCanvas ckCanvas = ckSurface.getCanvas();
ckCanvas.clear(const ui.Color(0x00000000));
ckCanvas.drawPicture(this);
final SkImage skImage = ckSurface.surface.makeImageSnapshot();
final SkImageInfo imageInfo = SkImageInfo(
@@ -116,7 +118,6 @@ class CkPicture extends ManagedSkiaObject<SkPicture> implements ui.Picture {
);
final Uint8List pixels = skImage.readPixels(0, 0, imageInfo);
final SkImage? rasterImage = canvasKit.MakeImage(imageInfo, pixels, 4 * width);
surface.dispose();
if (rasterImage == null) {
throw StateError('Unable to convert image pixels into SkImage.');
}

View File

@@ -46,6 +46,10 @@ class SurfaceFactory {
/// all painting commands.
final Surface baseSurface = Surface();
/// A surface used specifically for `Picture.toImage` calls, which can be
/// reused in order to avoid creating too many WebGL contexts.
late final Surface pictureToImageSurface = Surface();
/// The maximum number of surfaces which can be live at once.
final int maximumSurfaces;
@@ -81,7 +85,7 @@ class SurfaceFactory {
/// Gets an overlay surface from the cache or creates a new one if it wouldn't
/// exceed the maximum. If there are no available surfaces, returns `null`.
Surface? getOverlay() {
Surface? getSurface() {
if (_cache.isNotEmpty) {
final Surface surface = _cache.removeLast();
_liveSurfaces.add(surface);

View File

@@ -51,7 +51,7 @@ void testMain() {
});
test('Surface acquireFrame uses getH5vccSkSurface', () {
final Surface surface = SurfaceFactory.instance.getOverlay()!;
final Surface surface = SurfaceFactory.instance.getSurface()!;
surface.acquireFrame(ui.Size.zero);
expect(getH5vccSkSurfaceCalledCount, 1);

View File

@@ -26,20 +26,24 @@ void testMain() {
expect(SurfaceFactory(2).maximumSurfaces, 2);
});
test('getOverlay', () {
test('has a Surface dedicated to Picture.toImage', () {
expect(SurfaceFactory(1).pictureToImageSurface, isNotNull);
});
test('getSurface', () {
final SurfaceFactory factory = SurfaceFactory(3);
expect(factory.baseSurface, isNotNull);
expect(factory.debugSurfaceCount, equals(1));
// Get a surface from the factory, it should be unique.
final Surface? newSurface = factory.getOverlay();
final Surface? newSurface = factory.getSurface();
expect(newSurface, isNot(equals(factory.baseSurface)));
expect(factory.debugSurfaceCount, equals(2));
// Get another surface from the factory. Now we are at maximum capacity.
final Surface? anotherSurface = factory.getOverlay();
final Surface? anotherSurface = factory.getSurface();
expect(anotherSurface, isNot(equals(factory.baseSurface)));
expect(factory.debugSurfaceCount, equals(3));
@@ -49,12 +53,12 @@ void testMain() {
final SurfaceFactory factory = SurfaceFactory(3);
// Create a new surface and immediately release it.
final Surface? surface = factory.getOverlay();
final Surface? surface = factory.getSurface();
factory.releaseSurface(surface!);
// If we create a new surface, it should be the same as the one we
// just created.
final Surface? newSurface = factory.getOverlay();
final Surface? newSurface = factory.getSurface();
expect(newSurface, equals(surface));
});
@@ -63,7 +67,7 @@ void testMain() {
expect(factory.isLive(factory.baseSurface), isTrue);
final Surface? surface = factory.getOverlay();
final Surface? surface = factory.getSurface();
expect(factory.isLive(surface!), isTrue);
factory.releaseSurface(surface);
@@ -86,7 +90,7 @@ void testMain() {
// Create a few overlay surfaces
final List<Surface> overlays = <Surface>[];
for (int i = 0; i < 3; i++) {
overlays.add(originalFactory.getOverlay()!
overlays.add(originalFactory.getSurface()!
..acquireFrame(const ui.Size(10, 10))
..addToScene());
}

View File

@@ -21,7 +21,7 @@ void testMain() {
});
test('Surface allocates canvases efficiently', () {
final Surface? surface = SurfaceFactory.instance.getOverlay();
final Surface? surface = SurfaceFactory.instance.getSurface();
final CkSurface originalSurface =
surface!.acquireFrame(const ui.Size(9, 19)).skiaSurface;
final DomCanvasElement original = surface.htmlCanvas!;
@@ -125,7 +125,7 @@ void testMain() {
test(
'Surface creates new context when WebGL context is restored',
() async {
final Surface? surface = SurfaceFactory.instance.getOverlay();
final Surface? surface = SurfaceFactory.instance.getSurface();
expect(surface!.debugForceNewContext, isTrue);
final CkSurface before =
surface.acquireFrame(const ui.Size(9, 19)).skiaSurface;