glcontext_egl.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * Copyright 2011-2024 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include "bgfx_p.h"
  6. #if (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)
  7. # include "renderer_gl.h"
  8. # if BGFX_USE_EGL
  9. # if BX_PLATFORM_RPI
  10. # include <X11/Xlib.h>
  11. # include <bcm_host.h>
  12. # endif // BX_PLATFORM_RPI
  13. #define _EGL_CHECK(_check, _call) \
  14. BX_MACRO_BLOCK_BEGIN \
  15. EGLBoolean success = _call; \
  16. _check(success, #_call "; EGL error 0x%x", eglGetError() ); \
  17. BX_MACRO_BLOCK_END
  18. #if BGFX_CONFIG_DEBUG
  19. # define EGL_CHECK(_call) _EGL_CHECK(BX_ASSERT, _call)
  20. #else
  21. # define EGL_CHECK(_call) _call
  22. #endif // BGFX_CONFIG_DEBUG
  23. namespace bgfx { namespace gl
  24. {
  25. #ifndef EGL_CONTEXT_FLAG_NO_ERROR_BIT_KHR
  26. # define EGL_CONTEXT_FLAG_NO_ERROR_BIT_KHR 0x00000008
  27. #endif // EGL_CONTEXT_FLAG_NO_ERROR_BIT_KHR
  28. #if BGFX_USE_GL_DYNAMIC_LIB
  29. typedef void (*EGLPROC)(void);
  30. typedef EGLBoolean (EGLAPIENTRY* PGNEGLBINDAPIPROC)(EGLenum api);
  31. typedef EGLBoolean (EGLAPIENTRY* PFNEGLCHOOSECONFIGPROC)(EGLDisplay dpy, const EGLint* attrib_list, EGLConfig* configs, EGLint config_size, EGLint* num_config);
  32. typedef EGLContext (EGLAPIENTRY* PFNEGLCREATECONTEXTPROC)(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint* attrib_list);
  33. typedef EGLSurface (EGLAPIENTRY* PFNEGLCREATEPBUFFERSURFACEPROC)(EGLDisplay display, EGLConfig config, EGLint const* attrib_list);
  34. typedef EGLSurface (EGLAPIENTRY* PFNEGLCREATEWINDOWSURFACEPROC)(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint* attrib_list);
  35. typedef EGLBoolean (EGLAPIENTRY* PFNEGLDESTROYCONTEXTPROC)(EGLDisplay dpy, EGLContext ctx);
  36. typedef EGLBoolean (EGLAPIENTRY* PFNEGLDESTROYSURFACEPROC)(EGLDisplay dpy, EGLSurface surface);
  37. typedef EGLContext (EGLAPIENTRY* PFNEGLGETCURRENTCONTEXTPROC)(void);
  38. typedef EGLSurface (EGLAPIENTRY* PFNEGLGETCURRENTSURFACEPROC)(EGLint readdraw);
  39. typedef EGLDisplay (EGLAPIENTRY* PFNEGLGETDISPLAYPROC)(EGLNativeDisplayType display_id);
  40. typedef EGLint (EGLAPIENTRY* PFNEGLGETERRORPROC)(void);
  41. typedef EGLPROC (EGLAPIENTRY* PFNEGLGETPROCADDRESSPROC)(const char* procname);
  42. typedef EGLBoolean (EGLAPIENTRY* PFNEGLINITIALIZEPROC)(EGLDisplay dpy, EGLint* major, EGLint* minor);
  43. typedef EGLBoolean (EGLAPIENTRY* PFNEGLMAKECURRENTPROC)(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx);
  44. typedef EGLBoolean (EGLAPIENTRY* PFNEGLSWAPBUFFERSPROC)(EGLDisplay dpy, EGLSurface surface);
  45. typedef EGLBoolean (EGLAPIENTRY* PFNEGLSWAPINTERVALPROC)(EGLDisplay dpy, EGLint interval);
  46. typedef EGLBoolean (EGLAPIENTRY* PFNEGLTERMINATEPROC)(EGLDisplay dpy);
  47. typedef const char* (EGLAPIENTRY* PGNEGLQUERYSTRINGPROC)(EGLDisplay dpy, EGLint name);
  48. #define EGL_IMPORT \
  49. EGL_IMPORT_FUNC(PGNEGLBINDAPIPROC, eglBindAPI); \
  50. EGL_IMPORT_FUNC(PFNEGLCHOOSECONFIGPROC, eglChooseConfig); \
  51. EGL_IMPORT_FUNC(PFNEGLCREATECONTEXTPROC, eglCreateContext); \
  52. EGL_IMPORT_FUNC(PFNEGLCREATEPBUFFERSURFACEPROC, eglCreatePbufferSurface); \
  53. EGL_IMPORT_FUNC(PFNEGLCREATEWINDOWSURFACEPROC, eglCreateWindowSurface); \
  54. EGL_IMPORT_FUNC(PFNEGLDESTROYCONTEXTPROC, eglDestroyContext); \
  55. EGL_IMPORT_FUNC(PFNEGLDESTROYSURFACEPROC, eglDestroySurface); \
  56. EGL_IMPORT_FUNC(PFNEGLGETCURRENTCONTEXTPROC, eglGetCurrentContext); \
  57. EGL_IMPORT_FUNC(PFNEGLGETCURRENTSURFACEPROC, eglGetCurrentSurface); \
  58. EGL_IMPORT_FUNC(PFNEGLGETDISPLAYPROC, eglGetDisplay); \
  59. EGL_IMPORT_FUNC(PFNEGLGETERRORPROC, eglGetError); \
  60. EGL_IMPORT_FUNC(PFNEGLGETPROCADDRESSPROC, eglGetProcAddress); \
  61. EGL_IMPORT_FUNC(PFNEGLINITIALIZEPROC, eglInitialize); \
  62. EGL_IMPORT_FUNC(PFNEGLMAKECURRENTPROC, eglMakeCurrent); \
  63. EGL_IMPORT_FUNC(PFNEGLSWAPBUFFERSPROC, eglSwapBuffers); \
  64. EGL_IMPORT_FUNC(PFNEGLSWAPINTERVALPROC, eglSwapInterval); \
  65. EGL_IMPORT_FUNC(PFNEGLTERMINATEPROC, eglTerminate); \
  66. EGL_IMPORT_FUNC(PGNEGLQUERYSTRINGPROC, eglQueryString); \
  67. #define EGL_IMPORT_FUNC(_proto, _func) _proto _func
  68. EGL_IMPORT
  69. #undef EGL_IMPORT_FUNC
  70. void* eglOpen()
  71. {
  72. void* handle = bx::dlopen(
  73. #if BX_PLATFORM_LINUX
  74. "libEGL.so.1"
  75. #else
  76. "libEGL." BX_DL_EXT
  77. #endif // BX_PLATFORM_*
  78. );
  79. BGFX_FATAL(NULL != handle, Fatal::UnableToInitialize, "Failed to load libEGL dynamic library.");
  80. #define EGL_IMPORT_FUNC(_proto, _func) \
  81. _func = (_proto)bx::dlsym(handle, #_func); \
  82. BX_TRACE("%p " #_func, _func); \
  83. BGFX_FATAL(NULL != _func, Fatal::UnableToInitialize, "Failed get " #_func ".")
  84. EGL_IMPORT
  85. #undef EGL_IMPORT_FUNC
  86. return handle;
  87. }
  88. void eglClose(void* _handle)
  89. {
  90. bx::dlclose(_handle);
  91. #define EGL_IMPORT_FUNC(_proto, _func) _func = NULL
  92. EGL_IMPORT
  93. #undef EGL_IMPORT_FUNC
  94. }
  95. #else
  96. void* eglOpen()
  97. {
  98. return NULL;
  99. }
  100. void eglClose(void* /*_handle*/)
  101. {
  102. }
  103. #endif // BGFX_USE_GL_DYNAMIC_LIB
  104. # define GL_IMPORT(_optional, _proto, _func, _import) _proto _func = NULL
  105. # include "glimports.h"
  106. static EGLint s_contextAttrs[16];
  107. struct SwapChainGL
  108. {
  109. SwapChainGL(EGLDisplay _display, EGLConfig _config, EGLContext _context, EGLNativeWindowType _nwh)
  110. : m_nwh(_nwh)
  111. , m_display(_display)
  112. {
  113. EGLSurface defaultSurface = eglGetCurrentSurface(EGL_DRAW);
  114. if (EGLNativeWindowType(0) == _nwh)
  115. {
  116. m_surface = eglCreatePbufferSurface(m_display, _config, NULL);
  117. }
  118. else
  119. {
  120. m_surface = eglCreateWindowSurface(m_display, _config, _nwh, NULL);
  121. }
  122. BGFX_FATAL(m_surface != EGL_NO_SURFACE, Fatal::UnableToInitialize, "Failed to create surface.");
  123. m_context = eglCreateContext(m_display, _config, _context, s_contextAttrs);
  124. BX_ASSERT(NULL != m_context, "Create swap chain failed: %x", eglGetError() );
  125. makeCurrent();
  126. GL_CHECK(glClearColor(0.0f, 0.0f, 0.0f, 0.0f) );
  127. GL_CHECK(glClear(GL_COLOR_BUFFER_BIT) );
  128. swapBuffers();
  129. GL_CHECK(glClear(GL_COLOR_BUFFER_BIT) );
  130. swapBuffers();
  131. EGL_CHECK(eglMakeCurrent(m_display, defaultSurface, defaultSurface, _context) );
  132. }
  133. ~SwapChainGL()
  134. {
  135. EGLSurface defaultSurface = eglGetCurrentSurface(EGL_DRAW);
  136. EGLContext defaultContext = eglGetCurrentContext();
  137. EGL_CHECK(eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) );
  138. EGL_CHECK(eglDestroyContext(m_display, m_context) );
  139. EGL_CHECK(eglDestroySurface(m_display, m_surface) );
  140. EGL_CHECK(eglMakeCurrent(m_display, defaultSurface, defaultSurface, defaultContext) );
  141. }
  142. void makeCurrent()
  143. {
  144. EGL_CHECK(eglMakeCurrent(m_display, m_surface, m_surface, m_context) );
  145. }
  146. void swapBuffers()
  147. {
  148. EGL_CHECK(eglSwapBuffers(m_display, m_surface) );
  149. }
  150. EGLNativeWindowType m_nwh;
  151. EGLContext m_context;
  152. EGLDisplay m_display;
  153. EGLSurface m_surface;
  154. };
  155. # if BX_PLATFORM_RPI
  156. static EGL_DISPMANX_WINDOW_T s_dispmanWindow;
  157. # endif // BX_PLATFORM_RPI
  158. void GlContext::create(uint32_t _width, uint32_t _height, uint32_t _flags)
  159. {
  160. BX_UNUSED(_flags);
  161. # if BX_PLATFORM_RPI
  162. bcm_host_init();
  163. # endif // BX_PLATFORM_RPI
  164. m_eglLibrary = eglOpen();
  165. if (NULL == g_platformData.context)
  166. {
  167. # if BX_PLATFORM_RPI
  168. g_platformData.ndt = EGL_DEFAULT_DISPLAY;
  169. # endif // BX_PLATFORM_RPI
  170. BX_UNUSED(_width, _height);
  171. EGLNativeDisplayType ndt = (EGLNativeDisplayType)g_platformData.ndt;
  172. EGLNativeWindowType nwh = (EGLNativeWindowType )g_platformData.nwh;
  173. # if BX_PLATFORM_WINDOWS
  174. if (NULL == g_platformData.ndt)
  175. {
  176. ndt = GetDC( (HWND)g_platformData.nwh);
  177. }
  178. # endif // BX_PLATFORM_WINDOWS
  179. m_display = eglGetDisplay(NULL == ndt ? EGL_DEFAULT_DISPLAY : ndt);
  180. BGFX_FATAL(m_display != EGL_NO_DISPLAY, Fatal::UnableToInitialize, "Failed to create display %p", m_display);
  181. EGLint major = 0;
  182. EGLint minor = 0;
  183. EGLBoolean success = eglInitialize(m_display, &major, &minor);
  184. BGFX_FATAL(success && major >= 1 && minor >= 3, Fatal::UnableToInitialize, "Failed to initialize %d.%d", major, minor);
  185. BX_TRACE("EGL info:");
  186. const char* clientApis = eglQueryString(m_display, EGL_CLIENT_APIS);
  187. BX_TRACE(" APIs: %s", clientApis); BX_UNUSED(clientApis);
  188. const char* vendor = eglQueryString(m_display, EGL_VENDOR);
  189. BX_TRACE(" Vendor: %s", vendor); BX_UNUSED(vendor);
  190. const char* version = eglQueryString(m_display, EGL_VERSION);
  191. BX_TRACE("Version: %s", version); BX_UNUSED(version);
  192. const char* extensions = eglQueryString(m_display, EGL_EXTENSIONS);
  193. BX_TRACE("Supported EGL extensions:");
  194. dumpExtensions(extensions);
  195. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  196. {
  197. EGLBoolean ok = eglBindAPI(EGL_OPENGL_API);
  198. BGFX_FATAL(ok, Fatal::UnableToInitialize, "Could not set API! error: %d", eglGetError());
  199. }
  200. const bool hasEglAndroidRecordable = !bx::findIdentifierMatch(extensions, "EGL_ANDROID_recordable").isEmpty();
  201. const uint32_t glVersion = !!BGFX_CONFIG_RENDERER_OPENGL
  202. ? BGFX_CONFIG_RENDERER_OPENGL
  203. : BGFX_CONFIG_RENDERER_OPENGLES
  204. ;
  205. #if BX_PLATFORM_ANDROID
  206. const uint32_t msaa = (_flags&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT;
  207. const uint32_t msaaSamples = msaa == 0 ? 0 : 1<<msaa;
  208. m_msaaContext = true;
  209. #endif // BX_PLATFORM_ANDROID
  210. const bool headless = EGLNativeWindowType(0) == nwh;
  211. EGLint attrs[] =
  212. {
  213. EGL_RENDERABLE_TYPE, !!BGFX_CONFIG_RENDERER_OPENGL
  214. ? EGL_OPENGL_BIT
  215. : (glVersion >= 30) ? EGL_OPENGL_ES3_BIT_KHR : EGL_OPENGL_ES2_BIT
  216. ,
  217. EGL_SURFACE_TYPE, headless ? EGL_PBUFFER_BIT : EGL_WINDOW_BIT,
  218. EGL_BLUE_SIZE, 8,
  219. EGL_GREEN_SIZE, 8,
  220. EGL_RED_SIZE, 8,
  221. EGL_ALPHA_SIZE, 8,
  222. # if BX_PLATFORM_ANDROID
  223. EGL_DEPTH_SIZE, 16,
  224. EGL_SAMPLES, (EGLint)msaaSamples,
  225. # else
  226. EGL_DEPTH_SIZE, 24,
  227. # endif // BX_PLATFORM_
  228. EGL_STENCIL_SIZE, 8,
  229. // Android Recordable surface
  230. hasEglAndroidRecordable ? EGL_RECORDABLE_ANDROID : EGL_NONE,
  231. hasEglAndroidRecordable ? 1 : EGL_NONE,
  232. EGL_NONE
  233. };
  234. EGLint numConfig = 0;
  235. success = eglChooseConfig(m_display, attrs, &m_config, 1, &numConfig);
  236. BGFX_FATAL(success, Fatal::UnableToInitialize, "eglChooseConfig");
  237. # if BX_PLATFORM_ANDROID
  238. EGLint format;
  239. eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format);
  240. ANativeWindow_setBuffersGeometry( (ANativeWindow*)g_platformData.nwh, _width, _height, format);
  241. # elif BX_PLATFORM_RPI
  242. DISPMANX_DISPLAY_HANDLE_T dispmanDisplay = vc_dispmanx_display_open(0);
  243. DISPMANX_UPDATE_HANDLE_T dispmanUpdate = vc_dispmanx_update_start(0);
  244. VC_RECT_T dstRect = { 0, 0, int32_t(_width), int32_t(_height) };
  245. VC_RECT_T srcRect = { 0, 0, int32_t(_width) << 16, int32_t(_height) << 16 };
  246. DISPMANX_ELEMENT_HANDLE_T dispmanElement = vc_dispmanx_element_add(dispmanUpdate
  247. , dispmanDisplay
  248. , 0
  249. , &dstRect
  250. , 0
  251. , &srcRect
  252. , DISPMANX_PROTECTION_NONE
  253. , NULL
  254. , NULL
  255. , DISPMANX_NO_ROTATE
  256. );
  257. s_dispmanWindow.element = dispmanElement;
  258. s_dispmanWindow.width = _width;
  259. s_dispmanWindow.height = _height;
  260. nwh = &s_dispmanWindow;
  261. vc_dispmanx_update_submit_sync(dispmanUpdate);
  262. # endif // BX_PLATFORM_ANDROID
  263. if (headless)
  264. {
  265. EGLint pbAttribs[] =
  266. {
  267. EGL_WIDTH, EGLint(1),
  268. EGL_HEIGHT, EGLint(1),
  269. EGL_NONE
  270. };
  271. m_surface = eglCreatePbufferSurface(m_display, m_config, pbAttribs);
  272. }
  273. else
  274. {
  275. m_surface = eglCreateWindowSurface(m_display, m_config, nwh, NULL);
  276. }
  277. BGFX_FATAL(m_surface != EGL_NO_SURFACE, Fatal::UnableToInitialize, "Failed to create surface.");
  278. const bool hasEglKhrCreateContext = !bx::findIdentifierMatch(extensions, "EGL_KHR_create_context").isEmpty();
  279. const bool hasEglKhrNoError = !bx::findIdentifierMatch(extensions, "EGL_KHR_create_context_no_error").isEmpty();
  280. for (uint32_t ii = 0; ii < 2; ++ii)
  281. {
  282. bx::StaticMemoryBlockWriter writer(s_contextAttrs, sizeof(s_contextAttrs) );
  283. EGLint flags = 0;
  284. # if BX_PLATFORM_RPI
  285. BX_UNUSED(hasEglKhrCreateContext, hasEglKhrNoError);
  286. # else
  287. if (hasEglKhrCreateContext)
  288. {
  289. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  290. {
  291. bx::write(&writer, EGLint(EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR), bx::ErrorAssert{});
  292. bx::write(&writer, EGLint(EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR), bx::ErrorAssert{});
  293. }
  294. bx::write(&writer, EGLint(EGL_CONTEXT_MAJOR_VERSION_KHR), bx::ErrorAssert{});
  295. bx::write(&writer, EGLint(glVersion / 10), bx::ErrorAssert{});
  296. bx::write(&writer, EGLint(EGL_CONTEXT_MINOR_VERSION_KHR), bx::ErrorAssert{});
  297. bx::write(&writer, EGLint(glVersion % 10), bx::ErrorAssert{});
  298. flags |= BGFX_CONFIG_DEBUG && hasEglKhrNoError ? 0
  299. | EGL_CONTEXT_FLAG_NO_ERROR_BIT_KHR
  300. : 0
  301. ;
  302. if (0 == ii)
  303. {
  304. flags |= BGFX_CONFIG_DEBUG ? 0
  305. | EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR
  306. // | EGL_OPENGL_ES3_BIT_KHR
  307. : 0
  308. ;
  309. bx::write(&writer, EGLint(EGL_CONTEXT_FLAGS_KHR), bx::ErrorAssert{} );
  310. bx::write(&writer, flags, bx::ErrorAssert{});
  311. }
  312. }
  313. else
  314. # endif // BX_PLATFORM_RPI
  315. {
  316. bx::write(&writer, EGLint(EGL_CONTEXT_CLIENT_VERSION), bx::ErrorAssert{} );
  317. bx::write(&writer, EGLint(glVersion / 10), bx::ErrorAssert{} );
  318. }
  319. bx::write(&writer, EGLint(EGL_NONE), bx::ErrorAssert{} );
  320. m_context = eglCreateContext(m_display, m_config, EGL_NO_CONTEXT, s_contextAttrs);
  321. if (NULL != m_context)
  322. {
  323. break;
  324. }
  325. BX_TRACE("Failed to create EGL context with EGL_CONTEXT_FLAGS_KHR (%08x).", flags);
  326. }
  327. BGFX_FATAL(m_context != EGL_NO_CONTEXT, Fatal::UnableToInitialize, "Failed to create context.");
  328. success = eglMakeCurrent(m_display, m_surface, m_surface, m_context);
  329. BGFX_FATAL(success, Fatal::UnableToInitialize, "Failed to set context.");
  330. m_current = NULL;
  331. eglSwapInterval(m_display, 0);
  332. }
  333. import();
  334. g_internalData.context = m_context;
  335. }
  336. void GlContext::destroy()
  337. {
  338. if (NULL != m_display)
  339. {
  340. EGL_CHECK(eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) );
  341. EGL_CHECK(eglDestroyContext(m_display, m_context) );
  342. EGL_CHECK(eglDestroySurface(m_display, m_surface) );
  343. EGL_CHECK(eglTerminate(m_display) );
  344. m_context = NULL;
  345. }
  346. eglClose(m_eglLibrary);
  347. # if BX_PLATFORM_RPI
  348. bcm_host_deinit();
  349. # endif // BX_PLATFORM_RPI
  350. }
  351. void GlContext::resize(uint32_t _width, uint32_t _height, uint32_t _flags)
  352. {
  353. # if BX_PLATFORM_ANDROID
  354. if (NULL != m_display)
  355. {
  356. EGLNativeWindowType nwh = (EGLNativeWindowType )g_platformData.nwh;
  357. eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  358. eglDestroySurface(m_display, m_surface);
  359. m_surface = eglCreateWindowSurface(m_display, m_config, nwh, NULL);
  360. BGFX_FATAL(m_surface != EGL_NO_SURFACE, Fatal::UnableToInitialize, "Failed to create surface.");
  361. EGLBoolean success = eglMakeCurrent(m_display, m_surface, m_surface, m_context);
  362. BGFX_FATAL(success, Fatal::UnableToInitialize, "Failed to set context.");
  363. EGLint format;
  364. eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format);
  365. ANativeWindow_setBuffersGeometry( (ANativeWindow*)g_platformData.nwh, _width, _height, format);
  366. }
  367. # elif BX_PLATFORM_EMSCRIPTEN
  368. EMSCRIPTEN_CHECK(emscripten_set_canvas_element_size(HTML5_TARGET_CANVAS_SELECTOR, _width, _height) );
  369. # else
  370. BX_UNUSED(_width, _height);
  371. # endif // BX_PLATFORM_*
  372. if (NULL != m_display)
  373. {
  374. bool vsync = !!(_flags&BGFX_RESET_VSYNC);
  375. EGL_CHECK(eglSwapInterval(m_display, vsync ? 1 : 0) );
  376. }
  377. }
  378. uint64_t GlContext::getCaps() const
  379. {
  380. return BX_ENABLED(0
  381. | BX_PLATFORM_LINUX
  382. | BX_PLATFORM_WINDOWS
  383. | BX_PLATFORM_ANDROID
  384. )
  385. ? BGFX_CAPS_SWAP_CHAIN
  386. : 0
  387. ;
  388. }
  389. SwapChainGL* GlContext::createSwapChain(void* _nwh)
  390. {
  391. return BX_NEW(g_allocator, SwapChainGL)(m_display, m_config, m_context, (EGLNativeWindowType)_nwh);
  392. }
  393. void GlContext::destroySwapChain(SwapChainGL* _swapChain)
  394. {
  395. bx::deleteObject(g_allocator, _swapChain);
  396. }
  397. void GlContext::swap(SwapChainGL* _swapChain)
  398. {
  399. makeCurrent(_swapChain);
  400. if (NULL == _swapChain)
  401. {
  402. if (NULL != m_display)
  403. {
  404. EGL_CHECK(eglSwapBuffers(m_display, m_surface) );
  405. }
  406. }
  407. else
  408. {
  409. _swapChain->swapBuffers();
  410. }
  411. }
  412. void GlContext::makeCurrent(SwapChainGL* _swapChain)
  413. {
  414. if (m_current != _swapChain)
  415. {
  416. m_current = _swapChain;
  417. if (NULL == _swapChain)
  418. {
  419. if (NULL != m_display)
  420. {
  421. EGL_CHECK(eglMakeCurrent(m_display, m_surface, m_surface, m_context) );
  422. }
  423. }
  424. else
  425. {
  426. _swapChain->makeCurrent();
  427. }
  428. }
  429. }
  430. void GlContext::import()
  431. {
  432. BX_TRACE("Import:");
  433. # if BX_PLATFORM_WINDOWS || BX_PLATFORM_LINUX
  434. # if BX_PLATFORM_WINDOWS
  435. # define LIBRARY_NAME "libGL.dll"
  436. # elif BX_PLATFORM_LINUX
  437. # if BGFX_CONFIG_RENDERER_OPENGL
  438. # define LIBRARY_NAME "libGL.so.1"
  439. # else
  440. # define LIBRARY_NAME "libGLESv2.so.2"
  441. # endif
  442. # endif
  443. void* lib = bx::dlopen(LIBRARY_NAME);
  444. # define GL_EXTENSION(_optional, _proto, _func, _import) \
  445. { \
  446. if (NULL == _func) \
  447. { \
  448. _func = bx::dlsym<_proto>(lib, #_import); \
  449. BX_TRACE("\t%p " #_func " (" #_import ")", _func); \
  450. BGFX_FATAL(_optional || NULL != _func \
  451. , Fatal::UnableToInitialize \
  452. , "Failed to create OpenGLES context. eglGetProcAddress(\"%s\")" \
  453. , #_import); \
  454. } \
  455. }
  456. # else
  457. # define GL_EXTENSION(_optional, _proto, _func, _import) \
  458. { \
  459. if (NULL == _func) \
  460. { \
  461. _func = reinterpret_cast<_proto>(eglGetProcAddress(#_import) ); \
  462. BX_TRACE("\t%p " #_func " (" #_import ")", _func); \
  463. BGFX_FATAL(_optional || NULL != _func \
  464. , Fatal::UnableToInitialize \
  465. , "Failed to create OpenGLES context. eglGetProcAddress(\"%s\")" \
  466. , #_import); \
  467. } \
  468. }
  469. # endif // BX_PLATFORM_
  470. # include "glimports.h"
  471. # undef GL_EXTENSION
  472. }
  473. } /* namespace gl */ } // namespace bgfx
  474. # endif // BGFX_USE_EGL
  475. #endif // (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)