Skip to content

Commit be5cf15

Browse files
authored
[macOS] Move the glContext generation to FlutterOpenGLRenderer (flutter#22572)
1 parent e9d426a commit be5cf15

File tree

5 files changed

+31
-24
lines changed

5 files changed

+31
-24
lines changed

shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ NS_ASSUME_NONNULL_BEGIN
2323
*/
2424
@property(nonatomic, readonly, nullable) NSOpenGLContext* resourceContext;
2525

26+
/**
27+
* The main OpenGL which will be used for rendering contents to the FlutterView.
28+
*/
29+
@property(readwrite, nonatomic, nonnull) NSOpenGLContext* openGLContext;
30+
2631
/**
2732
* Intializes the renderer with the given FlutterEngine.
2833
*/

shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.mm

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ - (instancetype)initWithFlutterEngine:(FLUTTER_API_SYMBOL(FlutterEngine))engine
7272

7373
- (void)attachToFlutterView:(FlutterView*)view {
7474
_flutterView = view;
75-
_openGLContext = view.openGLContext;
7675
}
7776

7877
- (bool)makeCurrent {
@@ -112,6 +111,15 @@ - (NSOpenGLContext*)resourceContext {
112111
return _resourceContext;
113112
}
114113

114+
- (NSOpenGLContext*)openGLContext {
115+
if (!_openGLContext) {
116+
NSOpenGLContext* shareContext = [self resourceContext];
117+
_openGLContext = [[NSOpenGLContext alloc] initWithFormat:shareContext.pixelFormat
118+
shareContext:shareContext];
119+
}
120+
return _openGLContext;
121+
}
122+
115123
- (bool)makeResourceCurrent {
116124
[self.resourceContext makeCurrentContext];
117125
return true;

shell/platform/darwin/macos/framework/Source/FlutterView.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,14 @@
2020
*/
2121
@interface FlutterView : NSView
2222

23-
/**
24-
* The OpenGL context of backing surface.
25-
*/
26-
@property(readwrite, nonatomic, nonnull) NSOpenGLContext* openGLContext;
27-
2823
- (nullable instancetype)initWithFrame:(NSRect)frame
29-
shareContext:(nonnull NSOpenGLContext*)shareContext
24+
mainContext:(nonnull NSOpenGLContext*)mainContext
3025
reshapeListener:(nonnull id<FlutterViewReshapeListener>)reshapeListener
3126
NS_DESIGNATED_INITIALIZER;
3227

33-
- (nullable instancetype)initWithShareContext:(nonnull NSOpenGLContext*)shareContext
34-
reshapeListener:
35-
(nonnull id<FlutterViewReshapeListener>)reshapeListener;
28+
- (nullable instancetype)initWithMainContext:(nonnull NSOpenGLContext*)mainContext
29+
reshapeListener:
30+
(nonnull id<FlutterViewReshapeListener>)reshapeListener;
3631

3732
- (nullable instancetype)initWithFrame:(NSRect)frameRect
3833
pixelFormat:(nullable NSOpenGLPixelFormat*)format NS_UNAVAILABLE;

shell/platform/darwin/macos/framework/Source/FlutterView.mm

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,37 @@ @interface FlutterView () <FlutterResizeSynchronizerDelegate> {
1515
__weak id<FlutterViewReshapeListener> _reshapeListener;
1616
FlutterResizeSynchronizer* _resizeSynchronizer;
1717
FlutterSurfaceManager* _surfaceManager;
18+
NSOpenGLContext* _openGLContext;
1819
}
1920

2021
@end
2122

2223
@implementation FlutterView
2324

24-
- (instancetype)initWithShareContext:(NSOpenGLContext*)shareContext
25-
reshapeListener:(id<FlutterViewReshapeListener>)reshapeListener {
26-
return [self initWithFrame:NSZeroRect shareContext:shareContext reshapeListener:reshapeListener];
25+
- (instancetype)initWithMainContext:(NSOpenGLContext*)mainContext
26+
reshapeListener:(id<FlutterViewReshapeListener>)reshapeListener {
27+
return [self initWithFrame:NSZeroRect mainContext:mainContext reshapeListener:reshapeListener];
2728
}
2829

2930
- (instancetype)initWithFrame:(NSRect)frame
30-
shareContext:(NSOpenGLContext*)shareContext
31+
mainContext:(NSOpenGLContext*)mainContext
3132
reshapeListener:(id<FlutterViewReshapeListener>)reshapeListener {
3233
self = [super initWithFrame:frame];
3334
if (self) {
34-
self.openGLContext = [[NSOpenGLContext alloc] initWithFormat:shareContext.pixelFormat
35-
shareContext:shareContext];
36-
35+
_openGLContext = mainContext;
3736
[self setWantsLayer:YES];
3837

3938
_resizeSynchronizer = [[FlutterResizeSynchronizer alloc] initWithDelegate:self];
4039
_surfaceManager = [[FlutterSurfaceManager alloc] initWithLayer:self.layer
41-
openGLContext:self.openGLContext];
40+
openGLContext:_openGLContext];
4241

4342
_reshapeListener = reshapeListener;
4443
}
4544
return self;
4645
}
4746

4847
- (void)resizeSynchronizerFlush:(FlutterResizeSynchronizer*)synchronizer {
49-
MacOSGLContextSwitch context_switch(self.openGLContext);
48+
MacOSGLContextSwitch context_switch(_openGLContext);
5049
glFlush();
5150
}
5251

shell/platform/darwin/macos/framework/Source/FlutterViewController.mm

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,13 @@ - (instancetype)initWithProject:(nullable FlutterDartProject*)project {
239239
}
240240

241241
- (void)loadView {
242-
NSOpenGLContext* resourceContext = _engine.openGLRenderer.resourceContext;
243-
if (!resourceContext) {
244-
NSLog(@"Unable to create FlutterView; no resource context available.");
242+
NSOpenGLContext* mainContext = _engine.openGLRenderer.openGLContext;
243+
if (!mainContext) {
244+
NSLog(@"Unable to create FlutterView; no GL context available.");
245245
return;
246246
}
247-
FlutterView* flutterView = [[FlutterView alloc] initWithShareContext:resourceContext
248-
reshapeListener:self];
247+
FlutterView* flutterView = [[FlutterView alloc] initWithMainContext:mainContext
248+
reshapeListener:self];
249249
self.view = flutterView;
250250
}
251251

0 commit comments

Comments
 (0)