bgfx/src/glcontext_egl.cpp

366 lines
11 KiB
C++
Raw Normal View History

2013-01-13 21:39:25 -05:00
/*
2014-02-11 01:07:04 -05:00
* Copyright 2011-2014 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
2014-07-20 23:27:13 -04:00
#ifndef EGL_CONTEXT_MAJOR_VERSION_KHR
# define EGL_CONTEXT_MAJOR_VERSION_KHR EGL_CONTEXT_CLIENT_VERSION
#endif // EGL_CONTEXT_MAJOR_VERSION_KHR
#ifndef EGL_CONTEXT_MINOR_VERSION_KHR
# define EGL_CONTEXT_MINOR_VERSION_KHR 0x30FB
#endif // EGL_CONTEXT_MINOR_VERSION_KHR
2013-01-13 21:39:25 -05:00
namespace bgfx
{
#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.dll");
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
2014-09-24 22:22:15 -04:00
static const EGLint s_contextAttrs[] =
{
# if BGFX_CONFIG_RENDERER_OPENGLES >= 30
EGL_CONTEXT_MAJOR_VERSION_KHR, 3,
# if BGFX_CONFIG_RENDERER_OPENGLES >= 31
EGL_CONTEXT_MINOR_VERSION_KHR, 1,
# else
// EGL_CONTEXT_MINOR_VERSION_KHR, 0,
# endif // BGFX_CONFIG_RENDERER_OPENGLES >= 31
# elif BGFX_CONFIG_RENDERER_OPENGLES
EGL_CONTEXT_MAJOR_VERSION_KHR, 2,
// EGL_CONTEXT_MINOR_VERSION_KHR, 0,
# endif // BGFX_CONFIG_RENDERER_
EGL_NONE
};
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();
2013-12-11 01:18:00 -05:00
BX_UNUSED(_width, _height);
2013-01-13 21:39:25 -05:00
EGLNativeDisplayType ndt = EGL_DEFAULT_DISPLAY;
2014-09-24 22:22:15 -04:00
EGLNativeWindowType nwh = (EGLNativeWindowType)NULL;
2013-01-13 21:39:25 -05:00
# if BX_PLATFORM_WINDOWS
ndt = GetDC(g_bgfxHwnd);
2014-09-24 22:22:15 -04:00
nwh = g_bgfxHwnd;
2013-01-13 21:39:25 -05:00
# endif // BX_PLATFORM_
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);
EGLint attrs[] =
{
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
# if BX_PLATFORM_ANDROID
EGL_DEPTH_SIZE, 16,
# else
EGL_DEPTH_SIZE, 24,
# endif // BX_PLATFORM_
2013-10-20 01:17:52 -04:00
EGL_STENCIL_SIZE, 8,
2013-01-13 21:39:25 -05:00
EGL_NONE
};
2013-01-14 01:13:49 -05:00
EGLint numConfig = 0;
2014-09-24 22:22:15 -04:00
success = eglChooseConfig(m_display, attrs, &m_config, 1, &numConfig);
2013-01-14 01:13:49 -05:00
BGFX_FATAL(success, Fatal::UnableToInitialize, "eglChooseConfig");
2013-04-19 00:16:09 -04:00
# if BX_PLATFORM_ANDROID
EGLint format;
2014-09-24 22:22:15 -04:00
eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format);
2013-12-09 23:18:51 -05:00
ANativeWindow_setBuffersGeometry(g_bgfxAndroidWindow, _width, _height, format);
2014-09-24 22:22:15 -04:00
nwh = g_bgfxAndroidWindow;
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;
2014-09-24 22:22:15 -04:00
nwh = &s_dispmanWindow;
2014-08-24 23:39:35 -04:00
vc_dispmanx_update_submit_sync(dispmanUpdate);
2013-04-19 00:16:09 -04:00
# endif // BX_PLATFORM_ANDROID
2014-09-24 22:22:15 -04:00
m_surface = eglCreateWindowSurface(m_display, m_config, nwh, NULL);
2013-01-14 01:13:49 -05:00
BGFX_FATAL(m_surface != EGL_NO_SURFACE, Fatal::UnableToInitialize, "Failed to create surface.");
2014-09-24 22:22:15 -04:00
m_context = eglCreateContext(m_display, m_config, EGL_NO_CONTEXT, s_contextAttrs);
2013-01-13 21:39:25 -05:00
BGFX_FATAL(m_context != EGL_NO_CONTEXT, Fatal::UnableToInitialize, "Failed to create context.");
success = eglMakeCurrent(m_display, m_surface, m_surface, m_context);
BGFX_FATAL(success, Fatal::UnableToInitialize, "Failed to set context.");
2013-04-27 18:16:05 -04: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()
{
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;
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
}
2014-12-24 02:32:36 -05:00
void GlContext::resize(uint32_t _width, uint32_t _height, bool _vsync)
2013-01-13 21:39:25 -05:00
{
2014-12-24 02:32:36 -05:00
# if BX_PLATFORM_ANDROID
EGLint format;
eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format);
ANativeWindow_setBuffersGeometry(g_bgfxAndroidWindow, _width, _height, format);
# endif // BX_PLATFORM_ANDROID
2013-04-27 18:16:05 -04:00
eglSwapInterval(m_display, _vsync ? 1 : 0);
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)
{
2014-09-24 22:22:15 -04:00
if (NULL == _swapChain)
{
eglMakeCurrent(m_display, m_surface, m_surface, m_context);
eglSwapBuffers(m_display, m_surface);
}
else
{
_swapChain->makeCurrent();
_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
{
2014-09-24 22:22:15 -04:00
if (NULL == _swapChain)
{
eglMakeCurrent(m_display, m_surface, m_surface, m_context);
}
else
{
_swapChain->makeCurrent();
}
2014-09-07 20:17:38 -04:00
}
2013-01-13 21:39:25 -05:00
void GlContext::import()
{
BX_TRACE("Import:");
2014-03-19 01:50:24 -04:00
# if BX_PLATFORM_WINDOWS
void* glesv2 = bx::dlopen("libGLESv2.dll");
# 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 bgfx
# endif // BGFX_USE_EGL
2014-08-24 20:41:41 -04:00
#endif // (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)