| 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 | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 32 | #include <cutils/properties.h> | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 33 | #include <graphicsenv/IGpuService.h> | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 34 | #include <log/log.h> | 
| Yiwei Zhang | 49b9ac7 | 2019-08-05 16:57:17 -0700 | [diff] [blame] | 35 | #include <nativeloader/dlext_namespaces.h> | 
| Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 36 | #include <sys/prctl.h> | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 37 | #include <utils/Trace.h> | 
| Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 38 |  | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 39 | #include <memory> | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 40 | #include <string> | 
| Yiwei Zhang | 3c74da9 | 2019-06-28 10:16:49 -0700 | [diff] [blame] | 41 | #include <thread> | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 42 |  | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 43 | // TODO(ianelliott@): Get the following from an ANGLE header: | 
 | 44 | #define CURRENT_ANGLE_API_VERSION 2 // Current API verion we are targetting | 
 | 45 | // Version-2 API: | 
 | 46 | typedef bool (*fpANGLEGetFeatureSupportUtilAPIVersion)(unsigned int* versionToUse); | 
 | 47 | typedef bool (*fpANGLEAndroidParseRulesString)(const char* rulesString, void** rulesHandle, | 
 | 48 |                                                int* rulesVersion); | 
 | 49 | typedef bool (*fpANGLEGetSystemInfo)(void** handle); | 
 | 50 | typedef bool (*fpANGLEAddDeviceInfoToSystemInfo)(const char* deviceMfr, const char* deviceModel, | 
 | 51 |                                                  void* handle); | 
 | 52 | typedef bool (*fpANGLEShouldBeUsedForApplication)(void* rulesHandle, int rulesVersion, | 
 | 53 |                                                   void* systemInfoHandle, const char* appName); | 
 | 54 | typedef bool (*fpANGLEFreeRulesHandle)(void* handle); | 
 | 55 | typedef bool (*fpANGLEFreeSystemInfoHandle)(void* handle); | 
 | 56 |  | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 57 | namespace android { | 
 | 58 |  | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 59 | enum NativeLibrary { | 
 | 60 |     LLNDK = 0, | 
 | 61 |     VNDKSP = 1, | 
 | 62 | }; | 
 | 63 |  | 
 | 64 | static constexpr const char* kNativeLibrariesSystemConfigPath[] = {"/etc/llndk.libraries.txt", | 
 | 65 |                                                                    "/etc/vndksp.libraries.txt"}; | 
 | 66 |  | 
 | 67 | static std::string vndkVersionStr() { | 
 | 68 | #ifdef __BIONIC__ | 
 | 69 |     std::string version = android::base::GetProperty("ro.vndk.version", ""); | 
 | 70 |     if (version != "" && version != "current") { | 
 | 71 |         return "." + version; | 
 | 72 |     } | 
 | 73 | #endif | 
 | 74 |     return ""; | 
 | 75 | } | 
 | 76 |  | 
 | 77 | static void insertVndkVersionStr(std::string* fileName) { | 
 | 78 |     LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr"); | 
 | 79 |     size_t insertPos = fileName->find_last_of("."); | 
 | 80 |     if (insertPos == std::string::npos) { | 
 | 81 |         insertPos = fileName->length(); | 
 | 82 |     } | 
 | 83 |     fileName->insert(insertPos, vndkVersionStr()); | 
 | 84 | } | 
 | 85 |  | 
 | 86 | static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) { | 
 | 87 |     // Read list of public native libraries from the config file. | 
 | 88 |     std::string fileContent; | 
 | 89 |     if (!base::ReadFileToString(configFile, &fileContent)) { | 
 | 90 |         return false; | 
 | 91 |     } | 
 | 92 |  | 
 | 93 |     std::vector<std::string> lines = base::Split(fileContent, "\n"); | 
 | 94 |  | 
 | 95 |     for (auto& line : lines) { | 
 | 96 |         auto trimmedLine = base::Trim(line); | 
 | 97 |         if (!trimmedLine.empty()) { | 
 | 98 |             soNames->push_back(trimmedLine); | 
 | 99 |         } | 
 | 100 |     } | 
 | 101 |  | 
 | 102 |     return true; | 
 | 103 | } | 
 | 104 |  | 
 | 105 | static const std::string getSystemNativeLibraries(NativeLibrary type) { | 
 | 106 |     static const char* androidRootEnv = getenv("ANDROID_ROOT"); | 
 | 107 |     static const std::string rootDir = androidRootEnv != nullptr ? androidRootEnv : "/system"; | 
 | 108 |  | 
 | 109 |     std::string nativeLibrariesSystemConfig = rootDir + kNativeLibrariesSystemConfigPath[type]; | 
 | 110 |  | 
 | 111 |     insertVndkVersionStr(&nativeLibrariesSystemConfig); | 
 | 112 |  | 
 | 113 |     std::vector<std::string> soNames; | 
 | 114 |     if (!readConfig(nativeLibrariesSystemConfig, &soNames)) { | 
 | 115 |         ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str()); | 
 | 116 |         return ""; | 
 | 117 |     } | 
 | 118 |  | 
 | 119 |     return base::Join(soNames, ':'); | 
 | 120 | } | 
 | 121 |  | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 122 | /*static*/ GraphicsEnv& GraphicsEnv::getInstance() { | 
 | 123 |     static GraphicsEnv env; | 
 | 124 |     return env; | 
 | 125 | } | 
 | 126 |  | 
| Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 127 | int GraphicsEnv::getCanLoadSystemLibraries() { | 
 | 128 |     if (property_get_bool("ro.debuggable", false) && prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) { | 
 | 129 |         // Return an integer value since this crosses library boundaries | 
 | 130 |         return 1; | 
 | 131 |     } | 
 | 132 |     return 0; | 
 | 133 | } | 
 | 134 |  | 
| Yiwei Zhang | 6ef8494 | 2019-02-14 12:28:12 -0800 | [diff] [blame] | 135 | void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string path, | 
 | 136 |                                                  const std::string sphalLibraries) { | 
 | 137 |     if (!mDriverPath.empty() || !mSphalLibraries.empty()) { | 
 | 138 |         ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries " | 
 | 139 |               "from '%s' to '%s'", | 
 | 140 |               mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str()); | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 141 |         return; | 
 | 142 |     } | 
| Yiwei Zhang | 6ef8494 | 2019-02-14 12:28:12 -0800 | [diff] [blame] | 143 |     ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(), | 
 | 144 |           sphalLibraries.c_str()); | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 145 |     mDriverPath = path; | 
| Yiwei Zhang | 6ef8494 | 2019-02-14 12:28:12 -0800 | [diff] [blame] | 146 |     mSphalLibraries = sphalLibraries; | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 147 | } | 
 | 148 |  | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 149 | void GraphicsEnv::hintActivityLaunch() { | 
 | 150 |     ATRACE_CALL(); | 
 | 151 |  | 
| Yiwei Zhang | 3c74da9 | 2019-06-28 10:16:49 -0700 | [diff] [blame] | 152 |     std::thread trySendGpuStatsThread([this]() { | 
 | 153 |         // If there's already graphics driver preloaded in the process, just send | 
 | 154 |         // the stats info to GpuStats directly through async binder. | 
 | 155 |         std::lock_guard<std::mutex> lock(mStatsLock); | 
 | 156 |         if (mGpuStats.glDriverToSend) { | 
 | 157 |             mGpuStats.glDriverToSend = false; | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 158 |             sendGpuStatsLocked(GpuStatsInfo::Api::API_GL, true, mGpuStats.glDriverLoadingTime); | 
| Yiwei Zhang | 3c74da9 | 2019-06-28 10:16:49 -0700 | [diff] [blame] | 159 |         } | 
 | 160 |         if (mGpuStats.vkDriverToSend) { | 
 | 161 |             mGpuStats.vkDriverToSend = false; | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 162 |             sendGpuStatsLocked(GpuStatsInfo::Api::API_VK, true, mGpuStats.vkDriverLoadingTime); | 
| Yiwei Zhang | 3c74da9 | 2019-06-28 10:16:49 -0700 | [diff] [blame] | 163 |         } | 
 | 164 |     }); | 
 | 165 |     trySendGpuStatsThread.detach(); | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 166 | } | 
 | 167 |  | 
