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