Remove CreateFromBitmapRef and add
CopyTo(bitmap) replacement.

Change-Id: Ib73fb9f4bfe5f468eaf0f8f1bf68a93759eef00d
diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java
index 97d513a..b937721 100644
--- a/graphics/java/android/renderscript/Allocation.java
+++ b/graphics/java/android/renderscript/Allocation.java
@@ -55,13 +55,13 @@
         }
     }
 
-    public enum MipmapGenerationControl {
+    public enum MipmapControl {
         MIPMAP_NONE(0),
         MIPMAP_FULL(1),
         MIPMAP_ON_SYNC_TO_TEXTURE(2);
 
         int mID;
-        MipmapGenerationControl(int id) {
+        MipmapControl(int id) {
             mID = id;
         }
     }
@@ -131,6 +131,14 @@
         subData1D(0, mType.getCount(), i);
     }
 
+    private void validateBitmap(Bitmap b) {
+        mRS.validate();
+        if(mType.getX() != b.getWidth() ||
+           mType.getY() != b.getHeight()) {
+            throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
+        }
+    }
+
     public void copyFrom(int[] d) {
         mRS.validate();
         subData1D(0, mType.getCount(), d);
@@ -147,18 +155,17 @@
         mRS.validate();
         subData1D(0, mType.getCount(), d);
     }
-
     public void copyFrom(Bitmap b) {
-
-        mRS.validate();
-        if(mType.getX() != b.getWidth() ||
-           mType.getY() != b.getHeight()) {
-            throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
-        }
-
-        mRS.nAllocationUpdateFromBitmap(getID(), b);
+        validateBitmap(b);
+        mRS.nAllocationCopyFromBitmap(getID(), b);
     }
 
+    public void copyTo(Bitmap b) {
+        validateBitmap(b);
+        mRS.nAllocationCopyToBitmap(getID(), b);
+    }
+
+
     public void subData(int xoff, FieldPacker fp) {
         int eSize = mType.mElement.getSizeBytes();
         final byte[] data = fp.getData();
@@ -423,17 +430,17 @@
     }
 
     static private Type typeFromBitmap(RenderScript rs, Bitmap b,
-                                       MipmapGenerationControl mip) {
+                                       MipmapControl mip) {
         Element e = elementFromBitmap(rs, b);
         Type.Builder tb = new Type.Builder(rs, e);
         tb.setX(b.getWidth());
         tb.setY(b.getHeight());
-        tb.setMipmaps(mip == MipmapGenerationControl.MIPMAP_FULL);
+        tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
         return tb.create();
     }
 
     static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
-                                              MipmapGenerationControl mips,
+                                              MipmapControl mips,
                                               int usage) {
         rs.validate();
         Type t = typeFromBitmap(rs, b, mips);
@@ -447,15 +454,15 @@
 
     static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
                                               Element dstFmt, boolean genMips) {
-        MipmapGenerationControl mc = MipmapGenerationControl.MIPMAP_NONE;
+        MipmapControl mc = MipmapControl.MIPMAP_NONE;
         if (genMips) {
-            mc = MipmapGenerationControl.MIPMAP_ON_SYNC_TO_TEXTURE;
+            mc = MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE;
         }
         return createFromBitmap(rs, b, mc, USAGE_ALL);
     }
 
     static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
-                                                     MipmapGenerationControl mips,
+                                                     MipmapControl mips,
                                                      CubemapLayout layout,
                                                      int usage) {
         rs.validate();
@@ -482,7 +489,7 @@
         tb.setX(width);
         tb.setY(width);
         tb.setFaces(true);
-        tb.setMipmaps(mips == MipmapGenerationControl.MIPMAP_FULL);
+        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
         Type t = tb.create();
 
         int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage);
@@ -496,33 +503,17 @@
                                                      Element dstFmt,
                                                      boolean genMips,
                                                      CubemapLayout layout) {
-        MipmapGenerationControl mc = MipmapGenerationControl.MIPMAP_NONE;
+        MipmapControl mc = MipmapControl.MIPMAP_NONE;
         if (genMips) {
-            mc = MipmapGenerationControl.MIPMAP_ON_SYNC_TO_TEXTURE;
+            mc = MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE;
         }
         return createCubemapFromBitmap(rs, b, mc, layout, USAGE_ALL);
     }
 
