-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMGLDisplay.mm
90 lines (74 loc) · 1.65 KB
/
MGLDisplay.mm
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
//
// Copyright 2019 Le Hoang Quyen. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#import "MGLDisplay.h"
namespace
{
void Throw(NSString *msg)
{
[NSException raise:@"MGLSurfaceException" format:@"%@", msg];
}
}
// EGLDisplayHolder
@interface EGLDisplayHolder : NSObject
@property(nonatomic) EGLDisplay eglDisplay;
@end
@implementation EGLDisplayHolder
- (id)init
{
if (self = [super init])
{
// Init display
_eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (_eglDisplay == EGL_NO_DISPLAY)
{
Throw(@"Failed To call eglGetPlatformDisplay()");
}
if (!eglInitialize(_eglDisplay, NULL, NULL))
{
Throw(@"Failed To call eglInitialize()");
}
}
return self;
}
- (void)dealloc
{
if (_eglDisplay != EGL_NO_DISPLAY)
{
eglTerminate(_eglDisplay);
_eglDisplay = EGL_NO_DISPLAY;
}
}
@end
static EGLDisplayHolder *gGlobalDisplayHolder;
static MGLDisplay *gDefaultDisplay;
// MGLDisplay implementation
@interface MGLDisplay () {
EGLDisplayHolder *_eglDisplayHolder;
}
@end
@implementation MGLDisplay
+ (MGLDisplay *)defaultDisplay
{
if (!gDefaultDisplay)
{
gDefaultDisplay = [[MGLDisplay alloc] init];
}
return gDefaultDisplay;
}
- (id)init
{
if (self = [super init])
{
if (!gGlobalDisplayHolder)
{
gGlobalDisplayHolder = [[EGLDisplayHolder alloc] init];
}
_eglDisplayHolder = gGlobalDisplayHolder;
_eglDisplay = _eglDisplayHolder.eglDisplay;
}
return self;
}
@end