blob: a07627a657184a3bb7ca8d58fe85d9a9a8e860ca [file] [log] [blame]
Jesse Hall90b25ed2016-12-12 12:56:46 -08001/*
2 * Copyright 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Jesse Hall90b25ed2016-12-12 12:56:46 -080019//#define LOG_NDEBUG 1
20#define LOG_TAG "GraphicsEnv"
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080021
Jiyong Park27c39e12017-05-08 13:00:02 +090022#include <graphicsenv/GraphicsEnv.h>
Jesse Hall90b25ed2016-12-12 12:56:46 -080023
Yiwei Zhang64d89212018-11-27 19:58:29 -080024#include <dlfcn.h>
Tim Van Patten5f744f12018-12-12 11:46:21 -070025#include <unistd.h>
Yiwei Zhang64d89212018-11-27 19:58:29 -080026
27#include <android-base/file.h>
28#include <android-base/properties.h>
29#include <android-base/strings.h>
30#include <android/dlext.h>
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080031#include <binder/IServiceManager.h>
Yiwei Zhang64d89212018-11-27 19:58:29 -080032#include <cutils/properties.h>
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080033#include <graphicsenv/IGpuService.h>
Yiwei Zhang64d89212018-11-27 19:58:29 -080034#include <log/log.h>
Cody Northrop629ce4e2018-10-15 07:22:09 -060035#include <sys/prctl.h>
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080036#include <utils/Trace.h>
Cody Northrop629ce4e2018-10-15 07:22:09 -060037
Tim Van Patten5f744f12018-12-12 11:46:21 -070038#include <memory>
Tim Van Patten5f744f12018-12-12 11:46:21 -070039#include <string>
40
Jesse Hall57de0ff2017-05-05 16:41:35 -070041// TODO(b/37049319) Get this from a header once one exists
42extern "C" {
Yiwei Zhang64d89212018-11-27 19:58:29 -080043android_namespace_t* android_get_exported_namespace(const char*);
44android_namespace_t* android_create_namespace(const char* name, const char* ld_library_path,
45 const char* default_library_path, uint64_t type,
46 const char* permitted_when_isolated_path,
47 android_namespace_t* parent);
48bool android_link_namespaces(android_namespace_t* from, android_namespace_t* to,
49 const char* shared_libs_sonames);
Jiyong Park9b816a82018-01-02 17:37:37 +090050
Yiwei Zhang64d89212018-11-27 19:58:29 -080051enum {
52 ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
53 ANDROID_NAMESPACE_TYPE_SHARED = 2,
54};
Jesse Hall57de0ff2017-05-05 16:41:35 -070055}
56
Tim Van Patten5f744f12018-12-12 11:46:21 -070057// TODO(ianelliott@): Get the following from an ANGLE header:
58#define CURRENT_ANGLE_API_VERSION 2 // Current API verion we are targetting
59// Version-2 API:
60typedef bool (*fpANGLEGetFeatureSupportUtilAPIVersion)(unsigned int* versionToUse);
61typedef bool (*fpANGLEAndroidParseRulesString)(const char* rulesString, void** rulesHandle,
62 int* rulesVersion);
63typedef bool (*fpANGLEGetSystemInfo)(void** handle);
64typedef bool (*fpANGLEAddDeviceInfoToSystemInfo)(const char* deviceMfr, const char* deviceModel,
65 void* handle);
66typedef bool (*fpANGLEShouldBeUsedForApplication)(void* rulesHandle, int rulesVersion,
67 void* systemInfoHandle, const char* appName);
68typedef bool (*fpANGLEFreeRulesHandle)(void* handle);
69typedef bool (*fpANGLEFreeSystemInfoHandle)(void* handle);
70
Jesse Hall90b25ed2016-12-12 12:56:46 -080071namespace android {
72
Yiwei Zhang64d89212018-11-27 19:58:29 -080073enum NativeLibrary {
74 LLNDK = 0,
75 VNDKSP = 1,
76};
77
78static constexpr const char* kNativeLibrariesSystemConfigPath[] = {"/etc/llndk.libraries.txt",
79 "/etc/vndksp.libraries.txt"};
80
81static std::string vndkVersionStr() {
82#ifdef __BIONIC__
83 std::string version = android::base::GetProperty("ro.vndk.version", "");
84 if (version != "" && version != "current") {
85 return "." + version;
86 }
87#endif
88 return "";
89}
90
91static void insertVndkVersionStr(std::string* fileName) {
92 LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr");
93 size_t insertPos = fileName->find_last_of(".");
94 if (insertPos == std::string::npos) {
95 insertPos = fileName->length();
96 }
97 fileName->insert(insertPos, vndkVersionStr());
98}
99
100static 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
119static const std::string getSystemNativeLibraries(NativeLibrary type) {
120 static const char* androidRootEnv = getenv("ANDROID_ROOT");
121 static const std::string rootDir = androidRootEnv != nullptr ? androidRootEnv : "/system";
122
123 std::string nativeLibrariesSystemConfig = rootDir + kNativeLibrariesSystemConfigPath[type];
124
125 insertVndkVersionStr(&nativeLibrariesSystemConfig);
126
127 std::vector<std::string> soNames;
128 if (!readConfig(nativeLibrariesSystemConfig, &soNames)) {
129 ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str());
130 return "";
131 }
132
133 return base::Join(soNames, ':');
134}
135
Jesse Hall90b25ed2016-12-12 12:56:46 -0800136/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
137 static GraphicsEnv env;
138 return env;
139}
140
Cody Northrop629ce4e2018-10-15 07:22:09 -0600141int GraphicsEnv::getCanLoadSystemLibraries() {
142 if (property_get_bool("ro.debuggable", false) && prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
143 // Return an integer value since this crosses library boundaries
144 return 1;
145 }
146 return 0;
147}
148
Jesse Hall90b25ed2016-12-12 12:56:46 -0800149void GraphicsEnv::setDriverPath(const std::string path) {
150 if (!mDriverPath.empty()) {
Yiwei Zhang64d89212018-11-27 19:58:29 -0800151 ALOGV("ignoring attempt to change driver path from '%s' to '%s'", mDriverPath.c_str(),
152 path.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800153 return;
154 }
155 ALOGV("setting driver path to '%s'", path.c_str());
156 mDriverPath = path;
157}
158
Greg Kaiser210bb7e2019-02-12 12:40:05 -0800159void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800160 const std::string& driverVersionName, uint64_t driverVersionCode,
161 const std::string& appPackageName) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800162 ATRACE_CALL();
163
Yiwei Zhangd9861812019-02-13 11:51:55 -0800164 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800165 ALOGV("setGpuStats:\n"
166 "\tdriverPackageName[%s]\n"
167 "\tdriverVersionName[%s]\n"
168 "\tdriverVersionCode[%llu]\n"
169 "\tappPackageName[%s]\n",
170 driverPackageName.c_str(), driverVersionName.c_str(),
171 (unsigned long long)driverVersionCode, appPackageName.c_str());
172
Yiwei Zhangd9861812019-02-13 11:51:55 -0800173 mGpuStats.driverPackageName = driverPackageName;
174 mGpuStats.driverVersionName = driverVersionName;
175 mGpuStats.driverVersionCode = driverVersionCode;
176 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800177}
178
Yiwei Zhangd9861812019-02-13 11:51:55 -0800179void GraphicsEnv::setDriverToLoad(GraphicsEnv::Driver driver) {
180 ATRACE_CALL();
181
182 std::lock_guard<std::mutex> lock(mStatsLock);
183 switch (driver) {
184 case GraphicsEnv::Driver::GL:
185 case GraphicsEnv::Driver::GL_UPDATED:
186 case GraphicsEnv::Driver::ANGLE: {
187 if (mGpuStats.glDriverToLoad == GraphicsEnv::Driver::NONE) {
188 mGpuStats.glDriverToLoad = driver;
189 break;
190 }
191
192 if (mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE) {
193 mGpuStats.glDriverFallback = driver;
194 }
195 break;
196 }
197 case Driver::VULKAN:
198 case Driver::VULKAN_UPDATED: {
199 if (mGpuStats.vkDriverToLoad == GraphicsEnv::Driver::NONE) {
200 mGpuStats.vkDriverToLoad = driver;
201 break;
202 }
203
204 if (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE) {
205 mGpuStats.vkDriverFallback = driver;
206 }
207 break;
208 }
209 default:
210 break;
211 }
212}
213
214void GraphicsEnv::setDriverLoaded(GraphicsEnv::Api api, bool isLoaded, int64_t driverLoadingTime) {
215 ATRACE_CALL();
216
217 std::lock_guard<std::mutex> lock(mStatsLock);
218 GraphicsEnv::Driver driver = GraphicsEnv::Driver::NONE;
219 bool isIntendedDriverLoaded = false;
220 if (api == GraphicsEnv::Api::API_GL) {
221 driver = mGpuStats.glDriverToLoad;
222 isIntendedDriverLoaded = isLoaded &&
223 ((mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE) ||
224 (mGpuStats.glDriverToLoad == mGpuStats.glDriverFallback));
225 } else {
226 driver = mGpuStats.vkDriverToLoad;
227 isIntendedDriverLoaded =
228 isLoaded && (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE);
229 }
230
231 sendGpuStatsLocked(driver, isIntendedDriverLoaded, driverLoadingTime);
232}
233
234void GraphicsEnv::clearDriverLoadingInfo(GraphicsEnv::Api api) {
235 ATRACE_CALL();
236
237 std::lock_guard<std::mutex> lock(mStatsLock);
238 if (api == GraphicsEnv::Api::API_GL) {
239 mGpuStats.glDriverToLoad = GraphicsEnv::Driver::NONE;
240 mGpuStats.glDriverFallback = GraphicsEnv::Driver::NONE;
241 }
242}
243
244static sp<IGpuService> getGpuService() {
245 const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
246 if (!binder) {
247 ALOGE("Failed to get gpu service");
248 return nullptr;
249 }
250
251 return interface_cast<IGpuService>(binder);
252}
253
254void GraphicsEnv::sendGpuStatsLocked(GraphicsEnv::Driver driver, bool isDriverLoaded,
255 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800256 ATRACE_CALL();
257
258 // Do not sendGpuStats for those skipping the GraphicsEnvironment setup
259 if (mGpuStats.appPackageName.empty()) return;
260
261 ALOGV("sendGpuStats:\n"
262 "\tdriverPackageName[%s]\n"
263 "\tdriverVersionName[%s]\n"
264 "\tdriverVersionCode[%llu]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800265 "\tappPackageName[%s]\n"
266 "\tdriver[%d]\n"
267 "\tisDriverLoaded[%d]\n"
268 "\tdriverLoadingTime[%lld]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800269 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhangd9861812019-02-13 11:51:55 -0800270 (unsigned long long)mGpuStats.driverVersionCode, mGpuStats.appPackageName.c_str(),
271 static_cast<int32_t>(driver), isDriverLoaded, (long long)driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800272
Yiwei Zhangd9861812019-02-13 11:51:55 -0800273 const sp<IGpuService> gpuService = getGpuService();
274 if (gpuService) {
275 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
276 mGpuStats.driverVersionCode, mGpuStats.appPackageName, driver,
277 isDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800278 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800279}
280
Tim Van Patten5f744f12018-12-12 11:46:21 -0700281void* GraphicsEnv::loadLibrary(std::string name) {
282 const android_dlextinfo dlextinfo = {
283 .flags = ANDROID_DLEXT_USE_NAMESPACE,
284 .library_namespace = getAngleNamespace(),
285 };
286
287 std::string libName = std::string("lib") + name + "_angle.so";
288
289 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
290
291 if (so) {
292 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
293 return so;
294 } else {
295 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
296 }
297
298 return nullptr;
299}
300
301bool GraphicsEnv::checkAngleRules(void* so) {
302 char manufacturer[PROPERTY_VALUE_MAX];
303 char model[PROPERTY_VALUE_MAX];
304 property_get("ro.product.manufacturer", manufacturer, "UNSET");
305 property_get("ro.product.model", model, "UNSET");
306
307 auto ANGLEGetFeatureSupportUtilAPIVersion =
308 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
309 "ANGLEGetFeatureSupportUtilAPIVersion");
310
311 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
312 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
313 return false;
314 }
315
316 // Negotiate the interface version by requesting most recent known to the platform
317 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
318 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
319 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
320 "requested version %u",
321 versionToUse);
322 return false;
323 }
324
325 // Add and remove versions below as needed
326 bool useAngle = false;
327 switch (versionToUse) {
328 case 2: {
329 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
330 void* rulesHandle = nullptr;
331 int rulesVersion = 0;
332 void* systemInfoHandle = nullptr;
333
334 // Get the symbols for the feature-support-utility library:
335#define GET_SYMBOL(symbol) \
336 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
337 if (!symbol) { \
338 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
339 break; \
340 }
341 GET_SYMBOL(ANGLEAndroidParseRulesString);
342 GET_SYMBOL(ANGLEGetSystemInfo);
343 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
344 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
345 GET_SYMBOL(ANGLEFreeRulesHandle);
346 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
347
348 // Parse the rules, obtain the SystemInfo, and evaluate the
349 // application against the rules:
350 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
351 ALOGW("ANGLE feature-support library cannot parse rules file");
352 break;
353 }
354 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
355 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
356 break;
357 }
358 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) {
359 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
360 break;
361 }
362 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
363 systemInfoHandle, mAngleAppName.c_str());
364 (ANGLEFreeRulesHandle)(rulesHandle);
365 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
366 } break;
367
368 default:
369 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
370 }
371
372 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
373 return useAngle;
374}
375
376bool GraphicsEnv::shouldUseAngle(std::string appName) {
377 if (appName != mAngleAppName) {
378 // Make sure we are checking the app we were init'ed for
379 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
380 appName.c_str());
381 return false;
382 }
383
384 return shouldUseAngle();
385}
386
387bool GraphicsEnv::shouldUseAngle() {
388 // Make sure we are init'ed
389 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700390 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700391 return false;
392 }
393
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700394 return (mUseAngle == YES) ? true : false;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700395}
396
397void GraphicsEnv::updateUseAngle() {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700398 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700399
400 const char* ANGLE_PREFER_ANGLE = "angle";
401 const char* ANGLE_PREFER_NATIVE = "native";
402
403 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
404 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700405 mUseAngle = YES;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700406 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
407 ALOGV("User set \"Developer Options\" to force the use of Native");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700408 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700409 } else {
410 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
411 // load ANGLE and call the updatable opt-in/out logic:
Cody Northropc15d3822019-01-17 10:26:47 -0700412 void* featureSo = loadLibrary("feature_support");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700413 if (featureSo) {
414 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700415 mUseAngle = checkAngleRules(featureSo) ? YES : NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700416 dlclose(featureSo);
417 featureSo = nullptr;
418 } else {
419 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
420 }
421 }
422}
423
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600424void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700425 const std::string developerOptIn, const int rulesFd,
426 const long rulesOffset, const long rulesLength) {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700427 if (mUseAngle != UNKNOWN) {
428 // We've already figured out an answer for this app, so just return.
429 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
430 (mUseAngle == YES) ? "true" : "false");
431 return;
432 }
433
Tim Van Patten5f744f12018-12-12 11:46:21 -0700434 ALOGV("setting ANGLE path to '%s'", path.c_str());
435 mAnglePath = path;
436 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
437 mAngleAppName = appName;
438 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
439 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600440
Tim Van Patten5f744f12018-12-12 11:46:21 -0700441 lseek(rulesFd, rulesOffset, SEEK_SET);
442 mRulesBuffer = std::vector<char>(rulesLength + 1);
443 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
444 if (numBytesRead < 0) {
445 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
446 numBytesRead = 0;
447 } else if (numBytesRead == 0) {
448 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600449 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700450 if (numBytesRead != rulesLength) {
451 ALOGW("Did not read all of the necessary bytes from the rules file."
452 "expected: %ld, got: %zd",
453 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700454 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700455 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 10:34:58 -0600456
Tim Van Patten5f744f12018-12-12 11:46:21 -0700457 // Update the current status of whether we should use ANGLE or not
458 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600459}
460
Victor Khimenko4819b522018-07-13 17:24:18 +0200461void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600462 if (mLayerPaths.empty()) {
463 mLayerPaths = layerPaths;
464 mAppNamespace = appNamespace;
465 } else {
466 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800467 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600468 }
469}
470
Victor Khimenko4819b522018-07-13 17:24:18 +0200471NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600472 return mAppNamespace;
473}
474
Tim Van Patten5f744f12018-12-12 11:46:21 -0700475std::string& GraphicsEnv::getAngleAppName() {
476 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600477}
478
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600479const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600480 return mLayerPaths;
481}
482
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600483const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600484 return mDebugLayers;
485}
486
Cody Northropb9b01b62018-10-23 13:13:10 -0600487const std::string& GraphicsEnv::getDebugLayersGLES() {
488 return mDebugLayersGLES;
489}
490
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600491void GraphicsEnv::setDebugLayers(const std::string layers) {
492 mDebugLayers = layers;
493}
494
Cody Northropb9b01b62018-10-23 13:13:10 -0600495void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
496 mDebugLayersGLES = layers;
497}
498
Jesse Hall53457db2016-12-14 16:54:06 -0800499android_namespace_t* GraphicsEnv::getDriverNamespace() {
500 static std::once_flag once;
501 std::call_once(once, [this]() {
Yiwei Zhang64d89212018-11-27 19:58:29 -0800502 if (mDriverPath.empty()) return;
503
504 auto vndkNamespace = android_get_exported_namespace("vndk");
505 if (!vndkNamespace) return;
506
Jesse Hall57de0ff2017-05-05 16:41:35 -0700507 mDriverNamespace = android_create_namespace("gfx driver",
Peiyong Lin2e9c74c2018-10-24 11:18:07 -0700508 mDriverPath.c_str(), // ld_library_path
Jesse Hall57de0ff2017-05-05 16:41:35 -0700509 mDriverPath.c_str(), // default_library_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800510 ANDROID_NAMESPACE_TYPE_ISOLATED,
Jesse Hall57de0ff2017-05-05 16:41:35 -0700511 nullptr, // permitted_when_isolated_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800512 nullptr);
513
514 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
515 if (llndkLibraries.empty()) {
516 mDriverNamespace = nullptr;
517 return;
518 }
519 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
520 ALOGE("Failed to link default namespace[%s]", dlerror());
521 mDriverNamespace = nullptr;
522 return;
523 }
524
525 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
526 if (vndkspLibraries.empty()) {
527 mDriverNamespace = nullptr;
528 return;
529 }
530 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
531 ALOGE("Failed to link vndk namespace[%s]", dlerror());
532 mDriverNamespace = nullptr;
533 return;
534 }
Jesse Hall53457db2016-12-14 16:54:06 -0800535 });
Yiwei Zhang64d89212018-11-27 19:58:29 -0800536
Jesse Hall53457db2016-12-14 16:54:06 -0800537 return mDriverNamespace;
538}
539
Cody Northrop1f00e172018-04-02 11:23:31 -0600540android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700541 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600542
Cody Northrop3892cfa2019-01-30 10:03:12 -0700543 if (mAngleNamespace) {
544 return mAngleNamespace;
545 }
546
547 if (mAnglePath.empty()) {
548 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
549 return nullptr;
550 }
551
552 mAngleNamespace = android_create_namespace("ANGLE",
553 nullptr, // ld_library_path
554 mAnglePath.c_str(), // default_library_path
555 ANDROID_NAMESPACE_TYPE_SHARED |
556 ANDROID_NAMESPACE_TYPE_ISOLATED,
557 nullptr, // permitted_when_isolated_path
558 nullptr);
559
560 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600561
562 return mAngleNamespace;
563}
564
Jesse Hall90b25ed2016-12-12 12:56:46 -0800565} // namespace android