Browse Source

cocoadisplay: Fix crash with threading-model on newer macOS versions

Updates the context on the main thread instead of the draw thread now. If render_frame happens to run while the context needs updating, it will skip the frame.

Fixes #1286
rdb 3 years ago
parent
commit
4cf8187df7
1 changed files with 31 additions and 2 deletions
  1. 31 2
      panda/src/cocoadisplay/cocoaGraphicsWindow.mm

+ 31 - 2
panda/src/cocoadisplay/cocoaGraphicsWindow.mm

@@ -207,8 +207,13 @@ begin_frame(FrameMode mode, Thread *current_thread) {
 
 
   // Update the context if necessary, to make it reallocate buffers etc.
   // Update the context if necessary, to make it reallocate buffers etc.
   if (_context_needs_update) {
   if (_context_needs_update) {
-    [cocoagsg->_context update];
-    _context_needs_update = false;
+    if ([NSThread isMainThread]) {
+      [cocoagsg->_context update];
+      _context_needs_update = false;
+    } else {
+      cocoagsg->unlock_context();
+      return false;
+    }
   }
   }
 
 
   // Lock the view for drawing.
   // Lock the view for drawing.
@@ -349,6 +354,18 @@ process_events() {
   }
   }
 
 
   [pool release];
   [pool release];
+
+  if (_context_needs_update && _gsg != nullptr) {
+    CocoaGraphicsStateGuardian *cocoagsg;
+    DCAST_INTO_V(cocoagsg, _gsg);
+
+    if (cocoagsg != nullptr && cocoagsg->_context != nil) {
+      cocoagsg->lock_context();
+      _context_needs_update = false;
+      [cocoagsg->_context update];
+      cocoagsg->unlock_context();
+    }
+  }
 }
 }
 
 
 /**
 /**
@@ -1194,6 +1211,18 @@ set_properties_now(WindowProperties &properties) {
       break;
       break;
     }
     }
   }
   }
+
+  if (_context_needs_update && _gsg != nullptr) {
+    CocoaGraphicsStateGuardian *cocoagsg;
+    DCAST_INTO_V(cocoagsg, _gsg);
+
+    if (cocoagsg != nullptr && cocoagsg->_context != nil) {
+      cocoagsg->lock_context();
+      _context_needs_update = false;
+      [cocoagsg->_context update];
+      cocoagsg->unlock_context();
+    }
+  }
 }
 }
 
 
 /**
 /**