emulator opengl: pool of fixups to the system components.

Those fixes make the system load without failure.
Also the flinger and bootanimation are running.

Change-Id: Ieb7039e76c444df778a421a07bccc48514199245
diff --git a/tools/emulator/opengl/system/GLESv1/Android.mk b/tools/emulator/opengl/system/GLESv1/Android.mk
index aa5725c..e341292 100644
--- a/tools/emulator/opengl/system/GLESv1/Android.mk
+++ b/tools/emulator/opengl/system/GLESv1/Android.mk
@@ -26,12 +26,12 @@
 		$(call intermediates-dir-for, SHARED_LIBRARIES, libGLESv1_enc)
 
 LOCAL_MODULE_TAGS := debug
-LOCAL_MODULE := libGL_emulation
+LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/egl
+LOCAL_MODULE := libGLESv1_CM_emulation
 LOCAL_MODULE_CLASS := SHARED_LIBRARIES
 
 
 LOCAL_STATIC_LIBRARIES := \
-    libOpenglSystemCommon \
     libOpenglCodecCommon  \
 	libqemu
 
@@ -39,7 +39,8 @@
     libcutils \
 	libutils \
 	libdl \
-	libGLESv1_enc \
+    libOpenglSystemCommon \
+	libGLESv1_enc	\
 	lib_renderControl_enc
 
 
diff --git a/tools/emulator/opengl/system/GLESv1/gl.cpp b/tools/emulator/opengl/system/GLESv1/gl.cpp
index 9ff5f56..eb7c0a9 100644
--- a/tools/emulator/opengl/system/GLESv1/gl.cpp
+++ b/tools/emulator/opengl/system/GLESv1/gl.cpp
@@ -3,6 +3,10 @@
 #include "GLEncoder.h"
 #include "GLES/gl.h"
 #include "GLES/glext.h"
+#include "ErrorLog.h"
+#include <private/ui/android_natives_priv.h>
+#include "gralloc_cb.h"
+
 
 //XXX: fix this macro to get the context from fast tls path
 #define GET_CONTEXT gl_client_context_t * ctx = HostConnection::get()->glEncoder();
@@ -15,6 +19,39 @@
 static EGLClient_eglInterface * s_egl = NULL;
 static EGLClient_glesInterface * s_gl = NULL;
 
+#define DEFINE_AND_VALIDATE_HOST_CONNECTION(ret) \
+    HostConnection *hostCon = HostConnection::get(); \
+    if (!hostCon) { \
+        LOGE("egl: Failed to get host connection\n"); \
+        return ret; \
+    } \
+    renderControl_encoder_context_t *rcEnc = hostCon->rcEncoder(); \
+    if (!rcEnc) { \
+        LOGE("egl: Failed to get renderControl encoder context\n"); \
+        return ret; \
+    }
+
+//GL extensions
+void glEGLImageTargetTexture2DOES(void * self, GLenum target, GLeglImageOES image)
+{
+    DBG("glEGLImageTargetTexture2DOES");
+    //TODO: check error - we don't have a way to set gl error
+    android_native_buffer_t* native_buffer = (android_native_buffer_t*)image;
+
+    if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC) {
+        return;
+    }
+
+    if (native_buffer->common.version != sizeof(android_native_buffer_t)) {
+        return;
+    }
+
+    DEFINE_AND_VALIDATE_HOST_CONNECTION();
+    rcEnc->rcBindTexture(rcEnc, ((cb_handle_t *)(native_buffer->handle))->hostHandle);
+
+    return;
+}
+
 void * getProcAddress(const char * procname)
 {
     // search in GL function table
@@ -31,6 +68,13 @@
     glFinish();
 }
 
+void init()
+{
+    GET_CONTEXT;
+    ctx->set_glEGLImageTargetTexture2DOES(glEGLImageTargetTexture2DOES);
+}
+
+extern "C" {
 EGLClient_glesInterface * init_emul_gles(EGLClient_eglInterface *eglIface)
 {
     s_egl = eglIface;
@@ -39,8 +83,11 @@
         s_gl = new EGLClient_glesInterface();
         s_gl->getProcAddress = getProcAddress;
         s_gl->finish = finish;
+        s_gl->init = init;
     }
 
     return s_gl;
 }
+} //extern
+
 
diff --git a/tools/emulator/opengl/system/GLESv1_enc/Android.mk b/tools/emulator/opengl/system/GLESv1_enc/Android.mk
index 16f29b1..f48b1f7 100644
--- a/tools/emulator/opengl/system/GLESv1_enc/Android.mk
+++ b/tools/emulator/opengl/system/GLESv1_enc/Android.mk
@@ -19,19 +19,16 @@
 LOCAL_CFLAGS += -DLOG_TAG=\"egl_GLESv1_enc\"
 LOCAL_C_INCLUDES +=  \
     $(emulatorOpengl)/shared/OpenglCodecCommon \
-    $(emulatorOpengl)/system/OpenglSystemCommon \
     $(emulatorOpengl)/host/include/libOpenglRender \
     $(emulatorOpengl)/system/renderControl_enc \
 	$(call intermediates-dir-for, SHARED_LIBRARIES, lib_renderControl_enc) \
     $(glesv1_intermediates)
 
 LOCAL_STATIC_LIBRARIES := \
-    libOpenglSystemCommon \
     libOpenglCodecCommon  \
 	libqemu
 
 LOCAL_SHARED_LIBRARIES := 	\
-    lib_renderControl_enc	\
 	libcutils
 
 EMUGEN := $(HOST_OUT_EXECUTABLES)/emugen
diff --git a/tools/emulator/opengl/system/GLESv1_enc/GLEncoder.cpp b/tools/emulator/opengl/system/GLESv1_enc/GLEncoder.cpp
index 6633d13..9142bef 100644
--- a/tools/emulator/opengl/system/GLESv1_enc/GLEncoder.cpp
+++ b/tools/emulator/opengl/system/GLESv1_enc/GLEncoder.cpp
@@ -16,10 +16,6 @@
 #include "GLEncoder.h"
 #include "glUtils.h"
 #include "FixedBuffer.h"
-#include "HostConnection.h"
-#include <private/ui/android_natives_priv.h>
-#include "gralloc_cb.h"
-
 #include <cutils/log.h>
 #include <assert.h>
 
@@ -154,7 +150,7 @@
 {
     GLEncoder *ctx = (GLEncoder *)self;
     ctx->m_glPixelStorei_enc(ctx, param, value);
-    assert(ctx->m_state != NULL);
+    LOG_ASSERT(ctx->m_state, "GLEncoder::s_glPixelStorei");
     ctx->m_state->setPixelStore(param, value);
 }
 
@@ -455,6 +451,7 @@
 
 GLEncoder::GLEncoder(IOStream *stream) : gl_encoder_context_t(stream)
 {
+    m_initialized = false;
     m_state = NULL;
     m_compressedTextureFormats = NULL;
     // overrides;
@@ -483,7 +480,6 @@
     m_glDrawElements_enc = set_glDrawElements(s_glDrawElements);
     set_glGetString(s_glGetString);
     set_glFinish(s_glFinish);
-    set_glEGLImageTargetTexture2DOES(s_glEGLImageTargetTexture2DOES);
 
 }
 
