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.
This commit is contained in:
Chinmay Garde
2019-10-22 14:21:49 -07:00
committed by GitHub
parent f99f00b0e3
commit c108c0af17
3 changed files with 30 additions and 36 deletions

View File

@@ -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<NSString*, FlutterBinaryMessageHandler>* _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;
}

View File

@@ -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

View File

@@ -26,14 +26,6 @@
return self;
}
- (void)makeCurrentContext {
[self.openGLContext makeCurrentContext];
}
- (void)onPresent {
[self.openGLContext flushBuffer];
}
#pragma mark - NSView overrides
/**