src/video/cocoa/SDL_cocoametalview.m
author Sam Lantinga <slouken@libsdl.org>
Thu, 16 Jan 2020 20:49:25 -0800
changeset 13422 fd6a12de91c7
parent 13113 eab802de5fbb
permissions -rw-r--r--
Updated copyright date for 2020
icculus@11365
     1
/*
slouken@11816
     2
  Simple DirectMedia Layer
slouken@13422
     3
  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
icculus@11365
     4
slouken@11816
     5
  This software is provided 'as-is', without any express or implied
slouken@11816
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@11816
     7
  arising from the use of this software.
slouken@11816
     8
slouken@11816
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@11816
    10
  including commercial applications, and to alter it and redistribute it
slouken@11816
    11
  freely, subject to the following restrictions:
slouken@11816
    12
slouken@11816
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@11816
    14
     claim that you wrote the original software. If you use this software
slouken@11816
    15
     in a product, an acknowledgment in the product documentation would be
slouken@11816
    16
     appreciated but is not required.
slouken@11816
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@11816
    18
     misrepresented as being the original software.
slouken@11816
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@11816
    20
*/
icculus@11365
    21
/*
icculus@11365
    22
 * @author Mark Callow, www.edgewise-consulting.com.
icculus@11365
    23
 *
icculus@11365
    24
 * Thanks to Alex Szpakowski, @slime73 on GitHub, for his gist showing
icculus@11365
    25
 * how to add a CAMetalLayer backed view.
icculus@11365
    26
 */
icculus@11365
    27
icculus@11365
    28
#import "SDL_cocoametalview.h"
icculus@11365
    29
slime73@12998
    30
#if SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_METAL)
slouken@11407
    31
icculus@11365
    32
#include "SDL_assert.h"
slime73@13113
    33
#include "SDL_events.h"
slime73@13113
    34
slime73@13113
    35
static int SDLCALL
slime73@13113
    36
SDL_MetalViewEventWatch(void *userdata, SDL_Event *event)
slime73@13113
    37
{
slime73@13113
    38
    /* Update the drawable size when SDL receives a size changed event for
slime73@13113
    39
     * the window that contains the metal view. It would be nice to use
slime73@13113
    40
     * - (void)resizeWithOldSuperviewSize:(NSSize)oldSize and
slime73@13113
    41
     * - (void)viewDidChangeBackingProperties instead, but SDL's size change
slime73@13113
    42
     * events don't always happen in the same frame (for example when a
slime73@13113
    43
     * resizable window exits a fullscreen Space via the user pressing the OS
slime73@13113
    44
     * exit-space button). */
slime73@13113
    45
    if (event->type == SDL_WINDOWEVENT && event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
slime73@13113
    46
        @autoreleasepool {
slime73@13113
    47
            SDL_cocoametalview *view = (__bridge SDL_cocoametalview *)userdata;
slime73@13113
    48
            if (view.sdlWindowID == event->window.windowID) {
slime73@13113
    49
                [view updateDrawableSize];
slime73@13113
    50
            }
slime73@13113
    51
        }
slime73@13113
    52
    }
slime73@13113
    53
    return 0;
slime73@13113
    54
}
icculus@11365
    55
icculus@11365
    56
@implementation SDL_cocoametalview
icculus@11365
    57
icculus@11365
    58
/* Return a Metal-compatible layer. */
icculus@11365
    59
+ (Class)layerClass
icculus@11365
    60
{
slouken@12201
    61
    return NSClassFromString(@"CAMetalLayer");
icculus@11365
    62
}
icculus@11365
    63
icculus@11365
    64
/* Indicate the view wants to draw using a backing layer instead of drawRect. */
slime73@11797
    65
- (BOOL)wantsUpdateLayer
slime73@11435
    66
{
slime73@11435
    67
    return YES;
slime73@11435
    68
}
icculus@11365
    69
icculus@11365
    70
/* When the wantsLayer property is set to YES, this method will be invoked to
icculus@11365
    71
 * return a layer instance.
icculus@11365
    72
 */
slime73@11797
    73
- (CALayer*)makeBackingLayer
slime73@11435
    74
{
slime73@11435
    75
    return [self.class.layerClass layer];
slime73@11435
    76
}
icculus@11365
    77
icculus@11365
    78
- (instancetype)initWithFrame:(NSRect)frame
slime73@12320
    79
                      highDPI:(BOOL)highDPI
slime73@13113
    80
                     windowID:(Uint32)windowID;
icculus@11365
    81
{
slouken@12201
    82
    if ((self = [super initWithFrame:frame])) {
slime73@12320
    83
        self.highDPI = highDPI;
slime73@13113
    84
        self.sdlWindowID = windowID;
slime73@11809
    85
        self.wantsLayer = YES;
slime73@11809
    86
slime73@11435
    87
        /* Allow resize. */
slime73@11435
    88
        self.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
slime73@11809
    89
slime73@13113
    90
        SDL_AddEventWatch(SDL_MetalViewEventWatch, self);
slime73@13113
    91
slime73@12320
    92
        [self updateDrawableSize];
icculus@12002
    93
    }
icculus@11365
    94
  
slouken@12201
    95
    return self;
icculus@11365
    96
}
icculus@11365
    97
