Continue error check improvements and write some docs.

Change-Id: I345b08490da21d9ee715ff1ddc80c1a816b579f4
diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java
index dfd6ac8..9dc291b 100644
--- a/graphics/java/android/renderscript/Allocation.java
+++ b/graphics/java/android/renderscript/Allocation.java
@@ -45,9 +45,8 @@
 
     @Override
     void updateFromNative() {
-        mRS.validate();
-        mName = mRS.nGetName(mID);
-        int typeID = mRS.nAllocationGetType(mID);
+        super.updateFromNative();
+        int typeID = mRS.nAllocationGetType(getID());
         if(typeID != 0) {
             mType = new Type(typeID, mRS);
             mType.updateFromNative();
@@ -60,17 +59,17 @@
 
     public void uploadToTexture(int baseMipLevel) {
         mRS.validate();
-        mRS.nAllocationUploadToTexture(mID, false, baseMipLevel);
+        mRS.nAllocationUploadToTexture(getID(), false, baseMipLevel);
     }
 
     public void uploadToTexture(boolean genMips, int baseMipLevel) {
         mRS.validate();
-        mRS.nAllocationUploadToTexture(mID, genMips, baseMipLevel);
+        mRS.nAllocationUploadToTexture(getID(), genMips, baseMipLevel);
     }
 
     public void uploadToBufferObject() {
         mRS.validate();
-        mRS.nAllocationUploadToBufferObject(mID);
+        mRS.nAllocationUploadToBufferObject(getID());
     }
 
     public void data(int[] d) {
@@ -90,16 +89,15 @@
         subData1D(0, mType.getElementCount(), d);
     }
 
-    public void updateFromBitmap(Bitmap b)
-        throws IllegalArgumentException {
+    public void updateFromBitmap(Bitmap b) {
 
         mRS.validate();
         if(mType.getX() != b.getWidth() ||
            mType.getY() != b.getHeight()) {
-            throw new IllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
+            throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
         }
 
-        mRS.nAllocationUpdateFromBitmap(mID, b);
+        mRS.nAllocationUpdateFromBitmap(getID(), b);
     }
 
     public void subData(int xoff, FieldPacker fp) {
@@ -108,100 +106,100 @@
 
         int count = data.length / eSize;
         if ((eSize * count) != data.length) {
-            throw new IllegalArgumentException("Field packer length " + data.length +
+            throw new RSIllegalArgumentException("Field packer length " + data.length +
                                                " not divisible by element size " + eSize + ".");
         }
         data1DChecks(xoff, count, data.length, data.length);
-        mRS.nAllocationSubData1D(mID, xoff, count, data, data.length);
+        mRS.nAllocationSubData1D(getID(), xoff, count, data, data.length);
     }
 
 
     public void subElementData(int xoff, int component_number, FieldPacker fp) {
         if (component_number >= mType.mElement.mElements.length) {
-            throw new IllegalArgumentException("Component_number " + component_number + " out of range.");
+            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
         }
         if(xoff < 0) {
-            throw new IllegalArgumentException("Offset must be >= 0.");
+            throw new RSIllegalArgumentException("Offset must be >= 0.");
         }
 
         final byte[] data = fp.getData();
         int eSize = mType.mElement.mElements[component_number].getSizeBytes();
 
         if (data.length != eSize) {
-            throw new IllegalArgumentException("Field packer sizelength " + data.length +
+            throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
                                                " does not match component size " + eSize + ".");
         }
 
-        mRS.nAllocationSubElementData1D(mID, xoff, component_number, data, data.length);
+        mRS.nAllocationSubElementData1D(getID(), xoff, component_number, data, data.length);
     }
 
     private void data1DChecks(int off, int count, int len, int dataSize) {
         mRS.validate();
         if(off < 0) {
-            throw new IllegalArgumentException("Offset must be >= 0.");
+            throw new RSIllegalArgumentException("Offset must be >= 0.");
         }
         if(count < 1) {
-            throw new IllegalArgumentException("Count must be >= 1.");
+            throw new RSIllegalArgumentException("Count must be >= 1.");
         }
         if((off + count) > mType.getElementCount()) {
-            throw new IllegalArgumentException("Overflow, Available count " + mType.getElementCount() +
+            throw new RSIllegalArgumentException("Overflow, Available count " + mType.getElementCount() +
                                                ", got " + count + " at offset " + off + ".");
         }
         if((len) < dataSize) {
-            throw new IllegalArgumentException("Array too small for allocation type.");
+            throw new RSIllegalArgumentException("Array too small for allocation type.");
         }
     }
 
     public void subData1D(int off, int count, int[] d) {
         int dataSize = mType.mElement.getSizeBytes() * count;
         data1DChecks(off, count, d.length * 4, dataSize);
-        mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
+        mRS.nAllocationSubData1D(getID(), 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);
+        mRS.nAllocationSubData1D(getID(), 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);
+        mRS.nAllocationSubData1D(getID(), 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);
+        mRS.nAllocationSubData1D(getID(), off, count, d, dataSize);
     }
 
 
     public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
         mRS.validate();
-        mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
+        mRS.nAllocationSubData2D(getID(), xoff, yoff, w, h, d, d.length * 4);
     }
 
     public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
         mRS.validate();
-        mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
+        mRS.nAllocationSubData2D(getID(), xoff, yoff, w, h, d, d.length * 4);
     }
 
     public void readData(int[] d) {
         mRS.validate();
-        mRS.nAllocationRead(mID, d);
+        mRS.nAllocationRead(getID(), d);
     }
 
     public void readData(float[] d) {
         mRS.validate();
-        mRS.nAllocationRead(mID, d);
+        mRS.nAllocationRead(getID(), d);
     }
 
     public synchronized void resize(int dimX) {
         if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
-            throw new IllegalStateException("Resize only support for 1D allocations at this time.");
+            throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
         }
-        mRS.nAllocationResize1D(mID, dimX);
+        mRS.nAllocationResize1D(getID(), dimX);
         mRS.finish();  // Necessary because resize is fifoed and update is async.
 
-        int typeID = mRS.nAllocationGetType(mID);
+        int typeID = mRS.nAllocationGetType(getID());
         mType = new Type(typeID, mRS);
         mType.updateFromNative();
     }
@@ -209,12 +207,12 @@
     /*
     public void resize(int dimX, int dimY) {
         if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
-            throw new IllegalStateException("Resize only support for 2D allocations at this time.");
+            throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
         }
         if (mType.getY() == 0) {
-            throw new IllegalStateException("Resize only support for 2D allocations at this time.");
+            throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
         }
-        mRS.nAllocationResize2D(mID, dimX, dimY);
+        mRS.nAllocationResize2D(getID(), dimX, dimY);
     }
     */
 
@@ -225,27 +223,27 @@
 
         public void setConstraint(Dimension dim, int value) {
             mRS.validate();
-            mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
+            mRS.nAdapter1DSetConstraint(getID(), dim.mID, value);
         }
 
         public void data(int[] d) {
             mRS.validate();
-            mRS.nAdapter1DData(mID, d);
+            mRS.nAdapter1DData(getID(), d);
         }
 
         public void data(float[] d) {
             mRS.validate();
-            mRS.nAdapter1DData(mID, d);
+            mRS.nAdapter1DData(getID(), d);
         }
 
         public void subData(int off, int count, int[] d) {
             mRS.validate();
-            mRS.nAdapter1DSubData(mID, off, count, d);
+            mRS.nAdapter1DSubData(getID(), off, count, d);
         }
 
         public void subData(int off, int count, float[] d) {
             mRS.validate();
-            mRS.nAdapter1DSubData(mID, off, count, d);
+            mRS.nAdapter1DSubData(getID(), off, count, d);
         }
     }
 
@@ -253,9 +251,9 @@
         mRS.validate();
         int id = mRS.nAdapter1DCreate();
         if(id == 0) {
-            throw new IllegalStateException("allocation failed.");
+            throw new RSRuntimeException("Adapter creation failed.");
         }
-        mRS.nAdapter1DBindAllocation(id, mID);
+        mRS.nAdapter1DBindAllocation(id, getID());
         return new Adapter1D(id, mRS);
     }
 
