Home | History | Annotate | Download | only in rs
      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 
     17 #include <android_runtime/AndroidRuntime.h>
     18 #include <log/log.h>
     19 
     20 #include <string>
     21 
     22 // Use reflection to query the default cache dir.
     23 // The function is NOT thread-safe. It is expected to be called
     24 // at most once per process.
     25 extern "C" const char* rsQueryCacheDir() {
     26     static std::string cacheDir;
     27     // First check if we have JavaVM running in this process.
     28     if (android::AndroidRuntime::getJavaVM()) {
     29         JNIEnv* env = android::AndroidRuntime::getJNIEnv();
     30         if (env) {
     31             jclass cacheDirClass = env->FindClass("android/renderscript/RenderScriptCacheDir");
     32             jfieldID cacheDirID = env->GetStaticFieldID(cacheDirClass, "mCacheDir", "Ljava/io/File;");
     33             jobject cache_dir = env->GetStaticObjectField(cacheDirClass, cacheDirID);
     34 
     35             if (cache_dir) {
     36                 jclass fileClass = env->FindClass("java/io/File");
     37                 jmethodID getPath = env->GetMethodID(fileClass, "getPath", "()Ljava/lang/String;");
     38                 jstring path_string = (jstring)env->CallObjectMethod(cache_dir, getPath);
     39                 const char *path_chars = env->GetStringUTFChars(path_string, NULL);
     40 
     41                 ALOGD("Successfully queried cache dir: %s", path_chars);
     42                 cacheDir = std::string(path_chars);
     43                 env->ReleaseStringUTFChars(path_string, path_chars);
     44             } else {
     45                 ALOGD("Cache dir not initialized");
     46             }
     47         } else {
     48             ALOGD("Failed to query the default cache dir.");
     49         }
     50     }
     51 
     52     return cacheDir.c_str();
     53 }
     54 
     55