src/video/cocoa/SDL_cocoametalview.m
changeset 12320 758d4e1222a7
parent 12201 8bdc4d340419
child 12503 806492103856
     1.1 --- a/src/video/cocoa/SDL_cocoametalview.m	Thu Oct 11 15:14:48 2018 -0700
     1.2 +++ b/src/video/cocoa/SDL_cocoametalview.m	Fri Oct 12 17:55:42 2018 -0300
     1.3 @@ -33,9 +33,6 @@
     1.4  
     1.5  @implementation SDL_cocoametalview
     1.6  
     1.7 -/* The synthesized getter should be called by super's viewWithTag. */
     1.8 -@synthesize tag = _tag;
     1.9 -
    1.10  /* Return a Metal-compatible layer. */
    1.11  + (Class)layerClass
    1.12  {
    1.13 @@ -57,27 +54,48 @@
    1.14  }
    1.15  
    1.16  - (instancetype)initWithFrame:(NSRect)frame
    1.17 -                        scale:(CGFloat)scale
    1.18 +                      highDPI:(BOOL)highDPI
    1.19  {
    1.20      if ((self = [super initWithFrame:frame])) {
    1.21 -        _tag = METALVIEW_TAG;
    1.22 +        self.highDPI = highDPI;
    1.23          self.wantsLayer = YES;
    1.24  
    1.25          /* Allow resize. */
    1.26          self.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
    1.27  
    1.28 -        /* Set the desired scale. */
    1.29 -        ((CAMetalLayer *) self.layer).drawableSize = NSSizeToCGSize([self bounds].size);
    1.30 -        self.layer.contentsScale = scale;
    1.31 +        [self updateDrawableSize];
    1.32      }
    1.33    
    1.34      return self;
    1.35  }
    1.36  
    1.37 +- (NSInteger)tag
    1.38 +{
    1.39 +    return METALVIEW_TAG;
    1.40 +}
    1.41 +
    1.42 +- (void)updateDrawableSize
    1.43 +{
    1.44 +    CAMetalLayer *metalLayer = (CAMetalLayer *)self.layer;
    1.45 +    CGSize size = self.bounds.size;
    1.46 +    CGSize backingSize = size;
    1.47 +
    1.48 +    if (self.highDPI) {
    1.49 +        /* Note: NSHighResolutionCapable must be set to true in the app's
    1.50 +         * Info.plist in order for the backing size to be high res.
    1.51 +         */
    1.52 +        backingSize = [self convertSizeToBacking:size];
    1.53 +    }
    1.54 +
    1.55 +    metalLayer.contentsScale = backingSize.height / size.height;
    1.56 +    metalLayer.drawableSize = backingSize;
    1.57 +}
    1.58 +
    1.59  /* Set the size of the metal drawables when the view is resized. */
    1.60  - (void)resizeWithOldSuperviewSize:(NSSize)oldSize
    1.61  {
    1.62      [super resizeWithOldSuperviewSize:oldSize];
    1.63 +    [self updateDrawableSize];
    1.64  }
    1.65  
    1.66  @end
    1.67 @@ -87,24 +105,10 @@
    1.68  {
    1.69      SDL_WindowData* data = (__bridge SDL_WindowData *)window->driverdata;
    1.70      NSView *view = data->nswindow.contentView;
    1.71 -    CGFloat scale = 1.0;
    1.72 +    BOOL highDPI = (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) != 0;
    1.73 +    SDL_cocoametalview *metalview;
    1.74  
    1.75 -    if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
    1.76 -        /* Set the scale to the natural scale factor of the screen - then
    1.77 -         * the backing dimensions of the Metal view will match the pixel
    1.78 -         * dimensions of the screen rather than the dimensions in points
    1.79 -         * yielding high resolution on retine displays.
    1.80 -         *
    1.81 -         * N.B. In order for backingScaleFactor to be > 1,
    1.82 -         * NSHighResolutionCapable must be set to true in the app's Info.plist.
    1.83 -         */
    1.84 -        NSWindow* nswindow = data->nswindow;
    1.85 -        if ([nswindow.screen respondsToSelector:@selector(backingScaleFactor)])
    1.86 -            scale = data->nswindow.screen.backingScaleFactor;
    1.87 -    }
    1.88 -        
    1.89 -    SDL_cocoametalview *metalview
    1.90 -        = [[SDL_cocoametalview alloc] initWithFrame:view.frame scale:scale];
    1.91 +    metalview = [[SDL_cocoametalview alloc] initWithFrame:view.frame highDPI:highDPI];
    1.92      [view addSubview:metalview];
    1.93      return metalview;
    1.94  }