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 <graphicsenv/GraphicsEnv.h>
20 #include <nativehelper/ScopedUtfChars.h>
21 #include <nativeloader/native_loader.h>
22
23 #include <vector>
24
25 #include "core_jni_helpers.h"
26
27 namespace {
28
isDebuggable_native()29 bool isDebuggable_native() {
30 return android::GraphicsEnv::getInstance().isDebuggable();
31 }
32
setDriverPathAndSphalLibraries_native(JNIEnv * env,jobject clazz,jstring path,jstring sphalLibraries)33 void setDriverPathAndSphalLibraries_native(JNIEnv* env, jobject clazz, jstring path,
34 jstring sphalLibraries) {
35 ScopedUtfChars pathChars(env, path);
36 ScopedUtfChars sphalLibrariesChars(env, sphalLibraries);
37 android::GraphicsEnv::getInstance().setDriverPathAndSphalLibraries(pathChars.c_str(),
38 sphalLibrariesChars.c_str());
39 }
40
setGpuStats_native(JNIEnv * env,jobject clazz,jstring driverPackageName,jstring driverVersionName,jlong driverVersionCode,jlong driverBuildTime,jstring appPackageName,jint vulkanVersion)41 void setGpuStats_native(JNIEnv* env, jobject clazz, jstring driverPackageName,
42 jstring driverVersionName, jlong driverVersionCode,
43 jlong driverBuildTime, jstring appPackageName, jint vulkanVersion) {
44 ScopedUtfChars driverPackageNameChars(env, driverPackageName);
45 ScopedUtfChars driverVersionNameChars(env, driverVersionName);
46 ScopedUtfChars appPackageNameChars(env, appPackageName);
47 android::GraphicsEnv::getInstance().setGpuStats(driverPackageNameChars.c_str(),
48 driverVersionNameChars.c_str(),
49 driverVersionCode, driverBuildTime,
50 appPackageNameChars.c_str(), vulkanVersion);
51 }
52
setAngleInfo_native(JNIEnv * env,jobject clazz,jstring path,jboolean useNativeDriver,jstring packageName,jobjectArray featuresObj)53 void setAngleInfo_native(JNIEnv* env, jobject clazz, jstring path, jboolean useNativeDriver,
54 jstring packageName, jobjectArray featuresObj) {
55 ScopedUtfChars pathChars(env, path);
56 ScopedUtfChars packageNameChars(env, packageName);
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(), useNativeDriver,
77 packageNameChars.c_str(), features);
78 }
79
setLayerPaths_native(JNIEnv * env,jobject clazz,jobject classLoader,jstring layerPaths)80 void setLayerPaths_native(JNIEnv* env, jobject clazz, jobject classLoader, jstring layerPaths) {
81 android::NativeLoaderNamespace* appNamespace = android::FindNativeLoaderNamespaceByClassLoader(
82 env, classLoader);
83 ScopedUtfChars layerPathsChars(env, layerPaths);
84 android::GraphicsEnv::getInstance().setLayerPaths(appNamespace, layerPathsChars.c_str());
85 }
86
setDebugLayers_native(JNIEnv * env,jobject clazz,jstring layers)87 void setDebugLayers_native(JNIEnv* env, jobject clazz, jstring layers) {
88 if (layers != nullptr) {
89 ScopedUtfChars layersChars(env, layers);
90 android::GraphicsEnv::getInstance().setDebugLayers(layersChars.c_str());
91 }
92 }
93
setDebugLayersGLES_native(JNIEnv * env,jobject clazz,jstring layers)94 void setDebugLayersGLES_native(JNIEnv* env, jobject clazz, jstring layers) {
95 if (layers != nullptr) {
96 ScopedUtfChars layersChars(env, layers);
97 android::GraphicsEnv::getInstance().setDebugLayersGLES(layersChars.c_str());
98 }
99 }
100
setInjectLayersPrSetDumpable_native()101 bool setInjectLayersPrSetDumpable_native() {
102 return android::GraphicsEnv::getInstance().setInjectLayersPrSetDumpable();
103 }
104
hintActivityLaunch_native(JNIEnv * env,jobject clazz)105 void hintActivityLaunch_native(JNIEnv* env, jobject clazz) {
106 android::GraphicsEnv::getInstance().hintActivityLaunch();
107 }
108
nativeToggleAngleAsSystemDriver_native(JNIEnv * env,jobject clazz,jboolean enabled)109 void nativeToggleAngleAsSystemDriver_native(JNIEnv* env, jobject clazz, jboolean enabled) {
110 android::GraphicsEnv::getInstance().nativeToggleAngleAsSystemDriver(enabled);
111 }
112
113 const JNINativeMethod g_methods[] = {
114 {"isDebuggable", "()Z", reinterpret_cast<void*>(isDebuggable_native)},
115 {"setDriverPathAndSphalLibraries", "(Ljava/lang/String;Ljava/lang/String;)V",
116 reinterpret_cast<void*>(setDriverPathAndSphalLibraries_native)},
117 {"setGpuStats", "(Ljava/lang/String;Ljava/lang/String;JJLjava/lang/String;I)V",
118 reinterpret_cast<void*>(setGpuStats_native)},
119 {"setInjectLayersPrSetDumpable", "()Z",
120 reinterpret_cast<void*>(setInjectLayersPrSetDumpable_native)},
121 {"nativeSetAngleInfo", "(Ljava/lang/String;ZLjava/lang/String;[Ljava/lang/String;)V",
122 reinterpret_cast<void*>(setAngleInfo_native)},
123 {"setLayerPaths", "(Ljava/lang/ClassLoader;Ljava/lang/String;)V",
124 reinterpret_cast<void*>(setLayerPaths_native)},
125 {"setDebugLayers", "(Ljava/lang/String;)V", reinterpret_cast<void*>(setDebugLayers_native)},
126 {"setDebugLayersGLES", "(Ljava/lang/String;)V",
127 reinterpret_cast<void*>(setDebugLayersGLES_native)},
128 {"hintActivityLaunch", "()V", reinterpret_cast<void*>(hintActivityLaunch_native)},
129 {"nativeToggleAngleAsSystemDriver", "(Z)V",
130 reinterpret_cast<void*>(nativeToggleAngleAsSystemDriver_native)},
131 };
132
133 const char* const kGraphicsEnvironmentName = "android/os/GraphicsEnvironment";
134
135 } // anonymous namespace
136
137 namespace android {
138
register_android_os_GraphicsEnvironment(JNIEnv * env)139 int register_android_os_GraphicsEnvironment(JNIEnv* env) {
140 return RegisterMethodsOrDie(env, kGraphicsEnvironmentName, g_methods, NELEM(g_methods));
141 }
142
143 } // namespace android
144