Add support for drawBitmapMesh().

Change-Id: Ic77f9c534bb90dc7b9458299544bd50b8b6ae6a5
diff --git a/core/java/android/view/GLES20Canvas.java b/core/java/android/view/GLES20Canvas.java
index d82f051..e47dc93 100644
--- a/core/java/android/view/GLES20Canvas.java
+++ b/core/java/android/view/GLES20Canvas.java
@@ -630,9 +630,32 @@
     @Override
     public void drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[] verts,
             int vertOffset, int[] colors, int colorOffset, Paint paint) {
-        // TODO: Implement
+        if (meshWidth < 0 || meshHeight < 0 || vertOffset < 0 || colorOffset < 0) {
+            throw new ArrayIndexOutOfBoundsException();
+        }
+
+        if (meshWidth == 0 || meshHeight == 0) {
+            return;
+        }
+
+        final int count = (meshWidth + 1) * (meshHeight + 1);
+        checkRange(verts.length, vertOffset, count * 2);
+
+        // TODO: Colors are ignored for now
+        colors = null;
+        colorOffset = 0;
+
+        boolean hasColorFilter = paint != null && setupColorFilter(paint);
+        final int nativePaint = paint == null ? 0 : paint.mNativePaint;        
+        nDrawBitmapMesh(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, meshWidth, meshHeight,
+                verts, vertOffset, colors, colorOffset, nativePaint);
+        if (hasColorFilter) nResetModifiers(mRenderer);
     }
 
+    private native void nDrawBitmapMesh(int renderer, int bitmap, byte[] buffer,
+            int meshWidth, int meshHeight, float[] verts, int vertOffset,
+            int[] colors, int colorOffset, int paint);
+
     @Override
     public void drawCircle(float cx, float cy, float radius, Paint paint) {
         boolean hasModifier = setupModifiers(paint);
diff --git a/core/jni/android_view_GLES20Canvas.cpp b/core/jni/android_view_GLES20Canvas.cpp
index bfd2b58e..554e336 100644
--- a/core/jni/android_view_GLES20Canvas.cpp
+++ b/core/jni/android_view_GLES20Canvas.cpp
@@ -271,6 +271,22 @@
     renderer->drawBitmap(bitmap, matrix, paint);
 }
 
