Merge "Add FieldPacker to C++ reflected API."
diff --git a/cpp/Allocation.cpp b/cpp/Allocation.cpp
index 7275261..5dd6af6 100644
--- a/cpp/Allocation.cpp
+++ b/cpp/Allocation.cpp
@@ -37,7 +37,8 @@
 }
 
 Allocation::Allocation(void *id, sp<RS> rs, sp<const Type> t, uint32_t usage) :
-        BaseObj(id, rs) {
+    BaseObj(id, rs), mSelectedY(0), mSelectedZ(0), mSelectedLOD(0),
+    mSelectedFace(RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X) {
 
     if ((usage & ~(RS_ALLOCATION_USAGE_SCRIPT |
                    RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE |
@@ -159,7 +160,7 @@
     rsAllocationGenerateMipmaps(mRS->getContext(), getID());
 }
 
-void Allocation::copy1DRangeFromUnchecked(uint32_t off, size_t count, const void *data,
+void Allocation::copy1DRangeFrom(uint32_t off, size_t count, const void *data,
         size_t dataLen) {
 
     if(count < 1) {
@@ -178,7 +179,7 @@
     rsAllocation1DData(mRS->getContext(), getIDSafe(), off, mSelectedLOD, count, data, dataLen);
 }
 
-void Allocation::copy1DRangeToUnchecked(uint32_t off, size_t count, void *data, size_t dataLen) {
+void Allocation::copy1DRangeTo(uint32_t off, size_t count, void *data, size_t dataLen) {
     if(count < 1) {
         ALOGE("Count must be >= 1.");
         return;
@@ -194,28 +195,8 @@
     rsAllocation1DRead(mRS->getContext(), getIDSafe(), off, mSelectedLOD, count, data, dataLen);
 }
 
-void Allocation::copy1DRangeFrom(uint32_t off, size_t count, const int32_t *d, size_t dataLen) {
-    validateIsInt32();
-    copy1DRangeFromUnchecked(off, count, d, dataLen);
-}
-
-void Allocation::copy1DRangeFrom(uint32_t off, size_t count, const int16_t *d, size_t dataLen) {
-    validateIsInt16();
-    copy1DRangeFromUnchecked(off, count, d, dataLen);
-}
-
-void Allocation::copy1DRangeFrom(uint32_t off, size_t count, const int8_t *d, size_t dataLen) {
-    validateIsInt8();
-    copy1DRangeFromUnchecked(off, count, d, dataLen);
-}
-
-void Allocation::copy1DRangeFrom(uint32_t off, size_t count, const float *d, size_t dataLen) {
-    validateIsFloat32();
-    copy1DRangeFromUnchecked(off, count, d, dataLen);
-}
-
-void Allocation::copy1DRangeFrom(uint32_t off, size_t count, const Allocation *data,
-        uint32_t dataOff) {
+void Allocation::copy1DRangeFrom(uint32_t off, size_t count, sp<const Allocation> data,
+                                 uint32_t dataOff) {
 
     rsAllocationCopy2DRange(mRS->getContext(), getIDSafe(), off, 0,
                             mSelectedLOD, mSelectedFace,
@@ -234,35 +215,14 @@
 }
 
 void Allocation::copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
-                                 const int8_t *data, size_t dataLen) {
+                                 const void *data, size_t dataLen) {
     validate2DRange(xoff, yoff, w, h);
     rsAllocation2DData(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
                        w, h, data, dataLen);
 }
 
 void Allocation::copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
-                                 const int16_t *data, size_t dataLen) {
-    validate2DRange(xoff, yoff, w, h);
-    rsAllocation2DData(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
-                       w, h, data, dataLen);
-}
-
-void Allocation::copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
-                                 const int32_t *data, size_t dataLen) {
-    validate2DRange(xoff, yoff, w, h);
-    rsAllocation2DData(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
-                       w, h, data, dataLen);
-}
-
-void Allocation::copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
-                                 const float *data, size_t dataLen) {
-    validate2DRange(xoff, yoff, w, h);
-    rsAllocation2DData(mRS->getContext(), getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace,
-                       w, h, data, dataLen);
-}
-
-void Allocation::copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
-                                 const Allocation *data, size_t dataLen,
+                                 sp<const Allocation> data, size_t dataLen,
                                  uint32_t dataXoff, uint32_t dataYoff) {
     validate2DRange(xoff, yoff, w, h);
     rsAllocationCopy2DRange(mRS->getContext(), getIDSafe(), xoff, yoff,
@@ -306,7 +266,7 @@
 
 
 android::sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
-                        RsAllocationMipmapControl mips, uint32_t usage) {
+                                                RsAllocationMipmapControl mips, uint32_t usage) {
     void *id = rsAllocationCreateTyped(rs->getContext(), type->getID(), mips, usage, 0);
     if (id == 0) {
         ALOGE("Allocation creation failed.");
@@ -316,8 +276,10 @@
 }
 
 android::sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
-                                    RsAllocationMipmapControl mips, uint32_t usage, void *pointer) {
-    void *id = rsAllocationCreateTyped(rs->getContext(), type->getID(), mips, usage, (uint32_t)pointer);
+                                                RsAllocationMipmapControl mips, uint32_t usage,
+                                                void *pointer) {
+    void *id = rsAllocationCreateTyped(rs->getContext(), type->getID(), mips, usage,
+                                       (uint32_t)pointer);
     if (id == 0) {
         ALOGE("Allocation creation failed.");
     }
@@ -325,21 +287,25 @@
 }
 
 android::sp<Allocation> Allocation::createTyped(sp<RS> rs, sp<const Type> type,
-        uint32_t usage) {
+                                                uint32_t usage) {
     return createTyped(rs, type, RS_ALLOCATION_MIPMAP_NONE, usage);
 }
 
 android::sp<Allocation> Allocation::createSized(sp<RS> rs, sp<const Element> e,
-        size_t count, uint32_t usage) {
-
+                                                size_t count, uint32_t usage) {
     Type::Builder b(rs, e);
     b.setX(count);
     sp<const Type> t = b.create();
 
-    void *id = rsAllocationCreateTyped(rs->getContext(), t->getID(),
-        RS_ALLOCATION_MIPMAP_NONE, usage, 0);
-    if (id == 0) {
-        ALOGE("Allocation creation failed.");
-    }
-    return new Allocation(id, rs, t, usage);
+    return createTyped(rs, t, usage);
+}
+
+android::sp<Allocation> Allocation::createSized2D(sp<RS> rs, sp<const Element> e,
+                                                  size_t x, size_t y, uint32_t usage) {
+    Type::Builder b(rs, e);
+    b.setX(x);
+    b.setY(y);
+    sp<const Type> t = b.create();
+
+    return createTyped(rs, t, usage);
 }
diff --git a/cpp/RenderScript.cpp b/cpp/RenderScript.cpp
index fe4057d..4a96dce 100644
--- a/cpp/RenderScript.cpp
+++ b/cpp/RenderScript.cpp
@@ -161,3 +161,7 @@
 void RS::setMessageHandler(MessageHandlerFunc_t func) {
     mMessageFunc  = func;
 }
+
+void RS::finish() {
+    rsContextFinish(mContext);
+}
diff --git a/cpp/ScriptIntrinsics.cpp b/cpp/ScriptIntrinsics.cpp
index 2150d40..7436351 100644
--- a/cpp/ScriptIntrinsics.cpp
+++ b/cpp/ScriptIntrinsics.cpp
@@ -23,12 +23,12 @@
 using namespace android;
 using namespace renderscriptCpp;
 
-ScriptIntrinsic::ScriptIntrinsic(sp<RS> rs, int id, Element *e)
+ScriptIntrinsic::ScriptIntrinsic(sp<RS> rs, int id, sp<const Element> e)
     : Script(NULL, rs) {
-    mID = rsScriptIntrinsicCreate(rs->getContext(), id, e);
+    mID = rsScriptIntrinsicCreate(rs->getContext(), id, e->getID());
 }
 
-ScriptIntrinsicBlend::ScriptIntrinsicBlend(sp<RS> rs, Element *e)
+ScriptIntrinsicBlend::ScriptIntrinsicBlend(sp<RS> rs, sp<const Element> e)
     : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_BLEND, e) {
 
 }
@@ -95,7 +95,7 @@
     Script::forEach(35, in, out, NULL, 0);
 }
 
-ScriptIntrinsicBlur::ScriptIntrinsicBlur(sp<RS> rs, Element *e)
+ScriptIntrinsicBlur::ScriptIntrinsicBlur(sp<RS> rs, sp<const Element> e)
     : ScriptIntrinsic(rs, RS_SCRIPT_INTRINSIC_ID_BLUR, e) {
 
 }
diff --git a/cpp/rsCppStructs.h b/cpp/rsCppStructs.h
index 386da1a..f1376b0 100644
--- a/cpp/rsCppStructs.h
+++ b/cpp/rsCppStructs.h
@@ -56,6 +56,8 @@
 
     RsContext getContext() { return mContext; }
 
+    void finish();
+
  private:
     bool init(int targetApi, bool forceCpu);
     static void * threadProc(void *);
@@ -219,25 +221,17 @@
 
     void generateMipmaps();
 
-    void copy1DRangeFromUnchecked(uint32_t off, size_t count, const void *data, size_t dataLen);
-    void copy1DRangeToUnchecked(uint32_t off, size_t count, void *data, size_t dataLen);
+    void copy1DRangeFrom(uint32_t off, size_t count, const void *data, size_t dataLen);
+    void copy1DRangeTo(uint32_t off, size_t count, void *data, size_t dataLen);
 
-    void copy1DRangeFrom(uint32_t off, size_t count, const int32_t* d, size_t dataLen);
-    void copy1DRangeFrom(uint32_t off, size_t count, const int16_t* d, size_t dataLen);
-    void copy1DRangeFrom(uint32_t off, size_t count, const int8_t* d, size_t dataLen);
-    void copy1DRangeFrom(uint32_t off, size_t count, const float* d, size_t dataLen);
-    void copy1DRangeFrom(uint32_t off, size_t count, const Allocation *data, uint32_t dataOff);
+    void copy1DRangeFrom(uint32_t off, size_t count, sp<const Allocation> data, uint32_t dataOff);
 
     void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
-                         const int32_t *data, size_t dataLen);
+                         const void *data, size_t dataLen);
+    //TODO: add copy2DRangeTo
+
     void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
-                         const int16_t *data, size_t dataLen);
-    void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
-                         const int8_t *data, size_t dataLen);
-    void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
-                         const float *data, size_t dataLen);
-    void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
-                         const Allocation *data, size_t dataLen,
+                         sp<const Allocation> data, size_t dataLen,
                          uint32_t dataXoff, uint32_t dataYoff);
 
     void resize(int dimX);
