-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMGLContext.mm
326 lines (278 loc) · 7.38 KB
/
MGLContext.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
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
//
// MGLContext.m
// OpenGLES
//
// Created by Le Quyen on 16/10/19.
// Copyright © 2019 Google. All rights reserved.
//
#import "MGLContext.h"
#import "MGLContext+Private.h"
#include <pthread.h>
#include <vector>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <EGL/eglext_angle.h>
#include <EGL/eglplatform.h>
//#include <common/debug.h>
#import "MGLLayer+Private.h"
namespace
{
struct ThreadLocalInfo
{
__weak MGLContext *currentContext = nil;
__weak MGLLayer *currentLayer = nil;
};
#if TARGET_OS_SIMULATOR
pthread_key_t gThreadSpecificKey;
void ThreadTLSDestructor(void *data)
{
auto tlsData = reinterpret_cast<ThreadLocalInfo *>(data);
delete tlsData;
}
#endif
ThreadLocalInfo &CurrentTLS()
{
#if TARGET_OS_SIMULATOR
// There are some issuess with C++11 TLS could not be compiled on iOS
// simulator, so we have to fallback to use pthread TLS.
static pthread_once_t sKeyOnce = PTHREAD_ONCE_INIT;
pthread_once(&sKeyOnce, [] { pthread_key_create(&gThreadSpecificKey, ThreadTLSDestructor); });
auto tlsData = reinterpret_cast<ThreadLocalInfo *>(pthread_getspecific(gThreadSpecificKey));
if (!tlsData)
{
tlsData = new ThreadLocalInfo();
pthread_setspecific(gThreadSpecificKey, tlsData);
}
return *tlsData;
#else // TARGET_OS_SIMULATOR
static thread_local ThreadLocalInfo tls;
return tls;
#endif
}
void Throw(NSString *msg)
{
[NSException raise:@"MGLSurfaceException" format:@"%@", msg];
}
EGLContext CreateEGLContext(EGLDisplay display, MGLRenderingAPI api, EGLContext sharedContext)
{
// Init config
std::vector<EGLint> surfaceAttribs = {
EGL_RED_SIZE, EGL_DONT_CARE, EGL_GREEN_SIZE, EGL_DONT_CARE,
EGL_BLUE_SIZE, EGL_DONT_CARE, EGL_ALPHA_SIZE, EGL_DONT_CARE,
EGL_DEPTH_SIZE, EGL_DONT_CARE, EGL_STENCIL_SIZE, EGL_DONT_CARE,
EGL_SAMPLE_BUFFERS, EGL_DONT_CARE, EGL_SAMPLES, EGL_DONT_CARE,
};
surfaceAttribs.push_back(EGL_NONE);
EGLConfig config;
EGLint numConfigs;
if (!eglChooseConfig(display, surfaceAttribs.data(), &config, 1, &numConfigs) || numConfigs < 1)
{
Throw(@"Failed to call eglChooseConfig()");
}
// Init context
int ctxMajorVersion = 2;
int ctxMinorVersion = 0;
switch (api)
{
case kMGLRenderingAPIOpenGLES1:
ctxMajorVersion = 1;
ctxMinorVersion = 0;
break;
case kMGLRenderingAPIOpenGLES2:
ctxMajorVersion = 2;
ctxMinorVersion = 0;
break;
case kMGLRenderingAPIOpenGLES3:
ctxMajorVersion = 3;
ctxMinorVersion = 0;
break;
default:
abort();
}
EGLint ctxAttribs[] = {EGL_CONTEXT_MAJOR_VERSION, ctxMajorVersion, EGL_CONTEXT_MINOR_VERSION,
ctxMinorVersion, EGL_NONE};
EGLContext eglContext = eglCreateContext(display, config, sharedContext, ctxAttribs);
if (eglContext == EGL_NO_CONTEXT)
{
Throw(@"Failed to call eglCreateContext()");
}
return eglContext;
}
}
// MGLSharegroup implementation
@interface MGLSharegroup () {
__weak MGLContext *_firstContext;
EGLDisplay _sharedEGLDisplay;
EGLContext _sharedEGLContext;
}
// This shared context is only created when the shared group has more
// than one context.
- (EGLContext)sharedEGLContext;
- (void)addContext:(MGLContext *)context;
@end
@implementation MGLSharegroup
- (id)init
{
if (self = [super init])
{
_sharedEGLContext = EGL_NO_CONTEXT;
}
return self;
}
- (void)dealloc
{
if (_sharedEGLContext != EGL_NO_CONTEXT)
{
eglDestroyContext(_sharedEGLDisplay, _sharedEGLContext);
_sharedEGLContext = EGL_NO_CONTEXT;
}
}
- (EGLContext)sharedEGLContext
{
@synchronized(self)
{
return _sharedEGLContext;
}
}
- (void)addContext:(MGLContext *)context
{
@synchronized(self)
{
if (!_firstContext)
{
_firstContext = context;
}
else if (_sharedEGLContext == EGL_NO_CONTEXT)
{
// If we have more than one context in the shared group, create
// a "master" context to be shared by all contexts in the group.
_sharedEGLDisplay = _firstContext.display.eglDisplay;
_sharedEGLContext =
CreateEGLContext(_sharedEGLDisplay, _firstContext.API, _firstContext.eglContext);
}
}
}
@end
// MGLContext implementation
@implementation MGLContext
- (id)initWithAPI:(MGLRenderingAPI)api
{
return [self initWithAPI:api sharegroup:nil];
}
- (id)initWithAPI:(MGLRenderingAPI)api sharegroup:(MGLSharegroup *)sharegroup
{
if (self = [super init])
{
_renderingApi = api;
_display = [MGLDisplay defaultDisplay];
if (sharegroup)
{
_sharegroup = sharegroup;
}
else
{
_sharegroup = [MGLSharegroup new];
}
[_sharegroup addContext:self];
[self initContext];
}
return self;
}
- (MGLRenderingAPI)API
{
return _renderingApi;
}
- (EGLDisplay)eglDisplay
{
return _display.eglDisplay;
}
- (void)dealloc
{
[self releaseContext];
_display = nil;
}
- (void)releaseContext
{
if (eglGetCurrentContext() == _eglContext)
{
eglMakeCurrent(_display.eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
}
if (_eglContext != EGL_NO_CONTEXT)
{
eglDestroyContext(_display.eglDisplay, _eglContext);
_eglContext = EGL_NO_CONTEXT;
}
}
- (void)initContext
{
_eglContext =
CreateEGLContext(_display.eglDisplay, _renderingApi, _sharegroup.sharedEGLContext);
}
- (BOOL)present:(MGLLayer *)layer
{
return [layer present];
}
- (BOOL)renderbufferStorage:(NSUInteger)target fromDrawable:(id<EAGLDrawable>)drawable
{
return [self setCurrentContextForLayer:(id)drawable];
}
- (BOOL)presentRenderbuffer:(NSUInteger)target
{
return [self present:MGLContext.currentLayer];
}
+ (MGLContext *)currentContext
{
return CurrentTLS().currentContext;
}
+ (MGLLayer *)currentLayer
{
return CurrentTLS().currentLayer;
}
+ (BOOL)setCurrentContext:(MGLContext *)context
{
ThreadLocalInfo &tlsData = CurrentTLS();
if (context)
{
return [context setCurrentContextForLayer:tlsData.currentLayer];
}
// No context
tlsData.currentContext = nil;
tlsData.currentLayer = nil;
return eglMakeCurrent([MGLDisplay defaultDisplay].eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
EGL_NO_CONTEXT);
}
+ (BOOL)setCurrentContext:(MGLContext *)context forLayer:(MGLLayer *)layer
{
if (context)
{
return [context setCurrentContextForLayer:layer];
}
return [self setCurrentContext:nil];
}
- (BOOL)setCurrentContextForLayer:(MGLLayer *_Nullable)layer
{
if (!layer)
{
if (eglGetCurrentContext() != _eglContext ||
eglGetCurrentSurface(EGL_READ) != EGL_NO_SURFACE ||
eglGetCurrentSurface(EGL_DRAW) != EGL_NO_SURFACE)
{
if (!eglMakeCurrent(_display.eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, _eglContext))
{
return NO;
}
}
}
else
{
if (![layer setCurrentContext:self])
{
return NO;
}
}
ThreadLocalInfo &tlsData = CurrentTLS();
tlsData.currentContext = self;
tlsData.currentLayer = layer;
return YES;
}
@end