| 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() { | 
|  | 129 | return prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) > 0; | 
| Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 130 | } | 
|  | 131 |  | 
| Yiwei Zhang | 6ef8494 | 2019-02-14 12:28:12 -0800 | [diff] [blame] | 132 | void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string path, | 
|  | 133 | const std::string sphalLibraries) { | 
|  | 134 | if (!mDriverPath.empty() || !mSphalLibraries.empty()) { | 
|  | 135 | ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries " | 
|  | 136 | "from '%s' to '%s'", | 
|  | 137 | mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str()); | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 138 | return; | 
|  | 139 | } | 
| Yiwei Zhang | 6ef8494 | 2019-02-14 12:28:12 -0800 | [diff] [blame] | 140 | ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(), | 
|  | 141 | sphalLibraries.c_str()); | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 142 | mDriverPath = path; | 
| Yiwei Zhang | 6ef8494 | 2019-02-14 12:28:12 -0800 | [diff] [blame] | 143 | mSphalLibraries = sphalLibraries; | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 146 | void GraphicsEnv::hintActivityLaunch() { | 
|  | 147 | ATRACE_CALL(); | 
|  | 148 |  | 
| Yiwei Zhang | d381938 | 2020-01-07 19:53:56 -0800 | [diff] [blame] | 149 | { | 
|  | 150 | std::lock_guard<std::mutex> lock(mStatsLock); | 
|  | 151 | if (mActivityLaunched) return; | 
|  | 152 | mActivityLaunched = true; | 
|  | 153 | } | 
|  | 154 |  | 
| Yiwei Zhang | 3c74da9 | 2019-06-28 10:16:49 -0700 | [diff] [blame] | 155 | std::thread trySendGpuStatsThread([this]() { | 
|  | 156 | // If there's already graphics driver preloaded in the process, just send | 
|  | 157 | // the stats info to GpuStats directly through async binder. | 
|  | 158 | std::lock_guard<std::mutex> lock(mStatsLock); | 
|  | 159 | if (mGpuStats.glDriverToSend) { | 
|  | 160 | mGpuStats.glDriverToSend = false; | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 161 | sendGpuStatsLocked(GpuStatsInfo::Api::API_GL, true, mGpuStats.glDriverLoadingTime); | 
| Yiwei Zhang | 3c74da9 | 2019-06-28 10:16:49 -0700 | [diff] [blame] | 162 | } | 
|  | 163 | if (mGpuStats.vkDriverToSend) { | 
|  | 164 | mGpuStats.vkDriverToSend = false; | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 165 | sendGpuStatsLocked(GpuStatsInfo::Api::API_VK, true, mGpuStats.vkDriverLoadingTime); | 
| Yiwei Zhang | 3c74da9 | 2019-06-28 10:16:49 -0700 | [diff] [blame] | 166 | } | 
|  | 167 | }); | 
|  | 168 | trySendGpuStatsThread.detach(); | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 169 | } | 
|  | 170 |  | 
| Greg Kaiser | 210bb7e | 2019-02-12 12:40:05 -0800 | [diff] [blame] | 171 | void GraphicsEnv::setGpuStats(const std::string& driverPackageName, | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 172 | const std::string& driverVersionName, uint64_t driverVersionCode, | 
| Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 173 | int64_t driverBuildTime, const std::string& appPackageName, | 
|  | 174 | const int vulkanVersion) { | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 175 | ATRACE_CALL(); | 
|  | 176 |  | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 177 | std::lock_guard<std::mutex> lock(mStatsLock); | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 178 | ALOGV("setGpuStats:\n" | 
|  | 179 | "\tdriverPackageName[%s]\n" | 
|  | 180 | "\tdriverVersionName[%s]\n" | 
| Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 181 | "\tdriverVersionCode[%" PRIu64 "]\n" | 
|  | 182 | "\tdriverBuildTime[%" PRId64 "]\n" | 
| Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 183 | "\tappPackageName[%s]\n" | 
|  | 184 | "\tvulkanVersion[%d]\n", | 
| Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 185 | driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime, | 
| Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 186 | appPackageName.c_str(), vulkanVersion); | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 187 |  | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 188 | mGpuStats.driverPackageName = driverPackageName; | 
|  | 189 | mGpuStats.driverVersionName = driverVersionName; | 
|  | 190 | mGpuStats.driverVersionCode = driverVersionCode; | 
| Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 191 | mGpuStats.driverBuildTime = driverBuildTime; | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 192 | mGpuStats.appPackageName = appPackageName; | 
| Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 193 | mGpuStats.vulkanVersion = vulkanVersion; | 
| Yiwei Zhang | 8c8c181 | 2019-02-04 18:56:38 -0800 | [diff] [blame] | 194 | } | 
|  | 195 |  | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 196 | void GraphicsEnv::setDriverToLoad(GpuStatsInfo::Driver driver) { | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 197 | ATRACE_CALL(); | 
|  | 198 |  | 
|  | 199 | std::lock_guard<std::mutex> lock(mStatsLock); | 
|  | 200 | switch (driver) { | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 201 | case GpuStatsInfo::Driver::GL: | 
|  | 202 | case GpuStatsInfo::Driver::GL_UPDATED: | 
|  | 203 | case GpuStatsInfo::Driver::ANGLE: { | 
| Yiwei Zhang | 472cab0 | 2019-08-05 17:57:41 -0700 | [diff] [blame] | 204 | if (mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::NONE || | 
|  | 205 | mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::GL) { | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 206 | mGpuStats.glDriverToLoad = driver; | 
|  | 207 | break; | 
|  | 208 | } | 
|  | 209 |  | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 210 | if (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE) { | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 211 | mGpuStats.glDriverFallback = driver; | 
|  | 212 | } | 
|  | 213 | break; | 
|  | 214 | } | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 215 | case GpuStatsInfo::Driver::VULKAN: | 
|  | 216 | case GpuStatsInfo::Driver::VULKAN_UPDATED: { | 
| Yiwei Zhang | 472cab0 | 2019-08-05 17:57:41 -0700 | [diff] [blame] | 217 | if (mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::NONE || | 
|  | 218 | mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::VULKAN) { | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 219 | mGpuStats.vkDriverToLoad = driver; | 
|  | 220 | break; | 
|  | 221 | } | 
|  | 222 |  | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 223 | if (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE) { | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 224 | mGpuStats.vkDriverFallback = driver; | 
|  | 225 | } | 
|  | 226 | break; | 
|  | 227 | } | 
|  | 228 | default: | 
|  | 229 | break; | 
|  | 230 | } | 
|  | 231 | } | 
|  | 232 |  | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 233 | void GraphicsEnv::setDriverLoaded(GpuStatsInfo::Api api, bool isDriverLoaded, | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 234 | int64_t driverLoadingTime) { | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 235 | ATRACE_CALL(); | 
|  | 236 |  | 
|  | 237 | std::lock_guard<std::mutex> lock(mStatsLock); | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 238 | if (api == GpuStatsInfo::Api::API_GL) { | 
| Yiwei Zhang | d381938 | 2020-01-07 19:53:56 -0800 | [diff] [blame] | 239 | mGpuStats.glDriverToSend = true; | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 240 | mGpuStats.glDriverLoadingTime = driverLoadingTime; | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 241 | } else { | 
| Yiwei Zhang | d381938 | 2020-01-07 19:53:56 -0800 | [diff] [blame] | 242 | mGpuStats.vkDriverToSend = true; | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 243 | mGpuStats.vkDriverLoadingTime = driverLoadingTime; | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 244 | } | 
|  | 245 |  | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 246 | sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime); | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 247 | } | 
|  | 248 |  | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 249 | static sp<IGpuService> getGpuService() { | 
| Yiwei Zhang | 8e09730 | 2019-07-08 16:11:12 -0700 | [diff] [blame] | 250 | static const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu")); | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 251 | if (!binder) { | 
|  | 252 | ALOGE("Failed to get gpu service"); | 
|  | 253 | return nullptr; | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | return interface_cast<IGpuService>(binder); | 
|  | 257 | } | 
|  | 258 |  | 
| Yiwei Zhang | d381938 | 2020-01-07 19:53:56 -0800 | [diff] [blame] | 259 | bool GraphicsEnv::readyToSendGpuStatsLocked() { | 
|  | 260 | // Only send stats for processes having at least one activity launched and that process doesn't | 
|  | 261 | // skip the GraphicsEnvironment setup. | 
|  | 262 | return mActivityLaunched && !mGpuStats.appPackageName.empty(); | 
|  | 263 | } | 
|  | 264 |  | 
| Yiwei Zhang | bcba411 | 2019-07-03 13:39:32 -0700 | [diff] [blame] | 265 | void GraphicsEnv::setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value) { | 
| Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 266 | ATRACE_CALL(); | 
|  | 267 |  | 
| Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 268 | std::lock_guard<std::mutex> lock(mStatsLock); | 
| Yiwei Zhang | d381938 | 2020-01-07 19:53:56 -0800 | [diff] [blame] | 269 | if (!readyToSendGpuStatsLocked()) return; | 
|  | 270 |  | 
| Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 271 | const sp<IGpuService> gpuService = getGpuService(); | 
|  | 272 | if (gpuService) { | 
| Yiwei Zhang | bcba411 | 2019-07-03 13:39:32 -0700 | [diff] [blame] | 273 | gpuService->setTargetStats(mGpuStats.appPackageName, mGpuStats.driverVersionCode, stats, | 
|  | 274 | value); | 
| Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 275 | } | 
|  | 276 | } | 
|  | 277 |  | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 278 | void GraphicsEnv::sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded, | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 279 | int64_t driverLoadingTime) { | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 280 | ATRACE_CALL(); | 
|  | 281 |  | 
| Yiwei Zhang | d381938 | 2020-01-07 19:53:56 -0800 | [diff] [blame] | 282 | if (!readyToSendGpuStatsLocked()) return; | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 283 |  | 
|  | 284 | ALOGV("sendGpuStats:\n" | 
|  | 285 | "\tdriverPackageName[%s]\n" | 
|  | 286 | "\tdriverVersionName[%s]\n" | 
| Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 287 | "\tdriverVersionCode[%" PRIu64 "]\n" | 
|  | 288 | "\tdriverBuildTime[%" PRId64 "]\n" | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 289 | "\tappPackageName[%s]\n" | 
| Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 290 | "\tvulkanVersion[%d]\n" | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 291 | "\tapi[%d]\n" | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 292 | "\tisDriverLoaded[%d]\n" | 
| Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 293 | "\tdriverLoadingTime[%" PRId64 "]", | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 294 | mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(), | 
| Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 295 | mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(), | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 296 | mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime); | 
|  | 297 |  | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 298 | GpuStatsInfo::Driver driver = GpuStatsInfo::Driver::NONE; | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 299 | bool isIntendedDriverLoaded = false; | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 300 | if (api == GpuStatsInfo::Api::API_GL) { | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 301 | driver = mGpuStats.glDriverToLoad; | 
|  | 302 | isIntendedDriverLoaded = | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 303 | isDriverLoaded && (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE); | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 304 | } else { | 
|  | 305 | driver = mGpuStats.vkDriverToLoad; | 
|  | 306 | isIntendedDriverLoaded = | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 307 | isDriverLoaded && (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE); | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 308 | } | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 309 |  | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 310 | const sp<IGpuService> gpuService = getGpuService(); | 
|  | 311 | if (gpuService) { | 
|  | 312 | gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName, | 
| Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 313 | mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, | 
| Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 314 | mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver, | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 315 | isIntendedDriverLoaded, driverLoadingTime); | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 316 | } | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 317 | } | 
|  | 318 |  | 
| Adam Bodnar | 0afcca0 | 2019-09-17 13:23:17 -0700 | [diff] [blame] | 319 | bool GraphicsEnv::setInjectLayersPrSetDumpable() { | 
|  | 320 | if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) { | 
|  | 321 | return false; | 
|  | 322 | } | 
|  | 323 | return true; | 
|  | 324 | } | 
|  | 325 |  | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 326 | void* GraphicsEnv::loadLibrary(std::string name) { | 
|  | 327 | const android_dlextinfo dlextinfo = { | 
|  | 328 | .flags = ANDROID_DLEXT_USE_NAMESPACE, | 
|  | 329 | .library_namespace = getAngleNamespace(), | 
|  | 330 | }; | 
|  | 331 |  | 
|  | 332 | std::string libName = std::string("lib") + name + "_angle.so"; | 
|  | 333 |  | 
|  | 334 | void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo); | 
|  | 335 |  | 
|  | 336 | if (so) { | 
|  | 337 | ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so); | 
|  | 338 | return so; | 
|  | 339 | } else { | 
|  | 340 | ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror()); | 
|  | 341 | } | 
|  | 342 |  | 
|  | 343 | return nullptr; | 
|  | 344 | } | 
|  | 345 |  | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 346 | bool GraphicsEnv::shouldUseAngle(std::string appName) { | 
|  | 347 | if (appName != mAngleAppName) { | 
|  | 348 | // Make sure we are checking the app we were init'ed for | 
|  | 349 | ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(), | 
|  | 350 | appName.c_str()); | 
|  | 351 | return false; | 
|  | 352 | } | 
|  | 353 |  | 
|  | 354 | return shouldUseAngle(); | 
|  | 355 | } | 
|  | 356 |  | 
|  | 357 | bool GraphicsEnv::shouldUseAngle() { | 
|  | 358 | // Make sure we are init'ed | 
|  | 359 | if (mAngleAppName.empty()) { | 
| Cody Northrop | 2d7af74 | 2019-01-24 16:55:03 -0700 | [diff] [blame] | 360 | 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] | 361 | return false; | 
|  | 362 | } | 
|  | 363 |  | 
| Tim Van Patten | 8bd24e9 | 2019-02-08 10:16:40 -0700 | [diff] [blame] | 364 | return (mUseAngle == YES) ? true : false; | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 365 | } | 
|  | 366 |  | 
| Ian Elliott | 7c0a7ee | 2022-04-21 12:41:03 -0600 | [diff] [blame] | 367 | bool GraphicsEnv::angleIsSystemDriver() { | 
|  | 368 | // Make sure we are init'ed | 
|  | 369 | if (mAngleAppName.empty()) { | 
|  | 370 | ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE."); | 
|  | 371 | return false; | 
|  | 372 | } | 
|  | 373 |  | 
|  | 374 | return (mAngleIsSystemDriver == YES) ? true : false; | 
|  | 375 | } | 
|  | 376 |  | 
|  | 377 | bool GraphicsEnv::shouldForceLegacyDriver() { | 
|  | 378 | // Make sure we are init'ed | 
|  | 379 | if (mAngleAppName.empty()) { | 
|  | 380 | ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE."); | 
|  | 381 | return false; | 
|  | 382 | } | 
|  | 383 |  | 
|  | 384 | return (mAngleIsSystemDriver == YES && mUseAngle == NO) ? true : false; | 
|  | 385 | } | 
|  | 386 |  | 
|  | 387 | std::string GraphicsEnv::getLegacySuffix() { | 
|  | 388 | return mLegacyDriverSuffix; | 
|  | 389 | } | 
|  | 390 |  | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 391 | void GraphicsEnv::updateUseAngle() { | 
| Tim Van Patten | 8bd24e9 | 2019-02-08 10:16:40 -0700 | [diff] [blame] | 392 | mUseAngle = NO; | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 393 |  | 
|  | 394 | const char* ANGLE_PREFER_ANGLE = "angle"; | 
| Ian Elliott | 7c0a7ee | 2022-04-21 12:41:03 -0600 | [diff] [blame] | 395 | const char* ANGLE_PREFER_LEGACY = "legacy"; | 
|  | 396 | // The following is a deprecated version of "legacy" | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 397 | const char* ANGLE_PREFER_NATIVE = "native"; | 
|  | 398 |  | 
| Tim Van Patten | a53dcb2 | 2021-05-21 17:51:59 -0600 | [diff] [blame] | 399 | mUseAngle = NO; | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 400 | if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) { | 
| Ian Elliott | 7c0a7ee | 2022-04-21 12:41:03 -0600 | [diff] [blame] | 401 | ALOGI("Using ANGLE, the %s GLES driver for package '%s'", | 
|  | 402 | mAngleIsSystemDriver == YES ? "system" : "optional", mAngleAppName.c_str()); | 
| Tim Van Patten | 8bd24e9 | 2019-02-08 10:16:40 -0700 | [diff] [blame] | 403 | mUseAngle = YES; | 
| Ian Elliott | 7c0a7ee | 2022-04-21 12:41:03 -0600 | [diff] [blame] | 404 | } else if (mAngleDeveloperOptIn == ANGLE_PREFER_LEGACY || | 
|  | 405 | mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) { | 
|  | 406 | ALOGI("Using the (%s) Legacy GLES driver for package '%s'", | 
|  | 407 | mAngleIsSystemDriver == YES ? "optional" : "system", mAngleAppName.c_str()); | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 408 | } else { | 
| Tim Van Patten | a53dcb2 | 2021-05-21 17:51:59 -0600 | [diff] [blame] | 409 | ALOGV("User set invalid \"Developer Options\": '%s'", mAngleDeveloperOptIn.c_str()); | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 410 | } | 
|  | 411 | } | 
|  | 412 |  | 
| Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame] | 413 | void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName, | 
| Ian Elliott | 7c0a7ee | 2022-04-21 12:41:03 -0600 | [diff] [blame] | 414 | const bool angleIsSystemDriver, const std::string developerOptIn, | 
| Tim Van Patten | a53dcb2 | 2021-05-21 17:51:59 -0600 | [diff] [blame] | 415 | const std::vector<std::string> eglFeatures) { | 
| Ian Elliott | 7c0a7ee | 2022-04-21 12:41:03 -0600 | [diff] [blame] | 416 | // Set whether ANGLE is the system driver: | 
|  | 417 | mAngleIsSystemDriver = angleIsSystemDriver ? YES : NO; | 
|  | 418 |  | 
|  | 419 | // Note: Given the current logic and lack of the old rules file processing, | 
|  | 420 | // there seems to be little chance that mUseAngle != UNKNOWN.  Leave this | 
|  | 421 | // for now, even though it seems outdated. | 
| Tim Van Patten | 8bd24e9 | 2019-02-08 10:16:40 -0700 | [diff] [blame] | 422 | if (mUseAngle != UNKNOWN) { | 
|  | 423 | // We've already figured out an answer for this app, so just return. | 
|  | 424 | ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(), | 
|  | 425 | (mUseAngle == YES) ? "true" : "false"); | 
|  | 426 | return; | 
|  | 427 | } | 
|  | 428 |  | 
| Peiyong Lin | 2f707e6 | 2020-09-26 13:52:10 -0700 | [diff] [blame] | 429 | mAngleEglFeatures = std::move(eglFeatures); | 
|  | 430 |  | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 431 | ALOGV("setting ANGLE path to '%s'", path.c_str()); | 
|  | 432 | mAnglePath = path; | 
|  | 433 | ALOGV("setting ANGLE app name to '%s'", appName.c_str()); | 
|  | 434 | mAngleAppName = appName; | 
|  | 435 | ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str()); | 
|  | 436 | mAngleDeveloperOptIn = developerOptIn; | 
| Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame] | 437 |  | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 438 | // Update the current status of whether we should use ANGLE or not | 
|  | 439 | updateUseAngle(); | 
| Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 440 | } | 
|  | 441 |  | 
| Ian Elliott | 7c0a7ee | 2022-04-21 12:41:03 -0600 | [diff] [blame] | 442 | void GraphicsEnv::setLegacyDriverInfo(const std::string appName, const bool angleIsSystemDriver, | 
|  | 443 | const std::string legacyDriverName) { | 
|  | 444 | ALOGV("setting legacy app name to '%s'", appName.c_str()); | 
|  | 445 | mAngleAppName = appName; | 
|  | 446 |  | 
|  | 447 | // Force the use of the legacy driver instead of ANGLE | 
|  | 448 | const char* ANGLE_PREFER_LEGACY = "legacy"; | 
|  | 449 | mAngleDeveloperOptIn = ANGLE_PREFER_LEGACY; | 
|  | 450 | ALOGV("setting ANGLE application opt-in to 'legacy'"); | 
|  | 451 |  | 
|  | 452 | // Set whether ANGLE is the system driver: | 
|  | 453 | mAngleIsSystemDriver = angleIsSystemDriver ? YES : NO; | 
|  | 454 |  | 
|  | 455 | mLegacyDriverSuffix = legacyDriverName; | 
|  | 456 |  | 
|  | 457 | // Update the current status of whether we should use ANGLE or not | 
|  | 458 | updateUseAngle(); | 
|  | 459 | } | 
|  | 460 |  | 
| Victor Khimenko | 4819b52 | 2018-07-13 17:24:18 +0200 | [diff] [blame] | 461 | void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) { | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 462 | if (mLayerPaths.empty()) { | 
|  | 463 | mLayerPaths = layerPaths; | 
|  | 464 | mAppNamespace = appNamespace; | 
|  | 465 | } else { | 
|  | 466 | 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] | 467 | layerPaths.c_str(), appNamespace); | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 468 | } | 
|  | 469 | } | 
|  | 470 |  | 
| Victor Khimenko | 4819b52 | 2018-07-13 17:24:18 +0200 | [diff] [blame] | 471 | NativeLoaderNamespace* GraphicsEnv::getAppNamespace() { | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 472 | return mAppNamespace; | 
|  | 473 | } | 
|  | 474 |  | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 475 | std::string& GraphicsEnv::getAngleAppName() { | 
|  | 476 | return mAngleAppName; | 
| Cody Northrop | 04e7043 | 2018-09-06 10:34:58 -0600 | [diff] [blame] | 477 | } | 
|  | 478 |  | 
| Peiyong Lin | 2f707e6 | 2020-09-26 13:52:10 -0700 | [diff] [blame] | 479 | const std::vector<std::string>& GraphicsEnv::getAngleEglFeatures() { | 
|  | 480 | return mAngleEglFeatures; | 
|  | 481 | } | 
|  | 482 |  | 
| Courtney Goeltzenleuchter | 30ad2ab | 2018-10-30 08:20:44 -0600 | [diff] [blame] | 483 | const std::string& GraphicsEnv::getLayerPaths() { | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 484 | return mLayerPaths; | 
|  | 485 | } | 
|  | 486 |  | 
| Courtney Goeltzenleuchter | 30ad2ab | 2018-10-30 08:20:44 -0600 | [diff] [blame] | 487 | const std::string& GraphicsEnv::getDebugLayers() { | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 488 | return mDebugLayers; | 
|  | 489 | } | 
|  | 490 |  | 
| Cody Northrop | b9b01b6 | 2018-10-23 13:13:10 -0600 | [diff] [blame] | 491 | const std::string& GraphicsEnv::getDebugLayersGLES() { | 
|  | 492 | return mDebugLayersGLES; | 
|  | 493 | } | 
|  | 494 |  | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 495 | void GraphicsEnv::setDebugLayers(const std::string layers) { | 
|  | 496 | mDebugLayers = layers; | 
|  | 497 | } | 
|  | 498 |  | 
| Cody Northrop | b9b01b6 | 2018-10-23 13:13:10 -0600 | [diff] [blame] | 499 | void GraphicsEnv::setDebugLayersGLES(const std::string layers) { | 
|  | 500 | mDebugLayersGLES = layers; | 
|  | 501 | } | 
|  | 502 |  | 
| Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 503 | // 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] | 504 | // linked to the updatable gfx driver namespace correctly. | 
| Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 505 | bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* vndkNamespace) { | 
|  | 506 | const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK); | 
|  | 507 | if (llndkLibraries.empty()) { | 
|  | 508 | return false; | 
|  | 509 | } | 
|  | 510 | if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) { | 
|  | 511 | ALOGE("Failed to link default namespace[%s]", dlerror()); | 
|  | 512 | return false; | 
|  | 513 | } | 
|  | 514 |  | 
|  | 515 | const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP); | 
|  | 516 | if (vndkspLibraries.empty()) { | 
|  | 517 | return false; | 
|  | 518 | } | 
|  | 519 | if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) { | 
|  | 520 | ALOGE("Failed to link vndk namespace[%s]", dlerror()); | 
|  | 521 | return false; | 
|  | 522 | } | 
|  | 523 |  | 
|  | 524 | if (mSphalLibraries.empty()) { | 
|  | 525 | return true; | 
|  | 526 | } | 
|  | 527 |  | 
|  | 528 | // Make additional libraries in sphal to be accessible | 
|  | 529 | auto sphalNamespace = android_get_exported_namespace("sphal"); | 
|  | 530 | if (!sphalNamespace) { | 
|  | 531 | ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace", | 
|  | 532 | mSphalLibraries.c_str()); | 
|  | 533 | return false; | 
|  | 534 | } | 
|  | 535 |  | 
|  | 536 | if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) { | 
|  | 537 | ALOGE("Failed to link sphal namespace[%s]", dlerror()); | 
|  | 538 | return false; | 
|  | 539 | } | 
|  | 540 |  | 
|  | 541 | return true; | 
|  | 542 | } | 
|  | 543 |  | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 544 | android_namespace_t* GraphicsEnv::getDriverNamespace() { | 
| Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 545 | std::lock_guard<std::mutex> lock(mNamespaceMutex); | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 546 |  | 
| Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 547 | if (mDriverNamespace) { | 
|  | 548 | return mDriverNamespace; | 
|  | 549 | } | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 550 |  | 
| Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 551 | if (mDriverPath.empty()) { | 
| Peiyong Lin | 0acbfdc | 2020-06-17 18:47:12 -0700 | [diff] [blame] | 552 | // For an application process, driver path is empty means this application is not opted in | 
|  | 553 | // to use updatable driver. Application process doesn't have the ability to set up | 
|  | 554 | // environment variables and hence before `getenv` call will return. | 
|  | 555 | // For a process that is not an application process, if it's run from an environment, | 
|  | 556 | // for example shell, where environment variables can be set, then it can opt into using | 
|  | 557 | // udpatable driver by setting UPDATABLE_GFX_DRIVER to 1. By setting to 1 the developer | 
|  | 558 | // driver will be used currently. | 
|  | 559 | // TODO(b/159240322) Support the production updatable driver. | 
|  | 560 | const char* id = getenv("UPDATABLE_GFX_DRIVER"); | 
|  | 561 | if (id == nullptr || std::strcmp(id, "1")) { | 
|  | 562 | return nullptr; | 
|  | 563 | } | 
|  | 564 | const sp<IGpuService> gpuService = getGpuService(); | 
|  | 565 | if (!gpuService) { | 
|  | 566 | return nullptr; | 
|  | 567 | } | 
|  | 568 | mDriverPath = gpuService->getUpdatableDriverPath(); | 
|  | 569 | if (mDriverPath.empty()) { | 
|  | 570 | return nullptr; | 
|  | 571 | } | 
|  | 572 | mDriverPath.append(UPDATABLE_DRIVER_ABI); | 
|  | 573 | 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] | 574 | } | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 575 |  | 
| Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 576 | auto vndkNamespace = android_get_exported_namespace("vndk"); | 
|  | 577 | if (!vndkNamespace) { | 
|  | 578 | return nullptr; | 
|  | 579 | } | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 580 |  | 
| Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 581 | mDriverNamespace = android_create_namespace("gfx driver", | 
|  | 582 | mDriverPath.c_str(), // ld_library_path | 
|  | 583 | mDriverPath.c_str(), // default_library_path | 
|  | 584 | ANDROID_NAMESPACE_TYPE_ISOLATED, | 
|  | 585 | nullptr, // permitted_when_isolated_path | 
|  | 586 | nullptr); | 
| Yiwei Zhang | 6ef8494 | 2019-02-14 12:28:12 -0800 | [diff] [blame] | 587 |  | 
| Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 588 | if (!linkDriverNamespaceLocked(vndkNamespace)) { | 
|  | 589 | mDriverNamespace = nullptr; | 
|  | 590 | } | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 591 |  | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 592 | return mDriverNamespace; | 
|  | 593 | } | 
|  | 594 |  | 
| Peiyong Lin | db3ed6e | 2020-01-09 18:43:27 -0800 | [diff] [blame] | 595 | std::string GraphicsEnv::getDriverPath() const { | 
|  | 596 | return mDriverPath; | 
|  | 597 | } | 
|  | 598 |  | 
| Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 599 | android_namespace_t* GraphicsEnv::getAngleNamespace() { | 
| Cody Northrop | 3892cfa | 2019-01-30 10:03:12 -0700 | [diff] [blame] | 600 | std::lock_guard<std::mutex> lock(mNamespaceMutex); | 
| Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 601 |  | 
| Cody Northrop | 3892cfa | 2019-01-30 10:03:12 -0700 | [diff] [blame] | 602 | if (mAngleNamespace) { | 
|  | 603 | return mAngleNamespace; | 
|  | 604 | } | 
|  | 605 |  | 
|  | 606 | if (mAnglePath.empty()) { | 
|  | 607 | ALOGV("mAnglePath is empty, not creating ANGLE namespace"); | 
|  | 608 | return nullptr; | 
|  | 609 | } | 
|  | 610 |  | 
|  | 611 | mAngleNamespace = android_create_namespace("ANGLE", | 
|  | 612 | nullptr,            // ld_library_path | 
|  | 613 | mAnglePath.c_str(), // default_library_path | 
| Yiwei Zhang | 5b7604f | 2020-05-08 14:23:34 -0700 | [diff] [blame] | 614 | ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED, | 
| Cody Northrop | 3892cfa | 2019-01-30 10:03:12 -0700 | [diff] [blame] | 615 | nullptr, // permitted_when_isolated_path | 
|  | 616 | nullptr); | 
|  | 617 |  | 
|  | 618 | ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default"); | 
| Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 619 |  | 
|  | 620 | return mAngleNamespace; | 
|  | 621 | } | 
|  | 622 |  | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 623 | } // namespace android |