@@ -504,21 +500,3 @@
     ctx->glFinishRoundTrip(self);
 }
 
-void GLEncoder::s_glEGLImageTargetTexture2DOES(void * self, GLenum target, GLeglImageOES image)
-{
-    //TODO: check error - we don't have a way to set gl error
-    android_native_buffer_t* native_buffer = (android_native_buffer_t*)image;
-
-    if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC) {
-        return;
-    }
-
-    if (native_buffer->common.version != sizeof(android_native_buffer_t)) {
-        return;
-    }
-
-    DEFINE_AND_VALIDATE_HOST_CONNECTION();
-    rcEnc->rcBindTexture(rcEnc, ((cb_handle_t *)(native_buffer->handle))->hostHandle);
-
-    return;
-}
diff --git a/tools/emulator/opengl/system/GLESv1_enc/GLEncoder.h b/tools/emulator/opengl/system/GLESv1_enc/GLEncoder.h
index 2d727f5..6275820 100644
--- a/tools/emulator/opengl/system/GLESv1_enc/GLEncoder.h
+++ b/tools/emulator/opengl/system/GLESv1_enc/GLEncoder.h
@@ -30,8 +30,13 @@
     }
     void flush() { m_stream->flush(); }
     size_t pixelDataSize(GLsizei width, GLsizei height, GLenum format, GLenum type, int pack);
+
+    void setInitialized(){ m_initialized = true; };
+    bool isInitialized(){ return m_initialized; };
+
 private:
 
+    bool    m_initialized;
     GLClientState *m_state;
     FixedBuffer m_fixedBuffer;
     GLint *m_compressedTextureFormats;
diff --git a/tools/emulator/opengl/system/OpenglSystemCommon/Android.mk b/tools/emulator/opengl/system/OpenglSystemCommon/Android.mk
index 760bb72..33fc555 100644
--- a/tools/emulator/opengl/system/OpenglSystemCommon/Android.mk
+++ b/tools/emulator/opengl/system/OpenglSystemCommon/Android.mk
@@ -23,7 +23,19 @@
 		$(call intermediates-dir-for, SHARED_LIBRARIES, lib_renderControl_enc) \
 		$(call intermediates-dir-for, SHARED_LIBRARIES, libGLESv1_enc)
 
+LOCAL_STATIC_LIBRARIES := \
+    libOpenglCodecCommon  \
+	libqemu
+
+LOCAL_SHARED_LIBRARIES := \
+	lib_renderControl_enc	\
+	libGLESv1_enc	\
+	libcutils \
+	libutils 
+
+LOCAL_PRELINK_MODULE := false
 LOCAL_MODULE_TAGS := debug
 LOCAL_MODULE := libOpenglSystemCommon
+LOCAL_MODULE_CLASS := SHARED_LIBRARIES
 
-include $(BUILD_STATIC_LIBRARY)
+include $(BUILD_SHARED_LIBRARY)
diff --git a/tools/emulator/opengl/system/OpenglSystemCommon/EGLClientIface.h b/tools/emulator/opengl/system/OpenglSystemCommon/EGLClientIface.h
index df66ba6..f959b44 100644
--- a/tools/emulator/opengl/system/OpenglSystemCommon/EGLClientIface.h
+++ b/tools/emulator/opengl/system/OpenglSystemCommon/EGLClientIface.h
@@ -9,6 +9,7 @@
 
 typedef struct {
     void* (*getProcAddress)(const char *funcName);
+    void (*init)();
     void (*finish)();
 } EGLClient_glesInterface;
 
diff --git a/tools/emulator/opengl/system/OpenglSystemCommon/HostConnection.cpp b/tools/emulator/opengl/system/OpenglSystemCommon/HostConnection.cpp
index bfb43f9..1745554 100644
--- a/tools/emulator/opengl/system/OpenglSystemCommon/HostConnection.cpp
+++ b/tools/emulator/opengl/system/OpenglSystemCommon/HostConnection.cpp
@@ -20,7 +20,7 @@
 #include <cutils/log.h>
 
 #define STREAM_BUFFER_SIZE  4*1024*1024
-#define STREAM_PORT_NUM     4141
+#define STREAM_PORT_NUM     22468
 
 /* Set to 1 to use a QEMU pipe, or 0 for a TCP connection */
 #define  USE_QEMU_PIPE  1
@@ -56,6 +56,22 @@
             return NULL;
         }
 
+#if 0
+            TcpStream *stream = new TcpStream(STREAM_BUFFER_SIZE);
+            if (stream) {
+                if (stream->connect("10.0.2.2", STREAM_PORT_NUM) < 0) {
+                    LOGE("Failed to connect to host (TcpStream)!!!\n");
+                    delete stream;
+                    stream = NULL;
+                }
+                else {
+                    con->m_stream = stream;
+                    LOGE("Established TCP connection with HOST\n");
+                }
+            }
+            if (!stream)
+#endif
+
         if (useQemuPipe) {
             QemuPipeStream *stream = new QemuPipeStream(STREAM_BUFFER_SIZE);
             if (!stream) {
@@ -64,7 +80,7 @@
                 return NULL;
             }
             if (stream->connect() < 0) {
-                LOGE("Failed to connect to host !!!\n");
+                LOGE("Failed to connect to host (QemuPipeStream)!!!\n");
                 delete con;
                 return NULL;
             }
@@ -80,13 +96,13 @@
             }
 
             if (stream->connect("10.0.2.2", STREAM_PORT_NUM) < 0) {
-                LOGE("Failed to connect to host !!!\n");
+                LOGE("Failed to connect to host (TcpStream)!!!\n");
                 delete con;
                 return NULL;
             }
             con->m_stream = stream;
         }
-        LOGD("Host Connection established \n");
+        LOGD("HostConnection::get() New Host Connection established %p, tid %d\n", con, gettid());
         tinfo->hostConn = con;
     }
 
@@ -97,6 +113,7 @@
 {
     if (!m_glEnc) {
         m_glEnc = new GLEncoder(m_stream);
+        DBG("HostConnection::glEncoder new encoder %p, tid %d", m_glEnc, gettid());
         m_glEnc->setContextAccessor(s_getGLContext);
     }
     return m_glEnc;
diff --git a/tools/emulator/opengl/system/OpenglSystemCommon/QemuPipeStream.cpp b/tools/emulator/opengl/system/OpenglSystemCommon/QemuPipeStream.cpp
index 1a339ba..3f92724 100644
--- a/tools/emulator/opengl/system/OpenglSystemCommon/QemuPipeStream.cpp
+++ b/tools/emulator/opengl/system/OpenglSystemCommon/QemuPipeStream.cpp
@@ -84,6 +84,7 @@
 
 int QemuPipeStream::writeFully(const void *buf, size_t len)
 {
+    //DBG(">> QemuPipeStream::writeFully %d\n", len);
     if (!valid()) return -1;
 
     size_t res = len;
@@ -107,11 +108,13 @@
         ERR("QemuPipeStream::writeFully failed: %s\n", strerror(errno));
         break;
     }
+    //DBG("<< QemuPipeStream::writeFully %d\n", len );
     return retval;
 }
 
 const unsigned char *QemuPipeStream::readFully(void *buf, size_t len)
 {
+    //DBG(">> QemuPipeStream::readFully %d\n", len);
     if (!valid()) return NULL;
     if (!buf) {
         ERR("QemuPipeStream::readFully failed, buf=NULL");
@@ -135,11 +138,13 @@
             res -= stat;
         }
     }
