More RS cpp binding work.  All classes for
compute should be partially implemented at this time.

Change-Id: Iddf9405cc69513b708975d20783395f0be04c680
diff --git a/libs/rs/Allocation.h b/libs/rs/Allocation.h
index 059e61d..c9e00a4 100644
--- a/libs/rs/Allocation.h
+++ b/libs/rs/Allocation.h
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#ifndef __ANDROID_ALLOCATION_H__
+#define __ANDROID_ALLOCATION_H__
 
 #include <pthread.h>
 #include <rs.h>
@@ -22,7 +24,7 @@
 #include "Type.h"
 #include "Element.h"
 
-class Allocation : BaseObj {
+class Allocation : public BaseObj {
 protected:
     const Type *mType;
     uint32_t mUsage;
@@ -121,4 +123,4 @@
 
 };
 
-
+#endif
diff --git a/libs/rs/Android.mk b/libs/rs/Android.mk
index b7cc177..45ed453 100644
--- a/libs/rs/Android.mk
+++ b/libs/rs/Android.mk
@@ -132,7 +132,9 @@
 	BaseObj.cpp \
 	Element.cpp \
 	Type.cpp \
-	Allocation.cpp
+	Allocation.cpp \
+	Script.cpp \
+	ScriptC.cpp
 
 LOCAL_SHARED_LIBRARIES += libz libcutils libutils libEGL libGLESv1_CM libGLESv2 libui libbcc libbcinfo libgui
 
diff --git a/libs/rs/BaseObj.cpp b/libs/rs/BaseObj.cpp
index b2f5450..66e6fac 100644
--- a/libs/rs/BaseObj.cpp
+++ b/libs/rs/BaseObj.cpp
@@ -27,6 +27,11 @@
     return mID;
 }
 
