src/video/cocoa/SDL_cocoametalview.m
author Ryan C. Gordon <icculus@icculus.org>
Sun, 27 Aug 2017 23:25:12 -0400
changeset 11367 e8fc755a89e8
parent 11365 a9bd2625fa01
child 11388 16b174adc603
permissions -rw-r--r--
vulkan: Include a copy of vulkan.h and vk_platform.h.

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