macOS: Fix SDL_GL_CreateContext/MakeCurrent on non-main threads causing a Main Thread Checker warning when built with Xcode 11 / the macOS 10.15 SDK.
authorAlex Szpakowski <slime73@gmail.com>
Sat, 13 Jul 2019 17:04:02 -0300
changeset 129401e6980ce45c0
parent 12939 68977584a5d9
child 12941 9c848db04e09
macOS: Fix SDL_GL_CreateContext/MakeCurrent on non-main threads causing a Main Thread Checker warning when built with Xcode 11 / the macOS 10.15 SDK.

Fixes bug #4714.
src/video/cocoa/SDL_cocoaopengl.m
src/video/cocoa/SDL_cocoawindow.h
src/video/cocoa/SDL_cocoawindow.m
     1.1 --- a/src/video/cocoa/SDL_cocoaopengl.m	Fri Jul 12 21:28:43 2019 -0400
     1.2 +++ b/src/video/cocoa/SDL_cocoaopengl.m	Sat Jul 13 17:04:02 2019 -0300
     1.3 @@ -95,6 +95,18 @@
     1.4  
     1.5      if (newWindow) {
     1.6          SDL_WindowData *windowdata = (SDL_WindowData *)newWindow->driverdata;
     1.7 +        NSView *contentview = windowdata->sdlContentView;
     1.8 +
     1.9 +        /* This should never be nil since sdlContentView is only nil if the
    1.10 +           window was created via SDL_CreateWindowFrom, and SDL doesn't allow
    1.11 +           OpenGL contexts to be created in that case. However, it doesn't hurt
    1.12 +           to check. */
    1.13 +        if (contentview == nil) {
    1.14 +            /* Prefer to access the cached content view above instead of this,
    1.15 +               since as of Xcode 11 + SDK 10.15, [window contentView] causes
    1.16 +               Apple's Main Thread Checker to output a warning. */
    1.17 +            contentview = [windowdata->nswindow contentView];
    1.18 +        }
    1.19  
    1.20          /* Now sign up for scheduled updates for the new window. */
    1.21          NSMutableArray *contexts = windowdata->nscontexts;
    1.22 @@ -102,8 +114,8 @@
    1.23              [contexts addObject:self];
    1.24          }
    1.25  
    1.26 -        if ([self view] != [windowdata->nswindow contentView]) {
    1.27 -            [self setView:[windowdata->nswindow contentView]];
    1.28 +        if ([self view] != contentview) {
    1.29 +            [self setView:contentview];
    1.30              if (self == [NSOpenGLContext currentContext]) {
    1.31                  [self update];
    1.32              } else {
     2.1 --- a/src/video/cocoa/SDL_cocoawindow.h	Fri Jul 12 21:28:43 2019 -0400
     2.2 +++ b/src/video/cocoa/SDL_cocoawindow.h	Sat Jul 13 17:04:02 2019 -0300
     2.3 @@ -113,6 +113,7 @@
     2.4  {
     2.5      SDL_Window *window;
     2.6      NSWindow *nswindow;
     2.7 +    NSView *sdlContentView; /* nil if window is created via CreateWindowFrom */
     2.8      NSMutableArray *nscontexts;
     2.9      SDL_bool created;
    2.10      SDL_bool inWindowMove;
     3.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Fri Jul 12 21:28:43 2019 -0400
     3.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Sat Jul 13 17:04:02 2019 -0300
     3.3 @@ -1307,6 +1307,11 @@
     3.4      data->videodata = videodata;
     3.5      data->nscontexts = [[NSMutableArray alloc] init];
     3.6  
     3.7 +    /* Only store this for windows created by us since the content view might
     3.8 +     * get replaced from under us otherwise, and we only need it when the
     3.9 +     * window is guaranteed to be created by us (OpenGL contexts). */
    3.10 +    data->sdlContentView = created ? [nswindow contentView] : nil;
    3.11 +
    3.12      /* Create an event listener for the window */
    3.13      data->listener = [[Cocoa_WindowListener alloc] init];
    3.14