am f50b9eaa: Binder: Make sure binder objects do not overlap

* commit 'f50b9eaaeeaeae16981f11b05d3f3a6fb0dea30d':
  Binder: Make sure binder objects do not overlap
diff --git a/cmds/installd/commands.c b/cmds/installd/commands.c
index 84ad204..e9d6b15 100644
--- a/cmds/installd/commands.c
+++ b/cmds/installd/commands.c
@@ -184,7 +184,7 @@
     return delete_dir_contents(pkgdir, 0, "lib");
 }
 
-int make_user_data(const char *pkgname, uid_t uid, userid_t userid)
+int make_user_data(const char *pkgname, uid_t uid, userid_t userid, const char* seinfo)
 {
     char pkgdir[PKG_PATH_MAX];
     char applibdir[PKG_PATH_MAX];
@@ -245,7 +245,7 @@
         return -1;
     }
 
-    if (selinux_android_setfilecon(pkgdir, pkgname, uid) < 0) {
+    if (selinux_android_setfilecon2(pkgdir, pkgname, seinfo, uid) < 0) {
         ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
         unlink(libsymlink);
         unlink(pkgdir);
diff --git a/cmds/installd/installd.c b/cmds/installd/installd.c
index 1904408..0c80dac 100644
--- a/cmds/installd/installd.c
+++ b/cmds/installd/installd.c
@@ -16,6 +16,8 @@
 
 #include <sys/capability.h>
 #include <linux/prctl.h>
+#include <selinux/android.h>
+#include <selinux/avc.h>
 
 #include "installd.h"
 
@@ -103,7 +105,8 @@
 
 static int do_mk_user_data(char **arg, char reply[REPLY_MAX])
 {
-    return make_user_data(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, userid */
+    return make_user_data(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3]);
+                             /* pkgname, uid, userid, seinfo */
 }
 
 static int do_rm_user(char **arg, char reply[REPLY_MAX])
@@ -142,7 +145,7 @@
     { "rmuserdata",           2, do_rm_user_data },
     { "movefiles",            0, do_movefiles },
     { "linklib",              3, do_linklib },
-    { "mkuserdata",           3, do_mk_user_data },
+    { "mkuserdata",           4, do_mk_user_data },
     { "rmuser",               1, do_rm_user },
 };
 
@@ -389,6 +392,10 @@
             goto fail;
         }
 
+        if (selinux_android_restorecon(android_media_dir.path)) {
+            goto fail;
+        }
+
         // /data/media/0
         char owner_media_dir[PATH_MAX];
         snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path);
@@ -525,6 +532,7 @@
     struct sockaddr addr;
     socklen_t alen;
     int lsocket, s, count;
+    int selinux_enabled = (is_selinux_enabled() > 0);
 
     ALOGI("installd firing up\n");
 
@@ -538,6 +546,11 @@
         exit(1);
     }
 
+    if (selinux_enabled && selinux_status_open(true) < 0) {
+        ALOGE("Could not open selinux status; exiting.\n");
+        exit(1);
+    }
+
     drop_privileges();
 
     lsocket = android_get_control_socket(SOCKET_PATH);
@@ -576,6 +589,9 @@
                 break;
             }
             buf[count] = 0;
+            if (selinux_enabled && selinux_status_updated() > 0) {
+                selinux_android_seapp_context_reload();
+            }
             if (execute(s, buf)) break;
         }
         ALOGI("closing connection\n");
diff --git a/cmds/installd/installd.h b/cmds/installd/installd.h
index 635b07c..9ca2f86 100644
--- a/cmds/installd/installd.h
+++ b/cmds/installd/installd.h
@@ -194,7 +194,7 @@
 int renamepkg(const char *oldpkgname, const char *newpkgname);
 int fix_uid(const char *pkgname, uid_t uid, gid_t gid);
 int delete_user_data(const char *pkgname, userid_t userid);
-int make_user_data(const char *pkgname, uid_t uid, userid_t userid);
+int make_user_data(const char *pkgname, uid_t uid, userid_t userid, const char* seinfo);
 int delete_user(userid_t userid);
 int delete_cache(const char *pkgname, userid_t userid);
 int move_dex(const char *src, const char *dst);
diff --git a/cmds/screenshot/Android.mk b/cmds/screenshot/Android.mk
index 0afb2c5..1ee7807 100644
--- a/cmds/screenshot/Android.mk
+++ b/cmds/screenshot/Android.mk
@@ -7,6 +7,6 @@
 
 LOCAL_SHARED_LIBRARIES := libcutils libz liblog
 LOCAL_STATIC_LIBRARIES := libpng
-LOCAL_C_INCLUDES += external/zlib
+LOCAL_C_INCLUDES += external/zlib external/libpng
 
 include $(BUILD_EXECUTABLE)
diff --git a/cmds/screenshot/screenshot.c b/cmds/screenshot/screenshot.c
index cca80c3..be1ecd4 100644
--- a/cmds/screenshot/screenshot.c
+++ b/cmds/screenshot/screenshot.c
@@ -8,7 +8,7 @@
 #include <linux/fb.h>
 
 #include <zlib.h>
-#include <libpng/png.h>
+#include <png.h>
 
 #include "private/android_filesystem_config.h"
 
