Remove all Dalvik allocations from Cavnas.drawBitmap(int[], ...)

Change-Id: Ie28538a2104d21154fdc78a56525e7403f08287d
diff --git a/libs/hwui/DisplayListRenderer.cpp b/libs/hwui/DisplayListRenderer.cpp
index f6ca77c..d2a6a7a 100644
--- a/libs/hwui/DisplayListRenderer.cpp
+++ b/libs/hwui/DisplayListRenderer.cpp
@@ -49,6 +49,7 @@
     "DrawBitmap",
     "DrawBitmapMatrix",
     "DrawBitmapRect",
+    "DrawBitmapData",
     "DrawBitmapMesh",
     "DrawPatch",
     "DrawColor",
@@ -434,6 +435,14 @@
                         (char*) indent, OP_NAMES[op], bitmap, f1, f2, f3, f4, f5, f6, f7, f8, paint);
             }
             break;
+            case DrawBitmapData: {
+                SkBitmap* bitmap = getBitmapData();
+                float x = getFloat();
+                float y = getFloat();
+                SkPaint* paint = getPaint(renderer);
+                ALOGD("%s%s %.2f, %.2f, %p", (char*) indent, OP_NAMES[op], x, y, paint);
+            }
+            break;
             case DrawBitmapMesh: {
                 int verticesCount = 0;
                 uint32_t colorsCount = 0;
@@ -1020,6 +1029,19 @@
                 renderer.drawBitmap(bitmap, f1, f2, f3, f4, f5, f6, f7, f8, paint);
             }
             break;
+            case DrawBitmapData: {
+                SkBitmap* bitmap = getBitmapData();
+                float x = getFloat();
+                float y = getFloat();
+                SkPaint* paint = getPaint(renderer);
+                DISPLAY_LIST_LOGD("%s%s %p, %.2f, %.2f, %p", (char*) indent, OP_NAMES[op],
+                        bitmap, x, y, paint);
+                if (bitmap) {
+                    renderer.drawBitmap(bitmap, x, y, paint);
+                    delete bitmap;
+                }
+            }
+            break;
             case DrawBitmapMesh: {
                 int32_t verticesCount = 0;
                 uint32_t colorsCount = 0;
@@ -1487,6 +1509,15 @@
     addSkip(location);
 }
 
+void DisplayListRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
+    const bool reject = quickReject(left, top, left + bitmap->width(), bitmap->height());
+    uint32_t* location = addOp(DisplayList::DrawBitmapData, reject);
+    addBitmapData(bitmap);
+    addPoint(left, top);
+    addPaint(paint);
+    addSkip(location);
+}
+
 void DisplayListRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
         float* vertices, int* colors, SkPaint* paint) {
     addOp(DisplayList::DrawBitmapMesh);
diff --git a/libs/hwui/DisplayListRenderer.h b/libs/hwui/DisplayListRenderer.h
index 5ce770d..2f74f5b 100644
--- a/libs/hwui/DisplayListRenderer.h
+++ b/libs/hwui/DisplayListRenderer.h
@@ -91,6 +91,7 @@
         DrawBitmap,
         DrawBitmapMatrix,
         DrawBitmapRect,
+        DrawBitmapData,
         DrawBitmapMesh,
         DrawPatch,
         DrawColor,
@@ -422,6 +423,19 @@
         return (SkBitmap*) getInt();
     }
 
+    SkBitmap* getBitmapData() {
+        SkBitmap* bitmap = new SkBitmap;
+        bitmap->setConfig((SkBitmap::Config) getInt(), getInt(), getInt());
+        if (!bitmap->allocPixels()) {
+            delete bitmap;
+            return NULL;
+        }
+
+        bitmap->setPixels((void*) mReader.skip(bitmap->height() * bitmap->rowBytes()));
+
+        return bitmap;
+    }
+
     SkiaShader* getShader() {
         return (SkiaShader*) getInt();
     }
