opengles emulator: Fixed upside-down gl image

This fix performs a flip software copy to match our coords with those expected by the flinger.
We may think of a better implementation in the future.

Change-Id: Ic09a5d0e22f7e209b33c07c993a3d56e328dd3ed
diff --git a/tools/emulator/opengl/host/libs/libOpenglRender/ColorBuffer.cpp b/tools/emulator/opengl/host/libs/libOpenglRender/ColorBuffer.cpp
index 0a794ed..099eeea 100644
--- a/tools/emulator/opengl/host/libs/libOpenglRender/ColorBuffer.cpp
+++ b/tools/emulator/opengl/host/libs/libOpenglRender/ColorBuffer.cpp
@@ -231,10 +231,10 @@
                          +1.0f, -1.0f, 0.0f,
                          +1.0f, +1.0f, 0.0f };
 
-    GLfloat tcoords[] = { 0.0f, 0.0f,
-                           0.0f, 1.0f,
-                           1.0f, 0.0f,
-                           1.0f, 1.0f };
+    GLfloat tcoords[] = { 0.0f, 1.0f,
+                           0.0f, 0.0f,
+                           1.0f, 1.0f,
+                           1.0f, 0.0f };
 
     s_gl.glClientActiveTexture(GL_TEXTURE0);
     s_gl.glEnableClientState(GL_TEXTURE_COORD_ARRAY);
diff --git a/tools/emulator/opengl/host/libs/libOpenglRender/WindowSurface.cpp b/tools/emulator/opengl/host/libs/libOpenglRender/WindowSurface.cpp
index 21b92e2..0b12604 100644
--- a/tools/emulator/opengl/host/libs/libOpenglRender/WindowSurface.cpp
+++ b/tools/emulator/opengl/host/libs/libOpenglRender/WindowSurface.cpp
@@ -227,8 +227,27 @@
                           GL_RGBA, GL_UNSIGNED_BYTE, data);
     }
 
+#define FLIP_BUFFER 1
+#if FLIP_BUFFER
+    //We need to flip the pixels
+    int bpp = 4;
+    void *tmpBuf = m_xUpdateBuf.alloc(m_width * m_height * bpp);
+
+    int dst_line_len = m_width * bpp;
+    int src_line_len = m_width * bpp;
+    char *src = (char *)data;
+    char *dst = (char*)tmpBuf + (m_height-1)*dst_line_len;
+    for (uint32_t  y=0; y<m_height; y++) {
+        memcpy(dst, src, dst_line_len);
+        src += src_line_len;
+        dst -= dst_line_len;
+    }
+    // update the attached color buffer with the fliped readback pixels
+    m_attachedColorBuffer->update(GL_RGBA, GL_UNSIGNED_BYTE, tmpBuf);
+#else
     // update the attached color buffer with the readback pixels
     m_attachedColorBuffer->update(GL_RGBA, GL_UNSIGNED_BYTE, data);
+#endif
 
     // restore current context/surface
     s_egl.eglMakeCurrent(fb->getDisplay(), prevDrawSurf,
diff --git a/tools/emulator/opengl/host/libs/libOpenglRender/WindowSurface.h b/tools/emulator/opengl/host/libs/libOpenglRender/WindowSurface.h
index 2a496df..73ef86b 100644
--- a/tools/emulator/opengl/host/libs/libOpenglRender/WindowSurface.h
+++ b/tools/emulator/opengl/host/libs/libOpenglRender/WindowSurface.h
@@ -58,6 +58,7 @@
     bool m_useEGLImage;
     bool m_useBindToTexture;
     FixedBuffer m_xferBuffer;
+    FixedBuffer m_xUpdateBuf;
 };
 
 typedef SmartPtr<WindowSurface> WindowSurfacePtr;