glcontext_egl.cpp 3.7 KB

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