checkpoint filmstrip, implemented TLS.
diff --git a/rsScriptC.cpp b/rsScriptC.cpp
index c650656..557f3ae 100644
--- a/rsScriptC.cpp
+++ b/rsScriptC.cpp
@@ -24,6 +24,11 @@
 using namespace android;
 using namespace android::renderscript;
 
+#define GET_TLS()  Context::ScriptTLSStruct * tls = \
+    (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \
+    Context * rsc = tls->mContext; \
+    ScriptC * sc = (ScriptC *) tls->mScript
+
 
 ScriptC::ScriptC()
 {
@@ -38,220 +43,212 @@
     }
 }
 
-extern "C" void matrixLoadIdentity(void *con, rsc_Matrix *mat)
+extern "C" void matrixLoadIdentity(rsc_Matrix *mat)
 {
     Matrix *m = reinterpret_cast<Matrix *>(mat);
     m->loadIdentity();
 }
 
-extern "C" void matrixLoadFloat(void *con, rsc_Matrix *mat, const float *f)
+extern "C" void matrixLoadFloat(rsc_Matrix *mat, const float *f)
 {
     Matrix *m = reinterpret_cast<Matrix *>(mat);
     m->load(f);
 }
 
-extern "C" void matrixLoadMat(void *con, rsc_Matrix *mat, const rsc_Matrix *newmat)
+extern "C" void matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat)
 {
     Matrix *m = reinterpret_cast<Matrix *>(mat);
     m->load(reinterpret_cast<const Matrix *>(newmat));
 }
 
-extern "C" void matrixLoadRotate(void *con, rsc_Matrix *mat, float rot, float x, float y, float z)
+extern "C" void matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
 {
     Matrix *m = reinterpret_cast<Matrix *>(mat);
     m->loadRotate(rot, x, y, z);
 }
 
-extern "C" void matrixLoadScale(void *con, rsc_Matrix *mat, float x, float y, float z)
+extern "C" void matrixLoadScale(rsc_Matrix *mat, float x, float y, float z)
 {
     Matrix *m = reinterpret_cast<Matrix *>(mat);
     m->loadScale(x, y, z);
 }
 
-extern "C" void matrixLoadTranslate(void *con, rsc_Matrix *mat, float x, float y, float z)
+extern "C" void matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z)
 {
     Matrix *m = reinterpret_cast<Matrix *>(mat);
     m->loadTranslate(x, y, z);
 }
 
-extern "C" void matrixLoadMultiply(void *con, rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs)
+extern "C" void 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));
 }
 
-extern "C" void matrixMultiply(void *con, rsc_Matrix *mat, const rsc_Matrix *rhs)
+extern "C" void matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs)
 {
     Matrix *m = reinterpret_cast<Matrix *>(mat);
     m->multiply(reinterpret_cast<const Matrix *>(rhs));
 }
 
-extern "C" void matrixRotate(void *con, rsc_Matrix *mat, float rot, float x, float y, float z)
+extern "C" void matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
 {
     Matrix *m = reinterpret_cast<Matrix *>(mat);
     m->rotate(rot, x, y, z);
 }
 
-extern "C" void matrixScale(void *con, rsc_Matrix *mat, float x, float y, float z)
+extern "C" void matrixScale(rsc_Matrix *mat, float x, float y, float z)
 {
     Matrix *m = reinterpret_cast<Matrix *>(mat);
     m->scale(x, y, z);
 }
 
-extern "C" void matrixTranslate(void *con, rsc_Matrix *mat, float x, float y, float z)
+extern "C" void matrixTranslate(rsc_Matrix *mat, float x, float y, float z)
 {
     Matrix *m = reinterpret_cast<Matrix *>(mat);
     m->translate(x, y, z);
 }
 
 
-extern "C" const void * loadVp(void *vp, uint32_t bank, uint32_t offset)
+extern "C" const void * loadVp(uint32_t bank, uint32_t offset)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
-    return &static_cast<const uint8_t *>(env->mScript->mSlots[bank]->getPtr())[offset];
+    GET_TLS();
+    return &static_cast<const uint8_t *>(sc->mSlots[bank]->getPtr())[offset];
 }
 
