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

diff --git a/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.h b/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.h
index 7d9c19e..86990f1 100644
--- a/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.h
+++ b/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.h
@@ -24,6 +24,11 @@
 @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.
  */
 - (instancetype)initWithFlutterEngine:(FLUTTER_API_SYMBOL(FlutterEngine))engine;
diff --git a/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.mm b/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.mm
index 8ae9a2f..3893d17 100644
--- a/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.mm
+++ b/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.mm
@@ -72,7 +72,6 @@
 
 - (void)attachToFlutterView:(FlutterView*)view {
   _flutterView = view;
-  _openGLContext = view.openGLContext;
 }
 
 - (bool)makeCurrent {
@@ -112,6 +111,15 @@
   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;
diff --git a/shell/platform/darwin/macos/framework/Source/FlutterView.h b/shell/platform/darwin/macos/framework/Source/FlutterView.h
index cd96f58..611c57b 100644
--- a/shell/platform/darwin/macos/framework/Source/FlutterView.h
+++ b/shell/platform/darwin/macos/framework/Source/FlutterView.h
@@ -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;
diff --git a/shell/platform/darwin/macos/framework/Source/FlutterView.mm b/shell/platform/darwin/macos/framework/Source/FlutterView.mm
index d6ce889..978e09a 100644
--- a/shell/platform/darwin/macos/framework/Source/FlutterView.mm
+++ b/shell/platform/darwin/macos/framework/Source/FlutterView.mm
@@ -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();
 }
 
diff --git a/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm b/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm
index 8fce640..27f197e 100644
--- a/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm
+++ b/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm
@@ -239,13 +239,13 @@
 }
 
 - (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;
 }