From 9efa5369acaf49c3d39e5d3ac99647379b096641 Mon Sep 17 00:00:00 2001 From: Harry Terkelsen Date: Thu, 13 Oct 2022 16:46:17 -0700 Subject: [PATCH] [canvaskit] Maintain a Surface dedicated to Picture.toImage (flutter/engine#36740) --- .../src/engine/canvaskit/embedded_views.dart | 2 +- .../lib/src/engine/canvaskit/picture.dart | 5 +++-- .../src/engine/canvaskit/surface_factory.dart | 6 +++++- .../lib/web_ui/test/canvaskit/h5vcc_test.dart | 2 +- .../test/canvaskit/surface_factory_test.dart | 18 +++++++++++------- .../web_ui/test/canvaskit/surface_test.dart | 4 ++-- 6 files changed, 23 insertions(+), 14 deletions(-) diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/embedded_views.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/embedded_views.dart index a2e028a0e8..9b221f7c3d 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/embedded_views.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/embedded_views.dart @@ -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; } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/picture.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/picture.dart index c0cab6d8fa..87a94db734 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/picture.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/picture.dart @@ -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 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 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.'); } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/surface_factory.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/surface_factory.dart index 2165287ef1..c0a1b4f3c5 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/surface_factory.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/surface_factory.dart @@ -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); diff --git a/engine/src/flutter/lib/web_ui/test/canvaskit/h5vcc_test.dart b/engine/src/flutter/lib/web_ui/test/canvaskit/h5vcc_test.dart index de5d509052..a12aedcb77 100644 --- a/engine/src/flutter/lib/web_ui/test/canvaskit/h5vcc_test.dart +++ b/engine/src/flutter/lib/web_ui/test/canvaskit/h5vcc_test.dart @@ -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); diff --git a/engine/src/flutter/lib/web_ui/test/canvaskit/surface_factory_test.dart b/engine/src/flutter/lib/web_ui/test/canvaskit/surface_factory_test.dart index 9fa98d64f0..d9da96d693 100644 --- a/engine/src/flutter/lib/web_ui/test/canvaskit/surface_factory_test.dart +++ b/engine/src/flutter/lib/web_ui/test/canvaskit/surface_factory_test.dart @@ -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 overlays = []; for (int i = 0; i < 3; i++) { - overlays.add(originalFactory.getOverlay()! + overlays.add(originalFactory.getSurface()! ..acquireFrame(const ui.Size(10, 10)) ..addToScene()); } diff --git a/engine/src/flutter/lib/web_ui/test/canvaskit/surface_test.dart b/engine/src/flutter/lib/web_ui/test/canvaskit/surface_test.dart index 7253381aaf..98a3dcaf02 100644 --- a/engine/src/flutter/lib/web_ui/test/canvaskit/surface_test.dart +++ b/engine/src/flutter/lib/web_ui/test/canvaskit/surface_test.dart @@ -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;