bgfx/src/glcontext_egl.cpp

426 lines
12 KiB
C++
Raw Normal View History

2013-01-13 21:39:25 -05:00
/*
2015-01-01 18:04:46 -05:00
* Copyright 2011-2015 Branimir Karadzic. All rights reserved.
2013-01-13 21:39:25 -05:00
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#include "bgfx_p.h"
2014-08-24 20:41:41 -04:00
#if (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)
2013-01-13 21:39:25 -05:00
# include "renderer_gl.h"
# if BGFX_USE_EGL
2014-08-24 20:41:41 -04:00
# if BX_PLATFORM_RPI
# include <bcm_host.h>
# endif // BX_PLATFORM_RPI
namespace bgfx { namespace gl
2013-01-13 21:39:25 -05:00
{
#if BGFX_USE_GL_DYNAMIC_LIB
typedef void (*EGLPROC)(void);
typedef EGLPROC (EGLAPIENTRY* PFNEGLGETPROCADDRESSPROC)(const char *procname);
typedef EGLBoolean (EGLAPIENTRY* PFNEGLSWAPINTERVALPROC)(EGLDisplay dpy, EGLint interval);
typedef EGLBoolean (EGLAPIENTRY* PFNEGLMAKECURRENTPROC)(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx);
typedef EGLContext (EGLAPIENTRY* PFNEGLCREATECONTEXTPROC)(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list);
typedef EGLSurface (EGLAPIENTRY* PFNEGLCREATEWINDOWSURFACEPROC)(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint *attrib_list);
typedef EGLBoolean (EGLAPIENTRY* PFNEGLCHOOSECONFIGPROC)(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config);
typedef EGLBoolean (EGLAPIENTRY* PFNEGLINITIALIZEPROC)(EGLDisplay dpy, EGLint *major, EGLint *minor);
2014-09-24 22:22:15 -04:00
typedef EGLint (EGLAPIENTRY* PFNEGLGETERRORPROC)(void);
typedef EGLDisplay (EGLAPIENTRY* PFNEGLGETDISPLAYPROC)(EGLNativeDisplayType display_id);
typedef EGLBoolean (EGLAPIENTRY* PFNEGLTERMINATEPROC)(EGLDisplay dpy);
typedef EGLBoolean (EGLAPIENTRY* PFNEGLDESTROYSURFACEPROC)(EGLDisplay dpy, EGLSurface surface);
typedef EGLBoolean (EGLAPIENTRY* PFNEGLDESTROYCONTEXTPROC)(EGLDisplay dpy, EGLContext ctx);
typedef EGLBoolean (EGLAPIENTRY* PFNEGLSWAPBUFFERSPROC)(EGLDisplay dpy, EGLSurface surface);
#define EGL_IMPORT \
EGL_IMPORT_FUNC(PFNEGLGETPROCADDRESSPROC, eglGetProcAddress); \
EGL_IMPORT_FUNC(PFNEGLSWAPINTERVALPROC, eglSwapInterval); \
EGL_IMPORT_FUNC(PFNEGLMAKECURRENTPROC, eglMakeCurrent); \
EGL_IMPORT_FUNC(PFNEGLCREATECONTEXTPROC, eglCreateContext); \
EGL_IMPORT_FUNC(PFNEGLCREATEWINDOWSURFACEPROC, eglCreateWindowSurface); \
EGL_IMPORT_FUNC(PFNEGLCHOOSECONFIGPROC, eglChooseConfig); \
EGL_IMPORT_FUNC(PFNEGLINITIALIZEPROC, eglInitialize); \
2014-09-24 22:22:15 -04:00
EGL_IMPORT_FUNC(PFNEGLGETERRORPROC, eglGetError); \
EGL_IMPORT_FUNC(PFNEGLGETDISPLAYPROC, eglGetDisplay); \
EGL_IMPORT_FUNC(PFNEGLTERMINATEPROC, eglTerminate); \
EGL_IMPORT_FUNC(PFNEGLDESTROYSURFACEPROC, eglDestroySurface); \
EGL_IMPORT_FUNC(PFNEGLDESTROYCONTEXTPROC, eglDestroyContext); \
EGL_IMPORT_FUNC(PFNEGLSWAPBUFFERSPROC, eglSwapBuffers);
#define EGL_IMPORT_FUNC(_proto, _func) _proto _func
EGL_IMPORT
#undef EGL_IMPORT_FUNC
void* eglOpen()
{
void* handle = bx::dlopen("libEGL." BX_DL_EXT);
2015-01-31 18:06:44 -05:00
BGFX_FATAL(NULL != handle, Fatal::UnableToInitialize, "Failed to load libEGL dynamic library.");
#define EGL_IMPORT_FUNC(_proto, _func) \
_func = (_proto)bx::dlsym(handle, #_func); \
BX_TRACE("%p " #_func, _func); \
BGFX_FATAL(NULL != _func, Fatal::UnableToInitialize, "Failed get " #_func ".")
EGL_IMPORT
#undef EGL_IMPORT_FUNC
return handle;
}
void eglClose(void* _handle)
{
bx::dlclose(_handle);
#define EGL_IMPORT_FUNC(_proto, _func) _func = NULL
EGL_IMPORT
#undef EGL_IMPORT_FUNC
}
#else
void* eglOpen()
{
return NULL;
}
void eglClose(void* /*_handle*/)
{
}
#endif // BGFX_USE_GL_DYNAMIC_LIB
# define GL_IMPORT(_optional, _proto, _func, _import) _proto _func = NULL
# include "glimports.h"
2013-01-13 21:39:25 -05:00
static EGLint s_contextAttrs[16];
2014-09-24 22:22:15 -04:00
struct SwapChainGL
{
SwapChainGL(EGLDisplay _display, EGLConfig _config, EGLContext _context, EGLNativeWindowType _nwh)
: m_nwh(_nwh)
, m_display(_display)
{
m_surface = eglCreateWindowSurface(m_display, _config, _nwh, NULL);
BGFX_FATAL(m_surface != EGL_NO_SURFACE, Fatal::UnableToInitialize, "Failed to create surface.");
m_context = eglCreateContext(m_display, _config, _context, s_contextAttrs);
BX_CHECK(NULL != m_context, "Create swap chain failed: %x", eglGetError() );
makeCurrent();
GL_CHECK(glClearColor(0.0f, 0.0f, 0.0f, 0.0f) );
GL_CHECK(glClear(GL_COLOR_BUFFER_BIT) );
swapBuffers();
GL_CHECK(glClear(GL_COLOR_BUFFER_BIT) );
swapBuffers();
}
~SwapChainGL()
{
eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroyContext(m_display, m_context);
eglDestroySurface(m_display, m_surface);
}
void makeCurrent()
{
eglMakeCurrent(m_display, m_surface, m_surface, m_context);
}
void swapBuffers()
{
eglSwapBuffers(m_display, m_surface);
}
EGLNativeWindowType m_nwh;
EGLContext m_context;
EGLDisplay m_display;
EGLSurface m_surface;
};
2014-08-24 20:41:41 -04:00
# if BX_PLATFORM_RPI
2014-08-24 23:39:35 -04:00
static EGL_DISPMANX_WINDOW_T s_dispmanWindow;
2014-08-24 20:41:41 -04:00
void x11SetDisplayWindow(::Display* _display, ::Window _window)
{
2014-08-24 23:39:35 -04:00
// Noop for now...
BX_UNUSED(_display, _window);
2014-08-24 20:41:41 -04:00
}
# endif // BX_PLATFORM_RPI
2013-12-09 23:18:51 -05:00
void GlContext::create(uint32_t _width, uint32_t _height)
2013-01-13 21:39:25 -05:00
{
2014-08-24 20:41:41 -04:00
# if BX_PLATFORM_RPI
bcm_host_init();
# endif // BX_PLATFORM_RPI
m_eglLibrary = eglOpen();
if (NULL == g_platformData.context)
{
BX_UNUSED(_width, _height);
EGLNativeDisplayType ndt = (EGLNativeDisplayType)g_platformData.ndt;
EGLNativeWindowType nwh = (EGLNativeWindowType )g_platformData.nwh;
2013-01-13 21:39:25 -05:00
# if BX_PLATFORM_WINDOWS
if (NULL == g_platformData.ndt)
{
ndt = GetDC( (HWND)g_platformData.nwh);
}
# endif // BX_PLATFORM_WINDOWS
2013-01-13 21:39:25 -05:00
m_display = eglGetDisplay(ndt);
BGFX_FATAL(m_display != EGL_NO_DISPLAY, Fatal::UnableToInitialize, "Failed to create display %p", m_display);
2013-01-13 21:39:25 -05:00
EGLint major = 0;
EGLint minor = 0;
EGLBoolean success = eglInitialize(m_display, &major, &minor);
BGFX_FATAL(success && major >= 1 && minor >= 3, Fatal::UnableToInitialize, "Failed to initialize %d.%d", major, minor);
2015-04-23 00:39:20 -04:00
BX_TRACE("EGL info:");
const char* clientApis = eglQueryString(m_display, EGL_CLIENT_APIS);
2015-05-01 18:12:44 -04:00
BX_TRACE(" APIs: %s", clientApis); BX_UNUSED(clientApis);
2015-04-23 00:39:20 -04:00
const char* vendor = eglQueryString(m_display, EGL_VENDOR);
2015-05-01 18:12:44 -04:00
BX_TRACE(" Vendor: %s", vendor); BX_UNUSED(vendor);
2015-04-23 00:39:20 -04:00
const char* version = eglQueryString(m_display, EGL_VERSION);
2015-05-01 18:12:44 -04:00
BX_TRACE("Version: %s", version); BX_UNUSED(version);
2015-04-23 00:39:20 -04:00
const char* extensions = eglQueryString(m_display, EGL_EXTENSIONS);
BX_TRACE("Supported EGL extensions:");
dumpExtensions(extensions);
EGLint attrs[] =
{
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
2013-01-13 21:39:25 -05:00
# if BX_PLATFORM_ANDROID
EGL_DEPTH_SIZE, 16,
2013-01-13 21:39:25 -05:00
# else
EGL_DEPTH_SIZE, 24,
2013-01-13 21:39:25 -05:00
# endif // BX_PLATFORM_
EGL_STENCIL_SIZE, 8,
2013-01-13 21:39:25 -05:00
EGL_NONE
};
2013-01-13 21:39:25 -05:00
EGLint numConfig = 0;
success = eglChooseConfig(m_display, attrs, &m_config, 1, &numConfig);
BGFX_FATAL(success, Fatal::UnableToInitialize, "eglChooseConfig");
2013-01-14 01:13:49 -05:00
2013-04-19 00:16:09 -04:00
# if BX_PLATFORM_ANDROID
EGLint format;
eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format);
ANativeWindow_setBuffersGeometry( (ANativeWindow*)g_platformData.nwh, _width, _height, format);
2014-08-24 23:39:35 -04:00
# elif BX_PLATFORM_RPI
DISPMANX_DISPLAY_HANDLE_T dispmanDisplay = vc_dispmanx_display_open(0);
DISPMANX_UPDATE_HANDLE_T dispmanUpdate = vc_dispmanx_update_start(0);
VC_RECT_T dstRect = { 0, 0, _width, _height };
VC_RECT_T srcRect = { 0, 0, _width << 16, _height << 16 };
DISPMANX_ELEMENT_HANDLE_T dispmanElement = vc_dispmanx_element_add(dispmanUpdate
, dispmanDisplay
, 0
, &dstRect
, 0
, &srcRect
, DISPMANX_PROTECTION_NONE
, NULL
, NULL
, DISPMANX_NO_ROTATE
);
s_dispmanWindow.element = dispmanElement;
s_dispmanWindow.width = _width;
s_dispmanWindow.height = _height;
nwh = &s_dispmanWindow;
vc_dispmanx_update_submit_sync(dispmanUpdate);
2013-04-19 00:16:09 -04:00
# endif // BX_PLATFORM_ANDROID
m_surface = eglCreateWindowSurface(m_display, m_config, nwh, NULL);
BGFX_FATAL(m_surface != EGL_NO_SURFACE, Fatal::UnableToInitialize, "Failed to create surface.");
2013-01-14 01:13:49 -05:00
2015-04-23 00:39:20 -04:00
const bool hasEglKhrCreateContext = !!bx::findIdentifierMatch(extensions, "EGL_KHR_create_context");
for (uint32_t ii = 0; ii < 2; ++ii)
{
bx::StaticMemoryBlockWriter writer(s_contextAttrs, sizeof(s_contextAttrs) );
bx::write(&writer, EGLint(EGL_CONTEXT_MAJOR_VERSION_KHR) );
bx::write(&writer, EGLint(BGFX_CONFIG_RENDERER_OPENGLES / 10) );
bx::write(&writer, EGLint(EGL_CONTEXT_MINOR_VERSION_KHR) );
bx::write(&writer, EGLint(BGFX_CONFIG_RENDERER_OPENGLES % 10) );
2015-04-23 00:39:20 -04:00
EGLint flags = 0;
if (hasEglKhrCreateContext
&& 0 == ii)
{
flags = BGFX_CONFIG_DEBUG ? 0
| EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR
// | EGL_OPENGL_ES3_BIT_KHR
: 0
;
2015-04-23 00:39:20 -04:00
bx::write(&writer, EGLint(EGL_CONTEXT_FLAGS_KHR) );
bx::write(&writer, flags);
}
bx::write(&writer, EGLint(EGL_NONE) );
2015-04-23 00:39:20 -04:00
m_context = eglCreateContext(m_display, m_config, EGL_NO_CONTEXT, s_contextAttrs);
if (NULL != m_context)
{
break;
}
BX_TRACE("Failed to create EGL context with EGL_CONTEXT_FLAGS_KHR (%08x).", flags);
}
BGFX_FATAL(m_context != EGL_NO_CONTEXT, Fatal::UnableToInitialize, "Failed to create context.");
2013-01-13 21:39:25 -05:00
success = eglMakeCurrent(m_display, m_surface, m_surface, m_context);
BGFX_FATAL(success, Fatal::UnableToInitialize, "Failed to set context.");
m_current = NULL;
2013-01-13 21:39:25 -05:00
eglSwapInterval(m_display, 0);
}
2013-01-13 21:39:25 -05:00
# if BX_PLATFORM_EMSCRIPTEN
emscripten_set_canvas_size(_width, _height);
# endif // BX_PLATFORM_EMSCRIPTEN
import();
}
void GlContext::destroy()
{
2015-04-18 03:39:57 -04:00
if (NULL != m_display)
{
eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroyContext(m_display, m_context);
eglDestroySurface(m_display, m_surface);
eglTerminate(m_display);
m_context = NULL;
}
2015-04-18 03:39:57 -04:00
eglClose(m_eglLibrary);
2014-08-24 20:41:41 -04:00
# if BX_PLATFORM_RPI
bcm_host_deinit();
# endif // BX_PLATFORM_RPI
2013-01-13 21:39:25 -05:00
}
2015-04-07 00:31:26 -04:00
void GlContext::resize(uint32_t _width, uint32_t _height, uint32_t _flags)
2013-01-13 21:39:25 -05:00
{
BX_UNUSED(_width, _height);
2015-04-18 03:39:57 -04:00
2014-12-24 02:32:36 -05:00
# if BX_PLATFORM_ANDROID
2015-04-18 03:39:57 -04:00
if (NULL != m_display)
{
EGLint format;
eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format);
ANativeWindow_setBuffersGeometry( (ANativeWindow*)g_platformData.nwh, _width, _height, format);
}
2014-12-24 02:32:36 -05:00
# endif // BX_PLATFORM_ANDROID
2015-04-18 03:39:57 -04:00
if (NULL != m_display)
{
bool vsync = !!(_flags&BGFX_RESET_VSYNC);
eglSwapInterval(m_display, vsync ? 1 : 0);
2015-04-18 03:39:57 -04:00
}
2013-01-13 21:39:25 -05:00
}
2014-09-23 23:35:39 -04:00
bool GlContext::isSwapChainSupported()
{
2014-09-24 22:22:15 -04:00
return BX_ENABLED(0
| BX_PLATFORM_LINUX
| BX_PLATFORM_WINDOWS
);
2014-09-23 23:35:39 -04:00
}
2014-09-24 22:22:15 -04:00
SwapChainGL* GlContext::createSwapChain(void* _nwh)
2013-01-13 21:39:25 -05:00
{
2014-09-24 22:22:15 -04:00
return BX_NEW(g_allocator, SwapChainGL)(m_display, m_config, m_context, (EGLNativeWindowType)_nwh);
2014-09-07 20:17:38 -04:00
}
2014-11-30 12:06:47 -05:00
void GlContext::destroySwapChain(SwapChainGL* _swapChain)
2014-09-07 20:17:38 -04:00
{
2014-09-24 22:22:15 -04:00
BX_DELETE(g_allocator, _swapChain);
2014-09-07 20:17:38 -04:00
}
void GlContext::swap(SwapChainGL* _swapChain)
{
makeCurrent(_swapChain);
2014-09-24 22:22:15 -04:00
if (NULL == _swapChain)
{
2015-04-18 03:39:57 -04:00
if (NULL != m_display)
{
eglSwapBuffers(m_display, m_surface);
2015-04-18 03:39:57 -04:00
}
2014-09-24 22:22:15 -04:00
}
else
{
_swapChain->swapBuffers();
}
2013-01-13 21:39:25 -05:00
}
2014-09-24 22:22:15 -04:00
void GlContext::makeCurrent(SwapChainGL* _swapChain)
2014-09-07 20:17:38 -04:00
{
if (m_current != _swapChain)
2014-09-24 22:22:15 -04:00
{
m_current = _swapChain;
if (NULL == _swapChain)
{
2015-04-18 03:39:57 -04:00
if (NULL != m_display)
{
eglMakeCurrent(m_display, m_surface, m_surface, m_context);
2015-04-18 03:39:57 -04:00
}
}
else
{
_swapChain->makeCurrent();
}
2014-09-24 22:22:15 -04:00
}
2014-09-07 20:17:38 -04:00
}
2013-01-13 21:39:25 -05:00
void GlContext::import()
{
BX_TRACE("Import:");
# if BX_PLATFORM_WINDOWS || BX_PLATFORM_LINUX
void* glesv2 = bx::dlopen("libGLESv2." BX_DL_EXT);
# define GL_EXTENSION(_optional, _proto, _func, _import) \
{ \
if (NULL == _func) \
{ \
_func = (_proto)bx::dlsym(glesv2, #_import); \
BX_TRACE("\t%p " #_func " (" #_import ")", _func); \
BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGLES context. eglGetProcAddress(\"%s\")", #_import); \
} \
}
2014-03-19 01:50:24 -04:00
# else
# define GL_EXTENSION(_optional, _proto, _func, _import) \
{ \
if (NULL == _func) \
{ \
_func = (_proto)eglGetProcAddress(#_import); \
BX_TRACE("\t%p " #_func " (" #_import ")", _func); \
BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGLES context. eglGetProcAddress(\"%s\")", #_import); \
} \
}
2014-03-19 01:50:24 -04:00
# endif // BX_PLATFORM_
# include "glimports.h"
2013-01-13 21:39:25 -05:00
}
} /* namespace gl */ } // namespace bgfx
2013-01-13 21:39:25 -05:00
# endif // BGFX_USE_EGL
2014-08-24 20:41:41 -04:00
#endif // (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)