fix a bug where copybit only renders in the first buffer when used with s/w GL
diff --git a/opengl/libagl/TextureObjectManager.cpp b/opengl/libagl/TextureObjectManager.cpp
index 9deb2cf..255ccac 100644
--- a/opengl/libagl/TextureObjectManager.cpp
+++ b/opengl/libagl/TextureObjectManager.cpp
@@ -56,7 +56,7 @@
     generate_mipmap = GL_FALSE;
     direct = GL_FALSE;
 #ifdef LIBAGL_USE_GRALLOC_COPYBITS
-    copybits_fd = -1;
+    try_copybit = false;
 #endif // LIBAGL_USE_GRALLOC_COPYBITS
     buffer = 0;
 }
diff --git a/opengl/libagl/TextureObjectManager.h b/opengl/libagl/TextureObjectManager.h
index e0eadf1..496e694 100644
--- a/opengl/libagl/TextureObjectManager.h
+++ b/opengl/libagl/TextureObjectManager.h
@@ -80,7 +80,7 @@
     GLint               generate_mipmap;
     GLint               direct;
 #ifdef LIBAGL_USE_GRALLOC_COPYBITS
-    int                 copybits_fd;
+    bool                try_copybit;
 #endif // LIBAGL_USE_GRALLOC_COPYBITS
     android_native_buffer_t* buffer;
 };
diff --git a/opengl/libagl/copybit.cpp b/opengl/libagl/copybit.cpp
index 427e42a..a539a2b 100644
--- a/opengl/libagl/copybit.cpp
+++ b/opengl/libagl/copybit.cpp
@@ -27,23 +27,28 @@
 #include "primitives.h"
 #include "texture.h"
 #include "BufferObjectManager.h"
-
 #include "TextureObjectManager.h"
+
 #include <hardware/gralloc.h>
 #include <hardware/copybit.h>
