SDL  2.0
SDL_cocoametalview.m
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2017 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 #import "SDL_cocoametalview.h"
30 
31 #if SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_COCOA
32 
33 #include "SDL_assert.h"
34 
35 @implementation SDL_cocoametalview
36 
37 /* The synthesized getter should be called by super's viewWithTag. */
38 @synthesize tag = _tag;
39 
40 /* Return a Metal-compatible layer. */
41 + (Class)layerClass
42 {
43  return NSClassFromString(@"CAMetalLayer");
44 }
45 
46 /* Indicate the view wants to draw using a backing layer instead of drawRect. */
47 -(BOOL) wantsUpdateLayer
48 {
49  return YES;
50 }
51 
52 /* When the wantsLayer property is set to YES, this method will be invoked to
53  * return a layer instance.
54  */
55 -(CALayer*) makeBackingLayer
56 {
57  return [self.class.layerClass layer];
58 }
59 
60 - (instancetype)initWithFrame:(NSRect)frame
61  useHighDPI:(bool)useHighDPI
62 {
63  if ((self = [super initWithFrame:frame])) {
64 
65  /* Allow resize. */
66  self.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
67  _tag = METALVIEW_TAG;
68 
69  _useHighDPI = useHighDPI;
70  [self updateDrawableSize];
71  }
72 
73  return self;
74 }
75 
76 /* Set the size of the metal drawables when the view is resized. */
77 - (void)resizeSubviewsWithOldSize:(NSSize)oldSize {
78  [super resizeSubviewsWithOldSize:oldSize];
79  [self updateDrawableSize];
80 }
81 
82 - (void)updateDrawableSize
83 {
84  NSRect bounds = [self bounds];
85  if (_useHighDPI) {
86  bounds = [self convertRectToBacking:bounds];
87  }
88  ((CAMetalLayer *) self.layer).drawableSize = NSSizeToCGSize(bounds.size);
89 }
90 
91 @end
92 
93 SDL_cocoametalview*
94 Cocoa_Mtl_AddMetalView(SDL_Window* window)
95 {
97  NSView *view = data->nswindow.contentView;
98 
99  SDL_cocoametalview *metalview
100  = [[SDL_cocoametalview alloc] initWithFrame:view.frame
101  useHighDPI:(window->flags & SDL_WINDOW_ALLOW_HIGHDPI)];
102  // Instantiate the CAMetalLayer
103  metalview.wantsLayer = YES;
104  [view addSubview:metalview];
105  return metalview;
106 }
107 
108 void
109 Cocoa_Mtl_GetDrawableSize(SDL_Window * window, int * w, int * h)
110 {
111  SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
112  NSView *view = data->nswindow.contentView;
113  SDL_cocoametalview* 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  }
124 }
125 
126 #endif /* SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_COCOA */
127 
128 /* vi: set ts=4 sw=4 expandtab: */
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
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
#define METALVIEW_TAG
Uint32 flags
Definition: SDL_sysvideo.h:83