Merge "Throw an exception when trying to upload a recycled bitmap."
diff --git a/include/gui/ISurfaceTexture.h b/include/gui/ISurfaceTexture.h
index 77d37f1..168310c 100644
--- a/include/gui/ISurfaceTexture.h
+++ b/include/gui/ISurfaceTexture.h
@@ -72,6 +72,12 @@
 
     virtual status_t setCrop(const Rect& reg) = 0;
     virtual status_t setTransform(uint32_t transform) = 0;
+
+    // getAllocator retrieves the binder object that must be referenced as long
+    // as the GraphicBuffers dequeued from this ISurfaceTexture are referenced.
+    // Holding this binder reference prevents SurfaceFlinger from freeing the
+    // buffers before the client is done with them.
+    virtual sp<IBinder> getAllocator() = 0;
 };
 
 // ----------------------------------------------------------------------------
diff --git a/include/gui/SurfaceTexture.h b/include/gui/SurfaceTexture.h
index 79c33f5..31615d0 100644
--- a/include/gui/SurfaceTexture.h
+++ b/include/gui/SurfaceTexture.h
@@ -101,6 +101,12 @@
     // when a new frame becomes available.
     void setFrameAvailableListener(const sp<FrameAvailableListener>& l);
 
+    // getAllocator retrieves the binder object that must be referenced as long
+    // as the GraphicBuffers dequeued from this SurfaceTexture are referenced.
+    // Holding this binder reference prevents SurfaceFlinger from freeing the
+    // buffers before the client is done with them.
+    sp<IBinder> getAllocator();
+
 private:
 
     // freeAllBuffers frees the resources (both GraphicBuffer and EGLImage) for
diff --git a/include/gui/SurfaceTextureClient.h b/include/gui/SurfaceTextureClient.h
index dd1d490..ff2251d 100644
--- a/include/gui/SurfaceTextureClient.h
+++ b/include/gui/SurfaceTextureClient.h
@@ -83,6 +83,10 @@
     // interactions with the server using this interface.
     sp<ISurfaceTexture> mSurfaceTexture;
 
+    // mAllocator is the binder object that is referenced to prevent the
+    // dequeued buffers from being freed prematurely.
+    sp<IBinder> mAllocator;
+
     // mSlots stores the buffers that have been allocated for each buffer slot.
     // It is initialized to null pointers, and gets filled in with the result of
     // ISurfaceTexture::requestBuffer when the client dequeues a buffer from a
diff --git a/libs/gui/ISurfaceTexture.cpp b/libs/gui/ISurfaceTexture.cpp
index 90bca3c..d661fd5 100644
--- a/libs/gui/ISurfaceTexture.cpp
+++ b/libs/gui/ISurfaceTexture.cpp
@@ -38,6 +38,7 @@
     CANCEL_BUFFER,
     SET_CROP,
     SET_TRANSFORM,
+    GET_ALLOCATOR,
 };
 
 
@@ -123,6 +124,13 @@
         status_t result = reply.readInt32();
         return result;
     }
+
+    virtual sp<IBinder> getAllocator() {
+        Parcel data, reply;
+        data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor());
+        remote()->transact(GET_ALLOCATOR, data, &reply);
+        return reply.readStrongBinder();
+    }
 };
 
 IMPLEMENT_META_INTERFACE(SurfaceTexture, "android.gui.SurfaceTexture");
@@ -195,6 +203,12 @@
             reply->writeInt32(result);
             return NO_ERROR;
         } break;
+        case GET_ALLOCATOR: {
+            CHECK_INTERFACE(ISurfaceTexture, data, reply);
+            sp<IBinder> result = getAllocator();
+            reply->writeStrongBinder(result);
+            return NO_ERROR;
+        } break;
     }
     return BBinder::onTransact(code, data, reply, flags);
 }
diff --git a/libs/gui/SurfaceTexture.cpp b/libs/gui/SurfaceTexture.cpp
index 236ff4f..6466f2b 100644
--- a/libs/gui/SurfaceTexture.cpp
+++ b/libs/gui/SurfaceTexture.cpp
@@ -312,6 +312,11 @@
     mFrameAvailableListener = l;
 }
 