@@ -267,27 +265,27 @@
 
         public void setConstraint(Dimension dim, int value) {
             mRS.validate();
-            mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
+            mRS.nAdapter2DSetConstraint(getID(), dim.mID, value);
         }
 
         public void data(int[] d) {
             mRS.validate();
-            mRS.nAdapter2DData(mID, d);
+            mRS.nAdapter2DData(getID(), d);
         }
 
         public void data(float[] d) {
             mRS.validate();
-            mRS.nAdapter2DData(mID, d);
+            mRS.nAdapter2DData(getID(), d);
         }
 
         public void subData(int xoff, int yoff, int w, int h, int[] d) {
             mRS.validate();
-            mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
+            mRS.nAdapter2DSubData(getID(), xoff, yoff, w, h, d);
         }
 
         public void subData(int xoff, int yoff, int w, int h, float[] d) {
             mRS.validate();
-            mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
+            mRS.nAdapter2DSubData(getID(), xoff, yoff, w, h, d);
         }
     }
 
@@ -295,9 +293,12 @@
         mRS.validate();
         int id = mRS.nAdapter2DCreate();
         if(id == 0) {
-            throw new IllegalStateException("allocation failed.");
+            throw new RSRuntimeException("allocation failed.");
         }
-        mRS.nAdapter2DBindAllocation(id, mID);
+        mRS.nAdapter2DBindAllocation(id, getID());
+        if(id == 0) {
+            throw new RSRuntimeException("Adapter creation failed.");
+        }
         return new Adapter2D(id, mRS);
     }
 