@@ -574,6 +588,7 @@
     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 drawBitmapData(SkBitmap* bitmap, float left, float top, 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,
@@ -701,16 +716,23 @@
 
     void addInts(const int32_t* values, uint32_t count) {
         mWriter.writeInt(count);
-        for (uint32_t i = 0; i < count; i++) {
-            mWriter.writeInt(values[i]);
-        }
+        mWriter.write(values, count * sizeof(int32_t));
+    }
+
+    void addBitmapData(SkBitmap* bitmap) {
+        mWriter.writeInt(bitmap->config());
+        mWriter.writeInt(bitmap->width());
+        mWriter.writeInt(bitmap->height());
+
+        SkAutoLockPixels alp(*bitmap);
+        void* src = bitmap->getPixels();
+
+        mWriter.write(src, bitmap->rowBytes() * bitmap->height());
     }
 
     void addUInts(const uint32_t* values, int8_t count) {
         mWriter.writeInt(count);
-        for (int8_t i = 0; i < count; i++) {
-            mWriter.writeInt(values[i]);
-        }
+        mWriter.write(values, count * sizeof(uint32_t));
     }
 
     inline void addFloat(float value) {
@@ -719,9 +741,7 @@
 
     void addFloats(const float* values, int32_t count) {
         mWriter.writeInt(count);
-        for (int32_t i = 0; i < count; i++) {
-            mWriter.writeScalar(values[i]);
-        }
+        mWriter.write(values, count * sizeof(float));
     }
 
     inline void addPoint(float x, float y) {
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index da2192f..7f242c3 100644
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -1502,6 +1502,21 @@
     restore();
 }
 
+void OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
+    const float right = left + bitmap->width();
+    const float bottom = top + bitmap->height();
+
+    if (quickReject(left, top, right, bottom)) {
+        return;
+    }
+
+    mCaches.activeTexture(0);
+    Texture* texture = mCaches.textureCache.getTransient(bitmap);
+    const AutoTexture autoCleanup(texture);
+
+    drawTextureRect(left, top, right, bottom, texture, paint);
+}
+
 void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
         float* vertices, int* colors, SkPaint* paint) {
     // TODO: Do a quickReject
diff --git a/libs/hwui/OpenGLRenderer.h b/libs/hwui/OpenGLRenderer.h
index 18a6923..0ea0db7 100644
--- a/libs/hwui/OpenGLRenderer.h
+++ b/libs/hwui/OpenGLRenderer.h
@@ -115,6 +115,7 @@
     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 drawBitmapData(SkBitmap* bitmap, float left, float top, 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,
diff --git a/libs/hwui/TextureCache.cpp b/libs/hwui/TextureCache.cpp
index cc09aae..9fb61e4 100644
--- a/libs/hwui/TextureCache.cpp
+++ b/libs/hwui/TextureCache.cpp
@@ -160,6 +160,16 @@
     return texture;
 }
 
+Texture* TextureCache::getTransient(SkBitmap* bitmap) {
+    Texture* texture = new Texture;
+    texture->bitmapSize = bitmap->rowBytes() * bitmap->height();
+    texture->cleanup = true;
+
+    generateTexture(bitmap, texture, false);
+
+    return texture;
+}
+
 void TextureCache::remove(SkBitmap* bitmap) {
     mCache.remove(bitmap);
 }
diff --git a/libs/hwui/TextureCache.h b/libs/hwui/TextureCache.h
index 10a05e0..31a2e3d 100644
--- a/libs/hwui/TextureCache.h
+++ b/libs/hwui/TextureCache.h
@@ -67,6 +67,11 @@
      */
     Texture* get(SkBitmap* bitmap);
     /**
+     * Returns the texture associated with the specified bitmap. The generated
+     * texture is not kept in the cache. The caller must destroy the texture.
+     */
+    Texture* getTransient(SkBitmap* bitmap);
+    /**
      * Removes the texture associated with the specified bitmap.
      * Upon remove the texture is freed.
      */