+    //DBG("<< QemuPipeStream::readFully %d\n", len);
     return (const unsigned char *)buf;
 }
 
 const unsigned char *QemuPipeStream::read( void *buf, size_t *inout_len)
 {
+    //DBG(">> QemuPipeStream::read %d\n", *inout_len);
     if (!valid()) return NULL;
     if (!buf) {
       ERR("QemuPipeStream::read failed, buf=NULL");
@@ -153,6 +158,7 @@
         return (const unsigned char *)buf;
     }
 
+    //DBG("<< QemuPipeStream::read %d\n", *inout_len);
     return NULL;
 }
 
diff --git a/tools/emulator/opengl/system/OpenglSystemCommon/gralloc_cb.h b/tools/emulator/opengl/system/OpenglSystemCommon/gralloc_cb.h
index 3bfabc8..03b9b6d 100644
--- a/tools/emulator/opengl/system/OpenglSystemCommon/gralloc_cb.h
+++ b/tools/emulator/opengl/system/OpenglSystemCommon/gralloc_cb.h
@@ -6,7 +6,7 @@
 #include <cutils/native_handle.h>
 
 #define BUFFER_HANDLE_MAGIC ((int)0xabfabfab)
-#define CB_HANDLE_NUM_INTS(nfds) ((sizeof(cb_handle_t) - (nfds)*sizeof(int)) / sizeof(int))
+#define CB_HANDLE_NUM_INTS(nfds) (int)((sizeof(cb_handle_t) - (nfds)*sizeof(int)) / sizeof(int))
 
 //
 // Our buffer handle structure
@@ -32,7 +32,7 @@
         hostHandle(0)
     {
         version = sizeof(native_handle);
-        numFds = 1;
+        numFds = 0;
         numInts = CB_HANDLE_NUM_INTS(numFds);
     }
 
@@ -40,11 +40,21 @@
         magic = 0;
     }
 
+    void setFd(int p_fd) {
+        if (p_fd >= 0) {
+            numFds = 1;
+        }
+        else {
+            numFds = 0;
+        }
+        fd = p_fd;
+        numInts = CB_HANDLE_NUM_INTS(numFds);
+    }
+
     bool validate() const {
         return (version == sizeof(native_handle) &&
                 magic == BUFFER_HANDLE_MAGIC &&
-                numInts == CB_HANDLE_NUM_INTS(1) &&
-                numFds == 1);
+                numInts == CB_HANDLE_NUM_INTS(numFds));
     }
 
     bool canBePosted() {
diff --git a/tools/emulator/opengl/system/egl/Android.mk b/tools/emulator/opengl/system/egl/Android.mk
index 10a52bc..fb9e4d8 100644
--- a/tools/emulator/opengl/system/egl/Android.mk
+++ b/tools/emulator/opengl/system/egl/Android.mk
@@ -29,13 +29,13 @@
 		$(call intermediates-dir-for, SHARED_LIBRARIES, libGLESv1_enc)
 
 LOCAL_MODULE_TAGS := debug
+LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/egl
 LOCAL_MODULE := libEGL_emulation
 LOCAL_MODULE_CLASS := SHARED_LIBRARIES
-
+LOCAL_PRELINK_MODULE := false
 
 LOCAL_STATIC_LIBRARIES := \
-    libOpenglSystemCommon \
-    libOpenglCodecCommon \
+    libOpenglCodecCommon  \
 	libqemu
 
 LOCAL_SHARED_LIBRARIES := \
@@ -43,9 +43,29 @@
 	libutils \
 	libdl	\
     libGLESv1_enc \
+    libOpenglSystemCommon \
     lib_renderControl_enc
 
 
 include $(BUILD_SHARED_LIBRARY)
 
+#### egl.cfg ####
+
+# Ensure that this file is only copied to emulator-specific builds.
+# Other builds are device-specific and will provide their own
+# version of this file to point to the appropriate HW EGL libraries.
+#
+ifneq (,$(filter full full_x86 sdk sdk_x86,$(TARGET_PRODUCT)))
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := egl.cfg
+LOCAL_SRC_FILES := $(LOCAL_MODULE)
+
+LOCAL_MODULE_PATH := $(TARGET_OUT)/lib/egl
+LOCAL_MODULE_TAGS := debug
+LOCAL_MODULE_CLASS := ETC
+
+include $(BUILD_PREBUILT)
+endif # TARGET_PRODUCT in 'full sdk full_x86 sdk_x86'
+
 endif # of ifneq (,$(BUILD_EMULATOR_OPENGL_DRIVER))
diff --git a/tools/emulator/opengl/system/egl/egl.cfg b/tools/emulator/opengl/system/egl/egl.cfg
new file mode 100644
index 0000000..9d3f2dc
--- /dev/null
+++ b/tools/emulator/opengl/system/egl/egl.cfg
@@ -0,0 +1 @@
+0 0 emulation
diff --git a/tools/emulator/opengl/system/egl/egl.cpp b/tools/emulator/opengl/system/egl/egl.cpp
index 84a7664..c45e7e7 100644
--- a/tools/emulator/opengl/system/egl/egl.cpp
+++ b/tools/emulator/opengl/system/egl/egl.cpp
@@ -19,19 +19,63 @@
 #include "egl_ftable.h"
 #include <cutils/log.h>
 #include "gralloc_cb.h"
+#include "GLClientState.h"
 
 #include <private/ui/android_natives_priv.h>
 
 template<typename T>
-static T setError(GLint error, T returnValue) {
+static T setErrorFunc(GLint error, T returnValue) {
     getEGLThreadInfo()->eglError = error;
     return returnValue;
 }
 
+const char *  eglStrError(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";
+    }
+}
+
+#define LOG_EGL_ERRORS 1
+
+#ifdef LOG_EGL_ERRORS
+
+#define setErrorReturn(error, retVal)     \
+    {                                                \
+        LOGE("tid %d: %s(%d): error 0x%x (%s)", gettid(), __FUNCTION__, __LINE__, error, eglStrError(error));     \
+        return setErrorFunc(error, retVal);            \
+    }
+
+#define RETURN_ERROR(ret,err)           \
+    LOGE("tid %d: %s(%d): error 0x%x (%s)", gettid(), __FUNCTION__, __LINE__, err, eglStrError(err));    \
+    getEGLThreadInfo()->eglError = err;    \
+    return ret;
+
+#else //!LOG_EGL_ERRORS
+
+#define setErrorReturn(error, retVal) return setErrorFunc(error, retVal);
+
 #define RETURN_ERROR(ret,err)           \
     getEGLThreadInfo()->eglError = err; \
     return ret;
 
+#endif //LOG_EGL_ERRORS
+
 #define VALIDATE_CONFIG(cfg,ret) \
     if(((int)cfg<0)||((int)cfg>s_display.getNumConfigs())) { \
         RETURN_ERROR(ret,EGL_BAD_CONFIG); \
@@ -39,15 +83,13 @@
 
 #define VALIDATE_DISPLAY(dpy,ret) \
     if ((dpy) != (EGLDisplay)&s_display) { \
-        getEGLThreadInfo()->eglError = EGL_BAD_DISPLAY; \
-        return ret; \
+        RETURN_ERROR(ret, EGL_BAD_DISPLAY);    \
     }
 
 #define VALIDATE_DISPLAY_INIT(dpy,ret) \
     VALIDATE_DISPLAY(dpy, ret)    \
     if (!s_display.initialized()) {        \
-        getEGLThreadInfo()->eglError = EGL_NOT_INITIALIZED;    \
-        return ret; \
+        RETURN_ERROR(ret, EGL_NOT_INITIALIZED);    \
     }
 
 #define DEFINE_HOST_CONNECTION \
@@ -75,9 +117,9 @@
     if (surface != EGL_NO_SURFACE) {    \
         egl_surface_t* s( static_cast<egl_surface_t*>(surface) );    \
         if (!s->isValid())    \
-            return setError(EGL_BAD_SURFACE, EGL_FALSE);    \
+            setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);    \
         if (s->dpy != (EGLDisplay)&s_display)    \
-            return setError(EGL_BAD_DISPLAY, EGL_FALSE);    \
+            setErrorReturn(EGL_BAD_DISPLAY, EGL_FALSE);    \
     }
 
 
@@ -91,13 +133,8 @@
         NEVER_CURRENT   =   0x00020000
     };
 
