Cleanup sendToClient and add rsGetDt().
Remove legacy implemtation of matrix calls which were moved to llvm bc.

Change-Id: I527740590067db3bcb2147233ef41fb057f1d2a8
diff --git a/java/Fountain/src/com/android/fountain/fountain.rs b/java/Fountain/src/com/android/fountain/fountain.rs
index 34bea2f..f87ef59 100644
--- a/java/Fountain/src/com/android/fountain/fountain.rs
+++ b/java/Fountain/src/com/android/fountain/fountain.rs
@@ -19,13 +19,14 @@
 #pragma rs export_func(addParticles)
 
 int root() {
+    float dt = min(rsGetDt(), 0.1f);
     rsgClearColor(0.f, 0.f, 0.f, 1.f);
     const float height = rsgGetHeight();
     const int size = rsAllocationGetDimX(rsGetAllocation(point));
-
+    float dy2 = dt * (10.f);
     Point_t * p = point;
     for (int ct=0; ct < size; ct++) {
-        p->delta.y += 0.15f;
+        p->delta.y += dy2;
         p->position += p->delta;
         if ((p->position.y > height) && (p->delta.y > 0)) {
             p->delta.y *= -0.3f;
diff --git a/java/ImageProcessing/src/com/android/rs/image/threshold.rs b/java/ImageProcessing/src/com/android/rs/image/threshold.rs
index 5dcd372..8e198c7 100644
--- a/java/ImageProcessing/src/com/android/rs/image/threshold.rs
+++ b/java/ImageProcessing/src/com/android/rs/image/threshold.rs
@@ -17,6 +17,7 @@
 rs_script hBlurScript;
 rs_script levelsScript;
 
+const int CMD_FINISHED = 1;
 
 // Store our coefficients here
 static float gaussian[MAX_RADIUS * 2 + 1];
@@ -89,14 +90,11 @@
         rsForEach(levelsScript, rsGetAllocation(InPixel), rsGetAllocation(OutPixel), 0);
     }
 
-    int count = 0;
-    rsSendToClient(&count, 1, 4, 0);
+    rsSendToClientBlocking(CMD_FINISHED);
 }
 
 void filterBenchmark() {
     blur();
-
-    int count = 0;
-    rsSendToClient(&count, 1, 4, 0);
+    rsSendToClientBlocking(CMD_FINISHED);
 }
 
diff --git a/rsContext.cpp b/rsContext.cpp
index d6df581..d1784f3 100644
--- a/rsContext.cpp
+++ b/rsContext.cpp
@@ -780,9 +780,13 @@
         }
     }
     //LOGE("sendMessageToClient 2");
-    void *p = mIO.mToClient.reserve(len);
-    memcpy(p, data, len);
-    mIO.mToClient.commit(cmdID, len);
+    if (len > 0) {
+        void *p = mIO.mToClient.reserve(len);
+        memcpy(p, data, len);
+        mIO.mToClient.commit(cmdID, len);
+    } else {
+        mIO.mToClient.commit(cmdID, 0);
+    }
     //LOGE("sendMessageToClient 3");
     return true;
 }
diff --git a/rsScript.h b/rsScript.h
index 0717059..455ece7 100644
--- a/rsScript.h
+++ b/rsScript.h
@@ -40,7 +40,8 @@
     virtual ~Script();
 
     struct Enviroment_t {
-        uint32_t mStartTimeMillis;
+        int64_t mStartTimeMillis;
+        int64_t mLastDtTime;
         const char* mTimeZone;
 
         ObjectBaseRef<ProgramVertex> mVertex;
diff --git a/rsScriptC_Lib.cpp b/rsScriptC_Lib.cpp
index 9c29ca6..ccdde00 100644
--- a/rsScriptC_Lib.cpp
+++ b/rsScriptC_Lib.cpp
@@ -179,112 +179,30 @@
     return timeinfo->tm_year;
 }
 
-static int64_t SC_uptimeMillis2()
+static int64_t SC_uptimeMillis()
 {
     return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
 }
 
-static int64_t SC_startTimeMillis2()
+static int64_t SC_startTimeMillis()
 {
     GET_TLS();
     return sc->mEnviroment.mStartTimeMillis;
 }
 
-static int64_t SC_elapsedTimeMillis2()
+static int64_t SC_elapsedTimeMillis()
 {
     GET_TLS();
     return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC))
             - sc->mEnviroment.mStartTimeMillis;
 }
 
