src/video/cocoa/SDL_cocoametalview.m
author Sam Lantinga <slouken@libsdl.org>
Wed, 03 Jan 2018 10:03:25 -0800
changeset 11811 5d94cb6b24d3
parent 11809 4b858abfb24d
child 11816 dea9cf23bad5
permissions -rw-r--r--
Updated copyright for 2018
icculus@11365
     1
/*
icculus@11365
     2
 Simple DirectMedia Layer
slouken@11811
     3
 Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
icculus@11365
     4
 
icculus@11365
     5
 This software is provided 'as-is', without any express or implied
icculus@11365
     6
 warranty.  In no event will the authors be held liable for any damages
icculus@11365
     7
 arising from the use of this software.
icculus@11365
     8
 
icculus@11365
     9
 Permission is granted to anyone to use this software for any purpose,
icculus@11365
    10
 including commercial applications, and to alter it and redistribute it
icculus@11365
    11
 freely, subject to the following restrictions:
icculus@11365
    12
 
icculus@11365
    13
 1. The origin of this software must not be misrepresented; you must not
icculus@11365
    14
 claim that you wrote the original software. If you use this software
icculus@11365
    15
 in a product, an acknowledgment in the product documentation would be
icculus@11365
    16
 appreciated but is not required.
icculus@11365
    17
 2. Altered source versions must be plainly marked as such, and must not be
icculus@11365
    18
 misrepresented as being the original software.
icculus@11365
    19
 3. This notice may not be removed or altered from any source distribution.
icculus@11365
    20
 */
icculus@11365
    21
icculus@11365
    22
/*
icculus@11365
    23
 * @author Mark Callow, www.edgewise-consulting.com.
icculus@11365
    24
 *
icculus@11365
    25
 * Thanks to Alex Szpakowski, @slime73 on GitHub, for his gist showing
icculus@11365
    26
 * how to add a CAMetalLayer backed view.
icculus@11365
    27
 */
icculus@11365
    28
icculus@11365
    29
#import "SDL_cocoametalview.h"
icculus@11365
    30
slime73@11797
    31
#if SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_RENDER_METAL)
slouken@11407
    32
icculus@11365
    33
#include "SDL_assert.h"
icculus@11365
    34
icculus@11365
    35
@implementation SDL_cocoametalview
icculus@11365
    36
icculus@11365
    37
/* The synthesized getter should be called by super's viewWithTag. */
icculus@11365
    38
@synthesize tag = _tag;
icculus@11365
    39
icculus@11365
    40
/* Return a Metal-compatible layer. */
icculus@11365
    41
+ (Class)layerClass
icculus@11365
    42
{
slime73@11435
    43
	return NSClassFromString(@"CAMetalLayer");
icculus@11365
    44
}
icculus@11365
    45
icculus@11365
    46
/* Indicate the view wants to draw using a backing layer instead of drawRect. */
slime73@11797
    47
- (BOOL)wantsUpdateLayer
slime73@11435
    48
{
slime73@11435
    49
    return YES;
slime73@11435
    50
}
icculus@11365
    51
icculus@11365
    52
/* When the wantsLayer property is set to YES, this method will be invoked to
icculus@11365
    53
 * return a layer instance.
icculus@11365
    54
 */
slime73@11797
    55
- (CALayer*)makeBackingLayer
slime73@11435
    56
{
slime73@11435
    57
    return [self.class.layerClass layer];
slime73@11435
    58
}
icculus@11365
    59
icculus@11365
    60
- (instancetype)initWithFrame:(NSRect)frame
icculus@11365
    61
                   useHighDPI:(bool)useHighDPI
icculus@11365
    62
{
slime73@11435
    63
	if ((self = [super initWithFrame:frame])) {
slime73@11809
    64
        self.wantsLayer = YES;
slime73@11809
    65
slime73@11435
    66
        /* Allow resize. */
slime73@11435
    67
        self.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
slime73@11435
    68
        _tag = METALVIEW_TAG;
slime73@11809
    69
slime73@11435
    70
        _useHighDPI = useHighDPI;
slime73@11435
    71
        [self updateDrawableSize];
slime73@11435
    72
	}
icculus@11365
    73
  
slime73@11435
    74
	return self;
icculus@11365
    75
}
icculus@11365
    76
icculus@11365
    77
/* Set the size of the metal drawables when the view is resized. */
slime73@11797
    78
- (void)resizeWithOldSuperviewSize:(NSSize)oldSize
slime73@11797
    79
{
slime73@11797
    80
    [super resizeWithOldSuperviewSize:oldSize];
icculus@11365
    81
    [self updateDrawableSize];
icculus@11365
    82
}
icculus@11365
    83
icculus@11365
    84
- (void)updateDrawableSize
icculus@11365
    85
{
slime73@11435
    86
    NSRect bounds = [self bounds];
icculus@11365
    87
    if (_useHighDPI) {
slime73@11435
    88
        bounds = [self convertRectToBacking:bounds];
icculus@11365
    89
    }
slime73@11435
    90
    ((CAMetalLayer *) self.layer).drawableSize = NSSizeToCGSize(bounds.size);
icculus@11365
    91
}
icculus@11365
    92
icculus@11365
    93
@end
icculus@11365
    94
icculus@11365
    95
SDL_cocoametalview*
icculus@11365
    96
Cocoa_Mtl_AddMetalView(SDL_Window* window)
icculus@11365
    97
{
icculus@11365
    98
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
icculus@11365
    99
    NSView *view = data->nswindow.contentView;
icculus@11365
   100
icculus@11365
   101
    SDL_cocoametalview *metalview
icculus@11365
   102
        = [[SDL_cocoametalview alloc] initWithFrame:view.frame
icculus@11365
   103
                       useHighDPI:(window->flags & SDL_WINDOW_ALLOW_HIGHDPI)];
icculus@11365
   104
    [view addSubview:metalview];
icculus@11365
   105
    return metalview;
icculus@11365
   106
}
icculus@11365
   107
icculus@11365
   108
void
icculus@11365
   109
Cocoa_Mtl_GetDrawableSize(SDL_Window * window, int * w, int * h)
icculus@11365
   110
{
icculus@11365
   111
    SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
icculus@11365
   112
    NSView *view = data->nswindow.contentView;
icculus@11365
   113
    SDL_cocoametalview* metalview = [view viewWithTag:METALVIEW_TAG];
icculus@11365
   114
    if (metalview) {
icculus@11365
   115
        CAMetalLayer *layer = (CAMetalLayer*)metalview.layer;
icculus@11365
   116
        assert(layer != NULL);
slime73@11435
   117
        if (w) {
icculus@11365
   118
            *w = layer.drawableSize.width;
slime73@11435
   119
        }
slime73@11435
   120
        if (h) {
icculus@11365
   121
            *h = layer.drawableSize.height;
slime73@11435
   122
        }
icculus@11365
   123
    }
icculus@11365
   124
}
icculus@11365
   125
slime73@11797
   126
#endif /* SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_RENDER_METAL) */
slouken@11407
   127
icculus@11365
   128
/* vi: set ts=4 sw=4 expandtab: */