src/video/cocoa/SDL_cocoametalview.m
changeset 11365 a9bd2625fa01
child 11367 e8fc755a89e8
equal deleted inserted replaced
11364:ec27c4fd6880 11365:a9bd2625fa01
       
     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 /* this is (currently) only used with Vulkan. Remove this #if when that changes! */
       
    30 #if SDL_VIDEO_VULKAN_SURFACE && SDL_VIDEO_DRIVER_COCOA
       
    31 
       
    32 #import "SDL_cocoametalview.h"
       
    33 
       
    34 #include "SDL_assert.h"
       
    35 #include "SDL_loadso.h"
       
    36 #include <dlfcn.h>
       
    37 
       
    38 @implementation SDL_cocoametalview
       
    39 
       
    40 /* The synthesized getter should be called by super's viewWithTag. */
       
    41 @synthesize tag = _tag;
       
    42 
       
    43 /* Return a Metal-compatible layer. */
       
    44 + (Class)layerClass
       
    45 {
       
    46   return [CAMetalLayer class];
       
    47 }
       
    48 
       
    49 /* Indicate the view wants to draw using a backing layer instead of drawRect. */
       
    50 -(BOOL) wantsUpdateLayer { return YES; }
       
    51 
       
    52 /* When the wantsLayer property is set to YES, this method will be invoked to
       
    53  * return a layer instance.
       
    54  */
       
    55 -(CALayer*) makeBackingLayer { return [self.class.layerClass layer]; }
       
    56 
       
    57 - (instancetype)initWithFrame:(NSRect)frame
       
    58                    useHighDPI:(bool)useHighDPI
       
    59 {
       
    60   if ((self = [super initWithFrame:frame])) {
       
    61     
       
    62     /* Allow resize. */
       
    63     self.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
       
    64     _tag = METALVIEW_TAG;
       
    65       
       
    66     _useHighDPI = useHighDPI;
       
    67     [self updateDrawableSize];
       
    68   }
       
    69   
       
    70   return self;
       
    71 }
       
    72 
       
    73 /* Set the size of the metal drawables when the view is resized. */
       
    74 - (void)resizeSubviewsWithOldSize:(NSSize)oldSize {
       
    75     [super resizeSubviewsWithOldSize:oldSize];
       
    76     [self updateDrawableSize];
       
    77 }
       
    78 
       
    79 - (void)updateDrawableSize
       
    80 {
       
    81     if (_useHighDPI) {
       
    82         NSSize size = [self convertRectToBacking:[self bounds]].size;
       
    83          /* Isn't there a better way to convert from NSSize to CGSize? */
       
    84         CGSize cgsize = *(CGSize*)&size;
       
    85         ((CAMetalLayer *) self.layer).drawableSize = cgsize;
       
    86     }
       
    87 }
       
    88 
       
    89 @end
       
    90 
       
    91 SDL_cocoametalview*
       
    92 Cocoa_Mtl_AddMetalView(SDL_Window* window)
       
    93 {
       
    94     SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
       
    95     NSView *view = data->nswindow.contentView;
       
    96 
       
    97     SDL_cocoametalview *metalview
       
    98         = [[SDL_cocoametalview alloc] initWithFrame:view.frame
       
    99                        useHighDPI:(window->flags & SDL_WINDOW_ALLOW_HIGHDPI)];
       
   100     [view addSubview:metalview];
       
   101   
       
   102     return metalview;
       
   103 }
       
   104 
       
   105 void
       
   106 Cocoa_Mtl_GetDrawableSize(SDL_Window * window, int * w, int * h)
       
   107 {
       
   108     SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
       
   109     NSView *view = data->nswindow.contentView;
       
   110     SDL_cocoametalview* metalview = [view viewWithTag:METALVIEW_TAG];
       
   111     if (metalview) {
       
   112         CAMetalLayer *layer = (CAMetalLayer*)metalview.layer;
       
   113         assert(layer != NULL);
       
   114         if (w)
       
   115             *w = layer.drawableSize.width;
       
   116         if (h)
       
   117             *h = layer.drawableSize.height;
       
   118     }
       
   119 }
       
   120 
       
   121 #endif
       
   122 
       
   123 /* vi: set ts=4 sw=4 expandtab: */
       
   124