src/video/cocoa/SDL_cocoametalview.m
author Alex Szpakowski <slime73@gmail.com>
Thu, 31 Aug 2017 21:26:13 -0300
changeset 11435 181eadbddae2
parent 11407 ff9e8ae4d4fb
child 11507 071deb801690
permissions -rw-r--r--
Code style cleanup in the Cocoa and UIKit vulkan files.
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
#include "SDL_loadso.h"
icculus@11365
    35
#include <dlfcn.h>
icculus@11365
    36
icculus@11365
    37
@implementation SDL_cocoametalview
icculus@11365
    38
icculus@11365
    39
/* The synthesized getter should be called by super's viewWithTag. */
icculus@11365
    40
@synthesize tag = _tag;
icculus@11365
    41
icculus@11365
    42
/* Return a Metal-compatible layer. */
icculus@11365
    43
+ (Class)layerClass
icculus@11365
    44
{
slime73@11435
    45
	return NSClassFromString(@"CAMetalLayer");
icculus@11365
    46
}
icculus@11365
    47
icculus@11365
    48
/* Indicate the view wants to draw using a backing layer instead of drawRect. */
slime73@11435
    49
-(BOOL) wantsUpdateLayer
slime73@11435
    50
{
slime73@11435
    51
    return YES;
slime73@11435
    52
}
icculus@11365
    53
icculus@11365
    54
/* When the wantsLayer property is set to YES, this method will be invoked to
icculus@11365
    55
 * return a layer instance.
icculus@11365
    56
 */
slime73@11435
    57
-(CALayer*) makeBackingLayer
slime73@11435
    58
{
slime73@11435
    59
    return [self.class.layerClass layer];
slime73@11435
    60
}
icculus@11365
    61
icculus@11365
    62
- (instancetype)initWithFrame:(NSRect)frame
icculus@11365
    63
                   useHighDPI:(bool)useHighDPI
icculus@11365
    64
{
slime73@11435
    65
	if ((self = [super initWithFrame:frame])) {
icculus@11365
    66
    
slime73@11435
    67
        /* Allow resize. */
slime73@11435
    68
        self.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
slime73@11435
    69
        _tag = METALVIEW_TAG;
icculus@11365
    70
      
slime73@11435
    71
        _useHighDPI = useHighDPI;
slime73@11435
    72
        [self updateDrawableSize];
slime73@11435
    73
	}
icculus@11365
    74
  
slime73@11435
    75
	return self;
icculus@11365
    76
}
icculus@11365
    77
icculus@11365
    78
/* Set the size of the metal drawables when the view is resized. */
icculus@11365
    79
- (void)resizeSubviewsWithOldSize:(NSSize)oldSize {
icculus@11365
    80
    [super resizeSubviewsWithOldSize: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)];
slouken@11388
   104
    // Instantiate the CAMetalLayer
slouken@11388
   105
    metalview.wantsLayer = YES;
icculus@11365
   106
    [view addSubview:metalview];
icculus@11365
   107
    return metalview;
icculus@11365
   108
}
icculus@11365
   109
icculus@11365
   110
void
icculus@11365
   111
Cocoa_Mtl_GetDrawableSize(SDL_Window * window, int * w, int * h)
icculus@11365
   112
{
icculus@11365
   113
    SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
icculus@11365
   114
    NSView *view = data->nswindow.contentView;
icculus@11365
   115
    SDL_cocoametalview* metalview = [view viewWithTag:METALVIEW_TAG];
icculus@11365
   116
    if (metalview) {
icculus@11365
   117
        CAMetalLayer *layer = (CAMetalLayer*)metalview.layer;
icculus@11365
   118
        assert(layer != NULL);
slime73@11435
   119
        if (w) {
icculus@11365
   120
            *w = layer.drawableSize.width;
slime73@11435
   121
        }
slime73@11435
   122
        if (h) {
icculus@11365
   123
            *h = layer.drawableSize.height;
slime73@11435
   124
        }
icculus@11365
   125
    }
icculus@11365
   126
}
icculus@11365
   127
slouken@11407
   128
#endif /* SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_COCOA */
slouken@11407
   129
icculus@11365
   130
/* vi: set ts=4 sw=4 expandtab: */