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