SDL  2.0
SDL_uikitmetalview.m
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2020 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_VULKAN || SDL_VIDEO_METAL)
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 
77 UIKit_Metal_CreateView(_THIS, SDL_Window * window)
78 { @autoreleasepool {
79  SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
80  CGFloat scale = 1.0;
81  SDL_uikitmetalview *metalview;
82 
84  /* Set the scale to the natural scale factor of the screen - then
85  * the backing dimensions of the Metal view will match the pixel
86  * dimensions of the screen rather than the dimensions in points
87  * yielding high resolution on retine displays.
88  */
89  if ([data.uiwindow.screen respondsToSelector:@selector(nativeScale)]) {
90  scale = data.uiwindow.screen.nativeScale;
91  } else {
92  scale = data.uiwindow.screen.scale;
93  }
94  }
95 
96  metalview = [[SDL_uikitmetalview alloc] initWithFrame:data.uiwindow.bounds
97  scale:scale];
98  [metalview setSDLWindow:window];
99 
100  return (void*)CFBridgingRetain(metalview);
101 }}
102 
103 void
104 UIKit_Metal_DestroyView(_THIS, SDL_MetalView view)
105 { @autoreleasepool {
106  SDL_uikitmetalview *metalview = CFBridgingRelease(view);
107 
108  if ([metalview isKindOfClass:[SDL_uikitmetalview class]]) {
109  [metalview setSDLWindow:NULL];
110  }
111 }}
112 
113 void
114 UIKit_Metal_GetDrawableSize(SDL_Window * window, int * w, int * h)
115 {
116  @autoreleasepool {
117  SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
118  SDL_uikitview *view = (SDL_uikitview*)data.uiwindow.rootViewController.view;
119  SDL_uikitmetalview* metalview = [view viewWithTag:METALVIEW_TAG];
120  if (metalview) {
121  CAMetalLayer *layer = (CAMetalLayer*)metalview.layer;
122  assert(layer != NULL);
123  if (w) {
124  *w = layer.drawableSize.width;
125  }
126  if (h) {
127  *h = layer.drawableSize.height;
128  }
129  } else {
131  }
132  }
133 }
134 
135 #endif /* SDL_VIDEO_DRIVER_UIKIT && (SDL_VIDEO_VULKAN || SDL_VIDEO_METAL) */
SDL_WINDOW_ALLOW_HIGHDPI
@ SDL_WINDOW_ALLOW_HIGHDPI
Definition: SDL_video.h:112
scale
GLenum GLenum GLenum GLenum GLenum scale
Definition: SDL_opengl_glext.h:9378
if
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 abits op endm macro pixldst3 mem_operand op endm macro pixldst30 mem_operand op endm macro pixldst abits if abits elseif abits elseif abits elseif abits elseif abits pixldst0 abits else pixldst0 abits pixldst0 abits pixldst0 abits pixldst0 abits endif elseif abits else pixldst0 abits pixldst0 abits endif elseif abits else error unsupported bpp *numpix else pixst endif endm macro pixld1_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl else error unsupported endif endm macro pixld2_s mem_operand if mov asr add asl add asl mov asr sub UNIT_X add asl mov asr add asl add asl mov asr add UNIT_X add asl else pixld1_s mem_operand pixld1_s mem_operand endif endm macro pixld0_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl endif endm macro pixld_s_internal mem_operand if mem_operand pixld2_s mem_operand pixdeinterleave basereg elseif mem_operand elseif mem_operand elseif mem_operand elseif mem_operand pixld0_s mem_operand else pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else error unsupported mem_operand if bpp mem_operand endif endm macro vuzp8 reg2 vuzp d d &reg2 endm macro vzip8 reg2 vzip d d &reg2 endm macro pixdeinterleave basereg basereg basereg basereg basereg endif endm macro pixinterleave basereg basereg basereg basereg basereg endif endm macro PF boost_increment endif if endif PF tst PF addne PF subne PF cmp ORIG_W if endif if endif if endif PF subge ORIG_W PF subges if endif if endif if endif endif endm macro cache_preload_simple endif if dst_r_bpp pld[DST_R, #(PREFETCH_DISTANCE_SIMPLE *dst_r_bpp/8)] endif if mask_bpp pld if[MASK, #(PREFETCH_DISTANCE_SIMPLE *mask_bpp/8)] endif endif endm macro fetch_mask_pixblock pixld mask_basereg pixblock_size MASK endm macro ensure_destination_ptr_alignment process_pixblock_tail_head if beq irp skip1(dst_w_bpp<=(lowbit *8)) &&((lowbit *8)<(pixblock_size *dst_w_bpp)) .if lowbit< 16 tst DST_R
Definition: pixman-arm-neon-asm.h:469
NULL
#define NULL
Definition: begin_code.h:167
layer
GLenum GLuint GLint GLint layer
Definition: SDL_opengl_glext.h:1189
SDL_WindowData
Definition: SDL_androidwindow.h:38
assert
#define assert(x)
Definition: SDL_malloc.c:1227
h
GLfloat GLfloat GLfloat GLfloat h
Definition: SDL_opengl_glext.h:1949
SDL_MetalView
void * SDL_MetalView
A handle to a CAMetalLayer-backed NSView (macOS) or UIView (iOS/tvOS).
Definition: SDL_metal.h:44
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
_THIS
#define _THIS
Definition: SDL_alsa_audio.h:31
size
GLsizeiptr size
Definition: SDL_opengl_glext.h:540
SDL_uikitwindow.h
SDL_uikitview
Definition: SDL_uikitview.h:28
void
const SDL_PRINTF_FORMAT_STRING char int const SDL_PRINTF_FORMAT_STRING char int const SDL_PRINTF_FORMAT_STRING char int const SDL_PRINTF_FORMAT_STRING char const char const SDL_SCANF_FORMAT_STRING char return SDL_ThreadFunction const char void return Uint32 return Uint32 void
Definition: SDL_dynapi_procs.h:89
SDL_uikitmetalview.h
w
GLubyte GLubyte GLubyte GLubyte w
Definition: SDL_opengl_glext.h:734