diff --git a/include/android/looper.h b/include/android/looper.h
index 24e3967..74c0383 100644
--- a/include/android/looper.h
+++ b/include/android/looper.h
@@ -253,4 +253,4 @@
 };
 #endif
 
-#endif // ANDROID_NATIVE_WINDOW_H
+#endif // ANDROID_LOOPER_H
diff --git a/libs/binder/MemoryHeapBase.cpp b/libs/binder/MemoryHeapBase.cpp
index d1cbf1c..43a01e4 100644
--- a/libs/binder/MemoryHeapBase.cpp
+++ b/libs/binder/MemoryHeapBase.cpp
@@ -31,11 +31,6 @@
 
 #include <binder/MemoryHeapBase.h>
 
-#ifdef HAVE_ANDROID_OS
-#include <linux/android_pmem.h>
-#endif
-
-
 namespace android {
 
 // ---------------------------------------------------------------------------
@@ -108,18 +103,9 @@
 {
     if (size == 0) {
         // try to figure out the size automatically
-#ifdef HAVE_ANDROID_OS
-        // first try the PMEM ioctl
-        pmem_region reg;
-        int err = ioctl(fd, PMEM_GET_TOTAL_SIZE, &reg);
-        if (err == 0)
-            size = reg.len;
-#endif
-        if (size == 0) { // try fstat
-            struct stat sb;
-            if (fstat(fd, &sb) == 0)
-                size = sb.st_size;
-        }
+        struct stat sb;
+        if (fstat(fd, &sb) == 0)
+            size = sb.st_size;
         // if it didn't work, let mmap() fail.
     }
 
diff --git a/libs/gui/IGraphicBufferProducer.cpp b/libs/gui/IGraphicBufferProducer.cpp
index fc86e60..0f461e5 100644
--- a/libs/gui/IGraphicBufferProducer.cpp
+++ b/libs/gui/IGraphicBufferProducer.cpp
@@ -60,7 +60,11 @@
         bool nonNull = reply.readInt32();
         if (nonNull) {
             *buf = new GraphicBuffer();
-            reply.read(**buf);
+            result = reply.read(**buf);
+            if(result != NO_ERROR) {
+                (*buf).clear();
+                return result;
+            }
         }
         result = reply.readInt32();
         return result;
diff --git a/libs/gui/tests/SurfaceTexture_test.cpp b/libs/gui/tests/SurfaceTexture_test.cpp
index 05b0b67..e4fba15 100644
--- a/libs/gui/tests/SurfaceTexture_test.cpp
+++ b/libs/gui/tests/SurfaceTexture_test.cpp
@@ -35,7 +35,6 @@
 #include <GLES2/gl2ext.h>
 
 #include <ui/FramebufferNativeWindow.h>
-#include <utils/UniquePtr.h>
 #include <android/native_window.h>
 
 namespace android {
diff --git a/libs/ui/GraphicBuffer.cpp b/libs/ui/GraphicBuffer.cpp
index 0ecd3d9..96a7188 100644
--- a/libs/ui/GraphicBuffer.cpp
+++ b/libs/ui/GraphicBuffer.cpp
@@ -282,6 +282,8 @@
     if (handle != 0) {
         status_t err = mBufferMapper.registerBuffer(handle);
         if (err != NO_ERROR) {
+            width = height = stride = format = usage = 0;
+            handle = NULL;
             ALOGE("unflatten: registerBuffer failed: %s (%d)",
                     strerror(-err), err);
             return err;
diff --git a/opengl/libagl/Android.mk b/opengl/libagl/Android.mk
index 9886bf0..95a8ef2 100644
--- a/opengl/libagl/Android.mk
+++ b/opengl/libagl/Android.mk
@@ -44,7 +44,7 @@
 # we need to access the private Bionic header <bionic_tls.h>
 LOCAL_C_INCLUDES += bionic/libc/private
 
-LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/egl
+LOCAL_MODULE_RELATIVE_PATH := egl
 LOCAL_MODULE:= libGLES_android
 
 include $(BUILD_SHARED_LIBRARY)
diff --git a/opengl/libagl/Tokenizer.cpp b/opengl/libagl/Tokenizer.cpp
index eac8d6d..ac0a48c 100644
--- a/opengl/libagl/Tokenizer.cpp
+++ b/opengl/libagl/Tokenizer.cpp
@@ -163,9 +163,9 @@
 {
     const run_t* ranges = mRanges.array();
     const size_t c = mRanges.size();
-    ALOGD("Tokenizer (%p, size = %u)\n", this, c);
+    ALOGD("Tokenizer (%p, size = %zu)\n", this, c);
     for (size_t i=0 ; i<c ; i++) {
-        ALOGD("%u: (%u, %u)\n", i, ranges[i].first, ranges[i].length);
+        ALOGD("%zu: (%u, %u)\n", i, ranges[i].first, ranges[i].length);
     }
 }
 
diff --git a/opengl/libagl/context.h b/opengl/libagl/context.h
index 7065a30..c599a55 100644
--- a/opengl/libagl/context.h
+++ b/opengl/libagl/context.h
@@ -147,7 +147,11 @@
 
     vec4_t          color;
     vec4_t          texture[GGL_TEXTURE_UNIT_COUNT];
+#ifdef __LP64__
+    uint32_t        reserved1[2];
+#else
     uint32_t        reserved1[4];
+#endif
 
     inline void clear() {
         flags = index = locked = mru = 0;
@@ -578,10 +582,10 @@
 #ifdef HAVE_ANDROID_OS
     // We have a dedicated TLS slot in bionic
     inline void setGlThreadSpecific(ogles_context_t *value) {
-        ((uint32_t *)__get_tls())[TLS_SLOT_OPENGL] = (uint32_t)value;
+        __get_tls()[TLS_SLOT_OPENGL] = value;
     }
     inline ogles_context_t* getGlThreadSpecific() {
-        return (ogles_context_t *)(((unsigned *)__get_tls())[TLS_SLOT_OPENGL]);
+        return static_cast<ogles_context_t*>(__get_tls()[TLS_SLOT_OPENGL]);
     }
 #else
     extern pthread_key_t gGLKey;
diff --git a/opengl/libagl/egl.cpp b/opengl/libagl/egl.cpp
index bbbda76..f925e7d 100644
--- a/opengl/libagl/egl.cpp
+++ b/opengl/libagl/egl.cpp
@@ -78,20 +78,20 @@
             pthread_key_create(&gEGLErrorKey, NULL);
         pthread_mutex_unlock(&gErrorKeyMutex);
     }
-    pthread_setspecific(gEGLErrorKey, (void*)error);
+    pthread_setspecific(gEGLErrorKey, (void*)(uintptr_t)error);
     return returnValue;
 }
 
 static GLint getError() {
     if (ggl_unlikely(gEGLErrorKey == -1))
         return EGL_SUCCESS;
-    GLint error = (GLint)pthread_getspecific(gEGLErrorKey);
+    GLint error = (GLint)(uintptr_t)pthread_getspecific(gEGLErrorKey);
     if (error == 0) {
         // The TLS key has been created by another thread, but the value for
         // this thread has not been initialized.
         return EGL_SUCCESS;
     }
-    pthread_setspecific(gEGLErrorKey, (void*)EGL_SUCCESS);
+    pthread_setspecific(gEGLErrorKey, (void*)(uintptr_t)EGL_SUCCESS);
     return error;
 }
 
@@ -1201,7 +1201,7 @@
         EGLint attribute, EGLint *value)
 {
     size_t numConfigs =  NELEM(gConfigs);
-    int index = (int)config;
+    int index = (int)(uintptr_t)config;
     if (uint32_t(index) >= numConfigs)
         return setError(EGL_BAD_CONFIG, EGL_FALSE);
 
@@ -1448,7 +1448,7 @@
     }
     GLint i;
     for (i=0 ; i<numConfigs && i<config_size ; i++) {
-        *configs++ = (EGLConfig)i;
+        *configs++ = (EGLConfig)(uintptr_t)i;
     }
     *num_config = i;
     return EGL_TRUE;
@@ -1519,7 +1519,7 @@
         if (configs) {
             for (int i=0 ; config_size && i<numConfigs ; i++) {
                 if (possibleMatch & (1<<i)) {
-                    *configs++ = (EGLConfig)i;
+                    *configs++ = (EGLConfig)(uintptr_t)i;
                     config_size--;
                     n++;
                 }
diff --git a/opengl/libs/EGL/egl.cpp b/opengl/libs/EGL/egl.cpp
index f759e6b..67fbae5 100644
--- a/opengl/libs/EGL/egl.cpp
+++ b/opengl/libs/EGL/egl.cpp
@@ -27,7 +27,6 @@
 #include <cutils/log.h>
 #include <cutils/atomic.h>
 #include <cutils/properties.h>
-#include <cutils/memory.h>
 
 #include <utils/CallStack.h>
 #include <utils/String8.h>
@@ -42,6 +41,8 @@
 #include "egl_display.h"
 #include "egl_object.h"
 
+typedef __eglMustCastToProperFunctionPointerType EGLFuncPointer;
+
 // ----------------------------------------------------------------------------
 namespace android {
 // ----------------------------------------------------------------------------
@@ -234,11 +235,11 @@
     pthread_key_create(&gGLTraceKey, NULL);
     initEglTraceLevel();
 #endif
-    uint32_t addr = (uint32_t)((void*)gl_no_context);
-    android_memset32(
-            (uint32_t*)(void*)&gHooksNoContext,
-            addr,
-            sizeof(gHooksNoContext));
+    int numHooks = sizeof(gHooksNoContext) / sizeof(EGLFuncPointer);
+    EGLFuncPointer *iter = reinterpret_cast<EGLFuncPointer*>(&gHooksNoContext);
+    for (int hook = 0; hook < numHooks; ++hook) {
+        *(iter++) = reinterpret_cast<EGLFuncPointer>(gl_no_context);
+    }
 
     setGLHooksThreadSpecific(&gHooksNoContext);
 }
diff --git a/opengl/libs/EGL/eglApi.cpp b/opengl/libs/EGL/eglApi.cpp
index 0cc5265..d96b54f 100644
--- a/opengl/libs/EGL/eglApi.cpp
+++ b/opengl/libs/EGL/eglApi.cpp
@@ -204,7 +204,7 @@
 {
     clearError();
 
-    uint32_t index = uint32_t(display);
+    uintptr_t index = reinterpret_cast<uintptr_t>(display);
     if (index >= NUM_DISPLAYS) {
         return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
     }
diff --git a/opengl/tools/glgen/src/JniCodeEmitter.java b/opengl/tools/glgen/src/JniCodeEmitter.java
index d5e2d34..b1bd1fd 100644
--- a/opengl/tools/glgen/src/JniCodeEmitter.java
+++ b/opengl/tools/glgen/src/JniCodeEmitter.java
@@ -1073,6 +1073,7 @@
                 String decl = type.getDeclaration();
                 needsExit = true;
                 out.println(indent + "if (!" + cname + ") {");
+                out.println(indent + indent + "_exception = 1;");
                 out.println(indent + indent +
                             "_exceptionType = \"java/lang/IllegalArgumentException\";");
                 out.println(indent + indent +
diff --git a/services/surfaceflinger/DisplayDevice.cpp b/services/surfaceflinger/DisplayDevice.cpp
index 800137b..0d0f98d 100644
--- a/services/surfaceflinger/DisplayDevice.cpp
+++ b/services/surfaceflinger/DisplayDevice.cpp
@@ -53,6 +53,7 @@
         const sp<SurfaceFlinger>& flinger,
         DisplayType type,
         int32_t hwcId,
+        int format,
         bool isSecure,
         const wp<IBinder>& displayToken,
         const sp<DisplaySurface>& displaySurface,
@@ -76,9 +77,6 @@
     mNativeWindow = new Surface(producer, false);
     ANativeWindow* const window = mNativeWindow.get();
 
-    int format;
-    window->query(window, NATIVE_WINDOW_FORMAT, &format);
-
     // Make sure that composition can never be stalled by a virtual display
     // consumer that isn't processing buffers fast enough. We have to do this
     // in two places:
@@ -96,6 +94,9 @@
     EGLSurface surface;
     EGLint w, h;
     EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
+    if (config == EGL_NO_CONFIG) {
+        config = RenderEngine::chooseEglConfig(display, format);
+    }
     surface = eglCreateWindowSurface(display, config, window, NULL);
     eglQuerySurface(display, surface, EGL_WIDTH,  &mDisplayWidth);
     eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
diff --git a/services/surfaceflinger/DisplayDevice.h b/services/surfaceflinger/DisplayDevice.h
index c3abe89..01a9d2e 100644
--- a/services/surfaceflinger/DisplayDevice.h
+++ b/services/surfaceflinger/DisplayDevice.h
@@ -75,6 +75,7 @@
             const sp<SurfaceFlinger>& flinger,
             DisplayType type,
             int32_t hwcId,  // negative for non-HWC-composited displays
+            int format,
             bool isSecure,
             const wp<IBinder>& displayToken,
             const sp<DisplaySurface>& displaySurface,
diff --git a/services/surfaceflinger/DisplayHardware/HWComposer.cpp b/services/surfaceflinger/DisplayHardware/HWComposer.cpp
index c7d1a90..1b652c3 100644
--- a/services/surfaceflinger/DisplayHardware/HWComposer.cpp
+++ b/services/surfaceflinger/DisplayHardware/HWComposer.cpp
@@ -455,7 +455,11 @@
 }
 
 uint32_t HWComposer::getFormat(int disp) const {
-    return mDisplayData[disp].format;
+    if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
+        return HAL_PIXEL_FORMAT_RGBA_8888;
+    } else {
+        return mDisplayData[disp].format;
+    }
 }
 
 float HWComposer::getDpiX(int disp) const {
@@ -1147,7 +1151,7 @@
 }
 
 HWComposer::DisplayData::DisplayData()
-:   width(0), height(0), format(0),
+:   width(0), height(0), format(HAL_PIXEL_FORMAT_RGBA_8888),
     xdpi(0.0f), ydpi(0.0f),
     refresh(0),
     connected(false),
diff --git a/services/surfaceflinger/DisplayHardware/VirtualDisplaySurface.cpp b/services/surfaceflinger/DisplayHardware/VirtualDisplaySurface.cpp
index be5cf4a..d7fef8c 100644
--- a/services/surfaceflinger/DisplayHardware/VirtualDisplaySurface.cpp
+++ b/services/surfaceflinger/DisplayHardware/VirtualDisplaySurface.cpp
@@ -458,8 +458,10 @@
     mCompositionType = COMPOSITION_UNKNOWN;
     mSinkBufferWidth = 0;
     mSinkBufferHeight = 0;
+    mFbFence = Fence::NO_FENCE;
     mOutputFence = Fence::NO_FENCE;
     mOutputProducerSlot = -1;
+    mFbProducerSlot = -1;
 }
 
 status_t VirtualDisplaySurface::refreshOutputBuffer() {
diff --git a/services/surfaceflinger/MessageQueue.cpp b/services/surfaceflinger/MessageQueue.cpp
index c9c7b96..cc672b6 100644
--- a/services/surfaceflinger/MessageQueue.cpp
+++ b/services/surfaceflinger/MessageQueue.cpp
@@ -105,7 +105,7 @@
     mEventThread = eventThread;
     mEvents = eventThread->createEventConnection();
     mEventTube = mEvents->getDataChannel();
-    mLooper->addFd(mEventTube->getFd(), 0, ALOOPER_EVENT_INPUT,
+    mLooper->addFd(mEventTube->getFd(), 0, Looper::EVENT_INPUT,
             MessageQueue::cb_eventReceiver, this);
 }
 
@@ -114,12 +114,12 @@
         IPCThreadState::self()->flushCommands();
         int32_t ret = mLooper->pollOnce(-1);
         switch (ret) {
-            case ALOOPER_POLL_WAKE:
-            case ALOOPER_POLL_CALLBACK:
+            case Looper::POLL_WAKE:
+            case Looper::POLL_CALLBACK:
                 continue;
-            case ALOOPER_POLL_ERROR:
-                ALOGE("ALOOPER_POLL_ERROR");
-            case ALOOPER_POLL_TIMEOUT:
+            case Looper::POLL_ERROR:
+                ALOGE("Looper::POLL_ERROR");
+            case Looper::POLL_TIMEOUT:
                 // timeout (should not happen)
                 continue;
             default:
diff --git a/services/surfaceflinger/RenderEngine/RenderEngine.cpp b/services/surfaceflinger/RenderEngine/RenderEngine.cpp
index ba82cad..2871ce9 100644
--- a/services/surfaceflinger/RenderEngine/RenderEngine.cpp
+++ b/services/surfaceflinger/RenderEngine/RenderEngine.cpp
@@ -25,19 +25,51 @@
 #include "GLExtensions.h"
 #include "Mesh.h"
 
+EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
+
 // ---------------------------------------------------------------------------
 namespace android {
 // ---------------------------------------------------------------------------
 
-RenderEngine* RenderEngine::create(EGLDisplay display, EGLConfig config) {
-    EGLint renderableType = 0;
-    EGLint contextClientVersion = 0;
+static bool findExtension(const char* exts, const char* name) {
+    if (!exts)
+        return false;
+    size_t len = strlen(name);
 
-    // query the renderable type, setting the EGL_CONTEXT_CLIENT_VERSION accordingly
-    if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
-        LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
+    const char* pos = exts;
+    while ((pos = strstr(pos, name)) != NULL) {
+        if (pos[len] == '\0' || pos[len] == ' ')
+            return true;
+        pos += len;
     }
 
+    return false;
+}
+
+RenderEngine* RenderEngine::create(EGLDisplay display, int hwcFormat) {
+    // EGL_ANDROIDX_no_config_context is an experimental extension with no
+    // written specification. It will be replaced by something more formal.
+    // SurfaceFlinger is using it to allow a single EGLContext to render to
+    // both a 16-bit primary display framebuffer and a 32-bit virtual display
+    // framebuffer.
+    //
+    // The code assumes that ES2 or later is available if this extension is
+    // supported.
+    EGLConfig config = EGL_NO_CONFIG;
+    if (!findExtension(
+            eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS),
+            "EGL_ANDROIDX_no_config_context")) {
+        config = chooseEglConfig(display, hwcFormat);
+    }
+
+    EGLint renderableType = 0;
+    if (config == EGL_NO_CONFIG) {
+        renderableType = EGL_OPENGL_ES2_BIT;
+    } else if (!eglGetConfigAttrib(display, config,
+            EGL_RENDERABLE_TYPE, &renderableType)) {
+        LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
+    }
+    EGLint contextClientVersion = 0;
     if (renderableType & EGL_OPENGL_ES2_BIT) {
         contextClientVersion = 2;
     } else if (renderableType & EGL_OPENGL_ES_BIT) {
@@ -66,8 +98,12 @@
     // now figure out what version of GL did we actually get
     // NOTE: a dummy surface is not needed if KHR_create_context is supported
 
+    EGLConfig dummyConfig = config;
+    if (dummyConfig == EGL_NO_CONFIG) {
+        dummyConfig = chooseEglConfig(display, hwcFormat);
+    }
     EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE, EGL_NONE };
-    EGLSurface dummy = eglCreatePbufferSurface(display, config, attribs);
+    EGLSurface dummy = eglCreatePbufferSurface(display, dummyConfig, attribs);
     LOG_ALWAYS_FATAL_IF(dummy==EGL_NO_SURFACE, "can't create dummy pbuffer");
     EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt);
     LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current");
@@ -96,7 +132,7 @@
         engine = new GLES20RenderEngine();
         break;
     }
-    engine->setEGLContext(ctxt);
+    engine->setEGLHandles(config, ctxt);
 
     ALOGI("OpenGL ES informations:");
     ALOGI("vendor    : %s", extensions.getVendor());
@@ -118,10 +154,15 @@
 RenderEngine::~RenderEngine() {
 }
 
-void RenderEngine::setEGLContext(EGLContext ctxt) {
+void RenderEngine::setEGLHandles(EGLConfig config, EGLContext ctxt) {
+    mEGLConfig = config;
     mEGLContext = ctxt;
 }
 
+EGLContext RenderEngine::getEGLConfig() const {
+    return mEGLConfig;
+}
+
 EGLContext RenderEngine::getEGLContext() const {
     return mEGLContext;
 }
@@ -235,5 +276,163 @@
 }
 
 // ---------------------------------------------------------------------------
+
+static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs,
+        EGLint attribute, EGLint wanted, EGLConfig* outConfig) {
+    EGLConfig config = NULL;
+    EGLint numConfigs = -1, n = 0;
+    eglGetConfigs(dpy, NULL, 0, &numConfigs);
+    EGLConfig* const configs = new EGLConfig[numConfigs];
+    eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
+
+    if (n) {
+        if (attribute != EGL_NONE) {
+            for (int i=0 ; i<n ; i++) {
+                EGLint value = 0;
+                eglGetConfigAttrib(dpy, configs[i], attribute, &value);
+                if (wanted == value) {
+                    *outConfig = configs[i];
+                    delete [] configs;
+                    return NO_ERROR;
+                }
+            }
+        } else {
+            // just pick the first one
+            *outConfig = configs[0];
+            delete [] configs;
+            return NO_ERROR;
+        }
+    }
+    delete [] configs;
+    return NAME_NOT_FOUND;
+}
+
+class EGLAttributeVector {
+    struct Attribute;
+    class Adder;
+    friend class Adder;
+    KeyedVector<Attribute, EGLint> mList;
+    struct Attribute {
+        Attribute() {};
+        Attribute(EGLint v) : v(v) { }
+        EGLint v;
+        bool operator < (const Attribute& other) const {
+            // this places EGL_NONE at the end
+            EGLint lhs(v);
+            EGLint rhs(other.v);
+            if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
+            if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
+            return lhs < rhs;
+        }
+    };
+    class Adder {
+        friend class EGLAttributeVector;
+        EGLAttributeVector& v;
+        EGLint attribute;
+        Adder(EGLAttributeVector& v, EGLint attribute)
+            : v(v), attribute(attribute) {
+        }
+    public:
+        void operator = (EGLint value) {
+            if (attribute != EGL_NONE) {
+                v.mList.add(attribute, value);
+            }
+        }
+        operator EGLint () const { return v.mList[attribute]; }
+    };
+public:
+    EGLAttributeVector() {
+        mList.add(EGL_NONE, EGL_NONE);
+    }
+    void remove(EGLint attribute) {
+        if (attribute != EGL_NONE) {
+            mList.removeItem(attribute);
+        }
+    }
+    Adder operator [] (EGLint attribute) {
+        return Adder(*this, attribute);
+    }
+    EGLint operator [] (EGLint attribute) const {
+       return mList[attribute];
+    }
+    // cast-operator to (EGLint const*)
+    operator EGLint const* () const { return &mList.keyAt(0).v; }
+};
+
+
+static status_t selectEGLConfig(EGLDisplay display, EGLint format,
+    EGLint renderableType, EGLConfig* config) {
+    // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
+    // it is to be used with WIFI displays
+    status_t err;
+    EGLint wantedAttribute;
+    EGLint wantedAttributeValue;
+
+    EGLAttributeVector attribs;
+    if (renderableType) {
+        attribs[EGL_RENDERABLE_TYPE]            = renderableType;
+        attribs[EGL_RECORDABLE_ANDROID]         = EGL_TRUE;
+        attribs[EGL_SURFACE_TYPE]               = EGL_WINDOW_BIT|EGL_PBUFFER_BIT;
+        attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
+        attribs[EGL_RED_SIZE]                   = 8;
+        attribs[EGL_GREEN_SIZE]                 = 8;
+        attribs[EGL_BLUE_SIZE]                  = 8;
+        wantedAttribute                         = EGL_NONE;
+        wantedAttributeValue                    = EGL_NONE;
+    } else {
+        // if no renderable type specified, fallback to a simplified query
+        wantedAttribute                         = EGL_NATIVE_VISUAL_ID;
+        wantedAttributeValue                    = format;
+    }
+
+    err = selectConfigForAttribute(display, attribs,
+            wantedAttribute, wantedAttributeValue, config);
+    if (err == NO_ERROR) {
+        EGLint caveat;
+        if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
+            ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
+    }
+
+    return err;
+}
+
+EGLConfig RenderEngine::chooseEglConfig(EGLDisplay display, int format) {
+    status_t err;
+    EGLConfig config;
+
+    // First try to get an ES2 config
+    err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
+    if (err != NO_ERROR) {
+        // If ES2 fails, try ES1
+        err = selectEGLConfig(display, format, EGL_OPENGL_ES_BIT, &config);
+        if (err != NO_ERROR) {
+            // still didn't work, probably because we're on the emulator...
+            // try a simplified query
+            ALOGW("no suitable EGLConfig found, trying a simpler query");
+            err = selectEGLConfig(display, format, 0, &config);
+            if (err != NO_ERROR) {
+                // this EGL is too lame for android
+                LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
+            }
+        }
+    }
+
+    // print some debugging info
+    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);
+    ALOGI("EGL information:");
+    ALOGI("vendor    : %s", eglQueryString(display, EGL_VENDOR));
+    ALOGI("version   : %s", eglQueryString(display, EGL_VERSION));
+    ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
+    ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
+    ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
+
+    return config;
+}
+
+// ---------------------------------------------------------------------------
 }; // namespace android
 // ---------------------------------------------------------------------------
diff --git a/services/surfaceflinger/RenderEngine/RenderEngine.h b/services/surfaceflinger/RenderEngine/RenderEngine.h
index 3c7f9ab..577dc0a 100644
--- a/services/surfaceflinger/RenderEngine/RenderEngine.h
+++ b/services/surfaceflinger/RenderEngine/RenderEngine.h
@@ -25,6 +25,8 @@
 #include <EGL/eglext.h>
 #include <ui/mat4.h>
 
+#define EGL_NO_CONFIG ((EGLConfig)0)
+
 // ---------------------------------------------------------------------------
 namespace android {
 // ---------------------------------------------------------------------------
@@ -44,8 +46,9 @@
     };
     static GlesVersion parseGlesVersion(const char* str);
 
+    EGLConfig mEGLConfig;
     EGLContext mEGLContext;
-    void setEGLContext(EGLContext ctxt);
+    void setEGLHandles(EGLConfig config, EGLContext ctxt);
 
     virtual void bindImageAsFramebuffer(EGLImageKHR image, uint32_t* texName, uint32_t* fbName, uint32_t* status) = 0;
     virtual void unbindFramebuffer(uint32_t texName, uint32_t fbName) = 0;
@@ -55,7 +58,9 @@
     virtual ~RenderEngine() = 0;
 
 public:
-    static RenderEngine* create(EGLDisplay display, EGLConfig config);
+    static RenderEngine* create(EGLDisplay display, int hwcFormat);
+
+    static EGLConfig chooseEglConfig(EGLDisplay display, int format);
 
     // dump the extension strings. always call the base class.
     virtual void dump(String8& result);
@@ -107,6 +112,7 @@
     virtual size_t getMaxTextureSize() const = 0;
     virtual size_t getMaxViewportDims() const = 0;
 
+    EGLConfig getEGLConfig() const;
     EGLContext getEGLContext() const;
 };
 
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 9d94c87..c00b034 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -318,128 +318,6 @@
     postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
 }
 
-status_t SurfaceFlinger::selectConfigForAttribute(
-        EGLDisplay dpy,
-        EGLint const* attrs,
-        EGLint attribute, EGLint wanted,
-        EGLConfig* outConfig)
-{
-    EGLConfig config = NULL;
-    EGLint numConfigs = -1, n=0;
-    eglGetConfigs(dpy, NULL, 0, &numConfigs);
-    EGLConfig* const configs = new EGLConfig[numConfigs];
-    eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
-
-    if (n) {
-        if (attribute != EGL_NONE) {
-            for (int i=0 ; i<n ; i++) {
-                EGLint value = 0;
-                eglGetConfigAttrib(dpy, configs[i], attribute, &value);
-                if (wanted == value) {
-                    *outConfig = configs[i];
-                    delete [] configs;
-                    return NO_ERROR;
-                }
-            }
-        } else {
-            // just pick the first one
-            *outConfig = configs[0];
-            delete [] configs;
-            return NO_ERROR;
-        }
-    }
-    delete [] configs;
-    return NAME_NOT_FOUND;
-}
-
-class EGLAttributeVector {
-    struct Attribute;
-    class Adder;
-    friend class Adder;
-    KeyedVector<Attribute, EGLint> mList;
-    struct Attribute {
-        Attribute() {};
-        Attribute(EGLint v) : v(v) { }
-        EGLint v;
-        bool operator < (const Attribute& other) const {
-            // this places EGL_NONE at the end
-            EGLint lhs(v);
-            EGLint rhs(other.v);
-            if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
-            if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
-            return lhs < rhs;
-        }
-    };
-    class Adder {
-        friend class EGLAttributeVector;
-        EGLAttributeVector& v;
-        EGLint attribute;
-        Adder(EGLAttributeVector& v, EGLint attribute)
-            : v(v), attribute(attribute) {
-        }
-    public:
-        void operator = (EGLint value) {
-            if (attribute != EGL_NONE) {
-                v.mList.add(attribute, value);
-            }
-        }
-        operator EGLint () const { return v.mList[attribute]; }
-    };
-public:
-    EGLAttributeVector() {
-        mList.add(EGL_NONE, EGL_NONE);
-    }
-    void remove(EGLint attribute) {
-        if (attribute != EGL_NONE) {
-            mList.removeItem(attribute);
-        }
-    }
-    Adder operator [] (EGLint attribute) {
-        return Adder(*this, attribute);
-    }
-    EGLint operator [] (EGLint attribute) const {
-       return mList[attribute];
-    }
-    // cast-operator to (EGLint const*)
-    operator EGLint const* () const { return &mList.keyAt(0).v; }
-};
-
-status_t SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId,
-    EGLint renderableType, EGLConfig* config) {
-    // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
-    // it is to be used with WIFI displays
-    status_t err;
-    EGLint wantedAttribute;
-    EGLint wantedAttributeValue;
-
-    EGLAttributeVector attribs;
-    if (renderableType) {
-        attribs[EGL_RENDERABLE_TYPE]            = renderableType;
-        attribs[EGL_RECORDABLE_ANDROID]         = EGL_TRUE;
-        attribs[EGL_SURFACE_TYPE]               = EGL_WINDOW_BIT|EGL_PBUFFER_BIT;
-        attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
-        attribs[EGL_RED_SIZE]                   = 8;
-        attribs[EGL_GREEN_SIZE]                 = 8;
-        attribs[EGL_BLUE_SIZE]                  = 8;
-        wantedAttribute                         = EGL_NONE;
-        wantedAttributeValue                    = EGL_NONE;
-
-    } else {
-        // if no renderable type specified, fallback to a simplified query
-        wantedAttribute                         = EGL_NATIVE_VISUAL_ID;
-        wantedAttributeValue                    = nativeVisualId;
-    }
-
-    err = selectConfigForAttribute(display, attribs, wantedAttribute,
-        wantedAttributeValue, config);
-    if (err == NO_ERROR) {
-        EGLint caveat;
-        if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
-            ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
-    }
-    return err;
-}
-
 class DispSyncSource : public VSyncSource, private DispSync::Callback {
 public:
     DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync) :
