src/video/cocoa/SDL_cocoametalview.m
author Alex Szpakowski <slime73@gmail.com>
Thu, 22 Aug 2019 19:23:52 -0300
changeset 13029 3e281c2da43a
parent 12998 b87ba0fad17e
child 13113 eab802de5fbb
permissions -rw-r--r--
macOS: Use the proper type (NSSize instead of CGSize) for the bounds of metal views.
icculus@11365
     1
/*
slouken@11816
     2
  Simple DirectMedia Layer
slouken@12503
     3
  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
icculus@11365
     4
slouken@11816
     5
  This software is provided 'as-is', without any express or implied
slouken@11816
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@11816
     7
  arising from the use of this software.
slouken@11816
     8
slouken@11816
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@11816
    10
  including commercial applications, and to alter it and redistribute it
slouken@11816
    11
  freely, subject to the following restrictions:
slouken@11816
    12
slouken@11816
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@11816
    14
     claim that you wrote the original software. If you use this software
slouken@11816
    15
     in a product, an acknowledgment in the product documentation would be
slouken@11816
    16
     appreciated but is not required.
slouken@11816
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@11816
    18
     misrepresented as being the original software.
slouken@11816
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@11816
    20
*/
icculus@11365
    21
/*
icculus@11365
    22
 * @author Mark Callow, www.edgewise-consulting.com.
icculus@11365
    23
 *
icculus@11365
    24
 * Thanks to Alex Szpakowski, @slime73 on GitHub, for his gist showing
icculus@11365
    25
 * how to add a CAMetalLayer backed view.
icculus@11365
    26
 */
icculus@11365
    27
icculus@11365
    28
#import "SDL_cocoametalview.h"
icculus@11365
    29
slime73@12998
    30
#if SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_METAL)
slouken@11407
    31
icculus@11365
    32
#include "SDL_assert.h"
icculus@11365
    33
icculus@11365
    34
@implementation SDL_cocoametalview
icculus@11365
    35
icculus@11365
    36
/* Return a Metal-compatible layer. */
icculus@11365
    37
+ (Class)layerClass
icculus@11365
    38
{
slouken@12201
    39
    return NSClassFromString(@"CAMetalLayer");
icculus@11365
    40
}
icculus@11365
    41
icculus@11365
    42
/* Indicate the view wants to draw using a backing layer instead of drawRect. */
slime73@11797
    43
- (BOOL)wantsUpdateLayer
slime73@11435
    44
{
slime73@11435
    45
    return YES;
slime73@11435
    46
}
icculus@11365
    47
icculus@11365
    48
/* When the wantsLayer property is set to YES, this method will be invoked to
icculus@11365
    49
 * return a layer instance.
icculus@11365
    50
 */
slime73@11797
    51
- (CALayer*)makeBackingLayer
slime73@11435
    52
{
slime73@11435
    53
    return [self.class.layerClass layer];
slime73@11435
    54
}
icculus@11365
    55
icculus@11365
    56
- (instancetype)initWithFrame:(NSRect)frame
slime73@12320
    57
                      highDPI:(BOOL)highDPI
icculus@11365
    58
{
slouken@12201
    59
    if ((self = [super initWithFrame:frame])) {
slime73@12320
    60
        self.highDPI = highDPI;
slime73@11809
    61
        self.wantsLayer = YES;
slime73@11809
    62
slime73@11435
    63
        /* Allow resize. */
slime73@11435
    64
        self.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
slime73@11809
    65
slime73@12320
    66
        [self updateDrawableSize];
icculus@12002
    67
    }
icculus@11365
    68
  
slouken@12201
    69
    return self;
icculus@11365
    70
}
icculus@11365
    71
slime73@12320
    72
- (NSInteger)tag
slime73@12320
    73
{
slime73@12320
    74
    return METALVIEW_TAG;
slime73@12320
    75
}
slime73@12320
    76
slime73@12320
    77
