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