src/video/cocoa/SDL_cocoametalview.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 28 Aug 2017 02:30:41 -0700
changeset 11388 16b174adc603
parent 11367 e8fc755a89e8
child 11402 d459d8934897
permissions -rw-r--r--
Instantiate the CAMetalLayer so SDL_Vulkan_CreateSurface() doesn't fail
Error message was:
[mvk-info] MoltenVK version 0.18.2. Vulkan version 1.0.51.
[***MoltenVK ERROR***] VK_ERROR_INITIALIZATION_FAILED: On-screen rendering requires a view that is backed by a layer of type CAMetalLayer.
2017-08-28 02:17:29.579 testvulkan[95627:1716939] ERROR: SDL_Vulkan_CreateSurface(): vkCreateMacOSSurfaceMVK failed: VK_ERROR_INITIALIZATION_FAILED
     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     // Instantiate the CAMetalLayer
    98     metalview.wantsLayer = YES;
    99     [view addSubview:metalview];
   100     return metalview;
   101 }
   102 
   103 void
   104 Cocoa_Mtl_GetDrawableSize(SDL_Window * window, int * w, int * h)
   105 {
   106     SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
   107     NSView *view = data->nswindow.contentView;
   108     SDL_cocoametalview* metalview = [view viewWithTag:METALVIEW_TAG];
   109     if (metalview) {
   110         CAMetalLayer *layer = (CAMetalLayer*)metalview.layer;
   111         assert(layer != NULL);
   112         if (w)
   113             *w = layer.drawableSize.width;
   114         if (h)
   115             *h = layer.drawableSize.height;
   116     }
   117 }
   118 
   119 /* vi: set ts=4 sw=4 expandtab: */
   120