| Greg Kaiser | 210bb7e | 2019-02-12 12:40:05 -0800 | [diff] [blame] | 168 | void GraphicsEnv::setGpuStats(const std::string& driverPackageName, | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 169 |                               const std::string& driverVersionName, uint64_t driverVersionCode, | 
| Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 170 |                               int64_t driverBuildTime, const std::string& appPackageName, | 
 | 171 |                               const int vulkanVersion) { | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 172 |     ATRACE_CALL(); | 
 | 173 |  | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 174 |     std::lock_guard<std::mutex> lock(mStatsLock); | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 175 |     ALOGV("setGpuStats:\n" | 
 | 176 |           "\tdriverPackageName[%s]\n" | 
 | 177 |           "\tdriverVersionName[%s]\n" | 
| Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 178 |           "\tdriverVersionCode[%" PRIu64 "]\n" | 
 | 179 |           "\tdriverBuildTime[%" PRId64 "]\n" | 
| Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 180 |           "\tappPackageName[%s]\n" | 
 | 181 |           "\tvulkanVersion[%d]\n", | 
| Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 182 |           driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime, | 
| Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 183 |           appPackageName.c_str(), vulkanVersion); | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 184 |  | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 185 |     mGpuStats.driverPackageName = driverPackageName; | 
 | 186 |     mGpuStats.driverVersionName = driverVersionName; | 
 | 187 |     mGpuStats.driverVersionCode = driverVersionCode; | 
| Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 188 |     mGpuStats.driverBuildTime = driverBuildTime; | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 189 |     mGpuStats.appPackageName = appPackageName; | 
| Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 190 |     mGpuStats.vulkanVersion = vulkanVersion; | 
| Yiwei Zhang | 8c8c181 | 2019-02-04 18:56:38 -0800 | [diff] [blame] | 191 | } | 
 | 192 |  | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 193 | void GraphicsEnv::setDriverToLoad(GpuStatsInfo::Driver driver) { | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 194 |     ATRACE_CALL(); | 
 | 195 |  | 
 | 196 |     std::lock_guard<std::mutex> lock(mStatsLock); | 
 | 197 |     switch (driver) { | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 198 |         case GpuStatsInfo::Driver::GL: | 
 | 199 |         case GpuStatsInfo::Driver::GL_UPDATED: | 
 | 200 |         case GpuStatsInfo::Driver::ANGLE: { | 
| Yiwei Zhang | 472cab0 | 2019-08-05 17:57:41 -0700 | [diff] [blame] | 201 |             if (mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::NONE || | 
 | 202 |                 mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::GL) { | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 203 |                 mGpuStats.glDriverToLoad = driver; | 
 | 204 |                 break; | 
 | 205 |             } | 
 | 206 |  | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 207 |             if (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE) { | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 208 |                 mGpuStats.glDriverFallback = driver; | 
 | 209 |             } | 
 | 210 |             break; | 
 | 211 |         } | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 212 |         case GpuStatsInfo::Driver::VULKAN: | 
 | 213 |         case GpuStatsInfo::Driver::VULKAN_UPDATED: { | 
| Yiwei Zhang | 472cab0 | 2019-08-05 17:57:41 -0700 | [diff] [blame] | 214 |             if (mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::NONE || | 
 | 215 |                 mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::VULKAN) { | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 216 |                 mGpuStats.vkDriverToLoad = driver; | 
 | 217 |                 break; | 
 | 218 |             } | 
 | 219 |  | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 220 |             if (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE) { | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 221 |                 mGpuStats.vkDriverFallback = driver; | 
 | 222 |             } | 
 | 223 |             break; | 
 | 224 |         } | 
 | 225 |         default: | 
 | 226 |             break; | 
 | 227 |     } | 
 | 228 | } | 
 | 229 |  | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 230 | void GraphicsEnv::setDriverLoaded(GpuStatsInfo::Api api, bool isDriverLoaded, | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 231 |                                   int64_t driverLoadingTime) { | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 232 |     ATRACE_CALL(); | 
 | 233 |  | 
 | 234 |     std::lock_guard<std::mutex> lock(mStatsLock); | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 235 |     const bool doNotSend = mGpuStats.appPackageName.empty(); | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 236 |     if (api == GpuStatsInfo::Api::API_GL) { | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 237 |         if (doNotSend) mGpuStats.glDriverToSend = true; | 
 | 238 |         mGpuStats.glDriverLoadingTime = driverLoadingTime; | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 239 |     } else { | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 240 |         if (doNotSend) mGpuStats.vkDriverToSend = true; | 
 | 241 |         mGpuStats.vkDriverLoadingTime = driverLoadingTime; | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 242 |     } | 
 | 243 |  | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 244 |     sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime); | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 245 | } | 
 | 246 |  | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 247 | static sp<IGpuService> getGpuService() { | 
| Yiwei Zhang | 8e09730 | 2019-07-08 16:11:12 -0700 | [diff] [blame] | 248 |     static const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu")); | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 249 |     if (!binder) { | 
 | 250 |         ALOGE("Failed to get gpu service"); | 
 | 251 |         return nullptr; | 
 | 252 |     } | 
 | 253 |  | 
 | 254 |     return interface_cast<IGpuService>(binder); | 
 | 255 | } | 
 | 256 |  | 
