From c108c0af173c31a38eec80ca87666242fda2dc68 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Tue, 22 Oct 2019 14:21:49 -0700 Subject: [PATCH] Avoid accessing the Cocoa view on the GPU or IO task runners. (flutter/engine#13295) The view was being accessed from a background thread so its OpenGL context could be accessed. This tripped thread safety assertions in Cocoa. Now the OpenGL context is stashed in the FlutterEngine instance itself. --- .../macos/framework/Source/FlutterEngine.mm | 48 ++++++++++++------- .../macos/framework/Source/FlutterView.h | 10 ---- .../macos/framework/Source/FlutterView.mm | 8 ---- 3 files changed, 30 insertions(+), 36 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 42044b9c13..a4bdd2e46a 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 @@ -150,6 +150,10 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine, // The context provided to the Flutter engine for resource loading. NSOpenGLContext* _resourceContext; + // The context that is owned by the currently displayed FlutterView. This is stashed in the engine + // so that the view doesn't need to be accessed from a background thread. + NSOpenGLContext* _mainOpenGLContext; + // A mapping of channel names to the registered handlers for those channels. NSMutableDictionary* _messageHandlers; @@ -215,18 +219,26 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine, flutterArguments.platform_message_callback = (FlutterPlatformMessageCallback)OnPlatformMessage; flutterArguments.custom_dart_entrypoint = entrypoint.UTF8String; - FlutterEngineResult result = FlutterEngineRun( + FlutterEngineResult result = FlutterEngineInitialize( FLUTTER_ENGINE_VERSION, &rendererConfig, &flutterArguments, (__bridge void*)(self), &_engine); if (result != kSuccess) { - NSLog(@"Failed to start Flutter engine: error %d", result); + NSLog(@"Failed to initialize Flutter engine: error %d", result); return NO; } + + result = FlutterEngineRunInitialized(_engine); + if (result != kSuccess) { + NSLog(@"Failed to run an initialized engine: error %d", result); + return NO; + } + [self updateWindowMetrics]; return YES; } - (void)setViewController:(FlutterViewController*)controller { _viewController = controller; + _mainOpenGLContext = controller.flutterView.openGLContext; if (!controller && !_allowHeadlessExecution) { [self shutDownEngine]; _resourceContext = nil; @@ -281,33 +293,26 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine, #pragma mark - Private methods - (bool)engineCallbackOnMakeCurrent { - if (!_viewController.flutterView) { + if (!_mainOpenGLContext) { return false; } - [_viewController.flutterView makeCurrentContext]; + [_mainOpenGLContext makeCurrentContext]; return true; } - (bool)engineCallbackOnClearCurrent { - if (!_viewController.flutterView) { - return false; - } [NSOpenGLContext clearCurrentContext]; return true; } - (bool)engineCallbackOnPresent { - if (!_viewController.flutterView) { - return false; + if (!_mainOpenGLContext) { } - [_viewController.flutterView onPresent]; + [_mainOpenGLContext flushBuffer]; return true; } - (bool)engineCallbackOnMakeResourceCurrent { - if (!_viewController.flutterView) { - return false; - } [self.resourceContext makeCurrentContext]; return true; } @@ -344,11 +349,18 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine, * Note: Called from dealloc. Should not use accessors or other methods. */ - (void)shutDownEngine { - if (_engine) { - FlutterEngineResult result = FlutterEngineShutdown(_engine); - if (result != kSuccess) { - NSLog(@"Failed to shut down Flutter engine: error %d", result); - } + if (_engine == nullptr) { + return; + } + + FlutterEngineResult result = FlutterEngineDeinitialize(_engine); + if (result != kSuccess) { + NSLog(@"Could not de-initialize the Flutter engine: error %d", result); + } + + result = FlutterEngineShutdown(_engine); + if (result != kSuccess) { + NSLog(@"Failed to shut down Flutter engine: error %d", result); } _engine = nullptr; } 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 9342e748dc..e8c7ec3bd3 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 @@ -35,14 +35,4 @@ - (nullable instancetype)initWithCoder:(nonnull NSCoder*)coder NS_UNAVAILABLE; - (nonnull instancetype)init NS_UNAVAILABLE; -/** - * Sets this view as the current context object for OpenGL drawing. - */ -- (void)makeCurrentContext; - -/** - * Called when the OpenGL display should be updated. - */ -- (void)onPresent; - @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 5f3b46d194..c255106a41 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 @@ -26,14 +26,6 @@ return self; } -- (void)makeCurrentContext { - [self.openGLContext makeCurrentContext]; -} - -- (void)onPresent { - [self.openGLContext flushBuffer]; -} - #pragma mark - NSView overrides /**