int chooseConfig(EGLDisplay display, EGLConfig *config)
{
EGLint numConfigs = 0;
if(!eglGetConfigs(display, nullptr, 0, &numConfigs) || numConfigs == 0)
{
trace("ERROR: There are no graphics configurations available for this device");
return 0;
}
EGLConfig *configs = new EGLConfig[numConfigs];
eglGetConfigs(display, configs, numConfigs, &numConfigs);
EGLConfig bestConfig = 0;
EGLint bestSurfaceType = 0;
EGLint bestRenderableType = 0;
EGLint bestRedSize = 0;
EGLint bestGreenSize = 0;
EGLint bestBlueSize = 0;
EGLint bestAlphaSize = 0;
EGLint bestBufferSize = 0;
EGLint bestDepthSize = 0;
for(int i = 0; i < numConfigs; ++i)
{
EGLint surfaceType;
EGLint renderableType;
EGLint redSize;
EGLint greenSize;
EGLint blueSize;
EGLint alphaSize;
EGLint bufferSize;
EGLint depthSize;
eglGetConfigAttrib(display, configs[i], EGL_SURFACE_TYPE, &surfaceType);
eglGetConfigAttrib(display, configs[i], EGL_RENDERABLE_TYPE, &renderableType);
eglGetConfigAttrib(display, configs[i], EGL_RED_SIZE, &redSize);
eglGetConfigAttrib(display, configs[i], EGL_GREEN_SIZE, &greenSize);
eglGetConfigAttrib(display, configs[i], EGL_BLUE_SIZE, &blueSize);
eglGetConfigAttrib(display, configs[i], EGL_ALPHA_SIZE, &alphaSize);
eglGetConfigAttrib(display, configs[i], EGL_BUFFER_SIZE, &bufferSize);
eglGetConfigAttrib(display, configs[i], EGL_DEPTH_SIZE, &depthSize);
// Must support both of these. Window bit for main window,
// and pbuffer bit for the extra pbuffer in the aux ctx.
int neededSurfType = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
if(surfaceType & neededSurfType != neededSurfType)
continue;
// must support gles2
if(renderableType & EGL_OPENGL_ES2_BIT == 0)
continue;
// must have at least some bits in each channel
// example: RGB with no alpha is no good (for me)
if(redSize == 0 || greenSize == 0 || blueSize == 0 || alphaSize == 0)
continue;
// must have both a color buffer and a depth buffer
if(bufferSize == 0 || depthSize == 0)
continue;
// make sure config with most the precision is used
if(bestConfig == nullptr
|| renderableType > bestRenderableType
|| redSize > bestRedSize
|| greenSize > bestGreenSize
|| blueSize > bestBlueSize
|| alphaSize > bestAlphaSize
|| bufferSize > bestBufferSize
|| depthSize > bestDepthSize)
{
bestConfig = configs[i];
bestSurfaceType = surfaceType;
bestRenderableType = renderableType;
bestRedSize = redSize;
bestGreenSize = greenSize;
bestBlueSize = blueSize;
bestAlphaSize = alphaSize;
bestBufferSize = bufferSize;
bestDepthSize = depthSize;
}
}
delete [] configs;
// a config was found that matches my minimum requirements
if(bestConfig != 0)
{
*config = bestConfig;
trace("Using Display Configuration:");
trace("EGL_SURFACE_TYPE - ", bestSurfaceType);
trace("EGL_RENDERABLE_TYPE - ", bestRenderableType);
trace("EGL_RED_SIZE - ", bestRedSize);
trace("EGL_GREEN_SIZE - ", bestGreenSize);
trace("EGL_BLUE_SIZE - ", bestBlueSize);
trace("EGL_ALPHA_SIZE - ", bestAlphaSize);
trace("EGL_BUFFER_SIZE - ", bestBufferSize);
trace("EGL_DEPTH_SIZE - ", bestDepthSize);
trace("-----------------------------");
return 1;
}
// no config found.
return 0;
}