-static int32_t SC_uptimeMillis()
-{
-    return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
-}
-
-static int32_t SC_startTimeMillis()
+static float SC_getDt()
 {
     GET_TLS();
-    return sc->mEnviroment.mStartTimeMillis;
-}
-
-static int32_t SC_elapsedTimeMillis()
-{
-    GET_TLS();
-    return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC))
-            - sc->mEnviroment.mStartTimeMillis;
-}
-
-//////////////////////////////////////////////////////////////////////////////
-// Matrix routines
-//////////////////////////////////////////////////////////////////////////////
-
-
-static void SC_matrixLoadIdentity(rsc_Matrix *mat)
-{
-    Matrix *m = reinterpret_cast<Matrix *>(mat);
-    m->loadIdentity();
-}
-
-static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f)
-{
-    Matrix *m = reinterpret_cast<Matrix *>(mat);
-    m->load(f);
-}
-
-static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat)
-{
-    Matrix *m = reinterpret_cast<Matrix *>(mat);
-    m->load(reinterpret_cast<const Matrix *>(newmat));
-}
-
-static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
-{
-    Matrix *m = reinterpret_cast<Matrix *>(mat);
-    m->loadRotate(rot, x, y, z);
-}
-
-static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z)
-{
-    Matrix *m = reinterpret_cast<Matrix *>(mat);
-    m->loadScale(x, y, z);
-}
-
-static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z)
-{
-    Matrix *m = reinterpret_cast<Matrix *>(mat);
-    m->loadTranslate(x, y, z);
-}
-
-static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs)
-{
-    Matrix *m = reinterpret_cast<Matrix *>(mat);
-    m->loadMultiply(reinterpret_cast<const Matrix *>(lhs),
-                    reinterpret_cast<const Matrix *>(rhs));
-}
-
-static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs)
-{
-    Matrix *m = reinterpret_cast<Matrix *>(mat);
-    m->multiply(reinterpret_cast<const Matrix *>(rhs));
-}
-
-static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
-{
-    Matrix *m = reinterpret_cast<Matrix *>(mat);
-    m->rotate(rot, x, y, z);
-}
-
-static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z)
-{
-    Matrix *m = reinterpret_cast<Matrix *>(mat);
-    m->scale(x, y, z);
-}
-
-static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z)
-{
-    Matrix *m = reinterpret_cast<Matrix *>(mat);
-    m->translate(x, y, z);
+    int64_t l = sc->mEnviroment.mLastDtTime;
+    sc->mEnviroment.mLastDtTime = systemTime(SYSTEM_TIME_MONOTONIC);
+    return ((float)(sc->mEnviroment.mLastDtTime - l)) / 1.0e9;
 }
 
 
@@ -372,22 +290,40 @@
 static void SC_debugI32(const char *s, int32_t i) {
     LOGE("%s %i  0x%x", s, i, i);
 }
+static void SC_debugU32(const char *s, uint32_t i) {
+    LOGE("%s %i  0x%x", s, i, i);
+}
 
 static void SC_debugP(const char *s, const void *p) {
     LOGE("%s %p", s, p);
 }
 
-static uint32_t SC_toClient(void *data, int cmdID, int len, int waitForSpace)
+static uint32_t SC_toClient2(int cmdID, void *data, int len)
 {
     GET_TLS();
-    //LOGE("SC_toClient %i %i %i", cmdID, len, waitForSpace);
-    return rsc->sendMessageToClient(data, cmdID, len, waitForSpace != 0);
+    //LOGE("SC_toClient %i %i %i", cmdID, len);
+    return rsc->sendMessageToClient(data, cmdID, len, false);
 }
 
-static void SC_scriptCall(int scriptID)
+static uint32_t SC_toClient(int cmdID)
 {
     GET_TLS();
-    rsc->runScript((Script *)scriptID);
+    //LOGE("SC_toClient %i", cmdID);
+    return rsc->sendMessageToClient(NULL, cmdID, 0, false);
+}
+
+static uint32_t SC_toClientBlocking2(int cmdID, void *data, int len)
+{
+    GET_TLS();
+    //LOGE("SC_toClientBlocking %i %i", cmdID, len);
+    return rsc->sendMessageToClient(data, cmdID, len, true);
+}
+
+static uint32_t SC_toClientBlocking(int cmdID)
+{
+    GET_TLS();
+    //LOGE("SC_toClientBlocking %i", cmdID);
+    return rsc->sendMessageToClient(NULL, cmdID, 0, true);
 }
 
 int SC_divsi3(int a, int b)
@@ -453,12 +389,12 @@
     { "__divsi3", (void *)&SC_divsi3 },
 
     // allocation
