glcontext_egl.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2011-2013 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. # define GL_IMPORT(_optional, _proto, _func) _proto _func
  12. # include "glimports.h"
  13. # undef GL_IMPORT
  14. void GlContext::create(uint32_t /*_width*/, uint32_t /*_height*/)
  15. {
  16. EGLNativeDisplayType ndt = EGL_DEFAULT_DISPLAY;
  17. EGLNativeWindowType nwt = (EGLNativeWindowType)NULL;
  18. # if BX_PLATFORM_WINDOWS
  19. ndt = GetDC(g_bgfxHwnd);
  20. nwt = g_bgfxHwnd;
  21. # endif // BX_PLATFORM_
  22. m_display = eglGetDisplay(ndt);
  23. BGFX_FATAL(m_display != EGL_NO_DISPLAY, Fatal::UnableToInitialize, "Failed to create display 0x%08x", m_display);
  24. EGLint major = 0;
  25. EGLint minor = 0;
  26. EGLBoolean success = eglInitialize(m_display, &major, &minor);
  27. BGFX_FATAL(success && major >= 1 && minor >= 3, Fatal::UnableToInitialize, "Failed to initialize %d.%d", major, minor);
  28. EGLint attrs[] =
  29. {
  30. EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
  31. # if BX_PLATFORM_ANDROID
  32. EGL_DEPTH_SIZE, 16,
  33. # else
  34. EGL_DEPTH_SIZE, 24,
  35. # endif // BX_PLATFORM_
  36. EGL_NONE
  37. };
  38. EGLint numConfig = 0;
  39. EGLConfig config;
  40. success = eglChooseConfig(m_display, attrs, &config, 1, &numConfig);
  41. BGFX_FATAL(success, Fatal::UnableToInitialize, "eglChooseConfig");
  42. # if BX_PLATFORM_ANDROID
  43. EGLint format;
  44. eglGetConfigAttrib(m_display, config, EGL_NATIVE_VISUAL_ID, &format);
  45. ANativeWindow_setBuffersGeometry(g_bgfxAndroidWindow, 0, 0, format);
  46. nwt = g_bgfxAndroidWindow;
  47. # endif // BX_PLATFORM_ANDROID
  48. m_surface = eglCreateWindowSurface(m_display, config, nwt, NULL);
  49. BGFX_FATAL(m_surface != EGL_NO_SURFACE, Fatal::UnableToInitialize, "Failed to create surface.");
  50. EGLint contextAttrs[] =
  51. {
  52. # if BGFX_CONFIG_RENDERER_OPENGLES2
  53. EGL_CONTEXT_CLIENT_VERSION, 2,
  54. # elif BGFX_CONFIG_RENDERER_OPENGLES3
  55. EGL_CONTEXT_CLIENT_VERSION, 3,
  56. # endif // BGFX_CONFIG_RENDERER_
  57. EGL_NONE
  58. };
  59. m_context = eglCreateContext(m_display, config, EGL_NO_CONTEXT, contextAttrs);
  60. BGFX_FATAL(m_context != EGL_NO_CONTEXT, Fatal::UnableToInitialize, "Failed to create context.");
  61. success = eglMakeCurrent(m_display, m_surface, m_surface, m_context);
  62. BGFX_FATAL(success, Fatal::UnableToInitialize, "Failed to set context.");
  63. eglSwapInterval(m_display, 0);
  64. # if BX_PLATFORM_EMSCRIPTEN
  65. emscripten_set_canvas_size(_width, _height);
  66. # endif // BX_PLATFORM_EMSCRIPTEN
  67. import();
  68. }
  69. void GlContext::destroy()
  70. {
  71. eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  72. eglDestroyContext(m_display, m_context);
  73. eglDestroySurface(m_display, m_surface);
  74. eglTerminate(m_display);
  75. m_context = NULL;
  76. }
  77. void GlContext::resize(uint32_t /*_width*/, uint32_t /*_height*/, bool _vsync)
  78. {
  79. eglSwapInterval(m_display, _vsync ? 1 : 0);
  80. }
  81. void GlContext::swap()
  82. {
  83. eglMakeCurrent(m_display, m_surface, m_surface, m_context);
  84. eglSwapBuffers(m_display, m_surface);
  85. }
  86. void GlContext::import()
  87. {
  88. # if !BX_PLATFORM_EMSCRIPTEN
  89. # define GL_IMPORT(_optional, _proto, _func) \
  90. { \
  91. _func = (_proto)eglGetProcAddress(#_func); \
  92. BX_TRACE(#_func " 0x%08x", _func); \
  93. BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGLES context. eglGetProcAddress(\"%s\")", #_func); \
  94. }
  95. # include "glimports.h"
  96. # undef GL_IMPORT
  97. # endif // !BX_PLATFORM_EMSCRIPTEN
  98. }
  99. } // namespace bgfx
  100. # endif // BGFX_USE_EGL
  101. #endif // (BGFX_CONFIG_RENDERER_OPENGLES2|BGFX_CONFIG_RENDERER_OPENGLES3|BGFX_CONFIG_RENDERER_OPENGL)