Fix build issue and ref count bug with scripts.
diff --git a/libs/rs/rsContext.cpp b/libs/rs/rsContext.cpp
index ec4a309..a16bfc7 100644
--- a/libs/rs/rsContext.cpp
+++ b/libs/rs/rsContext.cpp
@@ -37,13 +37,9 @@
          EGL_NONE
      };
 
-     LOGE("EGL 1");
      mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
-     LOGE("EGL 2  %p", mDisplay);
      eglInitialize(mDisplay, &mMajorVersion, &mMinorVersion);
-     LOGE("EGL 3  %i  %i", mMajorVersion, mMinorVersion);
      eglChooseConfig(mDisplay, s_configAttribs, &mConfig, 1, &mNumConfigs);
-     LOGE("EGL 4  %p", mConfig);
 
      if (mWndSurface) {
          mSurface = eglCreateWindowSurface(mDisplay, mConfig,
@@ -55,13 +51,10 @@
                  NULL);
      }
 
-     LOGE("EGL 5");
      mContext = eglCreateContext(mDisplay, mConfig, NULL, NULL);
      eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
      eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mWidth);
      eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mHeight);
-     LOGE("EGL 9");
-
 }
 
 bool Context::runRootScript()
@@ -123,16 +116,11 @@
 {
      Context *rsc = static_cast<Context *>(vrsc);
 
-     LOGE("TP 1");
      gIO = new ThreadIO();
-
      rsc->mServerCommands.init(128);
      rsc->mServerReturns.init(128);
 
      rsc->initEGL();
-
-     LOGE("TP 2");
-
      rsc->mRunning = true;
      bool mDraw = true;
      while (!rsc->mExit) {
@@ -149,7 +137,6 @@
          }
      }
 
-     LOGE("TP 6");
      glClearColor(0,0,0,0);
      glClear(GL_COLOR_BUFFER_BIT);
      eglSwapBuffers(rsc->mDisplay, rsc->mSurface);
@@ -159,7 +146,6 @@
 
 Context::Context(Device *dev, Surface *sur)
 {
-    LOGE("CC 1");
     dev->addContext(this);
     mDev = dev;
     mRunning = false;
@@ -171,7 +157,6 @@
     // see comment in header
     gCon = this;
 
-    LOGE("CC 2");
     int status;
     pthread_attr_t threadAttr;
 
@@ -185,17 +170,16 @@
     sparam.sched_priority = ANDROID_PRIORITY_DISPLAY;
     pthread_attr_setschedparam(&threadAttr, &sparam);
 
+    LOGE("RS Launching thread");
     status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
     if (status) {
         LOGE("Failed to start rs context thread.");
     }
 
-    LOGE("CC 3");
     mWndSurface = sur;
     while(!mRunning) {
         sleep(1);
     }
-    LOGE("CC 4");
 
     pthread_attr_destroy(&threadAttr);
 }
@@ -205,14 +189,11 @@
     mExit = true;
     void *res;
 
-    LOGE("DES 1");
     int status = pthread_join(mThreadId, &res);
-    LOGE("DES 2");
 
     if (mDev) {
         mDev->removeContext(this);
     }
-    LOGE("DES 3");
 }
 
 void Context::swapBuffers()
diff --git a/libs/rs/rsScriptC.cpp b/libs/rs/rsScriptC.cpp
index be0191b..91b743c 100644
--- a/libs/rs/rsScriptC.cpp
+++ b/libs/rs/rsScriptC.cpp
@@ -27,9 +27,7 @@
 ScriptC::ScriptC()
 {
     mAccScript = NULL;
-    mScript = NULL;
-    mScriptText = NULL;
-    mScriptTextLength = 0;
+    memset(&mProgram, 0, sizeof(mProgram));
 }
 
 ScriptC::~ScriptC()
@@ -392,7 +390,7 @@
 bool ScriptC::run(Context *rsc, uint32_t launchID)
 {
     Env e = {rsc, this};
-    return mScript(&e, &scriptCPtrTable, launchID) != 0;
+    return mProgram.mScript(&e, &scriptCPtrTable, launchID) != 0;
 }
 
 ScriptCState::ScriptCState()
