Cleanup GL setup code and remove legacy 1.1 paths.
Reset pointers to NULL when allocations are unbound.

Change-Id: Ifaba634ecbb154970b7fb47faa8e6475d16d2f66
diff --git a/rsAllocation.cpp b/rsAllocation.cpp
index 60998c3..87c4f2b 100644
--- a/rsAllocation.cpp
+++ b/rsAllocation.cpp
@@ -724,10 +724,7 @@
     const Element *src = static_cast<const Element *>(_src);
     const Element *dst = static_cast<const Element *>(_dst);
 
-    // Check for pow2 on pre es 2.0 versions.
-    rsAssert(rsc->checkVersion2_0() || (!(w & (w-1)) && !(h & (h-1))));
-
-    //LOGE("rsi_AllocationCreateFromBitmap %i %i %i %i %i", w, h, dstFmt, srcFmt, genMips);
+    //LOGE("%p rsi_AllocationCreateFromBitmap %i %i %i", rsc, w, h, genMips);
     rsi_TypeBegin(rsc, _dst);
     rsi_TypeAdd(rsc, RS_DIMENSION_X, w);
     rsi_TypeAdd(rsc, RS_DIMENSION_Y, h);
diff --git a/rsAllocation.h b/rsAllocation.h
index b6b5d2f..ce5372f 100644
--- a/rsAllocation.h
+++ b/rsAllocation.h
@@ -90,6 +90,7 @@
     void decRefs(const void *ptr, size_t ct) const;
 
     void sendDirty() const;
+    bool getHasGraphicsMipmaps() const {return mTextureGenMipmap;}
 
 protected:
     ObjectBaseRef<const Type> mType;
diff --git a/rsContext.cpp b/rsContext.cpp
index 5327aac..3dbdbfb 100644
--- a/rsContext.cpp
+++ b/rsContext.cpp
@@ -232,23 +232,15 @@
 
 bool Context::setupCheck()
 {
-    if (checkVersion2_0()) {
-        if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
-            LOGE("Context::setupCheck() 1 fail");
-            return false;
-        }
-
-        mFragmentStore->setupGL2(this, &mStateFragmentStore);
-        mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
-        mRaster->setupGL2(this, &mStateRaster);
-        mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
-
-    } else {
-        mFragmentStore->setupGL(this, &mStateFragmentStore);
-        mFragment->setupGL(this, &mStateFragment);
-        mRaster->setupGL(this, &mStateRaster);
-        mVertex->setupGL(this, &mStateVertex);
+    if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
+        LOGE("Context::setupCheck() 1 fail");
+        return false;
     }
+
+    mFragmentStore->setupGL2(this, &mStateFragmentStore);
+    mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
+    mRaster->setupGL2(this, &mStateRaster);
+    mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
     return true;
 }
 
@@ -940,6 +932,7 @@
     LOGV("rsContextCreateGL %p, %i", vdev, useDepth);
     Device * dev = static_cast<Device *>(vdev);
     Context *rsc = new Context(dev, true, useDepth);
+    LOGV("rsContextCreateGL ret %p ", rsc);
     return rsc;
 }
 
diff --git a/rsContext.h b/rsContext.h
index 6d1a41d..e38ba55 100644
--- a/rsContext.h
+++ b/rsContext.h
@@ -153,9 +153,6 @@
     void timerPrint();
     void timerFrame();
 
-    bool checkVersion1_1() const {return (mGL.mMajorVersion > 1) || (mGL.mMinorVersion >= 1); }
-    bool checkVersion2_0() const {return mGL.mMajorVersion >= 2; }
-
     struct {
         bool mLogTimes;
         bool mLogScripts;
diff --git a/rsProgramFragment.cpp b/rsProgramFragment.cpp
index 3174e82..9817fca 100644
--- a/rsProgramFragment.cpp
+++ b/rsProgramFragment.cpp
@@ -103,13 +103,8 @@
     mDirty = true;
 }
 
-void ProgramFragment::setupGL(const Context *rsc, ProgramFragmentState *state)
-{
-}
-
 void ProgramFragment::setupGL2(const Context *rsc, ProgramFragmentState *state, ShaderCache *sc)
 {
-
     //LOGE("sgl2 frag1 %x", glGetError());
     if ((state->mLast.get() == this) && !mDirty) {
         return;
@@ -131,12 +126,12 @@
         glBindTexture(GL_TEXTURE_2D, mTextures[ct]->getTextureID());
         rsc->checkError("ProgramFragment::setupGL2 tex bind");
         if (mSamplers[ct].get()) {
-            mSamplers[ct]->setupGL(rsc, mTextures[ct]->getType()->getIsNp2());
+            mSamplers[ct]->setupGL(rsc, mTextures[ct].get());
         } 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);
+            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
             rsc->checkError("ProgramFragment::setupGL2 tex env");
         }
 
