src/video/cocoa/SDL_cocoametalview.m
author Sam Lantinga <slouken@libsdl.org>
Fri, 04 Jan 2019 22:01:14 -0800
changeset 12503 806492103856
parent 12320 758d4e1222a7
child 12797 a7ce9dc7a31f
permissions -rw-r--r--
Updated copyright for 2019
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@11797
    30
#if SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_RENDER_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@12320
    80
    CGSize size = self.bounds.size;
slime73@12320
    81
    CGSize 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@12320
    91
    metalLayer.drawableSize = 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
icculus@11365
   103
SDL_cocoametalview*
icculus@11365
   104
Cocoa_Mtl_AddMetalView(SDL_Window* window)
icculus@11365
   105
{
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@12320
   109
    SDL_cocoametalview *metalview;
icculus@11365
   110
slime73@12320
   111
    metalview = [[SDL_cocoametalview alloc] initWithFrame:view.frame highDPI:highDPI];
icculus@11365
   112
    [view addSubview:metalview];
icculus@11365
   113
    return metalview;
icculus@11365
   114
}
icculus@11365
   115
icculus@11365
   116
void
icculus@11365
   117
Cocoa_Mtl_GetDrawableSize(SDL_Window * window, int * w, int * h)
icculus@11365
   118
{
icculus@11365
   119
    SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
icculus@11365
   120
    NSView *view = data->nswindow.contentView;
icculus@11365
   121
    SDL_cocoametalview* metalview = [view viewWithTag:METALVIEW_TAG];
icculus@11365
   122
    if (metalview) {
icculus@11365
   123
        CAMetalLayer *layer = (CAMetalLayer*)metalview.layer;
icculus@11365
   124
        assert(layer != NULL);
slime73@11435
   125
        if (w) {
icculus@11365
   126
            *w = layer.drawableSize.width;
slime73@11435
   127
        }
slime73@11435
   128
        if (h) {
icculus@11365
   129
            *h = layer.drawableSize.height;
slime73@11435
   130
        }
libsdl@11886
   131
    } else {
libsdl@11886
   132
        SDL_GetWindowSize(window, w, h);
icculus@11365
   133
    }
icculus@11365
   134
}
icculus@11365
   135
slime73@11797
   136
#endif /* SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_RENDER_METAL) */
slouken@11407
   137
icculus@11365
   138
/* vi: set ts=4 sw=4 expandtab: */