rsLib cleanup

Change-Id: Ifb66059338e3435ac79435296a650c44699921aa
diff --git a/libs/rs/driver/rsdGL.cpp b/libs/rs/driver/rsdGL.cpp
index 48690d5..de9fb51 100644
--- a/libs/rs/driver/rsdGL.cpp
+++ b/libs/rs/driver/rsdGL.cpp
@@ -30,11 +30,8 @@
 #include <GLES2/gl2.h>
 #include <GLES2/gl2ext.h>
 
-//#include <cutils/sched_policy.h>
-//#include <sys/syscall.h>
 #include <string.h>
 
-
 #include "rsdCore.h"
 #include "rsdGL.h"
 
@@ -346,3 +343,27 @@
     eglSwapBuffers(dc->gl.egl.display, dc->gl.egl.surface);
 }
 
+void rsdGLCheckError(const android::renderscript::Context *rsc,
+                     const char *msg, bool isFatal) {
+    GLenum err = glGetError();
+    if (err != GL_NO_ERROR) {
+        char buf[1024];
+        snprintf(buf, sizeof(buf), "GL Error = 0x%08x, from: %s", err, msg);
+
+        if (isFatal) {
+            rsc->setError(RS_ERROR_FATAL_DRIVER, buf);
+        } else {
+            switch (err) {
+            case GL_OUT_OF_MEMORY:
+                rsc->setError(RS_ERROR_OUT_OF_MEMORY, buf);
+                break;
+            default:
+                rsc->setError(RS_ERROR_DRIVER, buf);
+                break;
+            }
+        }
+
+        LOGE("%p, %s", rsc, buf);
+    }
+
+}
diff --git a/libs/rs/driver/rsdGL.h b/libs/rs/driver/rsdGL.h
index 351b2d5..90cbe04 100644
--- a/libs/rs/driver/rsdGL.h
+++ b/libs/rs/driver/rsdGL.h
@@ -76,6 +76,8 @@
 bool rsdGLSetSurface(const android::renderscript::Context *rsc,
                      uint32_t w, uint32_t h, ANativeWindow *sur);
 void rsdGLSwap(const android::renderscript::Context *rsc);
+void rsdGLCheckError(const android::renderscript::Context *rsc,
+                     const char *msg, bool isFatal = false);
 
 #endif
 
diff --git a/libs/rs/driver/rsdMeshObj.cpp b/libs/rs/driver/rsdMeshObj.cpp
index 6bb33f7..2c07784 100644
--- a/libs/rs/driver/rsdMeshObj.cpp
+++ b/libs/rs/driver/rsdMeshObj.cpp
@@ -23,6 +23,7 @@
 #include <rsMesh.h>
 
 #include "rsdMeshObj.h"
+#include "rsdGL.h"
 
 using namespace android;
 using namespace android::renderscript;
@@ -134,7 +135,7 @@
         return;
     }
 
-    rsc->checkError("Mesh::renderPrimitiveRange 1");
+    rsdGLCheckError(rsc, "Mesh::renderPrimitiveRange 1");
     // update attributes with either buffer information or data ptr based on their current state
     for (uint32_t ct=0; ct < mAttribCount; ct++) {
         uint32_t allocIndex = mAttribAllocationIndex[ct];
@@ -149,9 +150,9 @@
     }
 
     RsdVertexArray va(mAttribs, mAttribCount);
-    va.setupGL2(rsc);
+    va.setup(rsc);
 
-    rsc->checkError("Mesh::renderPrimitiveRange 2");
+    rsdGLCheckError(rsc, "Mesh::renderPrimitiveRange 2");
     Mesh::Primitive_t *prim = mRSMesh->mHal.state.primitives[primIndex];
     if (prim->mIndexBuffer.get()) {
         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, prim->mIndexBuffer->getBufferObjectID());
@@ -160,7 +161,7 @@
         glDrawArrays(mGLPrimitives[primIndex], start, len);
     }
 
-    rsc->checkError("Mesh::renderPrimitiveRange");
+    rsdGLCheckError(rsc, "Mesh::renderPrimitiveRange");
 }
 
 void RsdMeshObj::updateGLPrimitives() {
diff --git a/libs/rs/driver/rsdShader.cpp b/libs/rs/driver/rsdShader.cpp
index 1710a8b..371266b 100644
--- a/libs/rs/driver/rsdShader.cpp
+++ b/libs/rs/driver/rsdShader.cpp
@@ -377,7 +377,7 @@
         glTexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisoValue);
     }
 
-    rsc->checkError("Sampler::setupGL2 tex env");
+    rsdGLCheckError(rsc, "Sampler::setup tex env");
 }
 
 void RsdShader::setupTextures(const Context *rsc, RsdShaderCache *sc) {
@@ -385,8 +385,10 @@
         return;
     }
 
+    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
+
     uint32_t numTexturesToBind = mRSProgram->mHal.state.texturesCount;
-    uint32_t numTexturesAvailable = rsc->getMaxFragmentTextures();
+    uint32_t numTexturesAvailable = dc->gl.gl.maxFragmentTextureImageUnits;
     if (numTexturesToBind >= numTexturesAvailable) {
         LOGE("Attempting to bind %u textures on shader id %u, but only %u are available",
              mRSProgram->mHal.state.texturesCount, (uint32_t)this, numTexturesAvailable);
@@ -408,7 +410,7 @@
             rsc->setError(RS_ERROR_BAD_SHADER, "Non-texture allocation bound to a shader");
         }
         glBindTexture(target, mRSProgram->mHal.state.textures[ct]->getTextureID());
