Atlas - SDL_uikitopengles.m
Home / ext / SDL / src / video / uikit Lines: 1 | Size: 8923 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#include "SDL_internal.h" 22 23#if defined(SDL_VIDEO_DRIVER_UIKIT) && (defined(SDL_VIDEO_OPENGL_ES) || defined(SDL_VIDEO_OPENGL_ES2)) 24 25#include "SDL_uikitopengles.h" 26#import "SDL_uikitopenglview.h" 27#include "SDL_uikitmodes.h" 28#include "SDL_uikitwindow.h" 29#include "SDL_uikitevents.h" 30#include "../SDL_sysvideo.h" 31#include "../../events/SDL_keyboard_c.h" 32#include "../../events/SDL_mouse_c.h" 33#include "../../power/uikit/SDL_syspower.h" 34#include "../../SDL_hints_c.h" 35#include <dlfcn.h> 36 37@interface SDLEAGLContext : EAGLContext 38 39// The OpenGL ES context owns a view / drawable. 40@property(nonatomic, strong) SDL_uikitopenglview *sdlView; 41 42@end 43 44@implementation SDLEAGLContext 45 46- (void)dealloc 47{ 48 /* When the context is deallocated, its view should be removed from any 49 * SDL window that it's attached to. */ 50 [self.sdlView setSDLWindow:NULL]; 51} 52 53@end 54 55SDL_FunctionPointer UIKit_GL_GetProcAddress(SDL_VideoDevice *_this, const char *proc) 56{ 57 /* Look through all SO's for the proc symbol. Here's why: 58 * -Looking for the path to the OpenGL Library seems not to work in the iOS Simulator. 59 * -We don't know that the path won't change in the future. */ 60 return dlsym(RTLD_DEFAULT, proc); 61} 62 63/* 64 note that SDL_GL_DestroyContext makes it current without passing the window 65*/ 66bool UIKit_GL_MakeCurrent(SDL_VideoDevice *_this, SDL_Window *window, SDL_GLContext context) 67{ 68 @autoreleasepool { 69 SDLEAGLContext *eaglcontext = (__bridge SDLEAGLContext *)context; 70 71 if (![EAGLContext setCurrentContext:eaglcontext]) { 72 return SDL_SetError("Could not make EAGL context current"); 73 } 74 75 if (eaglcontext) { 76 [eaglcontext.sdlView setSDLWindow:window]; 77 } 78 } 79 80 return true; 81} 82 83bool UIKit_GL_LoadLibrary(SDL_VideoDevice *_this, const char *path) 84{ 85 /* We shouldn't pass a path to this function, since we've already loaded the 86 * library. */ 87 if (path != NULL) { 88 return SDL_SetError("iOS GL Load Library just here for compatibility"); 89 } 90 return true; 91} 92 93bool UIKit_GL_SwapWindow(SDL_VideoDevice *_this, SDL_Window *window) 94{ 95 @autoreleasepool { 96 SDLEAGLContext *context = (__bridge SDLEAGLContext *)SDL_GL_GetCurrentContext(); 97 98#ifdef SDL_POWER_UIKIT 99 // Check once a frame to see if we should turn off the battery monitor. 100 SDL_UIKit_UpdateBatteryMonitoring(); 101#endif 102 103 [context.sdlView swapBuffers]; 104 105 /* You need to pump events in order for the OS to make changes visible. 106 * We don't pump events here because we don't want iOS application events 107 * (low memory, terminate, etc.) to happen inside low level rendering. */ 108 } 109 return true; 110} 111 112SDL_GLContext UIKit_GL_CreateContext(SDL_VideoDevice *_this, SDL_Window *window) 113{ 114 @autoreleasepool { 115 SDLEAGLContext *context = nil; 116 SDL_uikitopenglview *view; 117 SDL_UIKitWindowData *data = (__bridge SDL_UIKitWindowData *)window->internal; 118 CGRect frame = UIKit_ComputeViewFrame(window, data.uiwindow.screen); 119 EAGLSharegroup *sharegroup = nil; 120 CGFloat scale = 1.0; 121 int samples = 0; 122 int major = _this->gl_config.major_version; 123 int minor = _this->gl_config.minor_version; 124 125 /* The EAGLRenderingAPI enum values currently map 1:1 to major GLES 126 * versions. */ 127 EAGLRenderingAPI api = major; 128 129 // iOS currently doesn't support GLES >3.0. 130 if (major > 3 || (major == 3 && minor > 0)) { 131 SDL_SetError("OpenGL ES %d.%d context could not be created", major, minor); 132 return NULL; 133 } 134 135 if (_this->gl_config.multisamplebuffers > 0) { 136 samples = _this->gl_config.multisamplesamples; 137 } 138 139 if (_this->gl_config.share_with_current_context) { 140 EAGLContext *currContext = (__bridge EAGLContext *)SDL_GL_GetCurrentContext(); 141 sharegroup = currContext.sharegroup; 142 } 143 144 if (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) { 145 /* Set the scale to the natural scale factor of the screen - the 146 * backing dimensions of the OpenGL view will match the pixel 147 * dimensions of the screen rather than the dimensions in points. */ 148 scale = data.uiwindow.screen.nativeScale; 149 } 150 151 context = [[SDLEAGLContext alloc] initWithAPI:api sharegroup:sharegroup]; 152 if (!context) { 153 SDL_SetError("OpenGL ES %d context could not be created", _this->gl_config.major_version); 154 return NULL; 155 } 156 157 int srgb = _this->gl_config.framebuffer_srgb_capable; 158 const char *srgbhint = SDL_GetHint(SDL_HINT_OPENGL_FORCE_SRGB_FRAMEBUFFER); 159 if (srgbhint && *srgbhint) { 160 srgb = SDL_GetStringBoolean(srgbhint, false) ? 1 : 0; // there is no "skip" here, since initWithFrame expects it, so we'll treat it as false. 161 } 162 163 // construct our view, passing in SDL's OpenGL configuration data 164 view = [[SDL_uikitopenglview alloc] initWithFrame:frame 165 scale:scale 166 retainBacking:_this->gl_config.retained_backing 167 rBits:_this->gl_config.red_size 168 gBits:_this->gl_config.green_size 169 bBits:_this->gl_config.blue_size 170 aBits:_this->gl_config.alpha_size 171 depthBits:_this->gl_config.depth_size 172 stencilBits:_this->gl_config.stencil_size 173 sRGB:srgb 174 multisamples:samples 175 context:context]; 176 177 if (!view) { 178 return NULL; 179 } 180 181 SDL_PropertiesID props = SDL_GetWindowProperties(window); 182 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_UIKIT_OPENGL_FRAMEBUFFER_NUMBER, view.drawableFramebuffer); 183 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_UIKIT_OPENGL_RENDERBUFFER_NUMBER, view.drawableRenderbuffer); 184 SDL_SetNumberProperty(props, SDL_PROP_WINDOW_UIKIT_OPENGL_RESOLVE_FRAMEBUFFER_NUMBER, view.msaaResolveFramebuffer); 185 186 // The context owns the view / drawable. 187 context.sdlView = view; 188 189 if (!UIKit_GL_MakeCurrent(_this, window, (__bridge SDL_GLContext)context)) { 190 UIKit_GL_DestroyContext(_this, (SDL_GLContext)CFBridgingRetain(context)); 191 return NULL; 192 } 193 194 /* We return a +1'd context. The window's internal owns the view (via 195 * MakeCurrent.) */ 196 return (SDL_GLContext)CFBridgingRetain(context); 197 } 198} 199 200bool UIKit_GL_DestroyContext(SDL_VideoDevice *_this, SDL_GLContext context) 201{ 202 @autoreleasepool { 203 /* The context was retained in SDL_GL_CreateContext, so we release it 204 * here. The context's view will be detached from its window when the 205 * context is deallocated. */ 206 CFRelease(context); 207 } 208 return true; 209} 210 211void UIKit_GL_RestoreCurrentContext(void) 212{ 213 @autoreleasepool { 214 /* Some iOS system functionality (such as Dictation on the on-screen 215 keyboard) uses its own OpenGL ES context but doesn't restore the 216 previous one when it's done. This is a workaround to make sure the 217 expected SDL-created OpenGL ES context is active after the OS is 218 finished running its own code for the frame. If this isn't done, the 219 app may crash or have other nasty symptoms when Dictation is used. 220 */ 221 EAGLContext *context = (__bridge EAGLContext *)SDL_GL_GetCurrentContext(); 222 if (context != NULL && [EAGLContext currentContext] != context) { 223 [EAGLContext setCurrentContext:context]; 224 } 225 } 226} 227 228#endif // SDL_VIDEO_DRIVER_UIKIT 229[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.