-    EGLContext_t(EGLDisplay dpy, EGLConfig config) : dpy(dpy), config(config),
-        read(EGL_NO_SURFACE), draw(EGL_NO_SURFACE),
-        rcContext(0) {
-            flags = 0;
-            version = 1;
-        };
-    ~EGLContext_t(){};
+    EGLContext_t(EGLDisplay dpy, EGLConfig config);
+    ~EGLContext_t();
     uint32_t            flags;
     EGLDisplay          dpy;
     EGLConfig           config;
@@ -105,8 +142,24 @@
     EGLSurface          draw;
     EGLint                version;
     uint32_t             rcContext;
+
+    GLClientState * getClientState(){ return clientState; }
+private:
+    GLClientState    *    clientState;
 };
 
+EGLContext_t::EGLContext_t(EGLDisplay dpy, EGLConfig config) : dpy(dpy), config(config),
+        read(EGL_NO_SURFACE), draw(EGL_NO_SURFACE), rcContext(0)
+{
+    flags = 0;
+    version = 1;
+    clientState = new GLClientState();
+};
+
+EGLContext_t::~EGLContext_t()
+{
+    if (clientState) delete clientState;
+}
 // ----------------------------------------------------------------------------
 //egl_surface_t
 
@@ -127,6 +180,7 @@
     virtual     EGLBoolean  connect() { return EGL_TRUE; }
     virtual     void        disconnect() {}
     virtual     EGLBoolean     swapBuffers() { return EGL_TRUE; }
+    virtual     EGLint        getSwapBehavior() const;
 
     void         setRcSurface(uint32_t handle){ rcSurface = handle; }
     uint32_t     getRcSurface(){ return rcSurface; }
@@ -169,6 +223,10 @@
     texTarget = EGL_NO_TEXTURE;
 }
 
+EGLint egl_surface_t::getSwapBehavior() const {
+        return EGL_BUFFER_PRESERVED;
+}
+
 egl_surface_t::~egl_surface_t()
 {
 }
