cocoa: SDL_GetDisplayDPI() should account for Retina displays.
authorRyan C. Gordon <icculus@icculus.org>
Wed, 04 Dec 2019 12:20:24 -0500
changeset 133133b03741c0095
parent 13312 dde034962e3a
child 13314 2d936252374c
cocoa: SDL_GetDisplayDPI() should account for Retina displays.

Fixes Bugzilla #4856.
src/video/cocoa/SDL_cocoamodes.m
     1.1 --- a/src/video/cocoa/SDL_cocoamodes.m	Tue Dec 03 22:07:58 2019 -0800
     1.2 +++ b/src/video/cocoa/SDL_cocoamodes.m	Wed Dec 04 12:20:24 2019 -0500
     1.3 @@ -443,27 +443,41 @@
     1.4  
     1.5  int
     1.6  Cocoa_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi)
     1.7 +{ @autoreleasepool
     1.8  {
     1.9      const float MM_IN_INCH = 25.4f;
    1.10  
    1.11      SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
    1.12  
    1.13 -    CGSize displaySize = CGDisplayScreenSize(data->display);
    1.14 -    int pixelWidth =  (int) CGDisplayPixelsWide(data->display);
    1.15 -    int pixelHeight = (int) CGDisplayPixelsHigh(data->display);
    1.16 +    /* we need the backingScaleFactor for Retina displays, which is only exposed through NSScreen, not CGDisplay, afaik, so find our screen... */
    1.17 +    CGFloat scaleFactor = 1.0f;
    1.18 +    NSArray<NSScreen *> *screens = [NSScreen screens];
    1.19 +    for (NSScreen *screen in screens) {
    1.20 +        const CGDirectDisplayID dpyid = (const CGDirectDisplayID ) [[[screen deviceDescription] objectForKey:@"NSScreenNumber"] unsignedIntValue];
    1.21 +        if (dpyid == data->display) {
    1.22 +            if ([screen respondsToSelector:@selector(backingScaleFactor)]) {  // Mac OS X 10.7 and later
    1.23 +                scaleFactor = [screen backingScaleFactor];
    1.24 +                break;
    1.25 +            }
    1.26 +        }
    1.27 +    }
    1.28 +
    1.29 +    const CGSize displaySize = CGDisplayScreenSize(data->display);
    1.30 +    const int pixelWidth =  (int) CGDisplayPixelsWide(data->display);
    1.31 +    const int pixelHeight = (int) CGDisplayPixelsHigh(data->display);
    1.32  
    1.33      if (ddpi) {
    1.34 -        *ddpi = SDL_ComputeDiagonalDPI(pixelWidth, pixelHeight, displaySize.width / MM_IN_INCH, displaySize.height / MM_IN_INCH);
    1.35 +        *ddpi = (SDL_ComputeDiagonalDPI(pixelWidth, pixelHeight, displaySize.width / MM_IN_INCH, displaySize.height / MM_IN_INCH)) * scaleFactor;
    1.36      }
    1.37      if (hdpi) {
    1.38 -        *hdpi = pixelWidth * MM_IN_INCH / displaySize.width;
    1.39 +        *hdpi = (pixelWidth * MM_IN_INCH / displaySize.width) * scaleFactor;
    1.40      }
    1.41      if (vdpi) {
    1.42 -        *vdpi = pixelHeight * MM_IN_INCH / displaySize.height;
    1.43 +        *vdpi = (pixelHeight * MM_IN_INCH / displaySize.height) * scaleFactor;
    1.44      }
    1.45  
    1.46      return 0;
    1.47 -}
    1.48 +}}
    1.49  
    1.50  void
    1.51  Cocoa_GetDisplayModes(_THIS, SDL_VideoDisplay * display)