+sp<IBinder> SurfaceTexture::getAllocator() {
+    LOGV("SurfaceTexture::getAllocator");
+    return mGraphicBufferAlloc->asBinder();
+}
+
 void SurfaceTexture::freeAllBuffers() {
     for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
         mSlots[i].mGraphicBuffer = 0;
diff --git a/libs/gui/SurfaceTextureClient.cpp b/libs/gui/SurfaceTextureClient.cpp
index 50cbdb8..ee14ac9 100644
--- a/libs/gui/SurfaceTextureClient.cpp
+++ b/libs/gui/SurfaceTextureClient.cpp
@@ -25,8 +25,8 @@
 
 SurfaceTextureClient::SurfaceTextureClient(
         const sp<ISurfaceTexture>& surfaceTexture):
-        mSurfaceTexture(surfaceTexture), mReqWidth(1), mReqHeight(1),
-        mReqFormat(DEFAULT_FORMAT), mReqUsage(0), mMutex() {
+        mSurfaceTexture(surfaceTexture), mAllocator(0), mReqWidth(1),
+        mReqHeight(1), mReqFormat(DEFAULT_FORMAT), mReqUsage(0), mMutex() {
     // Initialize the ANativeWindow function pointers.
     ANativeWindow::setSwapInterval  = setSwapInterval;
     ANativeWindow::dequeueBuffer    = dequeueBuffer;
@@ -35,6 +35,9 @@
     ANativeWindow::queueBuffer      = queueBuffer;
     ANativeWindow::query            = query;
     ANativeWindow::perform          = perform;
+
+    // Get a reference to the allocator.
+    mAllocator = mSurfaceTexture->getAllocator();
 }
 
 int SurfaceTextureClient::setSwapInterval(ANativeWindow* window, int interval) {
diff --git a/services/surfaceflinger/LayerDim.cpp b/services/surfaceflinger/LayerDim.cpp
index 11f8feb..f79166d 100644
--- a/services/surfaceflinger/LayerDim.cpp
+++ b/services/surfaceflinger/LayerDim.cpp
@@ -30,29 +30,12 @@
 namespace android {
 // ---------------------------------------------------------------------------
 
-bool LayerDim::sUseTexture;
-GLuint LayerDim::sTexId;
-EGLImageKHR LayerDim::sImage;
-int32_t LayerDim::sWidth;
-int32_t LayerDim::sHeight;
-
-// ---------------------------------------------------------------------------
-
 LayerDim::LayerDim(SurfaceFlinger* flinger, DisplayID display,
         const sp<Client>& client)
     : LayerBaseClient(flinger, display, client)
 {
 }
 
-void LayerDim::initDimmer(SurfaceFlinger* flinger, uint32_t w, uint32_t h)
-{
-    sTexId = -1;
-    sImage = EGL_NO_IMAGE_KHR;
-    sWidth = w;
-    sHeight = h;
-    sUseTexture = false;
-}
-
 LayerDim::~LayerDim()
 {
 }
@@ -84,15 +67,7 @@
 #endif
         glDisable(GL_TEXTURE_2D);
 
-        GLshort w = sWidth;
-        GLshort h = sHeight;
-        const GLshort vertices[4][2] = {
-                { 0, 0 },
-                { 0, h },
-                { w, h },
-                { w, 0 }
-        };
-        glVertexPointer(2, GL_SHORT, 0, vertices);
+        glVertexPointer(2, GL_FLOAT, 0, mVertices);
 
         while (it != end) {
             const Rect& r = *it++;
diff --git a/services/surfaceflinger/LayerDim.h b/services/surfaceflinger/LayerDim.h
index a04a0c0..75f9a89 100644
--- a/services/surfaceflinger/LayerDim.h
+++ b/services/surfaceflinger/LayerDim.h
@@ -31,11 +31,6 @@
 
 class LayerDim : public LayerBaseClient
 {
-    static bool sUseTexture;
-    static GLuint sTexId;
-    static EGLImageKHR sImage;
-    static int32_t sWidth;
-    static int32_t sHeight;
 public:    
                 LayerDim(SurfaceFlinger* flinger, DisplayID display,
                         const sp<Client>& client);
@@ -47,8 +42,6 @@
     virtual bool isProtectedByApp() const { return false; }
     virtual bool isProtectedByDRM() const { return false; }
     virtual const char* getTypeId() const { return "LayerDim"; }
-
-    static void initDimmer(SurfaceFlinger* flinger, uint32_t w, uint32_t h);
 };
 
 // ---------------------------------------------------------------------------
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 87b66ea..fd3f0c2 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -259,8 +259,6 @@
     glLoadIdentity();
     glOrthof(0, w, h, 0, 0, 1);
 
-   LayerDim::initDimmer(this, w, h);
-
     mReadyToRunBarrier.open();
 
     /*