@@ -309,14 +310,16 @@
         mBitmapOptions.inScaled = false;
     }
 
-    static public Allocation createTyped(RenderScript rs, Type type)
-        throws IllegalArgumentException {
+    static public Allocation createTyped(RenderScript rs, Type type) {
 
         rs.validate();
-        if(type.mID == 0) {
-            throw new IllegalStateException("Bad Type");
+        if(type.getID() == 0) {
+            throw new RSInvalidStateException("Bad Type");
         }
-        int id = rs.nAllocationCreateTyped(type.mID);
+        int id = rs.nAllocationCreateTyped(type.getID());
+        if(id == 0) {
+            throw new RSRuntimeException("Allocation creation failed.");
+        }
         return new Allocation(id, rs, type);
     }
 
@@ -328,9 +331,9 @@
         b.add(Dimension.X, count);
         Type t = b.create();
 
-        int id = rs.nAllocationCreateTyped(t.mID);
+        int id = rs.nAllocationCreateTyped(t.getID());
         if(id == 0) {
-            throw new IllegalStateException("Bad element.");
+            throw new RSRuntimeException("Allocation creation failed.");
         }
         return new Allocation(id, rs, t);
     }
@@ -349,7 +352,7 @@
         if (bc == Bitmap.Config.RGB_565) {
             return Element.RGB_565(rs);
         }
-        throw new IllegalStateException("Bad bitmap type.");
+        throw new RSInvalidStateException("Bad bitmap type.");
     }
 
     static private Type typeFromBitmap(RenderScript rs, Bitmap b) {
@@ -360,28 +363,26 @@
         return tb.create();
     }
 
