src/video/cocoa/SDL_cocoametalview.m
changeset 11886 27d81cb6e6af
parent 11816 dea9cf23bad5
child 12002 9116d02d3feb
     1.1 --- a/src/video/cocoa/SDL_cocoametalview.m	Wed Feb 21 09:40:47 2018 -0800
     1.2 +++ b/src/video/cocoa/SDL_cocoametalview.m	Wed Feb 21 09:58:21 2018 -0800
     1.3 @@ -57,17 +57,19 @@
     1.4  }
     1.5  
     1.6  - (instancetype)initWithFrame:(NSRect)frame
     1.7 -                   useHighDPI:(bool)useHighDPI
     1.8 +                        scale:(CGFloat)scale
     1.9  {
    1.10  	if ((self = [super initWithFrame:frame])) {
    1.11 +        _tag = METALVIEW_TAG;
    1.12          self.wantsLayer = YES;
    1.13  
    1.14          /* Allow resize. */
    1.15          self.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
    1.16 -        _tag = METALVIEW_TAG;
    1.17  
    1.18 -        _useHighDPI = useHighDPI;
    1.19 -        [self updateDrawableSize];
    1.20 +        /* Set the desired scale. The default drawableSize of a CAMetalLayer
    1.21 +         * is its bounds x its scale so nothing further needs to be done.
    1.22 +         */
    1.23 +        self.layer.contentsScale = scale;
    1.24  	}
    1.25    
    1.26  	return self;
    1.27 @@ -77,16 +79,6 @@
    1.28  - (void)resizeWithOldSuperviewSize:(NSSize)oldSize
    1.29  {
    1.30      [super resizeWithOldSuperviewSize:oldSize];
    1.31 -    [self updateDrawableSize];
    1.32 -}
    1.33 -
    1.34 -- (void)updateDrawableSize
    1.35 -{
    1.36 -    NSRect bounds = [self bounds];
    1.37 -    if (_useHighDPI) {
    1.38 -        bounds = [self convertRectToBacking:bounds];
    1.39 -    }
    1.40 -    ((CAMetalLayer *) self.layer).drawableSize = NSSizeToCGSize(bounds.size);
    1.41  }
    1.42  
    1.43  @end
    1.44 @@ -94,12 +86,26 @@
    1.45  SDL_cocoametalview*
    1.46  Cocoa_Mtl_AddMetalView(SDL_Window* window)
    1.47  {
    1.48 -    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
    1.49 +    SDL_WindowData* data = (__bridge SDL_WindowData *)window->driverdata;
    1.50      NSView *view = data->nswindow.contentView;
    1.51 +    CGFloat scale = 1.0;
    1.52  
    1.53 +    if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
    1.54 +        /* Set the scale to the natural scale factor of the screen - then
    1.55 +         * the backing dimensions of the Metal view will match the pixel
    1.56 +         * dimensions of the screen rather than the dimensions in points
    1.57 +         * yielding high resolution on retine displays.
    1.58 +         *
    1.59 +         * N.B. In order for backingScaleFactor to be > 1,
    1.60 +         * NSHighResolutionCapable must be set to true in the app's Info.plist.
    1.61 +         */
    1.62 +        NSWindow* nswindow = data->nswindow;
    1.63 +        if ([nswindow.screen respondsToSelector:@selector(backingScaleFactor)])
    1.64 +            scale = data->nswindow.screen.backingScaleFactor;
    1.65 +    }
    1.66 +        
    1.67      SDL_cocoametalview *metalview
    1.68 -        = [[SDL_cocoametalview alloc] initWithFrame:view.frame
    1.69 -                       useHighDPI:(window->flags & SDL_WINDOW_ALLOW_HIGHDPI)];
    1.70 +        = [[SDL_cocoametalview alloc] initWithFrame:view.frame scale:scale];
    1.71      [view addSubview:metalview];
    1.72      return metalview;
    1.73  }
    1.74 @@ -119,6 +125,8 @@
    1.75          if (h) {
    1.76              *h = layer.drawableSize.height;
    1.77          }
    1.78 +    } else {
    1.79 +        SDL_GetWindowSize(window, w, h);
    1.80      }
    1.81  }
    1.82