-extern "C" float loadF(void *vp, uint32_t bank, uint32_t offset)
+extern "C" float loadF(uint32_t bank, uint32_t offset)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
-    //LOGE("bank %i, offset %i", bank, offset);
-    //LOGE("%p", env->mScript->mSlots[bank]->getPtr());
-    return static_cast<const float *>(env->mScript->mSlots[bank]->getPtr())[offset];
+    GET_TLS();
+    return static_cast<const float *>(sc->mSlots[bank]->getPtr())[offset];
 }
 
-extern "C" int32_t loadI32(void *vp, uint32_t bank, uint32_t offset)
+extern "C" int32_t loadI32(uint32_t bank, uint32_t offset)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
-    return static_cast<const int32_t *>(env->mScript->mSlots[bank]->getPtr())[offset];
+    GET_TLS();
+    return static_cast<const int32_t *>(sc->mSlots[bank]->getPtr())[offset];
 }
 
-extern "C" uint32_t loadU32(void *vp, uint32_t bank, uint32_t offset)
+extern "C" uint32_t loadU32(uint32_t bank, uint32_t offset)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
-    return static_cast<const uint32_t *>(env->mScript->mSlots[bank]->getPtr())[offset];
+    GET_TLS();
+    return static_cast<const uint32_t *>(sc->mSlots[bank]->getPtr())[offset];
 }
 
-extern "C" void loadEnvVec4(void *vp, uint32_t bank, uint32_t offset, rsc_Vector4 *v)
+extern "C" void loadEnvVec4(uint32_t bank, uint32_t offset, rsc_Vector4 *v)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
-    memcpy(v, &static_cast<const float *>(env->mScript->mSlots[bank]->getPtr())[offset], sizeof(rsc_Vector4));
+    GET_TLS();
+    memcpy(v, &static_cast<const float *>(sc->mSlots[bank]->getPtr())[offset], sizeof(rsc_Vector4));
 }
 
-extern "C" void loadEnvMatrix(void *vp, uint32_t bank, uint32_t offset, rsc_Matrix *m)
+extern "C" void loadEnvMatrix(uint32_t bank, uint32_t offset, rsc_Matrix *m)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
-    memcpy(m, &static_cast<const float *>(env->mScript->mSlots[bank]->getPtr())[offset], sizeof(rsc_Matrix));
+    GET_TLS();
+    memcpy(m, &static_cast<const float *>(sc->mSlots[bank]->getPtr())[offset], sizeof(rsc_Matrix));
 }
 
 
-extern "C" void storeF(void *vp, uint32_t bank, uint32_t offset, float v)
+extern "C" void storeF(uint32_t bank, uint32_t offset, float v)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
-    static_cast<float *>(env->mScript->mSlots[bank]->getPtr())[offset] = v;
+    GET_TLS();
+    static_cast<float *>(sc->mSlots[bank]->getPtr())[offset] = v;
 }
 
-extern "C" void storeI32(void *vp, uint32_t bank, uint32_t offset, int32_t v)
+extern "C" void storeI32(uint32_t bank, uint32_t offset, int32_t v)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
-    static_cast<int32_t *>(env->mScript->mSlots[bank]->getPtr())[offset] = v;
+    GET_TLS();
+    static_cast<int32_t *>(sc->mSlots[bank]->getPtr())[offset] = v;
 }
 
-extern "C" void storeU32(void *vp, uint32_t bank, uint32_t offset, uint32_t v)
+extern "C" void storeU32(uint32_t bank, uint32_t offset, uint32_t v)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
-    static_cast<uint32_t *>(env->mScript->mSlots[bank]->getPtr())[offset] = v;
+    GET_TLS();
+    static_cast<uint32_t *>(sc->mSlots[bank]->getPtr())[offset] = v;
 }
 