-    static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
-        throws IllegalArgumentException {
+    static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips) {
 
         rs.validate();
         Type t = typeFromBitmap(rs, b);
 
-        int id = rs.nAllocationCreateFromBitmap(dstFmt.mID, genMips, b);
+        int id = rs.nAllocationCreateFromBitmap(dstFmt.getID(), genMips, b);
         if(id == 0) {
-            throw new IllegalStateException("Load failed.");
+            throw new RSRuntimeException("Load failed.");
         }
         return new Allocation(id, rs, t);
     }
 
-    static public Allocation createBitmapRef(RenderScript rs, Bitmap b)
-        throws IllegalArgumentException {
+    static public Allocation createBitmapRef(RenderScript rs, Bitmap b) {
 
         rs.validate();
         Type t = typeFromBitmap(rs, b);
 
         int id = rs.nAllocationCreateBitmapRef(t.getID(), b);
         if(id == 0) {
-            throw new IllegalStateException("Load failed.");
+            throw new RSRuntimeException("Load failed.");
         }
 
         Allocation a = new Allocation(id, rs, t);
@@ -389,8 +390,7 @@
         return a;
     }
 
-    static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
-        throws IllegalArgumentException {
+    static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips) {
 
         rs.validate();
         InputStream is = null;
@@ -399,15 +399,12 @@
             is = res.openRawResource(id, value);
 
             int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
-            int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mID, genMips,
-                    asset);
+            int aId = rs.nAllocationCreateFromAssetStream(dstFmt.getID(), genMips, asset);
 
-            if(allocationId == 0) {
-                throw new IllegalStateException("Load failed.");
+            if (aId == 0) {
+                throw new RSRuntimeException("Load failed.");
             }
-            return new Allocation(allocationId, rs, null);
-        } catch (Exception e) {
-            // Ignore
+            return new Allocation(aId, rs, null);
         } finally {
             if (is != null) {
                 try {
@@ -417,12 +414,9 @@
                 }
             }
         }
-
-        return null;
     }
 
-    static public Allocation createFromString(RenderScript rs, String str)
-        throws IllegalArgumentException {
+    static public Allocation createFromString(RenderScript rs, String str) {
         byte[] allocArray = null;
         try {
             allocArray = str.getBytes("UTF-8");
@@ -431,9 +425,8 @@
             return alloc;
         }
         catch (Exception e) {
-            Log.e("rs", "could not convert string to utf-8");
+            throw new RSRuntimeException("Could not convert string to utf-8.");
         }
-        return null;
     }
 }
 
