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 /* Return a Metal-compatible layer. */
37 + (Class)layerClass
38 {
39  return NSClassFromString(@"CAMetalLayer");
40 }
41 
42 /* Indicate the view wants to draw using a backing layer instead of drawRect. */
43 - (BOOL)wantsUpdateLayer
44 {
45  return YES;
46 }
47 
48 /* When the wantsLayer property is set to YES, this method will be invoked to
49  * return a layer instance.
50  */
51 - (CALayer*)makeBackingLayer
52 {
53  return [self.class.layerClass layer];
54 }
55 
56 - (instancetype)initWithFrame:(NSRect)frame
57  highDPI:(BOOL)highDPI
58 {
59  if ((self = [super initWithFrame:frame])) {
60  self.highDPI = highDPI;
61  self.wantsLayer = YES;
62 
63  /* Allow resize. */
64  self.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
65 
66  [self updateDrawableSize];
67  }
68 
69  return self;
70 }
71 
72 - (NSInteger)tag
73 {
74  return METALVIEW_TAG;
75 }
76 
77 - (void)updateDrawableSize
78 {
79  CAMetalLayer *metalLayer = (CAMetalLayer *)self.layer;
80  CGSize size = self.bounds.size;
81  CGSize backingSize = size;
82 
83  if (self.highDPI) {
84  /* Note: NSHighResolutionCapable must be set to true in the app's
85  * Info.plist in order for the backing size to be high res.
86  */
87  backingSize = [self convertSizeToBacking:size];
88  }
89 
90  metalLayer.contentsScale = backingSize.height / size.height;
91  metalLayer.drawableSize = backingSize;
92 }
93 
94 /* Set the size of the metal drawables when the view is resized. */
95 - (void)resizeWithOldSuperviewSize:(NSSize)oldSize
96 {
97  [super resizeWithOldSuperviewSize:oldSize];
98  [self updateDrawableSize];
99 }
100 
101 @end
102 
103 SDL_cocoametalview*
104 Cocoa_Mtl_AddMetalView(SDL_Window* window)
105 {
106  SDL_WindowData* data = (__bridge SDL_WindowData *)window->driverdata;
107  NSView *view = data->nswindow.contentView;
108  BOOL highDPI = (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) != 0;
109  SDL_cocoametalview *metalview;
110 
111  metalview = [[SDL_cocoametalview alloc] initWithFrame:view.frame highDPI:highDPI];
112  [view addSubview:metalview];
113  return metalview;
114 }
115 
116 void
117 Cocoa_Mtl_GetDrawableSize(SDL_Window * window, int * w, int * h)
118 {
119  SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
120  NSView *view = data->nswindow.contentView;
121  SDL_cocoametalview* metalview = [view viewWithTag:METALVIEW_TAG];
122  if (metalview) {
123  CAMetalLayer *layer = (CAMetalLayer*)metalview.layer;
124  assert(layer != NULL);
125  if (w) {
126  *w = layer.drawableSize.width;
127  }
128  if (h) {
129  *h = layer.drawableSize.height;
130  }
131  } else {
133  }
134 }
135 
136 #endif /* SDL_VIDEO_DRIVER_COCOA && (SDL_VIDEO_VULKAN || SDL_VIDEO_RENDER_METAL) */
137 
138 /* vi: set ts=4 sw=4 expandtab: */
SDL_WINDOW_ALLOW_HIGHDPI
@ SDL_WINDOW_ALLOW_HIGHDPI
Definition: SDL_video.h:113
NULL
#define NULL
Definition: begin_code.h:164
layer
GLenum GLuint GLint GLint layer
Definition: SDL_opengl_glext.h:1186
SDL_WindowData
Definition: SDL_androidwindow.h:36
assert
#define assert(x)
Definition: SDL_malloc.c:1219
h
GLfloat GLfloat GLfloat GLfloat h
Definition: SDL_opengl_glext.h:1946
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:74
SDL_GetWindowSize
#define SDL_GetWindowSize
Definition: SDL_dynapi_overrides.h:527
window
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
frame
int frame
Definition: teststreaming.c:60
SDL_assert.h
size
GLsizeiptr size
Definition: SDL_opengl_glext.h:537
SDL_cocoametalview.h
void
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
Definition: SDL_dynapi_procs.h:89
SDL_WindowData::nswindow
NSWindow * nswindow
Definition: SDL_cocoawindow.h:115
w
GLubyte GLubyte GLubyte GLubyte w
Definition: SDL_opengl_glext.h:731