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