Fix some minor bugs with GL state setup that were exposed by Droids driver.
diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java
index 7d100eb..e5cf38e 100644
--- a/graphics/java/android/renderscript/Allocation.java
+++ b/graphics/java/android/renderscript/Allocation.java
@@ -45,13 +45,11 @@
 
     public void uploadToTexture(int baseMipLevel) {
         mRS.validate();
-        mRS.validateSurface();
         mRS.nAllocationUploadToTexture(mID, baseMipLevel);
     }
 
     public void uploadToBufferObject() {
         mRS.validate();
-        mRS.validateSurface();
         mRS.nAllocationUploadToBufferObject(mID);
     }
 
diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java
index b558de0..29361af 100644
--- a/graphics/java/android/renderscript/RenderScript.java
+++ b/graphics/java/android/renderscript/RenderScript.java
@@ -246,13 +246,8 @@
         }
     }
 
-    void validateSurface() {
-        //if (mSurface == null) {
-            //throw new IllegalStateException("Uploading data to GL with no surface.");
-        //}
-    }
-
     public void contextSetPriority(Priority p) {
+        validate();
         nContextSetPriority(p.mID);
     }
 
@@ -312,14 +307,17 @@
         mSurface = sur;
         mWidth = w;
         mHeight = h;
+        validate();
         nContextSetSurface(w, h, mSurface);
     }
 
     public void contextDump(int bits) {
+        validate();
         nContextDump(bits);
     }
 
     public void destroy() {
+        validate();
         nContextDeinitToClient();
         mMessageThread.mRun = false;
 
@@ -335,10 +333,12 @@
     }
 
     void pause() {
+        validate();
         nContextPause();
     }
 
     void resume() {
+        validate();
         nContextResume();
     }
 
@@ -379,22 +379,27 @@
     }
 
     public void contextBindRootScript(Script s) {
+        validate();
         nContextBindRootScript(safeID(s));
     }
 
     public void contextBindProgramFragmentStore(ProgramStore p) {
+        validate();
         nContextBindProgramFragmentStore(safeID(p));
     }
 
     public void contextBindProgramFragment(ProgramFragment p) {
+        validate();
         nContextBindProgramFragment(safeID(p));
     }
 
     public void contextBindProgramRaster(ProgramRaster p) {
+        validate();
         nContextBindProgramRaster(safeID(p));
     }
 
     public void contextBindProgramVertex(ProgramVertex p) {
+        validate();
         nContextBindProgramVertex(safeID(p));
     }
 
diff --git a/libs/rs/rsProgramFragment.cpp b/libs/rs/rsProgramFragment.cpp
index 00f19ae..15f3269 100644
--- a/libs/rs/rsProgramFragment.cpp
+++ b/libs/rs/rsProgramFragment.cpp
@@ -109,7 +109,7 @@
         }
 
         if (mSamplers[ct].get()) {
-            mSamplers[ct]->setupGL();
+            mSamplers[ct]->setupGL(rsc);
         } else {
             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
@@ -141,32 +141,35 @@
 
 void ProgramFragment::setupGL2(const Context *rsc, ProgramFragmentState *state, ShaderCache *sc)
 {
+
     //LOGE("sgl2 frag1 %x", glGetError());
     if ((state->mLast.get() == this) && !mDirty) {
         //return;
     }
     state->mLast.set(this);
 
+    rsc->checkError("ProgramFragment::setupGL2 start");
     for (uint32_t ct=0; ct < MAX_TEXTURE; ct++) {
         glActiveTexture(GL_TEXTURE0 + ct);
         if (!(mTextureEnableMask & (1 << ct)) || !mTextures[ct].get()) {
-            glDisable(GL_TEXTURE_2D);
             continue;
         }
 
         mTextures[ct]->uploadCheck(rsc);
         glBindTexture(GL_TEXTURE_2D, mTextures[ct]->getTextureID());
+        rsc->checkError("ProgramFragment::setupGL2 tex bind");
         if (mSamplers[ct].get()) {
-            mSamplers[ct]->setupGL();
+            mSamplers[ct]->setupGL(rsc);
         } else {
             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+            rsc->checkError("ProgramFragment::setupGL2 tex env");
         }
 
-        glEnable(GL_TEXTURE_2D);
         glUniform1i(sc->fragUniformSlot(ct), ct);
+        rsc->checkError("ProgramFragment::setupGL2 uniforms");
     }
 
     glActiveTexture(GL_TEXTURE0);
diff --git a/libs/rs/rsProgramVertex.cpp b/libs/rs/rsProgramVertex.cpp
index 2c9bdaa..28f13d4 100644
--- a/libs/rs/rsProgramVertex.cpp
+++ b/libs/rs/rsProgramVertex.cpp
@@ -204,6 +204,7 @@
         //return;
     }
 
+    rsc->checkError("ProgramVertex::setupGL2 start");
     glVertexAttrib4f(1, state->color[0], state->color[1], state->color[2], state->color[3]);
 
     const float *f = static_cast<const float *>(mConstants[0]->getPtr());
@@ -220,6 +221,7 @@
                            &f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET]);
     }
 
