| 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 |  | 
 | 17 | //#define LOG_NDEBUG 1 | 
 | 18 | #define LOG_TAG "GraphicsEnv" | 
| Jiyong Park | 27c39e1 | 2017-05-08 13:00:02 +0900 | [diff] [blame] | 19 | #include <graphicsenv/GraphicsEnv.h> | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 20 |  | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 21 | #include <dlfcn.h> | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 22 | #include <unistd.h> | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 23 |  | 
 | 24 | #include <android-base/file.h> | 
 | 25 | #include <android-base/properties.h> | 
 | 26 | #include <android-base/strings.h> | 
 | 27 | #include <android/dlext.h> | 
 | 28 | #include <cutils/properties.h> | 
 | 29 | #include <log/log.h> | 
| Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 30 | #include <sys/prctl.h> | 
 | 31 |  | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 32 | #include <memory> | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 33 | #include <mutex> | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 34 | #include <string> | 
 | 35 |  | 
 | 36 | #include <dlfcn.h> | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 37 |  | 
| Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 38 | // TODO(b/37049319) Get this from a header once one exists | 
 | 39 | extern "C" { | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 40 | android_namespace_t* android_get_exported_namespace(const char*); | 
 | 41 | android_namespace_t* android_create_namespace(const char* name, const char* ld_library_path, | 
 | 42 |                                               const char* default_library_path, uint64_t type, | 
 | 43 |                                               const char* permitted_when_isolated_path, | 
 | 44 |                                               android_namespace_t* parent); | 
 | 45 | bool android_link_namespaces(android_namespace_t* from, android_namespace_t* to, | 
 | 46 |                              const char* shared_libs_sonames); | 
| Jiyong Park | 9b816a8 | 2018-01-02 17:37:37 +0900 | [diff] [blame] | 47 |  | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 48 | enum { | 
 | 49 |     ANDROID_NAMESPACE_TYPE_ISOLATED = 1, | 
 | 50 |     ANDROID_NAMESPACE_TYPE_SHARED = 2, | 
 | 51 | }; | 
| Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 52 | } | 
 | 53 |  | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 54 | // TODO(ianelliott@): Get the following from an ANGLE header: | 
 | 55 | #define CURRENT_ANGLE_API_VERSION 2 // Current API verion we are targetting | 
 | 56 | // Version-2 API: | 
 | 57 | typedef bool (*fpANGLEGetFeatureSupportUtilAPIVersion)(unsigned int* versionToUse); | 
 | 58 | typedef bool (*fpANGLEAndroidParseRulesString)(const char* rulesString, void** rulesHandle, | 
 | 59 |                                                int* rulesVersion); | 
 | 60 | typedef bool (*fpANGLEGetSystemInfo)(void** handle); | 
 | 61 | typedef bool (*fpANGLEAddDeviceInfoToSystemInfo)(const char* deviceMfr, const char* deviceModel, | 
 | 62 |                                                  void* handle); | 
 | 63 | typedef bool (*fpANGLEShouldBeUsedForApplication)(void* rulesHandle, int rulesVersion, | 
 | 64 |                                                   void* systemInfoHandle, const char* appName); | 
 | 65 | typedef bool (*fpANGLEFreeRulesHandle)(void* handle); | 
 | 66 | typedef bool (*fpANGLEFreeSystemInfoHandle)(void* handle); | 
 | 67 |  | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 68 | namespace android { | 
 | 69 |  | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 70 | enum NativeLibrary { | 
 | 71 |     LLNDK = 0, | 
 | 72 |     VNDKSP = 1, | 
 | 73 | }; | 
 | 74 |  | 
 | 75 | static constexpr const char* kNativeLibrariesSystemConfigPath[] = {"/etc/llndk.libraries.txt", | 
 | 76 |                                                                    "/etc/vndksp.libraries.txt"}; | 
 | 77 |  | 
 | 78 | static std::string vndkVersionStr() { | 
 | 79 | #ifdef __BIONIC__ | 
 | 80 |     std::string version = android::base::GetProperty("ro.vndk.version", ""); | 
 | 81 |     if (version != "" && version != "current") { | 
 | 82 |         return "." + version; | 
 | 83 |     } | 
 | 84 | #endif | 
 | 85 |     return ""; | 
 | 86 | } | 
 | 87 |  | 
 | 88 | static void insertVndkVersionStr(std::string* fileName) { | 
 | 89 |     LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr"); | 
 | 90 |     size_t insertPos = fileName->find_last_of("."); | 
 | 91 |     if (insertPos == std::string::npos) { | 
 | 92 |         insertPos = fileName->length(); | 
 | 93 |     } | 
 | 94 |     fileName->insert(insertPos, vndkVersionStr()); | 
 | 95 | } | 
 | 96 |  | 
 | 97 | static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) { | 
 | 98 |     // Read list of public native libraries from the config file. | 
 | 99 |     std::string fileContent; | 
 | 100 |     if (!base::ReadFileToString(configFile, &fileContent)) { | 
 | 101 |         return false; | 
 | 102 |     } | 
 | 103 |  | 
 | 104 |     std::vector<std::string> lines = base::Split(fileContent, "\n"); | 
 | 105 |  | 
 | 106 |     for (auto& line : lines) { | 
 | 107 |         auto trimmedLine = base::Trim(line); | 
 | 108 |         if (!trimmedLine.empty()) { | 
 | 109 |             soNames->push_back(trimmedLine); | 
 | 110 |         } | 
 | 111 |     } | 
 | 112 |  | 
 | 113 |     return true; | 
 | 114 | } | 
 | 115 |  | 
 | 116 | static const std::string getSystemNativeLibraries(NativeLibrary type) { | 
 | 117 |     static const char* androidRootEnv = getenv("ANDROID_ROOT"); | 
 | 118 |     static const std::string rootDir = androidRootEnv != nullptr ? androidRootEnv : "/system"; | 
 | 119 |  | 
 | 120 |     std::string nativeLibrariesSystemConfig = rootDir + kNativeLibrariesSystemConfigPath[type]; | 
 | 121 |  | 
 | 122 |     insertVndkVersionStr(&nativeLibrariesSystemConfig); | 
 | 123 |  | 
 | 124 |     std::vector<std::string> soNames; | 
 | 125 |     if (!readConfig(nativeLibrariesSystemConfig, &soNames)) { | 
 | 126 |         ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str()); | 
 | 127 |         return ""; | 
 | 128 |     } | 
 | 129 |  | 
 | 130 |     return base::Join(soNames, ':'); | 
 | 131 | } | 
 | 132 |  | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 133 | /*static*/ GraphicsEnv& GraphicsEnv::getInstance() { | 
 | 134 |     static GraphicsEnv env; | 
 | 135 |     return env; | 
 | 136 | } | 
 | 137 |  | 
| Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 138 | int GraphicsEnv::getCanLoadSystemLibraries() { | 
 | 139 |     if (property_get_bool("ro.debuggable", false) && prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) { | 
 | 140 |         // Return an integer value since this crosses library boundaries | 
 | 141 |         return 1; | 
 | 142 |     } | 
 | 143 |     return 0; | 
 | 144 | } | 
 | 145 |  | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 146 | void GraphicsEnv::setDriverPath(const std::string path) { | 
 | 147 |     if (!mDriverPath.empty()) { | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 148 |         ALOGV("ignoring attempt to change driver path from '%s' to '%s'", mDriverPath.c_str(), | 
 | 149 |               path.c_str()); | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 150 |         return; | 
 | 151 |     } | 
 | 152 |     ALOGV("setting driver path to '%s'", path.c_str()); | 
 | 153 |     mDriverPath = path; | 
 | 154 | } | 
 | 155 |  | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 156 | void* GraphicsEnv::loadLibrary(std::string name) { | 
 | 157 |     const android_dlextinfo dlextinfo = { | 
 | 158 |             .flags = ANDROID_DLEXT_USE_NAMESPACE, | 
 | 159 |             .library_namespace = getAngleNamespace(), | 
 | 160 |     }; | 
 | 161 |  | 
 | 162 |     std::string libName = std::string("lib") + name + "_angle.so"; | 
 | 163 |  | 
 | 164 |     void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo); | 
 | 165 |  | 
 | 166 |     if (so) { | 
 | 167 |         ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so); | 
 | 168 |         return so; | 
 | 169 |     } else { | 
 | 170 |         ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror()); | 
 | 171 |     } | 
 | 172 |  | 
 | 173 |     return nullptr; | 
 | 174 | } | 
 | 175 |  | 
 | 176 | bool GraphicsEnv::checkAngleRules(void* so) { | 
 | 177 |     char manufacturer[PROPERTY_VALUE_MAX]; | 
 | 178 |     char model[PROPERTY_VALUE_MAX]; | 
 | 179 |     property_get("ro.product.manufacturer", manufacturer, "UNSET"); | 
 | 180 |     property_get("ro.product.model", model, "UNSET"); | 
 | 181 |  | 
 | 182 |     auto ANGLEGetFeatureSupportUtilAPIVersion = | 
 | 183 |             (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so, | 
 | 184 |                                                           "ANGLEGetFeatureSupportUtilAPIVersion"); | 
 | 185 |  | 
 | 186 |     if (!ANGLEGetFeatureSupportUtilAPIVersion) { | 
 | 187 |         ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function"); | 
 | 188 |         return false; | 
 | 189 |     } | 
 | 190 |  | 
 | 191 |     // Negotiate the interface version by requesting most recent known to the platform | 
 | 192 |     unsigned int versionToUse = CURRENT_ANGLE_API_VERSION; | 
 | 193 |     if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) { | 
 | 194 |         ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, " | 
 | 195 |               "requested version %u", | 
 | 196 |               versionToUse); | 
 | 197 |         return false; | 
 | 198 |     } | 
 | 199 |  | 
 | 200 |     // Add and remove versions below as needed | 
 | 201 |     bool useAngle = false; | 
 | 202 |     switch (versionToUse) { | 
 | 203 |         case 2: { | 
 | 204 |             ALOGV("Using version %d of ANGLE feature-support library", versionToUse); | 
 | 205 |             void* rulesHandle = nullptr; | 
 | 206 |             int rulesVersion = 0; | 
 | 207 |             void* systemInfoHandle = nullptr; | 
 | 208 |  | 
 | 209 |             // Get the symbols for the feature-support-utility library: | 
 | 210 | #define GET_SYMBOL(symbol)                                                 \ | 
 | 211 |     fp##symbol symbol = (fp##symbol)dlsym(so, #symbol);                    \ | 
 | 212 |     if (!symbol) {                                                         \ | 
 | 213 |         ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \ | 
 | 214 |         break;                                                             \ | 
 | 215 |     } | 
 | 216 |             GET_SYMBOL(ANGLEAndroidParseRulesString); | 
 | 217 |             GET_SYMBOL(ANGLEGetSystemInfo); | 
 | 218 |             GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo); | 
 | 219 |             GET_SYMBOL(ANGLEShouldBeUsedForApplication); | 
 | 220 |             GET_SYMBOL(ANGLEFreeRulesHandle); | 
 | 221 |             GET_SYMBOL(ANGLEFreeSystemInfoHandle); | 
 | 222 |  | 
 | 223 |             // Parse the rules, obtain the SystemInfo, and evaluate the | 
 | 224 |             // application against the rules: | 
 | 225 |             if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) { | 
 | 226 |                 ALOGW("ANGLE feature-support library cannot parse rules file"); | 
 | 227 |                 break; | 
 | 228 |             } | 
 | 229 |             if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) { | 
 | 230 |                 ALOGW("ANGLE feature-support library cannot obtain SystemInfo"); | 
 | 231 |                 break; | 
 | 232 |             } | 
 | 233 |             if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) { | 
 | 234 |                 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo"); | 
 | 235 |                 break; | 
 | 236 |             } | 
 | 237 |             useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion, | 
 | 238 |                                                          systemInfoHandle, mAngleAppName.c_str()); | 
 | 239 |             (ANGLEFreeRulesHandle)(rulesHandle); | 
 | 240 |             (ANGLEFreeSystemInfoHandle)(systemInfoHandle); | 
 | 241 |         } break; | 
 | 242 |  | 
 | 243 |         default: | 
 | 244 |             ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse); | 
 | 245 |     } | 
 | 246 |  | 
 | 247 |     ALOGV("Close temporarily-loaded ANGLE opt-in/out logic"); | 
 | 248 |     return useAngle; | 
 | 249 | } | 
 | 250 |  | 
 | 251 | bool GraphicsEnv::shouldUseAngle(std::string appName) { | 
 | 252 |     if (appName != mAngleAppName) { | 
 | 253 |         // Make sure we are checking the app we were init'ed for | 
 | 254 |         ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(), | 
 | 255 |               appName.c_str()); | 
 | 256 |         return false; | 
 | 257 |     } | 
 | 258 |  | 
 | 259 |     return shouldUseAngle(); | 
 | 260 | } | 
 | 261 |  | 
 | 262 | bool GraphicsEnv::shouldUseAngle() { | 
 | 263 |     // Make sure we are init'ed | 
 | 264 |     if (mAngleAppName.empty()) { | 
 | 265 |         ALOGE("App name is empty. setAngleInfo() must be called first to enable ANGLE."); | 
 | 266 |         return false; | 
 | 267 |     } | 
 | 268 |  | 
 | 269 |     return mUseAngle; | 
 | 270 | } | 
 | 271 |  | 
 | 272 | void GraphicsEnv::updateUseAngle() { | 
 | 273 |     mUseAngle = false; | 
 | 274 |  | 
 | 275 |     const char* ANGLE_PREFER_ANGLE = "angle"; | 
 | 276 |     const char* ANGLE_PREFER_NATIVE = "native"; | 
 | 277 |  | 
 | 278 |     if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) { | 
 | 279 |         ALOGV("User set \"Developer Options\" to force the use of ANGLE"); | 
 | 280 |         mUseAngle = true; | 
 | 281 |     } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) { | 
 | 282 |         ALOGV("User set \"Developer Options\" to force the use of Native"); | 
 | 283 |         mUseAngle = false; | 
 | 284 |     } else { | 
 | 285 |         // The "Developer Options" value wasn't set to force the use of ANGLE.  Need to temporarily | 
 | 286 |         // load ANGLE and call the updatable opt-in/out logic: | 
| Jakub Pawlowski | 6a5f97c | 2019-01-10 00:25:41 +0000 | [diff] [blame] | 287 |  | 
 | 288 |         // Check if ANGLE is enabled. Workaround for several bugs: | 
 | 289 |         // b/119305693 b/119322355 b/119305887 | 
 | 290 |         // Something is not working correctly in the feature library | 
 | 291 |         char prop[PROPERTY_VALUE_MAX]; | 
 | 292 |         property_get("debug.angle.enable", prop, "0"); | 
 | 293 |         void* featureSo = nullptr; | 
 | 294 |         if (atoi(prop)) { | 
 | 295 |             featureSo = loadLibrary("feature_support"); | 
 | 296 |         } | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 297 |         if (featureSo) { | 
 | 298 |             ALOGV("loaded ANGLE's opt-in/out logic from namespace"); | 
 | 299 |             mUseAngle = checkAngleRules(featureSo); | 
 | 300 |             dlclose(featureSo); | 
 | 301 |             featureSo = nullptr; | 
 | 302 |         } else { | 
 | 303 |             ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE."); | 
 | 304 |         } | 
 | 305 |     } | 
 | 306 | } | 
 | 307 |  | 
| Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame] | 308 | void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName, | 
| Tim Van Patten | a2a60a0 | 2018-11-09 16:51:15 -0700 | [diff] [blame] | 309 |                                const std::string developerOptIn, const int rulesFd, | 
 | 310 |                                const long rulesOffset, const long rulesLength) { | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 311 |     ALOGV("setting ANGLE path to '%s'", path.c_str()); | 
 | 312 |     mAnglePath = path; | 
 | 313 |     ALOGV("setting ANGLE app name to '%s'", appName.c_str()); | 
 | 314 |     mAngleAppName = appName; | 
 | 315 |     ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str()); | 
 | 316 |     mAngleDeveloperOptIn = developerOptIn; | 
| Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame] | 317 |  | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 318 |     lseek(rulesFd, rulesOffset, SEEK_SET); | 
 | 319 |     mRulesBuffer = std::vector<char>(rulesLength + 1); | 
 | 320 |     ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength); | 
 | 321 |     if (numBytesRead < 0) { | 
 | 322 |         ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead); | 
 | 323 |         numBytesRead = 0; | 
 | 324 |     } else if (numBytesRead == 0) { | 
 | 325 |         ALOGW("Empty rules file"); | 
| Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame] | 326 |     } | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 327 |     if (numBytesRead != rulesLength) { | 
 | 328 |         ALOGW("Did not read all of the necessary bytes from the rules file." | 
 | 329 |               "expected: %ld, got: %zd", | 
 | 330 |               rulesLength, numBytesRead); | 
| Tim Van Patten | a2a60a0 | 2018-11-09 16:51:15 -0700 | [diff] [blame] | 331 |     } | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 332 |     mRulesBuffer[numBytesRead] = '\0'; | 
| Cody Northrop | 04e7043 | 2018-09-06 10:34:58 -0600 | [diff] [blame] | 333 |  | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 334 |     // Update the current status of whether we should use ANGLE or not | 
 | 335 |     updateUseAngle(); | 
| Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 336 | } | 
 | 337 |  | 
| Victor Khimenko | 4819b52 | 2018-07-13 17:24:18 +0200 | [diff] [blame] | 338 | void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) { | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 339 |     if (mLayerPaths.empty()) { | 
 | 340 |         mLayerPaths = layerPaths; | 
 | 341 |         mAppNamespace = appNamespace; | 
 | 342 |     } else { | 
 | 343 |         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] | 344 |               layerPaths.c_str(), appNamespace); | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 345 |     } | 
 | 346 | } | 
 | 347 |  | 
| Victor Khimenko | 4819b52 | 2018-07-13 17:24:18 +0200 | [diff] [blame] | 348 | NativeLoaderNamespace* GraphicsEnv::getAppNamespace() { | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 349 |     return mAppNamespace; | 
 | 350 | } | 
 | 351 |  | 
| Tim Van Patten | 5f744f1 | 2018-12-12 11:46:21 -0700 | [diff] [blame] | 352 | std::string& GraphicsEnv::getAngleAppName() { | 
 | 353 |     return mAngleAppName; | 
| Cody Northrop | 04e7043 | 2018-09-06 10:34:58 -0600 | [diff] [blame] | 354 | } | 
 | 355 |  | 
| Courtney Goeltzenleuchter | 30ad2ab | 2018-10-30 08:20:44 -0600 | [diff] [blame] | 356 | const std::string& GraphicsEnv::getLayerPaths() { | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 357 |     return mLayerPaths; | 
 | 358 | } | 
 | 359 |  | 
