/
IOSGraphics.java
682 lines (572 loc) · 18.2 KB
/
IOSGraphics.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.badlogic.gdx.backends.iosrobovm;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Graphics;
import com.badlogic.gdx.LifecycleListener;
import com.badlogic.gdx.backends.iosrobovm.custom.HWMachine;
import com.badlogic.gdx.graphics.Cursor;
import com.badlogic.gdx.graphics.Cursor.SystemCursor;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.glutils.GLVersion;
import com.badlogic.gdx.utils.Array;
import org.robovm.apple.coregraphics.CGRect;
import org.robovm.apple.foundation.Foundation;
import org.robovm.apple.foundation.NSObject;
import org.robovm.apple.foundation.NSSet;
import org.robovm.apple.glkit.GLKView;
import org.robovm.apple.glkit.GLKViewController;
import org.robovm.apple.glkit.GLKViewControllerDelegate;
import org.robovm.apple.glkit.GLKViewDelegate;
import org.robovm.apple.glkit.GLKViewDrawableColorFormat;
import org.robovm.apple.glkit.GLKViewDrawableDepthFormat;
import org.robovm.apple.glkit.GLKViewDrawableMultisample;
import org.robovm.apple.glkit.GLKViewDrawableStencilFormat;
import org.robovm.apple.opengles.EAGLContext;
import org.robovm.apple.opengles.EAGLRenderingAPI;
import org.robovm.apple.uikit.UIApplication;
import org.robovm.apple.uikit.UIEdgeInsets;
import org.robovm.apple.uikit.UIEvent;
import org.robovm.apple.uikit.UIInterfaceOrientation;
import org.robovm.apple.uikit.UIInterfaceOrientationMask;
import org.robovm.apple.uikit.UIPress;
import org.robovm.apple.uikit.UIPressesEvent;
import org.robovm.apple.uikit.UIRectEdge;
import org.robovm.apple.uikit.UIView;
import org.robovm.objc.Selector;
import org.robovm.objc.annotation.BindSelector;
import org.robovm.objc.annotation.Method;
import org.robovm.rt.bro.annotation.Callback;
import org.robovm.rt.bro.annotation.Pointer;
public class IOSGraphics extends NSObject implements Graphics, GLKViewDelegate, GLKViewControllerDelegate {
private static final String tag = "IOSGraphics";
public static class IOSUIViewController extends GLKViewController {
final IOSApplication app;
final IOSGraphics graphics;
protected IOSUIViewController (IOSApplication app, IOSGraphics graphics) {
this.app = app;
this.graphics = graphics;
}
@Override
public void viewWillAppear (boolean arg0) {
super.viewWillAppear(arg0);
// start GLKViewController even though we may only draw a single frame
// (we may be in non-continuous mode)
setPaused(false);
}
@Override
public void viewDidAppear (boolean animated) {
super.viewDidAppear(animated);
if (app.viewControllerListener != null) app.viewControllerListener.viewDidAppear(animated);
}
@Override
public UIInterfaceOrientationMask getSupportedInterfaceOrientations () {
long mask = 0;
if (app.config.orientationLandscape) {
mask |= ((1 << UIInterfaceOrientation.LandscapeLeft.value()) | (1 << UIInterfaceOrientation.LandscapeRight.value()));
}
if (app.config.orientationPortrait) {
mask |= ((1 << UIInterfaceOrientation.Portrait.value()) | (1 << UIInterfaceOrientation.PortraitUpsideDown.value()));
}
return new UIInterfaceOrientationMask(mask);
}
@Override
public boolean shouldAutorotate () {
return true;
}
public boolean shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation orientation) {
// we return "true" if we support the orientation
switch (orientation) {
case LandscapeLeft:
case LandscapeRight:
return app.config.orientationLandscape;
default:
// assume portrait
return app.config.orientationPortrait;
}
}
@Override
public UIRectEdge getPreferredScreenEdgesDeferringSystemGestures() {
return app.config.screenEdgesDeferringSystemGestures;
}
@Override
public void viewDidLayoutSubviews () {
super.viewDidLayoutSubviews();
// get the view size and update graphics
final IOSScreenBounds oldBounds = graphics.screenBounds;
final IOSScreenBounds newBounds = app.computeBounds();
graphics.screenBounds = newBounds;
// Layout may happen without bounds changing, don't trigger resize in that case
if (graphics.created && (newBounds.width != oldBounds.width || newBounds.height != oldBounds.height)) {
graphics.makeCurrent();
graphics.updateSafeInsets();
app.listener.resize(newBounds.width, newBounds.height);
}
}
@Override
public boolean prefersStatusBarHidden () {
return !app.config.statusBarVisible;
}
@Override
public boolean prefersHomeIndicatorAutoHidden() {
return app.config.hideHomeIndicator;
}
@Callback
@BindSelector("shouldAutorotateToInterfaceOrientation:")
private static boolean shouldAutorotateToInterfaceOrientation (IOSUIViewController self, Selector sel,
UIInterfaceOrientation orientation) {
return self.shouldAutorotateToInterfaceOrientation(orientation);
}
@Override
public void pressesBegan(NSSet<UIPress> presses, UIPressesEvent event) {
if (presses == null || presses.isEmpty() || !app.input.onKey(presses.getValues().first().getKey(), true)) {
super.pressesBegan(presses, event);
}
}
@Override
public void pressesEnded(NSSet<UIPress> presses, UIPressesEvent event) {
if (presses == null || presses.isEmpty() || !app.input.onKey(presses.getValues().first().getKey(), false)) {
super.pressesEnded(presses, event);
}
}
}
IOSApplication app;
IOSInput input;
GL20 gl20;
GL30 gl30;
IOSScreenBounds screenBounds;
int safeInsetLeft, safeInsetTop, safeInsetBottom, safeInsetRight;
long lastFrameTime;
float deltaTime;
long framesStart;
int frames;
int fps;
BufferFormat bufferFormat;
String extensions;
private float ppiX = 0;
private float ppiY = 0;
private float ppcX = 0;
private float ppcY = 0;
private float density = 1;
volatile boolean appPaused;
private long frameId = -1;
private boolean isContinuous = true;
private boolean isFrameRequested = true;
IOSApplicationConfiguration config;
EAGLContext context;
GLVersion glVersion;
GLKView view;
IOSUIViewController viewController;
public IOSGraphics (IOSApplication app, IOSApplicationConfiguration config, IOSInput input, boolean useGLES30) {
this.config = config;
// setup view and OpenGL
screenBounds = app.computeBounds();
if (useGLES30) {
context = new EAGLContext(EAGLRenderingAPI.OpenGLES3);
if (context != null)
gl20 = gl30 = new IOSGLES30();
else
Gdx.app.log("IOGraphics", "OpenGL ES 3.0 not supported, falling back on 2.0");
}
if (context == null) {
context = new EAGLContext(EAGLRenderingAPI.OpenGLES2);
gl20 = new IOSGLES20();
gl30 = null;
}
view = new GLKView(new CGRect(0, 0, screenBounds.width, screenBounds.height), context) {
@Method(selector = "touchesBegan:withEvent:")
public void touchesBegan (@Pointer long touches, UIEvent event) {
IOSGraphics.this.input.onTouch(touches);
}
@Method(selector = "touchesCancelled:withEvent:")
public void touchesCancelled (@Pointer long touches, UIEvent event) {
IOSGraphics.this.input.onTouch(touches);
}
@Method(selector = "touchesEnded:withEvent:")
public void touchesEnded (@Pointer long touches, UIEvent event) {
IOSGraphics.this.input.onTouch(touches);
}
@Method(selector = "touchesMoved:withEvent:")
public void touchesMoved (@Pointer long touches, UIEvent event) {
IOSGraphics.this.input.onTouch(touches);
}
@Override
public void draw (CGRect rect) {
IOSGraphics.this.draw(this, rect);
}
};
view.setDelegate(this);
view.setDrawableColorFormat(config.colorFormat);
view.setDrawableDepthFormat(config.depthFormat);
view.setDrawableStencilFormat(config.stencilFormat);
view.setDrawableMultisample(config.multisample);
view.setMultipleTouchEnabled(true);
viewController = app.createUIViewController(this);
viewController.setView(view);
viewController.setDelegate(this);
viewController.setPreferredFramesPerSecond(config.preferredFramesPerSecond);
this.app = app;
this.input = input;
int r = 0, g = 0, b = 0, a = 0, depth = 0, stencil = 0, samples = 0;
if (config.colorFormat == GLKViewDrawableColorFormat.RGB565) {
r = 5;
g = 6;
b = 5;
a = 0;
} else {
r = g = b = a = 8;
}
if (config.depthFormat == GLKViewDrawableDepthFormat._16) {
depth = 16;
} else if (config.depthFormat == GLKViewDrawableDepthFormat._24) {
depth = 24;
} else {
depth = 0;
}
if (config.stencilFormat == GLKViewDrawableStencilFormat._8) {
stencil = 8;
}
if (config.multisample == GLKViewDrawableMultisample._4X) {
samples = 4;
}
bufferFormat = new BufferFormat(r, g, b, a, depth, stencil, samples, false);
String machineString = HWMachine.getMachineString();
IOSDevice device = config.knownDevices.get(machineString);
if (device == null) app.error(tag, "Machine ID: " + machineString + " not found, please report to LibGDX");
int ppi = device != null ? device.ppi : 163;
density = device != null ? device.ppi/160f : app.pixelsPerPoint;
ppiX = ppi;
ppiY = ppi;
ppcX = ppiX / 2.54f;
ppcY = ppiY / 2.54f;
app.debug(tag, "Display: ppi=" + ppi + ", density=" + density);
// time + FPS
lastFrameTime = System.nanoTime();
framesStart = lastFrameTime;
appPaused = false;
}
public void resume () {
if (!appPaused) return;
appPaused = false;
Array<LifecycleListener> listeners = app.lifecycleListeners;
synchronized (listeners) {
for (LifecycleListener listener : listeners) {
listener.resume();
}
}
app.listener.resume();
}
public void pause () {
if (appPaused) return;
appPaused = true;
Array<LifecycleListener> listeners = app.lifecycleListeners;
synchronized (listeners) {
for (LifecycleListener listener : listeners) {
listener.pause();
}
}
app.listener.pause();
}
boolean created = false;
@Override
public void draw (GLKView view, CGRect rect) {
makeCurrent();
// massive hack, GLKView resets the viewport on each draw call, so IOSGLES20
// stores the last known viewport and we reset it here...
gl20.glViewport(IOSGLES20.x, IOSGLES20.y, IOSGLES20.width, IOSGLES20.height);
if (!created) {
final int width = screenBounds.width;
final int height = screenBounds.height;
gl20.glViewport(0, 0, width, height);
String versionString = gl20.glGetString(GL20.GL_VERSION);
String vendorString = gl20.glGetString(GL20.GL_VENDOR);
String rendererString = gl20.glGetString(GL20.GL_RENDERER);
glVersion = new GLVersion(Application.ApplicationType.iOS, versionString, vendorString, rendererString);
updateSafeInsets();
app.listener.create();
app.listener.resize(width, height);
created = true;
}
if (appPaused) {
return;
}
long time = System.nanoTime();
deltaTime = (time - lastFrameTime) / 1000000000.0f;
lastFrameTime = time;
frames++;
if (time - framesStart >= 1000000000l) {
framesStart = time;
fps = frames;
frames = 0;
}
input.processEvents();
frameId++;
app.listener.render();
}
void makeCurrent () {
EAGLContext.setCurrentContext(context);
}
@Override
public void update (GLKViewController controller) {
makeCurrent();
app.processRunnables();
// pause the GLKViewController render loop if we are no longer continuous
// and if we haven't requested a frame in the last loop iteration
if (!isContinuous && !isFrameRequested) {
viewController.setPaused(true);
}
isFrameRequested = false;
}
@Override
public void willPause (GLKViewController controller, boolean pause) {
}
@Override
public GL20 getGL20 () {
return gl20;
}
@Override
public void setGL20 (GL20 gl20) {
this.gl20 = gl20;
if (gl30 == null) {
Gdx.gl = gl20;
Gdx.gl20 = gl20;
}
}
@Override
public boolean isGL30Available () {
return gl30 != null;
}
@Override
public GL30 getGL30 () {
return gl30;
}
@Override
public void setGL30 (GL30 gl30) {
this.gl30 = gl30;
if (gl30 != null) {
this.gl20 = gl30;
Gdx.gl = gl20;
Gdx.gl20 = gl20;
Gdx.gl30 = gl30;
}
}
@Override
public int getWidth () {
return screenBounds.width;
}
@Override
public int getHeight () {
return screenBounds.height;
}
@Override
public int getBackBufferWidth() {
return screenBounds.backBufferWidth;
}
@Override
public int getBackBufferHeight() {
return screenBounds.backBufferHeight;
}
/** @return amount of pixels per point */
public float getBackBufferScale() {
return app.pixelsPerPoint;
}
@Override
public float getDeltaTime () {
return deltaTime;
}
@Override
public float getRawDeltaTime () {
return deltaTime;
}
@Override
public int getFramesPerSecond () {
return fps;
}
@Override
public GraphicsType getType () {
return GraphicsType.iOSGL;
}
@Override
public GLVersion getGLVersion () {
return glVersion;
}
@Override
public float getPpiX () {
return ppiX;
}
@Override
public float getPpiY () {
return ppiY;
}
@Override
public float getPpcX () {
return ppcX;
}
@Override
public float getPpcY () {
return ppcY;
}
@Override
public float getDensity () {
return density;
}
@Override
public boolean supportsDisplayModeChange () {
return false;
}
@Override
public DisplayMode[] getDisplayModes () {
return new DisplayMode[] {getDisplayMode()};
}
@Override
public DisplayMode getDisplayMode () {
return new IOSDisplayMode(getWidth(), getHeight(), config.preferredFramesPerSecond, bufferFormat.r + bufferFormat.g
+ bufferFormat.b + bufferFormat.a);
}
@Override
public Monitor getPrimaryMonitor() {
return new IOSMonitor(0, 0, "Primary Monitor");
}
@Override
public Monitor getMonitor() {
return getPrimaryMonitor();
}
@Override
public Monitor[] getMonitors() {
return new Monitor[] { getPrimaryMonitor() };
}
@Override
public DisplayMode[] getDisplayModes(Monitor monitor) {
return getDisplayModes();
}
@Override
public DisplayMode getDisplayMode(Monitor monitor) {
return getDisplayMode();
}
protected void updateSafeInsets() {
safeInsetTop = 0;
safeInsetLeft = 0;
safeInsetRight = 0;
safeInsetBottom = 0;
if (Foundation.getMajorSystemVersion() >= 11) {
UIView view = UIApplication.getSharedApplication().getKeyWindow().getRootViewController().getView();
UIEdgeInsets edgeInsets = view.getSafeAreaInsets();
double top = edgeInsets.getTop() * view.getContentScaleFactor();
double bottom = edgeInsets.getBottom() * view.getContentScaleFactor();
double left = edgeInsets.getLeft() * view.getContentScaleFactor();
double right = edgeInsets.getRight() * view.getContentScaleFactor();
safeInsetTop = (int) top;
safeInsetLeft = (int) left;
safeInsetRight = (int) right;
safeInsetBottom = (int) bottom;
}
}
@Override
public int getSafeInsetLeft() {
return safeInsetLeft;
}
@Override
public int getSafeInsetTop() {
return safeInsetTop;
}
@Override
public int getSafeInsetBottom() {
return safeInsetBottom;
}
@Override
public int getSafeInsetRight() {
return safeInsetRight;
}
@Override
public boolean setFullscreenMode (DisplayMode displayMode) {
return false;
}
@Override
public boolean setWindowedMode (int width, int height) {
return false;
}
@Override
public void setTitle (String title) {
}
@Override
public void setUndecorated(boolean undecorated) {
}
@Override
public void setResizable(boolean resizable) {
}
@Override
public void setVSync (boolean vsync) {
}
@Override
public BufferFormat getBufferFormat () {
return bufferFormat;
}
@Override
public boolean supportsExtension (String extension) {
if (extensions == null) extensions = Gdx.gl.glGetString(GL20.GL_EXTENSIONS);
return extensions.contains(extension);
}
@Override
public void setContinuousRendering (boolean isContinuous) {
if (isContinuous != this.isContinuous) {
this.isContinuous = isContinuous;
// start the GLKViewController if we go from non-continuous -> continuous
if (isContinuous) viewController.setPaused(false);
}
}
@Override
public boolean isContinuousRendering () {
return isContinuous;
}
@Override
public void requestRendering () {
isFrameRequested = true;
// start the GLKViewController if we are in non-continuous mode
// (we should already be started in continuous mode)
if (!isContinuous) viewController.setPaused(false);
}
@Override
public boolean isFullscreen () {
return true;
}
@Override
public long getFrameId () {
return frameId;
}
@Override
public Cursor newCursor (Pixmap pixmap, int xHotspot, int yHotspot) {
return null;
}
@Override
public void setCursor (Cursor cursor) {
}
@Override
public void setSystemCursor (SystemCursor systemCursor) {
}
private class IOSDisplayMode extends DisplayMode {
protected IOSDisplayMode (int width, int height, int refreshRate, int bitsPerPixel) {
super(width, height, refreshRate, bitsPerPixel);
}
}
private class IOSMonitor extends Monitor {
protected IOSMonitor(int virtualX, int virtualY, String name) {
super(virtualX, virtualY, name);
}
}
}