Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 18 | |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 19 | //#define LOG_NDEBUG 1 |
| 20 | #define LOG_TAG "GraphicsEnv" |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 21 | |
Jiyong Park | 27c39e1 | 2017-05-08 13:00:02 +0900 | [diff] [blame] | 22 | #include <graphicsenv/GraphicsEnv.h> |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 23 | |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 24 | #include <dlfcn.h> |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 25 | #include <unistd.h> |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 26 | |
| 27 | #include <android-base/file.h> |
| 28 | #include <android-base/properties.h> |
| 29 | #include <android-base/strings.h> |
| 30 | #include <android/dlext.h> |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 31 | #include <binder/IServiceManager.h> |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 32 | #include <graphicsenv/IGpuService.h> |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 33 | #include <log/log.h> |
Yiwei Zhang | 49b9ac7 | 2019-08-05 16:57:17 -0700 | [diff] [blame] | 34 | #include <nativeloader/dlext_namespaces.h> |
Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 35 | #include <sys/prctl.h> |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 36 | #include <utils/Trace.h> |
Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 37 | |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 38 | #include <memory> |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 39 | #include <string> |
Yiwei Zhang | 3c74da9 | 2019-06-28 10:16:49 -0700 | [diff] [blame] | 40 | #include <thread> |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 41 | |
Peiyong Lin | 0acbfdc | 2020-06-17 18:47:12 -0700 | [diff] [blame] | 42 | // TODO(b/159240322): Extend this to x86 ABI. |
| 43 | #if defined(__LP64__) |
| 44 | #define UPDATABLE_DRIVER_ABI "arm64-v8a" |
| 45 | #else |
| 46 | #define UPDATABLE_DRIVER_ABI "armeabi-v7a" |
| 47 | #endif // defined(__LP64__) |
| 48 | |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 49 | // TODO(ianelliott@): Get the following from an ANGLE header: |
| 50 | #define CURRENT_ANGLE_API_VERSION 2 // Current API verion we are targetting |
| 51 | // Version-2 API: |
| 52 | typedef bool (*fpANGLEGetFeatureSupportUtilAPIVersion)(unsigned int* versionToUse); |
| 53 | typedef bool (*fpANGLEAndroidParseRulesString)(const char* rulesString, void** rulesHandle, |
| 54 | int* rulesVersion); |
| 55 | typedef bool (*fpANGLEGetSystemInfo)(void** handle); |
| 56 | typedef bool (*fpANGLEAddDeviceInfoToSystemInfo)(const char* deviceMfr, const char* deviceModel, |
| 57 | void* handle); |
| 58 | typedef bool (*fpANGLEShouldBeUsedForApplication)(void* rulesHandle, int rulesVersion, |
| 59 | void* systemInfoHandle, const char* appName); |
| 60 | typedef bool (*fpANGLEFreeRulesHandle)(void* handle); |
| 61 | typedef bool (*fpANGLEFreeSystemInfoHandle)(void* handle); |
| 62 | |
Kiyoung Kim | c0e3ed3 | 2023-08-11 11:22:15 +0900 | [diff] [blame] | 63 | namespace { |
| 64 | static bool isVndkEnabled() { |
| 65 | #ifdef __BIONIC__ |
Kiyoung Kim | 77f3709 | 2023-09-26 10:41:47 +0900 | [diff] [blame] | 66 | static bool isVndkEnabled = android::base::GetProperty("ro.vndk.version", "") != ""; |
Kiyoung Kim | c0e3ed3 | 2023-08-11 11:22:15 +0900 | [diff] [blame] | 67 | return isVndkEnabled; |
| 68 | #endif |
| 69 | return false; |
| 70 | } |
| 71 | } // namespace |
| 72 | |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 73 | namespace android { |
| 74 | |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 75 | enum NativeLibrary { |
| 76 | LLNDK = 0, |
| 77 | VNDKSP = 1, |
| 78 | }; |
| 79 | |
Jooyung Han | 7839680 | 2020-02-23 03:02:43 +0900 | [diff] [blame] | 80 | static constexpr const char* kNativeLibrariesSystemConfigPath[] = |
| 81 | {"/apex/com.android.vndk.v{}/etc/llndk.libraries.{}.txt", |
| 82 | "/apex/com.android.vndk.v{}/etc/vndksp.libraries.{}.txt"}; |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 83 | |
Kiyoung Kim | c0e3ed3 | 2023-08-11 11:22:15 +0900 | [diff] [blame] | 84 | static const char* kLlndkLibrariesTxtPath = "/system/etc/llndk.libraries.txt"; |
| 85 | |
Chris Forbes | 0cdac9a | 2024-08-16 15:12:27 +1200 | [diff] [blame] | 86 | // List of libraries that were previously available via VNDK-SP, |
| 87 | // and are now available via SPHAL. |
| 88 | // On modern devices that lack the VNDK APEX, the device no longer |
| 89 | // contains a helpful list of these libraries on the filesystem as above. |
| 90 | // See system/sepolicy/vendor/file_contexts |
| 91 | static const char* kFormerlyVndkspLibrariesList = |
| 92 | "android.hardware.common-V2-ndk.so:" |
| 93 | "android.hardware.common.fmq-V1-ndk.so:" |
| 94 | "android.hardware.graphics.allocator-V2-ndk.so:" |
Eino-Ville Talvala | eb94bdf | 2024-09-19 14:55:16 -0700 | [diff] [blame] | 95 | "android.hardware.graphics.common-V6-ndk.so:" |
Chris Forbes | 0cdac9a | 2024-08-16 15:12:27 +1200 | [diff] [blame] | 96 | "android.hardware.graphics.common@1.0.so:" |
| 97 | "android.hardware.graphics.common@1.1.so:" |
| 98 | "android.hardware.graphics.common@1.2.so:" |
| 99 | "android.hardware.graphics.composer3-V1-ndk.so:" |
| 100 | "android.hardware.graphics.mapper@2.0.so:" |
| 101 | "android.hardware.graphics.mapper@2.1.so:" |
| 102 | "android.hardware.graphics.mapper@3.0.so:" |
| 103 | "android.hardware.graphics.mapper@4.0.so:" |
| 104 | "android.hardware.renderscript@1.0.so:" |
| 105 | "android.hidl.memory.token@1.0.so:" |
| 106 | "android.hidl.memory@1.0-impl.so:" |
| 107 | "android.hidl.memory@1.0.so:" |
| 108 | "android.hidl.safe_union@1.0.so:" |
| 109 | "libRSCpuRef.so:" |
| 110 | "libRSDriver.so:" |
| 111 | "libRS_internal.so:" |
| 112 | "libbacktrace.so:" |
| 113 | "libbase.so:" |
| 114 | "libbcinfo.so:" |
| 115 | "libblas.so:" |
| 116 | "libc++.so:" |
| 117 | "libcompiler_rt.so:" |
| 118 | "libcutils.so:" |
| 119 | "libdmabufheap.so:" |
| 120 | "libft2.so:" |
| 121 | "libgralloctypes.so:" |
| 122 | "libhardware.so:" |
| 123 | "libhidlbase.so:" |
| 124 | "libhidlmemory.so:" |
| 125 | "libion.so:" |
| 126 | "libjsoncpp.so:" |
| 127 | "liblzma.so:" |
| 128 | "libpng.so:" |
| 129 | "libprocessgroup.so:" |
| 130 | "libunwindstack.so:" |
| 131 | "libutils.so:" |
| 132 | "libutilscallstack.so:" |
| 133 | "libz.so"; |
| 134 | |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 135 | static std::string vndkVersionStr() { |
| 136 | #ifdef __BIONIC__ |
Michael Hoisie | d1023f2 | 2020-03-26 22:52:58 -0700 | [diff] [blame] | 137 | return base::GetProperty("ro.vndk.version", ""); |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 138 | #endif |
| 139 | return ""; |
| 140 | } |
| 141 | |
| 142 | static void insertVndkVersionStr(std::string* fileName) { |
| 143 | LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr"); |
Jooyung Han | 7839680 | 2020-02-23 03:02:43 +0900 | [diff] [blame] | 144 | std::string version = vndkVersionStr(); |
| 145 | size_t pos = fileName->find("{}"); |
| 146 | while (pos != std::string::npos) { |
| 147 | fileName->replace(pos, 2, version); |
| 148 | pos = fileName->find("{}", pos + version.size()); |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 149 | } |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) { |
| 153 | // Read list of public native libraries from the config file. |
| 154 | std::string fileContent; |
| 155 | if (!base::ReadFileToString(configFile, &fileContent)) { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | std::vector<std::string> lines = base::Split(fileContent, "\n"); |
| 160 | |
| 161 | for (auto& line : lines) { |
| 162 | auto trimmedLine = base::Trim(line); |
| 163 | if (!trimmedLine.empty()) { |
| 164 | soNames->push_back(trimmedLine); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | static const std::string getSystemNativeLibraries(NativeLibrary type) { |
Kiyoung Kim | c0e3ed3 | 2023-08-11 11:22:15 +0900 | [diff] [blame] | 172 | std::string nativeLibrariesSystemConfig = ""; |
| 173 | |
Chris Forbes | 0cdac9a | 2024-08-16 15:12:27 +1200 | [diff] [blame] | 174 | if (!isVndkEnabled()) { |
| 175 | if (type == NativeLibrary::VNDKSP) { |
| 176 | return kFormerlyVndkspLibrariesList; |
| 177 | } else { |
| 178 | nativeLibrariesSystemConfig = kLlndkLibrariesTxtPath; |
| 179 | } |
Kiyoung Kim | c0e3ed3 | 2023-08-11 11:22:15 +0900 | [diff] [blame] | 180 | } else { |
| 181 | nativeLibrariesSystemConfig = kNativeLibrariesSystemConfigPath[type]; |
| 182 | insertVndkVersionStr(&nativeLibrariesSystemConfig); |
| 183 | } |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 184 | |
| 185 | std::vector<std::string> soNames; |
| 186 | if (!readConfig(nativeLibrariesSystemConfig, &soNames)) { |
| 187 | ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str()); |
| 188 | return ""; |
| 189 | } |
| 190 | |
| 191 | return base::Join(soNames, ':'); |
| 192 | } |
| 193 | |
Peiyong Lin | ef20e4e | 2023-07-03 02:33:00 +0000 | [diff] [blame] | 194 | static sp<IGpuService> getGpuService() { |
| 195 | static const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu")); |
| 196 | if (!binder) { |
| 197 | ALOGE("Failed to get gpu service"); |
| 198 | return nullptr; |
| 199 | } |
| 200 | |
| 201 | return interface_cast<IGpuService>(binder); |
| 202 | } |
| 203 | |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 204 | /*static*/ GraphicsEnv& GraphicsEnv::getInstance() { |
| 205 | static GraphicsEnv env; |
| 206 | return env; |
| 207 | } |
| 208 | |
Yiwei Zhang | 6a674c9 | 2019-11-08 11:55:36 -0800 | [diff] [blame] | 209 | bool GraphicsEnv::isDebuggable() { |
Cody Northrop | 383832f | 2022-11-01 13:54:57 -0600 | [diff] [blame] | 210 | // This flag determines if the application is marked debuggable |
| 211 | bool appDebuggable = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) > 0; |
| 212 | |
| 213 | // This flag is set only in `debuggable` builds of the platform |
| 214 | #if defined(ANDROID_DEBUGGABLE) |
| 215 | bool platformDebuggable = true; |
| 216 | #else |
| 217 | bool platformDebuggable = false; |
| 218 | #endif |
| 219 | |
| 220 | ALOGV("GraphicsEnv::isDebuggable returning appDebuggable=%s || platformDebuggable=%s", |
| 221 | appDebuggable ? "true" : "false", platformDebuggable ? "true" : "false"); |
| 222 | |
| 223 | return appDebuggable || platformDebuggable; |
Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 224 | } |
| 225 | |
Peiyong Lin | ef20e4e | 2023-07-03 02:33:00 +0000 | [diff] [blame] | 226 | /** |
| 227 | * APIs for updatable graphics drivers |
| 228 | */ |
| 229 | |
Peiyong Lin | 2f51e75 | 2023-06-15 22:35:14 +0000 | [diff] [blame] | 230 | void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string& path, |
| 231 | const std::string& sphalLibraries) { |
Yiwei Zhang | 6ef8494 | 2019-02-14 12:28:12 -0800 | [diff] [blame] | 232 | if (!mDriverPath.empty() || !mSphalLibraries.empty()) { |
| 233 | ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries " |
| 234 | "from '%s' to '%s'", |
| 235 | mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str()); |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 236 | return; |
| 237 | } |
Yiwei Zhang | 6ef8494 | 2019-02-14 12:28:12 -0800 | [diff] [blame] | 238 | ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(), |
| 239 | sphalLibraries.c_str()); |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 240 | mDriverPath = path; |
Yiwei Zhang | 6ef8494 | 2019-02-14 12:28:12 -0800 | [diff] [blame] | 241 | mSphalLibraries = sphalLibraries; |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Peiyong Lin | ef20e4e | 2023-07-03 02:33:00 +0000 | [diff] [blame] | 244 | // Return true if all the required libraries from vndk and sphal namespace are |
| 245 | // linked to the driver namespace correctly. |
| 246 | bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* destNamespace, |
| 247 | android_namespace_t* vndkNamespace, |
| 248 | const std::string& sharedSphalLibraries) { |
| 249 | const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK); |
| 250 | if (llndkLibraries.empty()) { |
| 251 | return false; |
| 252 | } |
| 253 | if (!android_link_namespaces(destNamespace, nullptr, llndkLibraries.c_str())) { |
| 254 | ALOGE("Failed to link default namespace[%s]", dlerror()); |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP); |
| 259 | if (vndkspLibraries.empty()) { |
| 260 | return false; |
| 261 | } |
| 262 | if (!android_link_namespaces(destNamespace, vndkNamespace, vndkspLibraries.c_str())) { |
| 263 | ALOGE("Failed to link vndk namespace[%s]", dlerror()); |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | if (sharedSphalLibraries.empty()) { |
| 268 | return true; |
| 269 | } |
| 270 | |
| 271 | // Make additional libraries in sphal to be accessible |
| 272 | auto sphalNamespace = android_get_exported_namespace("sphal"); |
| 273 | if (!sphalNamespace) { |
| 274 | ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace", |
| 275 | sharedSphalLibraries.c_str()); |
| 276 | return false; |
| 277 | } |
| 278 | |
| 279 | if (!android_link_namespaces(destNamespace, sphalNamespace, sharedSphalLibraries.c_str())) { |
| 280 | ALOGE("Failed to link sphal namespace[%s]", dlerror()); |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | return true; |
| 285 | } |
| 286 | |
| 287 | android_namespace_t* GraphicsEnv::getDriverNamespace() { |
| 288 | std::lock_guard<std::mutex> lock(mNamespaceMutex); |
| 289 | |
| 290 | if (mDriverNamespace) { |
| 291 | return mDriverNamespace; |
| 292 | } |
| 293 | |
| 294 | if (mDriverPath.empty()) { |
| 295 | // For an application process, driver path is empty means this application is not opted in |
| 296 | // to use updatable driver. Application process doesn't have the ability to set up |
| 297 | // environment variables and hence before `getenv` call will return. |
| 298 | // For a process that is not an application process, if it's run from an environment, |
| 299 | // for example shell, where environment variables can be set, then it can opt into using |
| 300 | // udpatable driver by setting UPDATABLE_GFX_DRIVER to 1. By setting to 1 the developer |
| 301 | // driver will be used currently. |
| 302 | // TODO(b/159240322) Support the production updatable driver. |
| 303 | const char* id = getenv("UPDATABLE_GFX_DRIVER"); |
| 304 | if (id == nullptr || std::strcmp(id, "1") != 0) { |
| 305 | return nullptr; |
| 306 | } |
| 307 | const sp<IGpuService> gpuService = getGpuService(); |
| 308 | if (!gpuService) { |
| 309 | return nullptr; |
| 310 | } |
| 311 | mDriverPath = gpuService->getUpdatableDriverPath(); |
| 312 | if (mDriverPath.empty()) { |
| 313 | return nullptr; |
| 314 | } |
| 315 | mDriverPath.append(UPDATABLE_DRIVER_ABI); |
| 316 | ALOGI("Driver path is setup via UPDATABLE_GFX_DRIVER: %s", mDriverPath.c_str()); |
| 317 | } |
| 318 | |
Chris Forbes | 0cdac9a | 2024-08-16 15:12:27 +1200 | [diff] [blame] | 319 | auto vndkNamespace = android_get_exported_namespace(isVndkEnabled() ? "vndk" : "sphal"); |
Peiyong Lin | ef20e4e | 2023-07-03 02:33:00 +0000 | [diff] [blame] | 320 | if (!vndkNamespace) { |
Ethan Chen | f5da7ee | 2024-01-18 19:47:01 -0800 | [diff] [blame] | 321 | mDriverNamespace = nullptr; |
| 322 | return mDriverNamespace; |
Peiyong Lin | ef20e4e | 2023-07-03 02:33:00 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | mDriverNamespace = android_create_namespace("updatable gfx driver", |
| 326 | mDriverPath.c_str(), // ld_library_path |
| 327 | mDriverPath.c_str(), // default_library_path |
| 328 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 329 | nullptr, // permitted_when_isolated_path |
| 330 | nullptr); |
| 331 | |
| 332 | if (!linkDriverNamespaceLocked(mDriverNamespace, vndkNamespace, mSphalLibraries)) { |
| 333 | mDriverNamespace = nullptr; |
| 334 | } |
| 335 | |
| 336 | return mDriverNamespace; |
| 337 | } |
| 338 | |
| 339 | std::string GraphicsEnv::getDriverPath() const { |
| 340 | return mDriverPath; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * APIs for GpuStats |
| 345 | */ |
| 346 | |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 347 | void GraphicsEnv::hintActivityLaunch() { |
| 348 | ATRACE_CALL(); |
| 349 | |
Yiwei Zhang | d381938 | 2020-01-07 19:53:56 -0800 | [diff] [blame] | 350 | { |
| 351 | std::lock_guard<std::mutex> lock(mStatsLock); |
| 352 | if (mActivityLaunched) return; |
| 353 | mActivityLaunched = true; |
| 354 | } |
| 355 | |
Yiwei Zhang | 3c74da9 | 2019-06-28 10:16:49 -0700 | [diff] [blame] | 356 | std::thread trySendGpuStatsThread([this]() { |
| 357 | // If there's already graphics driver preloaded in the process, just send |
| 358 | // the stats info to GpuStats directly through async binder. |
| 359 | std::lock_guard<std::mutex> lock(mStatsLock); |
| 360 | if (mGpuStats.glDriverToSend) { |
| 361 | mGpuStats.glDriverToSend = false; |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 362 | sendGpuStatsLocked(GpuStatsInfo::Api::API_GL, true, mGpuStats.glDriverLoadingTime); |
Yiwei Zhang | 3c74da9 | 2019-06-28 10:16:49 -0700 | [diff] [blame] | 363 | } |
| 364 | if (mGpuStats.vkDriverToSend) { |
| 365 | mGpuStats.vkDriverToSend = false; |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 366 | sendGpuStatsLocked(GpuStatsInfo::Api::API_VK, true, mGpuStats.vkDriverLoadingTime); |
Yiwei Zhang | 3c74da9 | 2019-06-28 10:16:49 -0700 | [diff] [blame] | 367 | } |
| 368 | }); |
| 369 | trySendGpuStatsThread.detach(); |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Greg Kaiser | 210bb7e | 2019-02-12 12:40:05 -0800 | [diff] [blame] | 372 | void GraphicsEnv::setGpuStats(const std::string& driverPackageName, |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 373 | const std::string& driverVersionName, uint64_t driverVersionCode, |
Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 374 | int64_t driverBuildTime, const std::string& appPackageName, |
| 375 | const int vulkanVersion) { |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 376 | ATRACE_CALL(); |
| 377 | |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 378 | std::lock_guard<std::mutex> lock(mStatsLock); |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 379 | ALOGV("setGpuStats:\n" |
| 380 | "\tdriverPackageName[%s]\n" |
| 381 | "\tdriverVersionName[%s]\n" |
Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 382 | "\tdriverVersionCode[%" PRIu64 "]\n" |
| 383 | "\tdriverBuildTime[%" PRId64 "]\n" |
Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 384 | "\tappPackageName[%s]\n" |
| 385 | "\tvulkanVersion[%d]\n", |
Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 386 | driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime, |
Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 387 | appPackageName.c_str(), vulkanVersion); |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 388 | |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 389 | mGpuStats.driverPackageName = driverPackageName; |
| 390 | mGpuStats.driverVersionName = driverVersionName; |
| 391 | mGpuStats.driverVersionCode = driverVersionCode; |
Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 392 | mGpuStats.driverBuildTime = driverBuildTime; |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 393 | mGpuStats.appPackageName = appPackageName; |
Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 394 | mGpuStats.vulkanVersion = vulkanVersion; |
Yiwei Zhang | 8c8c181 | 2019-02-04 18:56:38 -0800 | [diff] [blame] | 395 | } |
| 396 | |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 397 | void GraphicsEnv::setDriverToLoad(GpuStatsInfo::Driver driver) { |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 398 | ATRACE_CALL(); |
| 399 | |
| 400 | std::lock_guard<std::mutex> lock(mStatsLock); |
| 401 | switch (driver) { |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 402 | case GpuStatsInfo::Driver::GL: |
| 403 | case GpuStatsInfo::Driver::GL_UPDATED: |
Solti | 371af85 | 2024-08-22 21:22:19 +0000 | [diff] [blame] | 404 | case GpuStatsInfo::Driver::ANGLE: |
| 405 | mGpuStats.glDriverToLoad = driver; |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 406 | break; |
Solti | 371af85 | 2024-08-22 21:22:19 +0000 | [diff] [blame] | 407 | |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 408 | case GpuStatsInfo::Driver::VULKAN: |
| 409 | case GpuStatsInfo::Driver::VULKAN_UPDATED: { |
Yiwei Zhang | 472cab0 | 2019-08-05 17:57:41 -0700 | [diff] [blame] | 410 | if (mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::NONE || |
| 411 | mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::VULKAN) { |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 412 | mGpuStats.vkDriverToLoad = driver; |
| 413 | break; |
| 414 | } |
| 415 | |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 416 | if (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE) { |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 417 | mGpuStats.vkDriverFallback = driver; |
| 418 | } |
| 419 | break; |
| 420 | } |
| 421 | default: |
| 422 | break; |
| 423 | } |
| 424 | } |
| 425 | |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 426 | void GraphicsEnv::setDriverLoaded(GpuStatsInfo::Api api, bool isDriverLoaded, |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 427 | int64_t driverLoadingTime) { |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 428 | ATRACE_CALL(); |
| 429 | |
| 430 | std::lock_guard<std::mutex> lock(mStatsLock); |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 431 | if (api == GpuStatsInfo::Api::API_GL) { |
Yiwei Zhang | d381938 | 2020-01-07 19:53:56 -0800 | [diff] [blame] | 432 | mGpuStats.glDriverToSend = true; |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 433 | mGpuStats.glDriverLoadingTime = driverLoadingTime; |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 434 | } else { |
Yiwei Zhang | d381938 | 2020-01-07 19:53:56 -0800 | [diff] [blame] | 435 | mGpuStats.vkDriverToSend = true; |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 436 | mGpuStats.vkDriverLoadingTime = driverLoadingTime; |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 439 | sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime); |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Serdar Kocdemir | b2901c9 | 2022-11-17 00:39:05 +0000 | [diff] [blame] | 442 | // Hash function to calculate hash for null-terminated Vulkan extension names |
| 443 | // We store hash values of the extensions, rather than the actual names or |
| 444 | // indices to be able to support new extensions easily, avoid creating |
| 445 | // a table of 'known' extensions inside Android and reduce the runtime overhead. |
| 446 | static uint64_t calculateExtensionHash(const char* word) { |
| 447 | if (!word) { |
| 448 | return 0; |
| 449 | } |
| 450 | const size_t wordLen = strlen(word); |
| 451 | const uint32_t seed = 167; |
| 452 | uint64_t hash = 0; |
| 453 | for (size_t i = 0; i < wordLen; i++) { |
| 454 | hash = (hash * seed) + word[i]; |
| 455 | } |
| 456 | return hash; |
| 457 | } |
| 458 | |
| 459 | void GraphicsEnv::setVulkanInstanceExtensions(uint32_t enabledExtensionCount, |
| 460 | const char* const* ppEnabledExtensionNames) { |
| 461 | ATRACE_CALL(); |
| 462 | if (enabledExtensionCount == 0 || ppEnabledExtensionNames == nullptr) { |
| 463 | return; |
| 464 | } |
| 465 | |
| 466 | const uint32_t maxNumStats = android::GpuStatsAppInfo::MAX_NUM_EXTENSIONS; |
| 467 | uint64_t extensionHashes[maxNumStats]; |
| 468 | const uint32_t numStats = std::min(enabledExtensionCount, maxNumStats); |
| 469 | for(uint32_t i = 0; i < numStats; i++) { |
| 470 | extensionHashes[i] = calculateExtensionHash(ppEnabledExtensionNames[i]); |
| 471 | } |
| 472 | setTargetStatsArray(android::GpuStatsInfo::Stats::VULKAN_INSTANCE_EXTENSION, |
| 473 | extensionHashes, numStats); |
| 474 | } |
| 475 | |
| 476 | void GraphicsEnv::setVulkanDeviceExtensions(uint32_t enabledExtensionCount, |
| 477 | const char* const* ppEnabledExtensionNames) { |
| 478 | ATRACE_CALL(); |
| 479 | if (enabledExtensionCount == 0 || ppEnabledExtensionNames == nullptr) { |
| 480 | return; |
| 481 | } |
| 482 | |
| 483 | const uint32_t maxNumStats = android::GpuStatsAppInfo::MAX_NUM_EXTENSIONS; |
| 484 | uint64_t extensionHashes[maxNumStats]; |
| 485 | const uint32_t numStats = std::min(enabledExtensionCount, maxNumStats); |
| 486 | for(uint32_t i = 0; i < numStats; i++) { |
| 487 | extensionHashes[i] = calculateExtensionHash(ppEnabledExtensionNames[i]); |
| 488 | } |
| 489 | setTargetStatsArray(android::GpuStatsInfo::Stats::VULKAN_DEVICE_EXTENSION, |
| 490 | extensionHashes, numStats); |
| 491 | } |
| 492 | |
Tom Murphy | c23fcd0 | 2024-03-13 10:22:06 +0000 | [diff] [blame] | 493 | void GraphicsEnv::addVulkanEngineName(const char* engineName) { |
| 494 | ATRACE_CALL(); |
| 495 | if (engineName == nullptr) { |
| 496 | return; |
| 497 | } |
| 498 | std::lock_guard<std::mutex> lock(mStatsLock); |
| 499 | if (!readyToSendGpuStatsLocked()) return; |
| 500 | |
| 501 | const sp<IGpuService> gpuService = getGpuService(); |
| 502 | if (gpuService) { |
| 503 | gpuService->addVulkanEngineName(mGpuStats.appPackageName, mGpuStats.driverVersionCode, |
| 504 | engineName); |
| 505 | } |
| 506 | } |
| 507 | |
Yiwei Zhang | d381938 | 2020-01-07 19:53:56 -0800 | [diff] [blame] | 508 | bool GraphicsEnv::readyToSendGpuStatsLocked() { |
| 509 | // Only send stats for processes having at least one activity launched and that process doesn't |
| 510 | // skip the GraphicsEnvironment setup. |
| 511 | return mActivityLaunched && !mGpuStats.appPackageName.empty(); |
| 512 | } |
| 513 | |
Yiwei Zhang | bcba411 | 2019-07-03 13:39:32 -0700 | [diff] [blame] | 514 | void GraphicsEnv::setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value) { |
Serdar Kocdemir | b2901c9 | 2022-11-17 00:39:05 +0000 | [diff] [blame] | 515 | return setTargetStatsArray(stats, &value, 1); |
| 516 | } |
| 517 | |
| 518 | void GraphicsEnv::setTargetStatsArray(const GpuStatsInfo::Stats stats, const uint64_t* values, |
| 519 | const uint32_t valueCount) { |
Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 520 | ATRACE_CALL(); |
| 521 | |
Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 522 | std::lock_guard<std::mutex> lock(mStatsLock); |
Yiwei Zhang | d381938 | 2020-01-07 19:53:56 -0800 | [diff] [blame] | 523 | if (!readyToSendGpuStatsLocked()) return; |
| 524 | |
Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 525 | const sp<IGpuService> gpuService = getGpuService(); |
| 526 | if (gpuService) { |
Serdar Kocdemir | b2901c9 | 2022-11-17 00:39:05 +0000 | [diff] [blame] | 527 | gpuService->setTargetStatsArray(mGpuStats.appPackageName, mGpuStats.driverVersionCode, |
| 528 | stats, values, valueCount); |
Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 532 | void GraphicsEnv::sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded, |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 533 | int64_t driverLoadingTime) { |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 534 | ATRACE_CALL(); |
| 535 | |
Yiwei Zhang | d381938 | 2020-01-07 19:53:56 -0800 | [diff] [blame] | 536 | if (!readyToSendGpuStatsLocked()) return; |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 537 | |
| 538 | ALOGV("sendGpuStats:\n" |
| 539 | "\tdriverPackageName[%s]\n" |
| 540 | "\tdriverVersionName[%s]\n" |
Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 541 | "\tdriverVersionCode[%" PRIu64 "]\n" |
| 542 | "\tdriverBuildTime[%" PRId64 "]\n" |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 543 | "\tappPackageName[%s]\n" |
Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 544 | "\tvulkanVersion[%d]\n" |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 545 | "\tapi[%d]\n" |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 546 | "\tisDriverLoaded[%d]\n" |
Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 547 | "\tdriverLoadingTime[%" PRId64 "]", |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 548 | mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(), |
Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 549 | mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(), |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 550 | mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime); |
| 551 | |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 552 | GpuStatsInfo::Driver driver = GpuStatsInfo::Driver::NONE; |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 553 | bool isIntendedDriverLoaded = false; |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 554 | if (api == GpuStatsInfo::Api::API_GL) { |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 555 | driver = mGpuStats.glDriverToLoad; |
Solti | 371af85 | 2024-08-22 21:22:19 +0000 | [diff] [blame] | 556 | isIntendedDriverLoaded = isDriverLoaded; |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 557 | } else { |
| 558 | driver = mGpuStats.vkDriverToLoad; |
| 559 | isIntendedDriverLoaded = |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 560 | isDriverLoaded && (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE); |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 561 | } |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 562 | |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 563 | const sp<IGpuService> gpuService = getGpuService(); |
| 564 | if (gpuService) { |
| 565 | gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName, |
Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 566 | mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, |
Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 567 | mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver, |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 568 | isIntendedDriverLoaded, driverLoadingTime); |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 569 | } |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 570 | } |
| 571 | |
Adam Bodnar | 0afcca0 | 2019-09-17 13:23:17 -0700 | [diff] [blame] | 572 | bool GraphicsEnv::setInjectLayersPrSetDumpable() { |
| 573 | if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) { |
| 574 | return false; |
| 575 | } |
| 576 | return true; |
| 577 | } |
| 578 | |
Peiyong Lin | ef20e4e | 2023-07-03 02:33:00 +0000 | [diff] [blame] | 579 | /** |
| 580 | * APIs for ANGLE |
| 581 | */ |
| 582 | |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 583 | bool GraphicsEnv::shouldUseAngle() { |
| 584 | // Make sure we are init'ed |
Peiyong Lin | 2f51e75 | 2023-06-15 22:35:14 +0000 | [diff] [blame] | 585 | if (mPackageName.empty()) { |
| 586 | ALOGV("Package name is empty. setAngleInfo() has not been called to enable ANGLE."); |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 587 | return false; |
| 588 | } |
| 589 | |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 590 | return mShouldUseAngle; |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Peiyong Lin | 2df5a00 | 2023-08-01 23:31:34 +0000 | [diff] [blame] | 593 | // Set ANGLE information. |
| 594 | // If path is "system", it means system ANGLE must be used for the process. |
| 595 | // If shouldUseNativeDriver is true, it means native GLES drivers must be used for the process. |
| 596 | // If path is set to nonempty and shouldUseNativeDriver is true, ANGLE will be used regardless. |
| 597 | void GraphicsEnv::setAngleInfo(const std::string& path, const bool shouldUseNativeDriver, |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 598 | const std::string& packageName, |
Tim Van Patten | a53dcb2 | 2021-05-21 17:51:59 -0600 | [diff] [blame] | 599 | const std::vector<std::string> eglFeatures) { |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 600 | if (mShouldUseAngle) { |
| 601 | // ANGLE is already set up for this application process, even if the application |
| 602 | // needs to switch from apk to system or vice versa, the application process must |
| 603 | // be killed and relaunch so that the loader can properly load ANGLE again. |
| 604 | // The architecture does not support runtime switch between drivers, so just return. |
| 605 | ALOGE("ANGLE is already set for %s", packageName.c_str()); |
Tim Van Patten | 8bd24e9 | 2019-02-08 10:16:40 -0700 | [diff] [blame] | 606 | return; |
| 607 | } |
| 608 | |
Peiyong Lin | 2f707e6 | 2020-09-26 13:52:10 -0700 | [diff] [blame] | 609 | mAngleEglFeatures = std::move(eglFeatures); |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 610 | ALOGV("setting ANGLE path to '%s'", path.c_str()); |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 611 | mAnglePath = std::move(path); |
Peiyong Lin | 2f51e75 | 2023-06-15 22:35:14 +0000 | [diff] [blame] | 612 | ALOGV("setting app package name to '%s'", packageName.c_str()); |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 613 | mPackageName = std::move(packageName); |
Peiyong Lin | 2df5a00 | 2023-08-01 23:31:34 +0000 | [diff] [blame] | 614 | if (mAnglePath == "system") { |
| 615 | mShouldUseSystemAngle = true; |
| 616 | } |
| 617 | if (!mAnglePath.empty()) { |
| 618 | mShouldUseAngle = true; |
| 619 | } |
| 620 | mShouldUseNativeDriver = shouldUseNativeDriver; |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 621 | } |
| 622 | |
Peiyong Lin | 2f51e75 | 2023-06-15 22:35:14 +0000 | [diff] [blame] | 623 | std::string& GraphicsEnv::getPackageName() { |
| 624 | return mPackageName; |
Cody Northrop | 04e7043 | 2018-09-06 10:34:58 -0600 | [diff] [blame] | 625 | } |
| 626 | |
Peiyong Lin | 2f707e6 | 2020-09-26 13:52:10 -0700 | [diff] [blame] | 627 | const std::vector<std::string>& GraphicsEnv::getAngleEglFeatures() { |
| 628 | return mAngleEglFeatures; |
| 629 | } |
| 630 | |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 631 | android_namespace_t* GraphicsEnv::getAngleNamespace() { |
Cody Northrop | 3892cfa | 2019-01-30 10:03:12 -0700 | [diff] [blame] | 632 | std::lock_guard<std::mutex> lock(mNamespaceMutex); |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 633 | |
Cody Northrop | 3892cfa | 2019-01-30 10:03:12 -0700 | [diff] [blame] | 634 | if (mAngleNamespace) { |
| 635 | return mAngleNamespace; |
| 636 | } |
| 637 | |
Peiyong Lin | e4da7a8 | 2023-08-08 20:46:53 +0000 | [diff] [blame] | 638 | // If ANGLE path is not set, it means ANGLE should not be used for this process; |
| 639 | // or if ANGLE path is set and set to use system ANGLE, then a namespace is not needed |
| 640 | // because: |
| 641 | // 1) if the default OpenGL ES driver is already ANGLE, then the loader will skip; |
| 642 | // 2) if the default OpenGL ES driver is native, then there's no symbol conflict; |
| 643 | // 3) if there's no OpenGL ES driver is preloaded, then there's no symbol conflict. |
| 644 | if (mAnglePath.empty() || mShouldUseSystemAngle) { |
| 645 | ALOGV("mAnglePath is empty or use system ANGLE, abort creating ANGLE namespace"); |
Cody Northrop | 3892cfa | 2019-01-30 10:03:12 -0700 | [diff] [blame] | 646 | return nullptr; |
| 647 | } |
| 648 | |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 649 | // Construct the search paths for system ANGLE. |
| 650 | const char* const defaultLibraryPaths = |
| 651 | #if defined(__LP64__) |
Peiyong Lin | e4da7a8 | 2023-08-08 20:46:53 +0000 | [diff] [blame] | 652 | "/vendor/lib64/egl:/system/lib64"; |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 653 | #else |
Peiyong Lin | e4da7a8 | 2023-08-08 20:46:53 +0000 | [diff] [blame] | 654 | "/vendor/lib/egl:/system/lib"; |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 655 | #endif |
| 656 | |
| 657 | // If the application process will run on top of system ANGLE, construct the namespace |
| 658 | // with sphal namespace being the parent namespace so that search paths and libraries |
| 659 | // are properly inherited. |
| 660 | mAngleNamespace = |
| 661 | android_create_namespace("ANGLE", |
Peiyong Lin | e88e0ba | 2023-06-28 15:09:09 +0000 | [diff] [blame] | 662 | mShouldUseSystemAngle ? defaultLibraryPaths |
| 663 | : mAnglePath.c_str(), // ld_library_path |
| 664 | mShouldUseSystemAngle |
| 665 | ? defaultLibraryPaths |
| 666 | : mAnglePath.c_str(), // default_library_path |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 667 | ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED, |
| 668 | nullptr, // permitted_when_isolated_path |
Peiyong Lin | e88e0ba | 2023-06-28 15:09:09 +0000 | [diff] [blame] | 669 | mShouldUseSystemAngle ? android_get_exported_namespace("sphal") |
| 670 | : nullptr); // parent |
Cody Northrop | 3892cfa | 2019-01-30 10:03:12 -0700 | [diff] [blame] | 671 | |
| 672 | ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default"); |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 673 | |
Peiyong Lin | e88e0ba | 2023-06-28 15:09:09 +0000 | [diff] [blame] | 674 | if (!mShouldUseSystemAngle) { |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 675 | return mAngleNamespace; |
| 676 | } |
| 677 | |
Chris Forbes | 0cdac9a | 2024-08-16 15:12:27 +1200 | [diff] [blame] | 678 | auto vndkNamespace = android_get_exported_namespace(isVndkEnabled() ? "vndk" : "sphal"); |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 679 | if (!vndkNamespace) { |
Ethan Chen | f5da7ee | 2024-01-18 19:47:01 -0800 | [diff] [blame] | 680 | mAngleNamespace = nullptr; |
| 681 | return mAngleNamespace; |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | if (!linkDriverNamespaceLocked(mAngleNamespace, vndkNamespace, "")) { |
| 685 | mAngleNamespace = nullptr; |
| 686 | } |
| 687 | |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 688 | return mAngleNamespace; |
| 689 | } |
| 690 | |
Yuxin Hu | b69e988 | 2023-04-13 03:51:41 +0000 | [diff] [blame] | 691 | void GraphicsEnv::nativeToggleAngleAsSystemDriver(bool enabled) { |
| 692 | const sp<IGpuService> gpuService = getGpuService(); |
| 693 | if (!gpuService) { |
| 694 | ALOGE("No GPU service"); |
| 695 | return; |
| 696 | } |
| 697 | gpuService->toggleAngleAsSystemDriver(enabled); |
| 698 | } |
| 699 | |
Peiyong Lin | e88e0ba | 2023-06-28 15:09:09 +0000 | [diff] [blame] | 700 | bool GraphicsEnv::shouldUseSystemAngle() { |
| 701 | return mShouldUseSystemAngle; |
| 702 | } |
| 703 | |
Peiyong Lin | 2df5a00 | 2023-08-01 23:31:34 +0000 | [diff] [blame] | 704 | bool GraphicsEnv::shouldUseNativeDriver() { |
| 705 | return mShouldUseNativeDriver; |
| 706 | } |
| 707 | |
Peiyong Lin | ef20e4e | 2023-07-03 02:33:00 +0000 | [diff] [blame] | 708 | /** |
| 709 | * APIs for debuggable layers |
| 710 | */ |
| 711 | |
| 712 | void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, |
| 713 | const std::string& layerPaths) { |
| 714 | if (mLayerPaths.empty()) { |
| 715 | mLayerPaths = layerPaths; |
| 716 | mAppNamespace = appNamespace; |
| 717 | } else { |
| 718 | ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'", |
| 719 | layerPaths.c_str(), appNamespace); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | NativeLoaderNamespace* GraphicsEnv::getAppNamespace() { |
| 724 | return mAppNamespace; |
| 725 | } |
| 726 | |
| 727 | const std::string& GraphicsEnv::getLayerPaths() { |
| 728 | return mLayerPaths; |
| 729 | } |
| 730 | |
| 731 | const std::string& GraphicsEnv::getDebugLayers() { |
| 732 | return mDebugLayers; |
| 733 | } |
| 734 | |
| 735 | const std::string& GraphicsEnv::getDebugLayersGLES() { |
| 736 | return mDebugLayersGLES; |
| 737 | } |
| 738 | |
| 739 | void GraphicsEnv::setDebugLayers(const std::string& layers) { |
| 740 | mDebugLayers = layers; |
| 741 | } |
| 742 | |
| 743 | void GraphicsEnv::setDebugLayersGLES(const std::string& layers) { |
| 744 | mDebugLayersGLES = layers; |
| 745 | } |
| 746 | |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 747 | } // namespace android |