From d643eddfed58d099e9a62647c0f90e542cce0ff9 Mon Sep 17 00:00:00 2001 From: Harry Terkelsen Date: Mon, 6 Jul 2020 14:11:38 -0700 Subject: [PATCH] [CanvasKit] Dispose the overlay surface when a platform view is disposed --- .../src/engine/compositor/embedded_views.dart | 21 +++++++++++-------- .../lib/src/engine/compositor/surface.dart | 10 ++++++++- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/web_ui/lib/src/engine/compositor/embedded_views.dart b/lib/web_ui/lib/src/engine/compositor/embedded_views.dart index 3df67966522d5..dfa1ff947ef5f 100644 --- a/lib/web_ui/lib/src/engine/compositor/embedded_views.dart +++ b/lib/web_ui/lib/src/engine/compositor/embedded_views.dart @@ -2,7 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. - part of engine; /// This composites HTML views into the [ui.Scene]. @@ -255,7 +254,8 @@ class HtmlViewEmbedder { final CkPath path = CkPath(); path.addRRect(mutator.rrect!); _ensureSvgPathDefs(); - html.Element pathDefs = _svgPathDefs!.querySelector('#sk_path_defs')!; + html.Element pathDefs = + _svgPathDefs!.querySelector('#sk_path_defs')!; _clipPathCount += 1; html.Element newClipPath = html.Element.html('' @@ -266,7 +266,8 @@ class HtmlViewEmbedder { } else if (mutator.path != null) { final CkPath path = mutator.path as CkPath; _ensureSvgPathDefs(); - html.Element pathDefs = _svgPathDefs!.querySelector('#sk_path_defs')!; + html.Element pathDefs = + _svgPathDefs!.querySelector('#sk_path_defs')!; _clipPathCount += 1; html.Element newClipPath = html.Element.html('' @@ -369,6 +370,8 @@ class HtmlViewEmbedder { if (_overlays[viewId] != null) { final Overlay overlay = _overlays[viewId]!; overlay.surface.htmlElement?.remove(); + overlay.surface.htmlElement = null; + overlay.skSurface?.dispose(); } _overlays.remove(viewId); _currentCompositionParams.remove(viewId); @@ -402,10 +405,10 @@ class EmbeddedViewParams { if (identical(this, other)) { return true; } - return other is EmbeddedViewParams - && other.offset == offset - && other.size == size - && other.mutators == mutators; + return other is EmbeddedViewParams && + other.offset == offset && + other.size == size && + other.mutators == mutators; } int get hashCode => ui.hashValues(offset, size, mutators); @@ -524,8 +527,8 @@ class MutatorsStack extends Iterable { if (identical(other, this)) { return true; } - return other is MutatorsStack - && _listEquals(other._mutators, _mutators); + return other is MutatorsStack && + _listEquals(other._mutators, _mutators); } int get hashCode => ui.hashList(_mutators); diff --git a/lib/web_ui/lib/src/engine/compositor/surface.dart b/lib/web_ui/lib/src/engine/compositor/surface.dart index 3220f57c33cfa..2b910aff48748 100644 --- a/lib/web_ui/lib/src/engine/compositor/surface.dart +++ b/lib/web_ui/lib/src/engine/compositor/surface.dart @@ -134,7 +134,7 @@ class Surface { } bool _presentSurface() { - canvasKit.callMethod('setCurrentContext', [_surface!.context]); + canvasKit.callMethod('setCurrentContext', [_surface!.context]); _surface!.getCanvas().flush(); return true; } @@ -159,8 +159,16 @@ class CkSurface { int height() => _surface.callMethod('height'); void dispose() { + if (_isDisposed) { + return; + } + // Only resources from the current context can be disposed. + canvasKit.callMethod('setCurrentContext', [_glContext]); _surface.callMethod('dispose'); _grContext.callMethod('releaseResourcesAndAbandonContext'); _grContext.callMethod('delete'); + _isDisposed = true; } + + bool _isDisposed = false; }