Merge change 9397

* changes:
  Fix Win32 libutils to get a working SDK build.
diff --git a/cmds/keystore/netkeystore.c b/cmds/keystore/netkeystore.c
index eac455e..637e0d8 100644
--- a/cmds/keystore/netkeystore.c
+++ b/cmds/keystore/netkeystore.c
@@ -37,6 +37,7 @@
 #include "netkeystore.h"
 #include "keymgmt.h"
 
+#define  DBG  1
 #define  CMD_PUT_WITH_FILE  "putfile"
 
 typedef void CMD_FUNC(LPC_MARSHAL *cmd, LPC_MARSHAL *reply);
@@ -397,12 +398,12 @@
 
         // read the command, execute and send the result back.
         if(read_marshal(s, &cmd)) goto err;
-        LOGI("new connection\n");
+        if (DBG) LOGD("new connection\n");
         execute(&cmd, &reply);
         write_marshal(s, &reply);
 err:
         memset(&reply, 0, sizeof(LPC_MARSHAL));
-        LOGI("closing connection\n");
+        if (DBG) LOGD("closing connection\n");
         close(s);
     }
 
diff --git a/libs/binder/Parcel.cpp b/libs/binder/Parcel.cpp
index f40e4bd..785a3c5 100644
--- a/libs/binder/Parcel.cpp
+++ b/libs/binder/Parcel.cpp
@@ -661,7 +661,7 @@
 
 status_t Parcel::writeNativeHandle(const native_handle* handle)
 {
-    if (handle->version != sizeof(native_handle))
+    if (!handle || handle->version != sizeof(native_handle))
         return BAD_TYPE;
 
     status_t err;
diff --git a/libs/ui/Surface.cpp b/libs/ui/Surface.cpp
index 04ab64c..a4710aa 100644
--- a/libs/ui/Surface.cpp
+++ b/libs/ui/Surface.cpp
@@ -558,14 +558,16 @@
 
     volatile const surface_info_t* const back = lcblk->surface + backIdx;
     if (back->flags & surface_info_t::eNeedNewBuffer) {
-        getBufferLocked(backIdx);
+        err = getBufferLocked(backIdx);
     }
 
-    const sp<SurfaceBuffer>& backBuffer(mBuffers[backIdx]);
-    mDirtyRegion.set(backBuffer->width, backBuffer->height);
-    *buffer = backBuffer.get();
+    if (err == NO_ERROR) {
+        const sp<SurfaceBuffer>& backBuffer(mBuffers[backIdx]);
+        mDirtyRegion.set(backBuffer->width, backBuffer->height);
+        *buffer = backBuffer.get();
+    }
   
-    return NO_ERROR;
+    return err;
 }
 
 int Surface::lockBuffer(android_native_buffer_t* buffer)
@@ -729,7 +731,7 @@
             currentBuffer.clear();
         }
         err = getBufferMapper().registerBuffer(buffer->handle);
-        LOGW_IF(err, "map(...) failed %d (%s)", err, strerror(-err));
+        LOGW_IF(err, "registerBuffer(...) failed %d (%s)", err, strerror(-err));
         if (err == NO_ERROR) {
             currentBuffer = buffer;
         }
diff --git a/opengl/libagl/egl.cpp b/opengl/libagl/egl.cpp
index b0e54d8..cf66be3 100644
--- a/opengl/libagl/egl.cpp
+++ b/opengl/libagl/egl.cpp
@@ -145,7 +145,7 @@
 
     virtual     EGLBoolean  bindDrawSurface(ogles_context_t* gl) = 0;
     virtual     EGLBoolean  bindReadSurface(ogles_context_t* gl) = 0;
-    virtual     void        connect() {}
+    virtual     EGLBoolean  connect() { return EGL_TRUE; }
     virtual     void        disconnect() {}
     virtual     EGLint      getWidth() const = 0;
     virtual     EGLint      getHeight() const = 0;
@@ -214,7 +214,7 @@
     virtual     EGLBoolean  swapBuffers();
     virtual     EGLBoolean  bindDrawSurface(ogles_context_t* gl);
     virtual     EGLBoolean  bindReadSurface(ogles_context_t* gl);
