From 68bb09e4ec7e228c7ed31045b799a2d7cb1d6300 Mon Sep 17 00:00:00 2001 From: Matej Knopp Date: Tue, 16 Feb 2021 21:56:01 +0100 Subject: [PATCH] Unblock FlutterResizeSynchronizer on engine shutdown (flutter/engine#24264) --- .../macos/framework/Source/FlutterEngine.mm | 4 ++++ .../Source/FlutterResizeSynchronizer.h | 5 +++++ .../Source/FlutterResizeSynchronizer.mm | 20 ++++++++++++++----- .../macos/framework/Source/FlutterView.h | 6 ++++++ .../macos/framework/Source/FlutterView.mm | 4 ++++ 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm index 626197ed1e..cc7b84703e 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm @@ -489,6 +489,10 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FlutterEngi 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/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h index 72e2a29bba..4a621c4170 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h +++ b/engine/src/flutter/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/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.mm index 84d560f4ea..82ea1cc8a2 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.mm @@ -34,6 +34,9 @@ // Target size for resizing. CGSize _newSize; + // if YES prevents all synchronization + BOOL _shuttingDown; + __weak id _delegate; } @end @@ -54,7 +57,7 @@ return; } - if (!_receivedFirstFrame) { + if (!_receivedFirstFrame || _shuttingDown) { // No blocking until framework produces at least one frame notify(); return; @@ -77,7 +80,7 @@ _waiting = YES; - _condBlockRequestCommit.wait(lock, [&] { return _pendingCommit; }); + _condBlockRequestCommit.wait(lock, [&] { return _pendingCommit || _shuttingDown; }); [_delegate resizeSynchronizerFlush:self]; [_delegate resizeSynchronizerCommit:self]; @@ -104,7 +107,7 @@ - (void)requestCommit { std::unique_lock lock(_mutex); - if (!_acceptingCommit) { + if (!_acceptingCommit || _shuttingDown) { return; } @@ -113,7 +116,7 @@ _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 @@ _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/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h index 6dc7d87063..a3283b2f87 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h +++ b/engine/src/flutter/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/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterView.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterView.mm index 9043b72769..6a36f9b7d9 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterView.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterView.mm @@ -124,4 +124,8 @@ [_reshapeListener viewDidReshape:self]; } +- (void)shutdown { + [_resizeSynchronizer shutdown]; +} + @end