Refactored SDL_EGL_CreateContext: It now supports context flags and OpenGL ES 3+ contexts, and its behavior more closely matches the GLX and WGL context creation code.

Improved the code style consistency of SDL_egl.c.

Fixes bugzilla #2865.
This commit is contained in:
Alex Szpakowski 2015-05-11 21:03:36 -03:00
parent 5919a859b4
commit f1a7c00d06
1 changed files with 87 additions and 72 deletions

121
src/video/SDL_egl.c Normal file → Executable file
View File

@ -31,6 +31,13 @@
#include "SDL_loadso.h" #include "SDL_loadso.h"
#include "SDL_hints.h" #include "SDL_hints.h"
#ifdef EGL_KHR_create_context
/* EGL_OPENGL_ES3_BIT_KHR was added in version 13 of the extension. */
#ifndef EGL_OPENGL_ES3_BIT_KHR
#define EGL_OPENGL_ES3_BIT_KHR 0x00000040
#endif
#endif /* EGL_KHR_create_context */
#if SDL_VIDEO_DRIVER_RPI #if SDL_VIDEO_DRIVER_RPI
/* Raspbian places the OpenGL ES/EGL binaries in a non standard path */ /* Raspbian places the OpenGL ES/EGL binaries in a non standard path */
#define DEFAULT_EGL "/opt/vc/lib/libEGL.so" #define DEFAULT_EGL "/opt/vc/lib/libEGL.so"
@ -92,8 +99,7 @@ static int SDL_EGL_HasExtension(_THIS, const char *ext)
len = 0; len = 0;
ext_word = &exts[i + 1]; ext_word = &exts[i + 1];
} } else {
else {
len++; len++;
} }
} }
@ -194,8 +200,7 @@ SDL_EGL_LoadLibrary(_THIS, const char *egl_path, NativeDisplayType native_displa
if (_this->gl_config.major_version > 1) { if (_this->gl_config.major_version > 1) {
path = DEFAULT_OGL_ES2; path = DEFAULT_OGL_ES2;
egl_dll_handle = SDL_LoadObject(path); egl_dll_handle = SDL_LoadObject(path);
} } else {
else {
path = DEFAULT_OGL_ES; path = DEFAULT_OGL_ES;
egl_dll_handle = SDL_LoadObject(path); egl_dll_handle = SDL_LoadObject(path);
if (egl_dll_handle == NULL) { if (egl_dll_handle == NULL) {
@ -337,14 +342,19 @@ SDL_EGL_ChooseConfig(_THIS)
attribs[i++] = EGL_RENDERABLE_TYPE; attribs[i++] = EGL_RENDERABLE_TYPE;
if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) { if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
if (_this->gl_config.major_version == 2) { #ifdef EGL_KHR_create_context
if (_this->gl_config.major_version >= 3 &&
SDL_EGL_HasExtension(_this, "EGL_KHR_create_context")) {
attribs[i++] = EGL_OPENGL_ES3_BIT_KHR;
} else
#endif
if (_this->gl_config.major_version >= 2) {
attribs[i++] = EGL_OPENGL_ES2_BIT; attribs[i++] = EGL_OPENGL_ES2_BIT;
} else { } else {
attribs[i++] = EGL_OPENGL_ES_BIT; attribs[i++] = EGL_OPENGL_ES_BIT;
} }
_this->egl_data->eglBindAPI(EGL_OPENGL_ES_API); _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
} } else {
else {
attribs[i++] = EGL_OPENGL_BIT; attribs[i++] = EGL_OPENGL_BIT;
_this->egl_data->eglBindAPI(EGL_OPENGL_API); _this->egl_data->eglBindAPI(EGL_OPENGL_API);
} }
@ -387,7 +397,9 @@ SDL_EGL_ChooseConfig(_THIS)
best_bitdiff = bitdiff; best_bitdiff = bitdiff;
} }
if (bitdiff == 0) break; /* we found an exact match! */ if (bitdiff == 0) {
break; /* we found an exact match! */
}
} }
return 0; return 0;
@ -396,17 +408,12 @@ SDL_EGL_ChooseConfig(_THIS)
SDL_GLContext SDL_GLContext
SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface) SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
{ {
EGLint context_attrib_list[] = { /* max 14 values plus terminator. */
EGL_CONTEXT_CLIENT_VERSION, EGLint attribs[15];
1, int attr = 0;
EGL_NONE,
EGL_NONE,
EGL_NONE,
EGL_NONE,
EGL_NONE
};
EGLContext egl_context, share_context = EGL_NO_CONTEXT; EGLContext egl_context, share_context = EGL_NO_CONTEXT;
EGLint profile_mask = _this->gl_config.profile_mask;
if (!_this->egl_data) { if (!_this->egl_data) {
/* The EGL library wasn't loaded, SDL_GetError() should have info */ /* The EGL library wasn't loaded, SDL_GetError() should have info */
@ -417,47 +424,56 @@ SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
share_context = (EGLContext)SDL_GL_GetCurrentContext(); share_context = (EGLContext)SDL_GL_GetCurrentContext();
} }
/* Set the context version and other attributes. */
if (_this->gl_config.major_version < 3 && _this->gl_config.flags == 0 &&
(profile_mask == 0 || profile_mask == SDL_GL_CONTEXT_PROFILE_ES)) {
/* Create a context without using EGL_KHR_create_context attribs. */
if (profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
attribs[attr++] = EGL_CONTEXT_CLIENT_VERSION;
attribs[attr++] = SDL_max(_this->gl_config.major_version, 1);
}
} else {
#ifdef EGL_KHR_create_context
/* The Major/minor version, context profiles, and context flags can
* only be specified when this extension is available.
*/
if (SDL_EGL_HasExtension(_this, "EGL_KHR_create_context")) {
attribs[attr++] = EGL_CONTEXT_MAJOR_VERSION_KHR;
attribs[attr++] = _this->gl_config.major_version;
attribs[attr++] = EGL_CONTEXT_MINOR_VERSION_KHR;
attribs[attr++] = _this->gl_config.minor_version;
/* SDL profile bits match EGL profile bits. */
if (profile_mask != 0 && profile_mask != SDL_GL_CONTEXT_PROFILE_ES) {
attribs[attr++] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
attribs[attr++] = profile_mask;
}
/* SDL flags match EGL flags. */
if (_this->gl_config.flags != 0) {
attribs[attr++] = EGL_CONTEXT_FLAGS_KHR;
attribs[attr++] = _this->gl_config.flags;
}
} else
#endif /* EGL_KHR_create_context */
{
SDL_SetError("Could not create EGL context (context attributes are not supported)");
return NULL;
}
}
attribs[attr++] = EGL_NONE;
/* Bind the API */ /* Bind the API */
if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) { if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
_this->egl_data->eglBindAPI(EGL_OPENGL_ES_API); _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
if (_this->gl_config.major_version) { } else {
context_attrib_list[1] = _this->gl_config.major_version;
}
egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
_this->egl_data->egl_config,
share_context, context_attrib_list);
}
else {
_this->egl_data->eglBindAPI(EGL_OPENGL_API); _this->egl_data->eglBindAPI(EGL_OPENGL_API);
#ifdef EGL_KHR_create_context }
if(SDL_EGL_HasExtension(_this, "EGL_KHR_create_context")) {
context_attrib_list[0] = EGL_CONTEXT_MAJOR_VERSION_KHR;
context_attrib_list[1] = _this->gl_config.major_version;
context_attrib_list[2] = EGL_CONTEXT_MINOR_VERSION_KHR;
context_attrib_list[3] = _this->gl_config.minor_version;
context_attrib_list[4] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
switch(_this->gl_config.profile_mask) {
case SDL_GL_CONTEXT_PROFILE_COMPATIBILITY:
context_attrib_list[5] = EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR;
break;
case SDL_GL_CONTEXT_PROFILE_CORE:
default:
context_attrib_list[5] = EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR;
break;
}
}
else {
context_attrib_list[0] = EGL_NONE;
}
#else /* EGL_KHR_create_context */
context_attrib_list[0] = EGL_NONE;
#endif /* EGL_KHR_create_context */
egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display, egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
_this->egl_data->egl_config, _this->egl_data->egl_config,
share_context, context_attrib_list); share_context, attribs);
}
if (egl_context == EGL_NO_CONTEXT) { if (egl_context == EGL_NO_CONTEXT) {
SDL_SetError("Could not create EGL context"); SDL_SetError("Could not create EGL context");
@ -489,8 +505,7 @@ SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
*/ */
if (!egl_context || !egl_surface) { if (!egl_context || !egl_surface) {
_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
} } else {
else {
if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
egl_surface, egl_surface, egl_context)) { egl_surface, egl_surface, egl_context)) {
return SDL_SetError("Unable to make EGL context current"); return SDL_SetError("Unable to make EGL context current");