-extern "C" void storeEnvVec4(void *vp, uint32_t bank, uint32_t offset, const rsc_Vector4 *v)
+extern "C" void storeEnvVec4(uint32_t bank, uint32_t offset, const rsc_Vector4 *v)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
-    memcpy(&static_cast<float *>(env->mScript->mSlots[bank]->getPtr())[offset], v, sizeof(rsc_Vector4));
+    GET_TLS();
+    memcpy(&static_cast<float *>(sc->mSlots[bank]->getPtr())[offset], v, sizeof(rsc_Vector4));
 }
 
-extern "C" void storeEnvMatrix(void *vp, uint32_t bank, uint32_t offset, const rsc_Matrix *m)
+extern "C" void storeEnvMatrix(uint32_t bank, uint32_t offset, const rsc_Matrix *m)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
-    memcpy(&static_cast<float *>(env->mScript->mSlots[bank]->getPtr())[offset], m, sizeof(rsc_Matrix));
+    GET_TLS();
+    memcpy(&static_cast<float *>(sc->mSlots[bank]->getPtr())[offset], m, sizeof(rsc_Matrix));
 }
 
 
-extern "C" void color(void *vp, float r, float g, float b, float a)
+extern "C" void color(float r, float g, float b, float a)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
     glColor4f(r, g, b, a);
 }
 
-extern "C" void renderTriangleMesh(void *vp, RsTriangleMesh mesh)
+extern "C" void renderTriangleMesh(RsTriangleMesh mesh)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
-    rsi_TriangleMeshRender(env->mContext, mesh);
+    GET_TLS();
+    rsi_TriangleMeshRender(rsc, mesh);
 }
 
-extern "C" void renderTriangleMeshRange(void *vp, RsTriangleMesh mesh, uint32_t start, uint32_t count)
+extern "C" void renderTriangleMeshRange(RsTriangleMesh mesh, uint32_t start, uint32_t count)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
-    rsi_TriangleMeshRenderRange(env->mContext, mesh, start, count);
+    GET_TLS();
+    rsi_TriangleMeshRenderRange(rsc, mesh, start, count);
 }
 
-extern "C" void materialDiffuse(void *vp, float r, float g, float b, float a)
+extern "C" void materialDiffuse(float r, float g, float b, float a)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
     float v[] = {r, g, b, a};
     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, v);
 }
 
-extern "C" void materialSpecular(void *vp, float r, float g, float b, float a)
+extern "C" void materialSpecular(float r, float g, float b, float a)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
     float v[] = {r, g, b, a};
     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, v);
 }
 
-extern "C" void lightPosition(void *vp, float x, float y, float z, float w)
+extern "C" void lightPosition(float x, float y, float z, float w)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
     float v[] = {x, y, z, w};
     glLightfv(GL_LIGHT0, GL_POSITION, v);
 }
 
-extern "C" void materialShininess(void *vp, float s)
+extern "C" void materialShininess(float s)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &s);
 }
 
-extern "C" void uploadToTexture(void *vp, RsAllocation va, uint32_t baseMipLevel)
+extern "C" void uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
-    rsi_AllocationUploadToTexture(env->mContext, va, baseMipLevel);
+    GET_TLS();
+    rsi_AllocationUploadToTexture(rsc, va, baseMipLevel);
 }
 
-extern "C" void enable(void *vp, uint32_t p)
+extern "C" void enable(uint32_t p)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
     glEnable(p);
 }
 
-extern "C" void disable(void *vp, uint32_t p)
+extern "C" void disable(uint32_t p)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
     glDisable(p);
 }
 
-extern "C" uint32_t scriptRand(void *vp, uint32_t max)
+extern "C" uint32_t scriptRand(uint32_t max)
 {
     return (uint32_t)(((float)rand()) * max / RAND_MAX);
 }
 
 // Assumes (GL_FIXED) x,y,z (GL_UNSIGNED_BYTE)r,g,b,a
