diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2017-03-16 22:51:01 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2017-03-16 22:51:01 +0000 |
commit | 63e14e5542f9d77715e258459b1e6da9ba196e14 (patch) | |
tree | 7463aca713a980c566570f69d8751ce17423a7ce | |
parent | a505c9f5b5b9c144d359294f4ba0373b3183e7ce (diff) | |
parent | 10ad0772c14287f16131eed11ae3fafcd2c4a55b (diff) | |
download | rs-63e14e5542f9d77715e258459b1e6da9ba196e14.tar.gz |
Merge "Set the default cachedir during context create."
-rw-r--r-- | Android.mk | 2 | ||||
-rw-r--r-- | rsApiStubs.cpp | 41 | ||||
-rw-r--r-- | rsContext.cpp | 14 |
3 files changed, 48 insertions, 9 deletions
@@ -229,7 +229,7 @@ LOCAL_REQUIRED_MODULES := libRS_internal libRSDriver # Treble configuration LOCAL_SHARED_LIBRARIES += libhidlbase libhidltransport libhwbinder libutils android.hardware.renderscript@1.0 -LOCAL_SHARED_LIBRARIES += liblog libcutils +LOCAL_SHARED_LIBRARIES += liblog libcutils libandroid_runtime LOCAL_STATIC_LIBRARIES := \ libRSDispatch diff --git a/rsApiStubs.cpp b/rsApiStubs.cpp index bc08d7ae..feac2d1c 100644 --- a/rsApiStubs.cpp +++ b/rsApiStubs.cpp @@ -19,6 +19,8 @@ #include "rsFallbackAdaptation.h" #include "cpp/rsDispatch.h" +#include <android_runtime/AndroidRuntime.h> + // TODO: Figure out how to use different declared types for the two interfaces // to avoid the confusion. Currently, RsContext is used as the API type for // both the client interface and the dispatch table interface, but at the @@ -65,6 +67,10 @@ extern "C" int gDebuggerPresent = 0; // Context +// Mutex for locking reflection operation. +static std::mutex reflectionMutex; +// The defaultCacheDir will be reused if set, instead of query JNI. +static std::string defaultCacheDir; extern "C" RsContext rsContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion, RsContextType ct, uint32_t flags) { @@ -78,6 +84,41 @@ extern "C" RsContext rsContextCreate(RsDevice vdev, uint32_t version, uint32_t s } RsContextWrapper *ctxWrapper = new RsContextWrapper{context, instance.GetEntryFuncs()}; + + std::unique_lock<std::mutex> lock(reflectionMutex); + if (defaultCacheDir.size() == 0) { + // Use reflection to query the default cache dir. + // First check if we have JavaVM running in this process. + if (android::AndroidRuntime::getJavaVM()) { + JNIEnv* env = android::AndroidRuntime::getJNIEnv(); + if (env) { + jclass cacheDirClass = env->FindClass("android/renderscript/RenderScriptCacheDir"); + jfieldID cacheDirID = env->GetStaticFieldID(cacheDirClass, "mCacheDir", "Ljava/io/File;"); + jobject cache_dir = env->GetStaticObjectField(cacheDirClass, cacheDirID); + + jclass fileClass = env->FindClass("java/io/File"); + jmethodID getPath = env->GetMethodID(fileClass, "getPath", "()Ljava/lang/String;"); + jstring path_string = (jstring)env->CallObjectMethod(cache_dir, getPath); + const char *path_chars = env->GetStringUTFChars(path_string, NULL); + jsize path_length = env->GetStringUTFLength(path_string); + + ALOGD("Successfully queried cache dir: %s", path_chars); + defaultCacheDir = std::string(path_chars); + env->ReleaseStringUTFChars(path_string, path_chars); + } else { + ALOGE("Failed to set the default cache dir."); + } + } else { + ALOGD("Non JavaVM found in the process."); + } + } + + if (defaultCacheDir.size() > 0) { + ALOGD("Setting cache dir: %s", defaultCacheDir.c_str()); + rsContextSetCacheDir(ctxWrapper, + defaultCacheDir.c_str(), + defaultCacheDir.size()); + } return (RsContext) ctxWrapper; } diff --git a/rsContext.cpp b/rsContext.cpp index 5167e84a..da4262f6 100644 --- a/rsContext.cpp +++ b/rsContext.cpp @@ -458,14 +458,12 @@ Context::Context() { } void Context::setCacheDir(const char * cacheDir_arg, uint32_t length) { - if (!hasSetCacheDir) { - if (length <= PATH_MAX) { - memcpy(mCacheDir, cacheDir_arg, length); - mCacheDir[length] = 0; - hasSetCacheDir = true; - } else { - setError(RS_ERROR_BAD_VALUE, "Invalid path"); - } + if (length <= PATH_MAX) { + memcpy(mCacheDir, cacheDir_arg, length); + mCacheDir[length] = 0; + hasSetCacheDir = true; + } else { + setError(RS_ERROR_BAD_VALUE, "Invalid path"); } } |