cocoa: Update CVDisplayLink timing when screen changes.

This handles both the window moving to a new display and
changing the current display's refresh rate in System
Preferences

Reference Issue #4918.
This commit is contained in:
Ryan C. Gordon 2022-11-16 11:32:08 -05:00
parent 1fd66cc890
commit 7c760f7f79
4 changed files with 23 additions and 0 deletions

View File

@ -53,6 +53,7 @@ struct SDL_GLDriverData
shareContext:(NSOpenGLContext *)share; shareContext:(NSOpenGLContext *)share;
- (void)scheduleUpdate; - (void)scheduleUpdate;
- (void)updateIfNeeded; - (void)updateIfNeeded;
- (void)movedToNewScreen;
- (void)setWindow:(SDL_Window *)window; - (void)setWindow:(SDL_Window *)window;
- (SDL_Window*)window; - (SDL_Window*)window;
- (void)explicitUpdate; - (void)explicitUpdate;

View File

@ -97,6 +97,13 @@ DisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const
return self; return self;
} }
- (void)movedToNewScreen
{
if (self->displayLink) {
CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(self->displayLink, [self CGLContextObj], [[self pixelFormat] CGLPixelFormatObj]);
}
}
- (void)scheduleUpdate - (void)scheduleUpdate
{ {
SDL_AtomicAdd(&self->dirty, 1); SDL_AtomicAdd(&self->dirty, 1);
@ -483,6 +490,8 @@ Cocoa_GL_SwapWindow(_THIS, SDL_Window * window)
SDL_UnlockMutex(nscontext->swapIntervalMutex); SDL_UnlockMutex(nscontext->swapIntervalMutex);
} }
/*{ static Uint64 prev = 0; const Uint64 now = SDL_GetTicks64(); const unsigned int diff = (unsigned int) (now - prev); prev = now; printf("GLSWAPBUFFERS TICKS %u\n", diff); }*/
/* on 10.14 ("Mojave") and later, this deadlocks if two contexts in two /* on 10.14 ("Mojave") and later, this deadlocks if two contexts in two
threads try to swap at the same time, so put a mutex around it. */ threads try to swap at the same time, so put a mutex around it. */
SDL_LockMutex(videodata.swaplock); SDL_LockMutex(videodata.swaplock);

View File

@ -85,6 +85,7 @@ typedef enum
-(void) windowDidResignKey:(NSNotification *) aNotification; -(void) windowDidResignKey:(NSNotification *) aNotification;
-(void) windowDidChangeBackingProperties:(NSNotification *) aNotification; -(void) windowDidChangeBackingProperties:(NSNotification *) aNotification;
-(void) windowDidChangeScreenProfile:(NSNotification *) aNotification; -(void) windowDidChangeScreenProfile:(NSNotification *) aNotification;
-(void) windowDidChangeScreen:(NSNotification *) aNotification;
-(void) windowWillEnterFullScreen:(NSNotification *) aNotification; -(void) windowWillEnterFullScreen:(NSNotification *) aNotification;
-(void) windowDidEnterFullScreen:(NSNotification *) aNotification; -(void) windowDidEnterFullScreen:(NSNotification *) aNotification;
-(void) windowWillExitFullScreen:(NSNotification *) aNotification; -(void) windowWillExitFullScreen:(NSNotification *) aNotification;

View File

@ -500,6 +500,7 @@ Cocoa_UpdateClipCursor(SDL_Window * window)
[center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:window]; [center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:window];
[center addObserver:self selector:@selector(windowDidChangeBackingProperties:) name:NSWindowDidChangeBackingPropertiesNotification object:window]; [center addObserver:self selector:@selector(windowDidChangeBackingProperties:) name:NSWindowDidChangeBackingPropertiesNotification object:window];
[center addObserver:self selector:@selector(windowDidChangeScreenProfile:) name:NSWindowDidChangeScreenProfileNotification object:window]; [center addObserver:self selector:@selector(windowDidChangeScreenProfile:) name:NSWindowDidChangeScreenProfileNotification object:window];
[center addObserver:self selector:@selector(windowDidChangeScreen:) name:NSWindowDidChangeScreenNotification object:window];
[center addObserver:self selector:@selector(windowWillEnterFullScreen:) name:NSWindowWillEnterFullScreenNotification object:window]; [center addObserver:self selector:@selector(windowWillEnterFullScreen:) name:NSWindowWillEnterFullScreenNotification object:window];
[center addObserver:self selector:@selector(windowDidEnterFullScreen:) name:NSWindowDidEnterFullScreenNotification object:window]; [center addObserver:self selector:@selector(windowDidEnterFullScreen:) name:NSWindowDidEnterFullScreenNotification object:window];
[center addObserver:self selector:@selector(windowWillExitFullScreen:) name:NSWindowWillExitFullScreenNotification object:window]; [center addObserver:self selector:@selector(windowWillExitFullScreen:) name:NSWindowWillExitFullScreenNotification object:window];
@ -632,6 +633,7 @@ Cocoa_UpdateClipCursor(SDL_Window * window)
[center removeObserver:self name:NSWindowDidResignKeyNotification object:window]; [center removeObserver:self name:NSWindowDidResignKeyNotification object:window];
[center removeObserver:self name:NSWindowDidChangeBackingPropertiesNotification object:window]; [center removeObserver:self name:NSWindowDidChangeBackingPropertiesNotification object:window];
[center removeObserver:self name:NSWindowDidChangeScreenProfileNotification object:window]; [center removeObserver:self name:NSWindowDidChangeScreenProfileNotification object:window];
[center removeObserver:self name:NSWindowDidChangeScreenNotification object:window];
[center removeObserver:self name:NSWindowWillEnterFullScreenNotification object:window]; [center removeObserver:self name:NSWindowWillEnterFullScreenNotification object:window];
[center removeObserver:self name:NSWindowDidEnterFullScreenNotification object:window]; [center removeObserver:self name:NSWindowDidEnterFullScreenNotification object:window];
[center removeObserver:self name:NSWindowWillExitFullScreenNotification object:window]; [center removeObserver:self name:NSWindowWillExitFullScreenNotification object:window];
@ -920,6 +922,16 @@ Cocoa_UpdateClipCursor(SDL_Window * window)
SDL_SendWindowEvent(_data.window, SDL_WINDOWEVENT_ICCPROF_CHANGED, 0, 0); SDL_SendWindowEvent(_data.window, SDL_WINDOWEVENT_ICCPROF_CHANGED, 0, 0);
} }
- (void)windowDidChangeScreen:(NSNotification *)aNotification
{
/*printf("WINDOWDIDCHANGESCREEN\n");*/
if (_data && _data.nscontexts) {
for (SDLOpenGLContext *context in _data.nscontexts) {
[context movedToNewScreen];
}
}
}
- (void)windowWillEnterFullScreen:(NSNotification *)aNotification - (void)windowWillEnterFullScreen:(NSNotification *)aNotification
{ {
SDL_Window *window = _data.window; SDL_Window *window = _data.window;