@@ -221,7 +279,7 @@
 EGLBoolean egl_window_surface_t::rcCreate()
 {
     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
-    uint32_t rcSurface = rcEnc->rcCreateWindowSurface(rcEnc, (uint32_t)config, getWidth(), getHeight());
+    rcSurface = rcEnc->rcCreateWindowSurface(rcEnc, (uint32_t)config, getWidth(), getHeight());
     if (!rcSurface) {
         LOGE("rcCreateWindowSurface returned 0");
         return EGL_FALSE;
@@ -248,7 +306,7 @@
 {
     // dequeue a buffer
     if (nativeWindow->dequeueBuffer(nativeWindow, &buffer) != NO_ERROR) {
-        return setError(EGL_BAD_ALLOC, EGL_FALSE);
+        setErrorReturn(EGL_BAD_ALLOC, EGL_FALSE);
     }
     buffer->common.incRef(&buffer->common);
 
@@ -270,17 +328,19 @@
 EGLBoolean egl_window_surface_t::swapBuffers()
 {
     if (!buffer) {
-        return setError(EGL_BAD_ACCESS, EGL_FALSE);
+        setErrorReturn(EGL_BAD_ACCESS, EGL_FALSE);
     }
 
     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
 
+    rcEnc->rcFlushWindowColorBuffer(rcEnc, rcSurface);
+
     //post the back buffer
     nativeWindow->queueBuffer(nativeWindow, buffer);
 
     // dequeue a new buffer
     if (nativeWindow->dequeueBuffer(nativeWindow, &buffer)) {
-        return setError(EGL_BAD_ALLOC, EGL_FALSE);
+        setErrorReturn(EGL_BAD_ALLOC, EGL_FALSE);
     }
 
     rcEnc->rcSetWindowColorBuffer(rcEnc, rcSurface, ((cb_handle_t *)(buffer->handle))->hostHandle);
@@ -374,6 +434,8 @@
 static EGLClient_eglInterface s_eglIface = {
     getThreadInfo: getEGLThreadInfo
 };
+
+#define DBG_FUNC DBG("%s\n", __FUNCTION__)
 EGLDisplay eglGetDisplay(EGLNativeDisplayType display_id)
 {
     //
@@ -492,7 +554,7 @@
     }
 
     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
-    *num_config = rcEnc->rcChooseConfig(rcEnc, (EGLint*)attrib_list, attribs_size, (uint32_t*)configs, config_size);
+    *num_config = rcEnc->rcChooseConfig(rcEnc, (EGLint*)attrib_list, attribs_size * sizeof(EGLint), (uint32_t*)configs, config_size*sizeof(EGLint));
 
     return EGL_TRUE;
 }
@@ -517,27 +579,27 @@
     VALIDATE_DISPLAY_INIT(dpy, NULL);
     VALIDATE_CONFIG(config, EGL_FALSE);
     if (win == 0) {
-        return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
+        setErrorReturn(EGL_BAD_MATCH, EGL_NO_SURFACE);
     }
 
     EGLint surfaceType;
     if (s_display.getConfigAttrib(config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)    return EGL_FALSE;
 
     if (!(surfaceType & EGL_WINDOW_BIT)) {
-        return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
+        setErrorReturn(EGL_BAD_MATCH, EGL_NO_SURFACE);
     }
 
     if (static_cast<ANativeWindow*>(win)->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
-        return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
+        setErrorReturn(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
     }
 
     egl_surface_t* surface;
     surface = new egl_window_surface_t(&s_display, config, surfaceType, static_cast<ANativeWindow*>(win));
     if (!surface)
-        return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
+        setErrorReturn(EGL_BAD_ALLOC, EGL_NO_SURFACE);
     if (!surface->rcCreate()) {
         delete surface;
-        return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
+        setErrorReturn(EGL_BAD_ALLOC, EGL_NO_SURFACE);
     }
 
     return surface;
@@ -552,7 +614,7 @@
     if (s_display.getConfigAttrib(config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)    return EGL_FALSE;
 
     if (!(surfaceType & EGL_PBUFFER_BIT)) {
-        return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
+        setErrorReturn(EGL_BAD_MATCH, EGL_NO_SURFACE);
     }
 
     int32_t w = 0;
@@ -580,20 +642,20 @@
     }
     if (((texFormat == EGL_NO_TEXTURE)&&(texTarget != EGL_NO_TEXTURE)) ||
         ((texFormat != EGL_NO_TEXTURE)&&(texTarget == EGL_NO_TEXTURE))) {
-        return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
+        setErrorReturn(EGL_BAD_MATCH, EGL_NO_SURFACE);
     }
     // TODO: check EGL_TEXTURE_FORMAT - need to support eglBindTexImage
 
     GLenum pixelFormat;
-    if (s_display.getConfigPixelFormat(config, &pixelFormat) == EGL_FALSE)
-        return setError(EGL_BAD_MATCH, EGL_NO_SURFACE);
+    if (s_display.getConfigGLPixelFormat(config, &pixelFormat) == EGL_FALSE)
+        setErrorReturn(EGL_BAD_MATCH, EGL_NO_SURFACE);
 
     egl_surface_t* surface = new egl_pbuffer_surface_t(dpy, config, surfaceType, w, h, pixelFormat);
     if (!surface)
-        return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
+        setErrorReturn(EGL_BAD_ALLOC, EGL_NO_SURFACE);
     if (!surface->rcCreate()) {
         delete surface;
-        return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
+        setErrorReturn(EGL_BAD_ALLOC, EGL_NO_SURFACE);
     }
 
     //setup attributes
@@ -650,9 +712,13 @@
         case EGL_TEXTURE_TARGET:
             *value = surface->getTextureTarget();
             break;
+        case EGL_SWAP_BEHAVIOR:
+            *value = surface->getSwapBehavior();
+            break;
             //TODO: complete other attributes
         default:
-            ret = setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
+            LOGE("eglQuerySurface %x  EGL_BAD_ATTRIBUTE", attribute);
+            ret = setErrorFunc(EGL_BAD_ATTRIBUTE, EGL_FALSE);
             break;
     }
 
@@ -662,7 +728,7 @@
 EGLBoolean eglBindAPI(EGLenum api)
 {
     if (api != EGL_OPENGL_ES_API)
-        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
+        setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
     return EGL_TRUE;
 }
 
@@ -704,21 +770,21 @@
     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
     VALIDATE_SURFACE_RETURN(eglSurface, EGL_FALSE);
     if (eglSurface == EGL_NO_SURFACE) {
-        return setError(EGL_BAD_SURFACE, EGL_FALSE);
+        setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
     }
 
     if (buffer != EGL_BACK_BUFFER) {
-        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
+        setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
     }
 
     egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
 
     if (surface->getTextureFormat() == EGL_NO_TEXTURE) {
-        return setError(EGL_BAD_MATCH, EGL_FALSE);
+        setErrorReturn(EGL_BAD_MATCH, EGL_FALSE);
     }
 
     if (!(surface->getSurfaceType() & EGL_PBUFFER_BIT)) {
-        return setError(EGL_BAD_SURFACE, EGL_FALSE);
+        setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
     }
 
     //It's now safe to cast to pbuffer surface
@@ -752,7 +818,7 @@
     VALIDATE_CONFIG(config, EGL_NO_CONTEXT);
 
     EGLint version = 1; //default
-    while (attrib_list[0]) {
+    while (attrib_list && attrib_list[0]) {
         if (attrib_list[0] == EGL_CONTEXT_CLIENT_VERSION) version = attrib_list[1];
         attrib_list+=2;
     }
@@ -762,19 +828,19 @@
         EGLContext_t * shareCtx = static_cast<EGLContext_t*>(share_context);
         rcShareCtx = shareCtx->rcContext;
         if (shareCtx->dpy != dpy)
-            return setError(EGL_BAD_MATCH, EGL_NO_CONTEXT);
+            setErrorReturn(EGL_BAD_MATCH, EGL_NO_CONTEXT);
     }
 
     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_NO_CONTEXT);
     uint32_t rcContext = rcEnc->rcCreateContext(rcEnc, (uint32_t)config, rcShareCtx, version);
     if (!rcContext) {
         LOGE("rcCreateContext returned 0");
-        return setError(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
+        setErrorReturn(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
     }
 
     EGLContext_t * context = new EGLContext_t(dpy, config);
     if (!context)
-        return setError(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
+        setErrorReturn(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
 
     context->version = version;
     context->rcContext = rcContext;
@@ -812,9 +878,9 @@
     VALIDATE_SURFACE_RETURN(read, EGL_FALSE);
 
     if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
-        return setError(EGL_BAD_MATCH, EGL_FALSE);
+        setErrorReturn(EGL_BAD_MATCH, EGL_FALSE);
     if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
-        return setError(EGL_BAD_MATCH, EGL_FALSE);
+        setErrorReturn(EGL_BAD_MATCH, EGL_FALSE);
 
     EGLContext_t * context = static_cast<EGLContext_t*>(ctx);
     uint32_t ctxHandle = (context) ? context->rcContext : 0;
@@ -836,13 +902,13 @@
 
     if (context && (context->flags & EGLContext_t::IS_CURRENT) && (context != tInfo->currentContext)) {
         //context is current to another thread
-        return setError(EGL_BAD_ACCESS, EGL_FALSE);
+        setErrorReturn(EGL_BAD_ACCESS, EGL_FALSE);
     }
 
     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
     if (rcEnc->rcMakeCurrent(rcEnc, ctxHandle, drawHandle, readHandle) == EGL_FALSE) {
         LOGE("rcMakeCurrent returned EGL_FALSE");
-        return setError(EGL_BAD_CONTEXT, EGL_FALSE);
+        setErrorReturn(EGL_BAD_CONTEXT, EGL_FALSE);
     }
 
     //
@@ -858,15 +924,29 @@
         context->draw = draw;
         context->read = read;
         context->flags |= EGLContext_t::IS_CURRENT;
+        //set the client state
+        hostCon->glEncoder()->setClientState(context->getClientState());
     }
 
     if (tInfo->currentContext)
         tInfo->currentContext->flags &= ~EGLContext_t::IS_CURRENT;
 
+    //Check maybe we need to init the encoder, if it's first eglMakeCurrent
+    if (tInfo->currentContext) {
+        if (!hostCon->glEncoder()->isInitialized()) {
+            if (tInfo->currentContext->version == 2) {
+                s_display.gles2_iface()->init();
+            }
+            else {
+                s_display.gles_iface()->init();
+            }
+            hostCon->glEncoder()->setInitialized();
+        }
+    }
+
     //Now make current
     tInfo->currentContext = context;
 
-
     //connect the color buffer
     if (drawSurf)
         drawSurf->connect();
@@ -891,7 +971,7 @@
         case EGL_DRAW:
             return context->draw;
         default:
-            return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
+            setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
     }
 }
 
@@ -929,7 +1009,9 @@
                 *value = EGL_BACK_BUFFER; //single buffer not supported
             break;
         default:
-            return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
+            LOGE("eglQueryContext %x  EGL_BAD_ATTRIBUTE", attribute);
+            setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_FALSE);
+            break;
     }
 
     return ret;
@@ -961,15 +1043,15 @@
 {
     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
     if (eglSurface == EGL_NO_SURFACE)
-        return setError(EGL_BAD_SURFACE, EGL_FALSE);
+        setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
 
     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
 
     egl_surface_t* d = static_cast<egl_surface_t*>(eglSurface);
     if (!d->isValid())
-        return setError(EGL_BAD_SURFACE, EGL_FALSE);
+        setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
     if (d->dpy != dpy)
-        return setError(EGL_BAD_DISPLAY, EGL_FALSE);
+        setErrorReturn(EGL_BAD_DISPLAY, EGL_FALSE);
 
     // post the surface
     d->swapBuffers();
@@ -1001,19 +1083,19 @@
     VALIDATE_DISPLAY_INIT(dpy, EGL_NO_IMAGE_KHR);
 
     if (ctx != EGL_NO_CONTEXT) {
-        return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
+        setErrorReturn(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
     }
     if (target != EGL_NATIVE_BUFFER_ANDROID) {
-        return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
+        setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
     }
 
     android_native_buffer_t* native_buffer = (android_native_buffer_t*)buffer;
 
     if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
-        return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
+        setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
 
     if (native_buffer->common.version != sizeof(android_native_buffer_t))
-        return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
+        setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
 
     switch (native_buffer->format) {
         case HAL_PIXEL_FORMAT_RGBA_8888:
@@ -1025,7 +1107,7 @@
         case HAL_PIXEL_FORMAT_RGBA_4444:
             break;
         default:
-            return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
+            setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
     }
 
     native_buffer->common.incRef(&native_buffer->common);
@@ -1039,10 +1121,10 @@
     android_native_buffer_t* native_buffer = (android_native_buffer_t*)img;
 
     if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
-        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
+        setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
 
     if (native_buffer->common.version != sizeof(android_native_buffer_t))
-        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
+        setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
 
     native_buffer->common.decRef(&native_buffer->common);
 
diff --git a/tools/emulator/opengl/system/egl/eglDisplay.cpp b/tools/emulator/opengl/system/egl/eglDisplay.cpp
index 30974a7..78025b1 100644
--- a/tools/emulator/opengl/system/egl/eglDisplay.cpp
+++ b/tools/emulator/opengl/system/egl/eglDisplay.cpp
@@ -83,6 +83,7 @@
                                          &s_gles_lib);
         if (!m_gles_iface) {
             pthread_mutex_unlock(&m_lock);
+            LOGE("Failed to load gles1 iface");
             return false;
         }
 
@@ -100,6 +101,7 @@
         HostConnection *hcon = HostConnection::get();
         if (!hcon) {
             pthread_mutex_unlock(&m_lock);
+            LOGE("Failed to establish connection with the host\n");
             return false;
         }
 
@@ -109,6 +111,7 @@
         renderControl_encoder_context_t *rcEnc = hcon->rcEncoder();
         if (!rcEnc) {
             pthread_mutex_unlock(&m_lock);
+            LOGE("Failed to get renderControl encoder instance");
             return false;
         }
 
@@ -147,7 +150,7 @@
 
         uint32_t nInts = m_numConfigAttribs * (m_numConfigs + 1);
         EGLint tmp_buf[nInts];
-        m_configs = new EGLint[nInts-1];
+        m_configs = new EGLint[nInts-m_numConfigAttribs];
         if (!m_configs) {
             pthread_mutex_unlock(&m_lock);
             return false;
@@ -162,21 +165,34 @@
 
         //Fill the attributes vector.
         //The first m_numConfigAttribs values of tmp_buf are the actual attributes enums.
-        for (int i=0; i<m_numConfigAttribs; i++)
-        {
+        for (int i=0; i<m_numConfigAttribs; i++) {
             m_attribs.add(tmp_buf[i], i);
         }
 
         //Copy the actual configs data to m_configs
-        memcpy(m_configs, tmp_buf + m_numConfigAttribs, m_numConfigs*sizeof(EGLint));
+        memcpy(m_configs, tmp_buf + m_numConfigAttribs, m_numConfigs*m_numConfigAttribs*sizeof(EGLint));
 
         m_initialized = true;
     }
     pthread_mutex_unlock(&m_lock);
 
+    processConfigs();
+
     return true;
 }
 
+void eglDisplay::processConfigs()
+{
+    for (int i=0; i<m_numConfigs; i++) {
+        EGLConfig config = (EGLConfig)i;
+        //Setup the EGL_NATIVE_VISUAL_ID attribute
+        PixelFormat format;
+        if (getConfigNativePixelFormat(config, &format)) {
+            setConfigAttrib(config, EGL_NATIVE_VISUAL_ID, format);
+        }
+    }
+}
+
 void eglDisplay::terminate()
 {
     pthread_mutex_lock(&m_lock);
@@ -207,12 +223,14 @@
 {
     void *lib = dlopen(libName, RTLD_NOW);
     if (!lib) {
+        LOGE("Failed to dlopen %s", libName);
         return NULL;
     }
 
     init_emul_gles_t init_gles_func = (init_emul_gles_t)dlsym(lib,"init_emul_gles");
     if (!init_gles_func) {
-        dlclose((void*)libName);
+        LOGE("Failed to find init_emul_gles");
+        dlclose((void*)lib);
         return NULL;
     }
 
@@ -390,7 +408,41 @@
     return ret;
 }
 
-EGLBoolean eglDisplay::getConfigPixelFormat(EGLConfig config, GLenum * format)
+void eglDisplay::dumpConfig(EGLConfig config)
+{
+    EGLint value = 0;
+    DBG("^^^^^^^^^^ dumpConfig %d ^^^^^^^^^^^^^^^^^^", (int)config);
+    for (int i=0; i<m_numConfigAttribs; i++) {
+        getAttribValue(config, i, &value);
+        DBG("{%d}[%d] %d\n", (int)config, i, value);
+    }
+}
+
+/* To set the value of attribute <a> of config <c> use the following formula:
+ * *(m_configs + (int)c*m_numConfigAttribs + a) = value;
+ */
+EGLBoolean eglDisplay::setAttribValue(EGLConfig config, EGLint attribIdx, EGLint value)
+{
+    if (attribIdx == ATTRIBUTE_NONE)
+    {
+        LOGE("[%s] Bad attribute idx\n", __FUNCTION__);
+        return EGL_FALSE;
+    }
+    *(m_configs + (int)config*m_numConfigAttribs + attribIdx) = value;
+    return EGL_TRUE;
+}
+
+EGLBoolean eglDisplay::setConfigAttrib(EGLConfig config, EGLint attrib, EGLint value)
+{
+    //Though it seems that valueFor() is thread-safe, we don't take chanses
+    pthread_mutex_lock(&m_lock);
+    EGLBoolean ret = setAttribValue(config, m_attribs.valueFor(attrib), value);
+    pthread_mutex_unlock(&m_lock);
+    return ret;
+}
+
+
+EGLBoolean eglDisplay::getConfigNativePixelFormat(EGLConfig config, PixelFormat * format)
 {
     EGLint redSize, blueSize, greenSize, alphaSize;
 
@@ -404,11 +456,35 @@
     }
 
     //calculate the GL internal format
-    if ((redSize==8)&&(blueSize==8)&&(greenSize==8)&&(alphaSize==8)) *format = GL_RGBA;
-    else if ((redSize==8)&&(blueSize==8)&&(greenSize==8)&&(alphaSize==0)) *format = GL_RGB;
-    else if ((redSize==5)&&(blueSize==6)&&(greenSize==5)&&(alphaSize==0)) *format = GL_RGB565_OES;
-    else if ((redSize==5)&&(blueSize==5)&&(greenSize==5)&&(alphaSize==1)) *format = GL_RGB5_A1_OES;
-    else if ((redSize==4)&&(blueSize==4)&&(greenSize==4)&&(alphaSize==4)) *format = GL_RGBA4_OES;
+    if ((redSize==8)&&(greenSize==8)&&(blueSize==8)&&(alphaSize==8)) *format = PIXEL_FORMAT_RGBA_8888; //XXX: BGR?
+    else if ((redSize==8)&&(greenSize==8)&&(blueSize==8)&&(alphaSize==0)) *format = PIXEL_FORMAT_RGBX_8888; //XXX or PIXEL_FORMAT_RGB_888
+    else if ((redSize==5)&&(greenSize==6)&&(blueSize==5)&&(alphaSize==0)) *format = PIXEL_FORMAT_RGB_565;
+    else if ((redSize==5)&&(greenSize==5)&&(blueSize==5)&&(alphaSize==1)) *format = PIXEL_FORMAT_RGBA_5551;
+    else if ((redSize==4)&&(greenSize==4)&&(blueSize==4)&&(alphaSize==4)) *format = PIXEL_FORMAT_RGBA_4444;
+    else {
+        return EGL_FALSE;
+    }
+    return EGL_TRUE;
+}
+EGLBoolean eglDisplay::getConfigGLPixelFormat(EGLConfig config, GLenum * format)
+{
+    EGLint redSize, blueSize, greenSize, alphaSize;
+
+    if ( !(getAttribValue(config, m_attribs.valueFor(EGL_RED_SIZE), &redSize) &&
+        getAttribValue(config, m_attribs.valueFor(EGL_BLUE_SIZE), &blueSize) &&
+        getAttribValue(config, m_attribs.valueFor(EGL_GREEN_SIZE), &greenSize) &&
+        getAttribValue(config, m_attribs.valueFor(EGL_ALPHA_SIZE), &alphaSize)) )
+    {
+        LOGE("Couldn't find value for one of the pixel format attributes");
+        return EGL_FALSE;
+    }
+
+    //calculate the GL internal format
+    if ((redSize==8)&&(blueSize==8)&&(blueSize==8)&&(alphaSize==8)) *format = GL_RGBA;
+    else if ((redSize==8)&&(greenSize==8)&&(blueSize==8)&&(alphaSize==0)) *format = GL_RGB;
+    else if ((redSize==5)&&(greenSize==6)&&(blueSize==5)&&(alphaSize==0)) *format = GL_RGB565_OES;
+    else if ((redSize==5)&&(greenSize==5)&&(blueSize==5)&&(alphaSize==1)) *format = GL_RGB5_A1_OES;
+    else if ((redSize==4)&&(greenSize==4)&&(blueSize==4)&&(alphaSize==4)) *format = GL_RGBA4_OES;
     else return EGL_FALSE;
 
     return EGL_TRUE;
diff --git a/tools/emulator/opengl/system/egl/eglDisplay.h b/tools/emulator/opengl/system/egl/eglDisplay.h
index 6f36854..934c699 100644
--- a/tools/emulator/opengl/system/egl/eglDisplay.h
+++ b/tools/emulator/opengl/system/egl/eglDisplay.h
@@ -23,6 +23,8 @@
 #include "EGLClientIface.h"
 #include <utils/KeyedVector.h>
 
+#include <ui/PixelFormat.h>
+
 #define ATTRIBUTE_NONE -1
 //FIXME: are we in this namespace?
 using namespace android;
@@ -46,13 +48,18 @@
 
     int     getNumConfigs(){ return m_numConfigs; }
     EGLBoolean  getConfigAttrib(EGLConfig config, EGLint attrib, EGLint * value);
-    EGLBoolean getConfigPixelFormat(EGLConfig config, GLenum * format);
+    EGLBoolean  setConfigAttrib(EGLConfig config, EGLint attrib, EGLint value);
+    EGLBoolean getConfigGLPixelFormat(EGLConfig config, GLenum * format);
+    EGLBoolean getConfigNativePixelFormat(EGLConfig config, PixelFormat * format);
 
+    void     dumpConfig(EGLConfig config);
 private:
     EGLClient_glesInterface *loadGLESClientAPI(const char *libName,
                                                EGLClient_eglInterface *eglIface,
                                                void **libHandle);
     EGLBoolean getAttribValue(EGLConfig config, EGLint attribIdxi, EGLint * value);
+    EGLBoolean setAttribValue(EGLConfig config, EGLint attribIdxi, EGLint value);
+    void     processConfigs();
 
 private:
     pthread_mutex_t m_lock;
diff --git a/tools/emulator/opengl/system/gralloc/Android.mk b/tools/emulator/opengl/system/gralloc/Android.mk
index 7a7c340..9340185 100644
--- a/tools/emulator/opengl/system/gralloc/Android.mk
+++ b/tools/emulator/opengl/system/gralloc/Android.mk
@@ -31,12 +31,12 @@
 LOCAL_MODULE := gralloc.goldfish
 
 LOCAL_STATIC_LIBRARIES := \
-    libOpenglSystemCommon \
     libOpenglCodecCommon \
     libqemu
 
 LOCAL_SHARED_LIBRARIES := \
     libcutils \
+    libOpenglSystemCommon \
     libGLESv1_enc \
     lib_renderControl_enc
 
diff --git a/tools/emulator/opengl/system/gralloc/gralloc.cpp b/tools/emulator/opengl/system/gralloc/gralloc.cpp
index abc01a0..15c402f 100644
--- a/tools/emulator/opengl/system/gralloc/gralloc.cpp
+++ b/tools/emulator/opengl/system/gralloc/gralloc.cpp
@@ -29,6 +29,7 @@
 #include <cutils/log.h>
 
 
+#define DBG_FUNC DBG("%s\n", __FUNCTION__)
 //
 // our private gralloc module structure
 //
@@ -163,7 +164,7 @@
         *pStride = bpr / bpp;
     }
 
-    LOGD("gralloc_alloc ashmem_size=%d\n", ashmem_size);
+    LOGD("gralloc_alloc ashmem_size=%d, tid %d\n", ashmem_size, gettid());
 
     //
     // Allocate space in ashmem if needed
@@ -193,6 +194,8 @@
             delete cb;
             return err;
         }
+
+        cb->setFd(fd);
     }
 
     //
@@ -299,6 +302,12 @@
     return 0;
 }
 
