Attempt to create libRSDriver as shared library.

Change-Id: I047b32325efe2fa471b73c48fa7296beecc0c47d
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;
     }