- (void)updateDrawableSize
slime73@12320
    78
{
slime73@12320
    79
    CAMetalLayer *metalLayer = (CAMetalLayer *)self.layer;
slime73@13029
    80
    NSSize size = self.bounds.size;
slime73@13029
    81
    NSSize backingSize = size;
slime73@12320
    82
slime73@12320
    83
    if (self.highDPI) {
slime73@12320
    84
        /* Note: NSHighResolutionCapable must be set to true in the app's
slime73@12320
    85
         * Info.plist in order for the backing size to be high res.
slime73@12320
    86
         */
slime73@12320
    87
        backingSize = [self convertSizeToBacking:size];
slime73@12320
    88
    }
slime73@12320
    89
slime73@12320
    90
    metalLayer.contentsScale = backingSize.height / size.height;
slime73@13029
    91
    metalLayer.drawableSize = NSSizeToCGSize(backingSize);
slime73@12320
    92
}
slime73@12320
    93
icculus@11365
    94
/* Set the size of the metal drawables when the view is resized. */
slime73@11797
    95
- (void)resizeWithOldSuperviewSize:(NSSize)oldSize
slime73@11797
    96
{
slime73@11797
    97
    [super resizeWithOldSuperviewSize:oldSize];
slime73@12320
    98
    [self updateDrawableSize];
icculus@11365
    99
}
icculus@11365
   100
icculus@11365
   101
@end
icculus@11365
   102
slime73@12998
   103
SDL_MetalView
slime73@12998
   104
Cocoa_Metal_CreateView(_THIS, SDL_Window * window)
slime73@12998
   105
{ @autoreleasepool {
libsdl@11886
   106
    SDL_WindowData* data = (__bridge SDL_WindowData *)window->driverdata;
icculus@11365
   107
    NSView *view = data->nswindow.contentView;
slime73@12320
   108
    BOOL highDPI = (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) != 0;
slime73@12998
   109
    SDL_cocoametalview *newview;
slime73@12998
   110
    SDL_MetalView metalview;
icculus@11365
   111
slime73@12998
   112
    newview = [[SDL_cocoametalview alloc] initWithFrame:view.frame highDPI:highDPI];
slime73@12998
   113
    if (newview == nil) {
slime73@12998
   114
        return NULL;
slime73@12998
   115
    }
slime73@12998
   116
slime73@12998
   117
    [view addSubview:newview];
slime73@12998
   118
slime73@12998
   119
    metalview = (SDL_MetalView)CFBridgingRetain(newview);
slime73@12998
   120
    [newview release];
slime73@12998
   121
icculus@11365
   122
    return metalview;
slime73@12998
   123
}}
icculus@11365
   124
icculus@11365
   125
void
slime73@12998
   126
Cocoa_Metal_DestroyView(_THIS, SDL_MetalView view)
slime73@12998
   127
{ @autoreleasepool {
slime73@12998
   128
    SDL_cocoametalview *metalview = CFBridgingRelease(view);
slime73@12998
   129
    [metalview removeFromSuperview];
slime73@12998
   130
}}
slime73@12998
   131
slime73@12998
   132
void
slime73@12998
   133
Cocoa_Metal_GetDrawableSize(SDL_Window * window, int * w, int * h)
slime73@12998
   134
{ @autoreleasepool {
icculus@11365
   135
    SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
icculus@11365
   136
    NSView *view = data->nswindow.contentView;
icculus@11365
   137
    SDL_cocoametalview* metalview = [view viewWithTag:METALVIEW_TAG];
icculus@11365
   138
    if (metalview) {
icculus@11365
   139
        CAMetalLayer *layer = (CAMetalLayer*)metalview.layer;
flibitijibibo@12797
   140
        SDL_assert(layer != NULL);
slime73@11435
   141
        if (w) {
icculus@11365
   142
            *w = layer.drawableSize.width;
slime73@11435
   143
        }
slime73@11435
   144
        if (h) {
icculus@11365
   145
            *h = layer.drawableSize.height;
slime73@11435
   146
        }
libsdl@11886
   147
    } else {
libsdl@11886
   148
        SDL_GetWindowSize(window, w, h);
icculus@11365
   149
    }
slime73@12998
   150
}}
icculus@11365
   151
slime73@12998
   152
#endif /* SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_METAL) */
slouken@11407
   153
icculus@11365
   154
/* vi: set ts=4 sw=4 expandtab: */