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