Implement more type checks on Allocations.
Add tracking for allocations created using the "sized" helper.
Add more param validation for data upload calls.
diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java
index 30c81ab..957f2dd 100644
--- a/graphics/java/android/renderscript/Allocation.java
+++ b/graphics/java/android/renderscript/Allocation.java
@@ -48,50 +48,49 @@
     }
 
     public void data(int[] d) {
-        int size;
-        if(mType != null && mType.mElement != null) {
-            size = mType.mElement.mSize;
-            for(int ct=0; ct < mType.mValues.length; ct++) {
-                if(mType.mValues[ct] != 0) {
-                    size *= mType.mValues[ct];
-                }
-            }
-            if((d.length * 4) < size) {
-                throw new IllegalArgumentException("Array too small for allocation type.");
-            }
-            Log.e("rs", "Alloc data size=" + size);
-            mRS.nAllocationData(mID, d, size);
-            return;
-        }
-        mRS.nAllocationData(mID, d, d.length * 4);
+        subData1D(0, mType.getElementCount(), d);
+    }
+    public void data(short[] d) {
+        subData1D(0, mType.getElementCount(), d);
+    }
+    public void data(byte[] d) {
+        subData1D(0, mType.getElementCount(), d);
+    }
+    public void data(float[] d) {
+        subData1D(0, mType.getElementCount(), d);
     }
 
