diff --git a/README.md b/README.md index fdc950d..f66c355 100644 --- a/README.md +++ b/README.md @@ -248,8 +248,10 @@ A fatal native/driver error may prevent further GL work, including cleanup. On macOS, vsync waits for a per-window CoreVideo display link before presenting, with the native OpenGL swap interval disabled. The link follows the window's -display. Each wait is bounded to 100 ms so a sleeping display cannot indefinitely -block the frame loop; disabling vsync bypasses the wait. +display. Present waits only while no refresh has passed since the previous present, +so a frame the driver already held until a refresh is not delayed by another. Each +wait is bounded to 100 ms so a sleeping display cannot indefinitely block the frame +loop; disabling vsync bypasses the wait. Accept close/quit requests with `uhdk_quit`. Keep iterating to drain request completions and `STOPPED`; then `next_frame` returns false and `get_status` returns diff --git a/src/macos/native.m b/src/macos/native.m index 2a78088..db37298 100644 --- a/src/macos/native.m +++ b/src/macos/native.m @@ -63,6 +63,7 @@ typedef struct ZDisplayPacer { @interface ZGL : NSObject { ZDisplayPacer *_pacer; uhdk_status _displayLinkStatus; + uint64_t _presentedTick; } @property(nonatomic, strong) NSOpenGLContext *context; @property(nonatomic, weak) ZWindow *owner; @@ -275,9 +276,11 @@ static CVReturn displayLinkTick( // A sleeping/disconnected display must not leave the main thread blocked. int64_t deadline = z_io_now(1) + INT64_C(100000000); pthread_mutex_lock(&_pacer->mutex); - uint64_t tick = _pacer->tick; uhdk_status status = UHDK_STATUS_OK; - while (_pacer->tick == tick) { + // Wait only while no refresh has passed since the previous present. The GL driver + // can already have held this frame until a refresh; waiting for yet another tick + // would then show every such frame one refresh late. + while (_pacer->tick == _presentedTick) { int64_t remaining = deadline - z_io_now(1); if (remaining <= 0) break; @@ -293,6 +296,7 @@ static CVReturn displayLinkTick( break; } } + _presentedTick = _pacer->tick; pthread_mutex_unlock(&_pacer->mutex); return status; }