SDL  2.0
SDL_cocoametalview.m
Go to the documentation of this file.
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  * @author Mark Callow, www.edgewise-consulting.com.
23  *
24  * Thanks to Alex Szpakowski, @slime73 on GitHub, for his gist showing
25  * how to add a CAMetalLayer backed view.
26  */
27 
28 #import "SDL_cocoametalview.h"
29 
30 #if SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_RENDER_METAL)
31 
32 #include "SDL_assert.h"
33 
34 @implementation SDL_cocoametalview
35 
36 /* The synthesized getter should be called by super's viewWithTag. */
37 @synthesize tag = _tag;
38 
39 /* Return a Metal-compatible layer. */
40 + (Class)layerClass
41 {
42  return NSClassFromString(@"CAMetalLayer");
43 }
44 
45 /* Indicate the view wants to draw using a backing layer instead of drawRect. */
46 - (BOOL)wantsUpdateLayer
47 {
48  return YES;
49 }
50 
51 /* When the wantsLayer property is set to YES, this method will be invoked to
52  * return a layer instance.
53  */
54 - (CALayer*)makeBackingLayer
55 {
56  return [self.class.layerClass layer];
57 }
58 
59 - (instancetype)initWithFrame:(NSRect)frame
60  scale:(CGFloat)scale
61 {
62  if ((self = [super initWithFrame:frame])) {
63  _tag = METALVIEW_TAG;
64  self.wantsLayer = YES;
65 
66  /* Allow resize. */
67  self.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
68 
69  /* Set the desired scale. The default drawableSize of a CAMetalLayer
70  * is its bounds x its scale so nothing further needs to be done.
71  */
72  self.layer.contentsScale = scale;
73  }
74 
75  return self;
76 }
77 
78 /* Set the size of the metal drawables when the view is resized. */
79 - (void)resizeWithOldSuperviewSize:(NSSize)oldSize
80 {
81  [super resizeWithOldSuperviewSize:oldSize];
82 }
83 
84 @end
85 
86 SDL_cocoametalview*
87 Cocoa_Mtl_AddMetalView(SDL_Window* window)
88 {
89  SDL_WindowData* data = (__bridge SDL_WindowData *)window->driverdata;
90  NSView *view = data->nswindow.contentView;
91  CGFloat scale = 1.0;
92 
93  if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
94  /* Set the scale to the natural scale factor of the screen - then
95  * the backing dimensions of the Metal view will match the pixel
96  * dimensions of the screen rather than the dimensions in points
97  * yielding high resolution on retine displays.
98  *
99  * N.B. In order for backingScaleFactor to be > 1,
100  * NSHighResolutionCapable must be set to true in the app's Info.plist.
101  */
102  NSWindow* nswindow = data->nswindow;
103  if ([nswindow.screen respondsToSelector:@selector(backingScaleFactor)])
104  scale = data->nswindow.screen.backingScaleFactor;
105  }
106 
107  SDL_cocoametalview *metalview
108  = [[SDL_cocoametalview alloc] initWithFrame:view.frame scale:scale];
109  [view addSubview:metalview];
110  return metalview;
111 }
112 
113 void
114 Cocoa_Mtl_GetDrawableSize(SDL_Window * window, int * w, int * h)
115 {
116  SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
117  NSView *view = data->nswindow.contentView;
118  SDL_cocoametalview* metalview = [view viewWithTag:METALVIEW_TAG];
119  if (metalview) {
120  CAMetalLayer *layer = (CAMetalLayer*)metalview.layer;
121  assert(layer != NULL);
122  if (w) {
123  *w = layer.drawableSize.width;
124  }
125  if (h) {
126  *h = layer.drawableSize.height;
127  }
128  } else {
129  SDL_GetWindowSize(window, w, h);
130  }
131 }
132 
133 #endif /* SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_RENDER_METAL) */
134 
135 /* vi: set ts=4 sw=4 expandtab: */
GLenum GLenum GLenum GLenum GLenum scale
GLfloat GLfloat GLfloat GLfloat h
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
GLenum GLuint GLint GLint layer
#define assert(x)
Definition: SDL_malloc.c:1219
#define SDL_GetWindowSize
int frame
Definition: teststreaming.c:60
GLubyte GLubyte GLubyte GLubyte w
NSWindow * nswindow
#define NULL
Definition: begin_code.h:164
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 void
The type used to identify a window.
Definition: SDL_sysvideo.h:73
void * driverdata
Definition: SDL_sysvideo.h:111
Uint32 flags
Definition: SDL_sysvideo.h:83