diff options
author | David Gross <dgross@google.com> | 2016-01-12 12:19:15 -0800 |
---|---|---|
committer | David Gross <dgross@google.com> | 2016-01-15 12:20:08 -0800 |
commit | 26ef7a739b724452f93f850a1c268c42e439317b (patch) | |
tree | b07f22213b8eb7b2b98448ef971cb474a902beb1 /rs | |
parent | 2786002bd51b229d9a4672e3c43f835796de06ea (diff) |
Add general reduction plumbing starting with Java Script::reduce().
Requires coordinated change in frameworks/rs.
Bug: 23535724
Change-Id: I2fee6750cf542948d8fa87a98441002c4d84f36e
Diffstat (limited to 'rs')
-rw-r--r-- | rs/java/android/renderscript/RenderScript.java | 8 | ||||
-rw-r--r-- | rs/java/android/renderscript/Script.java | 42 | ||||
-rw-r--r-- | rs/jni/android_renderscript_RenderScript.cpp | 97 |
3 files changed, 145 insertions, 2 deletions
diff --git a/rs/java/android/renderscript/RenderScript.java b/rs/java/android/renderscript/RenderScript.java index 7eb80055c4d5..3668ddd62303 100644 --- a/rs/java/android/renderscript/RenderScript.java +++ b/rs/java/android/renderscript/RenderScript.java @@ -751,6 +751,14 @@ public class RenderScript { rsnScriptReduce(mContext, id, slot, ain, aout, limits); } + native void rsnScriptReduceNew(long con, long id, int slot, long[] ains, + long aout, int[] limits); + synchronized void nScriptReduceNew(long id, int slot, long ains[], long aout, + int[] limits) { + validate(); + rsnScriptReduceNew(mContext, id, slot, ains, aout, limits); + } + native void rsnScriptInvokeV(long con, long id, int slot, byte[] params); synchronized void nScriptInvokeV(long id, int slot, byte[] params) { validate(); diff --git a/rs/java/android/renderscript/Script.java b/rs/java/android/renderscript/Script.java index ed4c6c7be8d4..84f980dc24cd 100644 --- a/rs/java/android/renderscript/Script.java +++ b/rs/java/android/renderscript/Script.java @@ -284,7 +284,7 @@ public class Script extends BaseObj { } /** - * Only intended for use by generated reflected code. + * Only intended for use by generated reflected code. (Simple reduction) * * @hide */ @@ -312,6 +312,46 @@ public class Script extends BaseObj { mRS.nScriptReduce(getID(mRS), slot, in_id, out_id, limits); } + /** + * Only intended for use by generated reflected code. (General reduction) + * + * @hide + */ + protected void reduce(int slot, Allocation[] ains, Allocation aout, LaunchOptions sc) { + mRS.validate(); + if (ains == null || ains.length < 1) { + throw new RSIllegalArgumentException( + "At least one input is required."); + } + if (aout == null) { + throw new RSIllegalArgumentException( + "aout is required to be non-null."); + } + for (Allocation ain : ains) { + mRS.validateObject(ain); + } + + long[] in_ids = new long[ains.length]; + for (int index = 0; index < ains.length; ++index) { + in_ids[index] = ains[index].getID(mRS); + } + long out_id = aout.getID(mRS); + + int[] limits = null; + if (sc != null) { + limits = new int[6]; + + limits[0] = sc.xstart; + limits[1] = sc.xend; + limits[2] = sc.ystart; + limits[3] = sc.yend; + limits[4] = sc.zstart; + limits[5] = sc.zend; + } + + mRS.nScriptReduceNew(getID(mRS), slot, in_ids, out_id, limits); + } + long[] mInIdsBuffer; Script(long id, RenderScript rs) { diff --git a/rs/jni/android_renderscript_RenderScript.cpp b/rs/jni/android_renderscript_RenderScript.cpp index 113241da4572..39540709b16e 100644 --- a/rs/jni/android_renderscript_RenderScript.cpp +++ b/rs/jni/android_renderscript_RenderScript.cpp @@ -1988,7 +1988,6 @@ nScriptForEach(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, if (sizeof(RsAllocation) == sizeof(jlong)) { in_allocs = (RsAllocation*)in_ptr; - } else { // Convert from 64-bit jlong types to the native pointer type. @@ -2127,6 +2126,101 @@ nScriptReduce(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, } } +static void +nScriptReduceNew(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, + jlongArray ains, jlong aout, jintArray limits) +{ + if (kLogApi) { + ALOGD("nScriptReduceNew, con(%p), s(%p), slot(%i) ains(%p) aout(%" PRId64 ")", (RsContext)con, (void *)script, slot, ains, aout); + } + + if (ains == nullptr) { + ALOGE("At least one input required."); + // TODO (b/20758983): Report back to Java and throw an exception + return; + } + jint in_len = _env->GetArrayLength(ains); + if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) { + ALOGE("Too many arguments in kernel launch."); + // TODO (b/20758983): Report back to Java and throw an exception + return; + } + + jlong *in_ptr = _env->GetLongArrayElements(ains, nullptr); + if (in_ptr == nullptr) { + ALOGE("Failed to get Java array elements"); + // TODO (b/20758983): Report back to Java and throw an exception + return; + } + + RsAllocation *in_allocs = nullptr; + if (sizeof(RsAllocation) == sizeof(jlong)) { + in_allocs = (RsAllocation*)in_ptr; + } else { + // Convert from 64-bit jlong types to the native pointer type. + + in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation)); + if (in_allocs == nullptr) { + ALOGE("Failed launching kernel for lack of memory."); + // TODO (b/20758983): Report back to Java and throw an exception + _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT); + return; + } + + for (int index = in_len; --index >= 0;) { + in_allocs[index] = (RsAllocation)in_ptr[index]; + } + } + + RsScriptCall sc, *sca = nullptr; + uint32_t sc_size = 0; + + jint limit_len = 0; + jint *limit_ptr = nullptr; + + if (limits != nullptr) { + limit_len = _env->GetArrayLength(limits); + limit_ptr = _env->GetIntArrayElements(limits, nullptr); + if (limit_ptr == nullptr) { + ALOGE("Failed to get Java array elements"); + // TODO (b/20758983): Report back to Java and throw an exception + return; + } + + assert(limit_len == 6); + UNUSED(limit_len); // As the assert might not be compiled. + + sc.xStart = limit_ptr[0]; + sc.xEnd = limit_ptr[1]; + sc.yStart = limit_ptr[2]; + sc.yEnd = limit_ptr[3]; + sc.zStart = limit_ptr[4]; + sc.zEnd = limit_ptr[5]; + sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE; + sc.arrayStart = 0; + sc.arrayEnd = 0; + sc.array2Start = 0; + sc.array2End = 0; + sc.array3Start = 0; + sc.array3End = 0; + sc.array4Start = 0; + sc.array4End = 0; + + sca = ≻ + sc_size = sizeof(sc); + } + + rsScriptReduceNew((RsContext)con, (RsScript)script, slot, + in_allocs, in_len, (RsAllocation)aout, + sca, sc_size); + + _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT); + + if (limits != nullptr) { + _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT); + } +} + // ----------------------------------- static jlong @@ -2755,6 +2849,7 @@ static const JNINativeMethod methods[] = { {"rsnScriptForEach", "(JJI[JJ[B[I)V", (void*)nScriptForEach }, {"rsnScriptReduce", "(JJIJJ[I)V", (void*)nScriptReduce }, +{"rsnScriptReduceNew", "(JJI[JJ[I)V", (void*)nScriptReduceNew }, {"rsnScriptSetVarI", "(JJII)V", (void*)nScriptSetVarI }, {"rsnScriptGetVarI", "(JJI)I", (void*)nScriptGetVarI }, |