@@ -521,51 +399,12 @@
     mHwc = new HWComposer(this,
             *static_cast<HWComposer::EventHandler *>(this));
 
-    // First try to get an ES2 config
-    err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(), EGL_OPENGL_ES2_BIT,
-            &mEGLConfig);
-
-    if (err != NO_ERROR) {
-        // If ES2 fails, try ES1
-        err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(),
-                EGL_OPENGL_ES_BIT, &mEGLConfig);
-    }
-
-    if (err != NO_ERROR) {
-        // still didn't work, probably because we're on the emulator...
-        // try a simplified query
-        ALOGW("no suitable EGLConfig found, trying a simpler query");
-        err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(), 0, &mEGLConfig);
-    }
-
-    if (err != NO_ERROR) {
-        // this EGL is too lame for android
-        LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
-    }
-
-    // print some debugging info
-    EGLint r,g,b,a;
-    eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_RED_SIZE,   &r);
-    eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_GREEN_SIZE, &g);
-    eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_BLUE_SIZE,  &b);
-    eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_ALPHA_SIZE, &a);
-    ALOGI("EGL informations:");
-    ALOGI("vendor    : %s", eglQueryString(mEGLDisplay, EGL_VENDOR));
-    ALOGI("version   : %s", eglQueryString(mEGLDisplay, EGL_VERSION));
-    ALOGI("extensions: %s", eglQueryString(mEGLDisplay, EGL_EXTENSIONS));
-    ALOGI("Client API: %s", eglQueryString(mEGLDisplay, EGL_CLIENT_APIS)?:"Not Supported");
-    ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
-
     // get a RenderEngine for the given display / config (can't fail)
