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