-    { "rsAllocationGetDimX", (void *)&SC_allocGetDimX },
-    { "rsAllocationGetDimY", (void *)&SC_allocGetDimY },
-    { "rsAllocationGetDimZ", (void *)&SC_allocGetDimZ },
-    { "rsAllocationGetDimLOD", (void *)&SC_allocGetDimLOD },
-    { "rsAllocationGetDimFaces", (void *)&SC_allocGetDimFaces },
-    { "rsGetAllocation", (void *)&SC_getAllocation },
+    { "_Z19rsAllocationGetDimX13rs_allocation", (void *)&SC_allocGetDimX },
+    { "_Z19rsAllocationGetDimY13rs_allocation", (void *)&SC_allocGetDimY },
+    { "_Z19rsAllocationGetDimZ13rs_allocation", (void *)&SC_allocGetDimZ },
+    { "_Z21rsAllocationGetDimLOD13rs_allocation", (void *)&SC_allocGetDimLOD },
+    { "_Z23rsAllocationGetDimFaces13rs_allocation", (void *)&SC_allocGetDimFaces },
+    { "_Z15rsGetAllocationPKv", (void *)&SC_getAllocation },
 
     { "_Z14rsGetElementAt13rs_allocationj", (void *)&SC_getElementAtX },
     { "_Z14rsGetElementAt13rs_allocationjj", (void *)&SC_getElementAtXY },
@@ -471,6 +407,7 @@
     { "_Z7rsDebugPKcDv3_f", (void *)&SC_debugFv3 },
     { "_Z7rsDebugPKcDv4_f", (void *)&SC_debugFv4 },
     { "_Z7rsDebugPKci", (void *)&SC_debugI32 },
+    { "_Z7rsDebugPKcj", (void *)&SC_debugU32 },
     { "_Z7rsDebugPKcPKv", (void *)&SC_debugP },
     //extern void __attribute__((overloadable))rsDebug(const char *, const void *);
 
@@ -483,30 +420,32 @@
     { "_Z6rsFracf", (void *)&SC_frac },
 
     // time
+    { "_Z8rsSecond", (void *)&SC_second },
+    { "_Z8rsMinute", (void *)&SC_minute },
+    { "_Z6rsHour", (void *)&SC_hour },
+    { "_Z5rsDay", (void *)&SC_day },
+    { "_Z7rsMonth", (void *)&SC_month },
+    { "_Z6rsYear", (void *)&SC_year },
+    { "_Z14rsUptimeMillis", (void*)&SC_uptimeMillis },
+    { "_Z17rsStartTimeMillis", (void*)&SC_startTimeMillis },
+    { "_Z19rsElapsedTimeMillis", (void*)&SC_elapsedTimeMillis },
+    { "_Z7rsGetDt", (void*)&SC_getDt },
+
     { "rsSecond", (void *)&SC_second },
     { "rsMinute", (void *)&SC_minute },
     { "rsHour", (void *)&SC_hour },
     { "rsDay", (void *)&SC_day },
     { "rsMonth", (void *)&SC_month },
     { "rsYear", (void *)&SC_year },
-    { "rsUptimeMillis", (void*)&SC_uptimeMillis2 },
-    { "rsStartTimeMillis", (void*)&SC_startTimeMillis2 },
-    { "rsElapsedTimeMillis", (void*)&SC_elapsedTimeMillis2 },
+    { "rsUptimeMillis", (void*)&SC_uptimeMillis },
+    { "rsStartTimeMillis", (void*)&SC_startTimeMillis },
+    { "rsElapsedTimeMillis", (void*)&SC_elapsedTimeMillis },
+    { "rsGetDt", (void*)&SC_getDt },
 
-    { "rsSendToClient", (void *)&SC_toClient },
-
-    // matrix
-    { "rsMatrixLoadIdentity", (void *)&SC_matrixLoadIdentity },
-    { "rsMatrixLoadFloat", (void *)&SC_matrixLoadFloat },
-    { "rsMatrixLoadMat", (void *)&SC_matrixLoadMat },
-    { "rsMatrixLoadRotate", (void *)&SC_matrixLoadRotate },
-    { "rsMatrixLoadScale", (void *)&SC_matrixLoadScale },
-    { "rsMatrixLoadTranslate", (void *)&SC_matrixLoadTranslate },
-    { "rsMatrixLoadMultiply", (void *)&SC_matrixLoadMultiply },
-    { "rsMatrixMultiply", (void *)&SC_matrixMultiply },
-    { "rsMatrixRotate", (void *)&SC_matrixRotate },
-    { "rsMatrixScale", (void *)&SC_matrixScale },
-    { "rsMatrixTranslate", (void *)&SC_matrixTranslate },
+    { "_Z14rsSendToClienti", (void *)&SC_toClient },
+    { "_Z14rsSendToClientiPKvj", (void *)&SC_toClient2 },
+    { "_Z22rsSendToClientBlockingi", (void *)&SC_toClientBlocking },
+    { "_Z22rsSendToClientBlockingiPKvj", (void *)&SC_toClientBlocking2 },
 
     { "_Z9rsForEach9rs_script13rs_allocationS0_PKv", (void *)&SC_ForEach },
     //{ "_Z9rsForEach9rs_script13rs_allocationS0_PKv", (void *)&SC_ForEach2 },
