1 /*
2  * Copyright 2016 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 #define LOG_TAG "GraphicsEnvironment"
18 
19 #include <vector>
20 
21 #include <graphicsenv/GraphicsEnv.h>
22 #include <nativehelper/ScopedUtfChars.h>
23 #include <nativeloader/native_loader.h>
24 #include "core_jni_helpers.h"
25 
26 namespace {
27 
isDebuggable_native()28 bool isDebuggable_native() {
29     return android::GraphicsEnv::getInstance().isDebuggable();
30 }
31 
setDriverPathAndSphalLibraries_native(JNIEnv * env,jobject clazz,jstring path,jstring sphalLibraries)32 void setDriverPathAndSphalLibraries_native(JNIEnv* env, jobject clazz, jstring path,
33                                            jstring sphalLibraries) {
34     ScopedUtfChars pathChars(env, path);
35     ScopedUtfChars sphalLibrariesChars(env, sphalLibraries);
36     android::GraphicsEnv::getInstance().setDriverPathAndSphalLibraries(pathChars.c_str(),
37                                                                        sphalLibrariesChars.c_str());
38 }
39 
setGpuStats_native(JNIEnv * env,jobject clazz,jstring driverPackageName,jstring driverVersionName,jlong driverVersionCode,jlong driverBuildTime,jstring appPackageName,jint vulkanVersion)40 void setGpuStats_native(JNIEnv* env, jobject clazz, jstring driverPackageName,
41                         jstring driverVersionName, jlong driverVersionCode,
42                         jlong driverBuildTime, jstring appPackageName, jint vulkanVersion) {
43     ScopedUtfChars driverPackageNameChars(env, driverPackageName);
44     ScopedUtfChars driverVersionNameChars(env, driverVersionName);
45     ScopedUtfChars appPackageNameChars(env, appPackageName);
46     android::GraphicsEnv::getInstance().setGpuStats(driverPackageNameChars.c_str(),
47                                                     driverVersionNameChars.c_str(),
48                                                     driverVersionCode, driverBuildTime,
49                                                     appPackageNameChars.c_str(), vulkanVersion);
50 }
51 
setAngleInfo_native(JNIEnv * env,jobject clazz,jstring path,jstring appName,jstring devOptIn,jobjectArray featuresObj)52 void setAngleInfo_native(JNIEnv* env, jobject clazz, jstring path, jstring appName,
53                          jstring devOptIn, jobjectArray featuresObj) {
54     ScopedUtfChars pathChars(env, path);
55     ScopedUtfChars appNameChars(env, appName);
56     ScopedUtfChars devOptInChars(env, devOptIn);
57 
58     std::vector<std::string> features;
59     if (featuresObj != nullptr) {
60         jsize length = env->GetArrayLength(featuresObj);
61         for (jsize i = 0; i < length; ++i) {
62             jstring jstr = static_cast<jstring>(env->GetObjectArrayElement(featuresObj, i));
63             // null entries are ignored
64             if (jstr == nullptr) {
65                 continue;
66             }
67             const char* cstr = env->GetStringUTFChars(jstr, nullptr);
68             if (cstr == nullptr) {
69                 continue;
70             }
71             features.emplace_back(cstr);
72             env->ReleaseStringUTFChars(jstr, cstr);
73         }
74     }
75 
76     android::GraphicsEnv::getInstance().setAngleInfo(pathChars.c_str(), appNameChars.c_str(),
77                                                      devOptInChars.c_str(), features);
78 }
79 
shouldUseAngle_native(JNIEnv * env,jobject clazz,jstring appName)80 bool shouldUseAngle_native(JNIEnv* env, jobject clazz, jstring appName) {
81     ScopedUtfChars appNameChars(env, appName);
82     return android::GraphicsEnv::getInstance().shouldUseAngle(appNameChars.c_str());
83 }
84 
setLayerPaths_native(JNIEnv * env,jobject clazz,jobject classLoader,jstring layerPaths)85 void setLayerPaths_native(JNIEnv* env, jobject clazz, jobject classLoader, jstring layerPaths) {
86     android::NativeLoaderNamespace* appNamespace = android::FindNativeLoaderNamespaceByClassLoader(
87         env, classLoader);
88     ScopedUtfChars layerPathsChars(env, layerPaths);
89     android::GraphicsEnv::getInstance().setLayerPaths(appNamespace, layerPathsChars.c_str());
90 }
91 
setDebugLayers_native(JNIEnv * env,jobject clazz,jstring layers)92 void setDebugLayers_native(JNIEnv* env, jobject clazz, jstring layers) {
93     if (layers != nullptr) {
94         ScopedUtfChars layersChars(env, layers);
95         android::GraphicsEnv::getInstance().setDebugLayers(layersChars.c_str());
96     }
97 }
98 
setDebugLayersGLES_native(JNIEnv * env,jobject clazz,jstring layers)99 void setDebugLayersGLES_native(JNIEnv* env, jobject clazz, jstring layers) {
100     if (layers != nullptr) {
101         ScopedUtfChars layersChars(env, layers);
102         android::GraphicsEnv::getInstance().setDebugLayersGLES(layersChars.c_str());
103     }
104 }
105 
setInjectLayersPrSetDumpable_native()106 bool setInjectLayersPrSetDumpable_native() {
107     return android::GraphicsEnv::getInstance().setInjectLayersPrSetDumpable();
108 }
109 
hintActivityLaunch_native(JNIEnv * env,jobject clazz)110 void hintActivityLaunch_native(JNIEnv* env, jobject clazz) {
111     android::GraphicsEnv::getInstance().hintActivityLaunch();
112 }
113 
114 const JNINativeMethod g_methods[] = {
115         {"isDebuggable", "()Z", reinterpret_cast<void*>(isDebuggable_native)},
116         {"setDriverPathAndSphalLibraries", "(Ljava/lang/String;Ljava/lang/String;)V",
117          reinterpret_cast<void*>(setDriverPathAndSphalLibraries_native)},
118         {"setGpuStats", "(Ljava/lang/String;Ljava/lang/String;JJLjava/lang/String;I)V",
119          reinterpret_cast<void*>(setGpuStats_native)},
120         {"setInjectLayersPrSetDumpable", "()Z",
121          reinterpret_cast<void*>(setInjectLayersPrSetDumpable_native)},
122         {"setAngleInfo",
123          "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;)V",
124          reinterpret_cast<void*>(setAngleInfo_native)},
125         {"getShouldUseAngle", "(Ljava/lang/String;)Z",
126          reinterpret_cast<void*>(shouldUseAngle_native)},
127         {"setLayerPaths", "(Ljava/lang/ClassLoader;Ljava/lang/String;)V",
128          reinterpret_cast<void*>(setLayerPaths_native)},
129         {"setDebugLayers", "(Ljava/lang/String;)V", reinterpret_cast<void*>(setDebugLayers_native)},
130         {"setDebugLayersGLES", "(Ljava/lang/String;)V",
131          reinterpret_cast<void*>(setDebugLayersGLES_native)},
132         {"hintActivityLaunch", "()V", reinterpret_cast<void*>(hintActivityLaunch_native)},
133 };
134 
135 const char* const kGraphicsEnvironmentName = "android/os/GraphicsEnvironment";
136 
137 } // anonymous namespace
138 
139 namespace android {
140 
register_android_os_GraphicsEnvironment(JNIEnv * env)141 int register_android_os_GraphicsEnvironment(JNIEnv* env) {
142     return RegisterMethodsOrDie(env, kGraphicsEnvironmentName, g_methods, NELEM(g_methods));
143 }
144 
145 } // namespace android
146