@@ -252,6 +246,10 @@
                                    uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
     static sp<Allocation> createSized(sp<RS> rs, sp<const Element> e, size_t count,
                                    uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
+    static sp<Allocation> createSized2D(sp<RS> rs, sp<const Element> e,
+                                        size_t x, size_t y,
+                                        uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
+
 
 };
 
@@ -595,12 +593,12 @@
 
 class ScriptIntrinsic : public Script {
  protected:
-    ScriptIntrinsic(sp<RS> rs, int id, Element *e);
+    ScriptIntrinsic(sp<RS> rs, int id, sp<const Element> e);
 };
 
 class ScriptIntrinsicBlend : public ScriptIntrinsic {
  public:
-    ScriptIntrinsicBlend(sp<RS> rs, Element *e);
+    ScriptIntrinsicBlend(sp<RS> rs, sp <const Element> e);
     void blendClear(sp<Allocation> in, sp<Allocation> out);
     void blendSrc(sp<Allocation> in, sp<Allocation> out);
     void blendDst(sp<Allocation> in, sp<Allocation> out);
@@ -620,7 +618,7 @@
 
 class ScriptIntrinsicBlur : public ScriptIntrinsic {
  public:
-    ScriptIntrinsicBlur(sp<RS> rs, Element *e);
+    ScriptIntrinsicBlur(sp<RS> rs, sp <const Element> e);
     void blur(sp<Allocation> in, sp<Allocation> out);
     void setRadius(float radius);
 };
diff --git a/tests/cppallocation/Android.mk b/tests/cppallocation/Android.mk
new file mode 100644
index 0000000..32e1844
--- /dev/null
+++ b/tests/cppallocation/Android.mk
@@ -0,0 +1,34 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= \
+	multiply.rs \
+	compute.cpp
+
+LOCAL_SHARED_LIBRARIES := \
+	libRS \
+	libRScpp \
+	libz \
+	libcutils \
+	libutils \
+	libEGL \
+	libGLESv1_CM \
+	libGLESv2 \
+	libui \
+	libbcc \
+	libbcinfo \
+	libgui
+
+LOCAL_MODULE:= rstest-cppallocation
+
+LOCAL_MODULE_TAGS := tests
+
+intermediates := $(call intermediates-dir-for,STATIC_LIBRARIES,libRS,TARGET,)
+
+LOCAL_C_INCLUDES += frameworks/rs/cpp
+LOCAL_C_INCLUDES += frameworks/rs
+LOCAL_C_INCLUDES += $(intermediates)
+
+
+include $(BUILD_EXECUTABLE)
+
diff --git a/tests/cppallocation/compute.cpp b/tests/cppallocation/compute.cpp
new file mode 100644
index 0000000..214ffe9
--- /dev/null
+++ b/tests/cppallocation/compute.cpp
@@ -0,0 +1,63 @@
+
+#include "RenderScript.h"
+
+#include "ScriptC_multiply.h"
+
+using namespace android;
+using namespace renderscriptCpp;
+
+int main(int argc, char** argv)
+{
+
+    uint32_t numElems = 1024;
+
+    if (argc >= 2) {
+        int tempNumElems = atoi(argv[1]);
+        if (tempNumElems < 1) {
+            printf("numElems must be greater than 0\n");
+            return 1;
+        }
+        numElems = (uint32_t) tempNumElems;
+    }
+
+    sp<RS> rs = new RS();
+
+    bool r = rs->init();
+
+    sp<const Element> e = Element::U32(rs);
+
+    Type::Builder tb(rs, e);
+    tb.setX(numElems);
+    sp<const Type> t = tb.create();
+
+    sp<Allocation> ain = Allocation::createTyped(rs, t);
+    sp<Allocation> aout = Allocation::createTyped(rs, t);
+
+    sp<ScriptC_multiply> sc = new ScriptC_multiply(rs, NULL, 0);
+
+    uint32_t* buf = new uint32_t[numElems];
+    for (uint32_t ct=0; ct < numElems; ct++) {
+        buf[ct] = (uint32_t)ct;
+    }
+
+    ain->copy1DRangeFrom(0, numElems, buf, numElems*sizeof(uint32_t));
+
+    sc->forEach_multiply(ain, aout);
+
+    aout->copy1DRangeTo(0, numElems, buf, numElems*sizeof(uint32_t));
+
+    for (uint32_t ct=0; ct < numElems; ct++) {
+        if (buf[ct] !=  ct * 2) {
+            printf("Mismatch at location %d: %u\n", ct, buf[ct]);
+            return 1;
+        }
+    }
+
+    printf("Test successful with %u elems!\n", numElems);
+
+    sc.clear();
+    t.clear();
+    e.clear();
+    ain.clear();
+    aout.clear();
+}
diff --git a/tests/cppallocation/multiply.rs b/tests/cppallocation/multiply.rs
new file mode 100644
index 0000000..d1ffefb
--- /dev/null
+++ b/tests/cppallocation/multiply.rs
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2012 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.
+ */
+
+#pragma version(1)
+#pragma rs java_package_name(unused)
+#pragma rs_fp_relaxed
+
+uint32_t __attribute__((kernel)) multiply(uint32_t in) {
+    return in * 2;
+}
+
+
diff --git a/tests/cppbasic/compute.cpp b/tests/cppbasic/compute.cpp
index fef116f..e015165 100644
--- a/tests/cppbasic/compute.cpp
+++ b/tests/cppbasic/compute.cpp
@@ -39,8 +39,7 @@
     for (uint32_t ct=0; ct < t->getCount(); ct++) {
         buf[ct] = ct | (ct << 16);
     }
-    //ain->copy1DRangeFrom(0, 128*128, (int32_t *)buf, 128*128*4);
-    ain->copy1DRangeFromUnchecked(0, t->getCount(), buf, t->getCount()*4);
+    ain->copy1DRangeFrom(0, t->getCount(), buf, t->getCount()*4);
 
     sc->forEach_root(ain, aout);
     printf("for each done\n");
diff --git a/tests/latency/latency.cpp b/tests/latency/latency.cpp
index e6aaac9..b6e45e1 100644
--- a/tests/latency/latency.cpp
+++ b/tests/latency/latency.cpp
@@ -66,7 +66,7 @@
 
     uint32_t temp;
 
-    aout->copy1DRangeFromUnchecked(0, 1, &temp, sizeof(temp));
+    rs->finish();
 
     gettimeofday(&stop, NULL);