+#include <private/ui/android_natives_priv.h>
 #include "gralloc_priv.h"
 
 // ----------------------------------------------------------------------------
 
 namespace android {
 
-static void textureToCopyBitImage(const GGLSurface* surface, int fd, copybit_image_t* img) {
+static void textureToCopyBitImage(
+        const GGLSurface* surface, buffer_handle_t buffer, copybit_image_t* img) 
+{
+    // we know private_handle_t is good here
+    private_handle_t* hnd = (private_handle_t*)buffer;
     img->w      = surface->stride;
     img->h      = surface->height;
     img->format = surface->format;
-    img->offset = 0;
+    img->offset = hnd->offset;
     img->base   = surface->data;
-    img->fd     = fd;
+    img->fd     = hnd->fd;
 }
 
 struct clipRectRegion : public copybit_region_t {
@@ -109,7 +114,7 @@
 
 static bool checkContext(ogles_context_t* c) {
 
-	// By convenction copybitQuickCheckContext() has already returned true.
+	// By convention copybitQuickCheckContext() has already returned true.
 	// avoid checking the same information again.
 	
     if (c->copybits.blitEngine == NULL
@@ -118,7 +123,7 @@
         return false;
     }
 
-    // Note: The drawSurfaceFd is only set for destination
+    // Note: The drawSurfaceBuffer is only set for destination
     // surfaces types that are supported by the hardware and
     // do not have an alpha channel. So we don't have to re-check that here.
 
@@ -237,18 +242,20 @@
     // LOGW("calling copybits");
 
     copybit_device_t* copybit = c->copybits.blitEngine;
+
     copybit_image_t dst;
-    textureToCopyBitImage(&cbSurface, c->copybits.drawSurfaceFd, &dst);
+    buffer_handle_t target_hnd = c->copybits.drawSurfaceBuffer;
+    textureToCopyBitImage(&cbSurface, target_hnd, &dst);
     copybit_rect_t drect = {x, y, x+w, y+h};
 
+    // we know private_handle_t is good here
     copybit_image_t src;
-    textureToCopyBitImage(&textureObject->surface, textureObject->copybits_fd,
-            &src);
+    buffer_handle_t source_hnd = textureObject->buffer->handle;
+    textureToCopyBitImage(&textureObject->surface, source_hnd, &src);
     copybit_rect_t srect = { Ucr, Vcr + Hcr, Ucr + Wcr, Vcr };
 
     copybit->set_parameter(copybit, COPYBIT_TRANSFORM, transform);
     copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, planeAlpha);
-
     copybit->set_parameter(copybit, COPYBIT_DITHER,
             (enables & GGL_ENABLE_DITHER) ? COPYBIT_ENABLE : COPYBIT_DISABLE);
 
diff --git a/opengl/libagl/copybit.h b/opengl/libagl/copybit.h
index 1888aee..401b68c 100644
--- a/opengl/libagl/copybit.h
+++ b/opengl/libagl/copybit.h
@@ -32,9 +32,9 @@
         GLsizei count);
 
 inline bool copybitQuickCheckContext(ogles_context_t* c) {
-        return  c->copybits.drawSurfaceFd >= 0
+        return  c->copybits.drawSurfaceBuffer != 0
             && c->rasterizer.state.enabled_tmu == 1
-            && c->textures.tmu[0].texture->copybits_fd >= 0;
+            && c->textures.tmu[0].texture->try_copybit;
 }
 
 /*
diff --git a/opengl/libagl/egl.cpp b/opengl/libagl/egl.cpp
index 0df2bba..c6d5057 100644
--- a/opengl/libagl/egl.cpp
+++ b/opengl/libagl/egl.cpp
@@ -551,20 +551,20 @@
     gl->rasterizer.procs.colorBuffer(gl, &buffer);
     if (depth.data != gl->rasterizer.state.buffers.depth.data)
         gl->rasterizer.procs.depthBuffer(gl, &depth);
+
 #ifdef LIBAGL_USE_GRALLOC_COPYBITS
-    gl->copybits.drawSurfaceFd = -1;
+    gl->copybits.drawSurfaceBuffer = 0;
     if (supportedCopybitsDestinationFormat(buffer.format)) {
         buffer_handle_t handle = this->buffer->handle;
         if (handle != NULL) {
             private_handle_t* hand = private_handle_t::dynamicCast(handle);
-            if (hand != NULL) {
-                if (hand->usesPhysicallyContiguousMemory()) {
-                    gl->copybits.drawSurfaceFd = hand->fd;
-                }
+            if (hand != NULL && hand->usesPhysicallyContiguousMemory()) {
+                gl->copybits.drawSurfaceBuffer = handle;
             }
         }
     }
 #endif // LIBAGL_USE_GRALLOC_COPYBITS
+
     return EGL_TRUE;
 }
 EGLBoolean egl_window_surface_v2_t::bindReadSurface(ogles_context_t* gl)
diff --git a/opengl/libagl/state.cpp b/opengl/libagl/state.cpp
index a00f779..a59b3b0 100644
--- a/opengl/libagl/state.cpp
+++ b/opengl/libagl/state.cpp
@@ -101,7 +101,7 @@
     c->copybits.blitEngine = NULL;
     c->copybits.minScale = 0;
     c->copybits.maxScale = 0;
-    c->copybits.drawSurfaceFd = -1;
+    c->copybits.drawSurfaceBuffer = 0;
 
 #ifdef LIBAGL_USE_GRALLOC_COPYBITS
     hw_module_t const* module;
diff --git a/opengl/libagl/texture.cpp b/opengl/libagl/texture.cpp
index d675107..05fd46e 100644
--- a/opengl/libagl/texture.cpp
+++ b/opengl/libagl/texture.cpp
@@ -1544,12 +1544,10 @@
      *
      */
 #ifdef LIBAGL_USE_GRALLOC_COPYBITS
-    tex->copybits_fd = -1;
-    private_handle_t* hand;
-    if ((hand = private_handle_t::dynamicCast(native_buffer->handle)) != NULL) {
-        if (hand->usesPhysicallyContiguousMemory()) {
-            tex->copybits_fd = hand->fd;
-        }
+    tex->try_copybit = false;
+    private_handle_t* hnd = private_handle_t::dynamicCast(native_buffer->handle);
+    if (hnd && hnd->usesPhysicallyContiguousMemory()) {
+        tex->try_copybit = true;
     }
 #endif // LIBAGL_USE_GRALLOC_COPYBITS
 }