Merge change Ia67d5388 into eclair-mr2

* changes:
  remote destructors are not synchronously executed by the binder...
diff --git a/include/utils/ResourceTypes.h b/include/utils/ResourceTypes.h
index a845908..6090f60 100644
--- a/include/utils/ResourceTypes.h
+++ b/include/utils/ResourceTypes.h
@@ -454,6 +454,10 @@
 
     size_t size() const;
 
+#ifndef HAVE_ANDROID_OS
+    bool isUTF8() const;
+#endif
+
 private:
     status_t                    mError;
     void*                       mOwnedData;
diff --git a/libs/surfaceflinger/Layer.cpp b/libs/surfaceflinger/Layer.cpp
index f11bf18..1870d3a 100644
--- a/libs/surfaceflinger/Layer.cpp
+++ b/libs/surfaceflinger/Layer.cpp
@@ -93,6 +93,7 @@
 status_t Layer::ditch()
 {
     // the layer is not on screen anymore. free as much resources as possible
+    mFreezeLock.clear();
     destroy();
     return NO_ERROR;
 }
@@ -134,7 +135,14 @@
 {
     Mutex::Autolock _l(mLock);
     sp<GraphicBuffer> buffer(getFrontBufferLocked());
-    int index = mFrontBufferIndex;
+    if (buffer == NULL) {
+        // this situation can happen if we ran out of memory for instance.
+        // not much we can do. continue to use whatever texture was bound
+        // to this context.
+        return;
+    }
+
+    const int index = mFrontBufferIndex;
 
     // create the new texture name if needed
     if (UNLIKELY(mTextures[index].name == -1U)) {
diff --git a/libs/surfaceflinger/LayerBuffer.cpp b/libs/surfaceflinger/LayerBuffer.cpp
index 137c5c0..2ff6167 100644
--- a/libs/surfaceflinger/LayerBuffer.cpp
+++ b/libs/surfaceflinger/LayerBuffer.cpp
@@ -364,43 +364,6 @@
         return;
     }
 
-    if (mLayer.mBlitEngine) {
-        // create our temporary buffer and corresponding EGLImageKHR.
-        // note that the size of this buffer doesn't really matter,
-        // the final image will always be drawn with proper aspect ratio.
-
-        int w = layer.mTransformedBounds.width();
-        int h = layer.mTransformedBounds.height();
-        if (buffers.w * h != buffers.h * w) {
-            int t = w; w = h; h = t;
-        }
-        if (buffers.w * h == buffers.h * w) {
-            // same pixel area, don't use filtering
-            layer.mUseLinearFiltering = false;
-        }
-
-        mTempGraphicBuffer.clear();
-        mTempGraphicBuffer = new GraphicBuffer(
-                w, h, HAL_PIXEL_FORMAT_RGB_565,
-                GraphicBuffer::USAGE_HW_TEXTURE |
-                GraphicBuffer::USAGE_HW_2D);
-
-        if (mTempGraphicBuffer->initCheck() == NO_ERROR) {
-            NativeBuffer& dst(mTempBuffer);
-            dst.img.w = mTempGraphicBuffer->getStride();
-            dst.img.h = h;
-            dst.img.format = mTempGraphicBuffer->getPixelFormat();
-            dst.img.handle = (native_handle_t *)mTempGraphicBuffer->handle;
-            dst.img.base = 0;
-            dst.crop.l = 0;
-            dst.crop.t = 0;
-            dst.crop.r = w;
-            dst.crop.b = h;
-        } else {
-            mTempGraphicBuffer.clear();
-        }
-    }
-
     mBufferHeap = buffers;
     mLayer.setNeedsBlending((info.h_alpha - info.l_alpha) > 0);    
     mBufferSize = info.getScanlineSize(buffers.hor_stride)*buffers.ver_stride;
@@ -492,18 +455,7 @@
         copybit_device_t* copybit = mLayer.mBlitEngine;
         if (copybit) {
             // create our EGLImageKHR the first time
-            if (mTexture.image == EGL_NO_IMAGE_KHR) {
-                err = NO_MEMORY;
-                if (mTempGraphicBuffer!=0) {
-                    err = mLayer.initializeEglImage(
-                            mTempGraphicBuffer, &mTexture);
-                    // once the EGLImage has been created (whether it fails
-                    // or not) we don't need the graphic buffer reference
-                    // anymore.
-                    mTempGraphicBuffer.clear();
-                }
-            }
-
+            err = initTempBuffer();
             if (err == NO_ERROR) {
                 // NOTE: Assume the buffer is allocated with the proper USAGE flags
                 const NativeBuffer& dst(mTempBuffer);
@@ -542,6 +494,72 @@
     mLayer.drawWithOpenGL(clip, mTexture);
 }
 
+status_t LayerBuffer::BufferSource::initTempBuffer() const
+{
+    // figure out the size we need now
+    const ISurface::BufferHeap& buffers(mBufferHeap);
+    uint32_t w = mLayer.mTransformedBounds.width();
+    uint32_t h = mLayer.mTransformedBounds.height();
+    if (buffers.w * h != buffers.h * w) {
+        int t = w; w = h; h = t;
+    }
+
+    if (mTexture.image != EGL_NO_IMAGE_KHR) {
+        // we have an EGLImage, make sure the needed size didn't change
+        if (w!=mTexture.width || h!= mTexture.height) {
+            // delete the EGLImage and texture
+            EGLDisplay dpy(mLayer.mFlinger->graphicPlane(0).getEGLDisplay());
+            glDeleteTextures(1, &mTexture.name);
+            eglDestroyImageKHR(dpy, mTexture.image);
+            Texture defaultTexture;
+            mTexture = defaultTexture;
+            mTempGraphicBuffer.clear();
+        } else {
+            // we're good, we have an EGLImageKHR and it's (still) the
+            // right size
+            return NO_ERROR;
+        }
+    }
+
+    // figure out if we need linear filtering
+    if (buffers.w * h == buffers.h * w) {
+        // same pixel area, don't use filtering
+        mLayer.mUseLinearFiltering = false;
+    }
+
+    // Allocate a temporary buffer and create the corresponding EGLImageKHR
+
+    status_t err;
+    mTempGraphicBuffer.clear();
+    mTempGraphicBuffer = new GraphicBuffer(
+            w, h, HAL_PIXEL_FORMAT_RGB_565,
+            GraphicBuffer::USAGE_HW_TEXTURE |
+            GraphicBuffer::USAGE_HW_2D);
+
+    err = mTempGraphicBuffer->initCheck();
+    if (err == NO_ERROR) {
+        NativeBuffer& dst(mTempBuffer);
+        dst.img.w = mTempGraphicBuffer->getStride();
+        dst.img.h = h;
+        dst.img.format = mTempGraphicBuffer->getPixelFormat();
+        dst.img.handle = (native_handle_t *)mTempGraphicBuffer->handle;
+        dst.img.base = 0;
+        dst.crop.l = 0;
+        dst.crop.t = 0;
+        dst.crop.r = w;
+        dst.crop.b = h;
+
+        err = mLayer.initializeEglImage(
+                mTempGraphicBuffer, &mTexture);
+        // once the EGLImage has been created (whether it fails
+        // or not) we don't need the graphic buffer reference
+        // anymore.
+        mTempGraphicBuffer.clear();
+    }
+
+    return err;
+}
+
 // ---------------------------------------------------------------------------
 
 LayerBuffer::OverlaySource::OverlaySource(LayerBuffer& layer,
diff --git a/libs/surfaceflinger/LayerBuffer.h b/libs/surfaceflinger/LayerBuffer.h
index 1abb103..2ca63ac 100644
--- a/libs/surfaceflinger/LayerBuffer.h
+++ b/libs/surfaceflinger/LayerBuffer.h
@@ -130,13 +130,14 @@
         virtual bool transformed() const;
         virtual void destroy() { }
     private:
+        status_t initTempBuffer() const;
         mutable Mutex                   mBufferSourceLock;
         sp<Buffer>                      mBuffer;
         status_t                        mStatus;
         ISurface::BufferHeap            mBufferHeap;
         size_t                          mBufferSize;
         mutable LayerBase::Texture      mTexture;
-        NativeBuffer                    mTempBuffer;
+        mutable NativeBuffer            mTempBuffer;
         mutable sp<GraphicBuffer>       mTempGraphicBuffer;
     };
     
diff --git a/libs/utils/ResourceTypes.cpp b/libs/utils/ResourceTypes.cpp
index afca814..e8bd5cf 100644
--- a/libs/utils/ResourceTypes.cpp
+++ b/libs/utils/ResourceTypes.cpp
@@ -576,6 +576,13 @@
     return (mError == NO_ERROR) ? mHeader->stringCount : 0;
 }
 
+#ifndef HAVE_ANDROID_OS
+bool ResStringPool::isUTF8() const
+{
+    return (mHeader->flags&ResStringPool_header::UTF8_FLAG)!=0;
+}
+#endif
+
 // --------------------------------------------------------------------
 // --------------------------------------------------------------------
 // --------------------------------------------------------------------
@@ -4016,7 +4023,8 @@
         if (str == NULL) {
             printf("(string) null\n");
         } else {
-            printf("(string) \"%s\"\n",
+            printf("(string%d) \"%s\"\n",
+                    pkg->header->values.isUTF8()?8:16,
                     String8(str, len).string());
         } 
     } else if (value.dataType == Res_value::TYPE_FLOAT) {