Merge changes 20497,20498,20499

* changes:
  minor code clean-up
  added EGLUtils::strerror
  better error handling
diff --git a/include/ui/EGLUtils.h b/include/ui/EGLUtils.h
index 48777b6..a5bff81 100644
--- a/include/ui/EGLUtils.h
+++ b/include/ui/EGLUtils.h
@@ -31,6 +31,8 @@
 {
 public:
 
+    static const char *strerror(EGLint err);
+
     static status_t selectConfigForPixelFormat(
             EGLDisplay dpy,
             EGLint const* attrs,
diff --git a/libs/surfaceflinger/DisplayHardware/DisplayHardware.cpp b/libs/surfaceflinger/DisplayHardware/DisplayHardware.cpp
index 002a3ab..a479b4c 100644
--- a/libs/surfaceflinger/DisplayHardware/DisplayHardware.cpp
+++ b/libs/surfaceflinger/DisplayHardware/DisplayHardware.cpp
@@ -42,28 +42,6 @@
 
 using namespace android;
 
-static __attribute__((noinline))
-const char *egl_strerror(EGLint err)
-{
-    switch (err){
-        case EGL_SUCCESS:           return "EGL_SUCCESS";
-        case EGL_NOT_INITIALIZED:   return "EGL_NOT_INITIALIZED";
-        case EGL_BAD_ACCESS:        return "EGL_BAD_ACCESS";
-        case EGL_BAD_ALLOC:         return "EGL_BAD_ALLOC";
-        case EGL_BAD_ATTRIBUTE:     return "EGL_BAD_ATTRIBUTE";
-        case EGL_BAD_CONFIG:        return "EGL_BAD_CONFIG";
-        case EGL_BAD_CONTEXT:       return "EGL_BAD_CONTEXT";
-        case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
-        case EGL_BAD_DISPLAY:       return "EGL_BAD_DISPLAY";
-        case EGL_BAD_MATCH:         return "EGL_BAD_MATCH";
-        case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
-        case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
-        case EGL_BAD_PARAMETER:     return "EGL_BAD_PARAMETER";
-        case EGL_BAD_SURFACE:       return "EGL_BAD_SURFACE";
-        case EGL_CONTEXT_LOST:      return "EGL_CONTEXT_LOST";
-        default: return "UNKNOWN";
-    }
-}
 
 static __attribute__((noinline))
 void checkGLErrors()
@@ -80,7 +58,7 @@
     // GLESonGL seems to be returning 0 when there is no errors?
     if (error && error != EGL_SUCCESS)
         LOGE("%s error 0x%04x (%s)",
-                token, int(error), egl_strerror(error));
+                token, int(error), EGLUtils::strerror(error));
 }
 
 
