src/video/uikit/SDL_uikitmetalview.m
author Cameron Gutman <aicommander@gmail.com>
Sun, 28 Apr 2019 17:37:49 -0700
changeset 12746 132a2af7edac
parent 12707 8703488687ca
permissions -rw-r--r--
Fix use-after-free when pumping the event loop after SDL_DestroyWindow()

Closing the window is asynchronous, but we free the window data immediately,
so we can get an updateLayer callback before the window is really destroyed which
will cause us to access the freed memory.

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