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 | |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 86 | static std::string vndkVersionStr() { |
| 87 | #ifdef __BIONIC__ |
Michael Hoisie | d1023f2 | 2020-03-26 22:52:58 -0700 | [diff] [blame] | 88 | return base::GetProperty("ro.vndk.version", ""); |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 89 | #endif |
| 90 | return ""; |
| 91 | } |
| 92 | |
| 93 | static void insertVndkVersionStr(std::string* fileName) { |
| 94 | LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr"); |
Jooyung Han | 7839680 | 2020-02-23 03:02:43 +0900 | [diff] [blame] | 95 | std::string version = vndkVersionStr(); |
| 96 | size_t pos = fileName->find("{}"); |
| 97 | while (pos != std::string::npos) { |
| 98 | fileName->replace(pos, 2, version); |
| 99 | pos = fileName->find("{}", pos + version.size()); |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 100 | } |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) { |
| 104 | // Read list of public native libraries from the config file. |
| 105 | std::string fileContent; |
| 106 | if (!base::ReadFileToString(configFile, &fileContent)) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | std::vector<std::string> lines = base::Split(fileContent, "\n"); |
| 111 | |
| 112 | for (auto& line : lines) { |
| 113 | auto trimmedLine = base::Trim(line); |
| 114 | if (!trimmedLine.empty()) { |
| 115 | soNames->push_back(trimmedLine); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | static const std::string getSystemNativeLibraries(NativeLibrary type) { |
Kiyoung Kim | c0e3ed3 | 2023-08-11 11:22:15 +0900 | [diff] [blame] | 123 | std::string nativeLibrariesSystemConfig = ""; |
| 124 | |
| 125 | if (!isVndkEnabled() && type == NativeLibrary::LLNDK) { |
| 126 | nativeLibrariesSystemConfig = kLlndkLibrariesTxtPath; |
| 127 | } else { |
| 128 | nativeLibrariesSystemConfig = kNativeLibrariesSystemConfigPath[type]; |
| 129 | insertVndkVersionStr(&nativeLibrariesSystemConfig); |
| 130 | } |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 131 | |
| 132 | std::vector<std::string> soNames; |
| 133 | if (!readConfig(nativeLibrariesSystemConfig, &soNames)) { |
| 134 | ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str()); |
| 135 | return ""; |
| 136 | } |
| 137 | |
| 138 | return base::Join(soNames, ':'); |
| 139 | } |
| 140 | |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 141 | /*static*/ GraphicsEnv& GraphicsEnv::getInstance() { |
| 142 | static GraphicsEnv env; |
| 143 | return env; |
| 144 | } |
| 145 | |
Yiwei Zhang | 6a674c9 | 2019-11-08 11:55:36 -0800 | [diff] [blame] | 146 | bool GraphicsEnv::isDebuggable() { |
Cody Northrop | 383832f | 2022-11-01 13:54:57 -0600 | [diff] [blame] | 147 | // This flag determines if the application is marked debuggable |
| 148 | bool appDebuggable = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) > 0; |
| 149 | |
| 150 | // This flag is set only in `debuggable` builds of the platform |
| 151 | #if defined(ANDROID_DEBUGGABLE) |
| 152 | bool platformDebuggable = true; |
| 153 | #else |
| 154 | bool platformDebuggable = false; |
| 155 | #endif |
| 156 | |
| 157 | ALOGV("GraphicsEnv::isDebuggable returning appDebuggable=%s || platformDebuggable=%s", |
| 158 | appDebuggable ? "true" : "false", platformDebuggable ? "true" : "false"); |
| 159 | |
| 160 | return appDebuggable || platformDebuggable; |
Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 161 | } |
| 162 | |
Peiyong Lin | 2f51e75 | 2023-06-15 22:35:14 +0000 | [diff] [blame] | 163 | void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string& path, |
| 164 | const std::string& sphalLibraries) { |
Yiwei Zhang | 6ef8494 | 2019-02-14 12:28:12 -0800 | [diff] [blame] | 165 | if (!mDriverPath.empty() || !mSphalLibraries.empty()) { |
| 166 | ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries " |
| 167 | "from '%s' to '%s'", |
| 168 | mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str()); |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 169 | return; |
| 170 | } |
Yiwei Zhang | 6ef8494 | 2019-02-14 12:28:12 -0800 | [diff] [blame] | 171 | ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(), |
| 172 | sphalLibraries.c_str()); |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 173 | mDriverPath = path; |
Yiwei Zhang | 6ef8494 | 2019-02-14 12:28:12 -0800 | [diff] [blame] | 174 | mSphalLibraries = sphalLibraries; |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 177 | void GraphicsEnv::hintActivityLaunch() { |
| 178 | ATRACE_CALL(); |
| 179 | |
Yiwei Zhang | d381938 | 2020-01-07 19:53:56 -0800 | [diff] [blame] | 180 | { |
| 181 | std::lock_guard<std::mutex> lock(mStatsLock); |
| 182 | if (mActivityLaunched) return; |
| 183 | mActivityLaunched = true; |
| 184 | } |
| 185 | |
Yiwei Zhang | 3c74da9 | 2019-06-28 10:16:49 -0700 | [diff] [blame] | 186 | std::thread trySendGpuStatsThread([this]() { |
| 187 | // If there's already graphics driver preloaded in the process, just send |
| 188 | // the stats info to GpuStats directly through async binder. |
| 189 | std::lock_guard<std::mutex> lock(mStatsLock); |
| 190 | if (mGpuStats.glDriverToSend) { |
| 191 | mGpuStats.glDriverToSend = false; |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 192 | sendGpuStatsLocked(GpuStatsInfo::Api::API_GL, true, mGpuStats.glDriverLoadingTime); |
Yiwei Zhang | 3c74da9 | 2019-06-28 10:16:49 -0700 | [diff] [blame] | 193 | } |
| 194 | if (mGpuStats.vkDriverToSend) { |
| 195 | mGpuStats.vkDriverToSend = false; |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 196 | sendGpuStatsLocked(GpuStatsInfo::Api::API_VK, true, mGpuStats.vkDriverLoadingTime); |
Yiwei Zhang | 3c74da9 | 2019-06-28 10:16:49 -0700 | [diff] [blame] | 197 | } |
| 198 | }); |
| 199 | trySendGpuStatsThread.detach(); |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Greg Kaiser | 210bb7e | 2019-02-12 12:40:05 -0800 | [diff] [blame] | 202 | void GraphicsEnv::setGpuStats(const std::string& driverPackageName, |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 203 | const std::string& driverVersionName, uint64_t driverVersionCode, |
Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 204 | int64_t driverBuildTime, const std::string& appPackageName, |
| 205 | const int vulkanVersion) { |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 206 | ATRACE_CALL(); |
| 207 | |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 208 | std::lock_guard<std::mutex> lock(mStatsLock); |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 209 | ALOGV("setGpuStats:\n" |
| 210 | "\tdriverPackageName[%s]\n" |
| 211 | "\tdriverVersionName[%s]\n" |
Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 212 | "\tdriverVersionCode[%" PRIu64 "]\n" |
| 213 | "\tdriverBuildTime[%" PRId64 "]\n" |
Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 214 | "\tappPackageName[%s]\n" |
| 215 | "\tvulkanVersion[%d]\n", |
Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 216 | driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime, |
Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 217 | appPackageName.c_str(), vulkanVersion); |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 218 | |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 219 | mGpuStats.driverPackageName = driverPackageName; |
| 220 | mGpuStats.driverVersionName = driverVersionName; |
| 221 | mGpuStats.driverVersionCode = driverVersionCode; |
Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 222 | mGpuStats.driverBuildTime = driverBuildTime; |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 223 | mGpuStats.appPackageName = appPackageName; |
Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 224 | mGpuStats.vulkanVersion = vulkanVersion; |
Yiwei Zhang | 8c8c181 | 2019-02-04 18:56:38 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 227 | void GraphicsEnv::setDriverToLoad(GpuStatsInfo::Driver driver) { |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 228 | ATRACE_CALL(); |
| 229 | |
| 230 | std::lock_guard<std::mutex> lock(mStatsLock); |
| 231 | switch (driver) { |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 232 | case GpuStatsInfo::Driver::GL: |
| 233 | case GpuStatsInfo::Driver::GL_UPDATED: |
| 234 | case GpuStatsInfo::Driver::ANGLE: { |
Yiwei Zhang | 472cab0 | 2019-08-05 17:57:41 -0700 | [diff] [blame] | 235 | if (mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::NONE || |
| 236 | mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::GL) { |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 237 | mGpuStats.glDriverToLoad = driver; |
| 238 | break; |
| 239 | } |
| 240 | |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 241 | if (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE) { |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 242 | mGpuStats.glDriverFallback = driver; |
| 243 | } |
| 244 | break; |
| 245 | } |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 246 | case GpuStatsInfo::Driver::VULKAN: |
| 247 | case GpuStatsInfo::Driver::VULKAN_UPDATED: { |
Yiwei Zhang | 472cab0 | 2019-08-05 17:57:41 -0700 | [diff] [blame] | 248 | if (mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::NONE || |
| 249 | mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::VULKAN) { |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 250 | mGpuStats.vkDriverToLoad = driver; |
| 251 | break; |
| 252 | } |
| 253 | |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 254 | if (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE) { |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 255 | mGpuStats.vkDriverFallback = driver; |
| 256 | } |
| 257 | break; |
| 258 | } |
| 259 | default: |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 264 | void GraphicsEnv::setDriverLoaded(GpuStatsInfo::Api api, bool isDriverLoaded, |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 265 | int64_t driverLoadingTime) { |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 266 | ATRACE_CALL(); |
| 267 | |
| 268 | std::lock_guard<std::mutex> lock(mStatsLock); |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 269 | if (api == GpuStatsInfo::Api::API_GL) { |
Yiwei Zhang | d381938 | 2020-01-07 19:53:56 -0800 | [diff] [blame] | 270 | mGpuStats.glDriverToSend = true; |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 271 | mGpuStats.glDriverLoadingTime = driverLoadingTime; |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 272 | } else { |
Yiwei Zhang | d381938 | 2020-01-07 19:53:56 -0800 | [diff] [blame] | 273 | mGpuStats.vkDriverToSend = true; |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 274 | mGpuStats.vkDriverLoadingTime = driverLoadingTime; |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 277 | sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime); |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Serdar Kocdemir | b2901c9 | 2022-11-17 00:39:05 +0000 | [diff] [blame] | 280 | // Hash function to calculate hash for null-terminated Vulkan extension names |
| 281 | // We store hash values of the extensions, rather than the actual names or |
| 282 | // indices to be able to support new extensions easily, avoid creating |
| 283 | // a table of 'known' extensions inside Android and reduce the runtime overhead. |
| 284 | static uint64_t calculateExtensionHash(const char* word) { |
| 285 | if (!word) { |
| 286 | return 0; |
| 287 | } |
| 288 | const size_t wordLen = strlen(word); |
| 289 | const uint32_t seed = 167; |
| 290 | uint64_t hash = 0; |
| 291 | for (size_t i = 0; i < wordLen; i++) { |
| 292 | hash = (hash * seed) + word[i]; |
| 293 | } |
| 294 | return hash; |
| 295 | } |
| 296 | |
| 297 | void GraphicsEnv::setVulkanInstanceExtensions(uint32_t enabledExtensionCount, |
| 298 | const char* const* ppEnabledExtensionNames) { |
| 299 | ATRACE_CALL(); |
| 300 | if (enabledExtensionCount == 0 || ppEnabledExtensionNames == nullptr) { |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | const uint32_t maxNumStats = android::GpuStatsAppInfo::MAX_NUM_EXTENSIONS; |
| 305 | uint64_t extensionHashes[maxNumStats]; |
| 306 | const uint32_t numStats = std::min(enabledExtensionCount, maxNumStats); |
| 307 | for(uint32_t i = 0; i < numStats; i++) { |
| 308 | extensionHashes[i] = calculateExtensionHash(ppEnabledExtensionNames[i]); |
| 309 | } |
| 310 | setTargetStatsArray(android::GpuStatsInfo::Stats::VULKAN_INSTANCE_EXTENSION, |
| 311 | extensionHashes, numStats); |
| 312 | } |
| 313 | |
| 314 | void GraphicsEnv::setVulkanDeviceExtensions(uint32_t enabledExtensionCount, |
| 315 | const char* const* ppEnabledExtensionNames) { |
| 316 | ATRACE_CALL(); |
| 317 | if (enabledExtensionCount == 0 || ppEnabledExtensionNames == nullptr) { |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | const uint32_t maxNumStats = android::GpuStatsAppInfo::MAX_NUM_EXTENSIONS; |
| 322 | uint64_t extensionHashes[maxNumStats]; |
| 323 | const uint32_t numStats = std::min(enabledExtensionCount, maxNumStats); |
| 324 | for(uint32_t i = 0; i < numStats; i++) { |
| 325 | extensionHashes[i] = calculateExtensionHash(ppEnabledExtensionNames[i]); |
| 326 | } |
| 327 | setTargetStatsArray(android::GpuStatsInfo::Stats::VULKAN_DEVICE_EXTENSION, |
| 328 | extensionHashes, numStats); |
| 329 | } |
| 330 | |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 331 | static sp<IGpuService> getGpuService() { |
Yiwei Zhang | 8e09730 | 2019-07-08 16:11:12 -0700 | [diff] [blame] | 332 | static const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu")); |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 333 | if (!binder) { |
| 334 | ALOGE("Failed to get gpu service"); |
| 335 | return nullptr; |
| 336 | } |
| 337 | |
| 338 | return interface_cast<IGpuService>(binder); |
| 339 | } |
| 340 | |
Yiwei Zhang | d381938 | 2020-01-07 19:53:56 -0800 | [diff] [blame] | 341 | bool GraphicsEnv::readyToSendGpuStatsLocked() { |
| 342 | // Only send stats for processes having at least one activity launched and that process doesn't |
| 343 | // skip the GraphicsEnvironment setup. |
| 344 | return mActivityLaunched && !mGpuStats.appPackageName.empty(); |
| 345 | } |
| 346 | |
Yiwei Zhang | bcba411 | 2019-07-03 13:39:32 -0700 | [diff] [blame] | 347 | void GraphicsEnv::setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value) { |
Serdar Kocdemir | b2901c9 | 2022-11-17 00:39:05 +0000 | [diff] [blame] | 348 | return setTargetStatsArray(stats, &value, 1); |
| 349 | } |
| 350 | |
| 351 | void GraphicsEnv::setTargetStatsArray(const GpuStatsInfo::Stats stats, const uint64_t* values, |
| 352 | const uint32_t valueCount) { |
Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 353 | ATRACE_CALL(); |
| 354 | |
Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 355 | std::lock_guard<std::mutex> lock(mStatsLock); |
Yiwei Zhang | d381938 | 2020-01-07 19:53:56 -0800 | [diff] [blame] | 356 | if (!readyToSendGpuStatsLocked()) return; |
| 357 | |
Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 358 | const sp<IGpuService> gpuService = getGpuService(); |
| 359 | if (gpuService) { |
Serdar Kocdemir | b2901c9 | 2022-11-17 00:39:05 +0000 | [diff] [blame] | 360 | gpuService->setTargetStatsArray(mGpuStats.appPackageName, mGpuStats.driverVersionCode, |
| 361 | stats, values, valueCount); |
Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 365 | void GraphicsEnv::sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded, |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 366 | int64_t driverLoadingTime) { |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 367 | ATRACE_CALL(); |
| 368 | |
Yiwei Zhang | d381938 | 2020-01-07 19:53:56 -0800 | [diff] [blame] | 369 | if (!readyToSendGpuStatsLocked()) return; |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 370 | |
| 371 | ALOGV("sendGpuStats:\n" |
| 372 | "\tdriverPackageName[%s]\n" |
| 373 | "\tdriverVersionName[%s]\n" |
Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 374 | "\tdriverVersionCode[%" PRIu64 "]\n" |
| 375 | "\tdriverBuildTime[%" PRId64 "]\n" |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 376 | "\tappPackageName[%s]\n" |
Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 377 | "\tvulkanVersion[%d]\n" |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 378 | "\tapi[%d]\n" |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 379 | "\tisDriverLoaded[%d]\n" |
Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 380 | "\tdriverLoadingTime[%" PRId64 "]", |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 381 | mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(), |
Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 382 | mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(), |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 383 | mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime); |
| 384 | |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 385 | GpuStatsInfo::Driver driver = GpuStatsInfo::Driver::NONE; |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 386 | bool isIntendedDriverLoaded = false; |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 387 | if (api == GpuStatsInfo::Api::API_GL) { |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 388 | driver = mGpuStats.glDriverToLoad; |
| 389 | isIntendedDriverLoaded = |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 390 | isDriverLoaded && (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE); |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 391 | } else { |
| 392 | driver = mGpuStats.vkDriverToLoad; |
| 393 | isIntendedDriverLoaded = |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 394 | isDriverLoaded && (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE); |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 395 | } |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 396 | |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 397 | const sp<IGpuService> gpuService = getGpuService(); |
| 398 | if (gpuService) { |
| 399 | gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName, |
Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 400 | mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, |
Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 401 | mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver, |
Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 402 | isIntendedDriverLoaded, driverLoadingTime); |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 403 | } |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Adam Bodnar | 0afcca0 | 2019-09-17 13:23:17 -0700 | [diff] [blame] | 406 | bool GraphicsEnv::setInjectLayersPrSetDumpable() { |
| 407 | if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) { |
| 408 | return false; |
| 409 | } |
| 410 | return true; |
| 411 | } |
| 412 | |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 413 | bool GraphicsEnv::shouldUseAngle() { |
| 414 | // Make sure we are init'ed |
Peiyong Lin | 2f51e75 | 2023-06-15 22:35:14 +0000 | [diff] [blame] | 415 | if (mPackageName.empty()) { |
| 416 | 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] | 417 | return false; |
| 418 | } |
| 419 | |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 420 | return mShouldUseAngle; |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Peiyong Lin | e88e0ba | 2023-06-28 15:09:09 +0000 | [diff] [blame^] | 423 | void GraphicsEnv::setAngleInfo(const std::string& path, const bool shouldUseSystemAngle, |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 424 | const std::string& packageName, |
Tim Van Patten | a53dcb2 | 2021-05-21 17:51:59 -0600 | [diff] [blame] | 425 | const std::vector<std::string> eglFeatures) { |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 426 | if (mShouldUseAngle) { |
| 427 | // ANGLE is already set up for this application process, even if the application |
| 428 | // needs to switch from apk to system or vice versa, the application process must |
| 429 | // be killed and relaunch so that the loader can properly load ANGLE again. |
| 430 | // The architecture does not support runtime switch between drivers, so just return. |
| 431 | ALOGE("ANGLE is already set for %s", packageName.c_str()); |
Tim Van Patten | 8bd24e9 | 2019-02-08 10:16:40 -0700 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | |
Peiyong Lin | 2f707e6 | 2020-09-26 13:52:10 -0700 | [diff] [blame] | 435 | mAngleEglFeatures = std::move(eglFeatures); |
Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 436 | ALOGV("setting ANGLE path to '%s'", path.c_str()); |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 437 | mAnglePath = std::move(path); |
Peiyong Lin | 2f51e75 | 2023-06-15 22:35:14 +0000 | [diff] [blame] | 438 | ALOGV("setting app package name to '%s'", packageName.c_str()); |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 439 | mPackageName = std::move(packageName); |
| 440 | mShouldUseAngle = true; |
Peiyong Lin | e88e0ba | 2023-06-28 15:09:09 +0000 | [diff] [blame^] | 441 | mShouldUseSystemAngle = shouldUseSystemAngle; |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 442 | } |
| 443 | |
Peiyong Lin | 2f51e75 | 2023-06-15 22:35:14 +0000 | [diff] [blame] | 444 | void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, |
| 445 | const std::string& layerPaths) { |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 446 | if (mLayerPaths.empty()) { |
| 447 | mLayerPaths = layerPaths; |
| 448 | mAppNamespace = appNamespace; |
| 449 | } else { |
| 450 | ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'", |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 451 | layerPaths.c_str(), appNamespace); |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
Victor Khimenko | 4819b52 | 2018-07-13 17:24:18 +0200 | [diff] [blame] | 455 | NativeLoaderNamespace* GraphicsEnv::getAppNamespace() { |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 456 | return mAppNamespace; |
| 457 | } |
| 458 | |
Peiyong Lin | 2f51e75 | 2023-06-15 22:35:14 +0000 | [diff] [blame] | 459 | std::string& GraphicsEnv::getPackageName() { |
| 460 | return mPackageName; |
Cody Northrop | 04e7043 | 2018-09-06 10:34:58 -0600 | [diff] [blame] | 461 | } |
| 462 | |
Peiyong Lin | 2f707e6 | 2020-09-26 13:52:10 -0700 | [diff] [blame] | 463 | const std::vector<std::string>& GraphicsEnv::getAngleEglFeatures() { |
| 464 | return mAngleEglFeatures; |
| 465 | } |
| 466 | |
Courtney Goeltzenleuchter | 30ad2ab | 2018-10-30 08:20:44 -0600 | [diff] [blame] | 467 | const std::string& GraphicsEnv::getLayerPaths() { |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 468 | return mLayerPaths; |
| 469 | } |
| 470 | |
Courtney Goeltzenleuchter | 30ad2ab | 2018-10-30 08:20:44 -0600 | [diff] [blame] | 471 | const std::string& GraphicsEnv::getDebugLayers() { |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 472 | return mDebugLayers; |
| 473 | } |
| 474 | |
Cody Northrop | b9b01b6 | 2018-10-23 13:13:10 -0600 | [diff] [blame] | 475 | const std::string& GraphicsEnv::getDebugLayersGLES() { |
| 476 | return mDebugLayersGLES; |
| 477 | } |
| 478 | |
Peiyong Lin | 2f51e75 | 2023-06-15 22:35:14 +0000 | [diff] [blame] | 479 | void GraphicsEnv::setDebugLayers(const std::string& layers) { |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 480 | mDebugLayers = layers; |
| 481 | } |
| 482 | |
Peiyong Lin | 2f51e75 | 2023-06-15 22:35:14 +0000 | [diff] [blame] | 483 | void GraphicsEnv::setDebugLayersGLES(const std::string& layers) { |
Cody Northrop | b9b01b6 | 2018-10-23 13:13:10 -0600 | [diff] [blame] | 484 | mDebugLayersGLES = layers; |
| 485 | } |
| 486 | |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 487 | // Return true if all the required libraries from vndk and sphal namespace are |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 488 | // linked to the driver namespace correctly. |
| 489 | bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* destNamespace, |
| 490 | android_namespace_t* vndkNamespace, |
| 491 | const std::string& sharedSphalLibraries) { |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 492 | const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK); |
| 493 | if (llndkLibraries.empty()) { |
| 494 | return false; |
| 495 | } |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 496 | if (!android_link_namespaces(destNamespace, nullptr, llndkLibraries.c_str())) { |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 497 | ALOGE("Failed to link default namespace[%s]", dlerror()); |
| 498 | return false; |
| 499 | } |
| 500 | |
| 501 | const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP); |
| 502 | if (vndkspLibraries.empty()) { |
| 503 | return false; |
| 504 | } |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 505 | if (!android_link_namespaces(destNamespace, vndkNamespace, vndkspLibraries.c_str())) { |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 506 | ALOGE("Failed to link vndk namespace[%s]", dlerror()); |
| 507 | return false; |
| 508 | } |
| 509 | |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 510 | if (sharedSphalLibraries.empty()) { |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 511 | return true; |
| 512 | } |
| 513 | |
| 514 | // Make additional libraries in sphal to be accessible |
| 515 | auto sphalNamespace = android_get_exported_namespace("sphal"); |
| 516 | if (!sphalNamespace) { |
| 517 | ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace", |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 518 | sharedSphalLibraries.c_str()); |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 519 | return false; |
| 520 | } |
| 521 | |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 522 | if (!android_link_namespaces(destNamespace, sphalNamespace, sharedSphalLibraries.c_str())) { |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 523 | ALOGE("Failed to link sphal namespace[%s]", dlerror()); |
| 524 | return false; |
| 525 | } |
| 526 | |
| 527 | return true; |
| 528 | } |
| 529 | |
Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 530 | android_namespace_t* GraphicsEnv::getDriverNamespace() { |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 531 | std::lock_guard<std::mutex> lock(mNamespaceMutex); |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 532 | |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 533 | if (mDriverNamespace) { |
| 534 | return mDriverNamespace; |
| 535 | } |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 536 | |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 537 | if (mDriverPath.empty()) { |
Peiyong Lin | 0acbfdc | 2020-06-17 18:47:12 -0700 | [diff] [blame] | 538 | // For an application process, driver path is empty means this application is not opted in |
| 539 | // to use updatable driver. Application process doesn't have the ability to set up |
| 540 | // environment variables and hence before `getenv` call will return. |
| 541 | // For a process that is not an application process, if it's run from an environment, |
| 542 | // for example shell, where environment variables can be set, then it can opt into using |
| 543 | // udpatable driver by setting UPDATABLE_GFX_DRIVER to 1. By setting to 1 the developer |
| 544 | // driver will be used currently. |
| 545 | // TODO(b/159240322) Support the production updatable driver. |
| 546 | const char* id = getenv("UPDATABLE_GFX_DRIVER"); |
| 547 | if (id == nullptr || std::strcmp(id, "1")) { |
| 548 | return nullptr; |
| 549 | } |
| 550 | const sp<IGpuService> gpuService = getGpuService(); |
| 551 | if (!gpuService) { |
| 552 | return nullptr; |
| 553 | } |
| 554 | mDriverPath = gpuService->getUpdatableDriverPath(); |
| 555 | if (mDriverPath.empty()) { |
| 556 | return nullptr; |
| 557 | } |
| 558 | mDriverPath.append(UPDATABLE_DRIVER_ABI); |
| 559 | ALOGI("Driver path is setup via UPDATABLE_GFX_DRIVER: %s", mDriverPath.c_str()); |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 560 | } |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 561 | |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 562 | auto vndkNamespace = android_get_exported_namespace("vndk"); |
| 563 | if (!vndkNamespace) { |
| 564 | return nullptr; |
| 565 | } |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 566 | |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 567 | mDriverNamespace = android_create_namespace("gfx driver", |
| 568 | mDriverPath.c_str(), // ld_library_path |
| 569 | mDriverPath.c_str(), // default_library_path |
| 570 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 571 | nullptr, // permitted_when_isolated_path |
| 572 | nullptr); |
Yiwei Zhang | 6ef8494 | 2019-02-14 12:28:12 -0800 | [diff] [blame] | 573 | |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 574 | if (!linkDriverNamespaceLocked(mDriverNamespace, vndkNamespace, mSphalLibraries)) { |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 575 | mDriverNamespace = nullptr; |
| 576 | } |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 577 | |
Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 578 | return mDriverNamespace; |
| 579 | } |
| 580 | |
Peiyong Lin | db3ed6e | 2020-01-09 18:43:27 -0800 | [diff] [blame] | 581 | std::string GraphicsEnv::getDriverPath() const { |
| 582 | return mDriverPath; |
| 583 | } |
| 584 | |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 585 | android_namespace_t* GraphicsEnv::getAngleNamespace() { |
Cody Northrop | 3892cfa | 2019-01-30 10:03:12 -0700 | [diff] [blame] | 586 | std::lock_guard<std::mutex> lock(mNamespaceMutex); |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 587 | |
Cody Northrop | 3892cfa | 2019-01-30 10:03:12 -0700 | [diff] [blame] | 588 | if (mAngleNamespace) { |
| 589 | return mAngleNamespace; |
| 590 | } |
| 591 | |
Peiyong Lin | e88e0ba | 2023-06-28 15:09:09 +0000 | [diff] [blame^] | 592 | if (mAnglePath.empty() && !mShouldUseSystemAngle) { |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 593 | ALOGV("mAnglePath is empty and not using system ANGLE, abort creating ANGLE namespace"); |
Cody Northrop | 3892cfa | 2019-01-30 10:03:12 -0700 | [diff] [blame] | 594 | return nullptr; |
| 595 | } |
| 596 | |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 597 | // Construct the search paths for system ANGLE. |
| 598 | const char* const defaultLibraryPaths = |
| 599 | #if defined(__LP64__) |
| 600 | "/vendor/lib64/egl:/system/lib64/egl"; |
| 601 | #else |
| 602 | "/vendor/lib/egl:/system/lib/egl"; |
| 603 | #endif |
| 604 | |
| 605 | // If the application process will run on top of system ANGLE, construct the namespace |
| 606 | // with sphal namespace being the parent namespace so that search paths and libraries |
| 607 | // are properly inherited. |
| 608 | mAngleNamespace = |
| 609 | android_create_namespace("ANGLE", |
Peiyong Lin | e88e0ba | 2023-06-28 15:09:09 +0000 | [diff] [blame^] | 610 | mShouldUseSystemAngle ? defaultLibraryPaths |
| 611 | : mAnglePath.c_str(), // ld_library_path |
| 612 | mShouldUseSystemAngle |
| 613 | ? defaultLibraryPaths |
| 614 | : mAnglePath.c_str(), // default_library_path |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 615 | ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED, |
| 616 | nullptr, // permitted_when_isolated_path |
Peiyong Lin | e88e0ba | 2023-06-28 15:09:09 +0000 | [diff] [blame^] | 617 | mShouldUseSystemAngle ? android_get_exported_namespace("sphal") |
| 618 | : nullptr); // parent |
Cody Northrop | 3892cfa | 2019-01-30 10:03:12 -0700 | [diff] [blame] | 619 | |
| 620 | ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default"); |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 621 | |
Peiyong Lin | e88e0ba | 2023-06-28 15:09:09 +0000 | [diff] [blame^] | 622 | if (!mShouldUseSystemAngle) { |
Peiyong Lin | 17190c9 | 2023-06-21 07:03:47 +0000 | [diff] [blame] | 623 | return mAngleNamespace; |
| 624 | } |
| 625 | |
| 626 | auto vndkNamespace = android_get_exported_namespace("vndk"); |
| 627 | if (!vndkNamespace) { |
| 628 | return nullptr; |
| 629 | } |
| 630 | |
| 631 | if (!linkDriverNamespaceLocked(mAngleNamespace, vndkNamespace, "")) { |
| 632 | mAngleNamespace = nullptr; |
| 633 | } |
| 634 | |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 635 | return mAngleNamespace; |
| 636 | } |
| 637 | |
Yuxin Hu | b69e988 | 2023-04-13 03:51:41 +0000 | [diff] [blame] | 638 | void GraphicsEnv::nativeToggleAngleAsSystemDriver(bool enabled) { |
| 639 | const sp<IGpuService> gpuService = getGpuService(); |
| 640 | if (!gpuService) { |
| 641 | ALOGE("No GPU service"); |
| 642 | return; |
| 643 | } |
| 644 | gpuService->toggleAngleAsSystemDriver(enabled); |
| 645 | } |
| 646 | |
Peiyong Lin | e88e0ba | 2023-06-28 15:09:09 +0000 | [diff] [blame^] | 647 | bool GraphicsEnv::shouldUseSystemAngle() { |
| 648 | return mShouldUseSystemAngle; |
| 649 | } |
| 650 | |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 651 | } // namespace android |