+static int fb_compositionComplete(struct framebuffer_device_t* dev)
+{
+    LOGI("fb_compositionComplete");
+    return 0;
+}
+
 //
 // Framebuffer device functions
 //
@@ -650,6 +659,7 @@
         if (NULL == dev) {
             return -ENOMEM;
         }
+        memset(dev, 0, sizeof(fb_device_t));
 
         // Initialize our device structure
         //
@@ -659,7 +669,8 @@
         dev->device.common.close = fb_close;
         dev->device.setSwapInterval = fb_setSwapInterval;
         dev->device.post            = fb_post;
-        dev->device.setUpdateRect   = 0; //XXX: fb_setUpdateRect;
+        dev->device.setUpdateRect   = fb_setUpdateRect;
+        dev->device.compositionComplete = fb_compositionComplete; //XXX: this is a dummy
 
         const_cast<uint32_t&>(dev->device.flags) = 0;
         const_cast<uint32_t&>(dev->device.width) = width;
diff --git a/tools/emulator/opengl/system/renderControl_enc/Android.mk b/tools/emulator/opengl/system/renderControl_enc/Android.mk
index 497ec81..8b704cd 100644
--- a/tools/emulator/opengl/system/renderControl_enc/Android.mk
+++ b/tools/emulator/opengl/system/renderControl_enc/Android.mk
@@ -20,6 +20,8 @@
         libOpenglCodecCommon
 LOCAL_SHARED_LIBRARIES := libcutils
 
