Skip to content

Commit

Permalink
cocoa: OpenGL setView and update must be used on main thread (thanks,…
Browse files Browse the repository at this point in the history
… Tim!).

If called from background threads, use Grand Central Dispatch to use the
main thread instead. On the main thread, just call them directly.

Fixes Bugzilla #4932.
  • Loading branch information
icculus committed Apr 7, 2020
1 parent 486f0b6 commit 309d613
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/video/cocoa/SDL_cocoaopengl.h
Expand Up @@ -49,6 +49,8 @@ struct SDL_GLDriverData
- (void)scheduleUpdate;
- (void)updateIfNeeded;
- (void)setWindow:(SDL_Window *)window;
- (SDL_Window*)window;
- (void)explicitUpdate;

@end

Expand Down
38 changes: 27 additions & 11 deletions src/video/cocoa/SDL_cocoaopengl.m
Expand Up @@ -63,14 +63,10 @@ - (void)scheduleUpdate
/* This should only be called on the thread on which a user is using the context. */
- (void)updateIfNeeded
{
int value = SDL_AtomicSet(&self->dirty, 0);
const int value = SDL_AtomicSet(&self->dirty, 0);
if (value > 0) {
/* We call the real underlying update here, since -[SDLOpenGLContext update] just calls us. */
if ([NSThread isMainThread]) {
[super update];
} else {
[super performSelectorOnMainThread:@selector(update) withObject:nil waitUntilDone:NO];
}
[self explicitUpdate];
}
}

Expand Down Expand Up @@ -119,23 +115,41 @@ - (void)setWindow:(SDL_Window *)newWindow
}

if ([self view] != contentview) {
[self setView:contentview];
if ([NSThread isMainThread]) {
[self setView:contentview];
} else {
dispatch_sync(dispatch_get_main_queue(), ^{ [self setView:contentview]; });
}
if (self == [NSOpenGLContext currentContext]) {
[self update];
[self explicitUpdate];
} else {
[self scheduleUpdate];
}
}
} else {
[self clearDrawable];
if (self == [NSOpenGLContext currentContext]) {
[self update];
[self explicitUpdate];
} else {
[self scheduleUpdate];
}
}
}

- (SDL_Window*)window
{
return self->window;
}

- (void)explicitUpdate
{
if ([NSThread isMainThread]) {
[super update];
} else {
dispatch_sync(dispatch_get_main_queue(), ^{ [super update]; });
}
}

@end


Expand Down Expand Up @@ -352,8 +366,10 @@ - (void)setWindow:(SDL_Window *)newWindow
{
if (context) {
SDLOpenGLContext *nscontext = (SDLOpenGLContext *)context;
[nscontext setWindow:window];
[nscontext updateIfNeeded];
if ([nscontext window] != window) {
[nscontext setWindow:window];
[nscontext updateIfNeeded];
}
[nscontext makeCurrentContext];
} else {
[NSOpenGLContext clearCurrentContext];
Expand Down

0 comments on commit 309d613

Please sign in to comment.