Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 26ef7a73 authored by David Gross's avatar David Gross
Browse files

Add general reduction plumbing starting with Java Script::reduce().

Requires coordinated change in frameworks/rs.

Bug: 23535724
Change-Id: I2fee6750cf542948d8fa87a98441002c4d84f36e
parent 2786002b
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -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();
+41 −1
Original line number Diff line number Diff line
@@ -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) {
+96 −1
Original line number Diff line number Diff line
@@ -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;
        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 },