Moving samplers behind the hal.

Change-Id: I494e5a9d2b599d07b985328b346f1f10ae4972e1
diff --git a/Android.mk b/Android.mk
index 232ab36..c336c0d 100644
--- a/Android.mk
+++ b/Android.mk
@@ -124,6 +124,7 @@
 	driver/rsdProgramStore.cpp \
 	driver/rsdRuntimeMath.cpp \
 	driver/rsdRuntimeStubs.cpp \
+	driver/rsdSampler.cpp \
 	driver/rsdShader.cpp \
 	driver/rsdShaderCache.cpp \
 	driver/rsdVertexArray.cpp
diff --git a/driver/rsdCore.cpp b/driver/rsdCore.cpp
index d5d23c7..0a5f2ec 100644
--- a/driver/rsdCore.cpp
+++ b/driver/rsdCore.cpp
@@ -22,6 +22,7 @@
 #include "rsdProgramVertex.h"
 #include "rsdProgramFragment.h"
 #include "rsdMesh.h"
+#include "rsdSampler.h"
 
 #include <malloc.h>
 #include "rsContext.h"
@@ -90,7 +91,12 @@
         rsdMeshInit,
         rsdMeshDraw,
         rsdMeshDestroy
-    }
+    },
+
+    {
+        rsdSamplerInit,
+        rsdSamplerDestroy
+    },
 
 };
 
diff --git a/driver/rsdSampler.cpp b/driver/rsdSampler.cpp
new file mode 100644
index 0000000..af48c61
--- /dev/null
+++ b/driver/rsdSampler.cpp
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#include "rsdCore.h"
+#include "rsdSampler.h"
+
+#include "rsContext.h"
+#include "rsSampler.h"
+#include "rsProgramVertex.h"
+#include "rsProgramFragment.h"
+
+#include <GLES/gl.h>
+#include <GLES/glext.h>
+
+
+using namespace android;
+using namespace android::renderscript;
+
+bool rsdSamplerInit(const android::renderscript::Context *,
+                    const android::renderscript::Sampler *) {
+    return true;
+}
+
+void rsdSamplerDestroy(const android::renderscript::Context *rsc,
+                       const android::renderscript::Sampler *s) {
+}
diff --git a/driver/rsdSampler.h b/driver/rsdSampler.h
new file mode 100644
index 0000000..3a64e9e
--- /dev/null
+++ b/driver/rsdSampler.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef RSD_SAMPLER_H
+#define RSD_SAMPLER_H
+
+#include <rs_hal.h>
+
+
+bool rsdSamplerInit(const android::renderscript::Context *rsc,
+                    const android::renderscript::Sampler *);
+
+void rsdSamplerDestroy(const android::renderscript::Context *rsc,
+                       const android::renderscript::Sampler *);
+
+
+#endif // RSD_SAMPLER_H
diff --git a/driver/rsdShader.cpp b/driver/rsdShader.cpp
index fc623d6..1710a8b 100644
--- a/driver/rsdShader.cpp
+++ b/driver/rsdShader.cpp
@@ -21,6 +21,7 @@
 #include <rsContext.h>
 #include <rsProgram.h>
 
+#include "rsdCore.h"
 #include "rsdShader.h"
 #include "rsdShaderCache.h"
 
@@ -315,6 +316,70 @@
     }
 }
 
