diff options
Diffstat (limited to 'rs')
-rw-r--r-- | rs/java/android/renderscript/Allocation.java | 626 | ||||
-rw-r--r-- | rs/java/android/renderscript/ScriptGroup.java | 82 |
2 files changed, 392 insertions, 316 deletions
diff --git a/rs/java/android/renderscript/Allocation.java b/rs/java/android/renderscript/Allocation.java index bea138e..0a50593 100644 --- a/rs/java/android/renderscript/Allocation.java +++ b/rs/java/android/renderscript/Allocation.java @@ -51,6 +51,7 @@ import android.os.Trace; * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p> * </div> **/ + public class Allocation extends BaseObj { Type mType; Bitmap mBitmap; @@ -455,28 +456,31 @@ public class Allocation extends BaseObj { * */ public void syncAll(int srcLocation) { - Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll"); - switch (srcLocation) { - case USAGE_GRAPHICS_TEXTURE: - case USAGE_SCRIPT: - if ((mUsage & USAGE_SHARED) != 0) { - copyFrom(mBitmap); - } - break; - case USAGE_GRAPHICS_CONSTANTS: - case USAGE_GRAPHICS_VERTEX: - break; - case USAGE_SHARED: - if ((mUsage & USAGE_SHARED) != 0) { - copyTo(mBitmap); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll"); + switch (srcLocation) { + case USAGE_GRAPHICS_TEXTURE: + case USAGE_SCRIPT: + if ((mUsage & USAGE_SHARED) != 0) { + copyFrom(mBitmap); + } + break; + case USAGE_GRAPHICS_CONSTANTS: + case USAGE_GRAPHICS_VERTEX: + break; + case USAGE_SHARED: + if ((mUsage & USAGE_SHARED) != 0) { + copyTo(mBitmap); + } + break; + default: + throw new RSIllegalArgumentException("Source must be exactly one usage type."); } - break; - default: - throw new RSIllegalArgumentException("Source must be exactly one usage type."); + mRS.validate(); + mRS.nAllocationSyncAll(getIDSafe(), srcLocation); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); } - mRS.validate(); - mRS.nAllocationSyncAll(getIDSafe(), srcLocation); - Trace.traceEnd(RenderScript.TRACE_TAG); } /** @@ -487,14 +491,17 @@ public class Allocation extends BaseObj { * */ public void ioSend() { - Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend"); - if ((mUsage & USAGE_IO_OUTPUT) == 0) { - throw new RSIllegalArgumentException( - "Can only send buffer if IO_OUTPUT usage specified."); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend"); + if ((mUsage & USAGE_IO_OUTPUT) == 0) { + throw new RSIllegalArgumentException( + "Can only send buffer if IO_OUTPUT usage specified."); + } + mRS.validate(); + mRS.nAllocationIoSend(getID(mRS)); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); } - mRS.validate(); - mRS.nAllocationIoSend(getID(mRS)); - Trace.traceEnd(RenderScript.TRACE_TAG); } /** @@ -503,14 +510,17 @@ public class Allocation extends BaseObj { * */ public void ioReceive() { - Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive"); - if ((mUsage & USAGE_IO_INPUT) == 0) { - throw new RSIllegalArgumentException( - "Can only receive if IO_INPUT usage specified."); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive"); + if ((mUsage & USAGE_IO_INPUT) == 0) { + throw new RSIllegalArgumentException( + "Can only receive if IO_INPUT usage specified."); + } + mRS.validate(); + mRS.nAllocationIoReceive(getID(mRS)); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); } - mRS.validate(); - mRS.nAllocationIoReceive(getID(mRS)); - Trace.traceEnd(RenderScript.TRACE_TAG); } /** @@ -519,28 +529,31 @@ public class Allocation extends BaseObj { * @param d Source array. */ public void copyFrom(BaseObj[] d) { - Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom"); - mRS.validate(); - validateIsObject(); - if (d.length != mCurrentCount) { - throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " + - mCurrentCount + ", array length = " + d.length); - } - - if (RenderScript.sPointerSize == 8) { - long i[] = new long[d.length * 4]; - for (int ct=0; ct < d.length; ct++) { - i[ct * 4] = d[ct].getID(mRS); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom"); + mRS.validate(); + validateIsObject(); + if (d.length != mCurrentCount) { + throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " + + mCurrentCount + ", array length = " + d.length); } - copy1DRangeFromUnchecked(0, mCurrentCount, i); - } else { - int i[] = new int[d.length]; - for (int ct=0; ct < d.length; ct++) { - i[ct] = (int)d[ct].getID(mRS); + + if (RenderScript.sPointerSize == 8) { + long i[] = new long[d.length * 4]; + for (int ct=0; ct < d.length; ct++) { + i[ct * 4] = d[ct].getID(mRS); + } + copy1DRangeFromUnchecked(0, mCurrentCount, i); + } else { + int i[] = new int[d.length]; + for (int ct=0; ct < d.length; ct++) { + i[ct] = (int) d[ct].getID(mRS); + } + copy1DRangeFromUnchecked(0, mCurrentCount, i); } - copy1DRangeFromUnchecked(0, mCurrentCount, i); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); } - Trace.traceEnd(RenderScript.TRACE_TAG); } private void validateBitmapFormat(Bitmap b) { @@ -599,16 +612,19 @@ public class Allocation extends BaseObj { } private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) { - Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked"); - mRS.validate(); - if (mCurrentDimZ > 0) { - copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen); - } else if (mCurrentDimY > 0) { - copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen); - } else { - copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked"); + mRS.validate(); + if (mCurrentDimZ > 0) { + copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen); + } else if (mCurrentDimY > 0) { + copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen); + } else { + copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen); + } + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); } - Trace.traceEnd(RenderScript.TRACE_TAG); } /** @@ -619,10 +635,13 @@ public class Allocation extends BaseObj { * @param array The source data array */ public void copyFromUnchecked(Object array) { - Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked"); - copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false), - java.lang.reflect.Array.getLength(array)); - Trace.traceEnd(RenderScript.TRACE_TAG); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked"); + copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false), + java.lang.reflect.Array.getLength(array)); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); + } } /** @@ -679,10 +698,13 @@ public class Allocation extends BaseObj { * @param array The source data array */ public void copyFrom(Object array) { - Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom"); - copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true), - java.lang.reflect.Array.getLength(array)); - Trace.traceEnd(RenderScript.TRACE_TAG); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom"); + copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true), + java.lang.reflect.Array.getLength(array)); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); + } } /** @@ -747,19 +769,22 @@ public class Allocation extends BaseObj { * @param b the source bitmap */ public void copyFrom(Bitmap b) { - Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom"); - mRS.validate(); - if (b.getConfig() == null) { - Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888); - Canvas c = new Canvas(newBitmap); - c.drawBitmap(b, 0, 0, null); - copyFrom(newBitmap); - return; + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom"); + mRS.validate(); + if (b.getConfig() == null) { + Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888); + Canvas c = new Canvas(newBitmap); + c.drawBitmap(b, 0, 0, null); + copyFrom(newBitmap); + return; + } + validateBitmapSize(b); + validateBitmapFormat(b); + mRS.nAllocationCopyFromBitmap(getID(mRS), b); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); } - validateBitmapSize(b); - validateBitmapFormat(b); - mRS.nAllocationCopyFromBitmap(getID(mRS), b); - Trace.traceEnd(RenderScript.TRACE_TAG); } /** @@ -769,13 +794,16 @@ public class Allocation extends BaseObj { * @param a the source allocation */ public void copyFrom(Allocation a) { - Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom"); - mRS.validate(); - if (!mType.equals(a.getType())) { - throw new RSIllegalArgumentException("Types of allocations must match."); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom"); + mRS.validate(); + if (!mType.equals(a.getType())) { + throw new RSIllegalArgumentException("Types of allocations must match."); + } + copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); } - copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0); - Trace.traceEnd(RenderScript.TRACE_TAG); } /** @@ -890,17 +918,20 @@ public class Allocation extends BaseObj { private void copy1DRangeFromUnchecked(int off, int count, Object array, Element.DataType dt, int arrayLen) { - Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked"); - final int dataSize = mType.mElement.getBytesSize() * count; - // AutoPadding for Vec3 Element - boolean usePadding = false; - if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) { - usePadding = true; - } - data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding); - mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt, - mType.mElement.mType.mSize, usePadding); - Trace.traceEnd(RenderScript.TRACE_TAG); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked"); + final int dataSize = mType.mElement.getBytesSize() * count; + // AutoPadding for Vec3 Element + boolean usePadding = false; + if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) { + usePadding = true; + } + data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding); + mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt, + mType.mElement.mType.mSize, usePadding); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); + } } /** @@ -1074,28 +1105,31 @@ public class Allocation extends BaseObj { void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array, Element.DataType dt, int arrayLen) { - Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked"); - mRS.validate(); - validate2DRange(xoff, yoff, w, h); - final int dataSize = mType.mElement.getBytesSize() * w * h; - // AutoPadding for Vec3 Element - boolean usePadding = false; - int sizeBytes = arrayLen * dt.mSize; - if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) { - if (dataSize / 4 * 3 > sizeBytes) { - throw new RSIllegalArgumentException("Array too small for allocation type."); - } - usePadding = true; - sizeBytes = dataSize; - } else { - if (dataSize > sizeBytes) { - throw new RSIllegalArgumentException("Array too small for allocation type."); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked"); + mRS.validate(); + validate2DRange(xoff, yoff, w, h); + final int dataSize = mType.mElement.getBytesSize() * w * h; + // AutoPadding for Vec3 Element + boolean usePadding = false; + int sizeBytes = arrayLen * dt.mSize; + if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) { + if (dataSize / 4 * 3 > sizeBytes) { + throw new RSIllegalArgumentException("Array too small for allocation type."); + } + usePadding = true; + sizeBytes = dataSize; + } else { + if (dataSize > sizeBytes) { + throw new RSIllegalArgumentException("Array too small for allocation type."); + } } + mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h, + array, sizeBytes, dt, + mType.mElement.mType.mSize, usePadding); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); } - mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h, - array, sizeBytes, dt, - mType.mElement.mType.mSize, usePadding); - Trace.traceEnd(RenderScript.TRACE_TAG); } /** @@ -1109,11 +1143,14 @@ public class Allocation extends BaseObj { * @param array Data to be placed into the Allocation */ public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) { - Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom"); - copy2DRangeFromUnchecked(xoff, yoff, w, h, array, - validateObjectIsPrimitiveArray(array, true), - java.lang.reflect.Array.getLength(array)); - Trace.traceEnd(RenderScript.TRACE_TAG); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom"); + copy2DRangeFromUnchecked(xoff, yoff, w, h, array, + validateObjectIsPrimitiveArray(array, true), + java.lang.reflect.Array.getLength(array)); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); + } } /** @@ -1194,14 +1231,17 @@ public class Allocation extends BaseObj { */ public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Allocation data, int dataXoff, int dataYoff) { - Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom"); - mRS.validate(); - validate2DRange(xoff, yoff, w, h); - mRS.nAllocationData2D(getIDSafe(), xoff, yoff, - mSelectedLOD, mSelectedFace.mID, - w, h, data.getID(mRS), dataXoff, dataYoff, - data.mSelectedLOD, data.mSelectedFace.mID); - Trace.traceEnd(RenderScript.TRACE_TAG); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom"); + mRS.validate(); + validate2DRange(xoff, yoff, w, h); + mRS.nAllocationData2D(getIDSafe(), xoff, yoff, + mSelectedLOD, mSelectedFace.mID, + w, h, data.getID(mRS), dataXoff, dataYoff, + data.mSelectedLOD, data.mSelectedFace.mID); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); + } } /** @@ -1258,28 +1298,31 @@ public class Allocation extends BaseObj { */ private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, Object array, Element.DataType dt, int arrayLen) { - Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked"); - mRS.validate(); - validate3DRange(xoff, yoff, zoff, w, h, d); - final int dataSize = mType.mElement.getBytesSize() * w * h * d; - // AutoPadding for Vec3 Element - boolean usePadding = false; - int sizeBytes = arrayLen * dt.mSize; - if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) { - if (dataSize / 4 * 3 > sizeBytes) { - throw new RSIllegalArgumentException("Array too small for allocation type."); - } - usePadding = true; - sizeBytes = dataSize; - } else { - if (dataSize > sizeBytes) { - throw new RSIllegalArgumentException("Array too small for allocation type."); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked"); + mRS.validate(); + validate3DRange(xoff, yoff, zoff, w, h, d); + final int dataSize = mType.mElement.getBytesSize() * w * h * d; + // AutoPadding for Vec3 Element + boolean usePadding = false; + int sizeBytes = arrayLen * dt.mSize; + if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) { + if (dataSize / 4 * 3 > sizeBytes) { + throw new RSIllegalArgumentException("Array too small for allocation type."); + } + usePadding = true; + sizeBytes = dataSize; + } else { + if (dataSize > sizeBytes) { + throw new RSIllegalArgumentException("Array too small for allocation type."); + } } + mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d, + array, sizeBytes, dt, + mType.mElement.mType.mSize, usePadding); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); } - mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d, - array, sizeBytes, dt, - mType.mElement.mType.mSize, usePadding); - Trace.traceEnd(RenderScript.TRACE_TAG); } /** @@ -1295,11 +1338,14 @@ public class Allocation extends BaseObj { * @param array to be placed into the allocation */ public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) { - Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom"); - copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array, - validateObjectIsPrimitiveArray(array, true), - java.lang.reflect.Array.getLength(array)); - Trace.traceEnd(RenderScript.TRACE_TAG); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom"); + copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array, + validateObjectIsPrimitiveArray(array, true), + java.lang.reflect.Array.getLength(array)); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); + } } /** @@ -1334,34 +1380,40 @@ public class Allocation extends BaseObj { * @param b The bitmap to be set from the Allocation. */ public void copyTo(Bitmap b) { - Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo"); - mRS.validate(); - validateBitmapFormat(b); - validateBitmapSize(b); - mRS.nAllocationCopyToBitmap(getID(mRS), b); - Trace.traceEnd(RenderScript.TRACE_TAG); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo"); + mRS.validate(); + validateBitmapFormat(b); + validateBitmapSize(b); + mRS.nAllocationCopyToBitmap(getID(mRS), b); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); + } } private void copyTo(Object array, Element.DataType dt, int arrayLen) { - Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo"); - mRS.validate(); - boolean usePadding = false; - if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) { - usePadding = true; - } - if (usePadding) { - if (dt.mSize * arrayLen < mSize / 4 * 3) { - throw new RSIllegalArgumentException( - "Size of output array cannot be smaller than size of allocation."); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo"); + mRS.validate(); + boolean usePadding = false; + if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) { + usePadding = true; } - } else { - if (dt.mSize * arrayLen < mSize) { - throw new RSIllegalArgumentException( - "Size of output array cannot be smaller than size of allocation."); + if (usePadding) { + if (dt.mSize * arrayLen < mSize / 4 * 3) { + throw new RSIllegalArgumentException( + "Size of output array cannot be smaller than size of allocation."); + } + } else { + if (dt.mSize * arrayLen < mSize) { + throw new RSIllegalArgumentException( + "Size of output array cannot be smaller than size of allocation."); + } } + mRS.nAllocationRead(getID(mRS), array, dt, mType.mElement.mType.mSize, usePadding); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); } - mRS.nAllocationRead(getID(mRS), array, dt, mType.mElement.mType.mSize, usePadding); - Trace.traceEnd(RenderScript.TRACE_TAG); } /** @@ -1499,17 +1551,20 @@ public class Allocation extends BaseObj { private void copy1DRangeToUnchecked(int off, int count, Object array, Element.DataType dt, int arrayLen) { - Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeToUnchecked"); - final int dataSize = mType.mElement.getBytesSize() * count; - // AutoPadding for Vec3 Element - boolean usePadding = false; - if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) { - usePadding = true; - } - data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding); - mRS.nAllocationRead1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt, - mType.mElement.mType.mSize, usePadding); - Trace.traceEnd(RenderScript.TRACE_TAG); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeToUnchecked"); + final int dataSize = mType.mElement.getBytesSize() * count; + // AutoPadding for Vec3 Element + boolean usePadding = false; + if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) { + usePadding = true; + } + data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding); + mRS.nAllocationRead1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt, + mType.mElement.mType.mSize, usePadding); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); + } } /** @@ -1649,27 +1704,30 @@ public class Allocation extends BaseObj { void copy2DRangeToUnchecked(int xoff, int yoff, int w, int h, Object array, Element.DataType dt, int arrayLen) { - Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeToUnchecked"); - mRS.validate(); - validate2DRange(xoff, yoff, w, h); - final int dataSize = mType.mElement.getBytesSize() * w * h; - // AutoPadding for Vec3 Element - boolean usePadding = false; - int sizeBytes = arrayLen * dt.mSize; - if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) { - if (dataSize / 4 * 3 > sizeBytes) { - throw new RSIllegalArgumentException("Array too small for allocation type."); - } - usePadding = true; - sizeBytes = dataSize; - } else { - if (dataSize > sizeBytes) { - throw new RSIllegalArgumentException("Array too small for allocation type."); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeToUnchecked"); + mRS.validate(); + validate2DRange(xoff, yoff, w, h); + final int dataSize = mType.mElement.getBytesSize() * w * h; + // AutoPadding for Vec3 Element + boolean usePadding = false; + int sizeBytes = arrayLen * dt.mSize; + if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) { + if (dataSize / 4 * 3 > sizeBytes) { + throw new RSIllegalArgumentException("Array too small for allocation type."); + } + usePadding = true; + sizeBytes = dataSize; + } else { + if (dataSize > sizeBytes) { + throw new RSIllegalArgumentException("Array too small for allocation type."); + } } + mRS.nAllocationRead2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h, + array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); } - mRS.nAllocationRead2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h, - array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding); - Trace.traceEnd(RenderScript.TRACE_TAG); } /** @@ -1757,27 +1815,30 @@ public class Allocation extends BaseObj { */ private void copy3DRangeToUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, Object array, Element.DataType dt, int arrayLen) { - Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeToUnchecked"); - mRS.validate(); - validate3DRange(xoff, yoff, zoff, w, h, d); - final int dataSize = mType.mElement.getBytesSize() * w * h * d; - // AutoPadding for Vec3 Element - boolean usePadding = false; - int sizeBytes = arrayLen * dt.mSize; - if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) { - if (dataSize / 4 * 3 > sizeBytes) { - throw new RSIllegalArgumentException("Array too small for allocation type."); - } - usePadding = true; - sizeBytes = dataSize; - } else { - if (dataSize > sizeBytes) { - throw new RSIllegalArgumentException("Array too small for allocation type."); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeToUnchecked"); + mRS.validate(); + validate3DRange(xoff, yoff, zoff, w, h, d); + final int dataSize = mType.mElement.getBytesSize() * w * h * d; + // AutoPadding for Vec3 Element + boolean usePadding = false; + int sizeBytes = arrayLen * dt.mSize; + if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) { + if (dataSize / 4 * 3 > sizeBytes) { + throw new RSIllegalArgumentException("Array too small for allocation type."); + } + usePadding = true; + sizeBytes = dataSize; + } else { + if (dataSize > sizeBytes) { + throw new RSIllegalArgumentException("Array too small for allocation type."); + } } + mRS.nAllocationRead3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d, + array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); } - mRS.nAllocationRead3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d, - array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding); - Trace.traceEnd(RenderScript.TRACE_TAG); } /* @@ -1815,17 +1876,20 @@ public class Allocation extends BaseObj { * utilized */ static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) { - Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped"); - rs.validate(); - if (type.getID(rs) == 0) { - throw new RSInvalidStateException("Bad Type"); - } - long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0); - if (id == 0) { - throw new RSRuntimeException("Allocation creation failed."); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped"); + rs.validate(); + if (type.getID(rs) == 0) { + throw new RSInvalidStateException("Bad Type"); + } + long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0); + if (id == 0) { + throw new RSRuntimeException("Allocation creation failed."); + } + return new Allocation(id, rs, type, usage); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); } - Trace.traceEnd(RenderScript.TRACE_TAG); - return new Allocation(id, rs, type, usage); } /** @@ -1869,18 +1933,21 @@ public class Allocation extends BaseObj { */ static public Allocation createSized(RenderScript rs, Element e, int count, int usage) { - Trace.traceBegin(RenderScript.TRACE_TAG, "createSized"); - rs.validate(); - Type.Builder b = new Type.Builder(rs, e); - b.setX(count); - Type t = b.create(); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "createSized"); + rs.validate(); + Type.Builder b = new Type.Builder(rs, e); + b.setX(count); + Type t = b.create(); - long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0); - if (id == 0) { - throw new RSRuntimeException("Allocation creation failed."); + long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0); + if (id == 0) { + throw new RSRuntimeException("Allocation creation failed."); + } + return new Allocation(id, rs, t, usage); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); } - Trace.traceEnd(RenderScript.TRACE_TAG); - return new Allocation(id, rs, t, usage); } /** @@ -1939,44 +2006,47 @@ public class Allocation extends BaseObj { static public Allocation createFromBitmap(RenderScript rs, Bitmap b, MipmapControl mips, int usage) { - Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap"); - rs.validate(); + try { + Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap"); + rs.validate(); + + // WAR undocumented color formats + if (b.getConfig() == null) { + if ((usage & USAGE_SHARED) != 0) { + throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config."); + } + Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888); + Canvas c = new Canvas(newBitmap); + c.drawBitmap(b, 0, 0, null); + return createFromBitmap(rs, newBitmap, mips, usage); + } - // WAR undocumented color formats - if (b.getConfig() == null) { - if ((usage & USAGE_SHARED) != 0) { - throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config."); + Type t = typeFromBitmap(rs, b, mips); + + // enable optimized bitmap path only with no mipmap and script-only usage + if (mips == MipmapControl.MIPMAP_NONE && + t.getElement().isCompatible(Element.RGBA_8888(rs)) && + usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) { + long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage); + if (id == 0) { + throw new RSRuntimeException("Load failed."); + } + + // keep a reference to the Bitmap around to prevent GC + Allocation alloc = new Allocation(id, rs, t, usage); + alloc.setBitmap(b); + return alloc; } - Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888); - Canvas c = new Canvas(newBitmap); - c.drawBitmap(b, 0, 0, null); - return createFromBitmap(rs, newBitmap, mips, usage); - } - Type t = typeFromBitmap(rs, b, mips); - // enable optimized bitmap path only with no mipmap and script-only usage - if (mips == MipmapControl.MIPMAP_NONE && - t.getElement().isCompatible(Element.RGBA_8888(rs)) && - usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) { - long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage); + long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage); if (id == 0) { throw new RSRuntimeException("Load failed."); } - - // keep a reference to the Bitmap around to prevent GC - Allocation alloc = new Allocation(id, rs, t, usage); - alloc.setBitmap(b); - return alloc; - } - - - long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage); - if (id == 0) { - throw new RSRuntimeException("Load failed."); + return new Allocation(id, rs, t, usage); + } finally { + Trace.traceEnd(RenderScript.TRACE_TAG); } - Trace.traceEnd(RenderScript.TRACE_TAG); - return new Allocation(id, rs, t, usage); } /** diff --git a/rs/java/android/renderscript/ScriptGroup.java b/rs/java/android/renderscript/ScriptGroup.java index d1a12f9..82d9a2f 100644 --- a/rs/java/android/renderscript/ScriptGroup.java +++ b/rs/java/android/renderscript/ScriptGroup.java @@ -131,28 +131,16 @@ public final class ScriptGroup extends BaseObj { int i; for (i = 0; i < args.length; i++) { - Object obj = args[i]; fieldIDs[i] = 0; - if (obj instanceof Input) { - Input unbound = (Input)obj; - unbound.addReference(this, i); - } else { - retrieveValueAndDependenceInfo(rs, i, args[i], values, sizes, - depClosures, depFieldIDs); - } + retrieveValueAndDependenceInfo(rs, i, null, args[i], + values, sizes, depClosures, depFieldIDs); } - for (Map.Entry<Script.FieldID, Object> entry : globals.entrySet()) { Object obj = entry.getValue(); Script.FieldID fieldID = entry.getKey(); fieldIDs[i] = fieldID.getID(rs); - if (obj instanceof Input) { - Input unbound = (Input)obj; - unbound.addReference(this, fieldID); - } else { - retrieveValueAndDependenceInfo(rs, i, obj, values, - sizes, depClosures, depFieldIDs); - } + retrieveValueAndDependenceInfo(rs, i, fieldID, obj, + values, sizes, depClosures, depFieldIDs); i++; } @@ -184,13 +172,8 @@ public final class ScriptGroup extends BaseObj { Object obj = entry.getValue(); Script.FieldID fieldID = entry.getKey(); fieldIDs[i] = fieldID.getID(rs); - if (obj instanceof Input) { - Input unbound = (Input)obj; - unbound.addReference(this, fieldID); - } else { - retrieveValueAndDependenceInfo(rs, i, obj, values, - sizes, depClosures, depFieldIDs); - } + retrieveValueAndDependenceInfo(rs, i, fieldID, obj, values, + sizes, depClosures, depFieldIDs); i++; } @@ -200,9 +183,8 @@ public final class ScriptGroup extends BaseObj { setID(id); } - private static - void retrieveValueAndDependenceInfo(RenderScript rs, - int index, Object obj, + private void retrieveValueAndDependenceInfo(RenderScript rs, + int index, Script.FieldID fid, Object obj, long[] values, int[] sizes, long[] depClosures, long[] depFieldIDs) { @@ -213,20 +195,25 @@ public final class ScriptGroup extends BaseObj { depClosures[index] = f.getClosure().getID(rs); Script.FieldID fieldID = f.getFieldID(); depFieldIDs[index] = fieldID != null ? fieldID.getID(rs) : 0; - if (obj == null) { - // Value is originally created by the owner closure - values[index] = 0; - sizes[index] = 0; - return; - } } else { depClosures[index] = 0; depFieldIDs[index] = 0; } - ValueAndSize vs = new ValueAndSize(rs, obj); - values[index] = vs.value; - sizes[index] = vs.size; + if (obj instanceof Input) { + Input unbound = (Input)obj; + if (index < mArgs.length) { + unbound.addReference(this, index); + } else { + unbound.addReference(this, fid); + } + values[index] = 0; + sizes[index] = 0; + } else { + ValueAndSize vs = new ValueAndSize(rs, obj); + values[index] = vs.value; + sizes[index] = vs.size; + } } /** @@ -258,7 +245,11 @@ public final class ScriptGroup extends BaseObj { // without an associated value (reference). So this is not working for // cross-module (cross-script) linking in this case where a field not // explicitly bound. - f = new Future(this, field, mBindings.get(field)); + Object obj = mBindings.get(field); + if (obj instanceof Future) { + obj = ((Future)obj).getValue(); + } + f = new Future(this, field, obj); mGlobalFuture.put(field, f); } @@ -266,12 +257,18 @@ public final class ScriptGroup extends BaseObj { } void setArg(int index, Object obj) { + if (obj instanceof Future) { + obj = ((Future)obj).getValue(); + } mArgs[index] = obj; ValueAndSize vs = new ValueAndSize(mRS, obj); mRS.nClosureSetArg(getID(mRS), index, vs.value, vs.size); } void setGlobal(Script.FieldID fieldID, Object obj) { + if (obj instanceof Future) { + obj = ((Future)obj).getValue(); + } mBindings.put(fieldID, obj); ValueAndSize vs = new ValueAndSize(mRS, obj); mRS.nClosureSetGlobal(getID(mRS), fieldID.getID(mRS), vs.value, vs.size); @@ -344,6 +341,7 @@ public final class ScriptGroup extends BaseObj { // -1 means unset. Legal values are 0 .. n-1, where n is the number of // arguments for the referencing closure. List<Pair<Closure, Integer>> mArgIndex; + Object mValue; Input() { mFieldID = new ArrayList<Pair<Closure, Script.FieldID>>(); @@ -359,6 +357,7 @@ public final class ScriptGroup extends BaseObj { } void set(Object value) { + mValue = value; for (Pair<Closure, Integer> p : mArgIndex) { Closure closure = p.first; int index = p.second.intValue(); @@ -370,6 +369,8 @@ public final class ScriptGroup extends BaseObj { closure.setGlobal(fieldID, value); } } + + Object get() { return mValue; } } private String mName; @@ -434,7 +435,11 @@ public final class ScriptGroup extends BaseObj { Object[] outputObjs = new Object[mOutputs2.length]; int i = 0; for (Future f : mOutputs2) { - outputObjs[i++] = f.getValue(); + Object output = f.getValue(); + if (output instanceof Input) { + output = ((Input)output).get(); + } + outputObjs[i++] = output; } return outputObjs; } @@ -592,7 +597,8 @@ public final class ScriptGroup extends BaseObj { Node n = mNodes.get(ct); if (n.mInputs.size() == 0) { if (n.mOutputs.size() == 0 && mNodes.size() > 1) { - throw new RSInvalidStateException("Groups cannot contain unconnected scripts"); + String msg = "Groups cannot contain unconnected scripts"; + throw new RSInvalidStateException(msg); } validateDAGRecurse(n, ct+1); } |