src/video/cocoa/SDL_cocoametalview.m
author Sam Lantinga <slouken@libsdl.org>
Wed, 03 Jan 2018 11:31:42 -0800
changeset 11816 dea9cf23bad5
parent 11811 5d94cb6b24d3
child 11886 27d81cb6e6af
permissions -rw-r--r--
Fixed spacing in copyright headers
icculus@11365
     1
/*
slouken@11816
     2
  Simple DirectMedia Layer
slouken@11816
     3
  Copyright (C) 1997-2018 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
/* The synthesized getter should be called by super's viewWithTag. */
icculus@11365
    37
@synthesize tag = _tag;
icculus@11365
    38
icculus@11365
    39
/* Return a Metal-compatible layer. */
icculus@11365
    40
+ (Class)layerClass
icculus@11365
    41
{
slime73@11435
    42
	return NSClassFromString(@"CAMetalLayer");
icculus@11365
    43
}
icculus@11365
    44
icculus@11365
    45
/* Indicate the view wants to draw using a backing layer instead of drawRect. */
slime73@11797
    46
- (BOOL)wantsUpdateLayer
slime73@11435
    47
{
slime73@11435
    48
    return YES;
slime73@11435
    49
}
icculus@11365
    50
icculus@11365
    51
/* When the wantsLayer property is set to YES, this method will be invoked to
icculus@11365
    52
 * return a layer instance.
icculus@11365
    53
 */
slime73@11797
    54
- (CALayer*)makeBackingLayer
slime73@11435
    55
{
slime73@11435
    56
    return [self.class.layerClass layer];
slime73@11435
    57
}
icculus@11365
    58
icculus@11365
    59
- (instancetype)initWithFrame:(NSRect)frame
icculus@11365
    60
                   useHighDPI:(bool)useHighDPI
icculus@11365
    61
{
slime73@11435
    62
	if ((self = [super initWithFrame:frame])) {
slime73@11809
    63
        self.wantsLayer = YES;
slime73@11809
    64
slime73@11435
    65
        /* Allow resize. */
slime73@11435
    66
        self.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
slime73@11435
    67
        _tag = METALVIEW_TAG;
slime73@11809
    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. */
slime73@11797
    77
- (void)resizeWithOldSuperviewSize:(NSSize)oldSize
slime73@11797
    78
{
slime73@11797
    79
    [super resizeWithOldSuperviewSize:oldSize];
icculus@11365
    80
    [self updateDrawableSize];
icculus@11365
    81
}
icculus@11365
    82
icculus@11365
    83
- (void)updateDrawableSize
icculus@11365
    84
{
slime73@11435
    85
    NSRect bounds = [self bounds];
icculus@11365
    86
    if (_useHighDPI) {
slime73@11435
    87
        bounds = [self convertRectToBacking:bounds];
icculus@11365
    88
    }
slime73@11435
    89
    ((CAMetalLayer *) self.layer).drawableSize = NSSizeToCGSize(bounds.size);
icculus@11365
    90
}
icculus@11365
    91
icculus@11365
    92
@end
icculus@11365
    93
icculus@11365
    94
SDL_cocoametalview*
icculus@11365
    95
Cocoa_Mtl_AddMetalView(SDL_Window* window)
icculus@11365
    96
{
icculus@11365
    97
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
icculus@11365
    98
    NSView *view = data->nswindow.contentView;
icculus@11365
    99
icculus@11365
   100
    SDL_cocoametalview *metalview
icculus@11365
   101
        = [[SDL_cocoametalview alloc] initWithFrame:view.frame
icculus@11365
   102
                       useHighDPI:(window->flags & SDL_WINDOW_ALLOW_HIGHDPI)];
icculus@11365
   103
    [view addSubview:metalview];
icculus@11365
   104
    return metalview;
icculus@11365
   105
}
icculus@11365
   106
icculus@11365
   107
void
icculus@11365
   108
Cocoa_Mtl_GetDrawableSize(SDL_Window * window, int * w, int * h)
icculus@11365
   109
{
icculus@11365
   110
    SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
icculus@11365
   111
    NSView *view = data->nswindow.contentView;
icculus@11365
   112
    SDL_cocoametalview* metalview = [view viewWithTag:METALVIEW_TAG];
icculus@11365
   113
    if (metalview) {
icculus@11365
   114
        CAMetalLayer *layer = (CAMetalLayer*)metalview.layer;
icculus@11365
   115
        assert(layer != NULL);
slime73@11435
   116
        if (w) {
icculus@11365
   117
            *w = layer.drawableSize.width;
slime73@11435
   118
        }
slime73@11435
   119
        if (h) {
icculus@11365
   120
            *h = layer.drawableSize.height;
slime73@11435
   121
        }
icculus@11365
   122
    }
icculus@11365
   123
}
icculus@11365
   124
slime73@11797
   125
#endif /* SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_RENDER_METAL) */
slouken@11407
   126
icculus@11365
   127
/* vi: set ts=4 sw=4 expandtab: */