emulator opengl: Fix gralloc color buffer uploads

Use the correct OpenGL pixel type enum when uploading
color buffer content to host.

Change-Id: Idc859306c0398850505f0f348796bc4f12b63b6a
diff --git a/tools/emulator/opengl/system/OpenglSystemCommon/gralloc_cb.h b/tools/emulator/opengl/system/OpenglSystemCommon/gralloc_cb.h
index 03b9b6d..117378d 100644
--- a/tools/emulator/opengl/system/OpenglSystemCommon/gralloc_cb.h
+++ b/tools/emulator/opengl/system/OpenglSystemCommon/gralloc_cb.h
@@ -14,13 +14,15 @@
 struct cb_handle_t : public native_handle {
 
     cb_handle_t(int p_fd, int p_ashmemSize, int p_usage,
-                int p_width, int p_height, int p_glFormat) :
+                int p_width, int p_height,
+                int p_glFormat, int p_glType) :
         fd(p_fd),
         magic(BUFFER_HANDLE_MAGIC),
         usage(p_usage),
         width(p_width),
         height(p_height),
         glFormat(p_glFormat),
+        glType(p_glType),
         ashmemSize(p_ashmemSize),
         ashmemBase(NULL),
         ashmemBasePid(0),
@@ -70,6 +72,7 @@
     int width;              // buffer width
     int height;             // buffer height
     int glFormat;           // OpenGL format enum used for host h/w color buffer
+    int glType;             // OpenGL type enum used when uploading to host
     int ashmemSize;         // ashmem region size for the buffer (0 unless is HW_FB buffer or
                             //                                    s/w access is needed)
     int ashmemBase;         // CPU address of the mapped ashmem region
diff --git a/tools/emulator/opengl/system/gralloc/gralloc.cpp b/tools/emulator/opengl/system/gralloc/gralloc.cpp
index 103bed5..04a8f2f 100644
--- a/tools/emulator/opengl/system/gralloc/gralloc.cpp
+++ b/tools/emulator/opengl/system/gralloc/gralloc.cpp
@@ -121,6 +121,7 @@
     int ashmem_size = 0;
     *pStride = 0;
     GLenum glFormat = 0;
+    GLenum glType = 0;
 
     int bpp = 0;
     switch (format) {
@@ -129,22 +130,27 @@
         case HAL_PIXEL_FORMAT_BGRA_8888:
             bpp = 4;
             glFormat = GL_RGBA;
+            glType = GL_UNSIGNED_BYTE;
             break;
         case HAL_PIXEL_FORMAT_RGB_888:
             bpp = 3;
             glFormat = GL_RGB;
+            glType = GL_UNSIGNED_BYTE;
             break;
         case HAL_PIXEL_FORMAT_RGB_565:
             bpp = 2;
             glFormat = GL_RGB565_OES;
+            glType = GL_UNSIGNED_SHORT_5_6_5;
             break;
         case HAL_PIXEL_FORMAT_RGBA_5551:
             bpp = 2;
             glFormat = GL_RGB5_A1_OES;
+            glType = GL_UNSIGNED_SHORT_5_5_5_1;
             break;
         case HAL_PIXEL_FORMAT_RGBA_4444:
             bpp = 2;
             glFormat = GL_RGBA4_OES;
+            glType = GL_UNSIGNED_SHORT_4_4_4_4;
             break;
 
         default:
@@ -181,7 +187,8 @@
         }
     }
 
-    cb_handle_t *cb = new cb_handle_t(fd, ashmem_size, usage, w, h, glFormat);
+    cb_handle_t *cb = new cb_handle_t(fd, ashmem_size, usage,
+                                      w, h, glFormat, glType);
 
     if (ashmem_size > 0) {
         //
@@ -556,7 +563,7 @@
         }
 
         if (cb->lockedWidth < cb->width || cb->lockedHeight < cb->height) {
-            int bpp = glUtilsPixelBitSize(cb->glFormat, GL_UNSIGNED_BYTE) >> 3;
+            int bpp = glUtilsPixelBitSize(cb->glFormat, cb->glType) >> 3;
             char *tmpBuf = new char[cb->lockedWidth * cb->lockedHeight * bpp];
 
             int dst_line_len = cb->lockedWidth * bpp;
@@ -572,7 +579,7 @@
             rcEnc->rcUpdateColorBuffer(rcEnc, cb->hostHandle,
                                        cb->lockedLeft, cb->lockedTop,
                                        cb->lockedWidth, cb->lockedHeight,
-                                       cb->glFormat, GL_UNSIGNED_BYTE,
+                                       cb->glFormat, cb->glType,
                                        tmpBuf);
 
             delete [] tmpBuf;
@@ -580,7 +587,7 @@
         else {
             rcEnc->rcUpdateColorBuffer(rcEnc, cb->hostHandle, 0, 0,
                                        cb->width, cb->height,
-                                       cb->glFormat, GL_UNSIGNED_BYTE,
+                                       cb->glFormat, cb->glType,
                                        cpu_addr);
         }
     }