+static void android_view_GLES20Canvas_drawBitmapMesh(JNIEnv* env, jobject canvas,
+        OpenGLRenderer* renderer, SkBitmap* bitmap, jbyteArray buffer,
+        jint meshWidth, jint meshHeight, jfloatArray vertices, jint offset,
+        jintArray colors, jint colorOffset, SkPaint* paint) {
+    // This object allows the renderer to allocate a global JNI ref to the buffer object.
+    JavaHeapBitmapRef bitmapRef(env, bitmap, buffer);
+
+    jfloat* verticesArray = vertices ? env->GetFloatArrayElements(vertices, NULL) + offset : NULL;
+    jint* colorsArray = colors ? env->GetIntArrayElements(colors, NULL) + colorOffset : NULL;
+
+    renderer->drawBitmapMesh(bitmap, meshWidth, meshHeight, verticesArray, colorsArray, paint);
+
+    if (vertices) env->ReleaseFloatArrayElements(vertices, verticesArray, 0);
+    if (colors) env->ReleaseIntArrayElements(colors, colorsArray, 0);
+}
+
 static void android_view_GLES20Canvas_drawPatch(JNIEnv* env, jobject canvas,
         OpenGLRenderer* renderer, SkBitmap* bitmap, jbyteArray buffer, jbyteArray chunks,
         float left, float top, float right, float bottom, SkPaint* paint) {
@@ -393,24 +409,24 @@
 }
 
 static void android_view_GLES20Canvas_drawTextArray(JNIEnv* env, jobject canvas,
-        OpenGLRenderer* renderer, jcharArray text, int index, int count,
-        jfloat x, jfloat y, int flags, SkPaint* paint) {
+        OpenGLRenderer* renderer, jcharArray text, jint index, jint count,
+        jfloat x, jfloat y, jint flags, SkPaint* paint) {
     jchar* textArray = env->GetCharArrayElements(text, NULL);
     renderText(renderer, textArray + index, count, x, y, flags, paint);
     env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
 }
 
 static void android_view_GLES20Canvas_drawText(JNIEnv* env, jobject canvas,
-        OpenGLRenderer* renderer, jstring text, int start, int end,
-        jfloat x, jfloat y, int flags, SkPaint* paint) {
+        OpenGLRenderer* renderer, jstring text, jint start, jint end,
+        jfloat x, jfloat y, jint flags, SkPaint* paint) {
     const jchar* textArray = env->GetStringChars(text, NULL);
     renderText(renderer, textArray + start, end - start, x, y, flags, paint);
     env->ReleaseStringChars(text, textArray);
 }
 
 static void android_view_GLES20Canvas_drawTextRunArray(JNIEnv* env, jobject canvas,
-        OpenGLRenderer* renderer, jcharArray text, int index, int count,
-        int contextIndex, int contextCount, jfloat x, jfloat y, int dirFlags,
+        OpenGLRenderer* renderer, jcharArray text, jint index, jint count,
+        jint contextIndex, jint contextCount, jfloat x, jfloat y, jint dirFlags,
         SkPaint* paint) {
     jchar* textArray = env->GetCharArrayElements(text, NULL);
     renderTextRun(renderer, textArray + contextIndex, index - contextIndex,
@@ -419,8 +435,8 @@
  }
 
 static void android_view_GLES20Canvas_drawTextRun(JNIEnv* env, jobject canvas,
-        OpenGLRenderer* renderer, jstring text, int start, int end,
-        int contextStart, int contextEnd, jfloat x, jfloat y, int dirFlags,
+        OpenGLRenderer* renderer, jstring text, jint start, jint end,
+        jint contextStart, int contextEnd, jfloat x, jfloat y, jint dirFlags,
         SkPaint* paint) {
     const jchar* textArray = env->GetStringChars(text, NULL);
     jint count = end - start;
@@ -573,10 +589,13 @@
     { "nGetMatrix",         "(II)V",           (void*) android_view_GLES20Canvas_getMatrix },
     { "nConcatMatrix",      "(II)V",           (void*) android_view_GLES20Canvas_concatMatrix },
 
-    { "nDrawBitmap",        "(II[BFFI)V", (void*) android_view_GLES20Canvas_drawBitmap },
-    { "nDrawBitmap",        "(II[BFFFFFFFFI)V", (void*) android_view_GLES20Canvas_drawBitmapRect },
-    { "nDrawBitmap",        "(II[BII)V", (void*) android_view_GLES20Canvas_drawBitmapMatrix },
-    { "nDrawPatch",         "(II[B[BFFFFI)V", (void*) android_view_GLES20Canvas_drawPatch },
+    { "nDrawBitmap",        "(II[BFFI)V",      (void*) android_view_GLES20Canvas_drawBitmap },
+    { "nDrawBitmap",        "(II[BFFFFFFFFI)V",(void*) android_view_GLES20Canvas_drawBitmapRect },
+    { "nDrawBitmap",        "(II[BII)V",       (void*) android_view_GLES20Canvas_drawBitmapMatrix },
+
+    { "nDrawBitmapMesh",    "(II[BII[FI[III)V",(void*) android_view_GLES20Canvas_drawBitmapMesh },
+
+    { "nDrawPatch",         "(II[B[BFFFFI)V",  (void*) android_view_GLES20Canvas_drawPatch },
 
     { "nDrawColor",         "(III)V",          (void*) android_view_GLES20Canvas_drawColor },
     { "nDrawRect",          "(IFFFFI)V",       (void*) android_view_GLES20Canvas_drawRect },
diff --git a/graphics/java/android/graphics/Canvas.java b/graphics/java/android/graphics/Canvas.java
index 184620b..89e725a 100644
--- a/graphics/java/android/graphics/Canvas.java
+++ b/graphics/java/android/graphics/Canvas.java
@@ -1175,8 +1175,11 @@
         nativeDrawBitmapMatrix(mNativeCanvas, bitmap.ni(), matrix.ni(),
                 paint != null ? paint.mNativePaint : 0);
     }
-    
-    private static void checkRange(int length, int offset, int count) {
+
+    /**
+     * @hide
+     */
+    protected static void checkRange(int length, int offset, int count) {
         if ((offset | count) < 0 || offset + count > length) {
             throw new ArrayIndexOutOfBoundsException();
         }
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);
diff --git a/tests/HwAccelerationTest/AndroidManifest.xml b/tests/HwAccelerationTest/AndroidManifest.xml
index 1d67964..61f8e1a 100644
--- a/tests/HwAccelerationTest/AndroidManifest.xml
+++ b/tests/HwAccelerationTest/AndroidManifest.xml
@@ -25,6 +25,15 @@
         android:hardwareAccelerated="true">
 
         <activity
+                android:name="BitmapMeshActivity"
+                android:label="_BitmapMesh">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+
+        <activity
                 android:name="ShapesActivity"
                 android:label="_Shapes">
             <intent-filter>
diff --git a/tests/HwAccelerationTest/src/com/android/test/hwui/BitmapMeshActivity.java b/tests/HwAccelerationTest/src/com/android/test/hwui/BitmapMeshActivity.java
new file mode 100644
index 0000000..833d559
--- /dev/null
+++ b/tests/HwAccelerationTest/src/com/android/test/hwui/BitmapMeshActivity.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.test.hwui;
+
+import android.app.Activity;
+import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.graphics.Canvas;
+import android.graphics.Paint;
+import android.graphics.PorterDuff;
+import android.graphics.PorterDuffXfermode;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.View;
+
+@SuppressWarnings({"UnusedDeclaration"})
+public class BitmapMeshActivity extends Activity {
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        final BitmapMeshView view = new BitmapMeshView(this);
+        view.setDrawingCacheEnabled(true);
+        setContentView(view);
+    }
+
+    static class BitmapMeshView extends View {
+        private Paint mBitmapPaint;
+        private final Bitmap mBitmap1;
+
+        BitmapMeshView(Context c) {
+            super(c);
+
+            mBitmap1 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1);
+        }
+
+        @Override
+        protected void onDraw(Canvas canvas) {
+            super.onDraw(canvas);
+
+            canvas.drawARGB(255, 255, 255, 255);
+            canvas.translate(100, 100);
+            final float width = mBitmap1.getWidth() / 3.0f;
+            final float height = mBitmap1.getHeight() / 3.0f;
+            canvas.drawBitmapMesh(mBitmap1, 3, 3, new float[] {
+                    0.0f, 0.0f, width, 0.0f, width * 2, 0.0f, width * 3, 0.0f,
+                    0.0f, height, width, height, width * 2, height, width * 4, height,
+                    0.0f, height * 2, width, height * 2, width * 2, height * 2, width * 3, height * 2,
+                    0.0f, height * 4, width, height * 4, width * 2, height * 4, width * 4, height * 4,
+            }, 0, null, 0, null);
+        }
+    }
+}