+LOCAL_CFLAGS += -DDEBUG_PRINTOUT -O0 -g
+
 RC_GEN := \
 	$(rc_intermediates)/renderControl_enc.cpp \
 	$(rc_intermediates)/renderControl_enc.h
diff --git a/tools/emulator/opengl/system/renderControl_enc/README b/tools/emulator/opengl/system/renderControl_enc/README
index b8ffc49..df870f6 100644
--- a/tools/emulator/opengl/system/renderControl_enc/README
+++ b/tools/emulator/opengl/system/renderControl_enc/README
@@ -81,6 +81,9 @@
 void rcDestroyColorBuffer(uint32_t colorbuffer);
        destroyes a colorBuffer object.
 
+void rcFlushWindowColorBuffer(uint32_t windowSurface, uint32_t colorBuffer);
+	   This flushes the current window color buffer
+
 void rcSetWindowColorBuffer(uint32_t windowSurface, uint32_t colorBuffer);
        This set the target color buffer for a windowSurface, when set the
        previous target colorBuffer gets updated before switching to the new
diff --git a/tools/emulator/opengl/system/renderControl_enc/renderControl.in b/tools/emulator/opengl/system/renderControl_enc/renderControl.in
index c91a662..505c6ce 100644
--- a/tools/emulator/opengl/system/renderControl_enc/renderControl.in
+++ b/tools/emulator/opengl/system/renderControl_enc/renderControl.in
@@ -12,6 +12,7 @@
 GL_ENTRY(uint32_t, rcCreateColorBuffer, uint32_t width, uint32_t height, GLenum internalFormat)
 GL_ENTRY(void, rcDestroyColorBuffer, uint32_t colorbuffer)
 GL_ENTRY(void, rcSetWindowColorBuffer, uint32_t windowSurface, uint32_t colorBuffer)