-        rsc->checkError("ProgramFragment::setupGL2 tex bind");
+        rsdGLCheckError(rsc, "ProgramFragment::setup tex bind");
         if (mRSProgram->mHal.state.samplers[ct].get()) {
             setupSampler(rsc, mRSProgram->mHal.state.samplers[ct].get(), mRSProgram->mHal.state.textures[ct].get());
         } else {
@@ -416,16 +418,16 @@
             glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
             glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
             glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-            rsc->checkError("ProgramFragment::setupGL2 tex env");
+            rsdGLCheckError(rsc, "ProgramFragment::setup tex env");
         }
 
         glUniform1i(sc->fragUniformSlot(mTextureUniformIndexStart + ct), ct);
-        rsc->checkError("ProgramFragment::setupGL2 uniforms");
+        rsdGLCheckError(rsc, "ProgramFragment::setup uniforms");
     }
 
     glActiveTexture(GL_TEXTURE0);
     mDirty = false;
-    rsc->checkError("ProgramFragment::setupGL2");
+    rsdGLCheckError(rsc, "ProgramFragment::setup");
 }
 
 void RsdShader::setupUserConstants(const Context *rsc, RsdShaderCache *sc, bool isFragment) {
diff --git a/libs/rs/driver/rsdShaderCache.cpp b/libs/rs/driver/rsdShaderCache.cpp
index 18a8225..d11490c 100644
--- a/libs/rs/driver/rsdShaderCache.cpp
+++ b/libs/rs/driver/rsdShaderCache.cpp
@@ -19,6 +19,7 @@
 
 #include "rsdShader.h"
 #include "rsdShaderCache.h"
+#include "rsdGL.h"
 
 #include <GLES/gl.h>
 #include <GLES2/gl2.h>
@@ -128,7 +129,7 @@
             glUseProgram(mEntries[ct]->program);
             mCurrent = mEntries[ct];
             //LOGV("RsdShaderCache hit, using %i", ct);
-            rsc->checkError("RsdShaderCache::link (hit)");
+            rsdGLCheckError(rsc, "RsdShaderCache::link (hit)");
             return true;
         }
     }
@@ -230,7 +231,7 @@
 
     //LOGV("SC made program %i", e->program);
     glUseProgram(e->program);
-    rsc->checkError("RsdShaderCache::link (miss)");
+    rsdGLCheckError(rsc, "RsdShaderCache::link (miss)");
 
     return true;
 }
diff --git a/libs/rs/driver/rsdVertexArray.cpp b/libs/rs/driver/rsdVertexArray.cpp
index d0a5a54..62ec107 100644
--- a/libs/rs/driver/rsdVertexArray.cpp
+++ b/libs/rs/driver/rsdVertexArray.cpp
@@ -20,6 +20,7 @@
 #include <GLES/gl.h>
 #include <GLES2/gl2.h>
 
+#include "rsdGL.h"
 #include "rsdCore.h"
 #include "rsdVertexArray.h"
 #include "rsdShaderCache.h"
@@ -78,13 +79,13 @@
          mAttribs[idx].offset);
 }
 
-void RsdVertexArray::setupGL2(const Context *rsc) const {
+void RsdVertexArray::setup(const Context *rsc) const {
 
     RsdHal *dc = (RsdHal *)rsc->mHal.drv;
     RsdVertexArrayState *state = dc->gl.vertexArrayState;
     RsdShaderCache *sc = dc->gl.shaderCache;
 
-    rsc->checkError("RsdVertexArray::setupGL2 start");
+    rsdGLCheckError(rsc, "RsdVertexArray::setup start");
     uint32_t maxAttrs = state->mAttrsEnabledSize;
 
     for (uint32_t ct=1; ct < maxAttrs; ct++) {
@@ -94,7 +95,7 @@
         }
     }
 
-    rsc->checkError("RsdVertexArray::setupGL2 disabled");
+    rsdGLCheckError(rsc, "RsdVertexArray::setup disabled");
     for (uint32_t ct=0; ct < mCount; ct++) {
         int32_t slot = sc->vtxAttribSlot(mAttribs[ct].name);
         if (rsc->props.mLogShadersAttr) {
@@ -113,7 +114,7 @@
                               mAttribs[ct].stride,
                               mAttribs[ct].ptr + mAttribs[ct].offset);
     }
-    rsc->checkError("RsdVertexArray::setupGL2 done");
+    rsdGLCheckError(rsc, "RsdVertexArray::setup done");
 }
 ////////////////////////////////////////////
 RsdVertexArrayState::RsdVertexArrayState() {
diff --git a/libs/rs/driver/rsdVertexArray.h b/libs/rs/driver/rsdVertexArray.h
index 925a6ae..3e807a3 100644
--- a/libs/rs/driver/rsdVertexArray.h
+++ b/libs/rs/driver/rsdVertexArray.h
@@ -49,7 +49,7 @@
     RsdVertexArray(const Attrib *attribs, uint32_t numAttribs);
     virtual ~RsdVertexArray();
 
-    void setupGL2(const android::renderscript::Context *rsc) const;
+    void setup(const android::renderscript::Context *rsc) const;
     void logAttrib(uint32_t idx, uint32_t slot) const;
 
 protected: