Add support for drawBitmapMesh().

Change-Id: Ic77f9c534bb90dc7b9458299544bd50b8b6ae6a5
diff --git a/libs/hwui/DisplayListRenderer.cpp b/libs/hwui/DisplayListRenderer.cpp
index a74a95f..bdf056c 100644
--- a/libs/hwui/DisplayListRenderer.cpp
+++ b/libs/hwui/DisplayListRenderer.cpp
@@ -100,6 +100,7 @@
     "DrawBitmap",
     "DrawBitmapMatrix",
     "DrawBitmapRect",
+    "DrawBitmapMesh",
     "DrawPatch",
     "DrawColor",
     "DrawRect",
@@ -308,6 +309,19 @@
                         getFloat(), getFloat(), getFloat(), getFloat(), getPaint());
             }
             break;
+            case DrawBitmapMesh: {
+                int verticesCount = 0;
+                uint32_t colorsCount = 0;
+
+                SkBitmap* bitmap = getBitmap();
+                uint32_t meshWidth = getInt();
+                uint32_t meshHeight = getInt();
+                float* vertices = getFloats(verticesCount);
+                bool hasColors = getInt();
+                int* colors = hasColors ? getInts(colorsCount) : NULL;
+
+                renderer.drawBitmapMesh(bitmap, meshWidth, meshHeight, vertices, colors, getPaint());
+            }
             case DrawPatch: {
                 int32_t* xDivs = NULL;
                 int32_t* yDivs = NULL;
@@ -587,6 +601,22 @@
     addPaint(paint);
 }
 
+void DisplayListRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
+        float* vertices, int* colors, SkPaint* paint) {
+    addOp(DisplayList::DrawBitmapMesh);
+    addBitmap(bitmap);
+    addInt(meshWidth);
+    addInt(meshHeight);
+    addFloats(vertices, (meshWidth + 1) * (meshHeight + 1) * 2);
+    if (colors) {
+        addInt(1);
+        addInts(colors, (meshWidth + 1) * (meshHeight + 1));
+    } else {
+        addInt(0);
+    }
+    addPaint(paint);
+}
+
 void DisplayListRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
         const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
         float left, float top, float right, float bottom, SkPaint* paint) {
diff --git a/libs/hwui/DisplayListRenderer.h b/libs/hwui/DisplayListRenderer.h
index 4b727f6..7f9db8a 100644
--- a/libs/hwui/DisplayListRenderer.h
+++ b/libs/hwui/DisplayListRenderer.h
@@ -107,6 +107,7 @@
         DrawBitmap,
         DrawBitmapMatrix,
         DrawBitmapRect,
+        DrawBitmapMesh,
         DrawPatch,
         DrawColor,
         DrawRect,
@@ -267,6 +268,8 @@
     void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,
             float srcRight, float srcBottom, float dstLeft, float dstTop,
             float dstRight, float dstBottom, SkPaint* paint);
+    void drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
+            float* vertices, int* colors, SkPaint* paint);
     void drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
             const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
             float left, float top, float right, float bottom, SkPaint* paint);
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index 9528dbb..b06bbd0 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -1077,6 +1077,61 @@
     restore();
 }
 
+void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
+        float* vertices, int* colors, SkPaint* paint) {
+    // TODO: Do a quickReject
+    if (!vertices || mSnapshot->isIgnored()) {
+        return;
+    }
+
+    glActiveTexture(gTextureUnits[0]);
+    Texture* texture = mCaches.textureCache.get(bitmap);
+    if (!texture) return;
+    const AutoTexture autoCleanup(texture);
+    setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
+
+    int alpha;
+    SkXfermode::Mode mode;
+    getAlphaAndMode(paint, &alpha, &mode);
+
+    // TODO: Support the colors array
+    const uint32_t count = meshWidth * meshHeight * 6;
+    TextureVertex mesh[count];
+
+    TextureVertex* vertex = mesh;
+    for (int32_t y = 0; y < meshHeight; y++) {
+        for (int32_t x = 0; x < meshWidth; x++) {
+            uint32_t i = (y * (meshWidth + 1) + x) * 2;
+
+            float u1 = float(x) / meshWidth;
+            float u2 = float(x + 1) / meshWidth;
+            float v1 = float(y) / meshHeight;
+            float v2 = float(y + 1) / meshHeight;
+
+            int ax = i + (meshWidth + 1) * 2;
+            int ay = ax + 1;
+            int bx = i;
+            int by = bx + 1;
+            int cx = i + 2;
+            int cy = cx + 1;
+            int dx = i + (meshWidth + 1) * 2 + 2;
+            int dy = dx + 1;
+
+            TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
+            TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
+            TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
+
+            TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
+            TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
+            TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
+        }
+    }
+
+    drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
+            mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
+            GL_TRIANGLES, count);
+}
+
 void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
          float srcLeft, float srcTop, float srcRight, float srcBottom,
          float dstLeft, float dstTop, float dstRight, float dstBottom,
diff --git a/libs/hwui/OpenGLRenderer.h b/libs/hwui/OpenGLRenderer.h
index a43660b..42e93ad 100644
--- a/libs/hwui/OpenGLRenderer.h
+++ b/libs/hwui/OpenGLRenderer.h
@@ -102,6 +102,8 @@
     virtual void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,
             float srcRight, float srcBottom, float dstLeft, float dstTop,
             float dstRight, float dstBottom, SkPaint* paint);
+    virtual void drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
+            float* vertices, int* colors, SkPaint* paint);
     virtual void drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
             const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
             float left, float top, float right, float bottom, SkPaint* paint);