Merge "Add multitouch support to physics test. Fix context state overwrite calling invoke."
diff --git a/java/Balls/src/com/android/balls/BallsRS.java b/java/Balls/src/com/android/balls/BallsRS.java
index 897b231..4338f33 100644
--- a/java/Balls/src/com/android/balls/BallsRS.java
+++ b/java/Balls/src/com/android/balls/BallsRS.java
@@ -129,9 +129,7 @@
     }
 
     public void newTouchPosition(float x, float y, float pressure, int id) {
-        mPhysicsScript.set_touchX(x);
-        mPhysicsScript.set_touchY(y);
-        mPhysicsScript.set_touchPressure(pressure);
+        mPhysicsScript.invoke_touch(x, y, pressure, id);
     }
 
     public void setAccel(float x, float y) {
diff --git a/java/Balls/src/com/android/balls/BallsView.java b/java/Balls/src/com/android/balls/BallsView.java
index 12f017b..4442eec 100644
--- a/java/Balls/src/com/android/balls/BallsView.java
+++ b/java/Balls/src/com/android/balls/BallsView.java
@@ -82,6 +82,7 @@
             int pointerIndex = ev.getActionIndex();
             int pointerId = ev.getPointerId(pointerIndex);
             mRender.newTouchPosition(0, 0, 0, pointerId);
+            return false;
         }
         int count = ev.getHistorySize();
         int pcount = ev.getPointerCount();
diff --git a/java/Balls/src/com/android/balls/ball_physics.rs b/java/Balls/src/com/android/balls/ball_physics.rs
index 7c86c67..ff38be5 100644
--- a/java/Balls/src/com/android/balls/ball_physics.rs
+++ b/java/Balls/src/com/android/balls/ball_physics.rs
@@ -8,18 +8,22 @@
 float2 gMinPos = {0.f, 0.f};
 float2 gMaxPos = {1280.f, 700.f};
 
-float touchX;
-float touchY;
-float touchPressure = 0.f;
+static float2 touchPos[10];
+static float touchPressure[10];
 
-void setGamma(float g) {
+void touch(float x, float y, float pressure, int id) {
+    if (id >= 10) {
+        return;
+    }
+
+    touchPos[id].x = x;
+    touchPos[id].y = y;
+    touchPressure[id] = pressure;
 }
 
-
 void root(const Ball_t *ballIn, Ball_t *ballOut, const BallControl_t *ctl, uint32_t x) {
     float2 fv = {0, 0};
     float2 pos = ballIn->position;
-    //rsDebug("physics pos in", pos);
 
     int arcID = -1;
     float arcInvStr = 100000;
@@ -38,10 +42,6 @@
             if (len2 > 16 /* (minDist*minDist)*/)  {
                 // Repulsion
                 float len = sqrt(len2);
-                //if (len < arcInvStr) {
-                    //arcInvStr = len;
-                    //arcID = xin;
-                //}
                 fv -= (vec / (len * len * len)) * 20000.f * forceScale;
             } else {
                 if (len2 < 1) {
@@ -78,12 +78,13 @@
     fv -= gGravityVector * 4.f;
     fv *= ctl->dt;
 
-    if (touchPressure > 0.1f) {
-        float2 tp = {touchX, touchY};
-        float2 vec = tp - ballIn->position;
-        float2 vec2 = vec * vec;
-        float len2 = max(2.f, vec2.x + vec2.y);
-        fv -= (vec / len2) * touchPressure * 400.f;
+    for (int i=0; i < 10; i++) {
+        if (touchPressure[i] > 0.1f) {
+            float2 vec = touchPos[i] - ballIn->position;
+            float2 vec2 = vec * vec;
+            float len2 = max(2.f, vec2.x + vec2.y);
+            fv -= (vec / len2) * touchPressure[i] * 300.f;
+        }
     }
 
     ballOut->delta = (ballIn->delta * (1.f - 0.004f)) + fv;
@@ -138,11 +139,6 @@
         }
     }
 
-    //ballOut->color.b = 1.f;
-    //ballOut->color.r = min(sqrt(length(ballOut->delta)) * 0.1f, 1.f);
-    //ballOut->color.g = min(sqrt(length(fv) * 0.1f), 1.f);
-    //ballOut->arcID = arcID;
-    //ballOut->arcStr = 8 / arcInvStr;
     ballOut->size = ballIn->size;
 
     //rsDebug("physics pos out", ballOut->position);
diff --git a/rsScriptC.cpp b/rsScriptC.cpp
index d4edafd..b3dbf11 100644
--- a/rsScriptC.cpp
+++ b/rsScriptC.cpp
@@ -47,7 +47,6 @@
 }
 
 void ScriptC::setupScript(Context *rsc) {
-    setupGLState(rsc);
     mEnviroment.mStartTimeMillis
                 = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
 
@@ -123,6 +122,7 @@
         return 0;
     }
 
+    setupGLState(rsc);
     setupScript(rsc);
 
     uint32_t ret = 0;
@@ -278,6 +278,7 @@
 
     rsAssert(ain->getType()->getDimZ() == 0);
 
+    setupGLState(rsc);
     setupScript(rsc);
     Script * oldTLS = setTLS(this);
 
@@ -336,7 +337,6 @@
 }
 
 void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len) {
-    //LOGE("rsi_ScriptInvoke %i", slot);
     if ((slot >= mEnviroment.mInvokeFunctionCount) ||
         (mEnviroment.mInvokeFunctions[slot] == NULL)) {
         rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");