-
-    static public Allocation createBitmapRef(RenderScript rs, Bitmap b) {
-
-        rs.validate();
-        Type t = typeFromBitmap(rs, b, MipmapGenerationControl.MIPMAP_NONE);
-
-        int id = rs.nAllocationCreateBitmapRef(t.getID(), b);
-        if(id == 0) {
-            throw new RSRuntimeException("Load failed.");
-        }
-
-        Allocation a = new Allocation(id, rs, t, USAGE_SCRIPT);
-        a.mBitmap = b;
-        return a;
-    }
-
     static public Allocation createFromBitmapResource(RenderScript rs,
                                                       Resources res,
                                                       int id,
-                                                      MipmapGenerationControl mips,
+                                                      MipmapControl mips,
                                                       int usage) {
 
         rs.validate();
@@ -537,9 +528,9 @@
                                                       int id,
                                                       Element dstFmt,
                                                       boolean genMips) {
-        MipmapGenerationControl mc = MipmapGenerationControl.MIPMAP_NONE;
+        MipmapControl mc = MipmapControl.MIPMAP_NONE;
         if (genMips) {
-            mc = MipmapGenerationControl.MIPMAP_ON_SYNC_TO_TEXTURE;
+            mc = MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE;
         }
         return createFromBitmapResource(rs, res, id, mc, USAGE_ALL);
     }
diff --git a/graphics/java/android/renderscript/Element.java b/graphics/java/android/renderscript/Element.java
index 7e89a56..7616316 100644
--- a/graphics/java/android/renderscript/Element.java
+++ b/graphics/java/android/renderscript/Element.java
@@ -654,17 +654,5 @@
             return new Element(id, mRS, ein, sin, asin);
         }
     }
-
-    static void initPredefined(RenderScript rs) {
-        int a8 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
-                                   DataKind.PIXEL_A.mID, true, 1);
-        int rgba4444 = rs.nElementCreate(DataType.UNSIGNED_4_4_4_4.mID,
-                                         DataKind.PIXEL_RGBA.mID, true, 4);
-        int rgba8888 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
-                                         DataKind.PIXEL_RGBA.mID, true, 4);
-        int rgb565 = rs.nElementCreate(DataType.UNSIGNED_5_6_5.mID,
-                                       DataKind.PIXEL_RGB.mID, true, 3);
-        rs.nInitElements(a8, rgba4444, rgba8888, rgb565);
-    }
 }
 
diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java
index e3a9a67..3fa9965 100644
--- a/graphics/java/android/renderscript/RenderScript.java
+++ b/graphics/java/android/renderscript/RenderScript.java
@@ -65,7 +65,6 @@
     }
 
     // Non-threadsafe functions.
-    native void nInitElements(int a8, int rgba4444, int rgba8888, int rgb565);
     native int  nDeviceCreate();
     native void nDeviceDestroy(int dev);
     native void nDeviceSetConfig(int dev, int param, int value);
@@ -213,13 +212,19 @@
         return rsnAllocationCreateFromAssetStream(mContext, mips, assetStream, usage);
     }
 
+    native void  rsnAllocationCopyToBitmap(int con, int alloc, Bitmap bmp);
+    synchronized void nAllocationCopyToBitmap(int alloc, Bitmap bmp) {
+        rsnAllocationCopyToBitmap(mContext, alloc, bmp);
+    }
+
+
     native void rsnAllocationSyncAll(int con, int alloc, int src);
     synchronized void nAllocationSyncAll(int alloc, int src) {
         rsnAllocationSyncAll(mContext, alloc, src);
     }
-    native void  rsnAllocationUpdateFromBitmap(int con, int alloc, Bitmap bmp);
-    synchronized void nAllocationUpdateFromBitmap(int alloc, Bitmap bmp) {
-        rsnAllocationUpdateFromBitmap(mContext, alloc, bmp);
+    native void  rsnAllocationCopyFromBitmap(int con, int alloc, Bitmap bmp);
+    synchronized void nAllocationCopyFromBitmap(int alloc, Bitmap bmp) {
+        rsnAllocationCopyFromBitmap(mContext, alloc, bmp);
     }
 
     native void rsnAllocationUploadToTexture(int con, int alloc, boolean genMips, int baseMioLevel);
@@ -787,7 +792,6 @@
         rs.mContext = rs.nContextCreate(rs.mDev, 0);
         rs.mMessageThread = new MessageThread(rs);
         rs.mMessageThread.start();
-        Element.initPredefined(rs);
         return rs;
     }
 
