Merge "[RenderScript] Implement APIs for better multi-frame process support."

am: 6ebdbfa549

* commit '6ebdbfa5498c486a96f2b8b07858eb1a25a6d08a':
  [RenderScript] Implement APIs for better multi-frame process support.
This commit is contained in:
Miao Wang
2016-01-27 21:43:27 +00:00
committed by android-build-merger
3 changed files with 188 additions and 15 deletions

View File

@@ -55,12 +55,16 @@ import android.view.Surface;
**/
public class Allocation extends BaseObj {
private static final int MAX_NUMBER_IO_INPUT_ALLOC = 16;
Type mType;
Bitmap mBitmap;
int mUsage;
Allocation mAdaptedAllocation;
int mSize;
MipmapControl mMipmapControl;
long mTimeStamp = -1;
boolean mReadAllowed = true;
boolean mWriteAllowed = true;
boolean mAutoPadding = false;
@@ -277,6 +281,17 @@ public class Allocation extends BaseObj {
return mUsage;
}
/**
* @hide
* Get the Mipmap control flag of the Allocation.
*
* @return the Mipmap control flag of the Allocation
*
*/
public MipmapControl getMipmap() {
return mMipmapControl;
}
/**
* Enable/Disable AutoPadding for Vec3 elements.
* By default: Diabled.
@@ -359,6 +374,11 @@ public class Allocation extends BaseObj {
}
}
Allocation(long id, RenderScript rs, Type t, int usage, MipmapControl mips) {
this(id, rs, t, usage);
mMipmapControl = mips;
}
protected void finalize() throws Throwable {
RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
super.finalize();
@@ -521,7 +541,7 @@ public class Allocation extends BaseObj {
"Can only receive if IO_INPUT usage specified.");
}
mRS.validate();
mRS.nAllocationIoReceive(getID(mRS));
mTimeStamp = mRS.nAllocationIoReceive(getID(mRS));
} finally {
Trace.traceEnd(RenderScript.TRACE_TAG);
}
@@ -1890,7 +1910,7 @@ public class Allocation extends BaseObj {
if (id == 0) {
throw new RSRuntimeException("Allocation creation failed.");
}
return new Allocation(id, rs, type, usage);
return new Allocation(id, rs, type, usage, mips);
} finally {
Trace.traceEnd(RenderScript.TRACE_TAG);
}
@@ -1948,7 +1968,7 @@ public class Allocation extends BaseObj {
if (id == 0) {
throw new RSRuntimeException("Allocation creation failed.");
}
return new Allocation(id, rs, t, usage);
return new Allocation(id, rs, t, usage, MipmapControl.MIPMAP_NONE);
} finally {
Trace.traceEnd(RenderScript.TRACE_TAG);
}
@@ -2037,7 +2057,7 @@ public class Allocation extends BaseObj {
}
// keep a reference to the Bitmap around to prevent GC
Allocation alloc = new Allocation(id, rs, t, usage);
Allocation alloc = new Allocation(id, rs, t, usage, mips);
alloc.setBitmap(b);
return alloc;
}
@@ -2047,7 +2067,7 @@ public class Allocation extends BaseObj {
if (id == 0) {
throw new RSRuntimeException("Load failed.");
}
return new Allocation(id, rs, t, usage);
return new Allocation(id, rs, t, usage, mips);
} finally {
Trace.traceEnd(RenderScript.TRACE_TAG);
}
@@ -2088,6 +2108,108 @@ public class Allocation extends BaseObj {
return mByteBuffer;
}
/**
* @hide
* Creates a new Allocation Array with the given {@link
* android.renderscript.Type}, and usage flags.
* Note: If the input allocation is of usage: USAGE_IO_INPUT,
* the created Allocation will be sharing the same BufferQueue.
*
* @param rs RenderScript context
* @param t RenderScript type describing data layout
* @param usage bit field specifying how the Allocation is
* utilized
* @param numAlloc Number of Allocations in the array.
* @return Allocation[]
*/
public static Allocation[] createAllocations(RenderScript rs, Type t, int usage, int numAlloc) {
try {
Trace.traceBegin(RenderScript.TRACE_TAG, "createAllocations");
rs.validate();
if (t.getID(rs) == 0) {
throw new RSInvalidStateException("Bad Type");
}
Allocation[] mAllocationArray = new Allocation[numAlloc];
mAllocationArray[0] = createTyped(rs, t, usage);
if ((usage & USAGE_IO_INPUT) != 0) {
if (numAlloc > MAX_NUMBER_IO_INPUT_ALLOC) {
throw new RSIllegalArgumentException("Exceeds the max number of Allocations allowed: " +
MAX_NUMBER_IO_INPUT_ALLOC);
}
mAllocationArray[0].setupBufferQueue(numAlloc);;
}
for (int i=1; i<numAlloc; i++) {
mAllocationArray[i] = createFromAllcation(rs, mAllocationArray[0]);
}
return mAllocationArray;
} finally {
Trace.traceEnd(RenderScript.TRACE_TAG);
}
}
/**
* Creates a new Allocation with the given {@link
* android.renderscript.Allocation}. The same data layout of
* the input Allocation will be applied.
* If the input allocation is of usage: USAGE_IO_INPUT, the created
* Allocation will be sharing the same BufferQueue.
*
* @param rs Context to which the allocation will belong.
* @param alloc RenderScript Allocation describing data layout.
* @return Allocation sharing the same data structure.
*/
static Allocation createFromAllcation(RenderScript rs, Allocation alloc) {
try {
Trace.traceBegin(RenderScript.TRACE_TAG, "createFromAllcation");
rs.validate();
if (alloc.getID(rs) == 0) {
throw new RSInvalidStateException("Bad input Allocation");
}
Type type = alloc.getType();
int usage = alloc.getUsage();
MipmapControl mips = alloc.getMipmap();
long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
if (id == 0) {
throw new RSRuntimeException("Allocation creation failed.");
}
Allocation outAlloc = new Allocation(id, rs, type, usage, mips);
if ((usage & USAGE_IO_INPUT) != 0) {
outAlloc.shareBufferQueue(alloc);
}
return outAlloc;
} finally {
Trace.traceEnd(RenderScript.TRACE_TAG);
}
}
/**
* Initialize BufferQueue with specified max number of buffers.
*/
void setupBufferQueue(int numAlloc) {
mRS.validate();
if ((mUsage & USAGE_IO_INPUT) == 0) {
throw new RSInvalidStateException("Allocation is not USAGE_IO_INPUT.");
}
mRS.nAllocationSetupBufferQueue(getID(mRS), numAlloc);
}
/**
* Share the BufferQueue with another {@link #USAGE_IO_INPUT} Allocation.
*
* @param alloc Allocation to associate with allocation
*/
void shareBufferQueue(Allocation alloc) {
mRS.validate();
if ((mUsage & USAGE_IO_INPUT) == 0) {
throw new RSInvalidStateException("Allocation is not USAGE_IO_INPUT.");
}
mGetSurfaceSurface = alloc.getSurface();
mRS.nAllocationShareBufferQueue(getID(mRS), alloc.getID(mRS));
}
/**
* @hide
* Gets the stride of the Allocation.
@@ -2106,6 +2228,27 @@ public class Allocation extends BaseObj {
return mByteBufferStride;
}
/**
* @hide
* Get the timestamp for the most recent buffer held by this Allocation.
* The timestamp is guaranteed to be unique and monotonically increasing.
* Default value: -1. The timestamp will be updated after each {@link
* #ioReceive ioReceive()} call.
*
* It can be used to identify the images by comparing the unique timestamps
* when used with {@link android.hardware.camera2} APIs.
* Example steps:
* 1. Save {@link android.hardware.camera2.TotalCaptureResult} when the
* capture is completed.
* 2. Get the timestamp after {@link #ioReceive ioReceive()} call.
* 3. Comparing totalCaptureResult.get(CaptureResult.SENSOR_TIMESTAMP) with
* alloc.getTimeStamp().
* @return long Timestamp associated with the buffer held by the Allocation.
*/
public long getTimeStamp() {
return mTimeStamp;
}
/**
* Returns the handle to a raw buffer that is being managed by the screen
* compositor. This operation is only valid for Allocations with {@link
@@ -2210,7 +2353,7 @@ public class Allocation extends BaseObj {
if(id == 0) {
throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
}
return new Allocation(id, rs, t, usage);
return new Allocation(id, rs, t, usage, mips);
}
/**

View File

@@ -484,7 +484,6 @@ public class RenderScript {
rsnAllocationCopyToBitmap(mContext, alloc, bmp);
}
native void rsnAllocationSyncAll(long con, long alloc, int src);
synchronized void nAllocationSyncAll(long alloc, int src) {
validate();
@@ -497,6 +496,16 @@ public class RenderScript {
return rsnAllocationGetByteBuffer(mContext, alloc, stride, xBytesSize, dimY, dimZ);
}
native void rsnAllocationSetupBufferQueue(long con, long alloc, int numAlloc);
synchronized void nAllocationSetupBufferQueue(long alloc, int numAlloc) {
validate();
rsnAllocationSetupBufferQueue(mContext, alloc, numAlloc);
}
native void rsnAllocationShareBufferQueue(long con, long alloc1, long alloc2);
synchronized void nAllocationShareBufferQueue(long alloc1, long alloc2) {
validate();
rsnAllocationShareBufferQueue(mContext, alloc1, alloc2);
}
native Surface rsnAllocationGetSurface(long con, long alloc);
synchronized Surface nAllocationGetSurface(long alloc) {
validate();
@@ -512,13 +521,12 @@ public class RenderScript {
validate();
rsnAllocationIoSend(mContext, alloc);
}
native void rsnAllocationIoReceive(long con, long alloc);
synchronized void nAllocationIoReceive(long alloc) {
native long rsnAllocationIoReceive(long con, long alloc);
synchronized long nAllocationIoReceive(long alloc) {
validate();
rsnAllocationIoReceive(mContext, alloc);
return rsnAllocationIoReceive(mContext, alloc);
}
native void rsnAllocationGenerateMipmaps(long con, long alloc);
synchronized void nAllocationGenerateMipmaps(long alloc) {
validate();

View File

@@ -1223,6 +1223,27 @@ nAllocationSyncAll(JNIEnv *_env, jobject _this, jlong con, jlong a, jint bits)
rsAllocationSyncAll((RsContext)con, (RsAllocation)a, (RsAllocationUsageType)bits);
}
static void
nAllocationSetupBufferQueue(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint numAlloc)
{
if (kLogApi) {
ALOGD("nAllocationSetupBufferQueue, con(%p), alloc(%p), numAlloc(%d)", (RsContext)con,
(RsAllocation)alloc, numAlloc);
}
rsAllocationSetupBufferQueue((RsContext)con, (RsAllocation)alloc, (uint32_t)numAlloc);
}
static void
nAllocationShareBufferQueue(JNIEnv *_env, jobject _this, jlong con, jlong alloc1, jlong alloc2)
{
if (kLogApi) {
ALOGD("nAllocationShareBufferQueue, con(%p), alloc1(%p), alloc2(%p)", (RsContext)con,
(RsAllocation)alloc1, (RsAllocation)alloc2);
}
rsAllocationShareBufferQueue((RsContext)con, (RsAllocation)alloc1, (RsAllocation)alloc2);
}
static jobject
nAllocationGetSurface(JNIEnv *_env, jobject _this, jlong con, jlong a)
{
@@ -1265,16 +1286,15 @@ nAllocationIoSend(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
rsAllocationIoSend((RsContext)con, (RsAllocation)alloc);
}
static void
static jlong
nAllocationIoReceive(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
{
if (kLogApi) {
ALOGD("nAllocationIoReceive, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
}
rsAllocationIoReceive((RsContext)con, (RsAllocation)alloc);
return (jlong) rsAllocationIoReceive((RsContext)con, (RsAllocation)alloc);
}
static void
nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
{
@@ -2856,10 +2876,12 @@ static const JNINativeMethod methods[] = {
{"rsnAllocationCopyToBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyToBitmap },
{"rsnAllocationSyncAll", "(JJI)V", (void*)nAllocationSyncAll },
{"rsnAllocationSetupBufferQueue", "(JJI)V", (void*)nAllocationSetupBufferQueue },
{"rsnAllocationShareBufferQueue", "(JJJ)V", (void*)nAllocationShareBufferQueue },
{"rsnAllocationGetSurface", "(JJ)Landroid/view/Surface;", (void*)nAllocationGetSurface },
{"rsnAllocationSetSurface", "(JJLandroid/view/Surface;)V", (void*)nAllocationSetSurface },
{"rsnAllocationIoSend", "(JJ)V", (void*)nAllocationIoSend },
{"rsnAllocationIoReceive", "(JJ)V", (void*)nAllocationIoReceive },
{"rsnAllocationIoReceive", "(JJ)J", (void*)nAllocationIoReceive },
{"rsnAllocationData1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData1D },
{"rsnAllocationElementData", "(JJIIIII[BI)V", (void*)nAllocationElementData },
{"rsnAllocationData2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData2D },