glcontext_egl.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright 2011-2014 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  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. #ifndef EGL_CONTEXT_MAJOR_VERSION_KHR
  10. # define EGL_CONTEXT_MAJOR_VERSION_KHR EGL_CONTEXT_CLIENT_VERSION
  11. #endif // EGL_CONTEXT_MAJOR_VERSION_KHR
  12. #ifndef EGL_CONTEXT_MINOR_VERSION_KHR
  13. # define EGL_CONTEXT_MINOR_VERSION_KHR 0x30FB
  14. #endif // EGL_CONTEXT_MINOR_VERSION_KHR
  15. namespace bgfx
  16. {
  17. #if BGFX_USE_GL_DYNAMIC_LIB
  18. typedef void (*EGLPROC)(void);
  19. typedef EGLPROC (EGLAPIENTRY* PFNEGLGETPROCADDRESSPROC)(const char *procname);
  20. typedef EGLBoolean (EGLAPIENTRY* PFNEGLSWAPINTERVALPROC)(EGLDisplay dpy, EGLint interval);
  21. typedef EGLBoolean (EGLAPIENTRY* PFNEGLMAKECURRENTPROC)(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx);
  22. typedef EGLContext (EGLAPIENTRY* PFNEGLCREATECONTEXTPROC)(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list);
  23. typedef EGLSurface (EGLAPIENTRY* PFNEGLCREATEWINDOWSURFACEPROC)(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint *attrib_list);
  24. typedef EGLBoolean (EGLAPIENTRY* PFNEGLCHOOSECONFIGPROC)(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config);
  25. typedef EGLBoolean (EGLAPIENTRY* PFNEGLINITIALIZEPROC)(EGLDisplay dpy, EGLint *major, EGLint *minor);
  26. typedef EGLDisplay (EGLAPIENTRY* PFNEGLGETDISPLAYPROC)(EGLNativeDisplayType display_id);
  27. typedef EGLBoolean (EGLAPIENTRY* PFNEGLTERMINATEPROC)(EGLDisplay dpy);
  28. typedef EGLBoolean (EGLAPIENTRY* PFNEGLDESTROYSURFACEPROC)(EGLDisplay dpy, EGLSurface surface);
  29. typedef EGLBoolean (EGLAPIENTRY* PFNEGLDESTROYCONTEXTPROC)(EGLDisplay dpy, EGLContext ctx);
  30. typedef EGLBoolean (EGLAPIENTRY* PFNEGLSWAPBUFFERSPROC)(EGLDisplay dpy, EGLSurface surface);
  31. #define EGL_IMPORT \
  32. EGL_IMPORT_FUNC(PFNEGLGETPROCADDRESSPROC, eglGetProcAddress); \
  33. EGL_IMPORT_FUNC(PFNEGLSWAPINTERVALPROC, eglSwapInterval); \
  34. EGL_IMPORT_FUNC(PFNEGLMAKECURRENTPROC, eglMakeCurrent); \
  35. EGL_IMPORT_FUNC(PFNEGLCREATECONTEXTPROC, eglCreateContext); \
  36. EGL_IMPORT_FUNC(PFNEGLCREATEWINDOWSURFACEPROC, eglCreateWindowSurface); \
  37. EGL_IMPORT_FUNC(PFNEGLCHOOSECONFIGPROC, eglChooseConfig); \
  38. EGL_IMPORT_FUNC(PFNEGLINITIALIZEPROC, eglInitialize); \
  39. EGL_IMPORT_FUNC(PFNEGLGETDISPLAYPROC, eglGetDisplay); \
  40. EGL_IMPORT_FUNC(PFNEGLTERMINATEPROC, eglTerminate); \
  41. EGL_IMPORT_FUNC(PFNEGLDESTROYSURFACEPROC, eglDestroySurface); \
  42. EGL_IMPORT_FUNC(PFNEGLDESTROYCONTEXTPROC, eglDestroyContext); \
  43. EGL_IMPORT_FUNC(PFNEGLSWAPBUFFERSPROC, eglSwapBuffers);
  44. #define EGL_IMPORT_FUNC(_proto, _func) _proto _func
  45. EGL_IMPORT
  46. #undef EGL_IMPORT_FUNC
  47. void* eglOpen()
  48. {
  49. void* handle = bx::dlopen("libEGL.dll");
  50. BGFX_FATAL(NULL != handle, Fatal::UnableToInitialize, "Failed to load libEGL dynamic library.");
  51. #define EGL_IMPORT_FUNC(_proto, _func) \
  52. _func = (_proto)bx::dlsym(handle, #_func); \
  53. BX_TRACE("%p " #_func, _func); \
  54. BGFX_FATAL(NULL != _func, Fatal::UnableToInitialize, "Failed get " #_func ".")
  55. EGL_IMPORT
  56. #undef EGL_IMPORT_FUNC
  57. return handle;
  58. }
  59. void eglClose(void* _handle)
  60. {
  61. bx::dlclose(_handle);
  62. #define EGL_IMPORT_FUNC(_proto, _func) _func = NULL
  63. EGL_IMPORT
  64. #undef EGL_IMPORT_FUNC
  65. }
  66. #else
  67. void* eglOpen()
  68. {
  69. return NULL;
  70. }
  71. void eglClose(void* /*_handle*/)
  72. {
  73. }
  74. #endif // BGFX_USE_GL_DYNAMIC_LIB
  75. # define GL_IMPORT(_optional, _proto, _func, _import) _proto _func = NULL
  76. # include "glimports.h"
  77. void GlContext::create(uint32_t _width, uint32_t _height)
  78. {
  79. m_eglLibrary = eglOpen();
  80. BX_UNUSED(_width, _height);
  81. EGLNativeDisplayType ndt = EGL_DEFAULT_DISPLAY;
  82. EGLNativeWindowType nwt = (EGLNativeWindowType)NULL;
  83. # if BX_PLATFORM_WINDOWS
  84. ndt = GetDC(g_bgfxHwnd);
  85. nwt = g_bgfxHwnd;
  86. # endif // BX_PLATFORM_
  87. m_display = eglGetDisplay(ndt);
  88. BGFX_FATAL(m_display != EGL_NO_DISPLAY, Fatal::UnableToInitialize, "Failed to create display %p", m_display);
  89. EGLint major = 0;
  90. EGLint minor = 0;
  91. EGLBoolean success = eglInitialize(m_display, &major, &minor);
  92. BGFX_FATAL(success && major >= 1 && minor >= 3, Fatal::UnableToInitialize, "Failed to initialize %d.%d", major, minor);
  93. EGLint attrs[] =
  94. {
  95. EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
  96. # if BX_PLATFORM_ANDROID
  97. EGL_DEPTH_SIZE, 16,
  98. # else
  99. EGL_DEPTH_SIZE, 24,
  100. # endif // BX_PLATFORM_
  101. EGL_STENCIL_SIZE, 8,
  102. EGL_NONE
  103. };
  104. EGLint numConfig = 0;
  105. EGLConfig config;
  106. success = eglChooseConfig(m_display, attrs, &config, 1, &numConfig);
  107. BGFX_FATAL(success, Fatal::UnableToInitialize, "eglChooseConfig");
  108. # if BX_PLATFORM_ANDROID
  109. EGLint format;
  110. eglGetConfigAttrib(m_display, config, EGL_NATIVE_VISUAL_ID, &format);
  111. ANativeWindow_setBuffersGeometry(g_bgfxAndroidWindow, _width, _height, format);
  112. nwt = g_bgfxAndroidWindow;
  113. # endif // BX_PLATFORM_ANDROID
  114. m_surface = eglCreateWindowSurface(m_display, config, nwt, NULL);
  115. BGFX_FATAL(m_surface != EGL_NO_SURFACE, Fatal::UnableToInitialize, "Failed to create surface.");
  116. EGLint contextAttrs[] =
  117. {
  118. # if BGFX_CONFIG_RENDERER_OPENGLES >= 30
  119. EGL_CONTEXT_MAJOR_VERSION_KHR, 3,
  120. # if BGFX_CONFIG_RENDERER_OPENGLES >= 31
  121. EGL_CONTEXT_MINOR_VERSION_KHR, 1,
  122. # else
  123. // EGL_CONTEXT_MINOR_VERSION_KHR, 0,
  124. # endif // BGFX_CONFIG_RENDERER_OPENGLES >= 31
  125. # elif BGFX_CONFIG_RENDERER_OPENGLES
  126. EGL_CONTEXT_MAJOR_VERSION_KHR, 2,
  127. // EGL_CONTEXT_MINOR_VERSION_KHR, 0,
  128. # endif // BGFX_CONFIG_RENDERER_
  129. EGL_NONE
  130. };
  131. m_context = eglCreateContext(m_display, config, EGL_NO_CONTEXT, contextAttrs);
  132. BGFX_FATAL(m_context != EGL_NO_CONTEXT, Fatal::UnableToInitialize, "Failed to create context.");
  133. success = eglMakeCurrent(m_display, m_surface, m_surface, m_context);
  134. BGFX_FATAL(success, Fatal::UnableToInitialize, "Failed to set context.");
  135. eglSwapInterval(m_display, 0);
  136. # if BX_PLATFORM_EMSCRIPTEN
  137. emscripten_set_canvas_size(_width, _height);
  138. # endif // BX_PLATFORM_EMSCRIPTEN
  139. import();
  140. }
  141. void GlContext::destroy()
  142. {
  143. eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  144. eglDestroyContext(m_display, m_context);
  145. eglDestroySurface(m_display, m_surface);
  146. eglTerminate(m_display);
  147. m_context = NULL;
  148. eglClose(m_eglLibrary);
  149. }
  150. void GlContext::resize(uint32_t /*_width*/, uint32_t /*_height*/, bool _vsync)
  151. {
  152. eglSwapInterval(m_display, _vsync ? 1 : 0);
  153. }
  154. void GlContext::swap()
  155. {
  156. eglMakeCurrent(m_display, m_surface, m_surface, m_context);
  157. eglSwapBuffers(m_display, m_surface);
  158. }
  159. void GlContext::import()
  160. {
  161. BX_TRACE("Import:");
  162. # if BX_PLATFORM_WINDOWS
  163. void* glesv2 = bx::dlopen("libGLESv2.dll");
  164. # define GL_EXTENSION(_optional, _proto, _func, _import) \
  165. { \
  166. if (NULL == _func) \
  167. { \
  168. _func = (_proto)bx::dlsym(glesv2, #_import); \
  169. BX_TRACE("\t%p " #_func " (" #_import ")", _func); \
  170. BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGLES context. eglGetProcAddress(\"%s\")", #_import); \
  171. } \
  172. }
  173. # else
  174. # define GL_EXTENSION(_optional, _proto, _func, _import) \
  175. { \
  176. if (NULL == _func) \
  177. { \
  178. _func = (_proto)eglGetProcAddress(#_import); \
  179. BX_TRACE("\t%p " #_func " (" #_import ")", _func); \
  180. BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGLES context. eglGetProcAddress(\"%s\")", #_import); \
  181. } \
  182. }
  183. # endif // BX_PLATFORM_
  184. # include "glimports.h"
  185. }
  186. } // namespace bgfx
  187. # endif // BGFX_USE_EGL
  188. #endif // (BGFX_CONFIG_RENDERER_OPENGLES|BGFX_CONFIG_RENDERER_OPENGL)