[Android] Bug 1827, select the best matching config provided by eglChooseConfig
authorGabriel Jacobo <gabomdq@gmail.com>
Fri, 26 Jul 2013 12:22:40 -0300
changeset 7519b9ef4591d6b5
parent 7518 febc83bc2a43
child 7520 24fd80e8baaf
[Android] Bug 1827, select the best matching config provided by eglChooseConfig
android-project/src/org/libsdl/app/SDLActivity.java
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Thu Jul 25 18:11:09 2013 -0700
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Fri Jul 26 12:22:40 2013 -0300
     1.3 @@ -323,13 +323,47 @@
     1.4                  int[] version = new int[2];
     1.5                  egl.eglInitialize(dpy, version);
     1.6  
     1.7 -                EGLConfig[] configs = new EGLConfig[1];
     1.8 +                EGLConfig[] configs = new EGLConfig[128];
     1.9                  int[] num_config = new int[1];
    1.10                  if (!egl.eglChooseConfig(dpy, attribs, configs, 1, num_config) || num_config[0] == 0) {
    1.11                      Log.e("SDL", "No EGL config available");
    1.12                      return false;
    1.13                  }
    1.14 -                EGLConfig config = configs[0];
    1.15 +                EGLConfig config = null;
    1.16 +                int bestdiff = -1, bitdiff;
    1.17 +                int[] value = new int[1];
    1.18 +                
    1.19 +                // eglChooseConfig returns a number of configurations that match or exceed the requested attribs.
    1.20 +                // From those, we select the one that matches our requirements more closely
    1.21 +                Log.v("SDL", "Got " + num_config[0] + " valid modes from egl");
    1.22 +                for(int i = 0; i < num_config[0]; i++) {
    1.23 +                    bitdiff = 0;
    1.24 +                    // Go through some of the attributes and compute the bit difference between what we want and what we get.
    1.25 +                    for (int j = 0; ; j += 2) {
    1.26 +                        if (attribs[j] == EGL10.EGL_NONE)
    1.27 +                            break;
    1.28 +
    1.29 +                        if (attribs[j] == EGL10.EGL_RED_SIZE ||
    1.30 +                            attribs[j] == EGL10.EGL_GREEN_SIZE ||
    1.31 +                            attribs[j] == EGL10.EGL_BLUE_SIZE ||
    1.32 +                            attribs[j] == EGL10.EGL_ALPHA_SIZE ||
    1.33 +                            attribs[j] == EGL10.EGL_DEPTH_SIZE ||
    1.34 +                            attribs[j] == EGL10.EGL_STENCIL_SIZE) {
    1.35 +                            egl.eglGetConfigAttrib(dpy, configs[i], attribs[j], value);
    1.36 +                            bitdiff += value[0] - attribs[j + 1]; // value is always >= attrib
    1.37 +                        }
    1.38 +                    }
    1.39 +                    
    1.40 +                    if (bitdiff < bestdiff || bestdiff == -1) {
    1.41 +                        config = configs[i];
    1.42 +                        bestdiff = bitdiff;
    1.43 +                    }
    1.44 +                    
    1.45 +                    if (bitdiff == 0) break; // we found an exact match!
    1.46 +                }
    1.47 +                
    1.48 +                Log.d("SDL", "Selected mode with a total bit difference of " + bestdiff);
    1.49 +               
    1.50  
    1.51                  SDLActivity.mEGLDisplay = dpy;
    1.52                  SDLActivity.mEGLConfig = config;