Find the best EGL config available between those returned by eglChooseConfig This existed in the old Android Java code, it got lost in the migration to the commong EGL code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
diff --git a/src/video/SDL_egl.c b/src/video/SDL_egl.c
index f9ab8fa..73a6df1 100644
--- a/src/video/SDL_egl.c
+++ b/src/video/SDL_egl.c
@@ -218,7 +218,9 @@ SDL_EGL_ChooseConfig(_THIS)
/* 64 seems nice. */
EGLint attribs[64];
EGLint found_configs = 0;
- int i;
+ /* 128 seems even nicer here */
+ EGLConfig configs[128];
+ int i, j, best_bitdiff = -1, bitdiff, value;
if (!_this->egl_data) {
/* The EGL library wasn't loaded, SDL_GetError() should have info */
@@ -273,12 +275,44 @@ SDL_EGL_ChooseConfig(_THIS)
if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
attribs,
- &_this->egl_data->egl_config, 1,
+ configs, SDL_arraysize(configs),
&found_configs) == EGL_FALSE ||
found_configs == 0) {
return SDL_SetError("Couldn't find matching EGL config");
}
+ /* eglChooseConfig returns a number of configurations that match or exceed the requested attribs. */
+ /* From those, we select the one that matches our requirements more closely via a makeshift algorithm */
+
+ for ( i=0; i<found_configs; i++ ) {
+ bitdiff = 0;
+ for (j = 0; ; j += 2) {
+ if (attribs[j] == EGL_NONE) {
+ break;
+ }
+
+ if ( attribs[j+1] != EGL_DONT_CARE && (
+ attribs[j] == EGL_RED_SIZE ||
+ attribs[j] == EGL_GREEN_SIZE ||
+ attribs[j] == EGL_BLUE_SIZE ||
+ attribs[j] == EGL_ALPHA_SIZE ||
+ attribs[j] == EGL_DEPTH_SIZE ||
+ attribs[j] == EGL_STENCIL_SIZE)) {
+
+ _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], attribs[j], &value);
+ bitdiff += value - attribs[j + 1]; /* value is always >= attrib */
+ }
+ }
+
+ if (bitdiff < best_bitdiff || best_bitdiff == -1) {
+ _this->egl_data->egl_config = configs[i];
+
+ best_bitdiff = bitdiff;
+ }
+
+ if (bitdiff == 0) break; /* we found an exact match! */
+ }
+
return 0;
}