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
 }
diff --git a/opengl/tests/copybits/copybits.cpp b/opengl/tests/copybits/copybits.cpp
index d15526c..f8ca9b2 100644
--- a/opengl/tests/copybits/copybits.cpp
+++ b/opengl/tests/copybits/copybits.cpp
@@ -96,13 +96,11 @@
     status_t initSize(uint32_t w, uint32_t h);
 
     ssize_t                 mInitCheck;
-    uint32_t                mFlags;
-    uint32_t                mVStride;
     void*                   mData;
 };
 
 Buffer::Buffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage)
-    : SurfaceBuffer(), mInitCheck(NO_INIT), mVStride(0)
+    : SurfaceBuffer(), mInitCheck(NO_INIT)
 {
     this->usage = usage;
     this->format = format;
@@ -137,7 +135,6 @@
         if (err == NO_ERROR) {
             width  = w;
             height = h;
-            mVStride = 0;
         }
     }
 
@@ -154,7 +151,6 @@
         sur->height = height;
         sur->stride = stride;
         sur->format = format;
-        sur->vstride = mVStride;
         sur->data = static_cast<GGLubyte*>(vaddr);
     }
     return res;
@@ -355,7 +351,7 @@
 // #define USE_ALPHA_COLOR
 
 #define USE_GL_REPLACE
-// #define USE_GL_MODULATE
+//#define USE_GL_MODULATE
 
 // #define USE_BLEND
 
@@ -479,7 +475,7 @@
 
 int scale(int base, int factor) {
     static const float kTable[SCALE_COUNT] = {
-            0.1f, 0.25f, 0.5f, 0.75f, 1.0f,
+            0.24f, 0.25f, 0.5f, 0.75f, 1.0f,
             1.5f, 2.0f, 2.5f, 3.0f, 3.5f, 4.0f, 5.0f
     };
     return base * kTable[factor];
@@ -570,10 +566,10 @@
         return -1;
     }
     // Need to do a dummy eglSwapBuffers first. Don't know why.
-    glClearColor(0.4, 1.0, 0.4, 0.4);
+    glClearColor(0.4, 1.0, 0.4, 1.0);
     glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
     eglSwapBuffers(eglDisplay, eglSurface);
-
+    
     int cropRect[4] = {0,HEIGHT,WIDTH,-HEIGHT}; // Left bottom width height. Width and Height can be neg to flip.
     glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
 
@@ -592,6 +588,8 @@
         }
 
         eglSwapBuffers(eglDisplay, eglSurface);
+        LOGD("wait 1s");
+        usleep(1000000);
     }
     return 0;
 }