+GL_ENTRY(void, rcFlushWindowColorBuffer, uint32_t windowSurface)
 GL_ENTRY(EGLint, rcMakeCurrent, uint32_t context, uint32_t drawSurf, uint32_t readSurf)
 GL_ENTRY(void, rcFBPost, uint32_t colorBuffer)
 GL_ENTRY(void, rcFBSetSwapInterval, EGLint interval)
diff --git a/tools/emulator/opengl/tests/gles_android_wrapper/Android.mk b/tools/emulator/opengl/tests/gles_android_wrapper/Android.mk
index ad85036..f614798 100644
--- a/tools/emulator/opengl/tests/gles_android_wrapper/Android.mk
+++ b/tools/emulator/opengl/tests/gles_android_wrapper/Android.mk
@@ -127,8 +127,10 @@
 	libcutils \
 	libGLESv1_enc \
 	libGLESv2_enc \
-	libut_rendercontrol_enc
-LOCAL_STATIC_LIBRARIES := libOpenglCodecCommon libOpenglSystemCommon libqemu
+	libut_rendercontrol_enc \
+    libOpenglSystemCommon
+
+LOCAL_STATIC_LIBRARIES := libOpenglCodecCommon libqemu
 
 include $(BUILD_SHARED_LIBRARY)
 
@@ -139,6 +141,7 @@
 # version of this file to point to the appropriate HW EGL libraries.
 #
 ifneq (,$(filter full full_x86 sdk sdk_x86,$(TARGET_PRODUCT)))
+ifeq (,$(BUILD_EMULATOR_OPENGL_DRIVER))
 include $(CLEAR_VARS)
 
 LOCAL_MODULE := egl.cfg
@@ -149,6 +152,7 @@
 LOCAL_MODULE_CLASS := ETC
 
 include $(BUILD_PREBUILT)
+endif # building 'real' driver BUILD_EMULATOR_OPENGL_DRIVER
 endif # TARGET_PRODUCT in 'full sdk full_x86 sdk_x86'
 
 #### gles_emul.cfg ####