+void * BaseObj::getObjID(const BaseObj *o) {
+    return o == NULL ? NULL : o->getID();
+}
+
+
 BaseObj::BaseObj(void *id, RenderScript *rs) {
     mRS = rs;
     mID = id;
diff --git a/libs/rs/BaseObj.h b/libs/rs/BaseObj.h
index 91f3714..79761b1 100644
--- a/libs/rs/BaseObj.h
+++ b/libs/rs/BaseObj.h
@@ -27,6 +27,9 @@
 protected:
     friend class Element;
     friend class Type;
+    friend class Allocation;
+    friend class Script;
+    friend class ScriptC;
 
     void *mID;
     RenderScript *mRS;
@@ -37,6 +40,8 @@
     BaseObj(void *id, RenderScript *rs);
     void checkValid();
 
+    static void * getObjID(const BaseObj *o);
+
 public:
 
     virtual ~BaseObj();
diff --git a/libs/rs/Element.cpp b/libs/rs/Element.cpp
index c621c82..d193892 100644
--- a/libs/rs/Element.cpp
+++ b/libs/rs/Element.cpp
@@ -24,41 +24,40 @@
 
 const Element * Element::getSubElement(uint32_t index) {
     if (!mVisibleElementMap.size()) {
-        ALOGE("Element contains no sub-elements");
-        return NULL;
+        mRS->throwError("Element contains no sub-elements");
     }
     if (index >= mVisibleElementMap.size()) {
-        ALOGE("Illegal sub-element index");
+        mRS->throwError("Illegal sub-element index");
     }
     return mElements[mVisibleElementMap[index]];
 }
 
 const char * Element::getSubElementName(uint32_t index) {
     if (!mVisibleElementMap.size()) {
-        ALOGE("Element contains no sub-elements");
+        mRS->throwError("Element contains no sub-elements");
     }
     if (index >= mVisibleElementMap.size()) {
-        ALOGE("Illegal sub-element index");
+        mRS->throwError("Illegal sub-element index");
     }
     return mElementNames[mVisibleElementMap[index]];
 }
 
 size_t Element::getSubElementArraySize(uint32_t index) {
     if (!mVisibleElementMap.size()) {
-        ALOGE("Element contains no sub-elements");
+        mRS->throwError("Element contains no sub-elements");
     }
     if (index >= mVisibleElementMap.size()) {
-        ALOGE("Illegal sub-element index");
+        mRS->throwError("Illegal sub-element index");
     }
     return mArraySizes[mVisibleElementMap[index]];
 }
 
 uint32_t Element::getSubElementOffsetBytes(uint32_t index) {
     if (mVisibleElementMap.size()) {
-        ALOGE("Element contains no sub-elements");
+        mRS->throwError("Element contains no sub-elements");
     }
     if (index >= mVisibleElementMap.size()) {
-        ALOGE("Illegal sub-element index");
+        mRS->throwError("Illegal sub-element index");
     }
     return mOffsetInBytes[mVisibleElementMap[index]];
 }
@@ -293,54 +292,45 @@
 }
 
 const Element * Element::createUser(RenderScript *rs, RsDataType dt) {
-    ALOGE("createUser %p %i", rs, dt);
     void * id = rsElementCreate(rs->mContext, dt, RS_KIND_USER, false, 1);
     return new Element(id, rs, dt, RS_KIND_USER, false, 1);
 }
 
 const Element * Element::createVector(RenderScript *rs, RsDataType dt, uint32_t size) {
     if (size < 2 || size > 4) {
-        ALOGE("Vector size out of range 2-4.");
-        return NULL;
+        rs->throwError("Vector size out of range 2-4.");
     }
     void *id = rsElementCreate(rs->mContext, dt, RS_KIND_USER, false, size);
     return new Element(id, rs, dt, RS_KIND_USER, false, size);
 }
 
 const Element * Element::createPixel(RenderScript *rs, RsDataType dt, RsDataKind dk) {
-    ALOGE("createPixel %p %i %i", rs, dt, dk);
     if (!(dk == RS_KIND_PIXEL_L ||
           dk == RS_KIND_PIXEL_A ||
           dk == RS_KIND_PIXEL_LA ||
           dk == RS_KIND_PIXEL_RGB ||
           dk == RS_KIND_PIXEL_RGBA ||
           dk == RS_KIND_PIXEL_DEPTH)) {
-        ALOGE("Unsupported DataKind");
-        return NULL;
+        rs->throwError("Unsupported DataKind");
     }
     if (!(dt == RS_TYPE_UNSIGNED_8 ||
           dt == RS_TYPE_UNSIGNED_16 ||
           dt == RS_TYPE_UNSIGNED_5_6_5 ||
           dt == RS_TYPE_UNSIGNED_4_4_4_4 ||
           dt == RS_TYPE_UNSIGNED_5_5_5_1)) {
-        ALOGE("Unsupported DataType");
-        return NULL;
+        rs->throwError("Unsupported DataType");
     }
     if (dt == RS_TYPE_UNSIGNED_5_6_5 && dk != RS_KIND_PIXEL_RGB) {
-        ALOGE("Bad kind and type combo");
-        return NULL;
+        rs->throwError("Bad kind and type combo");
     }
     if (dt == RS_TYPE_UNSIGNED_5_5_5_1 && dk != RS_KIND_PIXEL_RGBA) {
-        ALOGE("Bad kind and type combo");
-        return NULL;
+        rs->throwError("Bad kind and type combo");
     }
     if (dt == RS_TYPE_UNSIGNED_4_4_4_4 && dk != RS_KIND_PIXEL_RGBA) {
-        ALOGE("Bad kind and type combo");
-        return NULL;
+        rs->throwError("Bad kind and type combo");
     }
     if (dt == RS_TYPE_UNSIGNED_16 && dk != RS_KIND_PIXEL_DEPTH) {
-        ALOGE("Bad kind and type combo");
-        return NULL;
+        rs->throwError("Bad kind and type combo");
     }
 
     int size = 1;
diff --git a/libs/rs/RenderScript.cpp b/libs/rs/RenderScript.cpp
index 58d1ce1..39f1024 100644
--- a/libs/rs/RenderScript.cpp
+++ b/libs/rs/RenderScript.cpp
@@ -76,6 +76,13 @@
     return true;
 }
 
