summaryrefslogtreecommitdiffstats
path: root/graphics/java/android/renderscript/Allocation.java
diff options
context:
space:
mode:
Diffstat (limited to 'graphics/java/android/renderscript/Allocation.java')
-rw-r--r--graphics/java/android/renderscript/Allocation.java283
1 files changed, 253 insertions, 30 deletions
diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java
index f285f5b..cd5300d 100644
--- a/graphics/java/android/renderscript/Allocation.java
+++ b/graphics/java/android/renderscript/Allocation.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2008 The Android Open Source Project
+ * Copyright (C) 2008-2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,8 @@ import android.content.res.Resources;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
+import android.view.Surface;
+import android.graphics.SurfaceTexture;
import android.util.Log;
import android.util.TypedValue;
@@ -78,6 +80,8 @@ public class Allocation extends BaseObj {
boolean mConstrainedFace;
boolean mConstrainedY;
boolean mConstrainedZ;
+ boolean mReadAllowed = true;
+ boolean mWriteAllowed = true;
int mSelectedY;
int mSelectedZ;
int mSelectedLOD;
@@ -99,7 +103,7 @@ public class Allocation extends BaseObj {
public static final int USAGE_SCRIPT = 0x0001;
/**
- * GRAPHICS_TEXTURE The allcation will be used as a texture
+ * GRAPHICS_TEXTURE The allocation will be used as a texture
* source by one or more graphics programs.
*
*/
@@ -121,12 +125,29 @@ public class Allocation extends BaseObj {
public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
/**
- * USAGE_GRAPHICS_RENDER_TARGET The allcation will be used as a
+ * USAGE_GRAPHICS_RENDER_TARGET The allocation will be used as a
* target for offscreen rendering
*
*/
public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
+ /**
+ * USAGE_IO_INPUT The allocation will be used as SurfaceTexture
+ * consumer. This usage will cause the allocation to be created
+ * read only.
+ *
+ * @hide
+ */
+ public static final int USAGE_IO_INPUT = 0x0020;
+
+ /**
+ * USAGE_IO_OUTPUT The allocation will be used as a
+ * SurfaceTexture producer. The dimensions and format of the
+ * SurfaceTexture will be forced to those of the allocation.
+ *
+ * @hide
+ */
+ public static final int USAGE_IO_OUTPUT = 0x0040;
/**
* Controls mipmap behavior when using the bitmap creation and
@@ -163,9 +184,43 @@ public class Allocation extends BaseObj {
private int getIDSafe() {
if (mAdaptedAllocation != null) {
- return mAdaptedAllocation.getID();
+ return mAdaptedAllocation.getID(mRS);
}
- return getID();
+ return getID(mRS);
+ }
+
+
+ /**
+ * Get the element of the type of the Allocation.
+ *
+ * @hide
+ * @return Element
+ *
+ */
+ public Element getElement() {
+ return mType.getElement();
+ }
+
+ /**
+ * Get the usage flags of the Allocation.
+ *
+ * @hide
+ * @return usage
+ *
+ */
+ public int getUsage() {
+ return mUsage;
+ }
+
+ /**
+ * Get the size of the Allocation in bytes.
+ *
+ * @hide
+ * @return sizeInBytes
+ *
+ */
+ public int getSizeBytes() {
+ return mType.getCount() * mType.getElement().getSizeBytes();
}
private void updateCacheInfo(Type t) {
@@ -187,10 +242,24 @@ public class Allocation extends BaseObj {
USAGE_GRAPHICS_TEXTURE |
USAGE_GRAPHICS_VERTEX |
USAGE_GRAPHICS_CONSTANTS |
- USAGE_GRAPHICS_RENDER_TARGET)) != 0) {
+ USAGE_GRAPHICS_RENDER_TARGET |
+ USAGE_IO_INPUT |
+ USAGE_IO_OUTPUT)) != 0) {
throw new RSIllegalArgumentException("Unknown usage specified.");
}
+
+ if ((usage & USAGE_IO_INPUT) != 0) {
+ mWriteAllowed = false;
+
+ if ((usage & ~(USAGE_IO_INPUT |
+ USAGE_GRAPHICS_TEXTURE |
+ USAGE_SCRIPT)) != 0) {
+ throw new RSIllegalArgumentException("Invalid usage combination.");
+ }
+ }
+
mType = t;
+ mUsage = usage;
if (t != null) {
updateCacheInfo(t);
@@ -252,7 +321,7 @@ public class Allocation extends BaseObj {
@Override
void updateFromNative() {
super.updateFromNative();
- int typeID = mRS.nAllocationGetType(getID());
+ int typeID = mRS.nAllocationGetType(getID(mRS));
if(typeID != 0) {
mType = new Type(typeID, mRS);
mType.updateFromNative();
@@ -260,10 +329,21 @@ public class Allocation extends BaseObj {
}
}
+ /**
+ * Get the type of the Allocation.
+ *
+ * @return Type
+ *
+ */
public Type getType() {
return mType;
}
+ /**
+ * Propagate changes from one usage of the allocation to the
+ * remaining usages of the allocation.
+ *
+ */
public void syncAll(int srcLocation) {
switch (srcLocation) {
case USAGE_SCRIPT:
@@ -278,6 +358,50 @@ public class Allocation extends BaseObj {
mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
}
+ /**
+ * Send a buffer to the output stream. The contents of the
+ * Allocation will be undefined after this operation.
+ *
+ * @hide
+ *
+ */
+ public void 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));
+ }
+
+ /**
+ * Delete once code is updated.
+ * @hide
+ */
+ public void ioSendOutput() {
+ ioSend();
+ }
+
+ /**
+ * Receive the latest input into the Allocation.
+ *
+ * @hide
+ *
+ */
+ public void ioReceive() {
+ if ((mUsage & USAGE_IO_INPUT) == 0) {
+ throw new RSIllegalArgumentException(
+ "Can only receive if IO_INPUT usage specified.");
+ }
+ mRS.validate();
+ mRS.nAllocationIoReceive(getID(mRS));
+ }
+
+ /**
+ * Copy an array of RS objects to the allocation.
+ *
+ * @param d Source array.
+ */
public void copyFrom(BaseObj[] d) {
mRS.validate();
validateIsObject();
@@ -287,7 +411,7 @@ public class Allocation extends BaseObj {
}
int i[] = new int[d.length];
for (int ct=0; ct < d.length; ct++) {
- i[ct] = d[ct].getID();
+ i[ct] = d[ct].getID(mRS);
}
copy1DRangeFromUnchecked(0, mCurrentCount, i);
}
@@ -447,7 +571,7 @@ public class Allocation extends BaseObj {
mRS.validate();
validateBitmapSize(b);
validateBitmapFormat(b);
- mRS.nAllocationCopyFromBitmap(getID(), b);
+ mRS.nAllocationCopyFromBitmap(getID(mRS), b);
}
/**
@@ -458,6 +582,7 @@ public class Allocation extends BaseObj {
* @param fp
*/
public void setFromFieldPacker(int xoff, FieldPacker fp) {
+ mRS.validate();
int eSize = mType.mElement.getSizeBytes();
final byte[] data = fp.getData();
@@ -478,6 +603,7 @@ public class Allocation extends BaseObj {
* @param fp
*/
public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
+ mRS.validate();
if (component_number >= mType.mElement.mElements.length) {
throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
}
@@ -487,6 +613,7 @@ public class Allocation extends BaseObj {
final byte[] data = fp.getData();
int eSize = mType.mElement.mElements[component_number].getSizeBytes();
+ eSize *= mType.mElement.mArraySizes[component_number];
if (data.length != eSize) {
throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
@@ -525,7 +652,7 @@ public class Allocation extends BaseObj {
* followup sync will be required.
*/
public void generateMipmaps() {
- mRS.nAllocationGenerateMipmaps(getID());
+ mRS.nAllocationGenerateMipmaps(getID(mRS));
}
/**
@@ -653,7 +780,7 @@ public class Allocation extends BaseObj {
public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
mRS.nAllocationData2D(getIDSafe(), off, 0,
mSelectedLOD, mSelectedFace.mID,
- count, 1, data.getID(), dataOff, 0,
+ count, 1, data.getID(mRS), dataOff, 0,
data.mSelectedLOD, data.mSelectedFace.mID);
}
@@ -730,7 +857,7 @@ public class Allocation extends BaseObj {
validate2DRange(xoff, yoff, w, h);
mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
mSelectedLOD, mSelectedFace.mID,
- w, h, data.getID(), dataXoff, dataYoff,
+ w, h, data.getID(mRS), dataXoff, dataYoff,
data.mSelectedLOD, data.mSelectedFace.mID);
}
@@ -761,7 +888,7 @@ public class Allocation extends BaseObj {
mRS.validate();
validateBitmapFormat(b);
validateBitmapSize(b);
- mRS.nAllocationCopyToBitmap(getID(), b);
+ mRS.nAllocationCopyToBitmap(getID(mRS), b);
}
/**
@@ -774,7 +901,7 @@ public class Allocation extends BaseObj {
public void copyTo(byte[] d) {
validateIsInt8();
mRS.validate();
- mRS.nAllocationRead(getID(), d);
+ mRS.nAllocationRead(getID(mRS), d);
}
/**
@@ -787,7 +914,7 @@ public class Allocation extends BaseObj {
public void copyTo(short[] d) {
validateIsInt16();
mRS.validate();
- mRS.nAllocationRead(getID(), d);
+ mRS.nAllocationRead(getID(mRS), d);
}
/**
@@ -800,7 +927,7 @@ public class Allocation extends BaseObj {
public void copyTo(int[] d) {
validateIsInt32();
mRS.validate();
- mRS.nAllocationRead(getID(), d);
+ mRS.nAllocationRead(getID(mRS), d);
}
/**
@@ -813,7 +940,7 @@ public class Allocation extends BaseObj {
public void copyTo(float[] d) {
validateIsFloat32();
mRS.validate();
- mRS.nAllocationRead(getID(), d);
+ mRS.nAllocationRead(getID(mRS), d);
}
/**
@@ -832,26 +959,46 @@ public class Allocation extends BaseObj {
if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
}
- mRS.nAllocationResize1D(getID(), dimX);
+ mRS.nAllocationResize1D(getID(mRS), dimX);
mRS.finish(); // Necessary because resize is fifoed and update is async.
- int typeID = mRS.nAllocationGetType(getID());
+ int typeID = mRS.nAllocationGetType(getID(mRS));
mType = new Type(typeID, mRS);
mType.updateFromNative();
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.
+ *
+ * @hide
+ * @param dimX The new size of the allocation.
+ * @param dimY The new size of the allocation.
+ */
public void resize(int dimX, int dimY) {
- if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
- throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
+ 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 RSIllegalStateException("Resize only support for 2D allocations at this time.");
+ throw new RSInvalidStateException(
+ "Resize only support for 2D allocations at this time.");
}
- mRS.nAllocationResize2D(getID(), dimX, dimY);
+ 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);
}
- */
@@ -872,10 +1019,34 @@ public class Allocation extends BaseObj {
*/
static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
rs.validate();
- if (type.getID() == 0) {
+ if (type.getID(rs) == 0) {
+ throw new RSInvalidStateException("Bad Type");
+ }
+ int 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);
+ }
+
+ /**
+ * @hide
+ * This API is hidden and only intended to be used for
+ * transitional purposes.
+ *
+ * @param type renderscript type describing data layout
+ * @param mips specifies desired mipmap behaviour for the
+ * allocation
+ * @param usage bit field specifying how the allocation is
+ * utilized
+ */
+ static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips,
+ int usage, int pointer) {
+ rs.validate();
+ if (type.getID(rs) == 0) {
throw new RSInvalidStateException("Bad Type");
}
- int id = rs.nAllocationCreateTyped(type.getID(), mips.mID, usage);
+ int id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, pointer);
if (id == 0) {
throw new RSRuntimeException("Allocation creation failed.");
}
@@ -930,7 +1101,7 @@ public class Allocation extends BaseObj {
b.setX(count);
Type t = b.create();
- int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage);
+ int id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
if (id == 0) {
throw new RSRuntimeException("Allocation creation failed.");
}
@@ -997,7 +1168,7 @@ public class Allocation extends BaseObj {
rs.validate();
Type t = typeFromBitmap(rs, b, mips);
- int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage);
+ int id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
if (id == 0) {
throw new RSRuntimeException("Load failed.");
}
@@ -1005,6 +1176,58 @@ public class Allocation extends BaseObj {
}
/**
+ *
+ *
+ * @hide
+ *
+ */
+ public SurfaceTexture getSurfaceTexture() {
+ if ((mUsage & USAGE_IO_INPUT) == 0) {
+ throw new RSInvalidStateException("Allocation is not a surface texture.");
+ }
+
+ int id = mRS.nAllocationGetSurfaceTextureID(getID(mRS));
+ SurfaceTexture st = new SurfaceTexture(id);
+ mRS.nAllocationGetSurfaceTextureID2(getID(mRS), st);
+
+ return st;
+ }
+
+ /**
+ *
+ * @hide
+ *
+ */
+ public Surface getSurface() {
+ return new Surface(getSurfaceTexture());
+ }
+
+ /**
+ * @hide
+ */
+ public void setSurface(Surface sur) {
+ mRS.validate();
+ if ((mUsage & USAGE_IO_OUTPUT) == 0) {
+ throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
+ }
+
+ mRS.nAllocationSetSurface(getID(mRS), sur);
+ }
+
+ /**
+ * @hide
+ */
+ public void setSurfaceTexture(SurfaceTexture st) {
+ mRS.validate();
+ if ((mUsage & USAGE_IO_OUTPUT) == 0) {
+ throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
+ }
+
+ Surface s = new Surface(st);
+ mRS.nAllocationSetSurface(getID(mRS), s);
+ }
+
+ /**
* Creates a non-mipmapped renderscript allocation to use as a
* graphics texture
*
@@ -1060,7 +1283,7 @@ public class Allocation extends BaseObj {
tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
Type t = tb.create();
- int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage);
+ int id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
if(id == 0) {
throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
}