Atlas - SDL_uikitmetalview.m

Home / ext / SDL / src / video / uikit Lines: 1 | Size: 4718 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2026 Sam Lantinga <[email protected]> 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 // Skip invalid sizes (can happen on visionOS before scene geometry is applied) 73 if (size.width <= 0 || size.height <= 0) { 74 return; 75 } 76 77 CAMetalLayer *metallayer = ((CAMetalLayer *)self.layer); 78 if (metallayer.drawableSize.width != size.width || 79 metallayer.drawableSize.height != size.height) { 80 metallayer.drawableSize = size; 81 SDL_SendWindowEvent([self getSDLWindow], SDL_EVENT_WINDOW_METAL_VIEW_RESIZED, 0, 0); 82 } 83} 84 85@end 86 87SDL_MetalView UIKit_Metal_CreateView(SDL_VideoDevice *_this, SDL_Window *window) 88{ 89 @autoreleasepool { 90 SDL_UIKitWindowData *data = (__bridge SDL_UIKitWindowData *)window->internal; 91 CGFloat scale = 1.0; 92 SDL_uikitmetalview *metalview; 93 94 if (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) { 95 /* Set the scale to the natural scale factor of the screen - then 96 * the backing dimensions of the Metal view will match the pixel 97 * dimensions of the screen rather than the dimensions in points 98 * yielding high resolution on retine displays. 99 */ 100#ifndef SDL_PLATFORM_VISIONOS 101 scale = data.uiwindow.screen.nativeScale; 102#else 103 // VisionOS doesn't use the concept of "nativeScale" like other iOS devices. 104 // We use a fixed scale factor of 2.0 to achieve better pixel density. 105 // This is because VisionOS presents a virtual 1280x720 "screen", but we need 106 // to render at a higher resolution for optimal visual quality. 107 // TODO: Consider making this configurable or determining it dynamically 108 // based on the specific visionOS device capabilities. 109 scale = 2.0; 110#endif 111 } 112 113 metalview = [[SDL_uikitmetalview alloc] initWithFrame:data.uiwindow.bounds 114 scale:scale]; 115 if (metalview == nil) { 116 SDL_OutOfMemory(); 117 return NULL; 118 } 119 120 [metalview setSDLWindow:window]; 121 122 return (void *)CFBridgingRetain(metalview); 123 } 124} 125 126void UIKit_Metal_DestroyView(SDL_VideoDevice *_this, SDL_MetalView view) 127{ 128 @autoreleasepool { 129 SDL_uikitmetalview *metalview = CFBridgingRelease(view); 130 131 if ([metalview isKindOfClass:[SDL_uikitmetalview class]]) { 132 [metalview setSDLWindow:NULL]; 133 } 134 } 135} 136 137void *UIKit_Metal_GetLayer(SDL_VideoDevice *_this, SDL_MetalView view) 138{ 139 @autoreleasepool { 140 SDL_uikitview *uiview = (__bridge SDL_uikitview *)view; 141 return (__bridge void *)uiview.layer; 142 } 143} 144 145#endif // SDL_VIDEO_DRIVER_UIKIT && (SDL_VIDEO_VULKAN || SDL_VIDEO_METAL) 146
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.