From 974098464fed96ae85696cae9de708a685b03194 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 18 Dec 2024 14:18:11 -0800 Subject: [PATCH] Enable high refresh rates on iOS Fixes https://github.com/libsdl-org/SDL/issues/7518 (cherry picked from commit 835b6e0c1ac37ad0af98dd4b1f26c5421dc02128) --- src/video/uikit/SDL_uikitviewcontroller.m | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/video/uikit/SDL_uikitviewcontroller.m b/src/video/uikit/SDL_uikitviewcontroller.m index 432033a8cb..82fd49dc38 100644 --- a/src/video/uikit/SDL_uikitviewcontroller.m +++ b/src/video/uikit/SDL_uikitviewcontroller.m @@ -118,6 +118,20 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o SDL_HideHomeIndicatorHintChanged, (__bridge void *) self); #endif + + /* Enable high refresh rates on iOS + * To enable this on phones, you should add the following line to Info.plist: + * CADisableMinimumFrameDurationOnPhone + */ + if (@available(iOS 15.0, tvOS 15.0, *)) { + SDL_DisplayMode mode; + if (SDL_GetDesktopDisplayMode(0, &mode) == 0 && mode.refresh_rate > 60.0f) { + int frame_rate = (int)mode.refresh_rate; + displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(doLoop:)]; + displayLink.preferredFrameRateRange = CAFrameRateRangeMake((frame_rate * 2) / 3, frame_rate, frame_rate); + [displayLink addToRunLoop:NSRunLoop.currentRunLoop forMode:NSDefaultRunLoopMode]; + } + } } return self; } @@ -147,6 +161,9 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o { [self stopAnimation]; + if (interval <= 0) { + interval = 1; + } animationInterval = interval; animationCallback = callback; animationCallbackParam = callbackParam; @@ -191,7 +208,7 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o - (void)doLoop:(CADisplayLink*)sender { /* Don't run the game loop while a messagebox is up */ - if (!UIKit_ShowingMessageBox()) { + if (animationCallback && !UIKit_ShowingMessageBox()) { /* See the comment in the function definition. */ #if defined(SDL_VIDEO_OPENGL_ES) || defined(SDL_VIDEO_OPENGL_ES2) UIKit_GL_RestoreCurrentContext();