diff --git a/graphics/java/android/renderscript/RenderScriptGL.java b/graphics/java/android/renderscript/RenderScriptGL.java
index 4a1c40a..0886db4 100644
--- a/graphics/java/android/renderscript/RenderScriptGL.java
+++ b/graphics/java/android/renderscript/RenderScriptGL.java
@@ -190,7 +190,6 @@
         }
         mMessageThread = new MessageThread(this);
         mMessageThread.start();
-        Element.initPredefined(this);
     }
 
     /**
diff --git a/graphics/jni/android_renderscript_RenderScript.cpp b/graphics/jni/android_renderscript_RenderScript.cpp
index 65acf93..8344842 100644
--- a/graphics/jni/android_renderscript_RenderScript.cpp
+++ b/graphics/jni/android_renderscript_RenderScript.cpp
@@ -59,11 +59,6 @@
 static jfieldID gNativeBitmapID = 0;
 static jfieldID gTypeNativeCache = 0;
 
-static RsElement g_A_8 = NULL;
-static RsElement g_RGBA_4444 = NULL;
-static RsElement g_RGBA_8888 = NULL;
-static RsElement g_RGB_565 = NULL;
-
 static void _nInit(JNIEnv *_env, jclass _this)
 {
     gContextId             = _env->GetFieldID(_this, "mContext", "I");
@@ -72,14 +67,6 @@
     gNativeBitmapID = _env->GetFieldID(bitmapClass, "mNativeBitmap", "I");
 }
 
-static void nInitElements(JNIEnv *_env, jobject _this, jint a8, jint rgba4444, jint rgba8888, jint rgb565)
-{
-    g_A_8 = reinterpret_cast<RsElement>(a8);
-    g_RGBA_4444 = reinterpret_cast<RsElement>(rgba4444);
-    g_RGBA_8888 = reinterpret_cast<RsElement>(rgba8888);
-    g_RGB_565 = reinterpret_cast<RsElement>(rgb565);
-}
-
 // ---------------------------------------------------------------------------
 
 static void
@@ -415,26 +402,6 @@
     rsAllocationSyncAll(con, (RsAllocation)a, (RsAllocationUsageType)bits);
 }
 
-static RsElement SkBitmapToPredefined(SkBitmap::Config cfg)
-{
-    switch (cfg) {
-    case SkBitmap::kA8_Config:
-        return g_A_8;
-    case SkBitmap::kARGB_4444_Config:
-        return g_RGBA_4444;
-    case SkBitmap::kARGB_8888_Config:
-        return g_RGBA_8888;
-    case SkBitmap::kRGB_565_Config:
-        return g_RGB_565;
-
-    default:
-        break;
-    }
-    // If we don't have a conversion mark it as a user type.
-    LOGE("Unsupported bitmap type");
-    return NULL;
-}
-
 static int
 nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, RsContext con, jint type, jint mip, jobject jbitmap, jint usage)
 {
@@ -464,20 +431,29 @@
 }
 
 static void
-nAllocationUpdateFromBitmap(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jobject jbitmap)
+nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jobject jbitmap)
 {
     SkBitmap const * nativeBitmap =
             (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
     const SkBitmap& bitmap(*nativeBitmap);
-    SkBitmap::Config config = bitmap.getConfig();
 
-    RsElement e = SkBitmapToPredefined(config);
-    if (e) {
-        bitmap.lockPixels();
-        const void* ptr = bitmap.getPixels();
-        rsAllocationUpdateFromBitmap(con, (RsAllocation)alloc, e, ptr);
-        bitmap.unlockPixels();
-    }
+    bitmap.lockPixels();
+    const void* ptr = bitmap.getPixels();
+    rsAllocationCopyFromBitmap(con, (RsAllocation)alloc, ptr, bitmap.getSize());
+    bitmap.unlockPixels();
+}
+
+static void
+nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jobject jbitmap)
+{
+    SkBitmap const * nativeBitmap =
+            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
+    const SkBitmap& bitmap(*nativeBitmap);
+
+    bitmap.lockPixels();
+    void* ptr = bitmap.getPixels();
+    rsAllocationCopyToBitmap(con, (RsAllocation)alloc, ptr, bitmap.getSize());
+    bitmap.unlockPixels();
 }
 
 static void ReleaseBitmapCallback(void *bmp)
@@ -486,44 +462,6 @@
     nativeBitmap->unlockPixels();
 }
 
-static int
-nAllocationCreateBitmapRef(JNIEnv *_env, jobject _this, RsContext con, jint type, jobject jbitmap)
-{
-    SkBitmap * nativeBitmap =
-            (SkBitmap *)_env->GetIntField(jbitmap, gNativeBitmapID);
-
-
-    nativeBitmap->lockPixels();
-    void* ptr = nativeBitmap->getPixels();
-    jint id = (jint)rsAllocationCreateBitmapRef(con, (RsType)type, ptr, nativeBitmap, ReleaseBitmapCallback);
-    return id;
-}
-
-static int
-nAllocationCreateFromAssetStream(JNIEnv *_env, jobject _this, RsContext con, jint dstFmt, jboolean genMips, jint native_asset, jint usage)
-{
-    /*
-    Asset* asset = reinterpret_cast<Asset*>(native_asset);
-    SkBitmap bitmap;
-    SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
-            &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode);
-
-    SkBitmap::Config config = bitmap.getConfig();
-
-    RsElement e = SkBitmapToPredefined(config);
-
-    if (e) {
-        bitmap.lockPixels();
-        const int w = bitmap.width();
-        const int h = bitmap.height();
-        const void* ptr = bitmap.getPixels();
-        jint id = (jint)rsaAllocationCreateFromBitmap(con, w, h, (RsElement)dstFmt, e, genMips, ptr, usage);
-        bitmap.unlockPixels();
-        return id;
-    }
-    */
-    return 0;
-}
 
 static void
 nAllocationSubData1D_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint count, jintArray data, int sizeBytes)
