Use MacOS-specific refresh rate check to avoid SDL race condition (#1262)

* Use MacOS-specific refresh rate check to avoid SDL race condition

* IsLowRefreshRate: Change back to `SDL_Init` just to be safe
This commit is contained in:
OpenSauce 2025-08-01 15:45:07 +00:00 committed by GitHub
parent 4b4d4f09da
commit 885bb71da8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 3 deletions

View File

@ -4,6 +4,7 @@
namespace AppleUtils {
float GetRefreshRate();
int IsLowPowerModeEnabled();
}
} // namespace AppleUtils

View File

@ -2,10 +2,29 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#import <Cocoa/Cocoa.h>
#import <CoreGraphics/CoreGraphics.h>
#import <Foundation/Foundation.h>
namespace AppleUtils {
float GetRefreshRate() { // TODO: How does this handle multi-monitor? -OS
NSScreen* screen = [NSScreen mainScreen];
if (screen) {
NSDictionary* screenInfo = [screen deviceDescription];
CGDirectDisplayID displayID =
(CGDirectDisplayID)[screenInfo[@"NSScreenNumber"] unsignedIntValue];
CGDisplayModeRef displayMode = CGDisplayCopyDisplayMode(displayID);
if (displayMode) {
CGFloat refreshRate = CGDisplayModeGetRefreshRate(displayMode);
CFRelease(displayMode);
return refreshRate;
}
}
return 60; // Something went wrong, so just return a generic value
}
int IsLowPowerModeEnabled() {
return (int)[NSProcessInfo processInfo].lowPowerModeEnabled;
}

View File

@ -60,7 +60,11 @@ constexpr static std::array<vk::DescriptorSetLayoutBinding, 1> PRESENT_BINDINGS
namespace {
static bool IsLowRefreshRate() {
#ifdef ENABLE_SDL2
#if defined(__APPLE__) || defined(ENABLE_SDL2)
#ifdef __APPLE__ // Need a special implementation because MacOS kills itself in disgust if the
// input thread calls SDL_PumpEvents at the same time as we're in SDL_Init here.
const auto cur_refresh_rate = AppleUtils::GetRefreshRate();
#elif defined(ENABLE_SDL2)
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
LOG_ERROR(Render_Vulkan, "SDL video failed to initialize, unable to check refresh rate");
return false;
@ -71,6 +75,7 @@ static bool IsLowRefreshRate() {
const auto cur_refresh_rate = cur_display_mode.refresh_rate;
SDL_QuitSubSystem(SDL_INIT_VIDEO);
#endif // __APPLE__
if (cur_refresh_rate < SCREEN_REFRESH_RATE) {
LOG_WARNING(Render_Vulkan,
@ -79,7 +84,7 @@ static bool IsLowRefreshRate() {
cur_refresh_rate);
return true;
}
#endif
#endif // defined(__APPLE__) || defined(ENABLE_SDL2)
#ifdef __APPLE__
// Apple's low power mode sometimes limits applications to 30fps without changing the refresh