+    rsc->checkError("ProgramVertex::setupGL2 begin uniforms");
     uint32_t uidx = 1;
     for (uint32_t ct=0; ct < mConstantCount; ct++) {
         Allocation *alloc = mConstants[ct+1].get();
diff --git a/libs/rs/rsSampler.cpp b/libs/rs/rsSampler.cpp
index f9bdb2e..7552d54 100644
--- a/libs/rs/rsSampler.cpp
+++ b/libs/rs/rsSampler.cpp
@@ -53,7 +53,7 @@
 {
 }
 
-void Sampler::setupGL()
+void Sampler::setupGL(const Context *rsc)
 {
     GLenum trans[] = {
         GL_NEAREST, //RS_SAMPLER_NEAREST,
@@ -69,6 +69,7 @@
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, trans[mWrapS]);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, trans[mWrapT]);
 
+    rsc->checkError("ProgramFragment::setupGL2 tex env");
 }
 
 void Sampler::bindToContext(SamplerState *ss, uint32_t slot)
@@ -83,18 +84,18 @@
     mBoundSlot = -1;
     ss->mSamplers[slot].clear();
 }
-
+/*
 void SamplerState::setupGL()
 {
     for (uint32_t ct=0; ct < RS_MAX_SAMPLER_SLOT; ct++) {
         Sampler *s = mSamplers[ct].get();
         if (s) {
-            s->setupGL();
+            s->setupGL(rsc);
         } else {
             glBindTexture(GL_TEXTURE_2D, 0);
         }
     }
-}
+}*/
 
 ////////////////////////////////
 
diff --git a/libs/rs/rsSampler.h b/libs/rs/rsSampler.h
index ccf9b4d..9e20a2f 100644
--- a/libs/rs/rsSampler.h
+++ b/libs/rs/rsSampler.h
@@ -41,7 +41,7 @@
     virtual ~Sampler();
 
     void bind(Allocation *);
-    void setupGL();
+    void setupGL(const Context *);
 
     void bindToContext(SamplerState *, uint32_t slot);
     void unbindFromContext(SamplerState *);
@@ -74,7 +74,7 @@
 
     ObjectBaseRef<Sampler> mSamplers[RS_MAX_SAMPLER_SLOT];
 
-    void setupGL();
+    //void setupGL();
 
 };
 
diff --git a/libs/rs/rsSimpleMesh.cpp b/libs/rs/rsSimpleMesh.cpp
index a819c07..53ce5cd 100644
--- a/libs/rs/rsSimpleMesh.cpp
+++ b/libs/rs/rsSimpleMesh.cpp
@@ -63,7 +63,7 @@
             va.setActiveBuffer(mVertexBuffers[ct]->getBufferObjectID());
             mVertexTypes[ct]->enableGLVertexBuffer2(&va);
         }
-        va.setupGL2(rsc, 0, &rsc->mShaderCache);
+        va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
     } else {
         for (uint32_t ct=0; ct < mVertexTypeCount; ct++) {
             mVertexBuffers[ct]->uploadCheck(rsc);
diff --git a/libs/rs/rsVertexArray.cpp b/libs/rs/rsVertexArray.cpp
index a1fd7442..d0c0414 100644
--- a/libs/rs/rsVertexArray.cpp
+++ b/libs/rs/rsVertexArray.cpp
@@ -180,10 +180,12 @@
 
 void VertexArray::setupGL2(const Context *rsc, class VertexArrayState *state, ShaderCache *sc) const
 {
-    for (int ct=1; ct < RS_MAX_ATTRIBS; ct++) {
+    rsc->checkError("VertexArray::setupGL2 start");
+    for (uint32_t ct=1; ct <= state->mLastEnableCount; ct++) {
         glDisableVertexAttribArray(ct);
     }
 
+    rsc->checkError("VertexArray::setupGL2 disabled");
     for (uint32_t ct=0; ct < mCount; ct++) {
         uint32_t slot = 0;
         if (sc->isUserVertexProgram()) {
@@ -203,10 +205,12 @@
                               mAttribs[ct].stride,
                               (void *)mAttribs[ct].offset);
     }
-    rsc->checkError("VertexArray::setupGL2");
+    state->mLastEnableCount = mCount;
+    rsc->checkError("VertexArray::setupGL2 done");
 }
 ////////////////////////////////////////////
 
 void VertexArrayState::init(Context *) {
+    mLastEnableCount = 0;
 }
 
diff --git a/libs/rs/rsVertexArray.h b/libs/rs/rsVertexArray.h
index 66b3ab00..3904cb6 100644
--- a/libs/rs/rsVertexArray.h
+++ b/libs/rs/rsVertexArray.h
@@ -73,6 +73,7 @@
 public:
     void init(Context *);
 
+    uint32_t mLastEnableCount;
     //VertexArray::Attrib mAttribs[VertexArray::_LAST];
 };