src/video/uikit/SDL_uikitmetalview.m
author Ryan C. Gordon <icculus@icculus.org>
Sun, 27 May 2018 20:30:03 -0400
changeset 12002 9116d02d3feb
parent 11918 c087b18ba812
child 12320 758d4e1222a7
permissions -rw-r--r--
metal: contrary to documentation, we need to set the drawableSize explicitly.

Fixes Bugzilla #4149.
     1 /*
     2  Simple DirectMedia Layer
     3  Copyright (C) 1997-2018 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_DRIVER_UIKIT && (SDL_VIDEO_RENDER_METAL || SDL_VIDEO_VULKAN)
    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 {
    50     if ((self = [super initWithFrame:frame])) {
    51         self.tag = METALVIEW_TAG;
    52         /* Set the desired scale. */
    53         ((CAMetalLayer *) self.layer).drawableSize = self.bounds.size;
    54         self.layer.contentsScale = scale;
    55     }
    56 
    57     return self;
    58 }
    59 
    60 /* Set the size of the metal drawables when the view is resized. */
    61 - (void)layoutSubviews
    62 {
    63     CGSize bounds;
    64 
    65     [super layoutSubviews];
    66 
    67     bounds = [self bounds].size;
    68     bounds.width *= self.layer.contentsScale;
    69     bounds.height *= self.layer.contentsScale;
    70     ((CAMetalLayer *) self.layer).drawableSize = bounds;
    71 }
    72 
    73 @end
    74 
    75 SDL_uikitmetalview*
    76 UIKit_Mtl_AddMetalView(SDL_Window* window)
    77 {
    78     SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
    79     SDL_uikitview *view = (SDL_uikitview*)data.uiwindow.rootViewController.view;
    80     CGFloat scale = 1.0;
    81 
    82     if ([view isKindOfClass:[SDL_uikitmetalview class]]) {
    83         return (SDL_uikitmetalview *)view;
    84     }
    85 
    86     if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
    87         /* Set the scale to the natural scale factor of the screen - then
    88          * the backing dimensions of the Metal view will match the pixel
    89          * dimensions of the screen rather than the dimensions in points
    90          * yielding high resolution on retine displays.
    91          */
    92 #ifdef __IPHONE_8_0
    93         if ([data.uiwindow.screen respondsToSelector:@selector(nativeScale)]) {
    94             scale = data.uiwindow.screen.nativeScale;
    95         } else
    96 #endif
    97         {
    98             scale = data.uiwindow.screen.scale;
    99         }
   100     }
   101     SDL_uikitmetalview *metalview
   102          = [[SDL_uikitmetalview alloc] initWithFrame:view.frame
   103                                                scale:scale];
   104     [metalview setSDLWindow:window];
   105 
   106     return metalview;
   107 }
   108 
   109 void
   110 UIKit_Mtl_GetDrawableSize(SDL_Window * window, int * w, int * h)
   111 {
   112     @autoreleasepool {
   113         SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
   114         SDL_uikitview *view = (SDL_uikitview*)data.uiwindow.rootViewController.view;
   115         SDL_uikitmetalview* 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         } else {
   126             SDL_GetWindowSize(window, w, h);
   127         }
   128     }
   129 }
   130 
   131 #endif /* SDL_VIDEO_DRIVER_UIKIT && (SDL_VIDEO_RENDER_METAL || SDL_VIDEO_VULKAN) */