summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiao Wang <miaowang@google.com>2017-03-14 18:58:02 -0700
committerMiao Wang <miaowang@google.com>2017-03-16 13:54:50 -0700
commit10ad0772c14287f16131eed11ae3fafcd2c4a55b (patch)
treed5e95935e191d70fde7a566fe00f10ee8c724bb1
parentee2aeb32af1bba3da2dc97d203a36e1c91c1edd6 (diff)
downloadrs-10ad0772c14287f16131eed11ae3fafcd2c4a55b.tar.gz
Set the default cachedir during context create.
- Use JNI reflection to access RenderScriptCacheDir.mCacheDir, and set it immediately after the context is created. - Java or C++ APIs could still call ContextSetCacheDir to override it. - The JNI query is guarded by std::unique_lock to make sure that the current thread is the only one attached to JNI. - This change ensures that vendors could always get a valid code cache dir even with support lib path, as RenderScriptCacheDir.mCacheDir is set by ActivityThread.java when launching the application, regardless of using RenderScript or not. Bug: 34396220 Test: mm, CTS tests pass, tested with RSTest_CompatLib to make sure that support lib path is covered. Change-Id: I542ed2fe03e34b72e85398271b748c0053a35ae3
-rw-r--r--Android.mk2
-rw-r--r--rsApiStubs.cpp41
-rw-r--r--rsContext.cpp14
3 files changed, 48 insertions, 9 deletions
diff --git a/Android.mk b/Android.mk
index 3e5f593f..238e6f23 100644
--- a/Android.mk
+++ b/Android.mk
@@ -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 fabfa6b2..21e84083 100644
--- a/rsContext.cpp
+++ b/rsContext.cpp
@@ -469,14 +469,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");
}
}