2# include <Headless/OpenGLContext/EglOpenGLContext.hpp>
4# include <glbinding-aux/ValidVersions.h>
5# include <glbinding/glbinding.h>
6# include <globjects/globjects.h>
12# include <EGL/eglext.h>
17using namespace glbinding;
19static const EGLint configAttribs[] = { EGL_SURFACE_TYPE,
33struct EglOpenGLContext::ContextEGL {
39EglOpenGLContext::EglOpenGLContext(
const glbinding::Version& glVersion,
41 OpenGLContext( glVersion, size ), m_eglContext { new ContextEGL } {
42 static const EGLint pbufferAttribs[] = {
50 static const int MAX_DEVICES = 16;
51 EGLDeviceEXT eglDevs[MAX_DEVICES];
55 PFNEGLQUERYDEVICESEXTPROC eglQueryDevicesEXT =
56 (PFNEGLQUERYDEVICESEXTPROC)eglGetProcAddress(
"eglQueryDevicesEXT" );
57 eglQueryDevicesEXT( MAX_DEVICES, eglDevs, &numDevices );
58 if ( numDevices == 0 ) {
59 LOG( Core::Utils::logERROR ) <<
"EGL context creation : no devices available.";
62 PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT =
63 (PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress(
"eglGetPlatformDisplayEXT" );
64 m_eglContext->m_eglDpy = eglGetPlatformDisplayEXT( EGL_PLATFORM_DEVICE_EXT, eglDevs[0], 0 );
67 if ( eglInitialize( m_eglContext->m_eglDpy, &major, &minor ) != EGL_TRUE ) {
68 LOG( Core::Utils::logERROR ) <<
"EGL context creation : Unable to initialize EGL ";
75 if ( eglChooseConfig( m_eglContext->m_eglDpy, configAttribs, &eglCfg, 1, &numConfigs ) !=
77 LOG( Core::Utils::logERROR ) <<
"EGL context creation : Unable to choose EGL config";
84 m_eglContext->m_eglSurf =
85 eglCreatePbufferSurface( m_eglContext->m_eglDpy, eglCfg, pbufferAttribs );
86 if ( m_eglContext->m_eglSurf == EGL_NO_SURFACE ) {
87 LOG( Core::Utils::logERROR ) <<
"EGL context creation : Unable to create pbuffer";
92 if ( eglBindAPI( EGL_OPENGL_API ) == EGL_FALSE ) {
93 LOG( Core::Utils::logERROR ) <<
"EGL context creation : Unable to bing OpenGL API";
98 EGLint contextAttribs[] = {
99 EGL_CONTEXT_MAJOR_VERSION,
100 glVersion.majorVersion(),
101 EGL_CONTEXT_MINOR_VERSION,
102 glVersion.minorVersion(),
103 EGL_CONTEXT_OPENGL_PROFILE_MASK,
104 EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT,
107 m_eglContext->m_eglCtx =
108 eglCreateContext( m_eglContext->m_eglDpy, eglCfg, EGL_NO_CONTEXT, contextAttribs );
109 if ( m_eglContext->m_eglCtx == EGL_NO_CONTEXT ) {
110 LOG( Core::Utils::logERROR ) <<
"EGL context creation : Unable to get context";
116 eglMakeCurrent( m_eglContext->m_eglDpy,
117 m_eglContext->m_eglSurf,
118 m_eglContext->m_eglSurf,
119 m_eglContext->m_eglCtx );
120 globjects::init( [](
const char* name ) {
return eglGetProcAddress( name ); } );
122EglOpenGLContext::~EglOpenGLContext() {
123 eglTerminate( m_eglContext->m_eglDpy );
125void EglOpenGLContext::makeCurrent()
const {
126 if ( m_eglContext->m_eglCtx ) {
127 eglMakeCurrent( m_eglContext->m_eglDpy,
128 m_eglContext->m_eglSurf,
129 m_eglContext->m_eglSurf,
130 m_eglContext->m_eglCtx );
134void EglOpenGLContext::doneCurrent()
const {
135 if ( m_eglContext->m_eglCtx ) {
136 eglMakeCurrent( m_eglContext->m_eglDpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
140bool EglOpenGLContext::isValid()
const {
141 return m_eglContext->m_eglCtx != EGL_NO_CONTEXT;
145 auto generalInfo = OpenGLContext::getInfo();
147 infoText << generalInfo <<
"Context provider : EGL\n";
148 return infoText.
str();
hepler function to manage enum as underlying types in VariableSet