@@ -1266,7 +1204,6 @@
 
 static JNINativeMethod methods[] = {
 {"_nInit",                         "()V",                                     (void*)_nInit },
-{"nInitElements",                  "(IIII)V",                                 (void*)nInitElements },
 
 {"nDeviceCreate",                  "()I",                                     (void*)nDeviceCreate },
 {"nDeviceDestroy",                 "(I)V",                                    (void*)nDeviceDestroy },
@@ -1311,10 +1248,10 @@
 {"rsnAllocationCreateTyped",         "(III)I",                                (void*)nAllocationCreateTyped },
 {"rsnAllocationCreateFromBitmap",    "(IIILandroid/graphics/Bitmap;I)I",      (void*)nAllocationCreateFromBitmap },
 {"rsnAllocationCubeCreateFromBitmap","(IIILandroid/graphics/Bitmap;I)I",      (void*)nAllocationCubeCreateFromBitmap },
-{"rsnAllocationCreateBitmapRef",     "(IILandroid/graphics/Bitmap;)I",        (void*)nAllocationCreateBitmapRef },
-{"rsnAllocationCreateFromAssetStream","(IIII)I",                              (void*)nAllocationCreateFromAssetStream },
 
-{"rsnAllocationUpdateFromBitmap",    "(IILandroid/graphics/Bitmap;)V",        (void*)nAllocationUpdateFromBitmap },
+{"rsnAllocationCopyFromBitmap",      "(IILandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyFromBitmap },
+{"rsnAllocationCopyToBitmap",        "(IILandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyToBitmap },
+
 {"rsnAllocationUploadToTexture",     "(IIZI)V",                               (void*)nAllocationUploadToTexture },
 {"rsnAllocationUploadToBufferObject","(II)V",                                 (void*)nAllocationUploadToBufferObject },
 {"rsnAllocationSyncAll",             "(III)V",                                (void*)nAllocationSyncAll },
diff --git a/libs/rs/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java b/libs/rs/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
index e935fa9..09654ab 100644
--- a/libs/rs/java/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
+++ b/libs/rs/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/libs/rs/rs.spec b/libs/rs/rs.spec
index 97ecca0..14094c4 100644
--- a/libs/rs/rs.spec
+++ b/libs/rs/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/libs/rs/rsAllocation.cpp b/libs/rs/rsAllocation.cpp
index f42be0e..10a5caf 100644
--- a/libs/rs/rsAllocation.cpp
+++ b/libs/rs/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) {