SDL  2.0
SDL_uikitopengles.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 #include "../../SDL_internal.h"
22 
23 #if SDL_VIDEO_DRIVER_UIKIT && (SDL_VIDEO_OPENGL_ES || 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_loadso.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 
55 void *
56 UIKit_GL_GetProcAddress(_THIS, const char *proc)
57 {
58  /* Look through all SO's for the proc symbol. Here's why:
59  * -Looking for the path to the OpenGL Library seems not to work in the iOS Simulator.
60  * -We don't know that the path won't change in the future. */
61  return dlsym(RTLD_DEFAULT, proc);
62 }
63 
64 /*
65  note that SDL_GL_DeleteContext makes it current without passing the window
66 */
67 int
68 UIKit_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
69 {
70  @autoreleasepool {
71  SDLEAGLContext *eaglcontext = (__bridge SDLEAGLContext *) context;
72 
73  if (![EAGLContext setCurrentContext:eaglcontext]) {
74  return SDL_SetError("Could not make EAGL context current");
75  }
76 
77  if (eaglcontext) {
78  [eaglcontext.sdlView setSDLWindow:window];
79  }
80  }
81 
82  return 0;
83 }
84 
85 void
86 UIKit_GL_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h)
87 {
88  @autoreleasepool {
89  SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
90  UIView *view = data.viewcontroller.view;
91  if ([view isKindOfClass:[SDL_uikitopenglview class]]) {
92  SDL_uikitopenglview *glview = (SDL_uikitopenglview *) view;
93  if (w) {
94  *w = glview.backingWidth;
95  }
96  if (h) {
97  *h = glview.backingHeight;
98  }
99  } else {
101  }
102  }
103 }
104 
105 int
106 UIKit_GL_LoadLibrary(_THIS, const char *path)
107 {
108  /* We shouldn't pass a path to this function, since we've already loaded the
109  * library. */
110  if (path != NULL) {
111  return SDL_SetError("iOS GL Load Library just here for compatibility");
112  }
113  return 0;
114 }
115 
116 int UIKit_GL_SwapWindow(_THIS, SDL_Window * window)
117 {
118  @autoreleasepool {
119  SDLEAGLContext *context = (__bridge SDLEAGLContext *) SDL_GL_GetCurrentContext();
120 
121 #if SDL_POWER_UIKIT
122  /* Check once a frame to see if we should turn off the battery monitor. */
123  SDL_UIKit_UpdateBatteryMonitoring();
124 #endif
125 
126  [context.sdlView swapBuffers];
127 
128  /* You need to pump events in order for the OS to make changes visible.
129  * We don't pump events here because we don't want iOS application events
130  * (low memory, terminate, etc.) to happen inside low level rendering. */
131  }
132  return 0;
133 }
134 
136 UIKit_GL_CreateContext(_THIS, SDL_Window * window)
137 {
138  @autoreleasepool {
139  SDLEAGLContext *context = nil;
140  SDL_uikitopenglview *view;
141  SDL_WindowData *data = (__bridge SDL_WindowData *) window->driverdata;
142  CGRect frame = UIKit_ComputeViewFrame(window, data.uiwindow.screen);
143  EAGLSharegroup *sharegroup = nil;
144  CGFloat scale = 1.0;
145  int samples = 0;
146  int major = _this->gl_config.major_version;
147  int minor = _this->gl_config.minor_version;
148 
149  /* The EAGLRenderingAPI enum values currently map 1:1 to major GLES
150  * versions. */
151  EAGLRenderingAPI api = major;
152 
153  /* iOS currently doesn't support GLES >3.0. iOS 6 also only supports up
154  * to GLES 2.0. */
155  if (major > 3 || (major == 3 && (minor > 0 || !UIKit_IsSystemVersionAtLeast(7.0)))) {
156  SDL_SetError("OpenGL ES %d.%d context could not be created", major, minor);
157  return NULL;
158  }
159 
162  }
163 
165  EAGLContext *context = (__bridge EAGLContext *) SDL_GL_GetCurrentContext();
166  sharegroup = context.sharegroup;
167  }
168 
169  if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
170  /* Set the scale to the natural scale factor of the screen - the
171  * backing dimensions of the OpenGL view will match the pixel
172  * dimensions of the screen rather than the dimensions in points. */
173  if ([data.uiwindow.screen respondsToSelector:@selector(nativeScale)]) {
174  scale = data.uiwindow.screen.nativeScale;
175  } else {
176  scale = data.uiwindow.screen.scale;
177  }
178  }
179 
180  context = [[SDLEAGLContext alloc] initWithAPI:api sharegroup:sharegroup];
181  if (!context) {
182  SDL_SetError("OpenGL ES %d context could not be created", _this->gl_config.major_version);
183  return NULL;
184  }
185 
186  /* construct our view, passing in SDL's OpenGL configuration data */
187  view = [[SDL_uikitopenglview alloc] initWithFrame:frame
188  scale:scale
189  retainBacking:_this->gl_config.retained_backing
190  rBits:_this->gl_config.red_size
191  gBits:_this->gl_config.green_size
192  bBits:_this->gl_config.blue_size
193  aBits:_this->gl_config.alpha_size
194  depthBits:_this->gl_config.depth_size
195  stencilBits:_this->gl_config.stencil_size
196  sRGB:_this->gl_config.framebuffer_srgb_capable
197  multisamples:samples
198  context:context];
199 
200  if (!view) {
201  return NULL;
202  }
203 
204  /* The context owns the view / drawable. */
205  context.sdlView = view;
206 
207  if (UIKit_GL_MakeCurrent(_this, window, (__bridge SDL_GLContext) context) < 0) {
208  UIKit_GL_DeleteContext(_this, (SDL_GLContext) CFBridgingRetain(context));
209  return NULL;
210  }
211 
212  /* We return a +1'd context. The window's driverdata owns the view (via
213  * MakeCurrent.) */
214  return (SDL_GLContext) CFBridgingRetain(context);
215  }
216 }
217 
218 void
219 UIKit_GL_DeleteContext(_THIS, SDL_GLContext context)
220 {
221  @autoreleasepool {
222  /* The context was retained in SDL_GL_CreateContext, so we release it
223  * here. The context's view will be detached from its window when the
224  * context is deallocated. */
225  CFRelease(context);
226  }
227 }
228 
229 void
230 UIKit_GL_RestoreCurrentContext(void)
231 {
232  @autoreleasepool {
233  /* Some iOS system functionality (such as Dictation on the on-screen
234  keyboard) uses its own OpenGL ES context but doesn't restore the
235  previous one when it's done. This is a workaround to make sure the
236  expected SDL-created OpenGL ES context is active after the OS is
237  finished running its own code for the frame. If this isn't done, the
238  app may crash or have other nasty symptoms when Dictation is used.
239  */
240  EAGLContext *context = (__bridge EAGLContext *) SDL_GL_GetCurrentContext();
241  if (context != NULL && [EAGLContext currentContext] != context) {
242  [EAGLContext setCurrentContext:context];
243  }
244  }
245 }
246 
247 #endif /* SDL_VIDEO_DRIVER_UIKIT */
248 
249 /* vi: set ts=4 sw=4 expandtab: */
SDL_uikitmodes.h
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
SDL_VideoDevice::multisamplesamples
int multisamplesamples
Definition: SDL_sysvideo.h:349
samples
GLsizei samples
Definition: SDL_opengl_glext.h:1188
SDL_WindowData
Definition: SDL_androidwindow.h:38
SDL_VideoDevice::major_version
int major_version
Definition: SDL_sysvideo.h:351
path
GLsizei const GLchar *const * path
Definition: SDL_opengl_glext.h:3733
h
GLfloat GLfloat GLfloat GLfloat h
Definition: SDL_opengl_glext.h:1949
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
SDL_WindowData::viewcontroller
SDL_uikitviewcontroller * viewcontroller
Definition: SDL_uikitwindow.h:47
SDL_VideoDevice::gl_config
struct SDL_VideoDevice::@255 gl_config
context
static screen_context_t context
Definition: video.c:25
_this
static SDL_VideoDevice * _this
Definition: SDL_video.c:121
window
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_uikitevents.h
SDL_uikitopenglview.h
SDL_VideoDevice::multisamplebuffers
int multisamplebuffers
Definition: SDL_sysvideo.h:348
frame
int frame
Definition: teststreaming.c:60
SDL_VideoDevice::share_with_current_context
int share_with_current_context
Definition: SDL_sysvideo.h:355
_THIS
#define _THIS
Definition: SDL_alsa_audio.h:31
SDL_GL_GetCurrentContext
#define SDL_GL_GetCurrentContext
Definition: SDL_dynapi_overrides.h:562
SDL_uikitopengles.h
SDL_uikitwindow.h
SDL_GLContext
void * SDL_GLContext
An opaque handle to an OpenGL context.
Definition: SDL_video.h:192
UIKit_IsSystemVersionAtLeast
SDL_bool UIKit_IsSystemVersionAtLeast(double version)
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
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_VideoDevice::minor_version
int minor_version
Definition: SDL_sysvideo.h:352
SDL_loadso.h
w
GLubyte GLubyte GLubyte GLubyte w
Definition: SDL_opengl_glext.h:734