diff --git a/graphics/java/android/renderscript/BaseObj.java b/graphics/java/android/renderscript/BaseObj.java
index 69907d9..026f7de 100644
--- a/graphics/java/android/renderscript/BaseObj.java
+++ b/graphics/java/android/renderscript/BaseObj.java
@@ -21,9 +21,12 @@
 /**
  * @hide
  *
+ * BaseObj is the base class for interfacing with native renderscript objects.
+ * It primarly contains code for tracking the native object ID and forcably
+ * disconecting the object from the native allocation for early cleanup.
+ *
  **/
 class BaseObj {
-
     BaseObj(int id, RenderScript rs) {
         rs.validate();
         mRS = rs;
@@ -31,6 +34,13 @@
         mDestroyed = false;
     }
 
+    void setID(int id) {
+        if (mID != 0) {
+            throw new RSRuntimeException("Internal Error, reset of object ID.");
+        }
+        mID = id;
+    }
+
     public int getID() {
         if (mDestroyed) {
             throw new RSInvalidStateException("using a destroyed object.");
@@ -38,9 +48,9 @@
         return mID;
     }
 
-    int mID;
-    boolean mDestroyed;
-    String mName;
+    private int mID;
+    private boolean mDestroyed;
+    private String mName;
     RenderScript mRS;
 
     public void setName(String s) {
@@ -74,7 +84,7 @@
         super.finalize();
     }
 
-    public void destroy() {
+    synchronized public void destroy() {
         if(mDestroyed) {
             throw new RSInvalidStateException("Object already destroyed.");
         }
@@ -85,6 +95,8 @@
     // If an object came from an a3d file, java fields need to be
     // created with objects from the native layer
     void updateFromNative() {
+        mRS.validate();
+        mName = mRS.nGetName(getID());
     }
 
 }
diff --git a/graphics/java/android/renderscript/Element.java b/graphics/java/android/renderscript/Element.java
index f844b7e..8907cd2 100644
--- a/graphics/java/android/renderscript/Element.java
+++ b/graphics/java/android/renderscript/Element.java
@@ -373,10 +373,11 @@
 
     @Override
     void updateFromNative() {
+        super.updateFromNative();
 
         // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
         int[] dataBuffer = new int[5];
-        mRS.nElementGetNativeData(mID, dataBuffer);
+        mRS.nElementGetNativeData(getID(), dataBuffer);
 
         mNormalized = dataBuffer[2] == 1 ? true : false;
         mVectorSize = dataBuffer[3];
@@ -399,7 +400,7 @@
             mElementNames = new String[numSubElements];
 
             int[] subElementIds = new int[numSubElements];
-            mRS.nElementGetSubElements(mID, subElementIds, mElementNames);
+            mRS.nElementGetSubElements(getID(), subElementIds, mElementNames);
             for(int i = 0; i < numSubElements; i ++) {
                 mElements[i] = new Element(subElementIds[i], mRS);
                 mElements[i].updateFromNative();
@@ -523,7 +524,7 @@
 
             int[] ids = new int[ein.length];
             for (int ct = 0; ct < ein.length; ct++ ) {
-                ids[ct] = ein[ct].mID;
+                ids[ct] = ein[ct].getID();
             }
             int id = mRS.nElementCreate2(ids, sin, asin);
             return new Element(id, mRS, ein, sin, asin);
diff --git a/graphics/java/android/renderscript/FileA3D.java b/graphics/java/android/renderscript/FileA3D.java
index fc74fc4..af85d8e 100644
--- a/graphics/java/android/renderscript/FileA3D.java
+++ b/graphics/java/android/renderscript/FileA3D.java
@@ -149,7 +149,7 @@
     }
 
     private void initEntries() {
-        int numFileEntries = mRS.nFileA3DGetNumIndexEntries(mID);
+        int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID());
         if(numFileEntries <= 0) {
             return;
         }
@@ -158,10 +158,10 @@
         int[] ids = new int[numFileEntries];
         String[] names = new String[numFileEntries];
 
-        mRS.nFileA3DGetIndexEntries(mID, numFileEntries, ids, names);
+        mRS.nFileA3DGetIndexEntries(getID(), numFileEntries, ids, names);
 
         for(int i = 0; i < numFileEntries; i ++) {
-            mFileEntries[i] = new IndexEntry(mRS, i, mID, names[i], ClassID.toClassID(ids[i]));
+            mFileEntries[i] = new IndexEntry(mRS, i, getID(), names[i], ClassID.toClassID(ids[i]));
         }
     }
 
diff --git a/graphics/java/android/renderscript/Mesh.java b/graphics/java/android/renderscript/Mesh.java
index bb382f2..4187992 100644
--- a/graphics/java/android/renderscript/Mesh.java
+++ b/graphics/java/android/renderscript/Mesh.java
@@ -60,16 +60,16 @@
 
     @Override
     void updateFromNative() {
-        mName = mRS.nGetName(mID);
-        int vtxCount = mRS.nMeshGetVertexBufferCount(mID);
-        int idxCount = mRS.nMeshGetIndexCount(mID);
+        super.updateFromNative();
+        int vtxCount = mRS.nMeshGetVertexBufferCount(getID());
+        int idxCount = mRS.nMeshGetIndexCount(getID());
 
         int[] vtxIDs = new int[vtxCount];
         int[] idxIDs = new int[idxCount];
         int[] primitives = new int[idxCount];
 
-        mRS.nMeshGetVertices(mID, vtxIDs, vtxCount);
-        mRS.nMeshGetIndices(mID, idxIDs, primitives, idxCount);
+        mRS.nMeshGetVertices(getID(), vtxIDs, vtxCount);
+        mRS.nMeshGetIndices(getID(), idxIDs, primitives, idxCount);
 
         mVertexBuffers = new Allocation[vtxCount];
         mIndexBuffers = new Allocation[idxCount];
@@ -292,7 +292,7 @@
 
             for(int ct = 0; ct < b.mVertexTypeCount; ct ++) {
                 Entry entry = b.mVertexTypes[ct];
-                rs.nMeshBindVertex(id, entry.a.mID, ct);
+                rs.nMeshBindVertex(id, entry.a.getID(), ct);
                 newMesh.mVertexBuffers[ct] = entry.a;
             }
             rs.nMeshInitVertexAttribs(id);
diff --git a/graphics/java/android/renderscript/Program.java b/graphics/java/android/renderscript/Program.java
index 35236ca..83c3601 100644
--- a/graphics/java/android/renderscript/Program.java
+++ b/graphics/java/android/renderscript/Program.java
@@ -54,7 +54,7 @@
             a.getType().getID() != mConstants[slot].getID()) {
             throw new IllegalArgumentException("Allocation type does not match slot type.");
         }
-        mRS.nProgramBindConstants(mID, slot, a.mID);
+        mRS.nProgramBindConstants(getID(), slot, a.getID());
     }
 
     public void bindTexture(Allocation va, int slot)
@@ -64,7 +64,7 @@
             throw new IllegalArgumentException("Slot ID out of range.");
         }
 
-        mRS.nProgramBindTexture(mID, slot, va.mID);
+        mRS.nProgramBindTexture(getID(), slot, va.getID());
     }
 
     public void bindSampler(Sampler vs, int slot)