| Yiwei Zhang | bcba411 | 2019-07-03 13:39:32 -0700 | [diff] [blame] | 257 | void GraphicsEnv::setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value) { | 
| Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 258 |     ATRACE_CALL(); | 
 | 259 |  | 
| Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 260 |     std::lock_guard<std::mutex> lock(mStatsLock); | 
 | 261 |     const sp<IGpuService> gpuService = getGpuService(); | 
 | 262 |     if (gpuService) { | 
| Yiwei Zhang | bcba411 | 2019-07-03 13:39:32 -0700 | [diff] [blame] | 263 |         gpuService->setTargetStats(mGpuStats.appPackageName, mGpuStats.driverVersionCode, stats, | 
 | 264 |                                    value); | 
| Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 265 |     } | 
 | 266 | } | 
 | 267 |  | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 268 | void GraphicsEnv::sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded, | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 269 |                                      int64_t driverLoadingTime) { | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 270 |     ATRACE_CALL(); | 
 | 271 |  | 
 | 272 |     // Do not sendGpuStats for those skipping the GraphicsEnvironment setup | 
 | 273 |     if (mGpuStats.appPackageName.empty()) return; | 
 | 274 |  | 
 | 275 |     ALOGV("sendGpuStats:\n" | 
 | 276 |           "\tdriverPackageName[%s]\n" | 
 | 277 |           "\tdriverVersionName[%s]\n" | 
| Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 278 |           "\tdriverVersionCode[%" PRIu64 "]\n" | 
 | 279 |           "\tdriverBuildTime[%" PRId64 "]\n" | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 280 |           "\tappPackageName[%s]\n" | 
| Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 281 |           "\tvulkanVersion[%d]\n" | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 282 |           "\tapi[%d]\n" | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 283 |           "\tisDriverLoaded[%d]\n" | 
| Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 284 |           "\tdriverLoadingTime[%" PRId64 "]", | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 285 |           mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(), | 
| Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 286 |           mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(), | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 287 |           mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime); | 
 | 288 |  | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 289 |     GpuStatsInfo::Driver driver = GpuStatsInfo::Driver::NONE; | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 290 |     bool isIntendedDriverLoaded = false; | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 291 |     if (api == GpuStatsInfo::Api::API_GL) { | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 292 |         driver = mGpuStats.glDriverToLoad; | 
 | 293 |         isIntendedDriverLoaded = | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 294 |                 isDriverLoaded && (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE); | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 295 |     } else { | 
 | 296 |         driver = mGpuStats.vkDriverToLoad; | 
 | 297 |         isIntendedDriverLoaded = | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 298 |                 isDriverLoaded && (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE); | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 299 |     } | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 300 |  | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 301 |     const sp<IGpuService> gpuService = getGpuService(); | 
 | 302 |     if (gpuService) { | 
 | 303 |         gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName, | 
| Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 304 |                                 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, | 
| Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 305 |                                 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver, | 
| Yiwei Zhang | 5c640c1 | 2019-05-08 18:29:38 -0700 | [diff] [blame] | 306 |                                 isIntendedDriverLoaded, driverLoadingTime); | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 307 |     } | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 308 | } | 
 | 309 |  | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 310 | void* GraphicsEnv::loadLibrary(std::string name) { | 
 | 311 |     const android_dlextinfo dlextinfo = { | 
 | 312 |             .flags = ANDROID_DLEXT_USE_NAMESPACE, | 
 | 313 |             .library_namespace = getAngleNamespace(), | 
 | 314 |     }; | 
 | 315 |  | 
 | 316 |     std::string libName = std::string("lib") + name + "_angle.so"; | 
 | 317 |  | 
 | 318 |     void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo); | 
 | 319 |  | 
 | 320 |     if (so) { | 
 | 321 |         ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so); | 
 | 322 |         return so; | 
 | 323 |     } else { | 
 | 324 |         ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror()); | 
 | 325 |     } | 
 | 326 |  | 
 | 327 |     return nullptr; | 
 | 328 | } | 
 | 329 |  | 
 | 330 | bool GraphicsEnv::checkAngleRules(void* so) { | 
 | 331 |     char manufacturer[PROPERTY_VALUE_MAX]; | 
 | 332 |     char model[PROPERTY_VALUE_MAX]; | 
 | 333 |     property_get("ro.product.manufacturer", manufacturer, "UNSET"); | 
 | 334 |     property_get("ro.product.model", model, "UNSET"); | 
 | 335 |  | 
 | 336 |     auto ANGLEGetFeatureSupportUtilAPIVersion = | 
 | 337 |             (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so, | 
 | 338 |                                                           "ANGLEGetFeatureSupportUtilAPIVersion"); | 
 | 339 |  | 
 | 340 |     if (!ANGLEGetFeatureSupportUtilAPIVersion) { | 
 | 341 |         ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function"); | 
 | 342 |         return false; | 
 | 343 |     } | 
 | 344 |  | 
 | 345 |     // Negotiate the interface version by requesting most recent known to the platform | 
 | 346 |     unsigned int versionToUse = CURRENT_ANGLE_API_VERSION; | 
 | 347 |     if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) { | 
 | 348 |         ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, " | 
 | 349 |               "requested version %u", | 
 | 350 |               versionToUse); | 
 | 351 |         return false; | 
 | 352 |     } | 
 | 353 |  | 
 | 354 |     // Add and remove versions below as needed | 
 | 355 |     bool useAngle = false; | 
 | 356 |     switch (versionToUse) { | 
 | 357 |         case 2: { | 
 | 358 |             ALOGV("Using version %d of ANGLE feature-support library", versionToUse); | 
 | 359 |             void* rulesHandle = nullptr; | 
 | 360 |             int rulesVersion = 0; | 
 | 361 |             void* systemInfoHandle = nullptr; | 
 | 362 |  | 
 | 363 |             // Get the symbols for the feature-support-utility library: | 
 | 364 | #define GET_SYMBOL(symbol)                                                 \ | 
 | 365 |     fp##symbol symbol = (fp##symbol)dlsym(so, #symbol);                    \ | 
 | 366 |     if (!symbol) {                                                         \ | 
 | 367 |         ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \ | 
 | 368 |         break;                                                             \ | 
 | 369 |     } | 
 | 370 |             GET_SYMBOL(ANGLEAndroidParseRulesString); | 
 | 371 |             GET_SYMBOL(ANGLEGetSystemInfo); | 
 | 372 |             GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo); | 
 | 373 |             GET_SYMBOL(ANGLEShouldBeUsedForApplication); | 
 | 374 |             GET_SYMBOL(ANGLEFreeRulesHandle); | 
 | 375 |             GET_SYMBOL(ANGLEFreeSystemInfoHandle); | 
 | 376 |  | 
 | 377 |             // Parse the rules, obtain the SystemInfo, and evaluate the | 
 | 378 |             // application against the rules: | 
 | 379 |             if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) { | 
 | 380 |                 ALOGW("ANGLE feature-support library cannot parse rules file"); | 
 | 381 |                 break; | 
 | 382 |             } | 
 | 383 |             if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) { | 
 | 384 |                 ALOGW("ANGLE feature-support library cannot obtain SystemInfo"); | 
 | 385 |                 break; | 
 | 386 |             } | 
 | 387 |             if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) { | 
 | 388 |                 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo"); | 
 | 389 |                 break; | 
 | 390 |             } | 
 | 391 |             useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion, | 
 | 392 |                                                          systemInfoHandle, mAngleAppName.c_str()); | 
 | 393 |             (ANGLEFreeRulesHandle)(rulesHandle); | 
 | 394 |             (ANGLEFreeSystemInfoHandle)(systemInfoHandle); | 
 | 395 |         } break; | 
 | 396 |  | 
 | 397 |         default: | 
 | 398 |             ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse); | 
 | 399 |     } | 
 | 400 |  | 
 | 401 |     ALOGV("Close temporarily-loaded ANGLE opt-in/out logic"); | 
 | 402 |     return useAngle; | 
 | 403 | } | 
 | 404 |  | 
 | 405 | bool GraphicsEnv::shouldUseAngle(std::string appName) { | 
 | 406 |     if (appName != mAngleAppName) { | 
 | 407 |         // Make sure we are checking the app we were init'ed for | 
 | 408 |         ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(), | 
 | 409 |               appName.c_str()); | 
 | 410 |         return false; | 
 | 411 |     } | 
 | 412 |  | 
 | 413 |     return shouldUseAngle(); | 
 | 414 | } | 
 | 415 |  | 
 | 416 | bool GraphicsEnv::shouldUseAngle() { | 
 | 417 |     // Make sure we are init'ed | 
 | 418 |     if (mAngleAppName.empty()) { | 
| Cody Northrop | 2d7af74 | 2019-01-24 16:55:03 -0700 | [diff] [blame] | 419 |         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] | 420 |         return false; | 
 | 421 |     } | 
 | 422 |  | 
