src/video/cocoa/SDL_cocoametalview.m
author Mark Callow <libsdl.org@callow.im>
Thu, 21 Sep 2017 14:01:12 +0900
changeset 11507 071deb801690
parent 11435 181eadbddae2
child 11797 4da70548aa06
permissions -rw-r--r--
macOS: remove unneeded #includes.
icculus@11365
     1
/*
icculus@11365
     2
 Simple DirectMedia Layer
icculus@11365
     3
 Copyright (C) 1997-2017 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
slouken@11407
    31
#if SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_COCOA
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@11435
    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@11435
    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])) {
icculus@11365
    64
    
slime73@11435
    65
        /* Allow resize. */
slime73@11435
    66
        self.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
slime73@11435
    67
        _tag = METALVIEW_TAG;
icculus@11365
    68
      
slime73@11435
    69
        _useHighDPI = useHighDPI;
slime73@11435
    70
        [self updateDrawableSize];
slime73@11435
    71
	}
icculus@11365
    72
  
slime73@11435
    73
	return self;
icculus@11365
    74
}
icculus@11365
    75
icculus@11365
    76
/* Set the size of the metal drawables when the view is resized. */
icculus@11365
    77
- (void)resizeSubviewsWithOldSize:(NSSize)oldSize {
icculus@11365
    78
    [super resizeSubviewsWithOldSize:oldSize];
icculus@11365
    79
    [self updateDrawableSize];
icculus@11365
    80
}
icculus@11365
    81
icculus@11365
    82
- (void)updateDrawableSize
icculus@11365
    83
{
slime73@11435
    84
    NSRect bounds = [self bounds];
icculus@11365
    85
    if (_useHighDPI) {
slime73@11435
    86
        bounds = [self convertRectToBacking:bounds];
icculus@11365
    87
    }
slime73@11435
    88
    ((CAMetalLayer *) self.layer).drawableSize = NSSizeToCGSize(bounds.size);
icculus@11365
    89
}
icculus@11365
    90
icculus@11365
    91
@end
icculus@11365
    92
icculus@11365
    93
SDL_cocoametalview*
icculus@11365
    94
Cocoa_Mtl_AddMetalView(SDL_Window* window)
icculus@11365
    95
{
icculus@11365
    96
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
icculus@11365
    97
    NSView *view = data->nswindow.contentView;
icculus@11365
    98
icculus@11365
    99
    SDL_cocoametalview *metalview
icculus@11365
   100
        = [[SDL_cocoametalview alloc] initWithFrame:view.frame
icculus@11365
   101
                       useHighDPI:(window->flags & SDL_WINDOW_ALLOW_HIGHDPI)];
slouken@11388
   102
    // Instantiate the CAMetalLayer
slouken@11388
   103
    metalview.wantsLayer = YES;
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
slouken@11407
   126
#endif /* SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_COCOA */
slouken@11407
   127
icculus@11365
   128
/* vi: set ts=4 sw=4 expandtab: */