@@ -74,7 +74,7 @@
             throw new IllegalArgumentException("Slot ID out of range.");
         }
 
-        mRS.nProgramBindSampler(mID, slot, vs.mID);
+        mRS.nProgramBindSampler(getID(), slot, vs.getID());
     }
 
 
diff --git a/graphics/java/android/renderscript/ProgramFragment.java b/graphics/java/android/renderscript/ProgramFragment.java
index 8858b74..d30e483 100644
--- a/graphics/java/android/renderscript/ProgramFragment.java
+++ b/graphics/java/android/renderscript/ProgramFragment.java
@@ -42,15 +42,15 @@
 
             for (int i=0; i < mInputCount; i++) {
                 tmp[idx++] = 0;
-                tmp[idx++] = mInputs[i].mID;
+                tmp[idx++] = mInputs[i].getID();
             }
             for (int i=0; i < mOutputCount; i++) {
                 tmp[idx++] = 1;
-                tmp[idx++] = mOutputs[i].mID;
+                tmp[idx++] = mOutputs[i].getID();
             }
             for (int i=0; i < mConstantCount; i++) {
                 tmp[idx++] = 2;
-                tmp[idx++] = mConstants[i].mID;
+                tmp[idx++] = mConstants[i].getID();
             }
             tmp[idx++] = 3;
             tmp[idx++] = mTextureCount;