| Tim Van Patten | 8bd24e9 | 2019-02-08 10:16:40 -0700 | [diff] [blame] | 423 |     return (mUseAngle == YES) ? true : false; | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 424 | } | 
 | 425 |  | 
 | 426 | void GraphicsEnv::updateUseAngle() { | 
| Tim Van Patten | 8bd24e9 | 2019-02-08 10:16:40 -0700 | [diff] [blame] | 427 |     mUseAngle = NO; | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 428 |  | 
 | 429 |     const char* ANGLE_PREFER_ANGLE = "angle"; | 
 | 430 |     const char* ANGLE_PREFER_NATIVE = "native"; | 
 | 431 |  | 
 | 432 |     if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) { | 
 | 433 |         ALOGV("User set \"Developer Options\" to force the use of ANGLE"); | 
| Tim Van Patten | 8bd24e9 | 2019-02-08 10:16:40 -0700 | [diff] [blame] | 434 |         mUseAngle = YES; | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 435 |     } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) { | 
 | 436 |         ALOGV("User set \"Developer Options\" to force the use of Native"); | 
| Tim Van Patten | 8bd24e9 | 2019-02-08 10:16:40 -0700 | [diff] [blame] | 437 |         mUseAngle = NO; | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 438 |     } else { | 
 | 439 |         // The "Developer Options" value wasn't set to force the use of ANGLE.  Need to temporarily | 
 | 440 |         // load ANGLE and call the updatable opt-in/out logic: | 
| Cody Northrop | c15d382 | 2019-01-17 10:26:47 -0700 | [diff] [blame] | 441 |         void* featureSo = loadLibrary("feature_support"); | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 442 |         if (featureSo) { | 
 | 443 |             ALOGV("loaded ANGLE's opt-in/out logic from namespace"); | 
| Tim Van Patten | 8bd24e9 | 2019-02-08 10:16:40 -0700 | [diff] [blame] | 444 |             mUseAngle = checkAngleRules(featureSo) ? YES : NO; | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 445 |             dlclose(featureSo); | 
 | 446 |             featureSo = nullptr; | 
 | 447 |         } else { | 
 | 448 |             ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE."); | 
 | 449 |         } | 
 | 450 |     } | 
 | 451 | } | 
 | 452 |  | 
| Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame] | 453 | void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName, | 
| Tim Van Patten | a2a60a0 | 2018-11-09 16:51:15 -0700 | [diff] [blame] | 454 |                                const std::string developerOptIn, const int rulesFd, | 
 | 455 |                                const long rulesOffset, const long rulesLength) { | 
| Tim Van Patten | 8bd24e9 | 2019-02-08 10:16:40 -0700 | [diff] [blame] | 456 |     if (mUseAngle != UNKNOWN) { | 
 | 457 |         // We've already figured out an answer for this app, so just return. | 
 | 458 |         ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(), | 
 | 459 |               (mUseAngle == YES) ? "true" : "false"); | 
 | 460 |         return; | 
 | 461 |     } | 
 | 462 |  | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 463 |     ALOGV("setting ANGLE path to '%s'", path.c_str()); | 
 | 464 |     mAnglePath = path; | 
 | 465 |     ALOGV("setting ANGLE app name to '%s'", appName.c_str()); | 
 | 466 |     mAngleAppName = appName; | 
 | 467 |     ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str()); | 
 | 468 |     mAngleDeveloperOptIn = developerOptIn; | 
| Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame] | 469 |  | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 470 |     lseek(rulesFd, rulesOffset, SEEK_SET); | 
 | 471 |     mRulesBuffer = std::vector<char>(rulesLength + 1); | 
 | 472 |     ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength); | 
 | 473 |     if (numBytesRead < 0) { | 
 | 474 |         ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead); | 
 | 475 |         numBytesRead = 0; | 
 | 476 |     } else if (numBytesRead == 0) { | 
 | 477 |         ALOGW("Empty rules file"); | 
| Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame] | 478 |     } | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 479 |     if (numBytesRead != rulesLength) { | 
 | 480 |         ALOGW("Did not read all of the necessary bytes from the rules file." | 
 | 481 |               "expected: %ld, got: %zd", | 
 | 482 |               rulesLength, numBytesRead); | 
| Tim Van Patten | a2a60a0 | 2018-11-09 16:51:15 -0700 | [diff] [blame] | 483 |     } | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 484 |     mRulesBuffer[numBytesRead] = '\0'; | 
| Cody Northrop | 04e7043 | 2018-09-06 10:34:58 -0600 | [diff] [blame] | 485 |  | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 486 |     // Update the current status of whether we should use ANGLE or not | 
 | 487 |     updateUseAngle(); | 
| Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 488 | } | 
 | 489 |  | 
| Victor Khimenko | 4819b52 | 2018-07-13 17:24:18 +0200 | [diff] [blame] | 490 | void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) { | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 491 |     if (mLayerPaths.empty()) { | 
 | 492 |         mLayerPaths = layerPaths; | 
 | 493 |         mAppNamespace = appNamespace; | 
 | 494 |     } else { | 
 | 495 |         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] | 496 |               layerPaths.c_str(), appNamespace); | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 497 |     } | 
 | 498 | } | 
 | 499 |  | 
| Victor Khimenko | 4819b52 | 2018-07-13 17:24:18 +0200 | [diff] [blame] | 500 | NativeLoaderNamespace* GraphicsEnv::getAppNamespace() { | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 501 |     return mAppNamespace; | 
 | 502 | } | 
 | 503 |  | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 504 | std::string& GraphicsEnv::getAngleAppName() { | 
 | 505 |     return mAngleAppName; | 
| Cody Northrop | 04e7043 | 2018-09-06 10:34:58 -0600 | [diff] [blame] | 506 | } | 
 | 507 |  | 
| Courtney Goeltzenleuchter | 30ad2ab | 2018-10-30 08:20:44 -0600 | [diff] [blame] | 508 | const std::string& GraphicsEnv::getLayerPaths() { | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 509 |     return mLayerPaths; | 
 | 510 | } | 
 | 511 |  | 
| Courtney Goeltzenleuchter | 30ad2ab | 2018-10-30 08:20:44 -0600 | [diff] [blame] | 512 | const std::string& GraphicsEnv::getDebugLayers() { | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 513 |     return mDebugLayers; | 
 | 514 | } | 
 | 515 |  | 
| Cody Northrop | b9b01b6 | 2018-10-23 13:13:10 -0600 | [diff] [blame] | 516 | const std::string& GraphicsEnv::getDebugLayersGLES() { | 
 | 517 |     return mDebugLayersGLES; | 
 | 518 | } | 
 | 519 |  | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 520 | void GraphicsEnv::setDebugLayers(const std::string layers) { | 
 | 521 |     mDebugLayers = layers; | 
 | 522 | } | 
 | 523 |  | 