slime73@13113
    98
- (void)dealloc
slime73@13113
    99
{
slime73@13113
   100
    SDL_DelEventWatch(SDL_MetalViewEventWatch, self);
slime73@13113
   101
    [super dealloc];
slime73@13113
   102
}
slime73@13113
   103
slime73@12320
   104
- (NSInteger)tag
slime73@12320
   105
{
slime73@12320
   106
    return METALVIEW_TAG;
slime73@12320
   107
}
slime73@12320
   108
slime73@12320
   109
- (void)updateDrawableSize
slime73@12320
   110
{
slime73@12320
   111
    CAMetalLayer *metalLayer = (CAMetalLayer *)self.layer;
slime73@13029
   112
    NSSize size = self.bounds.size;
slime73@13029
   113
    NSSize backingSize = size;
slime73@12320
   114
slime73@12320
   115
    if (self.highDPI) {
slime73@12320
   116
        /* Note: NSHighResolutionCapable must be set to true in the app's
slime73@12320
   117
         * Info.plist in order for the backing size to be high res.
slime73@12320
   118
         */
slime73@12320
   119
        backingSize = [self convertSizeToBacking:size];
slime73@12320
   120
    }
slime73@12320
   121
slime73@12320
   122
    metalLayer.contentsScale = backingSize.height / size.height;
slime73@13029
   123
    metalLayer.drawableSize = NSSizeToCGSize(backingSize);
slime73@12320
   124
}
slime73@12320
   125
icculus@11365
   126
@end
icculus@11365
   127
slime73@12998
   128
SDL_MetalView
slime73@12998
   129
Cocoa_Metal_CreateView(_THIS, SDL_Window * window)
slime73@12998
   130
{ @autoreleasepool {
libsdl@11886
   131
    SDL_WindowData* data = (__bridge SDL_WindowData *)window->driverdata;
icculus@11365
   132
    NSView *view = data->nswindow.contentView;
slime73@12320
   133
    BOOL highDPI = (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) != 0;
slime73@13113
   134
    Uint32 windowID = SDL_GetWindowID(window);
slime73@12998
   135
    SDL_cocoametalview *newview;
slime73@12998
   136
    SDL_MetalView metalview;
icculus@11365
   137
slime73@13113
   138
    newview = [[SDL_cocoametalview alloc] initWithFrame:view.frame
slime73@13113
   139
                                                highDPI:highDPI
slime73@13113
   140
                                                windowID:windowID];
slime73@12998
   141
    if (newview == nil) {
slime73@12998
   142
        return NULL;
slime73@12998
   143
    }
slime73@12998
   144
slime73@12998
   145
    [view addSubview:newview];
slime73@12998
   146
slime73@12998
   147
    metalview = (SDL_MetalView)CFBridgingRetain(newview);
slime73@12998
   148
    [newview release];
slime73@12998
   149
icculus@11365
   150
    return metalview;
slime73@12998
   151
}}
icculus@11365
   152
icculus@11365
   153
void
slime73@12998
   154
Cocoa_Metal_DestroyView(_THIS, SDL_MetalView view)
slime73@12998
   155
{ @autoreleasepool {
slime73@12998
   156
    SDL_cocoametalview *metalview = CFBridgingRelease(view);
slime73@12998
   157
    [metalview removeFromSuperview];
slime73@12998
   158
}}
slime73@12998
   159
slime73@12998
   160
void
slime73@12998
   161
Cocoa_Metal_GetDrawableSize(SDL_Window * window, int * w, int * h)
slime73@12998
   162
{ @autoreleasepool {
icculus@11365
   163
    SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
icculus@11365
   164
    NSView *view = data->nswindow.contentView;
icculus@11365
   165
    SDL_cocoametalview* metalview = [view viewWithTag:METALVIEW_TAG];
icculus@11365
   166
    if (metalview) {
icculus@11365
   167
        CAMetalLayer *layer = (CAMetalLayer*)metalview.layer;
flibitijibibo@12797
   168
        SDL_assert(layer != NULL);
slime73@11435
   169
        if (w) {
icculus@11365
   170
            *w = layer.drawableSize.width;
slime73@11435
   171
        }
slime73@11435
   172
        if (h) {
icculus@11365
   173
            *h = layer.drawableSize.height;
slime73@11435
   174
        }
libsdl@11886
   175
    } else {
libsdl@11886
   176
        SDL_GetWindowSize(window, w, h);
icculus@11365
   177
    }
slime73@12998
   178
}}
icculus@11365
   179
slime73@12998
   180
#endif /* SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_METAL) */
slouken@11407
   181
icculus@11365
   182
/* vi: set ts=4 sw=4 expandtab: */