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.
     1 /*
     2  Simple DirectMedia Layer
     3  Copyright (C) 1997-2017 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 /*
    23  * @author Mark Callow, www.edgewise-consulting.com.
    24  *
    25  * Thanks to Alex Szpakowski, @slime73 on GitHub, for his gist showing
    26  * how to add a CAMetalLayer backed view.
    27  */
    28 
    29 #import "SDL_cocoametalview.h"
    30 
    31 #if SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_COCOA
    32 
    33 #include "SDL_assert.h"
    34 #include "SDL_loadso.h"
    35 #include <dlfcn.h>
    36 
    37 @implementation SDL_cocoametalview
    38 
    39 /* The synthesized getter should be called by super's viewWithTag. */
    40 @synthesize tag = _tag;
    41 
    42 /* Return a Metal-compatible layer. */
    43 + (Class)layerClass
    44 {
    45 	return NSClassFromString(@"CAMetalLayer");
    46 }
    47 
    48 /* Indicate the view wants to draw using a backing layer instead of drawRect. */
    49 -(BOOL) wantsUpdateLayer
    50 {
    51     return YES;
    52 }
    53 
    54 /* When the wantsLayer property is set to YES, this method will be invoked to
    55  * return a layer instance.
    56  */
    57 -(CALayer*) makeBackingLayer
    58 {
    59     return [self.class.layerClass layer];
    60 }
    61 
    62 - (instancetype)initWithFrame:(NSRect)frame
    63                    useHighDPI:(bool)useHighDPI
    64 {
    65 	if ((self = [super initWithFrame:frame])) {
    66     
    67         /* Allow resize. */
    68         self.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
    69         _tag = METALVIEW_TAG;
    70       
    71         _useHighDPI = useHighDPI;
    72         [self updateDrawableSize];
    73 	}
    74   
    75 	return self;
    76 }
    77 
    78 /* Set the size of the metal drawables when the view is resized. */
    79 - (void)resizeSubviewsWithOldSize:(NSSize)oldSize {
    80     [super resizeSubviewsWithOldSize:oldSize];
    81     [self updateDrawableSize];
    82 }
    83 
    84 - (void)updateDrawableSize
    85 {
    86     NSRect bounds = [self bounds];
    87     if (_useHighDPI) {
    88         bounds = [self convertRectToBacking:bounds];
    89     }
    90     ((CAMetalLayer *) self.layer).drawableSize = NSSizeToCGSize(bounds.size);
    91 }
    92 
    93 @end
    94 
    95 SDL_cocoametalview*
    96 Cocoa_Mtl_AddMetalView(SDL_Window* window)
    97 {
    98     SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
    99     NSView *view = data->nswindow.contentView;
   100 
   101     SDL_cocoametalview *metalview
   102         = [[SDL_cocoametalview alloc] initWithFrame:view.frame
   103                        useHighDPI:(window->flags & SDL_WINDOW_ALLOW_HIGHDPI)];
   104     // Instantiate the CAMetalLayer
   105     metalview.wantsLayer = YES;
   106     [view addSubview:metalview];
   107     return metalview;
   108 }
   109 
   110 void
   111 Cocoa_Mtl_GetDrawableSize(SDL_Window * window, int * w, int * h)
   112 {
   113     SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
   114     NSView *view = data->nswindow.contentView;
   115     SDL_cocoametalview* metalview = [view viewWithTag:METALVIEW_TAG];
   116     if (metalview) {
   117         CAMetalLayer *layer = (CAMetalLayer*)metalview.layer;
   118         assert(layer != NULL);
   119         if (w) {
   120             *w = layer.drawableSize.width;
   121         }
   122         if (h) {
   123             *h = layer.drawableSize.height;
   124         }
   125     }
   126 }
   127 
   128 #endif /* SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_COCOA */
   129 
   130 /* vi: set ts=4 sw=4 expandtab: */