Remove CreateFromBitmapRef and add
CopyTo(bitmap) replacement.

Change-Id: Ib73fb9f4bfe5f468eaf0f8f1bf68a93759eef00d
diff --git a/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java b/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
index e935fa9..09654ab 100644
--- a/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
+++ b/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
@@ -283,7 +283,7 @@
             long t = java.lang.System.currentTimeMillis();
             if (true) {
                 mScript.invoke_filter();
-                mRS.finish();
+                mOutPixelsAllocation.copyTo(mBitmapOut);
             } else {
                 javaFilter();
                 mDisplayView.invalidate();
@@ -352,7 +352,7 @@
     public void surfaceCreated(SurfaceHolder holder) {
         createScript();
         mScript.invoke_filter();
-        mRS.finish();
+        mOutPixelsAllocation.copyTo(mBitmapOut);
     }
 
     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
@@ -365,8 +365,12 @@
         mRS = RenderScript.create();
         mRS.setMessageHandler(new FilterCallback());
 
-        mInPixelsAllocation = Allocation.createBitmapRef(mRS, mBitmapIn);
-        mOutPixelsAllocation = Allocation.createBitmapRef(mRS, mBitmapOut);
+        mInPixelsAllocation = Allocation.createFromBitmap(mRS, mBitmapIn,
+                                                          Allocation.MipmapControl.MIPMAP_NONE,
+                                                          Allocation.USAGE_SCRIPT);
+        mOutPixelsAllocation = Allocation.createFromBitmap(mRS, mBitmapOut,
+                                                           Allocation.MipmapControl.MIPMAP_NONE,
+                                                           Allocation.USAGE_SCRIPT);
 
         Type.Builder tb = new Type.Builder(mRS, Element.F32_4(mRS));
         tb.setX(mBitmapIn.getWidth());
@@ -419,7 +423,7 @@
         long t = java.lang.System.currentTimeMillis();
 
         mScript.invoke_filter();
-        mRS.finish();
+        mOutPixelsAllocation.copyTo(mBitmapOut);
 
         t = java.lang.System.currentTimeMillis() - t;
         android.util.Log.v("Img", "Renderscript frame time core ms " + t);
@@ -432,6 +436,6 @@
         mScript.set_radius(mRadius);
 
         mScript.invoke_filter();
-        mRS.finish();
+        mOutPixelsAllocation.copyTo(mBitmapOut);
     }
 }
diff --git a/rs.spec b/rs.spec
index 97ecca0..14094c4 100644
--- a/rs.spec
+++ b/rs.spec
@@ -77,10 +77,16 @@
 	ret RsElement
 	}
 
-AllocationUpdateFromBitmap {
+AllocationCopyFromBitmap {
 	param RsAllocation alloc
-	param RsElement srcFmt
 	param const void * data
+	param size_t dataLen
+	}
+
+AllocationCopyToBitmap {
+	param RsAllocation alloc
+	param void * data
+	param size_t dataLen
 	}
 
 AllocationCreateBitmapRef {
diff --git a/rsAllocation.cpp b/rsAllocation.cpp
index f42be0e..10a5caf 100644
--- a/rsAllocation.cpp
+++ b/rsAllocation.cpp
@@ -763,30 +763,42 @@
     return alloc;
 }
 
-void rsi_AllocationUpdateFromBitmap(Context *rsc, RsAllocation va,
-                                    RsElement _src, const void *data) {
+void rsi_AllocationCopyFromBitmap(Context *rsc, RsAllocation va, const void *data, size_t dataLen) {
     Allocation *texAlloc = static_cast<Allocation *>(va);
-    const Element *src = static_cast<const Element *>(_src);
-    const Element *dst = texAlloc->getType()->getElement();
-    uint32_t w = texAlloc->getType()->getDimX();
-    uint32_t h = texAlloc->getType()->getDimY();
-    bool genMips = texAlloc->getType()->getDimLOD();
+    const Type * t = texAlloc->getType();
 
-    ElementConverter_t cvt = pickConverter(dst, src);
-    if (cvt) {
-        cvt(texAlloc->getPtr(), data, w * h);
-        if (genMips) {
-            Adapter2D adapt(rsc, texAlloc);
-            Adapter2D adapt2(rsc, texAlloc);
-            for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
-                adapt.setLOD(lod);
-                adapt2.setLOD(lod + 1);
-                mip(adapt2, adapt);
-            }
-        }
-    } else {
-        rsc->setError(RS_ERROR_BAD_VALUE, "Unsupported bitmap format");
+    uint32_t w = t->getDimX();
+    uint32_t h = t->getDimY();
+    bool genMips = t->getDimLOD();
+    size_t s = w * h * t->getElementSizeBytes();
+    if (s != dataLen) {
+        rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
+        return;
     }
+
+    memcpy(texAlloc->getPtr(), data, s);
+    if (genMips) {
+        Adapter2D adapt(rsc, texAlloc);
+        Adapter2D adapt2(rsc, texAlloc);
+        for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
+            adapt.setLOD(lod);
+            adapt2.setLOD(lod + 1);
+            mip(adapt2, adapt);
+        }
+    }
+}
+
+void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) {
+    Allocation *texAlloc = static_cast<Allocation *>(va);
+    const Type * t = texAlloc->getType();
+
+    size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes();
+    if (s != dataLen) {
+        rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size");
+        return;
+    }
+
+    memcpy(data, texAlloc->getPtr(), s);
 }
 
 void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data, uint32_t sizeBytes) {