diff --git a/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm b/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm index 626197ed1ec9e..cc7b84703e2d6 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm @@ -489,6 +489,10 @@ - (void)shutDownEngine { return; } + if (_viewController && _viewController.flutterView) { + [_viewController.flutterView shutdown]; + } + FlutterEngineResult result = _embedderAPI.Deinitialize(_engine); if (result != kSuccess) { NSLog(@"Could not de-initialize the Flutter engine: error %d", result); diff --git a/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h b/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h index 72e2a29bbac0b..4a621c4170fce 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h +++ b/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h @@ -75,4 +75,9 @@ */ - (void)requestCommit; +/** + * Called when shutting down. Unblocks everything and prevents any further synchronization. + */ +- (void)shutdown; + @end diff --git a/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.mm b/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.mm index 84d560f4ea11e..82ea1cc8a2b32 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.mm @@ -34,6 +34,9 @@ @interface FlutterResizeSynchronizer () { // Target size for resizing. CGSize _newSize; + // if YES prevents all synchronization + BOOL _shuttingDown; + __weak id _delegate; } @end @@ -54,7 +57,7 @@ - (void)beginResize:(CGSize)size notify:(dispatch_block_t)notify { return; } - if (!_receivedFirstFrame) { + if (!_receivedFirstFrame || _shuttingDown) { // No blocking until framework produces at least one frame notify(); return; @@ -77,7 +80,7 @@ - (void)beginResize:(CGSize)size notify:(dispatch_block_t)notify { _waiting = YES; - _condBlockRequestCommit.wait(lock, [&] { return _pendingCommit; }); + _condBlockRequestCommit.wait(lock, [&] { return _pendingCommit || _shuttingDown; }); [_delegate resizeSynchronizerFlush:self]; [_delegate resizeSynchronizerCommit:self]; @@ -104,7 +107,7 @@ - (BOOL)shouldEnsureSurfaceForSize:(CGSize)size { - (void)requestCommit { std::unique_lock lock(_mutex); - if (!_acceptingCommit) { + if (!_acceptingCommit || _shuttingDown) { return; } @@ -113,7 +116,7 @@ - (void)requestCommit { _pendingCommit = YES; if (_waiting) { // BeginResize is in progress, interrupt it and schedule commit call _condBlockRequestCommit.notify_all(); - _condBlockBeginResize.wait(lock, [&]() { return !_pendingCommit; }); + _condBlockBeginResize.wait(lock, [&]() { return !_pendingCommit || _shuttingDown; }); } else { // No resize, schedule commit on platform thread and wait until either done // or interrupted by incoming BeginResize @@ -128,8 +131,15 @@ - (void)requestCommit { _condBlockBeginResize.notify_all(); } }); - _condBlockBeginResize.wait(lock, [&]() { return !_pendingCommit; }); + _condBlockBeginResize.wait(lock, [&]() { return !_pendingCommit || _shuttingDown; }); } } +- (void)shutdown { + std::unique_lock lock(_mutex); + _shuttingDown = YES; + _condBlockBeginResize.notify_all(); + _condBlockRequestCommit.notify_all(); +} + @end diff --git a/shell/platform/darwin/macos/framework/Source/FlutterView.h b/shell/platform/darwin/macos/framework/Source/FlutterView.h index 6dc7d870633ab..a3283b2f8710f 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterView.h +++ b/shell/platform/darwin/macos/framework/Source/FlutterView.h @@ -56,4 +56,10 @@ */ - (nonnull FlutterRenderBackingStore*)backingStoreForSize:(CGSize)size; +/** + * Must be called when shutting down. Unblocks raster thread and prevents any further + * synchronization. + */ +- (void)shutdown; + @end diff --git a/shell/platform/darwin/macos/framework/Source/FlutterView.mm b/shell/platform/darwin/macos/framework/Source/FlutterView.mm index 9043b72769f8b..6a36f9b7d96ca 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterView.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterView.mm @@ -124,4 +124,8 @@ - (void)viewDidChangeBackingProperties { [_reshapeListener viewDidReshape:self]; } +- (void)shutdown { + [_resizeSynchronizer shutdown]; +} + @end