sdl

FORK: Simple Directmedia Layer
git clone https://git.neptards.moe/neptards/sdl.git
Log | Files | Refs

SDL_uikitmetalview.m (4303B)


      1 /*
      2  Simple DirectMedia Layer
      3  Copyright (C) 1997-2020 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_VULKAN || SDL_VIDEO_METAL)
     32 
     33 #import "../SDL_sysvideo.h"
     34 #import "SDL_uikitwindow.h"
     35 #import "SDL_uikitmetalview.h"
     36 
     37 
     38 @implementation SDL_uikitmetalview
     39 
     40 /* Returns a Metal-compatible layer. */
     41 + (Class)layerClass
     42 {
     43     return [CAMetalLayer class];
     44 }
     45 
     46 - (instancetype)initWithFrame:(CGRect)frame
     47                         scale:(CGFloat)scale
     48 {
     49     if ((self = [super initWithFrame:frame])) {
     50         self.tag = METALVIEW_TAG;
     51         self.layer.contentsScale = scale;
     52         [self updateDrawableSize];
     53     }
     54 
     55     return self;
     56 }
     57 
     58 /* Set the size of the metal drawables when the view is resized. */
     59 - (void)layoutSubviews
     60 {
     61     [super layoutSubviews];
     62     [self updateDrawableSize];
     63 }
     64 
     65 - (void)updateDrawableSize
     66 {
     67     CGSize size = self.bounds.size;
     68     size.width *= self.layer.contentsScale;
     69     size.height *= self.layer.contentsScale;
     70     ((CAMetalLayer *)self.layer).drawableSize = size;
     71 }
     72 
     73 @end
     74 
     75 SDL_MetalView
     76 UIKit_Metal_CreateView(_THIS, SDL_Window * window)
     77 { @autoreleasepool {
     78     SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
     79     CGFloat scale = 1.0;
     80     SDL_uikitmetalview *metalview;
     81 
     82     if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
     83         /* Set the scale to the natural scale factor of the screen - then
     84          * the backing dimensions of the Metal view will match the pixel
     85          * dimensions of the screen rather than the dimensions in points
     86          * yielding high resolution on retine displays.
     87          */
     88         if ([data.uiwindow.screen respondsToSelector:@selector(nativeScale)]) {
     89             scale = data.uiwindow.screen.nativeScale;
     90         } else {
     91             scale = data.uiwindow.screen.scale;
     92         }
     93     }
     94 
     95     metalview = [[SDL_uikitmetalview alloc] initWithFrame:data.uiwindow.bounds
     96                                                     scale:scale];
     97     [metalview setSDLWindow:window];
     98 
     99     return (void*)CFBridgingRetain(metalview);
    100 }}
    101 
    102 void
    103 UIKit_Metal_DestroyView(_THIS, SDL_MetalView view)
    104 { @autoreleasepool {
    105     SDL_uikitmetalview *metalview = CFBridgingRelease(view);
    106 
    107     if ([metalview isKindOfClass:[SDL_uikitmetalview class]]) {
    108         [metalview setSDLWindow:NULL];
    109     }
    110 }}
    111 
    112 void *
    113 UIKit_Metal_GetLayer(_THIS, SDL_MetalView view)
    114 { @autoreleasepool {
    115     SDL_uikitview *uiview = (__bridge SDL_uikitview *)view;
    116     return (__bridge void *)uiview.layer;
    117 }}
    118 
    119 void
    120 UIKit_Metal_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h)
    121 {
    122     @autoreleasepool {
    123         SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
    124         SDL_uikitview *view = (SDL_uikitview*)data.uiwindow.rootViewController.view;
    125         SDL_uikitmetalview* metalview = [view viewWithTag:METALVIEW_TAG];
    126         if (metalview) {
    127             CAMetalLayer *layer = (CAMetalLayer*)metalview.layer;
    128             assert(layer != NULL);
    129             if (w) {
    130                 *w = layer.drawableSize.width;
    131             }
    132             if (h) {
    133                 *h = layer.drawableSize.height;
    134             }
    135         } else {
    136             SDL_GetWindowSize(window, w, h);
    137         }
    138     }
    139 }
    140 
    141 #endif /* SDL_VIDEO_DRIVER_UIKIT && (SDL_VIDEO_VULKAN || SDL_VIDEO_METAL) */