diff options
Diffstat (limited to 'graphics/java/android/renderscript/Allocation.java')
-rw-r--r-- | graphics/java/android/renderscript/Allocation.java | 256 |
1 files changed, 212 insertions, 44 deletions
diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java index b8564b6..5d1990a 100644 --- a/graphics/java/android/renderscript/Allocation.java +++ b/graphics/java/android/renderscript/Allocation.java @@ -18,6 +18,7 @@ package android.renderscript; import java.io.IOException; import java.io.InputStream; +import java.util.HashMap; import android.content.res.Resources; import android.content.res.AssetManager; import android.graphics.Bitmap; @@ -92,6 +93,9 @@ public class Allocation extends BaseObj { int mCurrentDimY; int mCurrentDimZ; int mCurrentCount; + static HashMap<Integer, Allocation> mAllocationMap = + new HashMap<Integer, Allocation>(); + IoInputNotifier mBufferNotifier; /** @@ -362,11 +366,20 @@ public class Allocation extends BaseObj { */ public void syncAll(int srcLocation) { switch (srcLocation) { + case USAGE_GRAPHICS_TEXTURE: case USAGE_SCRIPT: + if ((mUsage & USAGE_SHARED) != 0) { + copyFrom(mBitmap); + } + break; case USAGE_GRAPHICS_CONSTANTS: - case USAGE_GRAPHICS_TEXTURE: 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."); } @@ -492,7 +505,9 @@ public class Allocation extends BaseObj { */ public void copyFromUnchecked(int[] d) { mRS.validate(); - if (mCurrentDimY > 0) { + if (mCurrentDimZ > 0) { + copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d); + } else if (mCurrentDimY > 0) { copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d); } else { copy1DRangeFromUnchecked(0, mCurrentCount, d); @@ -507,7 +522,9 @@ public class Allocation extends BaseObj { */ public void copyFromUnchecked(short[] d) { mRS.validate(); - if (mCurrentDimY > 0) { + if (mCurrentDimZ > 0) { + copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d); + } else if (mCurrentDimY > 0) { copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d); } else { copy1DRangeFromUnchecked(0, mCurrentCount, d); @@ -522,7 +539,9 @@ public class Allocation extends BaseObj { */ public void copyFromUnchecked(byte[] d) { mRS.validate(); - if (mCurrentDimY > 0) { + if (mCurrentDimZ > 0) { + copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d); + } else if (mCurrentDimY > 0) { copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d); } else { copy1DRangeFromUnchecked(0, mCurrentCount, d); @@ -537,7 +556,9 @@ public class Allocation extends BaseObj { */ public void copyFromUnchecked(float[] d) { mRS.validate(); - if (mCurrentDimY > 0) { + if (mCurrentDimZ > 0) { + copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d); + } else if (mCurrentDimY > 0) { copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d); } else { copy1DRangeFromUnchecked(0, mCurrentCount, d); @@ -553,7 +574,9 @@ public class Allocation extends BaseObj { */ public void copyFrom(int[] d) { mRS.validate(); - if (mCurrentDimY > 0) { + if (mCurrentDimZ > 0) { + copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d); + } else if (mCurrentDimY > 0) { copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d); } else { copy1DRangeFrom(0, mCurrentCount, d); @@ -569,7 +592,9 @@ public class Allocation extends BaseObj { */ public void copyFrom(short[] d) { mRS.validate(); - if (mCurrentDimY > 0) { + if (mCurrentDimZ > 0) { + copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d); + } else if (mCurrentDimY > 0) { copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d); } else { copy1DRangeFrom(0, mCurrentCount, d); @@ -585,7 +610,9 @@ public class Allocation extends BaseObj { */ public void copyFrom(byte[] d) { mRS.validate(); - if (mCurrentDimY > 0) { + if (mCurrentDimZ > 0) { + copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d); + } else if (mCurrentDimY > 0) { copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d); } else { copy1DRangeFrom(0, mCurrentCount, d); @@ -601,7 +628,9 @@ public class Allocation extends BaseObj { */ public void copyFrom(float[] d) { mRS.validate(); - if (mCurrentDimY > 0) { + if (mCurrentDimZ > 0) { + copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d); + } else if (mCurrentDimY > 0) { copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d); } else { copy1DRangeFrom(0, mCurrentCount, d); @@ -967,12 +996,144 @@ public class Allocation extends BaseObj { Canvas c = new Canvas(newBitmap); c.drawBitmap(data, 0, 0, null); copy2DRangeFrom(xoff, yoff, newBitmap); + return; } validateBitmapFormat(data); validate2DRange(xoff, yoff, data.getWidth(), data.getHeight()); mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data); } + private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) { + if (mAdaptedAllocation != null) { + + } else { + + if (xoff < 0 || yoff < 0 || zoff < 0) { + throw new RSIllegalArgumentException("Offset cannot be negative."); + } + if (h < 0 || w < 0 || d < 0) { + throw new RSIllegalArgumentException("Height or width cannot be negative."); + } + if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) { + throw new RSIllegalArgumentException("Updated region larger than allocation."); + } + } + } + + /** + * @hide + * + */ + void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) { + mRS.validate(); + validate3DRange(xoff, yoff, zoff, w, h, d); + mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, + w, h, d, data, data.length); + } + + /** + * @hide + * + */ + void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) { + mRS.validate(); + validate3DRange(xoff, yoff, zoff, w, h, d); + mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, + w, h, d, data, data.length * 2); + } + + /** + * @hide + * + */ + void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) { + mRS.validate(); + validate3DRange(xoff, yoff, zoff, w, h, d); + mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, + w, h, d, data, data.length * 4); + } + + /** + * @hide + * + */ + void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) { + mRS.validate(); + validate3DRange(xoff, yoff, zoff, w, h, d); + mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, + w, h, d, data, data.length * 4); + } + + + /** + * @hide + * Copy a rectangular region from the array into the allocation. + * The incoming array is assumed to be tightly packed. + * + * @param xoff X offset of the region to update + * @param yoff Y offset of the region to update + * @param zoff Z offset of the region to update + * @param w Width of the incoming region to update + * @param h Height of the incoming region to update + * @param d Depth of the incoming region to update + * @param data to be placed into the allocation + */ + public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) { + validateIsInt8(); + copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data); + } + + /** + * @hide + * + */ + public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) { + validateIsInt16(); + copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data); + } + + /** + * @hide + * + */ + public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) { + validateIsInt32(); + copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data); + } + + /** + * @hide + * + */ + public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) { + validateIsFloat32(); + copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data); + } + + /** + * @hide + * Copy a rectangular region into the allocation from another + * allocation. + * + * @param xoff X offset of the region to update. + * @param yoff Y offset of the region to update. + * @param w Width of the incoming region to update. + * @param h Height of the incoming region to update. + * @param d Depth of the incoming region to update. + * @param data source allocation. + * @param dataXoff X offset in data of the region to update. + * @param dataYoff Y offset in data of the region to update. + * @param dataZoff Z offset in data of the region to update + */ + public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, + Allocation data, int dataXoff, int dataYoff, int dataZoff) { + mRS.validate(); + validate3DRange(xoff, yoff, zoff, w, h, d); + mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, + w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff, + data.mSelectedLOD); + } + /** * Copy from the Allocation into a Bitmap. The bitmap must @@ -1050,6 +1211,10 @@ public class Allocation extends BaseObj { * A new type will be created with the new dimension. * * @param dimX The new size of the allocation. + * + * @deprecated Renderscript objects should be immutable once + * created. The replacement is to create a new allocation and copy the + * contents. */ public synchronized void resize(int dimX) { if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) { @@ -1064,38 +1229,6 @@ public class Allocation extends BaseObj { updateCacheInfo(mType); } - /** - * Resize a 2D allocation. The contents of the allocation are - * preserved. If new elements are allocated objects are created - * with null contents and the new region is otherwise undefined. - * - * If the new region is smaller the references of any objects - * outside the new region will be released. - * - * A new type will be created with the new dimension. - * - * @param dimX The new size of the allocation. - * @param dimY The new size of the allocation. - */ - public synchronized void resize(int dimX, int dimY) { - if ((mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) { - throw new RSInvalidStateException( - "Resize only support for 2D allocations at this time."); - } - if (mType.getY() == 0) { - throw new RSInvalidStateException( - "Resize only support for 2D allocations at this time."); - } - mRS.nAllocationResize2D(getID(mRS), dimX, dimY); - mRS.finish(); // Necessary because resize is fifoed and update is async. - - int typeID = mRS.nAllocationGetType(getID(mRS)); - mType = new Type(typeID, mRS); - mType.updateFromNative(); - updateCacheInfo(mType); - } - - // creation @@ -1254,7 +1387,7 @@ public class Allocation extends BaseObj { // 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 == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) { int id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage); if (id == 0) { throw new RSRuntimeException("Load failed."); @@ -1326,7 +1459,7 @@ public class Allocation extends BaseObj { static public Allocation createFromBitmap(RenderScript rs, Bitmap b) { if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) { return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE, - USAGE_SHARED | USAGE_SCRIPT); + USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE); } return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE, USAGE_GRAPHICS_TEXTURE); @@ -1544,7 +1677,7 @@ public class Allocation extends BaseObj { if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) { return createFromBitmapResource(rs, res, id, MipmapControl.MIPMAP_NONE, - USAGE_SHARED | USAGE_SCRIPT); + USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE); } return createFromBitmapResource(rs, res, id, MipmapControl.MIPMAP_NONE, @@ -1576,6 +1709,41 @@ public class Allocation extends BaseObj { throw new RSRuntimeException("Could not convert string to utf-8."); } } + + /** + * Interface to handle notification when new buffers are + * available via USAGE_IO_INPUT. An application will receive + * one notification when a buffer is available. Additional + * buffers will not trigger new notifications until a buffer is + * processed. + */ + public interface IoInputNotifier { + public void onBufferAvailable(Allocation a); + } + + /** + * Set a notification handler for USAGE_IO_INPUT + * + * @param callback instance of the IoInputNotifier class to be called + * when buffer arrive. + */ + public void setIoInputNotificationHandler(IoInputNotifier callback) { + synchronized(mAllocationMap) { + mAllocationMap.put(new Integer(getID(mRS)), this); + mBufferNotifier = callback; + } + } + + static void sendBufferNotification(int id) { + synchronized(mAllocationMap) { + Allocation a = mAllocationMap.get(new Integer(id)); + + if ((a != null) && (a.mBufferNotifier != null)) { + a.mBufferNotifier.onBufferAvailable(a); + } + } + } + } |