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 == GraphicsEnv::Driver::GL) {
218 mGpuStats.glDriverToLoad = driver;
219 break;
220 }
221
222 if (mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE) {
223 mGpuStats.glDriverFallback = driver;
224 }
225 break;
226 }
227 case Driver::VULKAN:
228 case Driver::VULKAN_UPDATED: {
229 if (mGpuStats.vkDriverToLoad == GraphicsEnv::Driver::NONE ||
230 mGpuStats.vkDriverToLoad == GraphicsEnv::Driver::VULKAN) {
231 mGpuStats.vkDriverToLoad = driver;
232 break;
233 }
234
235 if (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE) {
236 mGpuStats.vkDriverFallback = driver;
237 }
238 break;
239 }
240 default:
241 break;
242 }
243 }
244
setDriverLoaded(GraphicsEnv::Api api,bool isDriverLoaded,int64_t driverLoadingTime)245 void GraphicsEnv::setDriverLoaded(GraphicsEnv::Api api, bool isDriverLoaded,
246 int64_t driverLoadingTime) {
247 ATRACE_CALL();
248
249 std::lock_guard<std::mutex> lock(mStatsLock);
250 const bool doNotSend = mGpuStats.appPackageName.empty();
251 if (api == GraphicsEnv::Api::API_GL) {
252 if (doNotSend) mGpuStats.glDriverToSend = true;
253 mGpuStats.glDriverLoadingTime = driverLoadingTime;
254 } else {
255 if (doNotSend) mGpuStats.vkDriverToSend = true;
256 mGpuStats.vkDriverLoadingTime = driverLoadingTime;
257 }
258
259 sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime);
260 }
261
getGpuService()262 static sp<IGpuService> getGpuService() {
263 const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
264 if (!binder) {
265 ALOGE("Failed to get gpu service");
266 return nullptr;
267 }
268
269 return interface_cast<IGpuService>(binder);
270 }
271
setTargetStats(const Stats stats,const uint64_t value)272 void GraphicsEnv::setTargetStats(const Stats stats, const uint64_t value) {
273 ATRACE_CALL();
274
275 std::lock_guard<std::mutex> lock(mStatsLock);
276 const sp<IGpuService> gpuService = getGpuService();
277 if (gpuService) {
278 gpuService->setTargetStats(mGpuStats.appPackageName, mGpuStats.driverVersionCode, stats,
279 value);
280 }
281 }
282
sendGpuStatsLocked(GraphicsEnv::Api api,bool isDriverLoaded,int64_t driverLoadingTime)283 void GraphicsEnv::sendGpuStatsLocked(GraphicsEnv::Api api, bool isDriverLoaded,
284 int64_t driverLoadingTime) {
285 ATRACE_CALL();
286
287 // Do not sendGpuStats for those skipping the GraphicsEnvironment setup
288 if (mGpuStats.appPackageName.empty()) return;
289
290 ALOGV("sendGpuStats:\n"
291 "\tdriverPackageName[%s]\n"
292 "\tdriverVersionName[%s]\n"
293 "\tdriverVersionCode[%" PRIu64 "]\n"
294 "\tdriverBuildTime[%" PRId64 "]\n"
295 "\tappPackageName[%s]\n"
296 "\tvulkanVersion[%d]\n"
297 "\tapi[%d]\n"
298 "\tisDriverLoaded[%d]\n"
299 "\tdriverLoadingTime[%" PRId64 "]",
300 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
301 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
302 mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime);
303
304 GraphicsEnv::Driver driver = GraphicsEnv::Driver::NONE;
305 bool isIntendedDriverLoaded = false;
306 if (api == GraphicsEnv::Api::API_GL) {
307 driver = mGpuStats.glDriverToLoad;
308 isIntendedDriverLoaded =
309 isDriverLoaded && (mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE);
310 } else {
311 driver = mGpuStats.vkDriverToLoad;
312 isIntendedDriverLoaded =
313 isDriverLoaded && (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE);
314 }
315
316 const sp<IGpuService> gpuService = getGpuService();
317 if (gpuService) {
318 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
319 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
320 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
321 isIntendedDriverLoaded, driverLoadingTime);
322 }
323 }
324
loadLibrary(std::string name)325 void* GraphicsEnv::loadLibrary(std::string name) {
326 const android_dlextinfo dlextinfo = {
327 .flags = ANDROID_DLEXT_USE_NAMESPACE,
328 .library_namespace = getAngleNamespace(),
329 };
330
331 std::string libName = std::string("lib") + name + "_angle.so";
332
333 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
334
335 if (so) {
336 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
337 return so;
338 } else {
339 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
340 }
341
342 return nullptr;
343 }
344
checkAngleRules(void * so)345 bool GraphicsEnv::checkAngleRules(void* so) {
346 char manufacturer[PROPERTY_VALUE_MAX];
347 char model[PROPERTY_VALUE_MAX];
348 property_get("ro.product.manufacturer", manufacturer, "UNSET");
349 property_get("ro.product.model", model, "UNSET");
350
351 auto ANGLEGetFeatureSupportUtilAPIVersion =
352 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
353 "ANGLEGetFeatureSupportUtilAPIVersion");
354
355 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
356 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
357 return false;
358 }
359
360 // Negotiate the interface version by requesting most recent known to the platform
361 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
362 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
363 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
364 "requested version %u",
365 versionToUse);
366 return false;
367 }
368
369 // Add and remove versions below as needed
370 bool useAngle = false;
371 switch (versionToUse) {
372 case 2: {
373 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
374 void* rulesHandle = nullptr;
375 int rulesVersion = 0;
376 void* systemInfoHandle = nullptr;
377
378 // Get the symbols for the feature-support-utility library:
379 #define GET_SYMBOL(symbol) \
380 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
381 if (!symbol) { \
382 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
383 break; \
384 }
385 GET_SYMBOL(ANGLEAndroidParseRulesString);
386 GET_SYMBOL(ANGLEGetSystemInfo);
387 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
388 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
389 GET_SYMBOL(ANGLEFreeRulesHandle);
390 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
391
392 // Parse the rules, obtain the SystemInfo, and evaluate the
393 // application against the rules:
394 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
395 ALOGW("ANGLE feature-support library cannot parse rules file");
396 break;
397 }
398 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
399 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
400 break;
401 }
402 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) {
403 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
404 break;
405 }
406 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
407 systemInfoHandle, mAngleAppName.c_str());
408 (ANGLEFreeRulesHandle)(rulesHandle);
409 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
410 } break;
411
412 default:
413 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
414 }
415
416 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
417 return useAngle;
418 }
419
shouldUseAngle(std::string appName)420 bool GraphicsEnv::shouldUseAngle(std::string appName) {
421 if (appName != mAngleAppName) {
422 // Make sure we are checking the app we were init'ed for
423 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
424 appName.c_str());
425 return false;
426 }
427
428 return shouldUseAngle();
429 }
430
shouldUseAngle()431 bool GraphicsEnv::shouldUseAngle() {
432 // Make sure we are init'ed
433 if (mAngleAppName.empty()) {
434 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
435 return false;
436 }
437
438 return (mUseAngle == YES) ? true : false;
439 }
440
updateUseAngle()441 void GraphicsEnv::updateUseAngle() {
442 mUseAngle = NO;
443
444 const char* ANGLE_PREFER_ANGLE = "angle";
445 const char* ANGLE_PREFER_NATIVE = "native";
446
447 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
448 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
449 mUseAngle = YES;
450 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
451 ALOGV("User set \"Developer Options\" to force the use of Native");
452 mUseAngle = NO;
453 } else {
454 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
455 // load ANGLE and call the updatable opt-in/out logic:
456 void* featureSo = loadLibrary("feature_support");
457 if (featureSo) {
458 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
459 mUseAngle = checkAngleRules(featureSo) ? YES : NO;
460 dlclose(featureSo);
461 featureSo = nullptr;
462 } else {
463 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
464 }
465 }
466 }
467
setAngleInfo(const std::string path,const std::string appName,const std::string developerOptIn,const int rulesFd,const long rulesOffset,const long rulesLength)468 void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
469 const std::string developerOptIn, const int rulesFd,
470 const long rulesOffset, const long rulesLength) {
471 if (mUseAngle != UNKNOWN) {
472 // We've already figured out an answer for this app, so just return.
473 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
474 (mUseAngle == YES) ? "true" : "false");
475 return;
476 }
477
478 ALOGV("setting ANGLE path to '%s'", path.c_str());
479 mAnglePath = path;
480 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
481 mAngleAppName = appName;
482 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
483 mAngleDeveloperOptIn = developerOptIn;
484
485 lseek(rulesFd, rulesOffset, SEEK_SET);
486 mRulesBuffer = std::vector<char>(rulesLength + 1);
487 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
488 if (numBytesRead < 0) {
489 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
490 numBytesRead = 0;
491 } else if (numBytesRead == 0) {
492 ALOGW("Empty rules file");
493 }
494 if (numBytesRead != rulesLength) {
495 ALOGW("Did not read all of the necessary bytes from the rules file."
496 "expected: %ld, got: %zd",
497 rulesLength, numBytesRead);
498 }
499 mRulesBuffer[numBytesRead] = '\0';
500
501 // Update the current status of whether we should use ANGLE or not
502 updateUseAngle();
503 }
504
setLayerPaths(NativeLoaderNamespace * appNamespace,const std::string layerPaths)505 void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
506 if (mLayerPaths.empty()) {
507 mLayerPaths = layerPaths;
508 mAppNamespace = appNamespace;
509 } else {
510 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
511 layerPaths.c_str(), appNamespace);
512 }
513 }
514
getAppNamespace()515 NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
516 return mAppNamespace;
517 }
518
getAngleAppName()519 std::string& GraphicsEnv::getAngleAppName() {
520 return mAngleAppName;
521 }
522
getLayerPaths()523 const std::string& GraphicsEnv::getLayerPaths() {
524 return mLayerPaths;
525 }
526
getDebugLayers()527 const std::string& GraphicsEnv::getDebugLayers() {
528 return mDebugLayers;
529 }
530
getDebugLayersGLES()531 const std::string& GraphicsEnv::getDebugLayersGLES() {
532 return mDebugLayersGLES;
533 }
534
setDebugLayers(const std::string layers)535 void GraphicsEnv::setDebugLayers(const std::string layers) {
536 mDebugLayers = layers;
537 }
538
setDebugLayersGLES(const std::string layers)539 void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
540 mDebugLayersGLES = layers;
541 }
542
543 // Return true if all the required libraries from vndk and sphal namespace are
544 // linked to the Game Driver namespace correctly.
linkDriverNamespaceLocked(android_namespace_t * vndkNamespace)545 bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* vndkNamespace) {
546 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
547 if (llndkLibraries.empty()) {
548 return false;
549 }
550 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
551 ALOGE("Failed to link default namespace[%s]", dlerror());
552 return false;
553 }
554
555 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
556 if (vndkspLibraries.empty()) {
557 return false;
558 }
559 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
560 ALOGE("Failed to link vndk namespace[%s]", dlerror());
561 return false;
562 }
563
564 if (mSphalLibraries.empty()) {
565 return true;
566 }
567
568 // Make additional libraries in sphal to be accessible
569 auto sphalNamespace = android_get_exported_namespace("sphal");
570 if (!sphalNamespace) {
571 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
572 mSphalLibraries.c_str());
573 return false;
574 }
575
576 if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) {
577 ALOGE("Failed to link sphal namespace[%s]", dlerror());
578 return false;
579 }
580
581 return true;
582 }
583
getDriverNamespace()584 android_namespace_t* GraphicsEnv::getDriverNamespace() {
585 std::lock_guard<std::mutex> lock(mNamespaceMutex);
586
587 if (mDriverNamespace) {
588 return mDriverNamespace;
589 }
590
591 if (mDriverPath.empty()) {
592 return nullptr;
593 }
594
595 auto vndkNamespace = android_get_exported_namespace("vndk");
596 if (!vndkNamespace) {
597 return nullptr;
598 }
599
600 mDriverNamespace = android_create_namespace("gfx driver",
601 mDriverPath.c_str(), // ld_library_path
602 mDriverPath.c_str(), // default_library_path
603 ANDROID_NAMESPACE_TYPE_ISOLATED,
604 nullptr, // permitted_when_isolated_path
605 nullptr);
606
607 if (!linkDriverNamespaceLocked(vndkNamespace)) {
608 mDriverNamespace = nullptr;
609 }
610
611 return mDriverNamespace;
612 }
613
getAngleNamespace()614 android_namespace_t* GraphicsEnv::getAngleNamespace() {
615 std::lock_guard<std::mutex> lock(mNamespaceMutex);
616
617 if (mAngleNamespace) {
618 return mAngleNamespace;
619 }
620
621 if (mAnglePath.empty()) {
622 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
623 return nullptr;
624 }
625
626 mAngleNamespace = android_create_namespace("ANGLE",
627 nullptr, // ld_library_path
628 mAnglePath.c_str(), // default_library_path
629 ANDROID_NAMESPACE_TYPE_SHARED |
630 ANDROID_NAMESPACE_TYPE_ISOLATED,
631 nullptr, // permitted_when_isolated_path
632 nullptr);
633
634 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
635
636 return mAngleNamespace;
637 }
638
639 } // namespace android
640