diff --git a/rsProgramFragment.h b/rsProgramFragment.h
index f08bb25..fb78b3f 100644
--- a/rsProgramFragment.h
+++ b/rsProgramFragment.h
@@ -34,7 +34,6 @@
                              uint32_t paramLength);
     virtual ~ProgramFragment();
 
-    virtual void setupGL(const Context *, ProgramFragmentState *);
     virtual void setupGL2(const Context *, ProgramFragmentState *, ShaderCache *sc);
 
     virtual void createShader();
diff --git a/rsProgramRaster.cpp b/rsProgramRaster.cpp
index 5b69370..62d060d 100644
--- a/rsProgramRaster.cpp
+++ b/rsProgramRaster.cpp
@@ -61,52 +61,6 @@
     mDirty = true;
 }
 
-void ProgramRaster::setupGL(const Context *rsc, ProgramRasterState *state)
-{
-    if (state->mLast.get() == this && !mDirty) {
-        return;
-    }
-    state->mLast.set(this);
-    mDirty = false;
-
-    if (mPointSmooth) {
-        glEnable(GL_POINT_SMOOTH);
-    } else {
-        glDisable(GL_POINT_SMOOTH);
-    }
-
-    glLineWidth(mLineWidth);
-    if (mLineSmooth) {
-        glEnable(GL_LINE_SMOOTH);
-    } else {
-        glDisable(GL_LINE_SMOOTH);
-    }
-
-    if (rsc->checkVersion1_1()) {
-#ifndef ANDROID_RS_BUILD_FOR_HOST
-        if (mPointSprite) {
-            glEnable(GL_POINT_SPRITE_OES);
-        } else {
-            glDisable(GL_POINT_SPRITE_OES);
-        }
-#endif //ANDROID_RS_BUILD_FOR_HOST
-    }
-
-    switch(mCull) {
-        case RS_CULL_BACK:
-            glEnable(GL_CULL_FACE);
-            glCullFace(GL_BACK);
-            break;
-        case RS_CULL_FRONT:
-            glEnable(GL_CULL_FACE);
-            glCullFace(GL_FRONT);
-            break;
-        case RS_CULL_NONE:
-            glDisable(GL_CULL_FACE);
-            break;
-    }
-}
-
 void ProgramRaster::setupGL2(const Context *rsc, ProgramRasterState *state)
 {
     if (state->mLast.get() == this && !mDirty) {
diff --git a/rsProgramRaster.h b/rsProgramRaster.h
index 801ab2a..d5ed686 100644
--- a/rsProgramRaster.h
+++ b/rsProgramRaster.h
@@ -34,7 +34,6 @@
                   bool pointSprite);
     virtual ~ProgramRaster();
 
-    virtual void setupGL(const Context *, ProgramRasterState *);
     virtual void setupGL2(const Context *, ProgramRasterState *);
     virtual void serialize(OStream *stream) const;
     virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_PROGRAM_RASTER; }
diff --git a/rsProgramStore.cpp b/rsProgramStore.cpp
index e741c0a..3f90d7a 100644
--- a/rsProgramStore.cpp
+++ b/rsProgramStore.cpp
@@ -56,41 +56,6 @@
 {
 }
 
