Merge "Issue proper parameters to root() calls for RS."
diff --git a/libs/rs/driver/rsdBcc.cpp b/libs/rs/driver/rsdBcc.cpp
index 176dd18..44ea79c 100644
--- a/libs/rs/driver/rsdBcc.cpp
+++ b/libs/rs/driver/rsdBcc.cpp
@@ -45,6 +45,7 @@
     InvokeFunc_t *mInvokeFunctions;
     void ** mFieldAddress;
     bool * mFieldIsObject;
+    const uint32_t *mExportForEachSignatureList;
 
     const uint8_t * mScriptText;
     uint32_t mScriptTextLength;
@@ -74,6 +75,7 @@
     size_t exportFuncCount = 0;
     size_t exportVarCount = 0;
     size_t objectSlotCount = 0;
+    size_t exportForEachSignatureCount = 0;
 
     DrvScript *drv = (DrvScript *)calloc(1, sizeof(DrvScript));
     if (drv == NULL) {
@@ -153,6 +155,10 @@
         }
     }
 
+    exportForEachSignatureCount = drv->ME->getExportForEachSignatureCount();
+    rsAssert(exportForEachSignatureCount <= 1);
+    drv->mExportForEachSignatureList = drv->ME->getExportForEachSignatureList();
+
     // Copy info over to runtime
     script->mHal.info.exportedFunctionCount = drv->ME->getExportFuncCount();
     script->mHal.info.exportedVariableCount = drv->ME->getExportVarCount();
@@ -179,6 +185,7 @@
 typedef struct {
     Context *rsc;
     Script *script;
+    uint32_t sig;
     const Allocation * ain;
     Allocation * aout;
     const void * usr;
@@ -206,7 +213,7 @@
     uint32_t dimZ;
     uint32_t dimArray;
 } MTLaunchStruct;
