Fix EGLUtils::selectConfigForPixelFormat()

- renderscript now calls EGL directly instead of relying on this function
- surfaceflinger also does its own EGLConfig selection
- selectConfigForPixelFormat stays for legacy reason (many tests use it) but
it now only tries to match the alpha channel of the format rather than the
format itself.

this will allow implementations who don't support the exact formats
defined in the HAL to work properly.

Bug: 4998223

Change-Id: Ic664dfc14d5072a514b6f77a115d1521bfc1578f
diff --git a/driver/rsdGL.cpp b/driver/rsdGL.cpp
index 1f7bb0f..04446ad 100644
--- a/driver/rsdGL.cpp
+++ b/driver/rsdGL.cpp
@@ -99,9 +99,8 @@
 
     for (size_t j = 0; j < sizeof(names) / sizeof(names[0]); j++) {
         EGLint value = -1;
-        EGLint returnVal = eglGetConfigAttrib(dpy, config, names[j].attribute, &value);
-        EGLint error = eglGetError();
-        if (returnVal && error == EGL_SUCCESS) {
+        EGLBoolean returnVal = eglGetConfigAttrib(dpy, config, names[j].attribute, &value);
+        if (returnVal) {
             LOGV(" %s: %d (0x%x)", names[j].name, value, value);
         }
     }
@@ -169,6 +168,24 @@
     configAttribsPtr[1] = EGL_OPENGL_ES2_BIT;
     configAttribsPtr += 2;
 
+    configAttribsPtr[0] = EGL_RED_SIZE;
+    configAttribsPtr[1] = 8;
+    configAttribsPtr += 2;
+
+    configAttribsPtr[0] = EGL_GREEN_SIZE;
+    configAttribsPtr[1] = 8;
+    configAttribsPtr += 2;
+
+    configAttribsPtr[0] = EGL_BLUE_SIZE;
+    configAttribsPtr[1] = 8;
+    configAttribsPtr += 2;
+
+    if (rsc->mUserSurfaceConfig.alphaMin > 0) {
+        configAttribsPtr[0] = EGL_ALPHA_SIZE;
+        configAttribsPtr[1] = rsc->mUserSurfaceConfig.alphaMin;
+        configAttribsPtr += 2;
+    }
+
     if (rsc->mUserSurfaceConfig.depthMin > 0) {
         configAttribsPtr[0] = EGL_DEPTH_SIZE;
         configAttribsPtr[1] = rsc->mUserSurfaceConfig.depthMin;
@@ -191,16 +208,53 @@
     eglInitialize(dc->gl.egl.display, &dc->gl.egl.majorVersion, &dc->gl.egl.minorVersion);
     checkEglError("eglInitialize");
 
-    PixelFormat pf = PIXEL_FORMAT_RGBA_8888;
-    if (rsc->mUserSurfaceConfig.alphaMin == 0) {
-        pf = PIXEL_FORMAT_RGBX_8888;
+    EGLBoolean ret;
+
+    EGLint numConfigs = -1, n = 0;
+    ret = eglChooseConfig(dc->gl.egl.display, configAttribs, 0, 0, &numConfigs);
+    checkEglError("eglGetConfigs", ret);
+
+    if (numConfigs) {
+        EGLConfig* const configs = new EGLConfig[numConfigs];
+
+        ret = eglChooseConfig(dc->gl.egl.display,
+                configAttribs, configs, numConfigs, &n);
+        if (!ret || !n) {
+            checkEglError("eglChooseConfig", ret);
+            LOGE("%p, couldn't find an EGLConfig matching the screen format\n", rsc);
+        }
+
+        // The first config is guaranteed to over-satisfy the constraints
+        dc->gl.egl.config = configs[0];
+
+        // go through the list and skip configs that over-satisfy our needs
+        for (int i=0 ; i<n ; i++) {
+            if (rsc->mUserSurfaceConfig.alphaMin <= 0) {
+                EGLint alphaSize;
+                eglGetConfigAttrib(dc->gl.egl.display,
+                        configs[i], EGL_ALPHA_SIZE, &alphaSize);
+                if (alphaSize > 0) {
+                    continue;
+                }
+            }
+
+            if (rsc->mUserSurfaceConfig.depthMin <= 0) {
+                EGLint depthSize;
+                eglGetConfigAttrib(dc->gl.egl.display,
+                        configs[i], EGL_DEPTH_SIZE, &depthSize);
+                if (depthSize > 0) {
+                    continue;
+                }
+            }
+
+            // Found one!
+            dc->gl.egl.config = configs[i];
+            break;
+        }
+
+        delete [] configs;
     }
 
-    status_t err = EGLUtils::selectConfigForPixelFormat(dc->gl.egl.display, configAttribs,
-                                                        pf, &dc->gl.egl.config);
-    if (err) {
-       LOGE("%p, couldn't find an EGLConfig matching the screen format\n", rsc);
-    }
     //if (props.mLogVisual) {
     if (0) {
         printEGLConfiguration(dc->gl.egl.display, dc->gl.egl.config);
@@ -227,8 +281,8 @@
         return false;
     }
 
-    EGLBoolean ret = eglMakeCurrent(dc->gl.egl.display, dc->gl.egl.surfaceDefault,
-                                    dc->gl.egl.surfaceDefault, dc->gl.egl.context);
+    ret = eglMakeCurrent(dc->gl.egl.display, dc->gl.egl.surfaceDefault,
+                         dc->gl.egl.surfaceDefault, dc->gl.egl.context);
     if (ret == EGL_FALSE) {
         LOGE("eglMakeCurrent returned EGL_FALSE");
         checkEglError("eglMakeCurrent", ret);