System: Check host page size on startup

This commit is contained in:
Stenzek
2024-05-16 17:24:42 +10:00
parent a5b1ee4f04
commit 35bdbf2a55
8 changed files with 182 additions and 25 deletions

View File

@ -1,17 +1,19 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#include "metal_layer.h"
#include "platform_misc.h"
#include "window_info.h"
#include "metal_layer.h"
#include "common/log.h"
#include "common/small_string.h"
#include <IOKit/pwr_mgt/IOPMLib.h>
#include <Cocoa/Cocoa.h>
#include <IOKit/pwr_mgt/IOPMLib.h>
#include <QuartzCore/QuartzCore.h>
#include <cinttypes>
#include <optional>
#include <sys/sysctl.h>
#include <vector>
Log_SetChannel(PlatformMisc);
@ -50,11 +52,11 @@ void PlatformMisc::SuspendScreensaver()
{
if (s_screensaver_suspended)
if (!SetScreensaverInhibitMacOS(true))
{
Log_ErrorPrintf("Failed to suspend screensaver.");
return;
}
if (!SetScreensaverInhibitMacOS(true))
{
Log_ErrorPrintf("Failed to suspend screensaver.");
return;
}
s_screensaver_suspended = true;
}
@ -70,6 +72,27 @@ void PlatformMisc::ResumeScreensaver()
s_screensaver_suspended = false;
}
template<typename T>
static std::optional<T> sysctlbyname(const char* name)
{
T output = 0;
size_t output_size = sizeof(output);
if (sysctlbyname(name, &output, &output_size, nullptr, 0) != 0)
return std::nullopt;
return output;
}
size_t PlatformMisc::GetRuntimePageSize()
{
return sysctlbyname<u32>("hw.pagesize").value_or(0);
}
size_t PlatformMisc::GetRuntimeCacheLineSize()
{
return static_cast<size_t>(std::max<s64>(sysctlbyname<s64>("hw.cachelinesize").value_or(0), 0));
}
bool PlatformMisc::PlaySoundAsync(const char* path)
{
NSString* nspath = [[NSString alloc] initWithUTF8String:path];
@ -80,46 +103,44 @@ bool PlatformMisc::PlaySoundAsync(const char* path)
return result;
}
bool CocoaTools::CreateMetalLayer(WindowInfo *wi)
bool CocoaTools::CreateMetalLayer(WindowInfo* wi)
{
// Punt off to main thread if we're not calling from it already.
if (![NSThread isMainThread])
{
bool ret;
dispatch_sync(dispatch_get_main_queue(), [&ret, wi]() {
ret = CreateMetalLayer(wi);
});
dispatch_sync(dispatch_get_main_queue(), [&ret, wi]() { ret = CreateMetalLayer(wi); });
return ret;
}
CAMetalLayer* layer = [CAMetalLayer layer];
if (layer == nil)
{
Log_ErrorPrint("Failed to create CAMetalLayer");
return false;
}
NSView* view = (__bridge NSView*)wi->window_handle;
[view setWantsLayer:TRUE];
[view setLayer:layer];
[layer setContentsScale:[[[view window] screen] backingScaleFactor]];
wi->surface_handle = (__bridge void*)layer;
return true;
}
void CocoaTools::DestroyMetalLayer(WindowInfo *wi)
void CocoaTools::DestroyMetalLayer(WindowInfo* wi)
{
if (!wi->surface_handle)
return;
// Punt off to main thread if we're not calling from it already.
if (![NSThread isMainThread])
{
dispatch_sync(dispatch_get_main_queue(), [wi]() { DestroyMetalLayer(wi); });
return;
}
NSView* view = (__bridge NSView*)wi->window_handle;
CAMetalLayer* layer = (__bridge CAMetalLayer*)wi->surface_handle;
[view setLayer:nil];