rsCacheDir.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright (C) 2017 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <android_runtime/AndroidRuntime.h>
  17. #include <log/log.h>
  18. #include <string>
  19. // Use reflection to query the default cache dir.
  20. // The function is NOT thread-safe. It is expected to be called
  21. // at most once per process.
  22. extern "C" const char* rsQueryCacheDir() {
  23. static std::string cacheDir;
  24. // First check if we have JavaVM running in this process.
  25. if (android::AndroidRuntime::getJavaVM()) {
  26. JNIEnv* env = android::AndroidRuntime::getJNIEnv();
  27. if (env) {
  28. jclass cacheDirClass = env->FindClass("android/renderscript/RenderScriptCacheDir");
  29. jfieldID cacheDirID = env->GetStaticFieldID(cacheDirClass, "mCacheDir", "Ljava/io/File;");
  30. jobject cache_dir = env->GetStaticObjectField(cacheDirClass, cacheDirID);
  31. if (cache_dir) {
  32. jclass fileClass = env->FindClass("java/io/File");
  33. jmethodID getPath = env->GetMethodID(fileClass, "getPath", "()Ljava/lang/String;");
  34. jstring path_string = (jstring)env->CallObjectMethod(cache_dir, getPath);
  35. const char *path_chars = env->GetStringUTFChars(path_string, NULL);
  36. ALOGD("Successfully queried cache dir: %s", path_chars);
  37. cacheDir = std::string(path_chars);
  38. env->ReleaseStringUTFChars(path_string, path_chars);
  39. } else {
  40. ALOGD("Cache dir not initialized");
  41. }
  42. } else {
  43. ALOGD("Failed to query the default cache dir.");
  44. }
  45. }
  46. return cacheDir.c_str();
  47. }