Add user-defined cache path.

Change-Id: I7374d8e84d8dc6e74a6faa526c6e68ae11da6500
diff --git a/cpp/RenderScript.cpp b/cpp/RenderScript.cpp
index 1da31c7..3a75ffe 100644
--- a/cpp/RenderScript.cpp
+++ b/cpp/RenderScript.cpp
@@ -66,8 +66,8 @@
     }
 }
 
-bool RS::init(uint32_t flags) {
-    return RS::init(RS_VERSION, flags);
+bool RS::init(std::string name, uint32_t flags) {
+    return RS::init(name, RS_VERSION, flags);
 }
 
 static bool loadSymbols(void* handle) {
@@ -462,12 +462,19 @@
     return false;
 }
 
-bool RS::init(int targetApi, uint32_t flags) {
+bool RS::init(std::string &name, int targetApi, uint32_t flags) {
+    if (mInit) {
+        return true;
+    }
+
     if (initDispatch(targetApi) == false) {
         ALOGE("Couldn't initialize dispatch table");
         return false;
     }
 
+    mCacheDir = name;
+    mCacheDir += "/com.android.renderscript.cache/";
+
     mDev = RS::dispatch->DeviceCreate();
     if (mDev == 0) {
         ALOGE("Device creation failed");
diff --git a/cpp/ScriptC.cpp b/cpp/ScriptC.cpp
index 0d653bd..69d3bd5 100644
--- a/cpp/ScriptC.cpp
+++ b/cpp/ScriptC.cpp
@@ -25,6 +25,6 @@
                  const char *cacheDir, size_t cacheDirLength)
 : Script(NULL, rs) {
     mID = RS::dispatch->ScriptCCreate(rs->getContext(), cachedName, cachedNameLength,
-                                      cacheDir, cacheDirLength, (const char *)codeTxt, codeLength);
+                                      rs->mCacheDir.c_str(), rs->mCacheDir.length(), (const char *)codeTxt, codeLength);
 }
 
diff --git a/cpp/rsCppStructs.h b/cpp/rsCppStructs.h
index e45e2fb..0df201b 100644
--- a/cpp/rsCppStructs.h
+++ b/cpp/rsCppStructs.h
@@ -90,10 +90,12 @@
 
     /**
      * Initializes a RenderScript context. A context must be initialized before it can be used.
+     * @param[in] name Directory name to be used by this context. This should be equivalent to
+     * Context.getCacheDir().
      * @param[in] flags Optional flags for this context.
      * @return true on success
      */
-    bool init(uint32_t flags = 0);
+    bool init(std::string name, uint32_t flags = 0);
 
     /**
      * Sets the error handler function for this context. This error handler is
@@ -147,7 +149,7 @@
     static bool usingNative;
     static bool initDispatch(int targetApi);
 
-    bool init(int targetApi, uint32_t flags);
+    bool init(std::string &name, int targetApi, uint32_t flags);
     static void * threadProc(void *);
 
     static bool gInitialized;
@@ -165,6 +167,8 @@
     MessageHandlerFunc_t mMessageFunc;
     bool mInit;
 
+    std::string mCacheDir;
+
     struct {
         sp<const Element> U8;
         sp<const Element> U8_2;
@@ -246,6 +250,7 @@
     } mSamplers;
     friend class Sampler;
     friend class Element;
+    friend class ScriptC;
 };
 
  /**