-    public void data(float[] d) {
-        int size;
-        if(mType != null && mType.mElement != null) {
-            size = mType.mElement.mSize;
-            for(int ct=0; ct < mType.mValues.length; ct++) {
-                if(mType.mValues[ct] != 0) {
-                    size *= mType.mValues[ct];
-                }
-            }
-            if((d.length * 4) < size) {
-                throw new IllegalArgumentException("Array too small for allocation type.");
-            }
-            Log.e("rs", "Alloc data size=" + size);
-            mRS.nAllocationData(mID, d, size);
-            return;
+    private void data1DChecks(int off, int count, int len, int dataSize) {
+        if((off < 0) || (count < 1) || ((off + count) > mType.getElementCount())) {
+            throw new IllegalArgumentException("Offset or Count out of bounds.");
         }
-        mRS.nAllocationData(mID, d, d.length * 4);
+        if((len) < dataSize) {
+            throw new IllegalArgumentException("Array too small for allocation type.");
+        }
     }
 
     public void subData1D(int off, int count, int[] d) {
-        mRS.nAllocationSubData1D(mID, off, count, d, count * 4);
+        int dataSize = mType.mElement.getSizeBytes() * count;
+        data1DChecks(off, count, d.length * 4, dataSize);
+        mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
+    }
+    public void subData1D(int off, int count, short[] d) {
+        int dataSize = mType.mElement.getSizeBytes() * count;
+        data1DChecks(off, count, d.length * 2, dataSize);
+        mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
+    }
+    public void subData1D(int off, int count, byte[] d) {
+        int dataSize = mType.mElement.getSizeBytes() * count;
+        data1DChecks(off, count, d.length, dataSize);
+        mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
+    }
+    public void subData1D(int off, int count, float[] d) {
+        int dataSize = mType.mElement.getSizeBytes() * count;
+        data1DChecks(off, count, d.length * 4, dataSize);
+        mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
     }
 
-    public void subData1D(int off, int count, float[] d) {
-        mRS.nAllocationSubData1D(mID, off, count, d, d.length * 4);
-    }
+
 
     public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
         mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
@@ -213,11 +212,15 @@
     static public Allocation createSized(RenderScript rs, Element e, int count)
         throws IllegalArgumentException {
 
-        int id = rs.nAllocationCreateSized(e.mID, count);
+        Type.Builder b = new Type.Builder(rs, e);
+        b.add(Dimension.X, count);
+        Type t = b.create();
+
+        int id = rs.nAllocationCreateTyped(t.mID);
         if(id == 0) {
             throw new IllegalStateException("Bad element.");
         }
-        return new Allocation(id, rs, null);
+        return new Allocation(id, rs, t);
     }
 
     static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
diff --git a/graphics/java/android/renderscript/Element.java b/graphics/java/android/renderscript/Element.java
index 04c36fd..0a586c4 100644
--- a/graphics/java/android/renderscript/Element.java
+++ b/graphics/java/android/renderscript/Element.java
@@ -26,18 +26,40 @@
     int mSize;
     Entry[] mEntries;
 
+    int getSizeBytes() {
+        return mSize;
+    }
+    int getComponentCount() {
+        return mEntries.length;
+    }
+    Element.DataType getComponentDataType(int num) {
+        return mEntries[num].mType;
+    }
+    Element.DataKind getComponentDataKind(int num) {
+        return mEntries[num].mKind;
+    }
+    boolean getComponentIsNormalized(int num) {
+        return mEntries[num].mIsNormalized;
+    }
+    int getComponentBits(int num) {
+        return mEntries[num].mBits;
+    }
+    String getComponentName(int num) {
+        return mEntries[num].mName;
+    }
+
     static class Entry {
-        Element mElement;
+        //Element mElement;
         Element.DataType mType;
         Element.DataKind mKind;
         boolean mIsNormalized;
         int mBits;
         String mName;
 
-        Entry(Element e, int bits) {
-            mElement = e;
-            int mBits = bits;
-        }
+        //Entry(Element e, int bits) {
+            //mElement = e;
+            //int mBits = bits;
+        //}
 
         Entry(DataType dt, DataKind dk, boolean isNorm, int bits, String name) {
             mType = dt;
@@ -266,14 +288,11 @@
         int bits = 0;
         for (int ct=0; ct < e.mEntries.length; ct++) {
             Entry en = e.mEntries[ct];
-            if(en.mElement !=  null) {
+            //if(en.mElement !=  null) {
                 //rs.nElementAdd(en.mElement.mID);
-            } else {
-                int norm = 0;
-                if (en.mIsNormalized) {
-                    norm = 1;
-                }
-                rs.nElementAdd(en.mKind.mID, en.mType.mID, norm, en.mBits, en.mName);
+            //} else
+            {
+                rs.nElementAdd(en.mKind.mID, en.mType.mID, en.mIsNormalized, en.mBits, en.mName);
                 bits += en.mBits;
             }
         }
@@ -308,11 +327,11 @@
             mEntryCount++;
         }
 
-        public Builder add(Element e) throws IllegalArgumentException {
-            Entry en = new Entry(e, e.mSize * 8);
-            addEntry(en);
-            return this;
-        }
+        //public Builder add(Element e) throws IllegalArgumentException {
+            //Entry en = new Entry(e, e.mSize * 8);
+            //addEntry(en);
+            //return this;
+        //}
 
         public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits, String name) {
             Entry en = new Entry(dt, dk, isNormalized, bits, name);
diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java
index d35c5e3..5831d13 100644
--- a/graphics/java/android/renderscript/RenderScript.java
+++ b/graphics/java/android/renderscript/RenderScript.java
@@ -80,7 +80,7 @@
     native int  nFileOpen(byte[] name);
 
     native void nElementBegin();
-    native void nElementAdd(int kind, int type, int norm, int bits, String s);
+    native void nElementAdd(int kind, int type, boolean norm, int bits, String s);
     native int  nElementCreate();
 
     native void nTypeBegin(int elementID);
@@ -90,17 +90,19 @@
     native void nTypeSetupFields(Type t, int[] types, int[] bits, Field[] IDs);
 
     native int  nAllocationCreateTyped(int type);
-    native int  nAllocationCreateSized(int elem, int count);
+    //native int  nAllocationCreateSized(int elem, int count);
     native int  nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp);
     native int  nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp);
     native int  nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream);
 
     native void nAllocationUploadToTexture(int alloc, int baseMioLevel);
     native void nAllocationUploadToBufferObject(int alloc);
-    native void nAllocationData(int id, int[] d, int sizeBytes);
-    native void nAllocationData(int id, float[] d, int sizeBytes);
+
     native void nAllocationSubData1D(int id, int off, int count, int[] d, int sizeBytes);
+    native void nAllocationSubData1D(int id, int off, int count, short[] d, int sizeBytes);
+    native void nAllocationSubData1D(int id, int off, int count, byte[] d, int sizeBytes);
     native void nAllocationSubData1D(int id, int off, int count, float[] d, int sizeBytes);
+
     native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes);
     native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes);
     native void nAllocationRead(int id, int[] d);
