summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/video/uikit/SDL_uikitmetalview.m
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/SDL-3.2.8/src/video/uikit/SDL_uikitmetalview.m')
-rw-r--r--contrib/SDL-3.2.8/src/video/uikit/SDL_uikitmetalview.m140
1 files changed, 140 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/video/uikit/SDL_uikitmetalview.m b/contrib/SDL-3.2.8/src/video/uikit/SDL_uikitmetalview.m
new file mode 100644
index 0000000..010dae4
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/video/uikit/SDL_uikitmetalview.m
@@ -0,0 +1,140 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 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 @slime73 on GitHub for their gist showing how to add a CAMetalLayer
26 * backed view.
27 */
28
29#include "SDL_internal.h"
30
31#if defined(SDL_VIDEO_DRIVER_UIKIT) && (defined(SDL_VIDEO_VULKAN) || defined(SDL_VIDEO_METAL))
32
33#include "../SDL_sysvideo.h"
34#include "../../events/SDL_windowevents_c.h"
35
36#import "SDL_uikitwindow.h"
37#import "SDL_uikitmetalview.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 = SDL_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
72 CAMetalLayer *metallayer = ((CAMetalLayer *)self.layer);
73 if (metallayer.drawableSize.width != size.width ||
74 metallayer.drawableSize.height != size.height) {
75 metallayer.drawableSize = size;
76 SDL_SendWindowEvent([self getSDLWindow], SDL_EVENT_WINDOW_METAL_VIEW_RESIZED, 0, 0);
77 }
78}
79
80@end
81
82SDL_MetalView UIKit_Metal_CreateView(SDL_VideoDevice *_this, SDL_Window *window)
83{
84 @autoreleasepool {
85 SDL_UIKitWindowData *data = (__bridge SDL_UIKitWindowData *)window->internal;
86 CGFloat scale = 1.0;
87 SDL_uikitmetalview *metalview;
88
89 if (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) {
90 /* Set the scale to the natural scale factor of the screen - then
91 * the backing dimensions of the Metal view will match the pixel
92 * dimensions of the screen rather than the dimensions in points
93 * yielding high resolution on retine displays.
94 */
95#ifndef SDL_PLATFORM_VISIONOS
96 scale = data.uiwindow.screen.nativeScale;
97#else
98 // VisionOS doesn't use the concept of "nativeScale" like other iOS devices.
99 // We use a fixed scale factor of 2.0 to achieve better pixel density.
100 // This is because VisionOS presents a virtual 1280x720 "screen", but we need
101 // to render at a higher resolution for optimal visual quality.
102 // TODO: Consider making this configurable or determining it dynamically
103 // based on the specific visionOS device capabilities.
104 scale = 2.0;
105#endif
106 }
107
108 metalview = [[SDL_uikitmetalview alloc] initWithFrame:data.uiwindow.bounds
109 scale:scale];
110 if (metalview == nil) {
111 SDL_OutOfMemory();
112 return NULL;
113 }
114
115 [metalview setSDLWindow:window];
116
117 return (void *)CFBridgingRetain(metalview);
118 }
119}
120
121void UIKit_Metal_DestroyView(SDL_VideoDevice *_this, SDL_MetalView view)
122{
123 @autoreleasepool {
124 SDL_uikitmetalview *metalview = CFBridgingRelease(view);
125
126 if ([metalview isKindOfClass:[SDL_uikitmetalview class]]) {
127 [metalview setSDLWindow:NULL];
128 }
129 }
130}
131
132void *UIKit_Metal_GetLayer(SDL_VideoDevice *_this, SDL_MetalView view)
133{
134 @autoreleasepool {
135 SDL_uikitview *uiview = (__bridge SDL_uikitview *)view;
136 return (__bridge void *)uiview.layer;
137 }
138}
139
140#endif // SDL_VIDEO_DRIVER_UIKIT && (SDL_VIDEO_VULKAN || SDL_VIDEO_METAL)