forked from bkaradzic/bgfx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglcontext_egl.cpp
476 lines (387 loc) · 16.2 KB
/
glcontext_egl.cpp
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
/*
* Copyright 2011-2019 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "bgfx_p.h"
#if (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)
# include "renderer_gl.h"
# if BGFX_USE_EGL
# if BX_PLATFORM_RPI
# include <X11/Xlib.h>
# include <bcm_host.h>
# endif // BX_PLATFORM_RPI
namespace bgfx { namespace gl
{
#ifndef EGL_CONTEXT_FLAG_NO_ERROR_BIT_KHR
# define EGL_CONTEXT_FLAG_NO_ERROR_BIT_KHR 0x00000008
#endif // EGL_CONTEXT_FLAG_NO_ERROR_BIT_KHR
#if BGFX_USE_GL_DYNAMIC_LIB
typedef void (*EGLPROC)(void);
typedef EGLBoolean (EGLAPIENTRY* PFNEGLCHOOSECONFIGPROC)(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config);
typedef EGLContext (EGLAPIENTRY* PFNEGLCREATECONTEXTPROC)(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list);
typedef EGLSurface (EGLAPIENTRY* PFNEGLCREATEWINDOWSURFACEPROC)(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint *attrib_list);
typedef EGLint (EGLAPIENTRY* PFNEGLGETERRORPROC)(void);
typedef EGLDisplay (EGLAPIENTRY* PFNEGLGETDISPLAYPROC)(EGLNativeDisplayType display_id);
typedef EGLPROC (EGLAPIENTRY* PFNEGLGETPROCADDRESSPROC)(const char *procname);
typedef EGLBoolean (EGLAPIENTRY* PFNEGLINITIALIZEPROC)(EGLDisplay dpy, EGLint *major, EGLint *minor);
typedef EGLBoolean (EGLAPIENTRY* PFNEGLMAKECURRENTPROC)(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx);
typedef EGLBoolean (EGLAPIENTRY* PFNEGLDESTROYCONTEXTPROC)(EGLDisplay dpy, EGLContext ctx);
typedef EGLBoolean (EGLAPIENTRY* PFNEGLDESTROYSURFACEPROC)(EGLDisplay dpy, EGLSurface surface);
typedef const char* (EGLAPIENTRY* PGNEGLQUERYSTRINGPROC)(EGLDisplay dpy, EGLint name);
typedef EGLBoolean (EGLAPIENTRY* PFNEGLSWAPBUFFERSPROC)(EGLDisplay dpy, EGLSurface surface);
typedef EGLBoolean (EGLAPIENTRY* PFNEGLSWAPINTERVALPROC)(EGLDisplay dpy, EGLint interval);
typedef EGLBoolean (EGLAPIENTRY* PFNEGLTERMINATEPROC)(EGLDisplay dpy);
#define EGL_IMPORT \
EGL_IMPORT_FUNC(PFNEGLCHOOSECONFIGPROC, eglChooseConfig); \
EGL_IMPORT_FUNC(PFNEGLCREATECONTEXTPROC, eglCreateContext); \
EGL_IMPORT_FUNC(PFNEGLCREATEWINDOWSURFACEPROC, eglCreateWindowSurface); \
EGL_IMPORT_FUNC(PFNEGLGETDISPLAYPROC, eglGetDisplay); \
EGL_IMPORT_FUNC(PFNEGLGETERRORPROC, eglGetError); \
EGL_IMPORT_FUNC(PFNEGLGETPROCADDRESSPROC, eglGetProcAddress); \
EGL_IMPORT_FUNC(PFNEGLDESTROYCONTEXTPROC, eglDestroyContext); \
EGL_IMPORT_FUNC(PFNEGLDESTROYSURFACEPROC, eglDestroySurface); \
EGL_IMPORT_FUNC(PFNEGLINITIALIZEPROC, eglInitialize); \
EGL_IMPORT_FUNC(PFNEGLMAKECURRENTPROC, eglMakeCurrent); \
EGL_IMPORT_FUNC(PGNEGLQUERYSTRINGPROC, eglQueryString); \
EGL_IMPORT_FUNC(PFNEGLSWAPBUFFERSPROC, eglSwapBuffers); \
EGL_IMPORT_FUNC(PFNEGLSWAPINTERVALPROC, eglSwapInterval); \
EGL_IMPORT_FUNC(PFNEGLTERMINATEPROC, eglTerminate);
#define EGL_IMPORT_FUNC(_proto, _func) _proto _func
EGL_IMPORT
#undef EGL_IMPORT_FUNC
void* eglOpen()
{
void* handle = bx::dlopen("libEGL." BX_DL_EXT);
BGFX_FATAL(NULL != handle, Fatal::UnableToInitialize, "Failed to load libEGL dynamic library.");
#define EGL_IMPORT_FUNC(_proto, _func) \
_func = (_proto)bx::dlsym(handle, #_func); \
BX_TRACE("%p " #_func, _func); \
BGFX_FATAL(NULL != _func, Fatal::UnableToInitialize, "Failed get " #_func ".")
EGL_IMPORT
#undef EGL_IMPORT_FUNC
return handle;
}
void eglClose(void* _handle)
{
bx::dlclose(_handle);
#define EGL_IMPORT_FUNC(_proto, _func) _func = NULL
EGL_IMPORT
#undef EGL_IMPORT_FUNC
}
#else
void* eglOpen()
{
return NULL;
}
void eglClose(void* /*_handle*/)
{
}
#endif // BGFX_USE_GL_DYNAMIC_LIB
# define GL_IMPORT(_optional, _proto, _func, _import) _proto _func = NULL
# include "glimports.h"
static EGLint s_contextAttrs[16];
struct SwapChainGL
{
SwapChainGL(EGLDisplay _display, EGLConfig _config, EGLContext _context, EGLNativeWindowType _nwh)
: m_nwh(_nwh)
, m_display(_display)
{
EGLSurface defaultSurface = eglGetCurrentSurface(EGL_DRAW);
m_surface = eglCreateWindowSurface(m_display, _config, _nwh, NULL);
BGFX_FATAL(m_surface != EGL_NO_SURFACE, Fatal::UnableToInitialize, "Failed to create surface.");
m_context = eglCreateContext(m_display, _config, _context, s_contextAttrs);
BX_CHECK(NULL != m_context, "Create swap chain failed: %x", eglGetError() );
makeCurrent();
GL_CHECK(glClearColor(0.0f, 0.0f, 0.0f, 0.0f) );
GL_CHECK(glClear(GL_COLOR_BUFFER_BIT) );
swapBuffers();
GL_CHECK(glClear(GL_COLOR_BUFFER_BIT) );
swapBuffers();
eglMakeCurrent(m_display, defaultSurface, defaultSurface, _context);
}
~SwapChainGL()
{
EGLSurface defaultSurface = eglGetCurrentSurface(EGL_DRAW);
EGLContext defaultContext = eglGetCurrentContext();
eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroyContext(m_display, m_context);
eglDestroySurface(m_display, m_surface);
eglMakeCurrent(m_display, defaultSurface, defaultSurface, defaultContext);
}
void makeCurrent()
{
eglMakeCurrent(m_display, m_surface, m_surface, m_context);
}
void swapBuffers()
{
eglSwapBuffers(m_display, m_surface);
}
EGLNativeWindowType m_nwh;
EGLContext m_context;
EGLDisplay m_display;
EGLSurface m_surface;
};
# if BX_PLATFORM_RPI
static EGL_DISPMANX_WINDOW_T s_dispmanWindow;
# endif // BX_PLATFORM_RPI
void GlContext::create(uint32_t _width, uint32_t _height)
{
# if BX_PLATFORM_RPI
bcm_host_init();
# endif // BX_PLATFORM_RPI
m_eglLibrary = eglOpen();
if (NULL == g_platformData.context)
{
# if BX_PLATFORM_RPI
g_platformData.ndt = EGL_DEFAULT_DISPLAY;
# endif // BX_PLATFORM_RPI
BX_UNUSED(_width, _height);
EGLNativeDisplayType ndt = (EGLNativeDisplayType)g_platformData.ndt;
EGLNativeWindowType nwh = (EGLNativeWindowType )g_platformData.nwh;
# if BX_PLATFORM_WINDOWS
if (NULL == g_platformData.ndt)
{
ndt = GetDC( (HWND)g_platformData.nwh);
}
# endif // BX_PLATFORM_WINDOWS
m_display = eglGetDisplay(ndt);
BGFX_FATAL(m_display != EGL_NO_DISPLAY, Fatal::UnableToInitialize, "Failed to create display %p", m_display);
EGLint major = 0;
EGLint minor = 0;
EGLBoolean success = eglInitialize(m_display, &major, &minor);
BGFX_FATAL(success && major >= 1 && minor >= 3, Fatal::UnableToInitialize, "Failed to initialize %d.%d", major, minor);
BX_TRACE("EGL info:");
const char* clientApis = eglQueryString(m_display, EGL_CLIENT_APIS);
BX_TRACE(" APIs: %s", clientApis); BX_UNUSED(clientApis);
const char* vendor = eglQueryString(m_display, EGL_VENDOR);
BX_TRACE(" Vendor: %s", vendor); BX_UNUSED(vendor);
const char* version = eglQueryString(m_display, EGL_VERSION);
BX_TRACE("Version: %s", version); BX_UNUSED(version);
const char* extensions = eglQueryString(m_display, EGL_EXTENSIONS);
BX_TRACE("Supported EGL extensions:");
dumpExtensions(extensions);
// https://www.khronos.org/registry/EGL/extensions/ANDROID/EGL_ANDROID_recordable.txt
const bool hasEglAndroidRecordable = !bx::findIdentifierMatch(extensions, "EGL_ANDROID_recordable").isEmpty();
EGLint attrs[] =
{
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
# if BX_PLATFORM_ANDROID
EGL_DEPTH_SIZE, 16,
# else
EGL_DEPTH_SIZE, 24,
# endif // BX_PLATFORM_
EGL_STENCIL_SIZE, 8,
// Android Recordable surface
hasEglAndroidRecordable ? 0x3142 : EGL_NONE,
hasEglAndroidRecordable ? 1 : EGL_NONE,
EGL_NONE
};
EGLint numConfig = 0;
success = eglChooseConfig(m_display, attrs, &m_config, 1, &numConfig);
BGFX_FATAL(success, Fatal::UnableToInitialize, "eglChooseConfig");
# if BX_PLATFORM_ANDROID
EGLint format;
eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format);
ANativeWindow_setBuffersGeometry( (ANativeWindow*)g_platformData.nwh, _width, _height, format);
# elif BX_PLATFORM_RPI
DISPMANX_DISPLAY_HANDLE_T dispmanDisplay = vc_dispmanx_display_open(0);
DISPMANX_UPDATE_HANDLE_T dispmanUpdate = vc_dispmanx_update_start(0);
VC_RECT_T dstRect = { 0, 0, int32_t(_width), int32_t(_height) };
VC_RECT_T srcRect = { 0, 0, int32_t(_width) << 16, int32_t(_height) << 16 };
DISPMANX_ELEMENT_HANDLE_T dispmanElement = vc_dispmanx_element_add(dispmanUpdate
, dispmanDisplay
, 0
, &dstRect
, 0
, &srcRect
, DISPMANX_PROTECTION_NONE
, NULL
, NULL
, DISPMANX_NO_ROTATE
);
s_dispmanWindow.element = dispmanElement;
s_dispmanWindow.width = _width;
s_dispmanWindow.height = _height;
nwh = &s_dispmanWindow;
vc_dispmanx_update_submit_sync(dispmanUpdate);
# endif // BX_PLATFORM_ANDROID
m_surface = eglCreateWindowSurface(m_display, m_config, nwh, NULL);
BGFX_FATAL(m_surface != EGL_NO_SURFACE, Fatal::UnableToInitialize, "Failed to create surface.");
const bool hasEglKhrCreateContext = !bx::findIdentifierMatch(extensions, "EGL_KHR_create_context").isEmpty();
const bool hasEglKhrNoError = !bx::findIdentifierMatch(extensions, "EGL_KHR_create_context_no_error").isEmpty();
const uint32_t gles = BGFX_CONFIG_RENDERER_OPENGLES;
for (uint32_t ii = 0; ii < 2; ++ii)
{
bx::StaticMemoryBlockWriter writer(s_contextAttrs, sizeof(s_contextAttrs) );
EGLint flags = 0;
# if BX_PLATFORM_RPI
BX_UNUSED(hasEglKhrCreateContext, hasEglKhrNoError);
# else
if (hasEglKhrCreateContext)
{
bx::write(&writer, EGLint(EGL_CONTEXT_MAJOR_VERSION_KHR) );
bx::write(&writer, EGLint(gles / 10) );
bx::write(&writer, EGLint(EGL_CONTEXT_MINOR_VERSION_KHR) );
bx::write(&writer, EGLint(gles % 10) );
flags |= BGFX_CONFIG_DEBUG && hasEglKhrNoError ? 0
| EGL_CONTEXT_FLAG_NO_ERROR_BIT_KHR
: 0
;
if (0 == ii)
{
flags |= BGFX_CONFIG_DEBUG ? 0
| EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR
// | EGL_OPENGL_ES3_BIT_KHR
: 0
;
bx::write(&writer, EGLint(EGL_CONTEXT_FLAGS_KHR) );
bx::write(&writer, flags);
}
}
else
# endif // BX_PLATFORM_RPI
{
bx::write(&writer, EGLint(EGL_CONTEXT_CLIENT_VERSION) );
bx::write(&writer, 2);
}
bx::write(&writer, EGLint(EGL_NONE) );
m_context = eglCreateContext(m_display, m_config, EGL_NO_CONTEXT, s_contextAttrs);
if (NULL != m_context)
{
break;
}
BX_TRACE("Failed to create EGL context with EGL_CONTEXT_FLAGS_KHR (%08x).", flags);
}
BGFX_FATAL(m_context != EGL_NO_CONTEXT, Fatal::UnableToInitialize, "Failed to create context.");
success = eglMakeCurrent(m_display, m_surface, m_surface, m_context);
BGFX_FATAL(success, Fatal::UnableToInitialize, "Failed to set context.");
m_current = NULL;
eglSwapInterval(m_display, 0);
}
import();
g_internalData.context = m_context;
}
void GlContext::destroy()
{
if (NULL != m_display)
{
eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroyContext(m_display, m_context);
eglDestroySurface(m_display, m_surface);
eglTerminate(m_display);
m_context = NULL;
}
eglClose(m_eglLibrary);
# if BX_PLATFORM_RPI
bcm_host_deinit();
# endif // BX_PLATFORM_RPI
}
void GlContext::resize(uint32_t _width, uint32_t _height, uint32_t _flags)
{
# if BX_PLATFORM_ANDROID
if (NULL != m_display)
{
EGLNativeWindowType nwh = (EGLNativeWindowType )g_platformData.nwh;
eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroySurface(m_display, m_surface);
m_surface = eglCreateWindowSurface(m_display, m_config, nwh, NULL);
BGFX_FATAL(m_surface != EGL_NO_SURFACE, Fatal::UnableToInitialize, "Failed to create surface.");
EGLBoolean success = eglMakeCurrent(m_display, m_surface, m_surface, m_context);
BGFX_FATAL(success, Fatal::UnableToInitialize, "Failed to set context.");
EGLint format;
eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format);
ANativeWindow_setBuffersGeometry( (ANativeWindow*)g_platformData.nwh, _width, _height, format);
}
# elif BX_PLATFORM_EMSCRIPTEN
emscripten_set_canvas_size(_width, _height);
# else
BX_UNUSED(_width, _height);
# endif // BX_PLATFORM_*
if (NULL != m_display)
{
bool vsync = !!(_flags&BGFX_RESET_VSYNC);
eglSwapInterval(m_display, vsync ? 1 : 0);
}
}
uint64_t GlContext::getCaps() const
{
return BX_ENABLED(0
| BX_PLATFORM_LINUX
| BX_PLATFORM_WINDOWS
| BX_PLATFORM_ANDROID
)
? BGFX_CAPS_SWAP_CHAIN
: 0
;
}
SwapChainGL* GlContext::createSwapChain(void* _nwh)
{
return BX_NEW(g_allocator, SwapChainGL)(m_display, m_config, m_context, (EGLNativeWindowType)_nwh);
}
void GlContext::destroySwapChain(SwapChainGL* _swapChain)
{
BX_DELETE(g_allocator, _swapChain);
}
void GlContext::swap(SwapChainGL* _swapChain)
{
makeCurrent(_swapChain);
if (NULL == _swapChain)
{
if (NULL != m_display)
{
eglSwapBuffers(m_display, m_surface);
}
}
else
{
_swapChain->swapBuffers();
}
}
void GlContext::makeCurrent(SwapChainGL* _swapChain)
{
if (m_current != _swapChain)
{
m_current = _swapChain;
if (NULL == _swapChain)
{
if (NULL != m_display)
{
eglMakeCurrent(m_display, m_surface, m_surface, m_context);
}
}
else
{
_swapChain->makeCurrent();
}
}
}
void GlContext::import()
{
BX_TRACE("Import:");
# if BX_PLATFORM_WINDOWS || BX_PLATFORM_LINUX
void* glesv2 = bx::dlopen("libGLESv2." BX_DL_EXT);
# define GL_EXTENSION(_optional, _proto, _func, _import) \
{ \
if (NULL == _func) \
{ \
_func = (_proto)bx::dlsym(glesv2, #_import); \
BX_TRACE("\t%p " #_func " (" #_import ")", _func); \
BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGLES context. eglGetProcAddress(\"%s\")", #_import); \
} \
}
# else
# define GL_EXTENSION(_optional, _proto, _func, _import) \
{ \
if (NULL == _func) \
{ \
_func = (_proto)eglGetProcAddress(#_import); \
BX_TRACE("\t%p " #_func " (" #_import ")", _func); \
BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGLES context. eglGetProcAddress(\"%s\")", #_import); \
} \
}
# endif // BX_PLATFORM_
# include "glimports.h"
}
} /* namespace gl */ } // namespace bgfx
# endif // BGFX_USE_EGL
#endif // (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)