glcontext_egl.cpp 7.1 KB

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