-typedef int (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
+typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
 
 static void wc_xy(void *usr, uint32_t idx) {
     MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
@@ -214,6 +221,8 @@
     memset(&p, 0, sizeof(p));
     p.usr = mtls->usr;
     p.usr_len = mtls->usrLen;
+    RsdHal * dc = (RsdHal *)mtls->rsc->mHal.drv;
+    uint32_t sig = mtls->sig;
 
     while (1) {
         uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
@@ -234,7 +243,7 @@
             for (p.x = mtls->xStart; p.x < mtls->xEnd; p.x++) {
                 p.in = xPtrIn;
                 p.out = xPtrOut;
-                ((rs_t)mtls->script->mHal.info.root) (p.in, p.out, p.usr, p.x, p.y, 0, 0);
+                dc->mForEachLaunch[sig](&mtls->script->mHal.info.root, &p);
                 xPtrIn += mtls->eStrideIn;
                 xPtrOut += mtls->eStrideOut;
             }
@@ -248,6 +257,8 @@
     memset(&p, 0, sizeof(p));
     p.usr = mtls->usr;
     p.usr_len = mtls->usrLen;
+    RsdHal * dc = (RsdHal *)mtls->rsc->mHal.drv;
+    uint32_t sig = mtls->sig;
 
     while (1) {
         uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
@@ -265,7 +276,7 @@
         for (p.x = xStart; p.x < xEnd; p.x++) {
             p.in = xPtrIn;
             p.out = xPtrOut;
-            ((rs_t)mtls->script->mHal.info.root) (p.in, p.out, p.usr, p.x, 0, 0, 0);
+            dc->mForEachLaunch[sig](&mtls->script->mHal.info.root, &p);
             xPtrIn += mtls->eStrideIn;
             xPtrOut += mtls->eStrideOut;
         }
@@ -286,6 +297,10 @@
     MTLaunchStruct mtls;
     memset(&mtls, 0, sizeof(mtls));
 
+    DrvScript *drv = (DrvScript *)s->mHal.drv;
+    // We only support slot 0 (root) at this point in time.
+    rsAssert(slot == 0);
+    mtls.sig = drv->mExportForEachSignatureList[slot];
     if (ain) {
         mtls.dimX = ain->getType()->getDimX();
         mtls.dimY = ain->getType()->getDimY();
@@ -369,6 +384,7 @@
         memset(&p, 0, sizeof(p));
         p.usr = mtls.usr;
         p.usr_len = mtls.usrLen;
+        uint32_t sig = mtls.sig;
 
         //LOGE("launch 3");
         for (p.ar[0] = mtls.arrayStart; p.ar[0] < mtls.arrayEnd; p.ar[0]++) {
@@ -383,7 +399,7 @@
                     for (p.x = mtls.xStart; p.x < mtls.xEnd; p.x++) {
                         p.in = xPtrIn;
                         p.out = xPtrOut;
-                        ((rs_t)s->mHal.info.root) (p.in, p.out, p.usr, p.x, p.y, p.z, p.ar[0]);
+                        dc->mForEachLaunch[sig](&s->mHal.info.root, &p);
                         xPtrIn += mtls.eStrideIn;
                         xPtrOut += mtls.eStrideOut;
                     }
diff --git a/libs/rs/driver/rsdCore.cpp b/libs/rs/driver/rsdCore.cpp
index 38f6895..171d045 100644
--- a/libs/rs/driver/rsdCore.cpp
+++ b/libs/rs/driver/rsdCore.cpp
@@ -43,6 +43,7 @@
 
 static void Shutdown(Context *rsc);
 static void SetPriority(const Context *rsc, int32_t priority);
+static void initForEach(outer_foreach_t* forEachLaunch);
 
 static RsdHalFunctions FunctionTable = {
     rsdGLInit,
@@ -206,6 +207,8 @@
     rsdgThreadTLSKeyCount++;
     pthread_mutex_unlock(&rsdgInitMutex);
 
+    initForEach(dc->mForEachLaunch);
+
     dc->mTlsStruct.mContext = rsc;
     dc->mTlsStruct.mScript = NULL;
     int status = pthread_setspecific(rsdgThreadTLSKey, &dc->mTlsStruct);
@@ -287,4 +290,112 @@
 
 }
 
+static void rsdForEach17(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(const void *, uint32_t);
+    (*(fe*)vRoot)(p->in, p->y);
+}
+
+static void rsdForEach18(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(void *, uint32_t);
+    (*(fe*)vRoot)(p->out, p->y);
+}
+
+static void rsdForEach19(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(const void *, void *, uint32_t);
+    (*(fe*)vRoot)(p->in, p->out, p->y);
+}
+
+static void rsdForEach21(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(const void *, const void *, uint32_t);
+    (*(fe*)vRoot)(p->in, p->usr, p->y);
+}
+
+static void rsdForEach22(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(void *, const void *, uint32_t);
+    (*(fe*)vRoot)(p->out, p->usr, p->y);
+}
+
+static void rsdForEach23(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(const void *, void *, const void *, uint32_t);
+    (*(fe*)vRoot)(p->in, p->out, p->usr, p->y);
+}
+
+static void rsdForEach25(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(const void *, uint32_t, uint32_t);
+    (*(fe*)vRoot)(p->in, p->x, p->y);
+}
+
+static void rsdForEach26(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(void *, uint32_t, uint32_t);
+    (*(fe*)vRoot)(p->out, p->x, p->y);
+}
+
+static void rsdForEach27(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(const void *, void *, uint32_t, uint32_t);
+    (*(fe*)vRoot)(p->in, p->out, p->x, p->y);
+}
+
+static void rsdForEach29(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(const void *, const void *, uint32_t, uint32_t);
+    (*(fe*)vRoot)(p->in, p->usr, p->x, p->y);
+}
+
+static void rsdForEach30(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(void *, const void *, uint32_t, uint32_t);
+    (*(fe*)vRoot)(p->out, p->usr, p->x, p->y);
+}
+
+static void rsdForEach31(const void *vRoot,
+        const android::renderscript::RsForEachStubParamStruct *p) {
+    typedef void (*fe)(const void *, void *, const void *, uint32_t, uint32_t);
+    (*(fe*)vRoot)(p->in, p->out, p->usr, p->x, p->y);
+}
+
+
+static void initForEach(outer_foreach_t* forEachLaunch) {
+    rsAssert(forEachLaunch);
+    forEachLaunch[0x00] = NULL;
+    forEachLaunch[0x01] = rsdForEach31; // in
+    forEachLaunch[0x02] = rsdForEach30; //     out
+    forEachLaunch[0x03] = rsdForEach31; // in, out
+    forEachLaunch[0x04] = NULL;
+    forEachLaunch[0x05] = rsdForEach29;  // in,      usr
+    forEachLaunch[0x06] = rsdForEach30; //     out, usr
+    forEachLaunch[0x07] = rsdForEach31; // in, out, usr
+    forEachLaunch[0x08] = NULL;
+    forEachLaunch[0x09] = rsdForEach25; // in,           x
+    forEachLaunch[0x0a] = rsdForEach26; //     out,      x
+    forEachLaunch[0x0b] = rsdForEach27; // in, out,      x
+    forEachLaunch[0x0c] = NULL;
+    forEachLaunch[0x0d] = rsdForEach29; // in,      usr, x
+    forEachLaunch[0x0e] = rsdForEach30; //     out, usr, x
+    forEachLaunch[0x0f] = rsdForEach31; // in, out, usr, x
+    forEachLaunch[0x10] = NULL;
+    forEachLaunch[0x11] = rsdForEach17; // in               y
+    forEachLaunch[0x12] = rsdForEach18; //     out,         y
+    forEachLaunch[0x13] = rsdForEach19; // in, out,         y
+    forEachLaunch[0x14] = NULL;
+    forEachLaunch[0x15] = rsdForEach21; // in,      usr,    y
+    forEachLaunch[0x16] = rsdForEach22; //     out, usr,    y
+    forEachLaunch[0x17] = rsdForEach23; // in, out, usr,    y
+    forEachLaunch[0x18] = NULL;
+    forEachLaunch[0x19] = rsdForEach25; // in,           x, y
+    forEachLaunch[0x1a] = rsdForEach26; //     out,      x, y
+    forEachLaunch[0x1b] = rsdForEach27; // in, out,      x, y
+    forEachLaunch[0x1c] = NULL;
+    forEachLaunch[0x1d] = rsdForEach29; // in,      usr, x, y
+    forEachLaunch[0x1e] = rsdForEach30; //     out, usr, x, y
+    forEachLaunch[0x1f] = rsdForEach31; // in, out, usr, x, y
+}
 
diff --git a/libs/rs/driver/rsdCore.h b/libs/rs/driver/rsdCore.h
index f393b60..159b72a 100644
--- a/libs/rs/driver/rsdCore.h
+++ b/libs/rs/driver/rsdCore.h
@@ -27,6 +27,9 @@
 typedef void (* InvokeFunc_t)(void);
 typedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
 
+typedef void (*outer_foreach_t)(const void *,
+    const android::renderscript::RsForEachStubParamStruct *);
+
 typedef struct RsdSymbolTableRec {
     const char * mName;
     void * mPtr;
@@ -57,6 +60,8 @@
     Workers mWorkers;
     bool mExit;
 
+    outer_foreach_t mForEachLaunch[32];
+
     ScriptTLSStruct mTlsStruct;
 
     RsdGL gl;
diff --git a/tests/RenderScriptTests/tests/src/com/android/rs/test/RSTestCore.java b/tests/RenderScriptTests/tests/src/com/android/rs/test/RSTestCore.java
index be012ee..e37e9b5 100644
--- a/tests/RenderScriptTests/tests/src/com/android/rs/test/RSTestCore.java
+++ b/tests/RenderScriptTests/tests/src/com/android/rs/test/RSTestCore.java
@@ -70,6 +70,7 @@
         unitTests.add(new UT_rstime(this, mRes, mCtx));
         unitTests.add(new UT_rstypes(this, mRes, mCtx));
         unitTests.add(new UT_alloc(this, mRes, mCtx));
+        unitTests.add(new UT_foreach(this, mRes, mCtx));
         unitTests.add(new UT_math(this, mRes, mCtx));
         unitTests.add(new UT_fp_mad(this, mRes, mCtx));
         /*
diff --git a/tests/RenderScriptTests/tests/src/com/android/rs/test/UT_foreach.java b/tests/RenderScriptTests/tests/src/com/android/rs/test/UT_foreach.java
new file mode 100644
index 0000000..1d2555e
--- /dev/null
+++ b/tests/RenderScriptTests/tests/src/com/android/rs/test/UT_foreach.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.rs.test;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.renderscript.*;
+
+public class UT_foreach extends UnitTest {
+    private Resources mRes;
+    private Allocation A;
+
+    protected UT_foreach(RSTestCore rstc, Resources res, Context ctx) {
+        super(rstc, "ForEach", ctx);
+        mRes = res;
+    }
+
+    private void initializeGlobals(RenderScript RS, ScriptC_foreach s) {
+        Type.Builder typeBuilder = new Type.Builder(RS, Element.I32(RS));
+        int X = 5;
+        int Y = 7;
+        s.set_dimX(X);
+        s.set_dimY(Y);
+        typeBuilder.setX(X).setY(Y);
+        A = Allocation.createTyped(RS, typeBuilder.create());
+        s.bind_a(A);
+
+        return;
+    }
+
+    public void run() {
+        RenderScript pRS = RenderScript.create(mCtx);
+        ScriptC_foreach s = new ScriptC_foreach(pRS, mRes, R.raw.foreach);
+        pRS.setMessageHandler(mRsMessage);
+        initializeGlobals(pRS, s);
+        s.forEach_root(A);
+        s.invoke_foreach_test();
+        pRS.finish();
+        waitForMessage();
+        pRS.destroy();
+    }
+}
diff --git a/tests/RenderScriptTests/tests/src/com/android/rs/test/foreach.rs b/tests/RenderScriptTests/tests/src/com/android/rs/test/foreach.rs
new file mode 100644
index 0000000..3ba3eef
--- /dev/null
+++ b/tests/RenderScriptTests/tests/src/com/android/rs/test/foreach.rs
@@ -0,0 +1,42 @@
+#include "shared.rsh"
+
+int *a;
+int dimX;
+int dimY;
+
+void root(int *out, uint32_t x, uint32_t y) {
+    *out = x + y * dimX;
+}
+
+static bool test_foreach_output() {
+    bool failed = false;
+    int i, j;
+
+    for (j = 0; j < dimY; j++) {
+        for (i = 0; i < dimX; i++) {
+            _RS_ASSERT(a[i + j * dimX] == (i + j * dimX));
+        }
+    }
+
+    if (failed) {
+        rsDebug("test_foreach_output FAILED", 0);
+    }
+    else {
+        rsDebug("test_foreach_output PASSED", 0);
+    }
+
+    return failed;
+}
+
+void foreach_test() {
+    bool failed = false;
+    failed |= test_foreach_output();
+
+    if (failed) {
+        rsSendToClientBlocking(RS_MSG_TEST_FAILED);
+    }
+    else {
+        rsSendToClientBlocking(RS_MSG_TEST_PASSED);
+    }
+}
+