diff --git a/graphics/java/android/renderscript/ProgramRaster.java b/graphics/java/android/renderscript/ProgramRaster.java
index 791dac8..5b55015 100644
--- a/graphics/java/android/renderscript/ProgramRaster.java
+++ b/graphics/java/android/renderscript/ProgramRaster.java
@@ -58,13 +58,13 @@
     void setLineWidth(float w) {
         mRS.validate();
         mLineWidth = w;
-        mRS.nProgramRasterSetLineWidth(mID, w);
+        mRS.nProgramRasterSetLineWidth(getID(), w);
     }
 
     void setCullMode(CullMode m) {
         mRS.validate();
         mCullMode = m;
-        mRS.nProgramRasterSetCullMode(mID, m.mID);
+        mRS.nProgramRasterSetCullMode(getID(), m.mID);
     }
 
     public static ProgramRaster CULL_BACK(RenderScript rs) {
diff --git a/graphics/java/android/renderscript/ProgramStore.java b/graphics/java/android/renderscript/ProgramStore.java
index 32c0d01..d191b06 100644
--- a/graphics/java/android/renderscript/ProgramStore.java
+++ b/graphics/java/android/renderscript/ProgramStore.java
@@ -288,15 +288,7 @@
         }
 
         static synchronized ProgramStore internalCreate(RenderScript rs, Builder b) {
-            int inID = 0;
-            int outID = 0;
-            if (b.mIn != null) {
-                inID = b.mIn.mID;
-            }
-            if (b.mOut != null) {
-                outID = b.mOut.mID;
-            }
-            rs.nProgramStoreBegin(inID, outID);
+            rs.nProgramStoreBegin(0, 0);
             rs.nProgramStoreDepthFunc(b.mDepthFunc.mID);
             rs.nProgramStoreDepthMask(b.mDepthMask);
             rs.nProgramStoreColorMask(b.mColorMaskR,
diff --git a/graphics/java/android/renderscript/ProgramVertex.java b/graphics/java/android/renderscript/ProgramVertex.java
index 65a0af2..13f017a 100644
--- a/graphics/java/android/renderscript/ProgramVertex.java
+++ b/graphics/java/android/renderscript/ProgramVertex.java
@@ -51,15 +51,15 @@
 
             for (int i=0; i < mInputCount; i++) {
                 tmp[idx++] = 0;
-                tmp[idx++] = mInputs[i].mID;
+                tmp[idx++] = mInputs[i].getID();
             }
             for (int i=0; i < mOutputCount; i++) {
                 tmp[idx++] = 1;
-                tmp[idx++] = mOutputs[i].mID;
+                tmp[idx++] = mOutputs[i].getID();
             }
             for (int i=0; i < mConstantCount; i++) {
                 tmp[idx++] = 2;
-                tmp[idx++] = mConstants[i].mID;
+                tmp[idx++] = mConstants[i].getID();
             }
             tmp[idx++] = 3;
             tmp[idx++] = mTextureCount;
diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java
index b3774d4..64afb6f 100644
--- a/graphics/java/android/renderscript/RenderScript.java
+++ b/graphics/java/android/renderscript/RenderScript.java
@@ -738,7 +738,7 @@
 
     protected int safeID(BaseObj o) {
         if(o != null) {
-            return o.mID;
+            return o.getID();
         }
         return 0;
     }
diff --git a/graphics/java/android/renderscript/Script.java b/graphics/java/android/renderscript/Script.java
index 430789a..7d7dd6d 100644
--- a/graphics/java/android/renderscript/Script.java
+++ b/graphics/java/android/renderscript/Script.java
@@ -38,19 +38,19 @@
         }
 
         public void execute() {
-            mRS.nScriptInvoke(mScript.mID, mSlot);
+            mRS.nScriptInvoke(mScript.getID(), mSlot);
         }
     }
 
     protected void invoke(int slot) {
-        mRS.nScriptInvoke(mID, slot);
+        mRS.nScriptInvoke(getID(), slot);
     }
 
     protected void invoke(int slot, FieldPacker v) {
         if (v != null) {
-            mRS.nScriptInvokeV(mID, slot, v.getData());
+            mRS.nScriptInvokeV(getID(), slot, v.getData());
         } else {
-            mRS.nScriptInvoke(mID, slot);
+            mRS.nScriptInvoke(getID(), slot);
         }
     }
 