-    mRenderEngine = RenderEngine::create(mEGLDisplay, mEGLConfig);
+    mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
 
     // retrieve the EGL context that was selected/created
     mEGLContext = mRenderEngine->getEGLContext();
 
-    // figure out which format we got
-    eglGetConfigAttrib(mEGLDisplay, mEGLConfig,
-            EGL_NATIVE_VISUAL_ID, &mEGLNativeVisualId);
-
     LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
             "couldn't create EGLContext");
 
@@ -581,10 +420,11 @@
 
             sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
             sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i, bq);
+            int32_t hwcId = allocateHwcDisplayId(type);
             sp<DisplayDevice> hw = new DisplayDevice(this,
-                    type, allocateHwcDisplayId(type), isSecure, token,
+                    type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
                     fbs, bq,
-                    mEGLConfig);
+                    mRenderEngine->getEGLConfig());
             if (i > DisplayDevice::DISPLAY_PRIMARY) {
                 // FIXME: currently we don't get blank/unblank requests
                 // for displays other than the main display, so we always
@@ -1346,8 +1186,10 @@
                     const wp<IBinder>& display(curr.keyAt(i));
                     if (dispSurface != NULL) {
                         sp<DisplayDevice> hw = new DisplayDevice(this,
-                                state.type, hwcDisplayId, state.isSecure,
-                                display, dispSurface, producer, mEGLConfig);
+                                state.type, hwcDisplayId,
+                                mHwc->getFormat(hwcDisplayId), state.isSecure,
+                                display, dispSurface, producer,
+                                mRenderEngine->getEGLConfig());
                         hw->setLayerStack(state.layerStack);
                         hw->setProjection(state.orientation,
                                 state.viewport, state.frame);
@@ -2544,7 +2386,6 @@
             "  refresh-rate              : %f fps\n"
             "  x-dpi                     : %f\n"
             "  y-dpi                     : %f\n"
-            "  EGL_NATIVE_VISUAL_ID      : %d\n"
             "  gpu_to_cpu_unsupported    : %d\n"
             ,
             mLastSwapBufferTime/1000.0,
@@ -2553,7 +2394,6 @@
             1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
             hwc.getDpiX(HWC_DISPLAY_PRIMARY),
             hwc.getDpiY(HWC_DISPLAY_PRIMARY),
-            mEGLNativeVisualId,
             !mGpuToCpuSupported);
 
     result.appendFormat("  eglSwapBuffers time: %f us\n",
diff --git a/services/surfaceflinger/SurfaceFlinger.h b/services/surfaceflinger/SurfaceFlinger.h
index f08e66a..80bb619 100644
--- a/services/surfaceflinger/SurfaceFlinger.h
+++ b/services/surfaceflinger/SurfaceFlinger.h
@@ -317,10 +317,6 @@
     /* ------------------------------------------------------------------------
      * EGL
      */
-    static status_t selectConfigForAttribute(EGLDisplay dpy,
-        EGLint const* attrs, EGLint attribute, EGLint value, EGLConfig* outConfig);
-    static status_t selectEGLConfig(EGLDisplay disp, EGLint visualId,
-        EGLint renderableType, EGLConfig* config);
     size_t getMaxTextureSize() const;
     size_t getMaxViewportDims() const;
 
@@ -431,9 +427,7 @@
     sp<EventThread> mSFEventThread;
     sp<EventControlThread> mEventControlThread;
     EGLContext mEGLContext;
-    EGLConfig mEGLConfig;
     EGLDisplay mEGLDisplay;
-    EGLint mEGLNativeVisualId;
     sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES];
 
     // Can only accessed from the main thread, these members