diff options
author | Miao Wang <miaowang@google.com> | 2015-11-25 11:21:13 -0800 |
---|---|---|
committer | Miao Wang <miaowang@google.com> | 2016-01-25 16:12:37 -0800 |
commit | 0facf021ea1a0399d956372b9d3ad9025a9a04d2 (patch) | |
tree | 158e05cbc7e58d435a2b242503b4cf5430cc55eb /rs/java/android/renderscript/Allocation.java | |
parent | 9e892f53dd489c824b6a39c45028f838123daa85 (diff) |
[RenderScript] Add API to map Allocation mallocptr to Java ByteBuffer
Bug: 25926361
Bug: 23535524
- Construct the ByteBuffer using the AllocationGetPointer.
- Add an API to query the stride of the allocation.
- Both ByteBuffer and Stride will be cached for normal Allocations.
if using USAGE_IO, since after each ioReceive, the mallocPtr will
change, getByteBuffer will always create a new one using the most
up-to-date mallocPtr.
Change-Id: I5e84b6690e83bb062c383043275524d0e51e46eb
Diffstat (limited to 'rs/java/android/renderscript/Allocation.java')
-rw-r--r-- | rs/java/android/renderscript/Allocation.java | 61 |
1 files changed, 59 insertions, 2 deletions
diff --git a/rs/java/android/renderscript/Allocation.java b/rs/java/android/renderscript/Allocation.java index a4876b92fadf..a71ba6364ac8 100644 --- a/rs/java/android/renderscript/Allocation.java +++ b/rs/java/android/renderscript/Allocation.java @@ -16,14 +16,16 @@ package android.renderscript; +import java.nio.ByteBuffer; import java.util.HashMap; + import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.view.Surface; -import android.util.Log; import android.graphics.Canvas; import android.os.Trace; +import android.util.Log; +import android.view.Surface; /** * <p> This class provides the primary method through which data is passed to @@ -78,6 +80,8 @@ public class Allocation extends BaseObj { OnBufferAvailableListener mBufferNotifier; private Surface mGetSurfaceSurface = null; + private ByteBuffer mByteBuffer = null; + private long mByteBufferStride = -1; private Element.DataType validateObjectIsPrimitiveArray(Object d, boolean checkType) { final Class c = d.getClass(); @@ -2050,6 +2054,59 @@ public class Allocation extends BaseObj { } /** + * @hide + * Gets or creates a ByteBuffer that contains the raw data of the current Allocation. + * If the Allocation is created with USAGE_IO_INPUT, the returned ByteBuffer + * would contain the up-to-date data as READ ONLY. + * For a 2D or 3D Allocation, the raw data maybe padded so that each row of + * the Allocation has certain alignment. The size of each row including padding, + * called stride, can be queried using the {@link #getStride()} method. + * + * Note: Operating on the ByteBuffer of a destroyed Allocation will triger errors. + * + * @return ByteBuffer The ByteBuffer associated with raw data pointer of the Allocation. + */ + public ByteBuffer getByteBuffer() { + // Create a new ByteBuffer if it is not initialized or using IO_INPUT. + if (mType.hasFaces()) { + throw new RSInvalidStateException("Cubemap is not supported for getByteBuffer()."); + } + if (mType.getYuv() == android.graphics.ImageFormat.NV21 || + mType.getYuv() == android.graphics.ImageFormat.YV12 || + mType.getYuv() == android.graphics.ImageFormat.YUV_420_888 ) { + throw new RSInvalidStateException("YUV format is not supported for getByteBuffer()."); + } + if (mByteBuffer == null || (mUsage & USAGE_IO_INPUT) != 0) { + int xBytesSize = mType.getX() * mType.getElement().getBytesSize(); + long[] stride = new long[1]; + mByteBuffer = mRS.nAllocationGetByteBuffer(getID(mRS), stride, xBytesSize, mType.getY(), mType.getZ()); + mByteBufferStride = stride[0]; + } + if ((mUsage & USAGE_IO_INPUT) != 0) { + return mByteBuffer.asReadOnlyBuffer(); + } + return mByteBuffer; + } + + /** + * @hide + * Gets the stride of the Allocation. + * For a 2D or 3D Allocation, the raw data maybe padded so that each row of + * the Allocation has certain alignment. The size of each row including such + * padding is called stride. + * + * @return the stride. For 1D Allocation, the stride will be the number of + * bytes of this Allocation. For 2D and 3D Allocations, the stride + * will be the stride in X dimension measuring in bytes. + */ + public long getStride() { + if (mByteBufferStride == -1) { + getByteBuffer(); + } + return mByteBufferStride; + } + + /** * Returns the handle to a raw buffer that is being managed by the screen * compositor. This operation is only valid for Allocations with {@link * #USAGE_IO_INPUT}. |