@@ -62,40 +62,40 @@
     public void bindAllocation(Allocation va, int slot) {
         mRS.validate();
         if (va != null) {
-            mRS.nScriptBindAllocation(mID, va.mID, slot);
+            mRS.nScriptBindAllocation(getID(), va.getID(), slot);
         } else {
-            mRS.nScriptBindAllocation(mID, 0, slot);
+            mRS.nScriptBindAllocation(getID(), 0, slot);
         }
     }
 
     public void setVar(int index, float v) {
-        mRS.nScriptSetVarF(mID, index, v);
+        mRS.nScriptSetVarF(getID(), index, v);
     }
 
     public void setVar(int index, double v) {
-        mRS.nScriptSetVarD(mID, index, v);
+        mRS.nScriptSetVarD(getID(), index, v);
     }
 
     public void setVar(int index, int v) {
-        mRS.nScriptSetVarI(mID, index, v);
+        mRS.nScriptSetVarI(getID(), index, v);
     }
 
     public void setVar(int index, long v) {
-        mRS.nScriptSetVarJ(mID, index, v);
+        mRS.nScriptSetVarJ(getID(), index, v);
     }
 
     public void setVar(int index, boolean v) {
-        mRS.nScriptSetVarI(mID, index, v ? 1 : 0);
+        mRS.nScriptSetVarI(getID(), index, v ? 1 : 0);
     }
 
     public void setVar(int index, FieldPacker v) {
-        mRS.nScriptSetVarV(mID, index, v.getData());
+        mRS.nScriptSetVarV(getID(), index, v.getData());
     }
 
     public void setTimeZone(String timeZone) {
         mRS.validate();
         try {
-            mRS.nScriptSetTimeZone(mID, timeZone.getBytes("UTF-8"));
+            mRS.nScriptSetTimeZone(getID(), timeZone.getBytes("UTF-8"));
         } catch (java.io.UnsupportedEncodingException e) {
             throw new RuntimeException(e);
         }
diff --git a/graphics/java/android/renderscript/ScriptC.java b/graphics/java/android/renderscript/ScriptC.java
index 5215795..44fc5fd 100644
--- a/graphics/java/android/renderscript/ScriptC.java
+++ b/graphics/java/android/renderscript/ScriptC.java
@@ -39,7 +39,8 @@
 
     protected ScriptC(RenderScript rs, Resources resources, int resourceID) {
         super(0, rs);
-        mID = internalCreate(rs, resources, resourceID);
+        int id = internalCreate(rs, resources, resourceID);
+        setID(id);
     }
 
 
diff --git a/graphics/java/android/renderscript/Type.java b/graphics/java/android/renderscript/Type.java
index 0d65737..ad933b8 100644
--- a/graphics/java/android/renderscript/Type.java
+++ b/graphics/java/android/renderscript/Type.java
@@ -106,7 +106,7 @@
         // We have 6 integer to obtain mDimX; mDimY; mDimZ;
         // mDimLOD; mDimFaces; mElement;
         int[] dataBuffer = new int[6];
-        mRS.nTypeGetNativeData(mID, dataBuffer);
+        mRS.nTypeGetNativeData(getID(), dataBuffer);
 
         mDimX = dataBuffer[0];
         mDimY = dataBuffer[1];
@@ -135,7 +135,7 @@
         }
 
         public Builder(RenderScript rs, Element e) {
-            if(e.mID == 0) {
+            if(e.getID() == 0) {
                 throw new RSIllegalArgumentException("Invalid element.");
             }
 
diff --git a/libs/rs/rsAllocation.cpp b/libs/rs/rsAllocation.cpp
index b74fa8e..28078fc 100644
--- a/libs/rs/rsAllocation.cpp
+++ b/libs/rs/rsAllocation.cpp
@@ -869,6 +869,8 @@
         }
     } else {
         rsc->setError(RS_ERROR_BAD_VALUE, "Unsupported bitmap format");
+        delete texAlloc;
+        return NULL;
     }
 
     return texAlloc;