+void RsdShader::setupSampler(const Context *rsc, const Sampler *s, const Allocation *tex) {
+    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
+
+    GLenum trans[] = {
+        GL_NEAREST, //RS_SAMPLER_NEAREST,
+        GL_LINEAR, //RS_SAMPLER_LINEAR,
+        GL_LINEAR_MIPMAP_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
+        GL_REPEAT, //RS_SAMPLER_WRAP,
+        GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
+        GL_LINEAR_MIPMAP_NEAREST, //RS_SAMPLER_LINEAR_MIP_NEAREST
+    };
+
+    GLenum transNP[] = {
+        GL_NEAREST, //RS_SAMPLER_NEAREST,
+        GL_LINEAR, //RS_SAMPLER_LINEAR,
+        GL_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
+        GL_CLAMP_TO_EDGE, //RS_SAMPLER_WRAP,
+        GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
+        GL_LINEAR, //RS_SAMPLER_LINEAR_MIP_NEAREST,
+    };
+
+    // This tells us the correct texture type
+    GLenum target = (GLenum)tex->getGLTarget();
+
+    if (!dc->gl.gl.OES_texture_npot && tex->getType()->getIsNp2()) {
+        if (tex->getHasGraphicsMipmaps() &&
+            (dc->gl.gl.GL_NV_texture_npot_2D_mipmap || dc->gl.gl.GL_IMG_texture_npot)) {
+            if (dc->gl.gl.GL_NV_texture_npot_2D_mipmap) {
+                glTexParameteri(target, GL_TEXTURE_MIN_FILTER, trans[s->mHal.state.minFilter]);
+            } else {
+                switch (trans[s->mHal.state.minFilter]) {
+                case GL_LINEAR_MIPMAP_LINEAR:
+                    glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
+                    break;
+                default:
+                    glTexParameteri(target, GL_TEXTURE_MIN_FILTER, trans[s->mHal.state.minFilter]);
+                    break;
+                }
+            }
+        } else {
+            glTexParameteri(target, GL_TEXTURE_MIN_FILTER, transNP[s->mHal.state.minFilter]);
+        }
+        glTexParameteri(target, GL_TEXTURE_MAG_FILTER, transNP[s->mHal.state.magFilter]);
+        glTexParameteri(target, GL_TEXTURE_WRAP_S, transNP[s->mHal.state.wrapS]);
+        glTexParameteri(target, GL_TEXTURE_WRAP_T, transNP[s->mHal.state.wrapT]);
+    } else {
+        if (tex->getHasGraphicsMipmaps()) {
+            glTexParameteri(target, GL_TEXTURE_MIN_FILTER, trans[s->mHal.state.minFilter]);
+        } else {
+            glTexParameteri(target, GL_TEXTURE_MIN_FILTER, transNP[s->mHal.state.minFilter]);
+        }
+        glTexParameteri(target, GL_TEXTURE_MAG_FILTER, trans[s->mHal.state.magFilter]);
+        glTexParameteri(target, GL_TEXTURE_WRAP_S, trans[s->mHal.state.wrapS]);
+        glTexParameteri(target, GL_TEXTURE_WRAP_T, trans[s->mHal.state.wrapT]);
+    }
+
+    float anisoValue = rsMin(dc->gl.gl.EXT_texture_max_aniso, s->mHal.state.aniso);
+    if (dc->gl.gl.EXT_texture_max_aniso > 1.0f) {
+        glTexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisoValue);
+    }
+
+    rsc->checkError("Sampler::setupGL2 tex env");
+}
+
 void RsdShader::setupTextures(const Context *rsc, RsdShaderCache *sc) {
     if (mRSProgram->mHal.state.texturesCount == 0) {
         return;
@@ -345,7 +410,7 @@
         glBindTexture(target, mRSProgram->mHal.state.textures[ct]->getTextureID());
         rsc->checkError("ProgramFragment::setupGL2 tex bind");
         if (mRSProgram->mHal.state.samplers[ct].get()) {
-            mRSProgram->mHal.state.samplers[ct]->setupGL(rsc, mRSProgram->mHal.state.textures[ct].get());
+            setupSampler(rsc, mRSProgram->mHal.state.samplers[ct].get(), mRSProgram->mHal.state.textures[ct].get());
         } else {
             glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
             glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
diff --git a/driver/rsdShader.h b/driver/rsdShader.h
index 37b1c3d..63c4231 100644
--- a/driver/rsdShader.h
+++ b/driver/rsdShader.h
@@ -70,6 +70,7 @@
     void setupUserConstants(const android::renderscript::Context *rsc, RsdShaderCache *sc, bool isFragment);
     void initAddUserElement(const android::renderscript::Element *e, android::String8 *names, uint32_t *arrayLengths, uint32_t *count, const char *prefix);
     void setupTextures(const android::renderscript::Context *rsc, RsdShaderCache *sc);
+    void setupSampler(const android::renderscript::Context *rsc, const android::renderscript::Sampler *s, const android::renderscript::Allocation *tex);
 
     void appendAttributes();
     void appendTextures();
diff --git a/rsProgram.cpp b/rsProgram.cpp
index c0fa95b..b1d8f48 100644
--- a/rsProgram.cpp
+++ b/rsProgram.cpp
@@ -20,13 +20,9 @@
 using namespace android;
 using namespace android::renderscript;
 
-Program::Program(Context *rsc) : ObjectBase(rsc) {
-   initMemberVars();
-}
-
 Program::Program(Context *rsc, const char * shaderText, uint32_t shaderLength,
                  const uint32_t * params, uint32_t paramLength)
-    : ObjectBase(rsc) {
+    : ProgramBase(rsc) {
 
     initMemberVars();
     for (uint32_t ct=0; ct < paramLength; ct+=2) {
diff --git a/rsProgram.h b/rsProgram.h
index 2922270..948ba3e 100644
--- a/rsProgram.h
+++ b/rsProgram.h
@@ -17,7 +17,7 @@
 #ifndef ANDROID_RS_PROGRAM_H
 #define ANDROID_RS_PROGRAM_H
 
-#include "rsObjectBase.h"
+#include "rsProgramBase.h"
 #include "rsElement.h"
 
 // ---------------------------------------------------------------------------
@@ -28,10 +28,9 @@
 #define RS_SHADER_ATTR "ATTRIB_"
 #define RS_SHADER_UNI "UNI_"
 
-class Program : public ObjectBase {
+class Program : public ProgramBase {
 public:
 
-    Program(Context *);
     Program(Context *, const char * shaderText, uint32_t shaderLength,
                        const uint32_t * params, uint32_t paramLength);
     virtual ~Program();
@@ -43,8 +42,6 @@
     void bindTexture(Context *, uint32_t slot, Allocation *);
     void bindSampler(Context *, uint32_t slot, Sampler *);
 
-    void forceDirty() const {mDirty = true;}
-
     struct Hal {
         mutable void *drv;
 
@@ -75,18 +72,13 @@
 
 protected:
     bool mIsInternal;
-
-    mutable bool mDirty;
     String8 mUserShader;
-
-    void logUniform(const Element *field, const float *fd, uint32_t arraySize );
-    void setUniform(Context *rsc, const Element *field, const float *fd, int32_t slot, uint32_t arraySize );
     void initMemberVars();
 };
 
 }
 }
-#endif
+#endif // ANDROID_RS_PROGRAM_H
 
 
 
diff --git a/rsProgramBase.h b/rsProgramBase.h
new file mode 100644
index 0000000..80da453
--- /dev/null
+++ b/rsProgramBase.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_RS_PROGRAM_BASE_H
+#define ANDROID_RS_PROGRAM_BASE_H
+
+#include "rsObjectBase.h"
+#include "rsElement.h"
+
+// ---------------------------------------------------------------------------
+namespace android {
+namespace renderscript {
+
+class ProgramBase : public ObjectBase {
+public:
+    ProgramBase(Context *rsc) : ObjectBase(rsc) {
+        mDirty = true;
+    }
+
+    void forceDirty() const {mDirty = true;}
+
+protected:
+    mutable bool mDirty;
+};
+
+}
+}
+#endif // ANDROID_RS_PROGRAM_BASE_H
+
+
+
diff --git a/rsProgramRaster.cpp b/rsProgramRaster.cpp
index 9617c4d..435561d 100644
--- a/rsProgramRaster.cpp
+++ b/rsProgramRaster.cpp
@@ -24,7 +24,7 @@
 ProgramRaster::ProgramRaster(Context *rsc, bool pointSmooth,
                              bool lineSmooth, bool pointSprite,
                              float lineWidth, RsCullMode cull)
-    : Program(rsc) {
+    : ProgramBase(rsc) {
 
     memset(&mHal, 0, sizeof(mHal));
 
diff --git a/rsProgramRaster.h b/rsProgramRaster.h
index 045a7c1..efdb948 100644
--- a/rsProgramRaster.h
+++ b/rsProgramRaster.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009 The Android Open Source Project
+ * Copyright (C) 2009-2011 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,7 +17,7 @@
 #ifndef ANDROID_RS_PROGRAM_RASTER_H
 #define ANDROID_RS_PROGRAM_RASTER_H
 
-#include "rsProgram.h"
+#include "rsProgramBase.h"
 
 // ---------------------------------------------------------------------------
 namespace android {
@@ -25,7 +25,7 @@
 
 class ProgramRasterState;
 
-class ProgramRaster : public Program {
+class ProgramRaster : public ProgramBase {
 public:
     ProgramRaster(Context *rsc,
                   bool pointSmooth,
diff --git a/rsProgramStore.cpp b/rsProgramStore.cpp
index 2ad65e9..8fe890b 100644
--- a/rsProgramStore.cpp
+++ b/rsProgramStore.cpp
@@ -25,7 +25,7 @@
                            bool colorMaskR, bool colorMaskG, bool colorMaskB, bool colorMaskA,
                            bool depthMask, bool ditherEnable,
                            RsBlendSrcFunc srcFunc, RsBlendDstFunc destFunc,
-                           RsDepthFunc depthFunc) : Program(rsc) {
+                           RsDepthFunc depthFunc) : ProgramBase(rsc) {
     memset(&mHal, 0, sizeof(mHal));
 
     mHal.state.ditherEnable = ditherEnable;
diff --git a/rsProgramStore.h b/rsProgramStore.h
index 88a06a8..77b3881 100644
--- a/rsProgramStore.h
+++ b/rsProgramStore.h
@@ -17,7 +17,7 @@
 #ifndef ANDROID_RS_PROGRAM_FRAGMENT_STORE_H
 #define ANDROID_RS_PROGRAM_FRAGMENT_STORE_H
 
-#include "rsProgram.h"
+#include "rsProgramBase.h"
 #include "rsStream.h"
 
 // ---------------------------------------------------------------------------
@@ -26,7 +26,7 @@
 
 class ProgramStoreState;
 
-class ProgramStore : public Program {
+class ProgramStore : public ProgramBase {
 public:
     ProgramStore(Context *,
                  bool colorMaskR, bool colorMaskG, bool colorMaskB, bool colorMaskA,
diff --git a/rsSampler.cpp b/rsSampler.cpp
index 670d07e..2a05d16 100644
--- a/rsSampler.cpp
+++ b/rsSampler.cpp
@@ -15,11 +15,6 @@
  */
 
 #include "rsContext.h"
-#ifndef ANDROID_RS_SERIALIZE
-#include <GLES/gl.h>
-#include <GLES/glext.h>
-#endif //ANDROID_RS_SERIALIZE
-
 #include "rsSampler.h"
 
 
@@ -45,71 +40,12 @@
     mHal.state.wrapT = wrapT;
     mHal.state.wrapR = wrapR;
     mHal.state.aniso = aniso;
+
+    mRSC->mHal.funcs.sampler.init(mRSC, this);
 }
 
 Sampler::~Sampler() {
-}
-
-void Sampler::setupGL(const Context *rsc, const Allocation *tex) {
-    GLenum trans[] = {
-        GL_NEAREST, //RS_SAMPLER_NEAREST,
-        GL_LINEAR, //RS_SAMPLER_LINEAR,
-        GL_LINEAR_MIPMAP_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
-        GL_REPEAT, //RS_SAMPLER_WRAP,
-        GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
-        GL_LINEAR_MIPMAP_NEAREST, //RS_SAMPLER_LINEAR_MIP_NEAREST
-    };
-
-    GLenum transNP[] = {
-        GL_NEAREST, //RS_SAMPLER_NEAREST,
-        GL_LINEAR, //RS_SAMPLER_LINEAR,
-        GL_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
-        GL_CLAMP_TO_EDGE, //RS_SAMPLER_WRAP,
-        GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
-        GL_LINEAR, //RS_SAMPLER_LINEAR_MIP_NEAREST,
-    };
-
-    // This tells us the correct texture type
-    GLenum target = (GLenum)tex->getGLTarget();
-
-    if (!rsc->ext_OES_texture_npot() && tex->getType()->getIsNp2()) {
-        if (tex->getHasGraphicsMipmaps() &&
-            (rsc->ext_GL_NV_texture_npot_2D_mipmap() || rsc->ext_GL_IMG_texture_npot())) {
-            if (rsc->ext_GL_NV_texture_npot_2D_mipmap()) {
-                glTexParameteri(target, GL_TEXTURE_MIN_FILTER, trans[mHal.state.minFilter]);
-            } else {
-                switch (trans[mHal.state.minFilter]) {
-                case GL_LINEAR_MIPMAP_LINEAR:
-                    glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
-                    break;
-                default:
-                    glTexParameteri(target, GL_TEXTURE_MIN_FILTER, trans[mHal.state.minFilter]);
-                    break;
-                }
-            }
-        } else {
-            glTexParameteri(target, GL_TEXTURE_MIN_FILTER, transNP[mHal.state.minFilter]);
-        }
-        glTexParameteri(target, GL_TEXTURE_MAG_FILTER, transNP[mHal.state.magFilter]);
-        glTexParameteri(target, GL_TEXTURE_WRAP_S, transNP[mHal.state.wrapS]);
-        glTexParameteri(target, GL_TEXTURE_WRAP_T, transNP[mHal.state.wrapT]);
-    } else {
-        if (tex->getHasGraphicsMipmaps()) {
-            glTexParameteri(target, GL_TEXTURE_MIN_FILTER, trans[mHal.state.minFilter]);
-        } else {
-            glTexParameteri(target, GL_TEXTURE_MIN_FILTER, transNP[mHal.state.minFilter]);
-        }
-        glTexParameteri(target, GL_TEXTURE_MAG_FILTER, trans[mHal.state.magFilter]);
-        glTexParameteri(target, GL_TEXTURE_WRAP_S, trans[mHal.state.wrapS]);
-        glTexParameteri(target, GL_TEXTURE_WRAP_T, trans[mHal.state.wrapT]);
-    }
-
-    float anisoValue = rsMin(rsc->ext_texture_max_aniso(), mHal.state.aniso);
-    if (rsc->ext_texture_max_aniso() > 1.0f) {
-        glTexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisoValue);
-    }
-
-    rsc->checkError("Sampler::setupGL2 tex env");
+    mRSC->mHal.funcs.sampler.destroy(mRSC, this);
 }
 
 void Sampler::bindToContext(SamplerState *ss, uint32_t slot) {
diff --git a/rsSampler.h b/rsSampler.h
index 192ee07..90b6082 100644
--- a/rsSampler.h
+++ b/rsSampler.h
@@ -40,9 +40,6 @@
 
     virtual ~Sampler();
 
-    void bind(Allocation *);
-    void setupGL(const Context *, const Allocation *);
-
     void bindToContext(SamplerState *, uint32_t slot);
     void unbindFromContext(SamplerState *);
 
diff --git a/rs_hal.h b/rs_hal.h
index d2f273b..4cc2abf 100644
--- a/rs_hal.h
+++ b/rs_hal.h
@@ -30,11 +30,13 @@
 class Allocation;
 class Script;
 class ScriptC;
+class Program;
 class ProgramStore;
 class ProgramRaster;
 class ProgramVertex;
 class ProgramFragment;
 class Mesh;
+class Sampler;
 
 typedef void *(*RsHalSymbolLookupFunc)(void *usrptr, char const *symbolName);
 
@@ -121,6 +123,11 @@
         void (*destroy)(const Context *rsc, const Mesh *m);
     } mesh;
 
+    struct {
+        bool (*init)(const Context *rsc, const Sampler *m);
+        void (*destroy)(const Context *rsc, const Sampler *m);
+    } sampler;
+
 } RsdHalFunctions;