-extern "C" void drawTriangleArray(void *vp, RsAllocation alloc, uint32_t count)
+extern "C" void drawTriangleArray(RsAllocation alloc, uint32_t count)
 {
+    GET_TLS();
+
     const Allocation *a = (const Allocation *)alloc;
     const uint32_t *ptr = (const uint32_t *)a->getPtr();
 
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
-    env->mContext->setupCheck();
+    rsc->setupCheck();
 
     glBindBuffer(GL_ARRAY_BUFFER, 0);
     //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
@@ -268,8 +265,9 @@
     glDrawArrays(GL_TRIANGLES, 0, count * 3);
 }
 
-extern "C" void drawRect(void *vp, int32_t x1, int32_t x2, int32_t y1, int32_t y2)
+extern "C" void drawRect(int32_t x1, int32_t x2, int32_t y1, int32_t y2)
 {
+    GET_TLS();
     x1 = (x1 << 16);
     x2 = (x2 << 16);
     y1 = (y1 << 16);
@@ -279,8 +277,7 @@
     static const int32_t tex[] = {0,0, 0,0x10000, 0x10000,0, 0x10000,0x10000};
 
 
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
-    env->mContext->setupCheck();
+    rsc->setupCheck();
 
     glBindBuffer(GL_ARRAY_BUFFER, 0);
     //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
@@ -297,40 +294,37 @@
     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
 }
 
-extern "C" void pfBindTexture(void *vp, RsProgramFragment vpf, uint32_t slot, RsAllocation va)
+extern "C" void pfBindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va)
 {
-    //LOGE("pfBindTexture %p", vpf);
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
-    rsi_ProgramFragmentBindTexture(env->mContext,
+    GET_TLS();
+    rsi_ProgramFragmentBindTexture(rsc,
                                    static_cast<ProgramFragment *>(vpf),
                                    slot,
                                    static_cast<Allocation *>(va));
 
 }
 
-extern "C" void pfBindSampler(void *vp, RsProgramFragment vpf, uint32_t slot, RsSampler vs)
+extern "C" void pfBindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
 {
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
-    rsi_ProgramFragmentBindSampler(env->mContext,
+    GET_TLS();
+    rsi_ProgramFragmentBindSampler(rsc,
                                    static_cast<ProgramFragment *>(vpf),
                                    slot,
                                    static_cast<Sampler *>(vs));
 
 }
 
-extern "C" void contextBindProgramFragmentStore(void *vp, RsProgramFragmentStore pfs)
+extern "C" void contextBindProgramFragmentStore(RsProgramFragmentStore pfs)
 {
-    //LOGE("contextBindProgramFragmentStore %p", pfs);
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
-    rsi_ContextBindProgramFragmentStore(env->mContext, pfs);
+    GET_TLS();
+    rsi_ContextBindProgramFragmentStore(rsc, pfs);
 
 }
 
-extern "C" void contextBindProgramFragment(void *vp, RsProgramFragment pf)
+extern "C" void contextBindProgramFragment(RsProgramFragment pf)
 {
-    //LOGE("contextBindProgramFragment %p", pf);
-    ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
-    rsi_ContextBindProgramFragment(env->mContext, pf);
+    GET_TLS();
+    rsi_ContextBindProgramFragment(rsc, pf);
 
 }
 
@@ -388,9 +382,10 @@
 };
 
 
-bool ScriptC::run(Context *rsc, uint32_t launchID)
+bool ScriptC::run(Context *rsc, uint32_t launchIndex)
 {
-    Env e = {rsc, this};
+    Context::ScriptTLSStruct * tls = 
+    (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey);
 
     if (mEnviroment.mFragmentStore.get()) {
         rsc->setFragmentStore(mEnviroment.mFragmentStore.get());
@@ -402,7 +397,9 @@
         rsc->setVertex(mEnviroment.mVertex.get());
     }
 
-    return mProgram.mScript(&e, &scriptCPtrTable, launchID) != 0;
+    tls->mScript = this;
+    return mProgram.mScript(launchIndex, &scriptCPtrTable) != 0;
+    tls->mScript = NULL;
 }
 
 ScriptCState::ScriptCState()