[macOS] Move the glContext generation to FlutterOpenGLRenderer (#22572)

This commit is contained in:
Kaushik Iska 2020-11-17 17:08:01 -06:00 committed by GitHub
parent e9d426aef7
commit be5cf151f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 24 deletions

View File

@ -23,6 +23,11 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property(nonatomic, readonly, nullable) NSOpenGLContext* resourceContext;
/**
* The main OpenGL which will be used for rendering contents to the FlutterView.
*/
@property(readwrite, nonatomic, nonnull) NSOpenGLContext* openGLContext;
/**
* Intializes the renderer with the given FlutterEngine.
*/

View File

@ -72,7 +72,6 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine,
- (void)attachToFlutterView:(FlutterView*)view {
_flutterView = view;
_openGLContext = view.openGLContext;
}
- (bool)makeCurrent {
@ -112,6 +111,15 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine,
return _resourceContext;
}
- (NSOpenGLContext*)openGLContext {
if (!_openGLContext) {
NSOpenGLContext* shareContext = [self resourceContext];
_openGLContext = [[NSOpenGLContext alloc] initWithFormat:shareContext.pixelFormat
shareContext:shareContext];
}
return _openGLContext;
}
- (bool)makeResourceCurrent {
[self.resourceContext makeCurrentContext];
return true;

View File

@ -20,19 +20,14 @@
*/
@interface FlutterView : NSView
/**
* The OpenGL context of backing surface.
*/
@property(readwrite, nonatomic, nonnull) NSOpenGLContext* openGLContext;
- (nullable instancetype)initWithFrame:(NSRect)frame
shareContext:(nonnull NSOpenGLContext*)shareContext
mainContext:(nonnull NSOpenGLContext*)mainContext
reshapeListener:(nonnull id<FlutterViewReshapeListener>)reshapeListener
NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithShareContext:(nonnull NSOpenGLContext*)shareContext
reshapeListener:
(nonnull id<FlutterViewReshapeListener>)reshapeListener;
- (nullable instancetype)initWithMainContext:(nonnull NSOpenGLContext*)mainContext
reshapeListener:
(nonnull id<FlutterViewReshapeListener>)reshapeListener;
- (nullable instancetype)initWithFrame:(NSRect)frameRect
pixelFormat:(nullable NSOpenGLPixelFormat*)format NS_UNAVAILABLE;

View File

@ -15,30 +15,29 @@
__weak id<FlutterViewReshapeListener> _reshapeListener;
FlutterResizeSynchronizer* _resizeSynchronizer;
FlutterSurfaceManager* _surfaceManager;
NSOpenGLContext* _openGLContext;
}
@end
@implementation FlutterView
- (instancetype)initWithShareContext:(NSOpenGLContext*)shareContext
reshapeListener:(id<FlutterViewReshapeListener>)reshapeListener {
return [self initWithFrame:NSZeroRect shareContext:shareContext reshapeListener:reshapeListener];
- (instancetype)initWithMainContext:(NSOpenGLContext*)mainContext
reshapeListener:(id<FlutterViewReshapeListener>)reshapeListener {
return [self initWithFrame:NSZeroRect mainContext:mainContext reshapeListener:reshapeListener];
}
- (instancetype)initWithFrame:(NSRect)frame
shareContext:(NSOpenGLContext*)shareContext
mainContext:(NSOpenGLContext*)mainContext
reshapeListener:(id<FlutterViewReshapeListener>)reshapeListener {
self = [super initWithFrame:frame];
if (self) {
self.openGLContext = [[NSOpenGLContext alloc] initWithFormat:shareContext.pixelFormat
shareContext:shareContext];
_openGLContext = mainContext;
[self setWantsLayer:YES];
_resizeSynchronizer = [[FlutterResizeSynchronizer alloc] initWithDelegate:self];
_surfaceManager = [[FlutterSurfaceManager alloc] initWithLayer:self.layer
openGLContext:self.openGLContext];
openGLContext:_openGLContext];
_reshapeListener = reshapeListener;
}
@ -46,7 +45,7 @@
}
- (void)resizeSynchronizerFlush:(FlutterResizeSynchronizer*)synchronizer {
MacOSGLContextSwitch context_switch(self.openGLContext);
MacOSGLContextSwitch context_switch(_openGLContext);
glFlush();
}

View File

@ -239,13 +239,13 @@ static void CommonInit(FlutterViewController* controller) {
}
- (void)loadView {
NSOpenGLContext* resourceContext = _engine.openGLRenderer.resourceContext;
if (!resourceContext) {
NSLog(@"Unable to create FlutterView; no resource context available.");
NSOpenGLContext* mainContext = _engine.openGLRenderer.openGLContext;
if (!mainContext) {
NSLog(@"Unable to create FlutterView; no GL context available.");
return;
}
FlutterView* flutterView = [[FlutterView alloc] initWithShareContext:resourceContext
reshapeListener:self];
FlutterView* flutterView = [[FlutterView alloc] initWithMainContext:mainContext
reshapeListener:self];
self.view = flutterView;
}