summaryrefslogtreecommitdiff
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.java74
1 files changed, 69 insertions, 5 deletions
diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java
index e6a5583afe04..5f0844af2f52 100644
--- a/graphics/java/android/renderscript/Allocation.java
+++ b/graphics/java/android/renderscript/Allocation.java
@@ -148,6 +148,19 @@ public class Allocation extends BaseObj {
public static final int USAGE_IO_OUTPUT = 0x0040;
/**
+ * USAGE_SHARED The allocation's backing store will be inherited
+ * from another object (usually a Bitmap); calling appropriate
+ * copy methods will be significantly faster than if the entire
+ * allocation were copied every time.
+ *
+ * This is set by default for allocations created with
+ * CreateFromBitmap(RenderScript, Bitmap) in API version 18 and
+ * higher.
+ *
+ */
+ public static final int USAGE_SHARED = 0x0080;
+
+ /**
* Controls mipmap behavior when using the bitmap creation and
* update functions.
*/
@@ -233,6 +246,10 @@ public class Allocation extends BaseObj {
}
}
+ private void setBitmap(Bitmap b) {
+ mBitmap = b;
+ }
+
Allocation(int id, RenderScript rs, Type t, int usage) {
super(id, rs);
if ((usage & ~(USAGE_SCRIPT |
@@ -241,7 +258,8 @@ public class Allocation extends BaseObj {
USAGE_GRAPHICS_CONSTANTS |
USAGE_GRAPHICS_RENDER_TARGET |
USAGE_IO_INPUT |
- USAGE_IO_OUTPUT)) != 0) {
+ USAGE_IO_OUTPUT |
+ USAGE_SHARED)) != 0) {
throw new RSIllegalArgumentException("Unknown usage specified.");
}
@@ -568,6 +586,21 @@ public class Allocation extends BaseObj {
}
/**
+ * Copy an allocation from an allocation. The types of both allocations
+ * must be identical.
+ *
+ * @param a the source allocation
+ */
+ public void copyFrom(Allocation a) {
+ mRS.validate();
+ if (!mType.equals(a.getType())) {
+ throw new RSIllegalArgumentException("Types of allocations must match.");
+ }
+ copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
+ }
+
+
+ /**
* This is only intended to be used by auto-generate code reflected from the
* renderscript script files.
*
@@ -971,11 +1004,10 @@ public class Allocation extends BaseObj {
*
* 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) {
+ 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.");
@@ -1137,6 +1169,22 @@ public class Allocation extends BaseObj {
rs.validate();
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)) {
+ int 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;
+ }
+
+
int id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
if (id == 0) {
throw new RSRuntimeException("Load failed.");
@@ -1202,8 +1250,11 @@ public class Allocation extends BaseObj {
}
/**
- * Creates a non-mipmapped renderscript allocation to use as a
- * graphics texture
+ * Creates a RenderScript allocation from a bitmap.
+ *
+ * With target API version 18 or greater, this allocation will be
+ * created with USAGE_SHARED. With target API version 17 or lower,
+ * this allocation will be created with USAGE_GRAPHICS_TEXTURE.
*
* @param rs Context to which the allocation will belong.
* @param b bitmap source for the allocation data
@@ -1212,6 +1263,10 @@ 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);
+ }
return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
USAGE_GRAPHICS_TEXTURE);
}
@@ -1411,6 +1466,10 @@ public class Allocation extends BaseObj {
* Creates a non-mipmapped renderscript allocation to use as a
* graphics texture from the bitmap referenced by resource id
*
+ * With target API version 18 or greater, this allocation will be
+ * created with USAGE_SHARED. With target API version 17 or lower,
+ * this allocation will be created with USAGE_GRAPHICS_TEXTURE.
+ *
* @param rs Context to which the allocation will belong.
* @param res application resources
* @param id resource id to load the data from
@@ -1421,6 +1480,11 @@ public class Allocation extends BaseObj {
static public Allocation createFromBitmapResource(RenderScript rs,
Resources res,
int id) {
+ if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
+ return createFromBitmapResource(rs, res, id,
+ MipmapControl.MIPMAP_NONE,
+ USAGE_SHARED | USAGE_SCRIPT);
+ }
return createFromBitmapResource(rs, res, id,
MipmapControl.MIPMAP_NONE,
USAGE_GRAPHICS_TEXTURE);