Add GLSurfaceView / external EGLContext support on Android by skipping the creation of BGFX's eglContext if g_bgfxAndroidWindow is set to NULL

This commit is contained in:
Ka-ming Chan 2015-04-18 15:26:17 +08:00
parent 62d2d744f9
commit 9e7e143f21

View file

@ -178,6 +178,15 @@ EGL_IMPORT
m_eglLibrary = eglOpen(); m_eglLibrary = eglOpen();
# if BX_PLATFORM_ANDROID
if (!g_bgfxAndroidWindow)
{
BX_TRACE("androidSetWindow() was not called, assuming EGLContext and buffer-swapping are managed outside bgfx.");
}
else
{
# endif
BX_UNUSED(_width, _height); BX_UNUSED(_width, _height);
EGLNativeDisplayType ndt = EGL_DEFAULT_DISPLAY; EGLNativeDisplayType ndt = EGL_DEFAULT_DISPLAY;
EGLNativeWindowType nwh = (EGLNativeWindowType)NULL; EGLNativeWindowType nwh = (EGLNativeWindowType)NULL;
@ -257,7 +266,9 @@ EGL_IMPORT
m_current = NULL; m_current = NULL;
eglSwapInterval(m_display, 0); eglSwapInterval(m_display, 0);
# if BX_PLATFORM_ANDROID
}
# endif
# if BX_PLATFORM_EMSCRIPTEN # if BX_PLATFORM_EMSCRIPTEN
emscripten_set_canvas_size(_width, _height); emscripten_set_canvas_size(_width, _height);
# endif // BX_PLATFORM_EMSCRIPTEN # endif // BX_PLATFORM_EMSCRIPTEN
@ -267,11 +278,14 @@ EGL_IMPORT
void GlContext::destroy() void GlContext::destroy()
{ {
eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); if (m_display)
eglDestroyContext(m_display, m_context); {
eglDestroySurface(m_display, m_surface); eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglTerminate(m_display); eglDestroyContext(m_display, m_context);
m_context = NULL; eglDestroySurface(m_display, m_surface);
eglTerminate(m_display);
m_context = NULL;
}
eglClose(m_eglLibrary); eglClose(m_eglLibrary);
@ -283,14 +297,19 @@ EGL_IMPORT
void GlContext::resize(uint32_t _width, uint32_t _height, uint32_t _flags) void GlContext::resize(uint32_t _width, uint32_t _height, uint32_t _flags)
{ {
BX_UNUSED(_width, _height); BX_UNUSED(_width, _height);
# if BX_PLATFORM_ANDROID # if BX_PLATFORM_ANDROID
EGLint format; if (m_display)
eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format); {
ANativeWindow_setBuffersGeometry(g_bgfxAndroidWindow, _width, _height, format); EGLint format;
eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format);
ANativeWindow_setBuffersGeometry(g_bgfxAndroidWindow, _width, _height, format);
}
# endif // BX_PLATFORM_ANDROID # endif // BX_PLATFORM_ANDROID
bool vsync = !!(_flags&BGFX_RESET_VSYNC); bool vsync = !!(_flags&BGFX_RESET_VSYNC);
eglSwapInterval(m_display, vsync ? 1 : 0); if (m_display)
eglSwapInterval(m_display, vsync ? 1 : 0);
} }
bool GlContext::isSwapChainSupported() bool GlContext::isSwapChainSupported()
@ -317,7 +336,8 @@ EGL_IMPORT
if (NULL == _swapChain) if (NULL == _swapChain)
{ {
eglSwapBuffers(m_display, m_surface); if (m_display)
eglSwapBuffers(m_display, m_surface);
} }
else else
{ {
@ -333,7 +353,8 @@ EGL_IMPORT
if (NULL == _swapChain) if (NULL == _swapChain)
{ {
eglMakeCurrent(m_display, m_surface, m_surface, m_context); if (m_display)
eglMakeCurrent(m_display, m_surface, m_surface, m_context);
} }
else else
{ {