Debian Codegraph @

src/iphone/iphone_display.m

raw
1 #include <allegro5/allegro.h>
2 #include <allegro5/allegro_opengl.h>
3 #include <allegro5/internal/aintern_iphone.h>
4 #include <allegro5/internal/aintern_opengl.h>
5 #include <allegro5/internal/aintern_vector.h>
6 #include <math.h>
7
8 #include "iphone.h"
9 #include "allegroAppDelegate.h"
10
11 ALLEGRO_DEBUG_CHANNEL("iphone")
12
13 static ALLEGRO_DISPLAY_INTERFACE *vt;
14
15 static float _screen_iscale = 1.0;
16 static float _screen_x, _screen_y;
17 static float _screen_w, _screen_h;
18 static bool _screen_hack;
19
20 bool _al_iphone_is_display_connected(ALLEGRO_DISPLAY *display);
21 void _al_iphone_connect_airplay(void);
22 extern ALLEGRO_MUTEX *_al_iphone_display_hotplug_mutex;
23
24 void _al_iphone_setup_opengl_view(ALLEGRO_DISPLAY *d, bool manage_backbuffer)
25 {
26 int w, h;
27
28 w = d->w;
29 h = d->h;
30
31 _al_iphone_reset_framebuffer(d);
32
33 _screen_w = w;
34 _screen_h = h;
35
36 if (manage_backbuffer) {
37 _al_ogl_setup_gl(d);
38 }
39 }
40
41 void _al_iphone_translate_from_screen(ALLEGRO_DISPLAY *d, int *x, int *y)
42 {
43 if (!_screen_hack) return;
44 // See _al_iphone_setup_opengl_view
45 float ox = *x, oy = *y;
46
47 if (d->w >= d->h) {
48 *x = _screen_x + oy * _screen_iscale;
49 *y = _screen_y + (_screen_w - ox) * _screen_iscale;
50 }
51 else {
52 // TODO
53 }
54 }
55
56 void _al_iphone_clip(ALLEGRO_BITMAP const *bitmap, int x_1, int y_1, int x_2, int y_2)
57 {
58 ALLEGRO_BITMAP *oglb = (void *)(bitmap->parent ? bitmap->parent : bitmap);
59 int h = oglb->h;
60 glScissor(x_1, h - y_2, x_2 - x_1, y_2 - y_1);
61 }
62
63 static void set_rgba8888(ALLEGRO_EXTRA_DISPLAY_SETTINGS *eds)
64 {
65 eds->settings[ALLEGRO_RED_SIZE] = 8;
66 eds->settings[ALLEGRO_GREEN_SIZE] = 8;
67 eds->settings[ALLEGRO_BLUE_SIZE] = 8;
68 eds->settings[ALLEGRO_ALPHA_SIZE] = 8;
69 eds->settings[ALLEGRO_RED_SHIFT] = 0;
70 eds->settings[ALLEGRO_GREEN_SHIFT] = 8;
71 eds->settings[ALLEGRO_BLUE_SHIFT] = 16;
72 eds->settings[ALLEGRO_ALPHA_SHIFT] = 24;
73 eds->settings[ALLEGRO_COLOR_SIZE] = 32;
74 }
75
76 static void set_rgb565(ALLEGRO_EXTRA_DISPLAY_SETTINGS *eds)
77 {
78 eds->settings[ALLEGRO_RED_SIZE] = 5;
79 eds->settings[ALLEGRO_GREEN_SIZE] = 6;
80 eds->settings[ALLEGRO_BLUE_SIZE] = 5;
81 eds->settings[ALLEGRO_ALPHA_SIZE] = 0;
82 eds->settings[ALLEGRO_RED_SHIFT] = 0;
83 eds->settings[ALLEGRO_GREEN_SHIFT] = 5;
84 eds->settings[ALLEGRO_BLUE_SHIFT] = 11;
85 eds->settings[ALLEGRO_ALPHA_SHIFT] = 0;
86 eds->settings[ALLEGRO_COLOR_SIZE] = 16;
87 }
88
89 #define VISUALS_COUNT 6
90 void _al_iphone_update_visuals(void)
91 {
92 ALLEGRO_EXTRA_DISPLAY_SETTINGS *ref;
93 ALLEGRO_SYSTEM_IPHONE *system = (void *)al_get_system_driver();
94
95 ref = _al_get_new_display_settings();
96
97 /* If we aren't called the first time, only updated scores. */
98 if (system->visuals) {
99 for (int i = 0; i < system->visuals_count; i++) {
100 ALLEGRO_EXTRA_DISPLAY_SETTINGS *eds = system->visuals[i];
101 eds->score = _al_score_display_settings(eds, ref);
102 }
103 return;
104 }
105
106 system->visuals = al_calloc(1, VISUALS_COUNT * sizeof(*system->visuals));
107 system->visuals_count = VISUALS_COUNT;
108
109 for (int i = 0; i < VISUALS_COUNT; i++) {
110 ALLEGRO_EXTRA_DISPLAY_SETTINGS *eds = al_calloc(1, sizeof *eds);
111 eds->settings[ALLEGRO_RENDER_METHOD] = 1;
112 eds->settings[ALLEGRO_COMPATIBLE_DISPLAY] = 1;
113 eds->settings[ALLEGRO_SWAP_METHOD] = 2;
114 eds->settings[ALLEGRO_VSYNC] = 1;
115 eds->settings[ALLEGRO_SUPPORTED_ORIENTATIONS] =
116 ref->settings[ALLEGRO_SUPPORTED_ORIENTATIONS];
117 switch (i) {
118 case 0:
119 set_rgba8888(eds);
120 break;
121 case 1:
122 set_rgb565(eds);
123 break;
124 case 2:
125 set_rgba8888(eds);
126 eds->settings[ALLEGRO_DEPTH_SIZE] = 16;
127 break;
128 case 3:
129 set_rgb565(eds);
130 eds->settings[ALLEGRO_DEPTH_SIZE] = 16;
131 break;
132 case 4:
133 set_rgba8888(eds);
134 eds->settings[ALLEGRO_DEPTH_SIZE] = 24;
135 eds->settings[ALLEGRO_STENCIL_SIZE] = 8;
136 break;
137 case 5:
138 set_rgb565(eds);
139 eds->settings[ALLEGRO_DEPTH_SIZE] = 24;
140 eds->settings[ALLEGRO_STENCIL_SIZE] = 8;
141 break;
142
143 }
144 eds->score = _al_score_display_settings(eds, ref);
145 eds->index = i;
146 system->visuals[i] = eds;
147 }
148 }
149
150 static ALLEGRO_DISPLAY *iphone_create_display(int w, int h)
151 {
152 ALLEGRO_DISPLAY_IPHONE *d = al_calloc(1, sizeof *d);
153 ALLEGRO_DISPLAY *display = (void*)d;
154 ALLEGRO_OGL_EXTRAS *ogl = al_calloc(1, sizeof *ogl);
155 display->ogl_extras = ogl;
156 display->vt = _al_get_iphone_display_interface();
157 display->flags = al_get_new_display_flags();
158 int adapter;
159
160 adapter = al_get_new_display_adapter();
161 if (adapter < 0)
162 adapter = 0;
163
164 if (adapter == 1) {
165 _al_iphone_connect_airplay();
166 }
167
168 if (display->flags & ALLEGRO_FULLSCREEN_WINDOW) {
169 _al_iphone_get_screen_size(adapter, &w, &h);
170 }
171
172 display->w = w;
173 display->h = h;
174
175 ALLEGRO_SYSTEM_IPHONE *system = (void *)al_get_system_driver();
176
177 /* Add ourself to the list of displays. */
178 ALLEGRO_DISPLAY_IPHONE **add;
179 add = _al_vector_alloc_back(&system->system.displays);
180 *add = d;
181
182 /* Each display is an event source. */
183 _al_event_source_init(&display->es);
184
185 _al_iphone_update_visuals();
186
187 ALLEGRO_EXTRA_DISPLAY_SETTINGS *eds[system->visuals_count];
188 memcpy(eds, system->visuals, sizeof(*eds) * system->visuals_count);
189 qsort(eds, system->visuals_count, sizeof(*eds), _al_display_settings_sorter);
190
191 ALLEGRO_INFO("Chose visual no. %i\n", eds[0]->index);
192
193 memcpy(&display->extra_settings, eds[0], sizeof(ALLEGRO_EXTRA_DISPLAY_SETTINGS));
194
195 display->flags |= ALLEGRO_OPENGL;
196 #ifdef ALLEGRO_CFG_OPENGLES2
197 display->flags |= ALLEGRO_PROGRAMMABLE_PIPELINE;
198 #endif
199 #ifdef ALLEGRO_CFG_OPENGLES
200 display->flags |= ALLEGRO_OPENGL_ES_PROFILE;
201 #endif
202
203 /* This will add an OpenGL view with an OpenGL context, then return. */
204 if (!_al_iphone_add_view(display)) {
205 /* FIXME: cleanup */
206 return NULL;
207 }
208
209 _al_iphone_make_view_current(display);
210
211 /* FIXME: there is some sort of race condition somewhere. */
212 al_rest(1.0);
213
214 _al_ogl_manage_extensions(display);
215 _al_ogl_set_extensions(ogl->extension_api);
216 _al_iphone_setup_opengl_view(display, true);
217
218 int ndisplays = system->system.displays._size;
219 [[UIApplication sharedApplication] setIdleTimerDisabled:(ndisplays > 1)];
220
221 /* Fill in opengl version */
222 const int v = display->ogl_extras->ogl_info.version;
223 display->extra_settings.settings[ALLEGRO_OPENGL_MAJOR_VERSION] = (v >> 24) & 0xFF;
224 display->extra_settings.settings[ALLEGRO_OPENGL_MINOR_VERSION] = (v >> 16) & 0xFF;
225
226 return display;
227 }
228
229 static void iphone_destroy_display(ALLEGRO_DISPLAY *d)
230 {
231 ALLEGRO_DISPLAY_IPHONE *idisplay = (ALLEGRO_DISPLAY_IPHONE *)d;
232 ALLEGRO_DISPLAY_IPHONE_EXTRA *extra = idisplay->extra;
233 bool disconnected = extra->disconnected;
234
235 _al_set_current_display_only(d);
236
237 while (d->bitmaps._size > 0) {
238 ALLEGRO_BITMAP **bptr = (ALLEGRO_BITMAP **)_al_vector_ref_back(&d->bitmaps);
239 ALLEGRO_BITMAP *b = *bptr;
240 _al_convert_to_memory_bitmap(b);
241 }
242
243 _al_event_source_free(&d->es);
244 _al_iphone_destroy_screen(d);
245
246 if (disconnected) {
247 _al_iphone_disconnect(d);
248 }
249
250 ALLEGRO_SYSTEM_IPHONE *system = (void *)al_get_system_driver();
251 _al_vector_find_and_delete(&system->system.displays, &d);
252
253 [[UIApplication sharedApplication] setIdleTimerDisabled:FALSE];
254 }
255
256 static bool iphone_set_current_display(ALLEGRO_DISPLAY *d)
257 {
258 (void)d;
259 _al_iphone_make_view_current(d);
260 _al_ogl_update_render_state(d);
261 return true;
262 }
263
264 static int iphone_get_orientation(ALLEGRO_DISPLAY *d)
265 {
266 return _al_iphone_get_orientation(d);
267 }
268
269
270 /* There can be only one window and only one OpenGL context, so all bitmaps
271 * are compatible.
272 */
273 static bool iphone_is_compatible_bitmap(ALLEGRO_DISPLAY *display,
274 ALLEGRO_BITMAP *bitmap)
275 {
276 (void)display;
277 (void)bitmap;
278 return true;
279 }
280
281 /* Resizing is not possible. */
282 static bool iphone_resize_display(ALLEGRO_DISPLAY *d, int w, int h)
283 {
284 (void)d;
285 (void)w;
286 (void)h;
287 return false;
288 }
289
290 /* The icon must be provided in the Info.plist file, it cannot be changed
291 * at runtime.
292 */
293 static void iphone_set_icons(ALLEGRO_DISPLAY *d, int num_icons, ALLEGRO_BITMAP *bitmaps[])
294 {
295 (void)d;
296 (void)num_icons;
297 (void)bitmaps;
298 }
299
300 /* There is no way to leave fullscreen so no window title is visible. */
301 static void iphone_set_window_title(ALLEGRO_DISPLAY *display, char const *title)
302 {
303 (void)display;
304 (void)title;
305 }
306
307 /* The window always spans the entire screen right now. */
308 static void iphone_set_window_position(ALLEGRO_DISPLAY *display, int x, int y)
309 {
310 (void)display;
311 (void)x;
312 (void)y;
313 }
314
315 /* The window cannot be constrained. */
316 static bool iphone_set_window_constraints(ALLEGRO_DISPLAY *display,
317 int min_w, int min_h, int max_w, int max_h)
318 {
319 (void)display;
320 (void)min_w;
321 (void)min_h;
322 (void)max_w;
323 (void)max_h;
324 return false;
325 }
326
327 /* Always fullscreen. */
328 static bool iphone_set_display_flag(ALLEGRO_DISPLAY *display,
329 int flag, bool onoff)
330 {
331 (void)display;
332 (void)flag;
333 (void)onoff;
334 return false;
335 }
336
337 static void iphone_get_window_position(ALLEGRO_DISPLAY *display, int *x, int *y)
338 {
339 (void)display;
340 *x = 0;
341 *y = 0;
342 }
343
344 /* The window cannot be constrained. */
345 static bool iphone_get_window_constraints(ALLEGRO_DISPLAY *display,
346 int *min_w, int *min_h, int *max_w, int *max_h)
347 {
348 (void)display;
349 (void)min_w;
350 (void)min_h;
351 (void)max_w;
352 (void)max_h;
353 return false;
354 }
355
356 static bool iphone_wait_for_vsync(ALLEGRO_DISPLAY *display)
357 {
358 (void)display;
359 return false;
360 }
361
362 static void iphone_flip_display(ALLEGRO_DISPLAY *d)
363 {
364 _al_iphone_flip_view(d);
365 }
366
367 static void iphone_update_display_region(ALLEGRO_DISPLAY *d, int x, int y,
368 int w, int h)
369 {
370 (void)x;
371 (void)y;
372 (void)w;
373 (void)h;
374 iphone_flip_display(d);
375 }
376
377 static bool iphone_acknowledge_resize(ALLEGRO_DISPLAY *d)
378 {
379 _al_iphone_recreate_framebuffer(d);
380 _al_iphone_setup_opengl_view(d, true);
381 return true;
382 }
383
384 static bool iphone_set_mouse_cursor(ALLEGRO_DISPLAY *display,
385 ALLEGRO_MOUSE_CURSOR *cursor)
386 {
387 (void)display;
388 (void)cursor;
389 return false;
390 }
391
392 static bool iphone_set_system_mouse_cursor(ALLEGRO_DISPLAY *display,
393 ALLEGRO_SYSTEM_MOUSE_CURSOR cursor_id)
394 {
395 (void)display;
396 (void)cursor_id;
397 return false;
398 }
399
400 static bool iphone_show_mouse_cursor(ALLEGRO_DISPLAY *display)
401 {
402 (void)display;
403 return false;
404 }
405
406 static bool iphone_hide_mouse_cursor(ALLEGRO_DISPLAY *display)
407 {
408 (void)display;
409 return false;
410 }
411
412 /* Obtain a reference to this driver. */
413 ALLEGRO_DISPLAY_INTERFACE *_al_get_iphone_display_interface(void)
414 {
415 if (vt)
416 return vt;
417
418 vt = al_calloc(1, sizeof *vt);
419
420 vt->create_display = iphone_create_display;
421 vt->destroy_display = iphone_destroy_display;
422 vt->set_current_display = iphone_set_current_display;
423 vt->flip_display = iphone_flip_display;
424 vt->update_display_region = iphone_update_display_region;
425 vt->acknowledge_resize = iphone_acknowledge_resize;
426 vt->create_bitmap = _al_ogl_create_bitmap;
427 vt->get_backbuffer = _al_ogl_get_backbuffer;
428 vt->set_target_bitmap = _al_ogl_set_target_bitmap;
429
430 vt->get_orientation = iphone_get_orientation;
431
432 vt->is_compatible_bitmap = iphone_is_compatible_bitmap;
433 vt->resize_display = iphone_resize_display;
434 vt->set_icons = iphone_set_icons;
435 vt->set_window_title = iphone_set_window_title;
436 vt->set_window_position = iphone_set_window_position;
437 vt->get_window_position = iphone_get_window_position;
438 vt->set_window_constraints = iphone_set_window_constraints;
439 vt->get_window_constraints = iphone_get_window_constraints;
440 vt->set_display_flag = iphone_set_display_flag;
441 vt->wait_for_vsync = iphone_wait_for_vsync;
442
443 vt->set_mouse_cursor = iphone_set_mouse_cursor;
444 vt->set_system_mouse_cursor = iphone_set_system_mouse_cursor;
445 vt->show_mouse_cursor = iphone_show_mouse_cursor;
446 vt->hide_mouse_cursor = iphone_hide_mouse_cursor;
447
448 vt->acknowledge_drawing_halt = _al_iphone_acknowledge_drawing_halt;
449 vt->update_render_state = _al_ogl_update_render_state;
450
451 _al_ogl_add_drawing_functions(vt);
452 _al_iphone_add_clipboard_functions(vt);
453
454 return vt;
455 }