Attempt to create libRSDriver as shared library.

Change-Id: I047b32325efe2fa471b73c48fa7296beecc0c47d
diff --git a/Android.mk b/Android.mk
index 5676462..2d4f580 100644
--- a/Android.mk
+++ b/Android.mk
@@ -39,8 +39,9 @@
         driver/rsdIntrinsics_Convolve.S
 endif
 
+LOCAL_SHARED_LIBRARIES += libRS
 LOCAL_SHARED_LIBRARIES += libcutils libutils libEGL libGLESv1_CM libGLESv2
-LOCAL_SHARED_LIBRARIES += libbcc libbcinfo libgui libsync
+LOCAL_SHARED_LIBRARIES += libbcc libbcinfo libui libgui libsync
 
 LOCAL_C_INCLUDES += frameworks/compile/libbcc/include
 
@@ -49,7 +50,7 @@
 LOCAL_LDLIBS := -lpthread -ldl
 LOCAL_MODULE_TAGS := optional
 
-include $(BUILD_STATIC_LIBRARY)
+include $(BUILD_SHARED_LIBRARY)
 
 # Build rsg-generator ====================
 include $(CLEAR_VARS)
@@ -79,6 +80,9 @@
 LOCAL_MODULE_CLASS := SHARED_LIBRARIES
 intermediates:= $(local-intermediates-dir)
 
+# We depend directly on doing dlopen(libRSDriver.so).
+LOCAL_REQUIRED_MODULES := libRSDriver
+
 # Generate custom headers
 
 GEN := $(addprefix $(intermediates)/, \
@@ -150,9 +154,9 @@
 	rsType.cpp
 
 LOCAL_SHARED_LIBRARIES += libcutils libutils libEGL libGLESv1_CM libGLESv2 libbcc
-LOCAL_SHARED_LIBRARIES += libui libbcinfo libgui libsync
+LOCAL_SHARED_LIBRARIES += libui libbcinfo libgui libsync libdl
 
-LOCAL_STATIC_LIBRARIES := libft2 libRSDriver
+LOCAL_STATIC_LIBRARIES := libft2
 
 LOCAL_C_INCLUDES += external/freetype/include
 LOCAL_C_INCLUDES += frameworks/compile/libbcc/include
diff --git a/driver/rsdCore.cpp b/driver/rsdCore.cpp
index d580a3d..b2e2b08 100644
--- a/driver/rsdCore.cpp
+++ b/driver/rsdCore.cpp
@@ -203,7 +203,9 @@
     }
 }
 
