Add error checks to the copyFrom functions.
Change-Id: Iac064c52eb58b05a94fa1c432304c6216256555b
diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java
index 852aabf..1789891 100644
--- a/graphics/java/android/renderscript/Allocation.java
+++ b/graphics/java/android/renderscript/Allocation.java
@@ -121,6 +121,58 @@
mType = t;
}
+ private void validateIsInt32() {
+ if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
+ (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
+ return;
+ }
+ throw new RSIllegalArgumentException(
+ "32 bit integer source does not match allocation type " + mType.mElement.mType);
+ }
+
+ private void validateIsInt16() {
+ if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
+ (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
+ return;
+ }
+ throw new RSIllegalArgumentException(
+ "16 bit integer source does not match allocation type " + mType.mElement.mType);
+ }
+
+ private void validateIsInt8() {
+ if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
+ (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
+ return;
+ }
+ throw new RSIllegalArgumentException(
+ "8 bit integer source does not match allocation type " + mType.mElement.mType);
+ }
+
+ private void validateIsFloat32() {
+ if (mType.mElement.mType == Element.DataType.FLOAT_32) {
+ return;
+ }
+ throw new RSIllegalArgumentException(
+ "32 bit float source does not match allocation type " + mType.mElement.mType);
+ }
+
+ private void validateIsObject() {
+ if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
+ (mType.mElement.mType == Element.DataType.RS_TYPE) ||
+ (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
+ (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
+ (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
+ (mType.mElement.mType == Element.DataType.RS_MESH) ||
+ (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
+ (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
+ (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
+ (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
+ return;
+ }
+ throw new RSIllegalArgumentException(
+ "Object source does not match allocation type " + mType.mElement.mType);
+ }
+
@Override
void updateFromNative() {
super.updateFromNative();
@@ -151,6 +203,7 @@
public void copyFrom(BaseObj[] d) {
mRS.validate();
+ validateIsObject();
if (d.length != mType.getCount()) {
throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
mType.getCount() + ", array length = " + d.length);
@@ -258,7 +311,6 @@
mRS.nAllocationData1D(getID(), xoff, 0, count, data, data.length);
}
-
/**
* This is only intended to be used by auto-generate code reflected from the
* renderscript script files.
@@ -317,27 +369,44 @@
mRS.nAllocationGenerateMipmaps(getID());
}
- public void copy1DRangeFrom(int off, int count, int[] d) {
+ void copy1DRangeFromUnchecked(int off, int count, int[] d) {
int dataSize = mType.mElement.getSizeBytes() * count;
data1DChecks(off, count, d.length * 4, dataSize);
mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
}
- public void copy1DRangeFrom(int off, int count, short[] d) {
+ void copy1DRangeFromUnchecked(int off, int count, short[] d) {
int dataSize = mType.mElement.getSizeBytes() * count;
data1DChecks(off, count, d.length * 2, dataSize);
mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
}
- public void copy1DRangeFrom(int off, int count, byte[] d) {
+ void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
int dataSize = mType.mElement.getSizeBytes() * count;
data1DChecks(off, count, d.length, dataSize);
mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
}
- public void copy1DRangeFrom(int off, int count, float[] d) {
+ void copy1DRangeFromUnchecked(int off, int count, float[] d) {
int dataSize = mType.mElement.getSizeBytes() * count;
data1DChecks(off, count, d.length * 4, dataSize);
mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
}
+ public void copy1DRangeFrom(int off, int count, int[] d) {
+ validateIsInt32();
+ copy1DRangeFromUnchecked(off, count, d);
+ }
+ public void copy1DRangeFrom(int off, int count, short[] d) {
+ validateIsInt16();
+ copy1DRangeFromUnchecked(off, count, d);
+ }
+ public void copy1DRangeFrom(int off, int count, byte[] d) {
+ validateIsInt8();
+ copy1DRangeFromUnchecked(off, count, d);
+ }
+ public void copy1DRangeFrom(int off, int count, float[] d) {
+ validateIsFloat32();
+ copy1DRangeFromUnchecked(off, count, d);
+ }
+
private void validate2DRange(int xoff, int yoff, int w, int h) {
if (xoff < 0 || yoff < 0) {
throw new RSIllegalArgumentException("Offset cannot be negative.");
@@ -411,21 +480,25 @@
}
public void copyTo(byte[] d) {
+ validateIsInt8();
mRS.validate();
mRS.nAllocationRead(getID(), d);
}
public void copyTo(short[] d) {
+ validateIsInt16();
mRS.validate();
mRS.nAllocationRead(getID(), d);
}
public void copyTo(int[] d) {
+ validateIsInt32();
mRS.validate();
mRS.nAllocationRead(getID(), d);
}
public void copyTo(float[] d) {
+ validateIsFloat32();
mRS.validate();
mRS.nAllocationRead(getID(), d);
}
diff --git a/graphics/java/android/renderscript/Mesh.java b/graphics/java/android/renderscript/Mesh.java
index fcf8178..c20151e 100644
--- a/graphics/java/android/renderscript/Mesh.java
+++ b/graphics/java/android/renderscript/Mesh.java
@@ -733,14 +733,14 @@
Mesh sm = smb.create();
- sm.getVertexAllocation(0).copyFrom(mVtxData);
+ sm.getVertexAllocation(0).copy1DRangeFromUnchecked(0, mVtxCount / floatCount, mVtxData);
if(uploadToBufferObject) {
if (uploadToBufferObject) {
sm.getVertexAllocation(0).syncAll(Allocation.USAGE_SCRIPT);
}
}
- sm.getIndexSetAllocation(0).copyFrom(mIndexData);
+ sm.getIndexSetAllocation(0).copy1DRangeFromUnchecked(0, mIndexCount, mIndexData);
if (uploadToBufferObject) {
sm.getIndexSetAllocation(0).syncAll(Allocation.USAGE_SCRIPT);
}
diff --git a/graphics/java/android/renderscript/ProgramFragmentFixedFunction.java b/graphics/java/android/renderscript/ProgramFragmentFixedFunction.java
index 1af31f8..666a3e6 100644
--- a/graphics/java/android/renderscript/ProgramFragmentFixedFunction.java
+++ b/graphics/java/android/renderscript/ProgramFragmentFixedFunction.java
@@ -280,9 +280,10 @@
pf.mTextureCount = MAX_TEXTURE;
if (!mVaryingColorEnable) {
Allocation constantData = Allocation.createTyped(mRS,constType);
- float[] data = new float[4];
- data[0] = data[1] = data[2] = data[3] = 1.0f;
- constantData.copyFrom(data);
+ FieldPacker fp = new FieldPacker(16);
+ Float4 f4 = new Float4(1.f, 1.f, 1.f, 1.f);
+ fp.addF32(f4);
+ constantData.setFromFieldPacker(0, fp);
pf.bindConstants(constantData, 0);
}
return pf;
diff --git a/graphics/java/android/renderscript/ProgramVertexFixedFunction.java b/graphics/java/android/renderscript/ProgramVertexFixedFunction.java
index 666c7ec..556964a 100644
--- a/graphics/java/android/renderscript/ProgramVertexFixedFunction.java
+++ b/graphics/java/android/renderscript/ProgramVertexFixedFunction.java
@@ -229,7 +229,7 @@
for(int i = 0; i < 16; i ++) {
mIOBuffer.addF32(m.mMat[i]);
}
- mAlloc.copyFrom(mIOBuffer.getData());
+ mAlloc.setFromFieldPacker(0, mIOBuffer);
}
/**