app/internal/window: [macOS,iOS] reduce display link starting and stopping

Recent changes to the macOS threading exposed a problem where a
window's display link may fail to start after being started and stopped
in rapid succession.

Introduce a displayLink type that waits a while after the last stop
request before stopping its display link. That seems to be the way
other projects are using display links.

As a bonus, the new implementation avoids the potentially expensive
overhead of frequent starting and stopping the underlying OS thread.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-05-29 19:01:17 +02:00
parent 3817941175
commit 1377bea3cd
6 changed files with 218 additions and 103 deletions
+29 -1
View File
@@ -31,7 +31,7 @@
- (void)windowDidChangeScreen:(NSNotification *)notification {
CGDirectDisplayID dispID = [[[self.window screen] deviceDescription][@"NSScreenNumber"] unsignedIntValue];
CFTypeRef view = (__bridge CFTypeRef)self.window.contentView;
gio_updateDisplayLink(view, dispID);
gio_onChangeScreen(view, dispID);
}
- (void)windowDidBecomeKey:(NSNotification *)notification {
gio_onFocus((__bridge CFTypeRef)self.window.contentView, YES);
@@ -81,6 +81,34 @@ CGFloat gio_getViewBackingScale(CFTypeRef viewRef) {
return [view.window backingScaleFactor];
}
static CVReturn displayLinkCallback(CVDisplayLinkRef dl, const CVTimeStamp *inNow, const CVTimeStamp *inOutputTime, CVOptionFlags flagsIn, CVOptionFlags *flagsOut, void *displayLinkContext) {
gio_onFrameCallback(dl);
return kCVReturnSuccess;
}
CFTypeRef gio_createDisplayLink(void) {
CVDisplayLinkRef dl;
CVDisplayLinkCreateWithActiveCGDisplays(&dl);
CVDisplayLinkSetOutputCallback(dl, displayLinkCallback, nil);
return dl;
}
int gio_startDisplayLink(CFTypeRef dl) {
return CVDisplayLinkStart((CVDisplayLinkRef)dl);
}
int gio_stopDisplayLink(CFTypeRef dl) {
return CVDisplayLinkStop((CVDisplayLinkRef)dl);
}
void gio_releaseDisplayLink(CFTypeRef dl) {
CVDisplayLinkRelease((CVDisplayLinkRef)dl);
}
void gio_setDisplayLinkDisplay(CFTypeRef dl, uint64_t did) {
CVDisplayLinkSetCurrentCGDisplay((CVDisplayLinkRef)dl, (CGDirectDisplayID)did);
}
void gio_main(CFTypeRef viewRef, const char *title, CGFloat width, CGFloat height) {
@autoreleasepool {
NSView *view = (NSView *)CFBridgingRelease(viewRef);