@@ -516,9 +455,6 @@
     //{ "sinf_fast", (void *)&SC_sinf_fast },
     //{ "cosf_fast", (void *)&SC_cosf_fast },
 
-    { "scriptCall", (void *)&SC_scriptCall },
-
-
     { NULL, NULL }
 };
 
diff --git a/scriptc/rs_math.rsh b/scriptc/rs_math.rsh
index bd6e5a9..309a952 100644
--- a/scriptc/rs_math.rsh
+++ b/scriptc/rs_math.rsh
@@ -7,12 +7,18 @@
 
 
 // Allocations
-extern rs_allocation rsGetAllocation(const void *);
-extern uint32_t rsAllocationGetDimX(rs_allocation);
-extern uint32_t rsAllocationGetDimY(rs_allocation);
-extern uint32_t rsAllocationGetDimZ(rs_allocation);
-extern uint32_t rsAllocationGetDimLOD(rs_allocation);
-extern uint32_t rsAllocationGetDimFaces(rs_allocation);
+extern rs_allocation __attribute__((overloadable))
+    rsGetAllocation(const void *);
+extern uint32_t __attribute__((overloadable))
+    rsAllocationGetDimX(rs_allocation);
+extern uint32_t __attribute__((overloadable))
+    rsAllocationGetDimY(rs_allocation);
+extern uint32_t __attribute__((overloadable))
+    rsAllocationGetDimZ(rs_allocation);
+extern uint32_t __attribute__((overloadable))
+    rsAllocationGetDimLOD(rs_allocation);
+extern uint32_t __attribute__((overloadable))
+    rsAllocationGetDimFaces(rs_allocation);
 
 extern const void * __attribute__((overloadable))
     rsGetElementAt(rs_allocation, uint32_t x);
@@ -28,30 +34,35 @@
 extern void __attribute__((overloadable))rsDebug(const char *, float3);
 extern void __attribute__((overloadable))rsDebug(const char *, float4);
 extern void __attribute__((overloadable))rsDebug(const char *, int);
+extern void __attribute__((overloadable))rsDebug(const char *, uint);
 extern void __attribute__((overloadable))rsDebug(const char *, const void *);
 #define RS_DEBUG(a) rsDebug(#a, a)
 #define RS_DEBUG_MARKER rsDebug(__FILE__, __LINE__)
 
 // RS Math
-extern int __attribute__((overloadable)) rsRand(int);
-extern int __attribute__((overloadable)) rsRand(int, int);
-extern float __attribute__((overloadable)) rsRand(float);
-extern float __attribute__((overloadable)) rsRand(float, float);
+extern int __attribute__((overloadable))rsRand(int);
+extern int __attribute__((overloadable))rsRand(int, int);
+extern float __attribute__((overloadable))rsRand(float);
+extern float __attribute__((overloadable))rsRand(float, float);
 
 extern float __attribute__((overloadable)) rsFrac(float);
 
 // time
-extern int32_t rsSecond();
-extern int32_t rsMinute();
-extern int32_t rsHour();
-extern int32_t rsDay();
-extern int32_t rsMonth();
-extern int32_t rsYear();
-extern int64_t rsUptimeMillis();
-extern int64_t rsStartTimeMillis();
-extern int64_t rsElapsedTimeMillis();
+extern int32_t /*__attribute__((overloadable))*/rsSecond();
+extern int32_t /*__attribute__((overloadable))*/rsMinute();
+extern int32_t /*__attribute__((overloadable))*/rsHour();
+extern int32_t /*__attribute__((overloadable))*/rsDay();
+extern int32_t /*__attribute__((overloadable))*/rsMonth();
+extern int32_t /*__attribute__((overloadable))*/rsYear();
+extern int64_t /*__attribute__((overloadable))*/rsUptimeMillis();
+extern int64_t /*__attribute__((overloadable))*/rsStartTimeMillis();
+extern int64_t /*__attribute__((overloadable))*/rsElapsedTimeMillis();
+extern float /*__attribute__((overloadable))*/rsGetDt();
 
-extern int rsSendToClient(void *data, int cmdID, int len, int waitForSpace);
+extern bool __attribute__((overloadable))rsSendToClient(int cmdID);
+extern bool __attribute__((overloadable))rsSendToClient(int cmdID, const void *data, uint len);
+extern void __attribute__((overloadable))rsSendToClientBlocking(int cmdID);
+extern void __attribute__((overloadable))rsSendToClientBlocking(int cmdID, const void *data, uint len);
 
 // Script to Script
 typedef struct rs_script_call {