diff --git a/graphics/java/android/renderscript/SimpleMesh.java b/graphics/java/android/renderscript/SimpleMesh.java
index 5d87654..dc74c61 100644
--- a/graphics/java/android/renderscript/SimpleMesh.java
+++ b/graphics/java/android/renderscript/SimpleMesh.java
@@ -162,7 +162,6 @@
         }
 
         public SimpleMesh create() {
-            Log.e("rs", "SimpleMesh create");
             SimpleMesh sm = internalCreate(mRS, this);
             sm.mVertexTypes = new Type[mVertexTypeCount];
             for(int ct=0; ct < mVertexTypeCount; ct++) {
@@ -177,7 +176,7 @@
     public static class TriangleMeshBuilder {
         float mVtxData[];
         int mVtxCount;
-        int mIndexData[];
+        short mIndexData[];
         int mIndexCount;
         RenderScript mRS;
         Element mElement;
@@ -191,7 +190,7 @@
             mVtxCount = 0;
             mIndexCount = 0;
             mVtxData = new float[128];
-            mIndexData = new int[128];
+            mIndexData = new short[128];
             mVtxSize = vtxSize;
             mNorm = norm;
             mTex = tex;
@@ -268,13 +267,13 @@
 
         public void addTriangle(int idx1, int idx2, int idx3) {
             if((mIndexCount + 3) >= mIndexData.length) {
-                int t[] = new int[mIndexData.length * 2];
+                short t[] = new short[mIndexData.length * 2];
                 System.arraycopy(mIndexData, 0, t, 0, mIndexData.length);
                 mIndexData = t;
             }
-            mIndexData[mIndexCount++] = idx1;
-            mIndexData[mIndexCount++] = idx2;
-            mIndexData[mIndexCount++] = idx3;
+            mIndexData[mIndexCount++] = (short)idx1;
+            mIndexData[mIndexCount++] = (short)idx2;
+            mIndexData[mIndexCount++] = (short)idx3;
         }
 
         public SimpleMesh create() {
@@ -309,10 +308,6 @@
             vertexAlloc.data(mVtxData);
             vertexAlloc.uploadToBufferObject();
 
-            // This is safe because length is a pow2
-            for(int ct=0; ct < (mIndexCount+1); ct += 2) {
-                mIndexData[ct >> 1] = mIndexData[ct] | (mIndexData[ct+1] << 16);
-            }
             indexAlloc.data(mIndexData);
             indexAlloc.uploadToBufferObject();
 
diff --git a/graphics/java/android/renderscript/Type.java b/graphics/java/android/renderscript/Type.java
index b6b7adf..df60990 100644
--- a/graphics/java/android/renderscript/Type.java
+++ b/graphics/java/android/renderscript/Type.java
@@ -23,13 +23,74 @@
  *
  **/
 public class Type extends BaseObj {
-    Dimension[] mDimensions;
-    int[] mValues;
+    int mDimX;
+    int mDimY;
+    int mDimZ;
+    boolean mDimLOD;
+    boolean mDimFaces;
+    int mElementCount;
     Element mElement;
+
     private int mNativeCache;
     Class mJavaClass;
 
 
+    public int getX() {
+        return mDimX;
+    }
+    public int getY() {
+        return mDimY;
+    }
+    public int getZ() {
+        return mDimZ;
+    }
+    public boolean getLOD() {
+        return mDimLOD;
+    }
+    public boolean getFaces() {
+        return mDimFaces;
+    }
+    public int getElementCount() {
+        return mElementCount;
+    }
+
+    void calcElementCount() {
+        boolean hasLod = getLOD();
+        int x = getX();
+        int y = getY();
+        int z = getZ();
+        int faces = 1;
+        if(getFaces()) {
+            faces = 6;
+        }
+        if(x == 0) {
+            x = 1;
+        }
+        if(y == 0) {
+            y = 1;
+        }
+        if(z == 0) {
+            z = 1;
+        }
+
+        int count = x * y * z * faces;
+        if(hasLod && (x > 1) && (y > 1) && (z > 1)) {
+            if(x > 1) {
+                x >>= 1;
+            }
+            if(y > 1) {
+                y >>= 1;
+            }
+            if(z > 1) {
+                z >>= 1;
+            }
+
+            count += x * y * z * faces;
+        }
+        mElementCount = count;
+    }
+
+
     Type(int id, RenderScript rs) {
         super(rs);
         mID = id;
@@ -131,12 +192,25 @@
         public Type create() {
             Type t = internalCreate(mRS, this);
             t.mElement = mElement;
-            t.mDimensions = new Dimension[mEntryCount];
-            t.mValues = new int[mEntryCount];
+
             for(int ct=0; ct < mEntryCount; ct++) {
-                t.mDimensions[ct] = mEntries[ct].mDim;
-                t.mValues[ct] = mEntries[ct].mValue;
+                if(mEntries[ct].mDim == Dimension.X) {
+                    t.mDimX = mEntries[ct].mValue;
+                }
+                if(mEntries[ct].mDim == Dimension.Y) {
+                    t.mDimY = mEntries[ct].mValue;
+                }
+                if(mEntries[ct].mDim == Dimension.Z) {
+                    t.mDimZ = mEntries[ct].mValue;
+                }
+                if(mEntries[ct].mDim == Dimension.LOD) {
+                    t.mDimLOD = mEntries[ct].mValue != 0;
+                }
+                if(mEntries[ct].mDim == Dimension.FACE) {
+                    t.mDimFaces = mEntries[ct].mValue != 0;
+                }
             }
+            t.calcElementCount();
             return t;
         }
     }
diff --git a/graphics/jni/android_renderscript_RenderScript.cpp b/graphics/jni/android_renderscript_RenderScript.cpp
index c7b5448..56a4223 100644
--- a/graphics/jni/android_renderscript_RenderScript.cpp
+++ b/graphics/jni/android_renderscript_RenderScript.cpp
@@ -181,7 +181,7 @@
 
 
 static void
-nElementAdd(JNIEnv *_env, jobject _this, jint kind, jint type, jint norm, jint bits, jstring name)
+nElementAdd(JNIEnv *_env, jobject _this, jint kind, jint type, jboolean norm, jint bits, jstring name)
 {
     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
     const char* n = NULL;
@@ -359,14 +359,6 @@
     return (jint) rsAllocationCreateTyped(con, (RsElement)e);
 }
 
-static jint
-nAllocationCreateSized(JNIEnv *_env, jobject _this, jint e, jint count)
-{
-    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
-    LOG_API("nAllocationCreateSized, con(%p), e(%p), count(%i)", con, (RsElement)e, count);
-    return (jint) rsAllocationCreateSized(con, (RsElement)e, count);
-}
-
 static void
 nAllocationUploadToTexture(JNIEnv *_env, jobject _this, jint a, jint mip)
 {
@@ -476,44 +468,44 @@
 
 
 static void
-nAllocationData_i(JNIEnv *_env, jobject _this, jint alloc, jintArray data, int sizeBytes)
-{
-    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
-    jint len = _env->GetArrayLength(data);
-    LOG_API("nAllocationData_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
-    jint *ptr = _env->GetIntArrayElements(data, NULL);
-    rsAllocationData(con, (RsAllocation)alloc, ptr, sizeBytes);
-    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
-}
-
-static void
-nAllocationData_f(JNIEnv *_env, jobject _this, jint alloc, jfloatArray data, int sizeBytes)
-{
-    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
-    jint len = _env->GetArrayLength(data);
-    LOG_API("nAllocationData_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
-    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
-    rsAllocationData(con, (RsAllocation)alloc, ptr, sizeBytes);
-    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
-}
-
-static void
 nAllocationSubData1D_i(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jintArray data, int sizeBytes)
 {
     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
     jint len = _env->GetArrayLength(data);
-    LOG_API("nAllocation1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAllocation)alloc, offset, count, len);
+    LOG_API("nAllocation1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
     jint *ptr = _env->GetIntArrayElements(data, NULL);
     rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
     _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
 }
 
 static void
+nAllocationSubData1D_s(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jshortArray data, int sizeBytes)
+{
+    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
+    jint len = _env->GetArrayLength(data);
+    LOG_API("nAllocation1DSubData_s, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
+    jshort *ptr = _env->GetShortArrayElements(data, NULL);
+    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
+    _env->ReleaseShortArrayElements(data, ptr, JNI_ABORT);
+}
+
+static void
+nAllocationSubData1D_b(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jbyteArray data, int sizeBytes)
+{
+    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
+    jint len = _env->GetArrayLength(data);
+    LOG_API("nAllocation1DSubData_b, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
+    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
+    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
+    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
+}
+
+static void
 nAllocationSubData1D_f(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jfloatArray data, int sizeBytes)
 {
     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
     jint len = _env->GetArrayLength(data);
-    LOG_API("nAllocation1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAllocation)alloc, offset, count, len);
+    LOG_API("nAllocation1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
     jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
     rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
     _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
@@ -1323,7 +1315,7 @@
 {"nFileOpen",                      "([B)I",                                (void*)nFileOpen },
 
 {"nElementBegin",                  "()V",                                  (void*)nElementBegin },
-{"nElementAdd",                    "(IIIILjava/lang/String;)V",            (void*)nElementAdd },
+{"nElementAdd",                    "(IIZILjava/lang/String;)V",            (void*)nElementAdd },
 {"nElementCreate",                 "()I",                                  (void*)nElementCreate },
 
 {"nTypeBegin",                     "(I)V",                                 (void*)nTypeBegin },
@@ -1333,15 +1325,14 @@
 {"nTypeSetupFields",               "(Landroid/renderscript/Type;[I[I[Ljava/lang/reflect/Field;)V", (void*)nTypeSetupFields },
 
 {"nAllocationCreateTyped",         "(I)I",                                 (void*)nAllocationCreateTyped },
-{"nAllocationCreateSized",         "(II)I",                                (void*)nAllocationCreateSized },
 {"nAllocationCreateFromBitmap",    "(IZLandroid/graphics/Bitmap;)I",       (void*)nAllocationCreateFromBitmap },
 {"nAllocationCreateFromBitmapBoxed","(IZLandroid/graphics/Bitmap;)I",      (void*)nAllocationCreateFromBitmapBoxed },
 {"nAllocationCreateFromAssetStream","(IZI)I",                              (void*)nAllocationCreateFromAssetStream },
 {"nAllocationUploadToTexture",     "(II)V",                                (void*)nAllocationUploadToTexture },
 {"nAllocationUploadToBufferObject","(I)V",                                 (void*)nAllocationUploadToBufferObject },
-{"nAllocationData",                "(I[II)V",                              (void*)nAllocationData_i },
-{"nAllocationData",                "(I[FI)V",                              (void*)nAllocationData_f },
 {"nAllocationSubData1D",           "(III[II)V",                            (void*)nAllocationSubData1D_i },
+{"nAllocationSubData1D",           "(III[SI)V",                            (void*)nAllocationSubData1D_s },
+{"nAllocationSubData1D",           "(III[BI)V",                            (void*)nAllocationSubData1D_b },
 {"nAllocationSubData1D",           "(III[FI)V",                            (void*)nAllocationSubData1D_f },
 {"nAllocationSubData2D",           "(IIIII[II)V",                          (void*)nAllocationSubData2D_i },
 {"nAllocationSubData2D",           "(IIIII[FI)V",                          (void*)nAllocationSubData2D_f },