src/video/uikit/SDL_uikitmetalview.m
author Sam Lantinga <slouken@libsdl.org>
Thu, 07 Dec 2017 17:12:03 -0800
changeset 11732 ad13456d6e7f
parent 11728 2f3ea7f5c671
child 11733 490588c02a65
permissions -rw-r--r--
Fixed compiling Metal renderer on iOS
     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 #include "../../SDL_internal.h"
    30 
    31 #if SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_UIKIT
    32 
    33 #import "../SDL_sysvideo.h"
    34 #import "SDL_uikitwindow.h"
    35 #import "SDL_uikitmetalview.h"
    36 
    37 #include "SDL_assert.h"
    38 
    39 @implementation SDL_uikitmetalview
    40 
    41 /* Returns a Metal-compatible layer. */
    42 + (Class)layerClass
    43 {
    44     return [CAMetalLayer class];
    45 }
    46 
    47 - (instancetype)initWithFrame:(CGRect)frame
    48                         scale:(CGFloat)scale
    49                           tag:(int)tag
    50 {
    51     if ((self = [super initWithFrame:frame])) {
    52         /* Set the appropriate scale (for retina display support) */
    53         self.contentScaleFactor = scale;
    54         self.tag = tag;
    55 
    56         [self updateDrawableSize];
    57     }
    58 
    59     return self;
    60 }
    61 
    62 /* Set the size of the metal drawables when the view is resized. */
    63 - (void)layoutSubviews
    64 {
    65     [super layoutSubviews];
    66     [self updateDrawableSize];
    67 }
    68 
    69 - (void)updateDrawableSize
    70 {
    71     CGSize size  = self.bounds.size;
    72     size.width  *= self.contentScaleFactor;
    73     size.height *= self.contentScaleFactor;
    74 
    75     ((CAMetalLayer *) self.layer).drawableSize = size;
    76 }
    77 
    78 @end
    79 
    80 SDL_uikitmetalview*
    81 UIKit_Mtl_AddMetalView(SDL_Window* window)
    82 {
    83     SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
    84     SDL_uikitview *view = (SDL_uikitview*)data.uiwindow.rootViewController.view;
    85     CGFloat scale = 1.0;
    86 
    87 	if ([view isKindOfClass:[SDL_uikitmetalview class]]) {
    88 		return (SDL_uikitmetalview *)view;
    89 	}
    90 
    91     if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
    92         /* Set the scale to the natural scale factor of the screen - the
    93          * backing dimensions of the Metal view will match the pixel
    94          * dimensions of the screen rather than the dimensions in points.
    95          */
    96 #ifdef __IPHONE_8_0
    97         if ([data.uiwindow.screen respondsToSelector:@selector(nativeScale)]) {
    98             scale = data.uiwindow.screen.nativeScale;
    99         } else
   100 #endif
   101         {
   102             scale = data.uiwindow.screen.scale;
   103         }
   104     }
   105     SDL_uikitmetalview *metalview
   106          = [[SDL_uikitmetalview alloc] initWithFrame:view.frame
   107                                           scale:scale
   108                                             tag:METALVIEW_TAG];
   109     [metalview setSDLWindow:window];
   110 
   111     return metalview;
   112 }
   113 
   114 void
   115 UIKit_Mtl_GetDrawableSize(SDL_Window * window, int * w, int * h)
   116 {
   117     @autoreleasepool {
   118         SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
   119         SDL_uikitview *view = (SDL_uikitview*)data.uiwindow.rootViewController.view;
   120         SDL_uikitmetalview* metalview = [view viewWithTag:METALVIEW_TAG];
   121         if (metalview) {
   122             CAMetalLayer *layer = (CAMetalLayer*)metalview.layer;
   123             assert(layer != NULL);
   124             if (w) {
   125                 *w = layer.drawableSize.width;
   126             }
   127             if (h) {
   128                 *h = layer.drawableSize.height;
   129             }
   130         } else {
   131             SDL_GetWindowSize(window, w, h);
   132         }
   133     }
   134 }
   135 
   136 #endif