@@ -112,28 +90,22 @@
 
 void DisplayHardware::init(uint32_t dpy)
 {
-    hw_module_t const* module;
-
     mNativeWindow = new FramebufferNativeWindow();
+    framebuffer_device_t const * fbDev = mNativeWindow->getDevice();
 
     mOverlayEngine = NULL;
+    hw_module_t const* module;
     if (hw_get_module(OVERLAY_HARDWARE_MODULE_ID, &module) == 0) {
         overlay_control_open(module, &mOverlayEngine);
     }
 
-    framebuffer_device_t const * fbDev = mNativeWindow->getDevice();
-
-    PixelFormatInfo fbFormatInfo;
-    getPixelFormatInfo(PixelFormat(fbDev->format), &fbFormatInfo);
-
     // initialize EGL
     const EGLint attribs[] = {
-            EGL_BUFFER_SIZE,    fbFormatInfo.bitsPerPixel,
-            EGL_DEPTH_SIZE,     0,
+            EGL_SURFACE_TYPE,   EGL_WINDOW_BIT,
             EGL_NONE
     };
     EGLint w, h, dummy;
-    EGLint numConfigs=0, n=0;
+    EGLint numConfigs=0;
     EGLSurface surface;
     EGLContext context;
     mFlags = 0;
@@ -146,10 +118,16 @@
     eglGetConfigs(display, NULL, 0, &numConfigs);
 
     EGLConfig config;
-    status_t err = EGLUtils::selectConfigForPixelFormat(
-            display, attribs, fbDev->format, &config);
+    status_t err = EGLUtils::selectConfigForNativeWindow(
+            display, attribs, mNativeWindow.get(), &config);
     LOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
     
+    EGLint r,g,b,a;
+    eglGetConfigAttrib(display, config, EGL_RED_SIZE,   &r);
+    eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
+    eglGetConfigAttrib(display, config, EGL_BLUE_SIZE,  &b);
+    eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
+
     /*
      * Gather EGL extensions
      */
@@ -163,7 +141,8 @@
     LOGI("version   : %s", eglQueryString(display, EGL_VERSION));
     LOGI("extensions: %s", egl_extensions);
     LOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
-
+    LOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
+    
 
     if (mNativeWindow->isUpdateOnDemand()) {
         mFlags |= UPDATE_ON_DEMAND;
diff --git a/libs/ui/EGLUtils.cpp b/libs/ui/EGLUtils.cpp
index 80bfdfd..1663313 100644
--- a/libs/ui/EGLUtils.cpp
+++ b/libs/ui/EGLUtils.cpp
@@ -17,6 +17,7 @@
 
 #define LOG_TAG "EGLUtils"
 
+#include <cutils/log.h>
 #include <utils/Errors.h>
 
 #include <ui/EGLUtils.h>
@@ -29,6 +30,28 @@
 namespace android {
 // ----------------------------------------------------------------------------
 
+const char *EGLUtils::strerror(EGLint err)
+{
+    switch (err){
+        case EGL_SUCCESS:           return "EGL_SUCCESS";
+        case EGL_NOT_INITIALIZED:   return "EGL_NOT_INITIALIZED";
+        case EGL_BAD_ACCESS:        return "EGL_BAD_ACCESS";
+        case EGL_BAD_ALLOC:         return "EGL_BAD_ALLOC";
+        case EGL_BAD_ATTRIBUTE:     return "EGL_BAD_ATTRIBUTE";
+        case EGL_BAD_CONFIG:        return "EGL_BAD_CONFIG";
+        case EGL_BAD_CONTEXT:       return "EGL_BAD_CONTEXT";
+        case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
+        case EGL_BAD_DISPLAY:       return "EGL_BAD_DISPLAY";
+        case EGL_BAD_MATCH:         return "EGL_BAD_MATCH";
+        case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
+        case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
+        case EGL_BAD_PARAMETER:     return "EGL_BAD_PARAMETER";
+        case EGL_BAD_SURFACE:       return "EGL_BAD_SURFACE";
+        case EGL_CONTEXT_LOST:      return "EGL_CONTEXT_LOST";
+        default: return "UNKNOWN";
+    }
+}
+
 status_t EGLUtils::selectConfigForPixelFormat(
         EGLDisplay dpy,
         EGLint const* attrs,
@@ -37,6 +60,9 @@
 {
     EGLint numConfigs = -1, n=0;
 
+    if (!attrs)
+        return BAD_VALUE;
+
     if (outConfig == NULL)
         return BAD_VALUE;
     
@@ -65,12 +91,13 @@
     EGLConfig config = NULL;
     for (i=0 ; i<n ; i++) {
         EGLint r,g,b,a;
-        eglGetConfigAttrib(dpy, configs[i], EGL_RED_SIZE,   &r);
-        eglGetConfigAttrib(dpy, configs[i], EGL_GREEN_SIZE, &g);
-        eglGetConfigAttrib(dpy, configs[i], EGL_BLUE_SIZE,  &b);
-        eglGetConfigAttrib(dpy, configs[i], EGL_ALPHA_SIZE, &a);
+        EGLConfig curr = configs[i];
+        eglGetConfigAttrib(dpy, curr, EGL_RED_SIZE,   &r);
+        eglGetConfigAttrib(dpy, curr, EGL_GREEN_SIZE, &g);
+        eglGetConfigAttrib(dpy, curr, EGL_BLUE_SIZE,  &b);
+        eglGetConfigAttrib(dpy, curr, EGL_ALPHA_SIZE, &a);
         if (fbSzA == a && fbSzR == r && fbSzG == g && fbSzB  == b) {
-            config = configs[i];
+            config = curr;
             break;
         }
     }
@@ -93,6 +120,10 @@
 {
     int err;
     int format;
+    
+    if (!window)
+        return BAD_VALUE;
+    
     if ((err = window->query(window, NATIVE_WINDOW_FORMAT, &format)) < 0) {
         return err;
     }
diff --git a/libs/ui/FramebufferNativeWindow.cpp b/libs/ui/FramebufferNativeWindow.cpp
index 7b85c7f..f6c666d 100644
--- a/libs/ui/FramebufferNativeWindow.cpp
+++ b/libs/ui/FramebufferNativeWindow.cpp
@@ -25,6 +25,7 @@
 #include <cutils/log.h>
 #include <cutils/atomic.h>
 #include <utils/threads.h>
+#include <utils/RefBase.h>
 
 #include <ui/SurfaceComposerClient.h>
 #include <ui/Rect.h>
@@ -81,10 +82,16 @@
     hw_module_t const* module;
     if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
         int stride;
-        framebuffer_open(module, &fbDev);
-        gralloc_open(module, &grDev);
         int err;
+        err = framebuffer_open(module, &fbDev);
+        LOGE_IF(err, "couldn't open framebuffer HAL (%s)", strerror(-err));
+        
+        err = gralloc_open(module, &grDev);
+        LOGE_IF(err, "couldn't open gralloc HAL (%s)", strerror(-err));
 
+        // bail out if we can't initialize the modules
+        if (!fbDev || !grDev)
+            return;
         
         mUpdateOnDemand = (fbDev->setUpdateRect != 0);
         
@@ -127,11 +134,19 @@
     android_native_window_t::query = query;
 }
 
-FramebufferNativeWindow::~FramebufferNativeWindow() {
-    grDev->free(grDev, buffers[0]->handle);
-    grDev->free(grDev, buffers[1]->handle);
-    gralloc_close(grDev);
-    framebuffer_close(fbDev);
+FramebufferNativeWindow::~FramebufferNativeWindow() 
+{
+    if (grDev) {
+        if (buffers[0] != NULL)
+            grDev->free(grDev, buffers[0]->handle);
+        if (buffers[1] != NULL)
+            grDev->free(grDev, buffers[1]->handle);
+        gralloc_close(grDev);
+    }
+
+    if (fbDev) {
+        framebuffer_close(fbDev);
+    }
 }
 
 status_t FramebufferNativeWindow::setUpdateRectangle(const Rect& r) 
@@ -216,6 +231,7 @@
             *value = fb->format;
             return NO_ERROR;
     }
+    *value = 0;
     return BAD_VALUE;
 }
 
@@ -223,9 +239,16 @@
 }; // namespace android
 // ----------------------------------------------------------------------------
 
+using namespace android;
 
 EGLNativeWindowType android_createDisplaySurface(void)
 {
-    return new android::FramebufferNativeWindow();
+    FramebufferNativeWindow* w;
+    w = new FramebufferNativeWindow();
+    if (w->getDevice() == NULL) {
+        // get a ref so it can be destroyed when we exit this block
+        sp<FramebufferNativeWindow> ref(w);
+        return NULL;
+    }
+    return (EGLNativeWindowType)w;
 }
-
diff --git a/opengl/tests/swapinterval/swapinterval.cpp b/opengl/tests/swapinterval/swapinterval.cpp
index cf908a0..80a6c21 100644
--- a/opengl/tests/swapinterval/swapinterval.cpp
+++ b/opengl/tests/swapinterval/swapinterval.cpp
@@ -1,21 +1,19 @@
 /*
-**
-** Copyright 2006, The Android Open Source Project
-**
-** Licensed under the Apache License, Version 2.0 (the "License"); 
-** you may not use this file except in compliance with the License. 
-** You may obtain a copy of the License at 
-**
-**     http://www.apache.org/licenses/LICENSE-2.0 
-**
-** Unless required by applicable law or agreed to in writing, software 
-** distributed under the License is distributed on an "AS IS" BASIS, 
-** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
-** See the License for the specific language governing permissions and 
-** limitations under the License.
-*/
-
-#define LOG_TAG "fillrate"
+ **
+ ** Copyright 2006, The Android Open Source Project
+ **
+ ** Licensed under the Apache License, Version 2.0 (the "License"); 
+ ** you may not use this file except in compliance with the License. 
+ ** You may obtain a copy of the License at 
+ **
+ **     http://www.apache.org/licenses/LICENSE-2.0 
+ **
+ ** Unless required by applicable law or agreed to in writing, software 
+ ** distributed under the License is distributed on an "AS IS" BASIS, 
+ ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ ** See the License for the specific language governing permissions and 
+ ** limitations under the License.
+ */
 
 #include <stdlib.h>
 #include <stdio.h>
@@ -33,72 +31,90 @@
 int main(int argc, char** argv)
 {
     EGLint configAttribs[] = {
-         EGL_DEPTH_SIZE, 0,
-         EGL_NONE
-     };
-     
-     EGLint majorVersion;
-     EGLint minorVersion;
-     EGLContext context;
-     EGLConfig config;
-     EGLSurface surface;
-     EGLint w, h;
-     EGLDisplay dpy;
+            EGL_SURFACE_TYPE,   EGL_WINDOW_BIT,
+            EGL_NONE
+    };
 
-     dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
-     eglInitialize(dpy, &majorVersion, &minorVersion);
-          
-     EGLNativeWindowType window = android_createDisplaySurface();
-     
-     status_t err = EGLUtils::selectConfigForNativeWindow(
-             dpy, configAttribs, window, &config);
-     if (err) {
-         fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n");
-         return 0;
-     }
+    EGLint majorVersion;
+    EGLint minorVersion;
+    EGLContext context;
+    EGLConfig config;
+    EGLint numConfigs=0;
+    EGLSurface surface;
+    EGLint w, h;
+    EGLDisplay dpy;
 
-     surface = eglCreateWindowSurface(dpy, config, window, NULL);
-     context = eglCreateContext(dpy, config, NULL, NULL);
-     eglMakeCurrent(dpy, surface, surface, context);   
-     eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
-     eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
-     
-     printf("w=%d, h=%d\n", w, h);
-     
-     glDisable(GL_DITHER);
-     glEnable(GL_BLEND);
+    dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
+    eglInitialize(dpy, 0 ,0) ;//&majorVersion, &minorVersion);
+    eglGetConfigs(dpy, NULL, 0, &numConfigs);
+    printf("# configs = %d\n", numConfigs);
+    
+    EGLNativeWindowType window = android_createDisplaySurface();
 
-     glViewport(0, 0, w, h);
-     glOrthof(0, w, 0, h, 0, 1);
+    status_t err = EGLUtils::selectConfigForNativeWindow(
+            dpy, configAttribs, window, &config);
+    if (err) {
+        fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n");
+        return 0;
+    }
 
-     eglSwapInterval(dpy, 1);
+    EGLint r,g,b,a;
+    eglGetConfigAttrib(dpy, config, EGL_RED_SIZE,   &r);
+    eglGetConfigAttrib(dpy, config, EGL_GREEN_SIZE, &g);
+    eglGetConfigAttrib(dpy, config, EGL_BLUE_SIZE,  &b);
+    eglGetConfigAttrib(dpy, config, EGL_ALPHA_SIZE, &a);
 
-     glClearColor(1,0,0,0);
-     glClear(GL_COLOR_BUFFER_BIT);
-     eglSwapBuffers(dpy, surface);
+    surface = eglCreateWindowSurface(dpy, config, window, NULL);
+    if (surface == EGL_NO_SURFACE) {
+        EGLint err = eglGetError();
+        fprintf(stderr, "%s, config=%p, format = %d-%d-%d-%d\n",
+                EGLUtils::strerror(err), config, r,g,b,a);
+        return 0;
+    } else {
+        printf("config=%p, format = %d-%d-%d-%d\n", config, r,g,b,a);
+    }
+
+    context = eglCreateContext(dpy, config, NULL, NULL);
+    eglMakeCurrent(dpy, surface, surface, context);   
+    eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
+    eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
+
+    printf("w=%d, h=%d\n", w, h);
+
+    glDisable(GL_DITHER);
+    glEnable(GL_BLEND);
+
+    glViewport(0, 0, w, h);
+    glOrthof(0, w, 0, h, 0, 1);
+
+    eglSwapInterval(dpy, 1);
+
+    glClearColor(1,0,0,0);
+    glClear(GL_COLOR_BUFFER_BIT);
+    eglSwapBuffers(dpy, surface);
 
 
-     int time = 10;
-     printf("screen should flash red/green quickly for %d s...\n", time);
-     
-     int c = 0;
-     nsecs_t start = systemTime();
-     nsecs_t t;
-     do {
-         glClearColor(1,0,0,0);
-         glClear(GL_COLOR_BUFFER_BIT);
-         eglSwapBuffers(dpy, surface);
-         glClearColor(0,1,0,0);
-         glClear(GL_COLOR_BUFFER_BIT);
-         eglSwapBuffers(dpy, surface);
-         t = systemTime() - start;
-         c += 2;
-     } while (int(ns2s(t))<=time);
-     
-     double p =  (double(t) / c) / 1000000000.0;
-     printf("refresh-rate is %f fps (%f ms)\n", 1.0f/p, p*1000.0);
-       
-     eglTerminate(dpy);
-     
-     return 0;
+    int time = 10;
+    printf("screen should flash red/green quickly for %d s...\n", time);
+
+    int c = 0;
+    nsecs_t start = systemTime();
+    nsecs_t t;
+    do {
+        glClearColor(1,0,0,0);
+        glClear(GL_COLOR_BUFFER_BIT);
+        eglSwapBuffers(dpy, surface);
+        glClearColor(0,1,0,0);
+        glClear(GL_COLOR_BUFFER_BIT);
+        eglSwapBuffers(dpy, surface);
+        t = systemTime() - start;
+        c += 2;
+    } while (int(ns2s(t))<=time);
+
+    double p =  (double(t) / c) / 1000000000.0;
+    printf("refresh-rate is %f fps (%f ms)\n", 1.0f/p, p*1000.0);
+
+    eglTerminate(dpy);
+
+    return 0;
 }