2

I'm looking to find the value that "About this Mac" shows (2560 x 1600 on my 13" MBP). I have tried CGDisplayBounds and NSScreen.main, both do not return those values but instead return what is used internal for rendering / measuring.

as by Kens suggestion:

let modes = CGDisplayCopyAllDisplayModes(CGMainDisplayID(), [kCGDisplayShowDuplicateLowResolutionModes: kCFBooleanTrue] as CFDictionary) as! [CGDisplayMode]

for mode in modes {
    let flags = String(format:"%02X", mode.ioFlags)
    print("\(mode.pixelWidth)x\(mode.pixelHeight) \(mode.width)x\(mode.height) 0x\(flags)")
}

Output is:

2560x1600 2560x1600 0x2000003 <- This would be the correct one
...
2880x1800 2880x1800 0x03 <- This one is the biggest 1x mode
...

So using the biggest 1x would get the wrong result. I added the ioFlags to the output. I belive this might the missing link;-)

Thank you Ken!

  • Hmm. Thanks for the upvote, but I think we still don't know the right technique. On my Retina iMac running macOS 10.12.6, no display mode has that `0x2000000` bit set in its `ioFlags`. And the largest is the real physical pixel count. I'm not sure if the difference is the display, the OS version, or something else. – Ken Thomases Dec 03 '18 at 23:30
  • Oh... Thank you for reporting back. I have access to 2 more Mac models later this week, will update the answer once I find out more. – Thies Ḉ Arntzen Dec 04 '18 at 12:19
  • Hey, can you doublecheck on your side, I believe that you should see the 0x2000000 bit. Search for kDisplayModeNativeFlag in [https://stackoverflow.com/questions/13859109/how-to-programmatically-determine-native-pixel-resolution-of-retina-macbook-pro/53551561#53551561]. [https://github.com/aosm/IOKitUser/blob/master/graphics.subproj/IODisplayLib.c#L1816] – Thies Ḉ Arntzen Dec 06 '18 at 17:00
  • I've checked repeatedly. I know about the other answer on that other SO question. That user and I have also interacted on [https://stackoverflow.com/a/53563363/1312143](https://stackoverflow.com/a/53563363/1312143). No display mode on my system has an `ioFlags` value greater than `0x400003`. Evidently the conditions necessary for setting `kDisplayModeNativeFlag` in that IOKit source code are never met, for whatever reason. Same is true on a couple of other iMac models I've tested. – Ken Thomases Dec 06 '18 at 19:18
  • I'm beginning to think that reading the output from "system_profiler SPDisplaysDataType | grep Resolution:" might be my best bet. – Thies Ḉ Arntzen Dec 06 '18 at 22:31

2 Answers2

3

I think the best approach is to enumerate all of the display modes (including the 1x modes) and find a) one whose ioFlags includes kDisplayModeNativeFlag, or, if none has that flag, b) the biggest 1x mode's dimensions.

You would use CGDisplayCopyAllDisplayModes() and pass a dictionary with the key kCGDisplayShowDuplicateLowResolutionModes mapped to kCFBooleanTrue as the options to get all of the modes. You can test that CGDisplayModeGetPixelWidth() is equal to CGDisplayModeGetWidth() to determine which are 1x.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • after passing the kCGDisplayShowDuplicateLowResolutionModes for me still getting same resolutions(few resolutions) same resolutions which i was getting when i pass nil. i am using mac mini . – jarvis12 Apr 16 '19 at 04:52
  • @jarvis12 You should probably open a new question. You may have done it incorrectly, and a new question is the better way to figure that out. (You'll need to show your code.) That said, unless you've attached a Retina-capable display to your Mini, there are no other resolutions to get. – Ken Thomases Apr 16 '19 at 05:28
  • see [this](https://stackoverflow.com/questions/55650921/how-to-show-all-supported-resolutions-for-display-in-mac-app) mine is also same problem same code . – jarvis12 Apr 16 '19 at 06:02
  • This heuristic is working well for me so far, even in mirrored modes. – Rhythmic Fistman Aug 11 '20 at 14:43
1

NSScreen has backingScaleFactor property to check the scale

Quang Vĩnh Hà
  • 494
  • 3
  • 8
  • Thank you! This should be the accepted answer: `NSScreen.main?.backingScaleFactor` – VoodooBoot Jan 30 '21 at 22:29
  • 1
    This is unfortunately not correct. Even the official documentation states that `backingScaleFactor` "[...] does not represent anything concrete, such as pixel density or physical size, because it can vary based on the configured display mode." At the example of my 16" MacBook Pro from 2019: My physical display resolution is 3072 × 1920. By default, macOS translates this into a virtual screen space of 1792 × 1120. That results in a pixel density of 12 phyxical pixels for 7 virtual ones, or 12/7 = 1.71... as an actual ratio between virtual and physical pixels. `backingScaleFactor` reports 2. – manu_unter Apr 07 '22 at 12:43
  • Unless your screen is exactly one pixel or two pixel per point, everything is drawn into a buffer, and the buffer is drawn onto the monitor scaled. backingScaleFactor is the scale factor for the buffer. So in this example, the buffer is scaled down to about 85.5%. – gnasher729 Jun 03 '23 at 11:12