-void ProgramStore::setupGL(const Context *rsc, ProgramStoreState *state)
-{
-    if (state->mLast.get() == this) {
-        return;
-    }
-    state->mLast.set(this);
-
-    glColorMask(mColorRWriteEnable,
-                mColorGWriteEnable,
-                mColorBWriteEnable,
-                mColorAWriteEnable);
-    if (mBlendEnable) {
-        glEnable(GL_BLEND);
-        glBlendFunc(mBlendSrc, mBlendDst);
-    } else {
-        glDisable(GL_BLEND);
-    }
-
-    //LOGE("pfs  %i, %i, %x", mDepthWriteEnable, mDepthTestEnable, mDepthFunc);
-
-    glDepthMask(mDepthWriteEnable);
-    if(mDepthTestEnable || mDepthWriteEnable) {
-        glEnable(GL_DEPTH_TEST);
-        glDepthFunc(mDepthFunc);
-    } else {
-        glDisable(GL_DEPTH_TEST);
-    }
-
-    if (mDitherEnable) {
-        glEnable(GL_DITHER);
-    } else {
-        glDisable(GL_DITHER);
-    }
-}
-
 void ProgramStore::setupGL2(const Context *rsc, ProgramStoreState *state)
 {
     if (state->mLast.get() == this) {
diff --git a/rsProgramStore.h b/rsProgramStore.h
index fe8d78e..95bcf3c 100644
--- a/rsProgramStore.h
+++ b/rsProgramStore.h
@@ -32,7 +32,6 @@
     ProgramStore(Context *);
     virtual ~ProgramStore();
 
-    virtual void setupGL(const Context *, ProgramStoreState *);
     virtual void setupGL2(const Context *, ProgramStoreState *);
 
     void setDepthFunc(RsDepthFunc);
diff --git a/rsProgramVertex.cpp b/rsProgramVertex.cpp
index 28084d7..6446b55 100644
--- a/rsProgramVertex.cpp
+++ b/rsProgramVertex.cpp
@@ -68,11 +68,6 @@
     LOGV("%6.4f, %6.4f, %6.4f, %6.4f", f[3], f[7], f[11], f[15]);
 }
 
-void ProgramVertex::setupGL(const Context *rsc, ProgramVertexState *state)
-{
-    assert(0);
-}
-
 void ProgramVertex::loadShader(Context *rsc) {
     Program::loadShader(rsc, GL_VERTEX_SHADER);
 }
diff --git a/rsProgramVertex.h b/rsProgramVertex.h
index d6b3f5a..65ce541 100644
--- a/rsProgramVertex.h
+++ b/rsProgramVertex.h
@@ -35,7 +35,6 @@
     ProgramVertex(Context *, bool texMat);
     virtual ~ProgramVertex();
 
-    virtual void setupGL(const Context *rsc, ProgramVertexState *state);
     virtual void setupGL2(const Context *rsc, ProgramVertexState *state, ShaderCache *sc);
 
 
diff --git a/rsSampler.cpp b/rsSampler.cpp
index 47b8a61..c6a848c 100644
--- a/rsSampler.cpp
+++ b/rsSampler.cpp
@@ -59,7 +59,7 @@
 {
 }
 
-void Sampler::setupGL(const Context *rsc, bool npot)
+void Sampler::setupGL(const Context *rsc, const Allocation *tex)
 {
     GLenum trans[] = {
         GL_NEAREST, //RS_SAMPLER_NEAREST,
@@ -77,13 +77,17 @@
         GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
     };
 
-    if (!rsc->ext_OES_texture_npot() && npot) {
+    if (!rsc->ext_OES_texture_npot() && tex->getType()->getIsNp2()) {
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, transNP[mMagFilter]);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, transNP[mWrapS]);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, transNP[mWrapT]);
     } else {
-        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
+        if (tex->getHasGraphicsMipmaps()) {
+            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
+        } else {
+            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
+        }
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, trans[mWrapS]);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, trans[mWrapT]);
diff --git a/rsSampler.h b/rsSampler.h
index 3786439..32a8efd 100644
--- a/rsSampler.h
+++ b/rsSampler.h
@@ -41,7 +41,7 @@
     virtual ~Sampler();
 
     void bind(Allocation *);
-    void setupGL(const Context *, bool npot);
+    void setupGL(const Context *, const Allocation *);
 
     void bindToContext(SamplerState *, uint32_t slot);
     void unbindFromContext(SamplerState *);
diff --git a/rsScriptC.cpp b/rsScriptC.cpp
index 662791d..cbc5df9 100644
--- a/rsScriptC.cpp
+++ b/rsScriptC.cpp
@@ -56,9 +56,16 @@
                 = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
 
     for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) {
-        if (!mSlots[ct].get())
+        if (mSlots[ct].get() && !mTypes[ct].get()) {
+            mTypes[ct].set(mSlots[ct]->getType());
+        }
+
+        if (!mTypes[ct].get())
             continue;
-        void *ptr = mSlots[ct]->getPtr();
+        void *ptr = NULL;
+        if (mSlots[ct].get()) {
+            ptr = mSlots[ct]->getPtr();
+        }
         void **dest = ((void ***)mEnviroment.mFieldAddress)[ct];
         //LOGE("setupScript %i %p = %p    %p %i", ct, dest, ptr, mSlots[ct]->getType(), mSlots[ct]->getType()->getDimX());
 
diff --git a/rsVertexArray.h b/rsVertexArray.h
index 7c609b2..bd76d87 100644
--- a/rsVertexArray.h
+++ b/rsVertexArray.h
@@ -65,7 +65,6 @@
     //void addLegacy(uint32_t type, uint32_t size, uint32_t stride, bool normalized, uint32_t offset);
     void add(uint32_t type, uint32_t size, uint32_t stride, bool normalized, uint32_t offset, const char *name);
 
-    void setupGL(const Context *rsc, class VertexArrayState *) const;
     void setupGL2(const Context *rsc, class VertexArrayState *, ShaderCache *) const;
     void logAttrib(uint32_t idx, uint32_t slot) const;