Skip to content

Commit

Permalink
macOS: Fix SDL_GL_CreateContext/MakeCurrent on non-main threads causi…
Browse files Browse the repository at this point in the history
…ng a Main Thread Checker warning when built with Xcode 11 / the macOS 10.15 SDK.

Fixes bug #4714.
  • Loading branch information
slime73 committed Jul 13, 2019
1 parent 73536d9 commit 8fb8adf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/video/cocoa/SDL_cocoaopengl.m
Expand Up @@ -95,15 +95,27 @@ - (void)setWindow:(SDL_Window *)newWindow

if (newWindow) {
SDL_WindowData *windowdata = (SDL_WindowData *)newWindow->driverdata;
NSView *contentview = windowdata->sdlContentView;

/* This should never be nil since sdlContentView is only nil if the
window was created via SDL_CreateWindowFrom, and SDL doesn't allow
OpenGL contexts to be created in that case. However, it doesn't hurt
to check. */
if (contentview == nil) {
/* Prefer to access the cached content view above instead of this,
since as of Xcode 11 + SDK 10.15, [window contentView] causes
Apple's Main Thread Checker to output a warning. */
contentview = [windowdata->nswindow contentView];
}

/* Now sign up for scheduled updates for the new window. */
NSMutableArray *contexts = windowdata->nscontexts;
@synchronized (contexts) {
[contexts addObject:self];
}

if ([self view] != [windowdata->nswindow contentView]) {
[self setView:[windowdata->nswindow contentView]];
if ([self view] != contentview) {
[self setView:contentview];
if (self == [NSOpenGLContext currentContext]) {
[self update];
} else {
Expand Down
1 change: 1 addition & 0 deletions src/video/cocoa/SDL_cocoawindow.h
Expand Up @@ -113,6 +113,7 @@ struct SDL_WindowData
{
SDL_Window *window;
NSWindow *nswindow;
NSView *sdlContentView; /* nil if window is created via CreateWindowFrom */
NSMutableArray *nscontexts;
SDL_bool created;
SDL_bool inWindowMove;
Expand Down
5 changes: 5 additions & 0 deletions src/video/cocoa/SDL_cocoawindow.m
Expand Up @@ -1307,6 +1307,11 @@ - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
data->videodata = videodata;
data->nscontexts = [[NSMutableArray alloc] init];

/* Only store this for windows created by us since the content view might
* get replaced from under us otherwise, and we only need it when the
* window is guaranteed to be created by us (OpenGL contexts). */
data->sdlContentView = created ? [nswindow contentView] : nil;

/* Create an event listener for the window */
data->listener = [[Cocoa_WindowListener alloc] init];

Expand Down

0 comments on commit 8fb8adf

Please sign in to comment.