@@ -409,32 +407,33 @@
 
 void ScriptCState::clear()
 {
+    memset(&mProgram, 0, sizeof(mProgram));
+
     mConstantBufferTypes.clear();
-    mClearColor[0] = 0;
-    mClearColor[1] = 0;
-    mClearColor[2] = 0;
-    mClearColor[3] = 1;
-    mClearDepth = 1;
-    mClearStencil = 0;
+
+    memset(&mEnviroment, 0, sizeof(mEnviroment));
+    mEnviroment.mClearColor[0] = 0;
+    mEnviroment.mClearColor[1] = 0;
+    mEnviroment.mClearColor[2] = 0;
+    mEnviroment.mClearColor[3] = 1;
+    mEnviroment.mClearDepth = 1;
+    mEnviroment.mClearStencil = 0;
+    mEnviroment.mIsRoot = false;
+    mEnviroment.mIsOrtho = true;
+
     mAccScript = NULL;
-    mScript = NULL;
-    mIsRoot = false;
-    mIsOrtho = true;
-    mScriptText = NULL;
-    mScriptTextLength = 0;
+
 }
 
 void ScriptCState::runCompiler()
 {
     mAccScript = accCreateScript();
 
-    LOGE("Compiler 1");
-    const char* scriptSource[] = {mScriptText};
-    int scriptLength[] = {mScriptTextLength} ;
+    const char* scriptSource[] = {mProgram.mScriptText};
+    int scriptLength[] = {mProgram.mScriptTextLength} ;
     accScriptSource(mAccScript, 1, scriptSource, scriptLength);
     accCompileScript(mAccScript);
-    accGetScriptLabel(mAccScript, "main", (ACCvoid**) &mScript);
-    LOGE("Compiler 1");
+    accGetScriptLabel(mAccScript, "main", (ACCvoid**) &mProgram.mScript);
 }
 
 namespace android {
@@ -449,22 +448,22 @@
 void rsi_ScriptCSetClearColor(Context * rsc, float r, float g, float b, float a)
 {
     ScriptCState *ss = &rsc->mScriptC;
-    ss->mClearColor[0] = r;
-    ss->mClearColor[1] = g;
-    ss->mClearColor[2] = b;
-    ss->mClearColor[3] = a;
+    ss->mEnviroment.mClearColor[0] = r;
+    ss->mEnviroment.mClearColor[1] = g;
+    ss->mEnviroment.mClearColor[2] = b;
+    ss->mEnviroment.mClearColor[3] = a;
 }
 
 void rsi_ScriptCSetClearDepth(Context * rsc, float v)
 {
     ScriptCState *ss = &rsc->mScriptC;
-    ss->mClearDepth = v;
+    ss->mEnviroment.mClearDepth = v;
 }
 
 void rsi_ScriptCSetClearStencil(Context * rsc, uint32_t v)
 {
     ScriptCState *ss = &rsc->mScriptC;
-    ss->mClearStencil = v;
+    ss->mEnviroment.mClearStencil = v;
 }
 
 void rsi_ScriptCAddType(Context * rsc, RsType vt)
@@ -473,30 +472,29 @@
     ss->mConstantBufferTypes.add(static_cast<const Type *>(vt));
 }
 
-void rsi_ScriptCSetScript(Context * rsc, void* accScript, void *vp)
+void rsi_ScriptCSetScript(Context * rsc, void *vp)
 {
     ScriptCState *ss = &rsc->mScriptC;
-    ss->mAccScript = reinterpret_cast<ACCscript*>(accScript);
-    ss->mScript = reinterpret_cast<rsc_RunScript>(vp);
+    ss->mProgram.mScript = reinterpret_cast<rsc_RunScript>(vp);
 }
 
 void rsi_ScriptCSetRoot(Context * rsc, bool isRoot)
 {
     ScriptCState *ss = &rsc->mScriptC;
-    ss->mIsRoot = isRoot;
+    ss->mEnviroment.mIsRoot = isRoot;
 }
 
 void rsi_ScriptCSetOrtho(Context * rsc, bool isOrtho)
 {
     ScriptCState *ss = &rsc->mScriptC;
-    ss->mIsOrtho = isOrtho;
+    ss->mEnviroment.mIsOrtho = isOrtho;
 }
 
 void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len)
 {
     ScriptCState *ss = &rsc->mScriptC;
-    ss->mScriptText = text;
-    ss->mScriptTextLength = len;
+    ss->mProgram.mScriptText = text;
+    ss->mProgram.mScriptTextLength = len;
 }
 
 
@@ -507,19 +505,12 @@
     ss->runCompiler();
 
     ScriptC *s = new ScriptC();
+    s->incRef();
     s->mAccScript = ss->mAccScript;
     ss->mAccScript = NULL;
-    s->mScript = ss->mScript;
-    s->mClearColor[0] = ss->mClearColor[0];
-    s->mClearColor[1] = ss->mClearColor[1];
-    s->mClearColor[2] = ss->mClearColor[2];
-    s->mClearColor[3] = ss->mClearColor[3];
-    s->mClearDepth = ss->mClearDepth;
-    s->mClearStencil = ss->mClearStencil;
-    s->mIsRoot = ss->mIsRoot;
-    s->mIsOrtho = ss->mIsOrtho;
-    s->mScriptText = ss->mScriptText;
-    s->mScriptTextLength = ss->mScriptTextLength;
+    s->mEnviroment = ss->mEnviroment;
+    s->mProgram = ss->mProgram;
+    ss->clear();
     return s;
 }
 
diff --git a/libs/rs/rsScriptC.h b/libs/rs/rsScriptC.h
index 664bd6d..5b79b90 100644
--- a/libs/rs/rsScriptC.h
+++ b/libs/rs/rsScriptC.h
@@ -36,15 +36,32 @@
     ScriptC();
     virtual ~ScriptC();
 
+    struct Program_t {
+        const char * mScriptText;
+        uint32_t mScriptTextLength;
 
-    virtual bool run(Context *, uint32_t launchID);
+        bool useStateVertex;
+        bool useStateRaster;
+        bool useStateFragment;
+        bool useStateStore;
 
-    const char * mScriptText;
-    uint32_t mScriptTextLength;
+        rsc_RunScript mScript;
+    };
+
+    struct Enviroment_t {
+        bool mIsRoot;
+        bool mIsOrtho;
+        float mClearColor[4];
+        float mClearDepth;
+        uint32_t mClearStencil;
+    };
+
+    Program_t mProgram;
+    Enviroment_t mEnviroment;
 
     ACCscript*    mAccScript;
-    rsc_RunScript mScript;
 
+    virtual bool run(Context *, uint32_t launchID);
 
     struct Env {
         Context *mContext;
@@ -60,14 +77,9 @@
     ~ScriptCState();
 
     ACCscript* mAccScript;
-    const char * mScriptText;
-    uint32_t mScriptTextLength;
-    rsc_RunScript mScript;
-    float mClearColor[4];
-    float mClearDepth;
-    uint32_t mClearStencil;
-    bool mIsRoot;
-    bool mIsOrtho;
+
+    ScriptC::Program_t mProgram;
+    ScriptC::Enviroment_t mEnviroment;
 
     Vector<const Type *> mConstantBufferTypes;