Handle Retina / DPI correctly

This commit is contained in:
Edward Li 2020-01-27 06:10:24 +08:00
parent 0d9214df40
commit 0ba07d98e7
2 changed files with 22 additions and 4 deletions

View File

@ -22,7 +22,8 @@ class EmuGLView: NSOpenGLView, NSWindowDelegate {
openGLContext!.makeCurrentContext()
if (emu_single_frame(Int32(dirtyRect.width), Int32(dirtyRect.height)) != 0)
let rect = convertToBacking(dirtyRect)
if (emu_single_frame(Int32(rect.width), Int32(rect.height)) != 0)
{
openGLContext!.flushBuffer()
}

View File

@ -133,9 +133,26 @@ extern "C" void emu_gles_init(int width, int height) {
// Calculate screen DPI
NSScreen *screen = [NSScreen mainScreen];
NSDictionary *description = [screen deviceDescription];
NSSize displayPixelSize = [[description objectForKey:NSDeviceSize] sizeValue];
CGSize displayPhysicalSize = CGDisplayScreenSize([[description objectForKey:@"NSScreenNumber"] unsignedIntValue]);
screen_dpi = (int)(displayPixelSize.width / displayPhysicalSize.width) * 25.4f;
CGDirectDisplayID displayID = [[description objectForKey:@"NSScreenNumber"] unsignedIntValue];
CGSize displayPhysicalSize = CGDisplayScreenSize(displayID);
//Neither CGDisplayScreenSize(description's NSScreenNumber) nor [NSScreen backingScaleFactor] can calculate the correct dpi in macOS. E.g. backingScaleFactor is always 2 in all display modes for rMBP 16"
NSSize displayNativeSize;
CFArrayRef allDisplayModes = CGDisplayCopyAllDisplayModes(displayID, NULL);
CFIndex n = CFArrayGetCount(allDisplayModes);
for(int i = 0; i < n; ++i)
{
CGDisplayModeRef m = (CGDisplayModeRef)CFArrayGetValueAtIndex(allDisplayModes, i);
if(CGDisplayModeGetIOFlags(m) & kDisplayModeNativeFlag)
{
displayNativeSize.width = CGDisplayModeGetPixelWidth(m);
displayNativeSize.height = CGDisplayModeGetPixelHeight(m);
break;
}
}
CFRelease(allDisplayModes);
screen_dpi = (int)(displayNativeSize.width / displayPhysicalSize.width * 25.4f);
screen_width = width;
screen_height = height;