|
| 1 | +/* |
| 2 | + * cocoa_displaylink.m |
| 3 | + * Copyright (C) 2024 Kovid Goyal <kovid at kovidgoyal.net> |
| 4 | + * |
| 5 | + * Distributed under terms of the GPL3 license. |
| 6 | + */ |
| 7 | + |
| 8 | +// CVDisplayLink is deprecated |
| 9 | +#pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 10 | + |
| 11 | +#include "internal.h" |
| 12 | +#include <CoreVideo/CVDisplayLink.h> |
| 13 | + |
| 14 | +#define DISPLAY_LINK_SHUTDOWN_CHECK_INTERVAL s_to_monotonic_t(30ll) |
| 15 | + |
| 16 | +typedef struct _GLFWDisplayLinkNS |
| 17 | +{ |
| 18 | + CVDisplayLinkRef displayLink; |
| 19 | + CGDirectDisplayID displayID; |
| 20 | + monotonic_t lastRenderFrameRequestedAt, first_unserviced_render_frame_request_at; |
| 21 | +} _GLFWDisplayLinkNS; |
| 22 | + |
| 23 | +static struct { |
| 24 | + _GLFWDisplayLinkNS entries[256]; |
| 25 | + size_t count; |
| 26 | +} displayLinks = {0}; |
| 27 | + |
| 28 | +static CGDirectDisplayID |
| 29 | +displayIDForWindow(_GLFWwindow *w) { |
| 30 | + NSWindow *nw = w->ns.object; |
| 31 | + NSDictionary *dict = [nw.screen deviceDescription]; |
| 32 | + NSNumber *displayIDns = dict[@"NSScreenNumber"]; |
| 33 | + if (displayIDns) return [displayIDns unsignedIntValue]; |
| 34 | + return (CGDirectDisplayID)-1; |
| 35 | +} |
| 36 | + |
| 37 | +void |
| 38 | +_glfwClearDisplayLinks(void) { |
| 39 | + for (size_t i = 0; i < displayLinks.count; i++) { |
| 40 | + if (displayLinks.entries[i].displayLink) { |
| 41 | + CVDisplayLinkStop(displayLinks.entries[i].displayLink); |
| 42 | + CVDisplayLinkRelease(displayLinks.entries[i].displayLink); |
| 43 | + } |
| 44 | + } |
| 45 | + memset(displayLinks.entries, 0, sizeof(_GLFWDisplayLinkNS) * displayLinks.count); |
| 46 | + displayLinks.count = 0; |
| 47 | +} |
| 48 | + |
| 49 | +static CVReturn |
| 50 | +displayLinkCallback( |
| 51 | + CVDisplayLinkRef displayLink UNUSED, |
| 52 | + const CVTimeStamp* now UNUSED, const CVTimeStamp* outputTime UNUSED, |
| 53 | + CVOptionFlags flagsIn UNUSED, CVOptionFlags* flagsOut UNUSED, void* userInfo) { |
| 54 | + CGDirectDisplayID displayID = (uintptr_t)userInfo; |
| 55 | + NSNumber *arg = [NSNumber numberWithUnsignedInt:displayID]; |
| 56 | + [NSApp performSelectorOnMainThread:@selector(render_frame_received:) withObject:arg waitUntilDone:NO]; |
| 57 | + [arg release]; |
| 58 | + return kCVReturnSuccess; |
| 59 | +} |
| 60 | + |
| 61 | +static void |
| 62 | +_glfw_create_cv_display_link(_GLFWDisplayLinkNS *entry) { |
| 63 | + CVDisplayLinkCreateWithCGDisplay(entry->displayID, &entry->displayLink); |
| 64 | + CVDisplayLinkSetOutputCallback(entry->displayLink, &displayLinkCallback, (void*)(uintptr_t)entry->displayID); |
| 65 | +} |
| 66 | + |
| 67 | +unsigned |
| 68 | +_glfwCreateDisplayLink(CGDirectDisplayID displayID) { |
| 69 | + if (displayLinks.count >= arraysz(displayLinks.entries) - 1) { |
| 70 | + _glfwInputError(GLFW_PLATFORM_ERROR, "Too many monitors cannot create display link"); |
| 71 | + return displayLinks.count; |
| 72 | + } |
| 73 | + for (unsigned i = 0; i < displayLinks.count; i++) { |
| 74 | + // already created in this run |
| 75 | + if (displayLinks.entries[i].displayID == displayID) return i; |
| 76 | + } |
| 77 | + _GLFWDisplayLinkNS *entry = &displayLinks.entries[displayLinks.count++]; |
| 78 | + memset(entry, 0, sizeof(_GLFWDisplayLinkNS)); |
| 79 | + entry->displayID = displayID; |
| 80 | + _glfw_create_cv_display_link(entry); |
| 81 | + return displayLinks.count - 1; |
| 82 | +} |
| 83 | + |
| 84 | +static unsigned long long display_link_shutdown_timer = 0; |
| 85 | + |
| 86 | +static void |
| 87 | +_glfwShutdownCVDisplayLink(unsigned long long timer_id UNUSED, void *user_data UNUSED) { |
| 88 | + display_link_shutdown_timer = 0; |
| 89 | + for (size_t i = 0; i < displayLinks.count; i++) { |
| 90 | + _GLFWDisplayLinkNS *dl = &displayLinks.entries[i]; |
| 91 | + if (dl->displayLink) CVDisplayLinkStop(dl->displayLink); |
| 92 | + dl->lastRenderFrameRequestedAt = 0; |
| 93 | + dl->first_unserviced_render_frame_request_at = 0; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +void |
| 98 | +_glfwRequestRenderFrame(_GLFWwindow *w) { |
| 99 | + CGDirectDisplayID displayID = displayIDForWindow(w); |
| 100 | + if (display_link_shutdown_timer) { |
| 101 | + _glfwPlatformUpdateTimer(display_link_shutdown_timer, DISPLAY_LINK_SHUTDOWN_CHECK_INTERVAL, true); |
| 102 | + } else { |
| 103 | + display_link_shutdown_timer = _glfwPlatformAddTimer(DISPLAY_LINK_SHUTDOWN_CHECK_INTERVAL, false, _glfwShutdownCVDisplayLink, NULL, NULL); |
| 104 | + } |
| 105 | + monotonic_t now = glfwGetTime(); |
| 106 | + bool found_display_link = false; |
| 107 | + _GLFWDisplayLinkNS *dl = NULL; |
| 108 | + for (size_t i = 0; i < displayLinks.count; i++) { |
| 109 | + dl = &displayLinks.entries[i]; |
| 110 | + if (dl->displayID == displayID) { |
| 111 | + found_display_link = true; |
| 112 | + dl->lastRenderFrameRequestedAt = now; |
| 113 | + if (!dl->first_unserviced_render_frame_request_at) dl->first_unserviced_render_frame_request_at = now; |
| 114 | + if (!CVDisplayLinkIsRunning(dl->displayLink)) CVDisplayLinkStart(dl->displayLink); |
| 115 | + else if (now - dl->first_unserviced_render_frame_request_at > s_to_monotonic_t(1ll)) { |
| 116 | + // display link is stuck need to recreate it because Apple can't even |
| 117 | + // get a simple timer right |
| 118 | + CVDisplayLinkRelease(dl->displayLink); dl->displayLink = nil; |
| 119 | + dl->first_unserviced_render_frame_request_at = now; |
| 120 | + _glfw_create_cv_display_link(dl); |
| 121 | + _glfwInputError(GLFW_PLATFORM_ERROR, |
| 122 | + "CVDisplayLink stuck possibly because of sleep/screensaver + Apple's incompetence, recreating."); |
| 123 | + if (!CVDisplayLinkIsRunning(dl->displayLink)) CVDisplayLinkStart(dl->displayLink); |
| 124 | + } |
| 125 | + } else if (dl->displayLink && dl->lastRenderFrameRequestedAt && now - dl->lastRenderFrameRequestedAt >= DISPLAY_LINK_SHUTDOWN_CHECK_INTERVAL) { |
| 126 | + CVDisplayLinkStop(dl->displayLink); |
| 127 | + dl->lastRenderFrameRequestedAt = 0; |
| 128 | + dl->first_unserviced_render_frame_request_at = 0; |
| 129 | + } |
| 130 | + } |
| 131 | + if (!found_display_link) { |
| 132 | + unsigned idx = _glfwCreateDisplayLink(displayID); |
| 133 | + if (idx < displayLinks.count) { |
| 134 | + dl = &displayLinks.entries[idx]; |
| 135 | + dl->lastRenderFrameRequestedAt = now; |
| 136 | + dl->first_unserviced_render_frame_request_at = now; |
| 137 | + if (!CVDisplayLinkIsRunning(dl->displayLink)) CVDisplayLinkStart(dl->displayLink); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +void |
| 143 | +_glfwDispatchRenderFrame(CGDirectDisplayID displayID) { |
| 144 | + _GLFWwindow *w = _glfw.windowListHead; |
| 145 | + while (w) { |
| 146 | + if (w->ns.renderFrameRequested && displayID == displayIDForWindow(w)) { |
| 147 | + w->ns.renderFrameRequested = false; |
| 148 | + w->ns.renderFrameCallback((GLFWwindow*)w); |
| 149 | + } |
| 150 | + w = w->next; |
| 151 | + } |
| 152 | + for (size_t i = 0; i < displayLinks.count; i++) { |
| 153 | + _GLFWDisplayLinkNS *dl = &displayLinks.entries[i]; |
| 154 | + if (dl->displayID == displayID) { |
| 155 | + dl->first_unserviced_render_frame_request_at = 0; |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | + |
0 commit comments