Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 67 additions & 11 deletions plume_apple.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
//

#include "plume_apple.h"

#include <TargetConditionals.h>
#if TARGET_OS_OSX
#import <AppKit/AppKit.h>
#import <Foundation/Foundation.h>
#import <IOKit/IOKitLib.h>
#else
#import <UIKit/UIKit.h>
#endif
#import <Foundation/Foundation.h>

#if TARGET_OS_OSX
static uint32_t plumeGetEntryProperty(io_registry_entry_t entry, CFStringRef propertyName) {
uint32_t value = 0;
CFTypeRef cfProp = IORegistryEntrySearchCFProperty(entry, kIOServicePlane, propertyName, kCFAllocatorDefault, kIORegistryIterateRecursively | kIORegistryIterateParents);
Expand All @@ -27,9 +32,10 @@ static uint32_t plumeGetEntryProperty(io_registry_entry_t entry, CFStringRef pro

return value;
}

#endif
namespace plume {
RenderDeviceVendor getRenderDeviceVendor(uint64_t registryID) {
#if TARGET_OS_OSX
io_service_t entry = IOServiceGetMatchingService(MACH_PORT_NULL, IORegistryEntryIDMatching(registryID));

if (entry) {
Expand All @@ -42,17 +48,30 @@ RenderDeviceVendor getRenderDeviceVendor(uint64_t registryID) {
}
IOObjectRelease(entry); // Release the entry if we couldn't get parent
}

return RenderDeviceVendor::UNKNOWN;
#else
return RenderDeviceVendor::APPLE;
#endif
}

#if TARGET_OS_OSX
CGFloat getScaleFactor(NSWindow *nsWindow) {
#ifdef PLUME_APPLE_RETINA_ENABLED
return [nsWindow backingScaleFactor];
#else
return 1.0f;
#endif
}
#else
CGFloat getScaleFactor(UIWindow *uiWindow) {
#ifdef PLUME_APPLE_RETINA_ENABLED
return [uiWindow screen].scale;
#else
return 1.0f;
#endif
}
#endif

// MARK: - CocoaWindow

Expand All @@ -61,17 +80,23 @@ CGFloat getScaleFactor(NSWindow *nsWindow) {
cachedAttributes = {0, 0, 0, 0};

if ([NSThread isMainThread]) {
#if TARGET_OS_OSX
NSWindow *nsWindow = (__bridge NSWindow *)windowHandle;
NSRect contentFrame = [[nsWindow contentView] frame];
CGFloat scaleFactor = getScaleFactor(nsWindow);

NSScreen *screen = [nsWindow screen];
#else
UIWindow *uiWindow = (__bridge UIWindow *)windowHandle;
CGRect contentFrame = uiWindow.bounds;
CGFloat scaleFactor = getScaleFactor(uiWindow);
UIScreen *screen = [uiWindow screen];
#endif
cachedAttributes.x = (int)round(contentFrame.origin.x);
cachedAttributes.y = (int)round(contentFrame.origin.y);
cachedAttributes.width = (int)round(contentFrame.size.width * scaleFactor);
cachedAttributes.height = (int)round(contentFrame.size.height * scaleFactor);

NSScreen *screen = [nsWindow screen];
if (@available(macOS 12.0, *)) {

if (@available(macOS 12.0, iOS 10.3, *)) {
cachedRefreshRate.store((int)[screen maximumFramesPerSecond]);
}
} else {
Expand All @@ -84,10 +109,16 @@ CGFloat getScaleFactor(NSWindow *nsWindow) {

void CocoaWindow::updateWindowAttributesInternal(bool forceSync) {
auto updateBlock = ^{
#if TARGET_OS_OSX
NSWindow *nsWindow = (__bridge NSWindow *)windowHandle;
NSRect contentFrame = [[nsWindow contentView] frame];
CGFloat scaleFactor = getScaleFactor(nsWindow);

#else
UIWindow *uiWindow = (__bridge UIWindow *)windowHandle;
CGRect contentFrame = uiWindow.bounds;
CGFloat scaleFactor = getScaleFactor(uiWindow);
#endif

std::lock_guard<std::mutex> lock(attributesMutex);
cachedAttributes.x = (int)round(contentFrame.origin.x);
cachedAttributes.y = (int)round(contentFrame.origin.y);
Expand All @@ -104,11 +135,19 @@ CGFloat getScaleFactor(NSWindow *nsWindow) {

void CocoaWindow::updateRefreshRateInternal(bool forceSync) {
auto updateBlock = ^{
#if TARGET_OS_OSX
NSWindow *nsWindow = (__bridge NSWindow *)windowHandle;
NSScreen *screen = [nsWindow screen];
if (@available(macOS 12.0, *)) {
cachedRefreshRate.store((int)[screen maximumFramesPerSecond]);
}
#else
UIWindow *uiWindow = (__bridge UIWindow *)windowHandle;
UIScreen *screen = [uiWindow screen];
if (@available(iOS 10.3, *)) {
cachedRefreshRate.store((int)[screen maximumFramesPerSecond]);
}
#endif
};

if (forceSync) {
Expand All @@ -119,10 +158,16 @@ CGFloat getScaleFactor(NSWindow *nsWindow) {
}

void CocoaWindow::getWindowAttributes(CocoaWindowAttributes* attributes) const {
if ([NSThread isMainThread]) {
if ([NSThread isMainThread]) {
#if TARGET_OS_OSX
NSWindow *nsWindow = (__bridge NSWindow *)windowHandle;
NSRect contentFrame = [[nsWindow contentView] frame];
CGFloat scaleFactor = getScaleFactor(nsWindow);
#else
UIWindow* nsWindow = (__bridge UIWindow *)windowHandle;
CGRect contentFrame = nsWindow.bounds;
CGFloat scaleFactor = getScaleFactor(nsWindow);
#endif

{
std::lock_guard<std::mutex> lock(attributesMutex);
Expand All @@ -145,6 +190,7 @@ CGFloat getScaleFactor(NSWindow *nsWindow) {

int CocoaWindow::getRefreshRate() const {
if ([NSThread isMainThread]) {
#if TARGET_OS_OSX
NSWindow *nsWindow = (__bridge NSWindow *)windowHandle;
NSScreen *screen = [nsWindow screen];

Expand All @@ -153,7 +199,15 @@ CGFloat getScaleFactor(NSWindow *nsWindow) {
const_cast<CocoaWindow*>(this)->cachedRefreshRate.store(freshRate);
return freshRate;
}

#else
UIWindow *uiWindow = (__bridge UIWindow *)windowHandle;
UIScreen *screen = [uiWindow screen];
if (@available(iOS 10.3, *)) {
int freshRate = (int)[screen maximumFramesPerSecond];
const_cast<CocoaWindow*>(this)->cachedRefreshRate.store(freshRate);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CocoaWindow should probably be renamed if it's also going to be used for iOS

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should it be renamed to then?

return freshRate;
}
#endif
return cachedRefreshRate.load();
} else {
int rate = cachedRefreshRate.load();
Expand All @@ -165,6 +219,7 @@ CGFloat getScaleFactor(NSWindow *nsWindow) {
}

void CocoaWindow::toggleFullscreen() {
#if TARGET_OS_OSX
if ([NSThread isMainThread]) {
NSWindow *nsWindow = (__bridge NSWindow *)windowHandle;
[nsWindow toggleFullScreen:NULL];
Expand All @@ -174,5 +229,6 @@ CGFloat getScaleFactor(NSWindow *nsWindow) {
[nsWindow toggleFullScreen:NULL];
});
}
#endif
}
}