| Courtney Goeltzenleuchter | 30ad2ab | 2018-10-30 08:20:44 -0600 | [diff] [blame] | 360 | const std::string& GraphicsEnv::getDebugLayers() { | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 361 |     return mDebugLayers; | 
 | 362 | } | 
 | 363 |  | 
| Cody Northrop | b9b01b6 | 2018-10-23 13:13:10 -0600 | [diff] [blame] | 364 | const std::string& GraphicsEnv::getDebugLayersGLES() { | 
 | 365 |     return mDebugLayersGLES; | 
 | 366 | } | 
 | 367 |  | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 368 | void GraphicsEnv::setDebugLayers(const std::string layers) { | 
 | 369 |     mDebugLayers = layers; | 
 | 370 | } | 
 | 371 |  | 
| Cody Northrop | b9b01b6 | 2018-10-23 13:13:10 -0600 | [diff] [blame] | 372 | void GraphicsEnv::setDebugLayersGLES(const std::string layers) { | 
 | 373 |     mDebugLayersGLES = layers; | 
 | 374 | } | 
 | 375 |  | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 376 | android_namespace_t* GraphicsEnv::getDriverNamespace() { | 
 | 377 |     static std::once_flag once; | 
 | 378 |     std::call_once(once, [this]() { | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 379 |         if (mDriverPath.empty()) return; | 
 | 380 |  | 
 | 381 |         auto vndkNamespace = android_get_exported_namespace("vndk"); | 
 | 382 |         if (!vndkNamespace) return; | 
 | 383 |  | 
| Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 384 |         mDriverNamespace = android_create_namespace("gfx driver", | 
| Peiyong Lin | 2e9c74c | 2018-10-24 11:18:07 -0700 | [diff] [blame] | 385 |                                                     mDriverPath.c_str(), // ld_library_path | 
| Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 386 |                                                     mDriverPath.c_str(), // default_library_path | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 387 |                                                     ANDROID_NAMESPACE_TYPE_ISOLATED, | 
| Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 388 |                                                     nullptr, // permitted_when_isolated_path | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 389 |                                                     nullptr); | 
 | 390 |  | 
 | 391 |         const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK); | 
 | 392 |         if (llndkLibraries.empty()) { | 
 | 393 |             mDriverNamespace = nullptr; | 
 | 394 |             return; | 
 | 395 |         } | 
 | 396 |         if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) { | 
 | 397 |             ALOGE("Failed to link default namespace[%s]", dlerror()); | 
 | 398 |             mDriverNamespace = nullptr; | 
 | 399 |             return; | 
 | 400 |         } | 
 | 401 |  | 
 | 402 |         const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP); | 
 | 403 |         if (vndkspLibraries.empty()) { | 
 | 404 |             mDriverNamespace = nullptr; | 
 | 405 |             return; | 
 | 406 |         } | 
 | 407 |         if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) { | 
 | 408 |             ALOGE("Failed to link vndk namespace[%s]", dlerror()); | 
 | 409 |             mDriverNamespace = nullptr; | 
 | 410 |             return; | 
 | 411 |         } | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 412 |     }); | 
| Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 413 |  | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 414 |     return mDriverNamespace; | 
 | 415 | } | 
 | 416 |  | 
| Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 417 | android_namespace_t* GraphicsEnv::getAngleNamespace() { | 
 | 418 |     static std::once_flag once; | 
 | 419 |     std::call_once(once, [this]() { | 
 | 420 |         if (mAnglePath.empty()) return; | 
 | 421 |  | 
 | 422 |         mAngleNamespace = android_create_namespace("ANGLE", | 
 | 423 |                                                    nullptr,            // ld_library_path | 
 | 424 |                                                    mAnglePath.c_str(), // default_library_path | 
 | 425 |                                                    ANDROID_NAMESPACE_TYPE_SHARED | | 
 | 426 |                                                            ANDROID_NAMESPACE_TYPE_ISOLATED, | 
 | 427 |                                                    nullptr, // permitted_when_isolated_path | 
 | 428 |                                                    nullptr); | 
 | 429 |         if (!mAngleNamespace) ALOGD("Could not create ANGLE namespace from default"); | 
 | 430 |     }); | 
 | 431 |  | 
 | 432 |     return mAngleNamespace; | 
 | 433 | } | 
 | 434 |  | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 435 | } // namespace android |