-bool rsdHalInit(Context *rsc, uint32_t version_major, uint32_t version_minor) {
+extern "C" bool rsdHalInit(RsContext c, uint32_t version_major,
+                           uint32_t version_minor) {
+    Context *rsc = (Context*) c;
     rsc->mHal.funcs = FunctionTable;
 
     RsdHal *dc = (RsdHal *)calloc(1, sizeof(RsdHal));
diff --git a/driver/rsdRuntimeStubs.cpp b/driver/rsdRuntimeStubs.cpp
index da92839..1ab676e 100644
--- a/driver/rsdRuntimeStubs.cpp
+++ b/driver/rsdRuntimeStubs.cpp
@@ -28,6 +28,8 @@
 #include "rsdRuntime.h"
 #include "rsdPath.h"
 #include "rsdAllocation.h"
+#include "rsdShaderCache.h"
+#include "rsdVertexArray.h"
 
 #include <time.h>
 
@@ -231,11 +233,33 @@
                                  float x3, float y3, float z3, float u3, float v3,
                                  float x4, float y4, float z4, float u4, float v4) {
     GET_TLS();
-    rsrDrawQuadTexCoords(rsc, sc,
-                         x1, y1, z1, u1, v1,
-                         x2, y2, z2, u2, v2,
-                         x3, y3, z3, u3, v3,
-                         x4, y4, z4, u4, v4);
+
+    if (!rsc->setupCheck()) {
+        return;
+    }
+
+    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
+    if (!dc->gl.shaderCache->setup(rsc)) {
+        return;
+    }
+
+    //ALOGE("Quad");
+    //ALOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
+    //ALOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
+    //ALOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
+    //ALOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
+
+    float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
+    const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
+
+    RsdVertexArray::Attrib attribs[2];
+    attribs[0].set(GL_FLOAT, 3, 12, false, (uint32_t)vtx, "ATTRIB_position");
+    attribs[1].set(GL_FLOAT, 2, 8, false, (uint32_t)tex, "ATTRIB_texture0");
+
+    RsdVertexArray va(attribs, 2);
+    va.setup(rsc);
+
+    RSD_CALL_GL(glDrawArrays, GL_TRIANGLE_FAN, 0, 4);
 }
 
 static void SC_DrawQuad(float x1, float y1, float z1,
@@ -243,17 +267,35 @@
                         float x3, float y3, float z3,
                         float x4, float y4, float z4) {
     GET_TLS();
-    rsrDrawQuad(rsc, sc, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4);
+    SC_DrawQuadTexCoords(x1, y1, z1, 0, 1,
+                         x2, y2, z2, 1, 1,
+                         x3, y3, z3, 1, 0,
+                         x4, y4, z4, 0, 0);
 }
 
 static void SC_DrawSpriteScreenspace(float x, float y, float z, float w, float h) {
     GET_TLS();
-    rsrDrawSpriteScreenspace(rsc, sc, x, y, z, w, h);
+
+    ObjectBaseRef<const ProgramVertex> tmp(rsc->getProgramVertex());
+    rsc->setProgramVertex(rsc->getDefaultProgramVertex());
+    //rsc->setupCheck();
+
+    //GLint crop[4] = {0, h, w, -h};
+
+    float sh = rsc->getHeight();
+
+    SC_DrawQuad(x,   sh - y,     z,
+                x+w, sh - y,     z,
+                x+w, sh - (y+h), z,
+                x,   sh - (y+h), z);
+    rsc->setProgramVertex((ProgramVertex *)tmp.get());
 }
 
 static void SC_DrawRect(float x1, float y1, float x2, float y2, float z) {
     GET_TLS();
-    rsrDrawRect(rsc, sc, x1, y1, x2, y2, z);
+
+    SC_DrawQuad(x1, y2, z, x2, y2, z, x2, y1, z, x1, y1, z);
+
 }
 
 static void SC_DrawPath(Path *p) {
diff --git a/rsContext.cpp b/rsContext.cpp
index a2d8129..803b73d 100644
--- a/rsContext.cpp
+++ b/rsContext.cpp
@@ -30,6 +30,8 @@
 
 #include <sys/syscall.h>
 
+#include <dlfcn.h>
+
 using namespace android;
 using namespace android::renderscript;
 
@@ -219,8 +221,35 @@
     rsc->props.mLogVisual = getProp("debug.rs.visual") != 0;
     rsc->props.mDebugMaxThreads = getProp("debug.rs.max-threads");
 
-    if (!rsdHalInit(rsc, 0, 0)) {
-        rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed initializing GL");
+    void *driverSO = dlopen("libRSDriver.so", RTLD_LAZY);
+    if (driverSO == NULL) {
+        rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed loading RS driver");
+        ALOGE("Failed loading RS driver: %s", dlerror());
+        return NULL;
+    }
+
+    // Need to call dlerror() to clear buffer before using it for dlsym().
+    (void) dlerror();
+    typedef bool (*HalSig)(Context*, uint32_t, uint32_t);
+    HalSig halInit = (HalSig) dlsym(driverSO, "rsdHalInit");
+
+    // If we can't find the C variant, we go looking for the C++ version.
+    if (halInit == NULL) {
+        ALOGW("Falling back to find C++ rsdHalInit: %s", dlerror());
+        halInit = (HalSig) dlsym(driverSO,
+                "_Z10rsdHalInitPN7android12renderscript7ContextEjj");
+    }
+
+    if (halInit == NULL) {
+        rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed to find rsdHalInit");
+        dlclose(driverSO);
+        ALOGE("Failed to find rsdHalInit: %s", dlerror());
+        return NULL;
+    }
+
+    if (!(*halInit)(rsc, 0, 0)) {
+        rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed initializing RS Driver");
+        dlclose(driverSO);
         ALOGE("Hal init failed");
         return NULL;
     }
diff --git a/rsRuntime.h b/rsRuntime.h
index eff691b..7a1d5e2 100644
--- a/rsRuntime.h
+++ b/rsRuntime.h
@@ -57,19 +57,6 @@
 // Drawing
 //////////////////////////////////////////////////////////////////////////////
 
-void rsrDrawQuadTexCoords(Context *, Script *,
-                          float x1, float y1, float z1, float u1, float v1,
-                          float x2, float y2, float z2, float u2, float v2,
-                          float x3, float y3, float z3, float u3, float v3,
-                          float x4, float y4, float z4, float u4, float v4);
-void rsrDrawQuad(Context *, Script *,
-                 float x1, float y1, float z1,
-                 float x2, float y2, float z2,
-                 float x3, float y3, float z3,
-                 float x4, float y4, float z4);
-void rsrDrawSpriteScreenspace(Context *, Script *,
-                              float x, float y, float z, float w, float h);
-void rsrDrawRect(Context *, Script *, float x1, float y1, float x2, float y2, float z);
 void rsrDrawPath(Context *, Script *, Path *);
 void rsrDrawMesh(Context *, Script *, Mesh *);
 void rsrDrawMeshPrimitive(Context *, Script *, Mesh *, uint32_t primIndex);
diff --git a/rsScriptC_LibGL.cpp b/rsScriptC_LibGL.cpp
index 6a897a3..63fb53e 100644
--- a/rsScriptC_LibGL.cpp
+++ b/rsScriptC_LibGL.cpp
@@ -147,72 +147,6 @@
 // Drawing
 //////////////////////////////////////////////////////////////////////////////
 
-void rsrDrawQuadTexCoords(Context *rsc, Script *sc,
-                          float x1, float y1, float z1, float u1, float v1,
-                          float x2, float y2, float z2, float u2, float v2,
-                          float x3, float y3, float z3, float u3, float v3,
-                          float x4, float y4, float z4, float u4, float v4) {
-    if (!rsc->setupCheck()) {
-        return;
-    }
-
-    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
-    if (!dc->gl.shaderCache->setup(rsc)) {
-        return;
-    }
-
-    //ALOGE("Quad");
-    //ALOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
-    //ALOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
-    //ALOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
-    //ALOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
-
-    float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
-    const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
-
-    RsdVertexArray::Attrib attribs[2];
-    attribs[0].set(GL_FLOAT, 3, 12, false, (uint32_t)vtx, "ATTRIB_position");
-    attribs[1].set(GL_FLOAT, 2, 8, false, (uint32_t)tex, "ATTRIB_texture0");
-
-    RsdVertexArray va(attribs, 2);
-    va.setup(rsc);
-
-    RSD_CALL_GL(glDrawArrays, GL_TRIANGLE_FAN, 0, 4);
-}
-
-void rsrDrawQuad(Context *rsc, Script *sc,
-                 float x1, float y1, float z1,
-                 float x2, float y2, float z2,
-                 float x3, float y3, float z3,
-                 float x4, float y4, float z4) {
-    rsrDrawQuadTexCoords(rsc, sc, x1, y1, z1, 0, 1,
-                                  x2, y2, z2, 1, 1,
-                                  x3, y3, z3, 1, 0,
-                                  x4, y4, z4, 0, 0);
-}
-
-void rsrDrawSpriteScreenspace(Context *rsc, Script *sc,
-                              float x, float y, float z, float w, float h) {
-    ObjectBaseRef<const ProgramVertex> tmp(rsc->getProgramVertex());
-    rsc->setProgramVertex(rsc->getDefaultProgramVertex());
-    //rsc->setupCheck();
-
-    //GLint crop[4] = {0, h, w, -h};
-
-    float sh = rsc->getHeight();
-
-    rsrDrawQuad(rsc, sc,
-                x,   sh - y,     z,
-                x+w, sh - y,     z,
-                x+w, sh - (y+h), z,
-                x,   sh - (y+h), z);
-    rsc->setProgramVertex((ProgramVertex *)tmp.get());
-}
-
-void rsrDrawRect(Context *rsc, Script *sc, float x1, float y1, float x2, float y2, float z) {
-    //ALOGE("SC_drawRect %f,%f  %f,%f  %f", x1, y1, x2, y2, z);
-    rsrDrawQuad(rsc, sc, x1, y2, z, x2, y2, z, x2, y1, z, x1, y1, z);
-}
 
 void rsrDrawPath(Context *rsc, Script *sc, Path *sm) {
     CHECK_OBJ(sm);
diff --git a/rs_hal.h b/rs_hal.h
index c521ef5..51f6327 100644
--- a/rs_hal.h
+++ b/rs_hal.h
@@ -264,8 +264,15 @@
 }
 }
 
+#ifdef __cplusplus
+extern "C" {
+#endif
 
-bool rsdHalInit(android::renderscript::Context *, uint32_t version_major, uint32_t version_minor);
+bool rsdHalInit(RsContext, uint32_t version_major, uint32_t version_minor);
+
+#ifdef __cplusplus
+}
+#endif
 
 #endif