glcontext_egl.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /*
  2. * Copyright 2011-2025 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(PFNEGLRELEASETHREADPROC, eglReleaseThread); \
  64. EGL_IMPORT_FUNC(PFNEGLSWAPBUFFERSPROC, eglSwapBuffers); \
  65. EGL_IMPORT_FUNC(PFNEGLSWAPINTERVALPROC, eglSwapInterval); \
  66. EGL_IMPORT_FUNC(PFNEGLTERMINATEPROC, eglTerminate); \
  67. EGL_IMPORT_FUNC(PGNEGLQUERYSTRINGPROC, eglQueryString); \
  68. #define EGL_IMPORT_FUNC(_proto, _func) _proto _func
  69. EGL_IMPORT
  70. #undef EGL_IMPORT_FUNC
  71. void* eglOpen()
  72. {
  73. void* handle = bx::dlopen(
  74. #if BX_PLATFORM_LINUX
  75. "libEGL.so.1"
  76. #else
  77. "libEGL." BX_DL_EXT
  78. #endif // BX_PLATFORM_*
  79. );
  80. BGFX_FATAL(NULL != handle, Fatal::UnableToInitialize, "Failed to load libEGL dynamic library.");
  81. #define EGL_IMPORT_FUNC(_proto, _func) \
  82. _func = (_proto)bx::dlsym(handle, #_func); \
  83. BX_TRACE("%p " #_func, _func); \
  84. BGFX_FATAL(NULL != _func, Fatal::UnableToInitialize, "Failed get " #_func ".")
  85. EGL_IMPORT
  86. #undef EGL_IMPORT_FUNC
  87. return handle;
  88. }
  89. void eglClose(void* _handle)
  90. {
  91. bx::dlclose(_handle);
  92. #define EGL_IMPORT_FUNC(_proto, _func) _func = NULL
  93. EGL_IMPORT
  94. #undef EGL_IMPORT_FUNC
  95. }
  96. #else
  97. void* eglOpen()
  98. {
  99. return NULL;
  100. }
  101. void eglClose(void* /*_handle*/)
  102. {
  103. }
  104. #endif // BGFX_USE_GL_DYNAMIC_LIB
  105. #if BX_PLATFORM_LINUX
  106. # define WL_EGL_IMPORT \
  107. WL_EGL_FUNC(struct wl_egl_window *, wl_egl_window_create, (struct wl_surface *, int, int) ) \
  108. WL_EGL_FUNC(void, wl_egl_window_destroy, (struct wl_egl_window *)) \
  109. WL_EGL_FUNC(void, wl_egl_window_resize, (struct wl_egl_window *, int, int, int, int)) \
  110. WL_EGL_FUNC(void, wl_egl_window_get_attached_size, (struct wl_egl_window *, int *, int *) ) \
  111. # define WL_EGL_FUNC(rt, fname, params) \
  112. typedef rt(*PFNWLEGL_##fname) params; \
  113. PFNWLEGL_##fname BGFX_WAYLAND_##fname;
  114. WL_EGL_IMPORT
  115. # undef WL_EGL_FUNC
  116. void* waylandEglOpen()
  117. {
  118. void* handle = bx::dlopen("libwayland-egl.so.1");
  119. BGFX_FATAL(handle != NULL, Fatal::UnableToInitialize, "Could not dlopen() libwayland-egl.so.1");
  120. # define WL_EGL_FUNC(rt, fname, params) BGFX_WAYLAND_##fname = (PFNWLEGL_##fname) bx::dlsym(handle, #fname);
  121. WL_EGL_IMPORT
  122. # undef WL_EGL_FUNC
  123. return handle;
  124. }
  125. void waylandEglClose(void* _handle)
  126. {
  127. bx::dlclose(_handle);
  128. # define WL_EGL_FUNC(rt, fname, params) BGFX_WAYLAND_##fname = NULL;
  129. WL_EGL_IMPORT
  130. # undef WL_EGL_FUNC
  131. }
  132. #endif // BX_PLATFORM_LINUX
  133. # define GL_IMPORT(_optional, _proto, _func, _import) _proto _func = NULL
  134. # include "glimports.h"
  135. static EGLint s_contextAttrs[16];
  136. struct SwapChainGL
  137. {
  138. SwapChainGL(EGLDisplay _display, EGLConfig _config, EGLContext _context, EGLNativeWindowType _nwh, int _width, int _height)
  139. : m_nwh(_nwh)
  140. , m_display(_display)
  141. # if BX_PLATFORM_LINUX
  142. , m_eglWindow(NULL)
  143. # endif
  144. {
  145. EGLSurface defaultSurface = eglGetCurrentSurface(EGL_DRAW);
  146. BX_UNUSED(_width, _height);
  147. if (EGLNativeWindowType(0) == _nwh)
  148. {
  149. m_surface = eglCreatePbufferSurface(m_display, _config, NULL);
  150. }
  151. else
  152. {
  153. # if BX_PLATFORM_LINUX
  154. if (g_platformData.type == NativeWindowHandleType::Wayland)
  155. {
  156. // A wl_surface needs to be first wrapped in a wl_egl_window
  157. // before it can be used to create the EGLSurface.
  158. m_eglWindow = BGFX_WAYLAND_wl_egl_window_create( (wl_surface*)_nwh, _width, _height);
  159. _nwh = (EGLNativeWindowType) m_eglWindow;
  160. }
  161. # endif
  162. m_surface = eglCreateWindowSurface(m_display, _config, _nwh, NULL);
  163. }
  164. BGFX_FATAL(m_surface != EGL_NO_SURFACE, Fatal::UnableToInitialize, "Failed to create surface.");
  165. m_context = eglCreateContext(m_display, _config, _context, s_contextAttrs);
  166. BX_ASSERT(NULL != m_context, "Create swap chain failed: %x", eglGetError() );
  167. makeCurrent();
  168. GL_CHECK(glClearColor(0.0f, 0.0f, 0.0f, 0.0f) );
  169. GL_CHECK(glClear(GL_COLOR_BUFFER_BIT) );
  170. swapBuffers();
  171. GL_CHECK(glClear(GL_COLOR_BUFFER_BIT) );
  172. swapBuffers();
  173. EGL_CHECK(eglMakeCurrent(m_display, defaultSurface, defaultSurface, _context) );
  174. }
  175. ~SwapChainGL()
  176. {
  177. EGLSurface defaultSurface = eglGetCurrentSurface(EGL_DRAW);
  178. EGLContext defaultContext = eglGetCurrentContext();
  179. EGL_CHECK(eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) );
  180. EGL_CHECK(eglDestroyContext(m_display, m_context) );
  181. EGL_CHECK(eglDestroySurface(m_display, m_surface) );
  182. # if BX_PLATFORM_LINUX
  183. if (m_eglWindow)
  184. {
  185. BGFX_WAYLAND_wl_egl_window_destroy(m_eglWindow);
  186. }
  187. # endif
  188. EGL_CHECK(eglMakeCurrent(m_display, defaultSurface, defaultSurface, defaultContext) );
  189. }
  190. void makeCurrent()
  191. {
  192. EGL_CHECK(eglMakeCurrent(m_display, m_surface, m_surface, m_context) );
  193. }
  194. void swapBuffers()
  195. {
  196. EGL_CHECK(eglSwapBuffers(m_display, m_surface) );
  197. }
  198. EGLNativeWindowType m_nwh;
  199. EGLContext m_context;
  200. EGLDisplay m_display;
  201. EGLSurface m_surface;
  202. # if BX_PLATFORM_LINUX
  203. wl_egl_window *m_eglWindow;
  204. # endif
  205. };
  206. # if BX_PLATFORM_RPI
  207. static EGL_DISPMANX_WINDOW_T s_dispmanWindow;
  208. # endif // BX_PLATFORM_RPI
  209. void GlContext::create(uint32_t _width, uint32_t _height, uint32_t _flags)
  210. {
  211. BX_UNUSED(_flags);
  212. # if BX_PLATFORM_RPI
  213. bcm_host_init();
  214. # endif // BX_PLATFORM_RPI
  215. m_eglDll = eglOpen();
  216. if (NULL == g_platformData.context)
  217. {
  218. # if BX_PLATFORM_RPI
  219. g_platformData.ndt = EGL_DEFAULT_DISPLAY;
  220. # endif // BX_PLATFORM_RPI
  221. BX_UNUSED(_width, _height);
  222. EGLNativeDisplayType ndt = (EGLNativeDisplayType)g_platformData.ndt;
  223. EGLNativeWindowType nwh = (EGLNativeWindowType )g_platformData.nwh;
  224. # if BX_PLATFORM_WINDOWS
  225. if (NULL == g_platformData.ndt)
  226. {
  227. ndt = GetDC( (HWND)g_platformData.nwh);
  228. }
  229. # endif // BX_PLATFORM_WINDOWS
  230. m_display = eglGetDisplay(NULL == ndt ? EGL_DEFAULT_DISPLAY : ndt);
  231. BGFX_FATAL(m_display != EGL_NO_DISPLAY, Fatal::UnableToInitialize, "Failed to create display %p", m_display);
  232. EGLint major = 0;
  233. EGLint minor = 0;
  234. EGLBoolean success = eglInitialize(m_display, &major, &minor);
  235. BGFX_FATAL(success && major >= 1 && minor >= 3, Fatal::UnableToInitialize, "Failed to initialize %d.%d", major, minor);
  236. BX_TRACE("EGL info:");
  237. const char* clientApis = eglQueryString(m_display, EGL_CLIENT_APIS);
  238. BX_TRACE(" APIs: %s", clientApis); BX_UNUSED(clientApis);
  239. const char* vendor = eglQueryString(m_display, EGL_VENDOR);
  240. BX_TRACE(" Vendor: %s", vendor); BX_UNUSED(vendor);
  241. const char* version = eglQueryString(m_display, EGL_VERSION);
  242. BX_TRACE("Version: %s", version); BX_UNUSED(version);
  243. const char* extensions = eglQueryString(m_display, EGL_EXTENSIONS);
  244. BX_TRACE("Supported EGL extensions:");
  245. dumpExtensions(extensions);
  246. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  247. {
  248. EGLBoolean ok = eglBindAPI(EGL_OPENGL_API);
  249. BGFX_FATAL(ok, Fatal::UnableToInitialize, "Could not set API! error: %d", eglGetError());
  250. }
  251. const bool hasEglAndroidRecordable = !bx::findIdentifierMatch(extensions, "EGL_ANDROID_recordable").isEmpty();
  252. const uint32_t glVersion = !!BGFX_CONFIG_RENDERER_OPENGL
  253. ? BGFX_CONFIG_RENDERER_OPENGL
  254. : BGFX_CONFIG_RENDERER_OPENGLES
  255. ;
  256. #if BX_PLATFORM_ANDROID
  257. const uint32_t msaa = (_flags&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT;
  258. const uint32_t msaaSamples = msaa == 0 ? 0 : 1<<msaa;
  259. m_msaaContext = true;
  260. #endif // BX_PLATFORM_ANDROID
  261. const bool headless = EGLNativeWindowType(0) == nwh;
  262. EGLint attrs[] =
  263. {
  264. EGL_RENDERABLE_TYPE, !!BGFX_CONFIG_RENDERER_OPENGL
  265. ? EGL_OPENGL_BIT
  266. : (glVersion >= 30) ? EGL_OPENGL_ES3_BIT_KHR : EGL_OPENGL_ES2_BIT
  267. ,
  268. EGL_SURFACE_TYPE, headless ? EGL_PBUFFER_BIT : EGL_WINDOW_BIT,
  269. EGL_BLUE_SIZE, 8,
  270. EGL_GREEN_SIZE, 8,
  271. EGL_RED_SIZE, 8,
  272. EGL_ALPHA_SIZE, 8,
  273. # if BX_PLATFORM_ANDROID
  274. EGL_DEPTH_SIZE, 16,
  275. EGL_SAMPLES, (EGLint)msaaSamples,
  276. # else
  277. EGL_DEPTH_SIZE, 24,
  278. # endif // BX_PLATFORM_
  279. EGL_STENCIL_SIZE, 8,
  280. // Android Recordable surface
  281. hasEglAndroidRecordable ? EGL_RECORDABLE_ANDROID : EGL_NONE,
  282. hasEglAndroidRecordable ? 1 : EGL_NONE,
  283. EGL_NONE
  284. };
  285. EGLint numConfig = 0;
  286. success = eglChooseConfig(m_display, attrs, &m_config, 1, &numConfig);
  287. BGFX_FATAL(success, Fatal::UnableToInitialize, "eglChooseConfig");
  288. # if BX_PLATFORM_ANDROID
  289. EGLint format;
  290. eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format);
  291. ANativeWindow_setBuffersGeometry( (ANativeWindow*)g_platformData.nwh, _width, _height, format);
  292. # elif BX_PLATFORM_RPI
  293. DISPMANX_DISPLAY_HANDLE_T dispmanDisplay = vc_dispmanx_display_open(0);
  294. DISPMANX_UPDATE_HANDLE_T dispmanUpdate = vc_dispmanx_update_start(0);
  295. VC_RECT_T dstRect = { 0, 0, int32_t(_width), int32_t(_height) };
  296. VC_RECT_T srcRect = { 0, 0, int32_t(_width) << 16, int32_t(_height) << 16 };
  297. DISPMANX_ELEMENT_HANDLE_T dispmanElement = vc_dispmanx_element_add(dispmanUpdate
  298. , dispmanDisplay
  299. , 0
  300. , &dstRect
  301. , 0
  302. , &srcRect
  303. , DISPMANX_PROTECTION_NONE
  304. , NULL
  305. , NULL
  306. , DISPMANX_NO_ROTATE
  307. );
  308. s_dispmanWindow.element = dispmanElement;
  309. s_dispmanWindow.width = _width;
  310. s_dispmanWindow.height = _height;
  311. nwh = &s_dispmanWindow;
  312. vc_dispmanx_update_submit_sync(dispmanUpdate);
  313. # endif // BX_PLATFORM_ANDROID
  314. # if BX_PLATFORM_LINUX
  315. if (g_platformData.type == NativeWindowHandleType::Wayland)
  316. {
  317. m_waylandEglDll = waylandEglOpen();
  318. }
  319. # endif
  320. if (headless)
  321. {
  322. EGLint pbAttribs[] =
  323. {
  324. EGL_WIDTH, EGLint(1),
  325. EGL_HEIGHT, EGLint(1),
  326. EGL_NONE
  327. };
  328. m_surface = eglCreatePbufferSurface(m_display, m_config, pbAttribs);
  329. }
  330. else
  331. {
  332. # if BX_PLATFORM_LINUX
  333. if (g_platformData.type == NativeWindowHandleType::Wayland)
  334. {
  335. // A wl_surface needs to be first wrapped in a wl_egl_window
  336. // before it can be used to create the EGLSurface.
  337. m_eglWindow = BGFX_WAYLAND_wl_egl_window_create( (wl_surface*)nwh, _width, _height);
  338. nwh = (EGLNativeWindowType) m_eglWindow;
  339. }
  340. # endif
  341. m_surface = eglCreateWindowSurface(m_display, m_config, nwh, NULL);
  342. }
  343. BGFX_FATAL(m_surface != EGL_NO_SURFACE, Fatal::UnableToInitialize, "Failed to create surface.");
  344. const bool hasEglKhrCreateContext = !bx::findIdentifierMatch(extensions, "EGL_KHR_create_context").isEmpty();
  345. const bool hasEglKhrNoError = !bx::findIdentifierMatch(extensions, "EGL_KHR_create_context_no_error").isEmpty();
  346. for (uint32_t ii = 0; ii < 2; ++ii)
  347. {
  348. bx::StaticMemoryBlockWriter writer(s_contextAttrs, sizeof(s_contextAttrs) );
  349. EGLint flags = 0;
  350. # if BX_PLATFORM_RPI
  351. BX_UNUSED(hasEglKhrCreateContext, hasEglKhrNoError);
  352. # else
  353. if (hasEglKhrCreateContext)
  354. {
  355. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  356. {
  357. bx::write(&writer, EGLint(EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR), bx::ErrorAssert{});
  358. bx::write(&writer, EGLint(EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR), bx::ErrorAssert{});
  359. }
  360. bx::write(&writer, EGLint(EGL_CONTEXT_MAJOR_VERSION_KHR), bx::ErrorAssert{});
  361. bx::write(&writer, EGLint(glVersion / 10), bx::ErrorAssert{});
  362. bx::write(&writer, EGLint(EGL_CONTEXT_MINOR_VERSION_KHR), bx::ErrorAssert{});
  363. bx::write(&writer, EGLint(glVersion % 10), bx::ErrorAssert{});
  364. flags |= BGFX_CONFIG_DEBUG && hasEglKhrNoError ? 0
  365. | EGL_CONTEXT_FLAG_NO_ERROR_BIT_KHR
  366. : 0
  367. ;
  368. if (0 == ii)
  369. {
  370. flags |= BGFX_CONFIG_DEBUG ? 0
  371. | EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR
  372. // | EGL_OPENGL_ES3_BIT_KHR
  373. : 0
  374. ;
  375. bx::write(&writer, EGLint(EGL_CONTEXT_FLAGS_KHR), bx::ErrorAssert{} );
  376. bx::write(&writer, flags, bx::ErrorAssert{});
  377. }
  378. }
  379. else
  380. # endif // BX_PLATFORM_RPI
  381. {
  382. bx::write(&writer, EGLint(EGL_CONTEXT_CLIENT_VERSION), bx::ErrorAssert{} );
  383. bx::write(&writer, EGLint(glVersion / 10), bx::ErrorAssert{} );
  384. }
  385. bx::write(&writer, EGLint(EGL_NONE), bx::ErrorAssert{} );
  386. m_context = eglCreateContext(m_display, m_config, EGL_NO_CONTEXT, s_contextAttrs);
  387. if (NULL != m_context)
  388. {
  389. break;
  390. }
  391. BX_TRACE("Failed to create EGL context with EGL_CONTEXT_FLAGS_KHR (%08x).", flags);
  392. }
  393. BGFX_FATAL(m_context != EGL_NO_CONTEXT, Fatal::UnableToInitialize, "Failed to create context.");
  394. success = eglMakeCurrent(m_display, m_surface, m_surface, m_context);
  395. BGFX_FATAL(success, Fatal::UnableToInitialize, "Failed to set context.");
  396. m_current = NULL;
  397. eglSwapInterval(m_display, 0);
  398. }
  399. import();
  400. g_internalData.context = m_context;
  401. }
  402. void GlContext::destroy()
  403. {
  404. BX_TRACE("GLContext::destroy()");
  405. if (NULL != m_display)
  406. {
  407. EGL_CHECK(eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) );
  408. EGL_CHECK(eglDestroyContext(m_display, m_context) );
  409. EGL_CHECK(eglDestroySurface(m_display, m_surface) );
  410. # if BX_PLATFORM_LINUX
  411. if (m_eglWindow)
  412. {
  413. BGFX_WAYLAND_wl_egl_window_destroy(m_eglWindow);
  414. waylandEglClose(m_waylandEglDll);
  415. m_waylandEglDll = NULL;
  416. }
  417. # endif
  418. EGL_CHECK(eglTerminate(m_display) );
  419. m_context = NULL;
  420. }
  421. EGL_CHECK(eglReleaseThread() );
  422. eglClose(m_eglDll);
  423. m_eglDll = NULL;
  424. # if BX_PLATFORM_RPI
  425. bcm_host_deinit();
  426. # endif // BX_PLATFORM_RPI
  427. }
  428. void GlContext::resize(uint32_t _width, uint32_t _height, uint32_t _flags)
  429. {
  430. # if BX_PLATFORM_ANDROID
  431. if (NULL != m_display)
  432. {
  433. EGLNativeWindowType nwh = (EGLNativeWindowType )g_platformData.nwh;
  434. eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  435. eglDestroySurface(m_display, m_surface);
  436. m_surface = eglCreateWindowSurface(m_display, m_config, nwh, NULL);
  437. BGFX_FATAL(m_surface != EGL_NO_SURFACE, Fatal::UnableToInitialize, "Failed to create surface.");
  438. EGLBoolean success = eglMakeCurrent(m_display, m_surface, m_surface, m_context);
  439. BGFX_FATAL(success, Fatal::UnableToInitialize, "Failed to set context.");
  440. EGLint format;
  441. eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format);
  442. ANativeWindow_setBuffersGeometry( (ANativeWindow*)g_platformData.nwh, _width, _height, format);
  443. }
  444. # elif BX_PLATFORM_EMSCRIPTEN
  445. EMSCRIPTEN_CHECK(emscripten_set_canvas_element_size(HTML5_TARGET_CANVAS_SELECTOR, _width, _height) );
  446. # elif BX_PLATFORM_LINUX
  447. if (NULL != m_eglWindow)
  448. {
  449. BGFX_WAYLAND_wl_egl_window_resize(m_eglWindow, _width, _height, 0, 0);
  450. }
  451. # else
  452. BX_UNUSED(_width, _height);
  453. # endif // BX_PLATFORM_*
  454. if (NULL != m_display)
  455. {
  456. bool vsync = !!(_flags&BGFX_RESET_VSYNC);
  457. EGL_CHECK(eglSwapInterval(m_display, vsync ? 1 : 0) );
  458. }
  459. }
  460. uint64_t GlContext::getCaps() const
  461. {
  462. return BX_ENABLED(0
  463. | BX_PLATFORM_LINUX
  464. | BX_PLATFORM_WINDOWS
  465. | BX_PLATFORM_ANDROID
  466. )
  467. ? BGFX_CAPS_SWAP_CHAIN
  468. : 0
  469. ;
  470. }
  471. SwapChainGL* GlContext::createSwapChain(void* _nwh, int _width, int _height)
  472. {
  473. return BX_NEW(g_allocator, SwapChainGL)(m_display, m_config, m_context, (EGLNativeWindowType)_nwh, _width, _height);
  474. }
  475. void GlContext::destroySwapChain(SwapChainGL* _swapChain)
  476. {
  477. bx::deleteObject(g_allocator, _swapChain);
  478. }
  479. void GlContext::swap(SwapChainGL* _swapChain)
  480. {
  481. makeCurrent(_swapChain);
  482. if (NULL == _swapChain)
  483. {
  484. if (NULL != m_display)
  485. {
  486. EGL_CHECK(eglSwapBuffers(m_display, m_surface) );
  487. }
  488. }
  489. else
  490. {
  491. _swapChain->swapBuffers();
  492. }
  493. }
  494. void GlContext::makeCurrent(SwapChainGL* _swapChain)
  495. {
  496. if (m_current != _swapChain)
  497. {
  498. m_current = _swapChain;
  499. if (NULL == _swapChain)
  500. {
  501. if (NULL != m_display)
  502. {
  503. EGL_CHECK(eglMakeCurrent(m_display, m_surface, m_surface, m_context) );
  504. }
  505. }
  506. else
  507. {
  508. _swapChain->makeCurrent();
  509. }
  510. }
  511. }
  512. void GlContext::import()
  513. {
  514. BX_TRACE("Import:");
  515. # if BX_PLATFORM_WINDOWS || BX_PLATFORM_LINUX
  516. # if BX_PLATFORM_WINDOWS
  517. # define LIBRARY_NAME "libGL.dll"
  518. # elif BX_PLATFORM_LINUX
  519. # if BGFX_CONFIG_RENDERER_OPENGL
  520. # define LIBRARY_NAME "libGL.so.1"
  521. # else
  522. # define LIBRARY_NAME "libGLESv2.so.2"
  523. # endif
  524. # endif
  525. void* lib = bx::dlopen(LIBRARY_NAME);
  526. # define GL_EXTENSION(_optional, _proto, _func, _import) \
  527. { \
  528. if (NULL == _func) \
  529. { \
  530. _func = bx::dlsym<_proto>(lib, #_import); \
  531. BX_TRACE("\t%p " #_func " (" #_import ")", _func); \
  532. BGFX_FATAL(_optional || NULL != _func \
  533. , Fatal::UnableToInitialize \
  534. , "Failed to create OpenGLES context. eglGetProcAddress(\"%s\")" \
  535. , #_import); \
  536. } \
  537. }
  538. # else
  539. # define GL_EXTENSION(_optional, _proto, _func, _import) \
  540. { \
  541. if (NULL == _func) \
  542. { \
  543. _func = reinterpret_cast<_proto>(eglGetProcAddress(#_import) ); \
  544. BX_TRACE("\t%p " #_func " (" #_import ")", _func); \
  545. BGFX_FATAL(_optional || NULL != _func \
  546. , Fatal::UnableToInitialize \
  547. , "Failed to create OpenGLES context. eglGetProcAddress(\"%s\")" \
  548. , #_import); \
  549. } \
  550. }
  551. # endif // BX_PLATFORM_
  552. # include "glimports.h"
  553. # undef GL_EXTENSION
  554. }
  555. } /* namespace gl */ } // namespace bgfx
  556. # endif // BGFX_USE_EGL
  557. #endif // (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)