diff --git a/glfw/cocoa_displaylink.m b/glfw/cocoa_displaylink.m index 81512706d..43386e48e 100644 --- a/glfw/cocoa_displaylink.m +++ b/glfw/cocoa_displaylink.m @@ -5,18 +5,23 @@ * Distributed under terms of the GPL3 license. */ -// CVDisplayLink is deprecated replace with CADisplayLink via [NSScreen displayLink] once base macOS version is 14 +// On macOS 14 and newer CADisplayLink, obtained via [NSScreen displayLinkWithTarget:selector:] +// is used. It fires on the main thread run loop. CVDisplayLink is deprecated +// and is used only as a fallback on older macOS versions. Remove it once base +// macOS version is 14. #pragma clang diagnostic ignored "-Wdeprecated-declarations" #include "internal.h" #include +#include #include #define DISPLAY_LINK_SHUTDOWN_CHECK_INTERVAL s_to_monotonic_t(30ll) #define MAX_NUM_OF_DISPLAYS 256 typedef struct _GLFWDisplayLinkNS { - CVDisplayLinkRef displayLink; + // Either a CADisplayLink or a CVDisplayLinkRef depending on use_ca_display_link() + CFTypeRef displayLink; CGDirectDisplayID displayID; monotonic_t lastRenderFrameRequestedAt, first_unserviced_render_frame_request_at; bool pending_dispatch; @@ -39,6 +44,145 @@ lock_for_entry(_GLFWDisplayLinkNS *entry) { return &displayLinks.locks[index_for_entry(entry)]; } +static bool +use_ca_display_link(void) { + static int ans = -1; + if (ans < 0) { + if (@available(macOS 14.0, *)) ans = 1; + else ans = 0; + } + return ans == 1; +} + +static void _glfwDispatchRenderFrame(void *); + +static void +on_display_link_fired(_GLFWDisplayLinkNS *entry, bool on_main_thread) { + os_unfair_lock *lock = lock_for_entry(entry); + os_unfair_lock_lock(lock); + const bool should_dispatch = entry->first_unserviced_render_frame_request_at && !entry->pending_dispatch; + CGDirectDisplayID displayID = entry->displayID; + if (should_dispatch) entry->pending_dispatch = true; + os_unfair_lock_unlock(lock); + if (should_dispatch) { + if (on_main_thread) _glfwDispatchRenderFrame((void *)(uintptr_t)displayID); + else dispatch_async_f(dispatch_get_main_queue(), (void *)(uintptr_t)displayID, _glfwDispatchRenderFrame); + } +} + +// CADisplayLink retains its target, so use a separate target object that +// holds no references to anything that needs releasing. +API_AVAILABLE(macos(14.0)) +@interface GLFWDisplayLinkTarget : NSObject { + _GLFWDisplayLinkNS *entry; +} +- (instancetype)initWithEntry:(_GLFWDisplayLinkNS *)e; +- (void)displayLinkFired:(CADisplayLink *)sender; +@end + +@implementation GLFWDisplayLinkTarget +- (instancetype)initWithEntry:(_GLFWDisplayLinkNS *)e { + self = [super init]; + if (self) entry = e; + return self; +} + +- (void)displayLinkFired:(CADisplayLink *)sender { + (void)sender; + on_display_link_fired(entry, true); +} +@end + +static CVReturn +displayLinkCallback( + CVDisplayLinkRef displayLink UNUSED, + const CVTimeStamp *now UNUSED, + const CVTimeStamp *outputTime UNUSED, + CVOptionFlags flagsIn UNUSED, + CVOptionFlags *flagsOut UNUSED, + void *userInfo) { + _GLFWDisplayLinkNS *entry = (_GLFWDisplayLinkNS *)userInfo; + if (entry) on_display_link_fired(entry, false); + return kCVReturnSuccess; +} + +static NSScreen * +screen_for_display_id(CGDirectDisplayID displayID) { + for (NSScreen *screen in [NSScreen screens]) { + NSNumber *n = [screen deviceDescription][@"NSScreenNumber"]; + if (n && [n unsignedIntValue] == displayID) return screen; + } + return nil; +} + +// Must be called with the lock for entry held +static void +create_display_link(_GLFWDisplayLinkNS *entry) { + entry->displayLink = NULL; + if (use_ca_display_link()) { + if (@available(macOS 14.0, *)) { + @autoreleasepool { + NSScreen *screen = screen_for_display_id(entry->displayID); + if (!screen) return; + GLFWDisplayLinkTarget *target = [[GLFWDisplayLinkTarget alloc] initWithEntry:entry]; + CADisplayLink *link = [screen displayLinkWithTarget:target selector:@selector(displayLinkFired:)]; + [target release]; + if (!link) return; + link.paused = YES; + // Use common modes so that frames are delivered during live resize and modal loops + [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; + entry->displayLink = (CFTypeRef)[link retain]; + } + } + } else { + CVDisplayLinkRef link = NULL; + CVDisplayLinkCreateWithCGDisplay(entry->displayID, &link); + if (link) { + CVDisplayLinkSetOutputCallback(link, &displayLinkCallback, entry); + entry->displayLink = link; + } + } +} + +static void +link_start(CFTypeRef link) { + if (use_ca_display_link()) { + if (@available(macOS 14.0, *)) ((CADisplayLink *)link).paused = NO; + } else CVDisplayLinkStart((CVDisplayLinkRef)link); +} + +static void +link_stop(CFTypeRef link) { + if (use_ca_display_link()) { + if (@available(macOS 14.0, *)) ((CADisplayLink *)link).paused = YES; + } else CVDisplayLinkStop((CVDisplayLinkRef)link); +} + +static bool +link_is_running(CFTypeRef link) { + if (use_ca_display_link()) { + if (@available(macOS 14.0, *)) return !((CADisplayLink *)link).paused; + return false; + } + return CVDisplayLinkIsRunning((CVDisplayLinkRef)link); +} + +static void +link_retain(CFTypeRef link) { CFRetain(link); } + +static void +link_release(CFTypeRef link) { CFRelease(link); } + +// Stop the link and release the reference owned by an entry +static void +link_destroy(CFTypeRef link) { + if (use_ca_display_link()) { + // invalidate removes the link from the run loop and releases its target + if (@available(macOS 14.0, *)) [(CADisplayLink *)link invalidate]; + } else CVDisplayLinkStop((CVDisplayLinkRef)link); + link_release(link); +} + static CGDirectDisplayID displayIDForWindow(_GLFWwindow *w) { NSWindow *nw = w->ns.object; @@ -54,50 +198,18 @@ _glfwClearDisplayLinks(void) { _GLFWDisplayLinkNS *entry = &displayLinks.entries[i]; os_unfair_lock *lock = &displayLinks.locks[i]; os_unfair_lock_lock(lock); - CVDisplayLinkRef link = entry->displayLink; + CFTypeRef link = entry->displayLink; entry->displayLink = NULL; entry->displayID = (CGDirectDisplayID)0; entry->lastRenderFrameRequestedAt = 0; entry->first_unserviced_render_frame_request_at = 0; entry->pending_dispatch = false; os_unfair_lock_unlock(lock); - if (link) { - CVDisplayLinkStop(link); - CVDisplayLinkRelease(link); - } + if (link) link_destroy(link); } displayLinks.count = 0; } -static void _glfwDispatchRenderFrame(void *); - -static CVReturn -displayLinkCallback( - CVDisplayLinkRef displayLink UNUSED, - const CVTimeStamp *now UNUSED, - const CVTimeStamp *outputTime UNUSED, - CVOptionFlags flagsIn UNUSED, - CVOptionFlags *flagsOut UNUSED, - void *userInfo) { - _GLFWDisplayLinkNS *entry = (_GLFWDisplayLinkNS *)userInfo; - if (entry) { - os_unfair_lock *lock = lock_for_entry(entry); - os_unfair_lock_lock(lock); - const bool should_dispatch = entry->first_unserviced_render_frame_request_at && !entry->pending_dispatch; - CGDirectDisplayID displayID = entry->displayID; - if (should_dispatch) entry->pending_dispatch = true; - os_unfair_lock_unlock(lock); - if (should_dispatch) dispatch_async_f(dispatch_get_main_queue(), (void *)(uintptr_t)displayID, _glfwDispatchRenderFrame); - } - return kCVReturnSuccess; -} - -static void -_glfw_create_cv_display_link(_GLFWDisplayLinkNS *entry) { - CVDisplayLinkCreateWithCGDisplay(entry->displayID, &entry->displayLink); - if (entry->displayLink) CVDisplayLinkSetOutputCallback(entry->displayLink, &displayLinkCallback, entry); -} - unsigned _glfwCreateDisplayLink(CGDirectDisplayID displayID) { for (unsigned i = 0; i < displayLinks.count; i++) { @@ -122,7 +234,7 @@ _glfwCreateDisplayLink(CGDirectDisplayID displayID) { memset(entry, 0, sizeof(entry[0])); entry->displayID = displayID; displayLinks.count++; - _glfw_create_cv_display_link(entry); + create_display_link(entry); os_unfair_lock_unlock(lock); return idx; } @@ -130,21 +242,21 @@ _glfwCreateDisplayLink(CGDirectDisplayID displayID) { static unsigned long long display_link_shutdown_timer = 0; static void -_glfwShutdownCVDisplayLink(unsigned long long timer_id UNUSED, void *user_data UNUSED) { +_glfwShutdownDisplayLinks(unsigned long long timer_id UNUSED, void *user_data UNUSED) { display_link_shutdown_timer = 0; for (size_t i = 0; i < displayLinks.count; i++) { _GLFWDisplayLinkNS *dl = &displayLinks.entries[i]; os_unfair_lock *lock = &displayLinks.locks[i]; os_unfair_lock_lock(lock); - CVDisplayLinkRef link = dl->displayLink; - if (link) CVDisplayLinkRetain(link); + CFTypeRef link = dl->displayLink; + if (link) link_retain(link); dl->lastRenderFrameRequestedAt = 0; dl->first_unserviced_render_frame_request_at = 0; dl->pending_dispatch = false; os_unfair_lock_unlock(lock); if (link) { - CVDisplayLinkStop(link); - CVDisplayLinkRelease(link); + link_stop(link); + link_release(link); } } } @@ -155,7 +267,7 @@ _glfwRequestRenderFrame(_GLFWwindow *w) { if (display_link_shutdown_timer) { _glfwPlatformUpdateTimer(display_link_shutdown_timer, DISPLAY_LINK_SHUTDOWN_CHECK_INTERVAL, true); } else { - display_link_shutdown_timer = _glfwPlatformAddTimer(DISPLAY_LINK_SHUTDOWN_CHECK_INTERVAL, false, _glfwShutdownCVDisplayLink, NULL, NULL); + display_link_shutdown_timer = _glfwPlatformAddTimer(DISPLAY_LINK_SHUTDOWN_CHECK_INTERVAL, false, _glfwShutdownDisplayLinks, NULL, NULL); } monotonic_t now = glfwGetTime(); bool found_display_link = false; @@ -164,11 +276,17 @@ _glfwRequestRenderFrame(_GLFWwindow *w) { dl = &displayLinks.entries[i]; bool need_start = false, need_stop = false, need_recreate = false; bool retain_link = false; - CVDisplayLinkRef link = NULL; - CVDisplayLinkRef new_link = NULL; + CFTypeRef link = NULL; + CFTypeRef new_link = NULL; bool new_link_retained = false; os_unfair_lock *lock = &displayLinks.locks[i]; os_unfair_lock_lock(lock); + if (dl->displayID == displayID && !dl->displayLink && use_ca_display_link()) { + // NSScreen may not have been available when the link was first + // created, for example, if AppKit had not yet caught up with a + // display configuration change, so try again. + create_display_link(dl); + } link = dl->displayLink; if (dl->displayID == displayID) { found_display_link = true; @@ -180,7 +298,7 @@ _glfwRequestRenderFrame(_GLFWwindow *w) { } dl->pending_dispatch = false; if (link) { - if (!CVDisplayLinkIsRunning(link)) { + if (!link_is_running(link)) { need_start = true; retain_link = true; } else if (now - first_unserviced > s_to_monotonic_t(1ll)) { @@ -196,29 +314,28 @@ _glfwRequestRenderFrame(_GLFWwindow *w) { dl->first_unserviced_render_frame_request_at = 0; dl->pending_dispatch = false; } - if (retain_link && link) CVDisplayLinkRetain(link); + if (retain_link && link) link_retain(link); os_unfair_lock_unlock(lock); if (need_recreate && link) { - CVDisplayLinkStop(link); - CVDisplayLinkRelease(link); + link_destroy(link); os_unfair_lock_lock(lock); - _glfw_create_cv_display_link(dl); + create_display_link(dl); new_link = dl->displayLink; if (new_link) { - CVDisplayLinkRetain(new_link); + link_retain(new_link); new_link_retained = true; } dl->pending_dispatch = false; os_unfair_lock_unlock(lock); if (new_link) { - if (!CVDisplayLinkIsRunning(new_link)) CVDisplayLinkStart(new_link); - if (new_link_retained) CVDisplayLinkRelease(new_link); + if (!link_is_running(new_link)) link_start(new_link); + if (new_link_retained) link_release(new_link); } - _glfwInputError(GLFW_PLATFORM_ERROR, "CVDisplayLink stuck possibly because of sleep/screensaver + Apple's incompetence, recreating."); + _glfwInputError(GLFW_PLATFORM_ERROR, "Display link stuck possibly because of sleep/screensaver + Apple's incompetence, recreating."); } else { - if (need_start && link) CVDisplayLinkStart(link); - else if (need_stop && link) CVDisplayLinkStop(link); - if (retain_link && link) CVDisplayLinkRelease(link); + if (need_start && link) link_start(link); + else if (need_stop && link) link_stop(link); + if (retain_link && link) link_release(link); } } if (!found_display_link) { @@ -230,12 +347,12 @@ _glfwRequestRenderFrame(_GLFWwindow *w) { dl->lastRenderFrameRequestedAt = now; dl->first_unserviced_render_frame_request_at = now; dl->pending_dispatch = false; - CVDisplayLinkRef link = dl->displayLink; - if (link) CVDisplayLinkRetain(link); + CFTypeRef link = dl->displayLink; + if (link) link_retain(link); os_unfair_lock_unlock(lock); if (link) { - if (!CVDisplayLinkIsRunning(link)) CVDisplayLinkStart(link); - CVDisplayLinkRelease(link); + if (!link_is_running(link)) link_start(link); + link_release(link); } } } @@ -255,7 +372,7 @@ _glfwDispatchRenderFrame(void *passed_in_data) { for (size_t i = 0; i < displayLinks.count; i++) { _GLFWDisplayLinkNS *dl = &displayLinks.entries[i]; bool need_stop = false; - CVDisplayLinkRef link = NULL; + CFTypeRef link = NULL; os_unfair_lock *lock = &displayLinks.locks[i]; os_unfair_lock_lock(lock); if (dl->displayID == displayID) { @@ -271,16 +388,16 @@ _glfwDispatchRenderFrame(void *passed_in_data) { window = window->next; } link = dl->displayLink; - if (!any_pending_request && link && CVDisplayLinkIsRunning(link)) { + if (!any_pending_request && link && link_is_running(link)) { need_stop = true; - CVDisplayLinkRetain(link); + link_retain(link); dl->lastRenderFrameRequestedAt = 0; } } os_unfair_lock_unlock(lock); if (need_stop && link) { - CVDisplayLinkStop(link); - CVDisplayLinkRelease(link); + link_stop(link); + link_release(link); } } } diff --git a/glfw/cocoa_window.m b/glfw/cocoa_window.m index 9995e4950..b5c00f1b4 100644 --- a/glfw/cocoa_window.m +++ b/glfw/cocoa_window.m @@ -777,7 +777,7 @@ static void flush_focus_loss_text(_GLFWwindow *window); - (void)windowDidChangeScreen:(NSNotification *)notification { (void)notification; if (window->ns.renderFrameRequested && window->ns.renderFrameCallback) { - // Ensure that if the window changed its monitor, CVDisplayLink + // Ensure that if the window changed its monitor, the display link // is running for the new monitor requestRenderFrame(window, window->ns.renderFrameCallback); } diff --git a/setup.py b/setup.py index 5c1dd5d5c..dfcbe9256 100755 --- a/setup.py +++ b/setup.py @@ -1247,7 +1247,7 @@ def glfw_init_env( elif module == 'cocoa': ans.cppflags.append('-DGL_SILENCE_DEPRECATION') - for f_ in 'Cocoa IOKit CoreFoundation CoreVideo UniformTypeIdentifiers'.split(): + for f_ in 'Cocoa IOKit CoreFoundation CoreVideo QuartzCore UniformTypeIdentifiers'.split(): ans.ldpaths.extend(('-framework', f_)) elif module == 'wayland':