| Cody Northrop | b9b01b6 | 2018-10-23 13:13:10 -0600 | [diff] [blame] | 524 | void GraphicsEnv::setDebugLayersGLES(const std::string layers) { | 
 | 525 |     mDebugLayersGLES = layers; | 
 | 526 | } | 
 | 527 |  | 
| Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 528 | // Return true if all the required libraries from vndk and sphal namespace are | 
 | 529 | // linked to the Game Driver namespace correctly. | 
 | 530 | bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* vndkNamespace) { | 
 | 531 |     const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK); | 
 | 532 |     if (llndkLibraries.empty()) { | 
 | 533 |         return false; | 
 | 534 |     } | 
 | 535 |     if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) { | 
 | 536 |         ALOGE("Failed to link default namespace[%s]", dlerror()); | 
 | 537 |         return false; | 
 | 538 |     } | 
 | 539 |  | 
 | 540 |     const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP); | 
 | 541 |     if (vndkspLibraries.empty()) { | 
 | 542 |         return false; | 
 | 543 |     } | 
 | 544 |     if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) { | 
 | 545 |         ALOGE("Failed to link vndk namespace[%s]", dlerror()); | 
 | 546 |         return false; | 
 | 547 |     } | 
 | 548 |  | 
 | 549 |     if (mSphalLibraries.empty()) { | 
 | 550 |         return true; | 
 | 551 |     } | 
 | 552 |  | 
 | 553 |     // Make additional libraries in sphal to be accessible | 
 | 554 |     auto sphalNamespace = android_get_exported_namespace("sphal"); | 
 | 555 |     if (!sphalNamespace) { | 
 | 556 |         ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace", | 
 | 557 |               mSphalLibraries.c_str()); | 
 | 558 |         return false; | 
 | 559 |     } | 
 | 560 |  | 
 | 561 |     if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) { | 
 | 562 |         ALOGE("Failed to link sphal namespace[%s]", dlerror()); | 
 | 563 |         return false; | 
 | 564 |     } | 
 | 565 |  | 
 | 566 |     return true; | 
 | 567 | } | 
 | 568 |  | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 569 | android_namespace_t* GraphicsEnv::getDriverNamespace() { | 
| Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 570 |     std::lock_guard<std::mutex> lock(mNamespaceMutex); | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 571 |  | 
| Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 572 |     if (mDriverNamespace) { | 
 | 573 |         return mDriverNamespace; | 
 | 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 |     if (mDriverPath.empty()) { | 
 | 577 |         return nullptr; | 
 | 578 |     } | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 579 |  | 
| Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 580 |     auto vndkNamespace = android_get_exported_namespace("vndk"); | 
 | 581 |     if (!vndkNamespace) { | 
 | 582 |         return nullptr; | 
 | 583 |     } | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 584 |  | 
| Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 585 |     mDriverNamespace = android_create_namespace("gfx driver", | 
 | 586 |                                                 mDriverPath.c_str(), // ld_library_path | 
 | 587 |                                                 mDriverPath.c_str(), // default_library_path | 
 | 588 |                                                 ANDROID_NAMESPACE_TYPE_ISOLATED, | 
 | 589 |                                                 nullptr, // permitted_when_isolated_path | 
 | 590 |                                                 nullptr); | 
| Yiwei Zhang | 6ef8494 | 2019-02-14 12:28:12 -0800 | [diff] [blame] | 591 |  | 
| Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 592 |     if (!linkDriverNamespaceLocked(vndkNamespace)) { | 
 | 593 |         mDriverNamespace = nullptr; | 
 | 594 |     } | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 595 |  | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 596 |     return mDriverNamespace; | 
 | 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 | 
 | 614 |                                                ANDROID_NAMESPACE_TYPE_SHARED | | 
 | 615 |                                                        ANDROID_NAMESPACE_TYPE_ISOLATED, | 
 | 616 |                                                nullptr, // permitted_when_isolated_path | 
 | 617 |                                                nullptr); | 
 | 618 |  | 
 | 619 |     ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default"); | 
| Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 620 |  | 
 | 621 |     return mAngleNamespace; | 
 | 622 | } | 
 | 623 |  | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 624 | } // namespace android |