Skip to content

Commit

Permalink
[macOS] Move the glContext generation to FlutterOpenGLRenderer (flutt…
Browse files Browse the repository at this point in the history
  • Loading branch information
iskakaushik authored Nov 17, 2020
1 parent e9d426a commit be5cf15
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ - (instancetype)initWithFlutterEngine:(FLUTTER_API_SYMBOL(FlutterEngine))engine

- (void)attachToFlutterView:(FlutterView*)view {
_flutterView = view;
_openGLContext = view.openGLContext;
}

- (bool)makeCurrent {
Expand Down Expand Up @@ -112,6 +111,15 @@ - (NSOpenGLContext*)resourceContext {
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;
Expand Down
13 changes: 4 additions & 9 deletions shell/platform/darwin/macos/framework/Source/FlutterView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 8 additions & 9 deletions shell/platform/darwin/macos/framework/Source/FlutterView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,37 @@ @interface FlutterView () <FlutterResizeSynchronizerDelegate> {
__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;
}
return self;
}

- (void)resizeSynchronizerFlush:(FlutterResizeSynchronizer*)synchronizer {
MacOSGLContextSwitch context_switch(self.openGLContext);
MacOSGLContextSwitch context_switch(_openGLContext);
glFlush();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,13 @@ - (instancetype)initWithProject:(nullable FlutterDartProject*)project {
}

- (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;
}

Expand Down

0 comments on commit be5cf15

Please sign in to comment.