+void RenderScript::throwError(const char *err) const {
+    ALOGE("RS CPP error: %s", err);
+    int * v = NULL;
+    v[0] = 0;
+}
+
+
 void * RenderScript::threadProc(void *vrsc) {
     RenderScript *rs = static_cast<RenderScript *>(vrsc);
     size_t rbuf_size = 256;
diff --git a/libs/rs/RenderScript.h b/libs/rs/RenderScript.h
index 2d352be..0eb6a6d 100644
--- a/libs/rs/RenderScript.h
+++ b/libs/rs/RenderScript.h
@@ -33,6 +33,8 @@
     friend class Allocation;
     friend class Element;
     friend class Type;
+    friend class Script;
+    friend class ScriptC;
 
 public:
     RenderScript();
@@ -144,6 +146,7 @@
 
 
 
+    void throwError(const char *err) const;
 
     static void * threadProc(void *);
 
diff --git a/libs/rs/Script.cpp b/libs/rs/Script.cpp
new file mode 100644
index 0000000..b6112dd
--- /dev/null
+++ b/libs/rs/Script.cpp
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2008-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.
+ */
+
+#include <utils/Log.h>
+#include <malloc.h>
+
+#include "RenderScript.h"
+#include "Element.h"
+#include "Type.h"
+#include "Allocation.h"
+#include "Script.h"
+
+void Script::invoke(uint32_t slot, const void *v, size_t len) {
+    rsScriptInvokeV(mRS->mContext, getID(), slot, v, len);
+}
+
+void Script::forEach(uint32_t slot, const Allocation *ain, const Allocation *aout,
+                       const void *usr, size_t usrLen) {
+    if ((ain == NULL) && (aout == NULL)) {
+        mRS->throwError("At least one of ain or aout is required to be non-null.");
+    }
+    void *in_id = BaseObj::getObjID(ain);
+    void *out_id = BaseObj::getObjID(aout);
+    rsScriptForEach(mRS->mContext, getID(), slot, in_id, out_id, usr, usrLen);
+}
+
+
+Script::Script(void *id, RenderScript *rs) : BaseObj(id, rs) {
+}
+
+
+void Script::bindAllocation(const Allocation *va, uint32_t slot) {
+    rsScriptBindAllocation(mRS->mContext, getID(), BaseObj::getObjID(va), slot);
+}
+
+
+void Script::setVar(uint32_t index, const BaseObj *o) {
+    rsScriptSetVarObj(mRS->mContext, getID(), index, (o == NULL) ? 0 : o->getID());
+}
+
+void Script::setVar(uint32_t index, const void *v, size_t len) {
+    rsScriptSetVarV(mRS->mContext, getID(), index, v, len);
+}
+
+
+
+void Script::FieldBase::init(RenderScript *rs, uint32_t dimx, uint32_t usages) {
+    mAllocation = Allocation::createSized(rs, mElement, dimx, RS_ALLOCATION_USAGE_SCRIPT | usages);
+}
+
+//Script::FieldBase::FieldBase() {
+//}
+
+
diff --git a/libs/rs/Script.h b/libs/rs/Script.h
new file mode 100644
index 0000000..54d1e40
--- /dev/null
+++ b/libs/rs/Script.h
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2008-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.
+ */
+
+#ifndef __ANDROID_SCRIPT_H__
+#define __ANDROID_SCRIPT_H__
+
+#include <pthread.h>
+#include <rs.h>
+
+#include "RenderScript.h"
+#include "Allocation.h"
+
+class Type;
+class Element;
+class Allocation;
+
+class Script : public BaseObj {
+protected:
+    Script(void *id, RenderScript *rs);
+    void forEach(uint32_t slot, const Allocation *in, const Allocation *out, const void *v, size_t);
+    void bindAllocation(const Allocation *va, uint32_t slot);
+    void setVar(uint32_t index, const void *, size_t len);
+    void setVar(uint32_t index, const BaseObj *o);
+    void invoke(uint32_t slot, const void *v, size_t len);
+
+
+    void invoke(uint32_t slot) {
+        invoke(slot, NULL, 0);
+    }
+    void setVar(uint32_t index, float v) {
+        setVar(index, &v, sizeof(v));
+    }
+    void setVar(uint32_t index, double v) {
+        setVar(index, &v, sizeof(v));
+    }
+    void setVar(uint32_t index, int32_t v) {
+        setVar(index, &v, sizeof(v));
+    }
+    void setVar(uint32_t index, int64_t v) {
+        setVar(index, &v, sizeof(v));
+    }
+    void setVar(uint32_t index, bool v) {
+        setVar(index, &v, sizeof(v));
+    }
+
+public:
+    class FieldBase {
+    protected:
+        const Element *mElement;
+        Allocation *mAllocation;
+
+        void init(RenderScript *rs, uint32_t dimx, uint32_t usages = 0);
+
+    public:
+        const Element *getElement() {
+            return mElement;
+        }
+
+        const Type *getType() {
+            return mAllocation->getType();
+        }
+
+        const Allocation *getAllocation() {
+            return mAllocation;
+        }
+
+        //void updateAllocation();
+    };
+};
+
+#endif
diff --git a/libs/rs/ScriptC.cpp b/libs/rs/ScriptC.cpp
new file mode 100644
index 0000000..53d75b8
--- /dev/null
+++ b/libs/rs/ScriptC.cpp
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2008-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.
+ */
+
+#include <utils/Log.h>
+#include <malloc.h>
+
+#include "ScriptC.h"
+
+ScriptC::ScriptC(RenderScript *rs,
+                 const char *codeTxt, size_t codeLength,
+                 const char *cachedName, size_t cachedNameLength,
+                 const char *cacheDir, size_t cacheDirLength)
+: Script(NULL, rs) {
+    mID = rsScriptCCreate(rs->mContext, cachedName, cachedNameLength,
+                          cacheDir, cacheDirLength, codeTxt, codeLength);
+}
+
diff --git a/libs/rs/ScriptC.h b/libs/rs/ScriptC.h
new file mode 100644
index 0000000..25f00ec
--- /dev/null
+++ b/libs/rs/ScriptC.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2008-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.
+ */
+
+#ifndef __ANDROID_SCRIPTC_H__
+#define __ANDROID_SCRIPTC_H__
+
+#include <pthread.h>
+#include <rs.h>
+
+#include "Script.h"
+
+class ScriptC : public Script {
+protected:
+    ScriptC(RenderScript *rs, void *txt, size_t len);
+    ScriptC(RenderScript *rs,
+            const char *codeTxt, size_t codeLength,
+            const char *cachedName, size_t cachedNameLength,
+            const char *cacheDir, size_t cacheDirLength);
+
+};
+
+#endif
diff --git a/libs/rs/Type.h b/libs/rs/Type.h
index fb4e8b1..53481c3 100644
--- a/libs/rs/Type.h
+++ b/libs/rs/Type.h
@@ -19,7 +19,7 @@
 
 #include <rs.h>
 #include "RenderScript.h"
-#include "BaseObj.h"
+#include "Element.h"
 
 class Type : public BaseObj {
 protected:
diff --git a/libs/rs/tests/compute.cpp b/libs/rs/tests/compute.cpp
index 702b974..28b135f 100644
--- a/libs/rs/tests/compute.cpp
+++ b/libs/rs/tests/compute.cpp
@@ -10,8 +10,6 @@
     RenderScript *rs = new RenderScript();
     printf("New RS %p\n", rs);
 
-    //usleep(100000);
-
     bool r = rs->init(16);
     printf("Init returned %i\n", r);
 
@@ -29,8 +27,6 @@
     printf("Allocation %p\n", a1);
 
 
-    //usleep(1000000);
-
 
     printf("Deleting stuff\n");
     delete t;