Moving samplers behind the hal.

Change-Id: I494e5a9d2b599d07b985328b346f1f10ae4972e1
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();