Find the best EGL config available between those returned by eglChooseConfig
authorGabriel Jacobo <gabomdq@gmail.com>
Tue, 19 Nov 2013 11:04:05 -0300
changeset 8015ba26f042e36d
parent 8014 0a4b6dec7de5
child 8016 3746af784462
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.
src/video/SDL_egl.c
     1.1 --- a/src/video/SDL_egl.c	Tue Nov 19 10:56:38 2013 -0300
     1.2 +++ b/src/video/SDL_egl.c	Tue Nov 19 11:04:05 2013 -0300
     1.3 @@ -218,7 +218,9 @@
     1.4      /* 64 seems nice. */
     1.5      EGLint attribs[64];
     1.6      EGLint found_configs = 0;
     1.7 -    int i;
     1.8 +    /* 128 seems even nicer here */
     1.9 +    EGLConfig configs[128];
    1.10 +    int i, j, best_bitdiff = -1, bitdiff, value;
    1.11      
    1.12      if (!_this->egl_data) {
    1.13          /* The EGL library wasn't loaded, SDL_GetError() should have info */
    1.14 @@ -273,12 +275,44 @@
    1.15      
    1.16      if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
    1.17          attribs,
    1.18 -        &_this->egl_data->egl_config, 1,
    1.19 +        configs, SDL_arraysize(configs),
    1.20          &found_configs) == EGL_FALSE ||
    1.21          found_configs == 0) {
    1.22          return SDL_SetError("Couldn't find matching EGL config");
    1.23      }
    1.24      
    1.25 +    /* eglChooseConfig returns a number of configurations that match or exceed the requested attribs. */
    1.26 +    /* From those, we select the one that matches our requirements more closely via a makeshift algorithm */
    1.27 +    
    1.28 +    for ( i=0; i<found_configs; i++ ) {
    1.29 +        bitdiff = 0;
    1.30 +        for (j = 0; ; j += 2) {
    1.31 +            if (attribs[j] == EGL_NONE) {
    1.32 +               break;
    1.33 +            }
    1.34 +            
    1.35 +            if ( attribs[j+1] != EGL_DONT_CARE && (
    1.36 +                attribs[j] == EGL_RED_SIZE ||
    1.37 +                attribs[j] == EGL_GREEN_SIZE ||
    1.38 +                attribs[j] == EGL_BLUE_SIZE ||
    1.39 +                attribs[j] == EGL_ALPHA_SIZE ||
    1.40 +                attribs[j] == EGL_DEPTH_SIZE ||
    1.41 +                attribs[j] == EGL_STENCIL_SIZE)) {
    1.42 +                
    1.43 +                _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], attribs[j], &value);
    1.44 +                bitdiff += value - attribs[j + 1]; /* value is always >= attrib */
    1.45 +            }
    1.46 +        }
    1.47 +
    1.48 +        if (bitdiff < best_bitdiff || best_bitdiff == -1) {
    1.49 +            _this->egl_data->egl_config = configs[i];
    1.50 +            
    1.51 +            best_bitdiff = bitdiff;
    1.52 +        }
    1.53 +           
    1.54 +        if (bitdiff == 0) break; /* we found an exact match! */
    1.55 +    }
    1.56 +    
    1.57      return 0;
    1.58  }
    1.59