-    virtual     void        connect();
+    virtual     EGLBoolean  connect();
     virtual     void        disconnect();
     virtual     EGLint      getWidth() const    { return width;  }
     virtual     EGLint      getHeight() const   { return height; }
@@ -382,10 +382,12 @@
     }
 }
 
-void egl_window_surface_v2_t::connect() 
+EGLBoolean egl_window_surface_v2_t::connect() 
 {
     // dequeue a buffer
-    nativeWindow->dequeueBuffer(nativeWindow, &buffer);
+    if (nativeWindow->dequeueBuffer(nativeWindow, &buffer) != NO_ERROR) {
+        return setError(EGL_BAD_ALLOC, EGL_FALSE);
+    }
 
     // allocate a corresponding depth-buffer
     width = buffer->width;
@@ -396,8 +398,7 @@
         depth.stride  = depth.width; // use the width here
         depth.data    = (GGLubyte*)malloc(depth.stride*depth.height*2);
         if (depth.data == 0) {
-            setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
-            return;
+            return setError(EGL_BAD_ALLOC, EGL_FALSE);
         }
     }
 
@@ -411,9 +412,10 @@
             GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
         LOGE("connect() failed to lock buffer %p (%ux%u)",
                 buffer, buffer->width, buffer->height);
-        setError(EGL_BAD_ACCESS, EGL_NO_SURFACE);
+        return setError(EGL_BAD_ACCESS, EGL_FALSE);
         // FIXME: we should make sure we're not accessing the buffer anymore
     }
+    return EGL_TRUE;
 }
 
 void egl_window_surface_v2_t::disconnect() 
@@ -444,6 +446,7 @@
 
 status_t egl_window_surface_v2_t::unlock(android_native_buffer_t* buf)
 {
+    if (!buf) return BAD_VALUE;
     int err = module->unlock(module, buf->handle);
     return err;
 }
@@ -515,6 +518,10 @@
 
 EGLBoolean egl_window_surface_v2_t::swapBuffers()
 {
+    if (!buffer) {
+        return setError(EGL_BAD_ACCESS, EGL_FALSE);
+    }
+    
     /*
      * Handle eglSetSwapRectangleANDROID()
      * We copyback from the front buffer 
@@ -580,7 +587,7 @@
             GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) {
         LOGE("eglSwapBuffers() failed to lock buffer %p (%ux%u)",
                 buffer, buffer->width, buffer->height);
-        setError(EGL_BAD_ACCESS, EGL_NO_SURFACE);
+        return setError(EGL_BAD_ACCESS, EGL_FALSE);
         // FIXME: we should make sure we're not accessing the buffer anymore
     }
 
@@ -1748,7 +1755,9 @@
                 ogles_scissor(gl, 0, 0, w, h);
             }
             if (d) {
-                d->connect();
+                if (d->connect() == EGL_FALSE) {
+                    return EGL_FALSE;
+                }
                 d->ctx = ctx;
                 d->bindDrawSurface(gl);
             }
diff --git a/opengl/libs/EGL/egl.cpp b/opengl/libs/EGL/egl.cpp
index c2003dd..236d247 100644
--- a/opengl/libs/EGL/egl.cpp
+++ b/opengl/libs/EGL/egl.cpp
@@ -135,9 +135,10 @@
 
 struct tls_t
 {
-    tls_t() : error(EGL_SUCCESS), ctx(0) { }
+    tls_t() : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE) { }
     EGLint      error;
     EGLContext  ctx;
+    EGLBoolean  logCallWithNoContext;
 };
 
 
@@ -352,8 +353,14 @@
 }
 
 static void gl_no_context() {
-    LOGE("call to OpenGL ES API with no current context");
+    tls_t* tls = getTLS();
+    if (tls->logCallWithNoContext == EGL_TRUE) {
+        tls->logCallWithNoContext = EGL_FALSE;
+        LOGE("call to OpenGL ES API with no current context "
+             "(logged once per thread)");
+    }
 }
+
 static void early_egl_init(void) 
 {
 #if !USE_FAST_TLS_KEY