blob: 386f9f0dfb11662f100ee0cc8f876d6d73891b23 [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,
Yiwei Zhang512a7232019-02-15 16:04:41 -0800161 const std::string& driverBuildDate,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800162 const std::string& appPackageName) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800163 ATRACE_CALL();
164
Yiwei Zhangd9861812019-02-13 11:51:55 -0800165 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800166 ALOGV("setGpuStats:\n"
167 "\tdriverPackageName[%s]\n"
168 "\tdriverVersionName[%s]\n"
169 "\tdriverVersionCode[%llu]\n"
Yiwei Zhang512a7232019-02-15 16:04:41 -0800170 "\tdriverBuildDate[%s]\n"
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800171 "\tappPackageName[%s]\n",
172 driverPackageName.c_str(), driverVersionName.c_str(),
Yiwei Zhang512a7232019-02-15 16:04:41 -0800173 (unsigned long long)driverVersionCode, driverBuildDate.c_str(), appPackageName.c_str());
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800174
Yiwei Zhangd9861812019-02-13 11:51:55 -0800175 mGpuStats.driverPackageName = driverPackageName;
176 mGpuStats.driverVersionName = driverVersionName;
177 mGpuStats.driverVersionCode = driverVersionCode;
Yiwei Zhang512a7232019-02-15 16:04:41 -0800178 mGpuStats.driverBuildDate = driverBuildDate;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800179 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800180}
181
Yiwei Zhangd9861812019-02-13 11:51:55 -0800182void GraphicsEnv::setDriverToLoad(GraphicsEnv::Driver driver) {
183 ATRACE_CALL();
184
185 std::lock_guard<std::mutex> lock(mStatsLock);
186 switch (driver) {
187 case GraphicsEnv::Driver::GL:
188 case GraphicsEnv::Driver::GL_UPDATED:
189 case GraphicsEnv::Driver::ANGLE: {
190 if (mGpuStats.glDriverToLoad == GraphicsEnv::Driver::NONE) {
191 mGpuStats.glDriverToLoad = driver;
192 break;
193 }
194
195 if (mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE) {
196 mGpuStats.glDriverFallback = driver;
197 }
198 break;
199 }
200 case Driver::VULKAN:
201 case Driver::VULKAN_UPDATED: {
202 if (mGpuStats.vkDriverToLoad == GraphicsEnv::Driver::NONE) {
203 mGpuStats.vkDriverToLoad = driver;
204 break;
205 }
206
207 if (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE) {
208 mGpuStats.vkDriverFallback = driver;
209 }
210 break;
211 }
212 default:
213 break;
214 }
215}
216
217void GraphicsEnv::setDriverLoaded(GraphicsEnv::Api api, bool isLoaded, int64_t driverLoadingTime) {
218 ATRACE_CALL();
219
220 std::lock_guard<std::mutex> lock(mStatsLock);
221 GraphicsEnv::Driver driver = GraphicsEnv::Driver::NONE;
222 bool isIntendedDriverLoaded = false;
223 if (api == GraphicsEnv::Api::API_GL) {
224 driver = mGpuStats.glDriverToLoad;
225 isIntendedDriverLoaded = isLoaded &&
226 ((mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE) ||
227 (mGpuStats.glDriverToLoad == mGpuStats.glDriverFallback));
228 } else {
229 driver = mGpuStats.vkDriverToLoad;
230 isIntendedDriverLoaded =
231 isLoaded && (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE);
232 }
233
234 sendGpuStatsLocked(driver, isIntendedDriverLoaded, driverLoadingTime);
235}
236
237void GraphicsEnv::clearDriverLoadingInfo(GraphicsEnv::Api api) {
238 ATRACE_CALL();
239
240 std::lock_guard<std::mutex> lock(mStatsLock);
241 if (api == GraphicsEnv::Api::API_GL) {
242 mGpuStats.glDriverToLoad = GraphicsEnv::Driver::NONE;
243 mGpuStats.glDriverFallback = GraphicsEnv::Driver::NONE;
244 }
245}
246
247static sp<IGpuService> getGpuService() {
248 const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
249 if (!binder) {
250 ALOGE("Failed to get gpu service");
251 return nullptr;
252 }
253
254 return interface_cast<IGpuService>(binder);
255}
256
257void GraphicsEnv::sendGpuStatsLocked(GraphicsEnv::Driver driver, bool isDriverLoaded,
258 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800259 ATRACE_CALL();
260
261 // Do not sendGpuStats for those skipping the GraphicsEnvironment setup
262 if (mGpuStats.appPackageName.empty()) return;
263
264 ALOGV("sendGpuStats:\n"
265 "\tdriverPackageName[%s]\n"
266 "\tdriverVersionName[%s]\n"
267 "\tdriverVersionCode[%llu]\n"
Yiwei Zhang512a7232019-02-15 16:04:41 -0800268 "\tdriverBuildDate[%s]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800269 "\tappPackageName[%s]\n"
270 "\tdriver[%d]\n"
271 "\tisDriverLoaded[%d]\n"
272 "\tdriverLoadingTime[%lld]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800273 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang512a7232019-02-15 16:04:41 -0800274 (unsigned long long)mGpuStats.driverVersionCode, mGpuStats.driverBuildDate.c_str(),
275 mGpuStats.appPackageName.c_str(), static_cast<int32_t>(driver), isDriverLoaded,
276 (long long)driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800277
Yiwei Zhangd9861812019-02-13 11:51:55 -0800278 const sp<IGpuService> gpuService = getGpuService();
279 if (gpuService) {
280 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang512a7232019-02-15 16:04:41 -0800281 mGpuStats.driverVersionCode, mGpuStats.driverBuildDate,
282 mGpuStats.appPackageName, driver, isDriverLoaded,
283 driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800284 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800285}
286
Tim Van Patten5f744f12018-12-12 11:46:21 -0700287void* GraphicsEnv::loadLibrary(std::string name) {
288 const android_dlextinfo dlextinfo = {
289 .flags = ANDROID_DLEXT_USE_NAMESPACE,
290 .library_namespace = getAngleNamespace(),
291 };
292
293 std::string libName = std::string("lib") + name + "_angle.so";
294
295 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
296
297 if (so) {
298 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
299 return so;
300 } else {
301 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
302 }
303
304 return nullptr;
305}
306
307bool GraphicsEnv::checkAngleRules(void* so) {
308 char manufacturer[PROPERTY_VALUE_MAX];
309 char model[PROPERTY_VALUE_MAX];
310 property_get("ro.product.manufacturer", manufacturer, "UNSET");
311 property_get("ro.product.model", model, "UNSET");
312
313 auto ANGLEGetFeatureSupportUtilAPIVersion =
314 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
315 "ANGLEGetFeatureSupportUtilAPIVersion");
316
317 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
318 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
319 return false;
320 }
321
322 // Negotiate the interface version by requesting most recent known to the platform
323 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
324 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
325 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
326 "requested version %u",
327 versionToUse);
328 return false;
329 }
330
331 // Add and remove versions below as needed
332 bool useAngle = false;
333 switch (versionToUse) {
334 case 2: {
335 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
336 void* rulesHandle = nullptr;
337 int rulesVersion = 0;
338 void* systemInfoHandle = nullptr;
339
340 // Get the symbols for the feature-support-utility library:
341#define GET_SYMBOL(symbol) \
342 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
343 if (!symbol) { \
344 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
345 break; \
346 }
347 GET_SYMBOL(ANGLEAndroidParseRulesString);
348 GET_SYMBOL(ANGLEGetSystemInfo);
349 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
350 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
351 GET_SYMBOL(ANGLEFreeRulesHandle);
352 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
353
354 // Parse the rules, obtain the SystemInfo, and evaluate the
355 // application against the rules:
356 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
357 ALOGW("ANGLE feature-support library cannot parse rules file");
358 break;
359 }
360 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
361 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
362 break;
363 }
364 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) {
365 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
366 break;
367 }
368 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
369 systemInfoHandle, mAngleAppName.c_str());
370 (ANGLEFreeRulesHandle)(rulesHandle);
371 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
372 } break;
373
374 default:
375 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
376 }
377
378 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
379 return useAngle;
380}
381
382bool GraphicsEnv::shouldUseAngle(std::string appName) {
383 if (appName != mAngleAppName) {
384 // Make sure we are checking the app we were init'ed for
385 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
386 appName.c_str());
387 return false;
388 }
389
390 return shouldUseAngle();
391}
392
393bool GraphicsEnv::shouldUseAngle() {
394 // Make sure we are init'ed
395 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700396 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700397 return false;
398 }
399
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700400 return (mUseAngle == YES) ? true : false;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700401}
402
403void GraphicsEnv::updateUseAngle() {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700404 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700405
406 const char* ANGLE_PREFER_ANGLE = "angle";
407 const char* ANGLE_PREFER_NATIVE = "native";
408
409 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
410 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700411 mUseAngle = YES;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700412 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
413 ALOGV("User set \"Developer Options\" to force the use of Native");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700414 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700415 } else {
416 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
417 // load ANGLE and call the updatable opt-in/out logic:
Cody Northropc15d3822019-01-17 10:26:47 -0700418 void* featureSo = loadLibrary("feature_support");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700419 if (featureSo) {
420 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700421 mUseAngle = checkAngleRules(featureSo) ? YES : NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700422 dlclose(featureSo);
423 featureSo = nullptr;
424 } else {
425 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
426 }
427 }
428}
429
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600430void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700431 const std::string developerOptIn, const int rulesFd,
432 const long rulesOffset, const long rulesLength) {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700433 if (mUseAngle != UNKNOWN) {
434 // We've already figured out an answer for this app, so just return.
435 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
436 (mUseAngle == YES) ? "true" : "false");
437 return;
438 }
439
Tim Van Patten5f744f12018-12-12 11:46:21 -0700440 ALOGV("setting ANGLE path to '%s'", path.c_str());
441 mAnglePath = path;
442 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
443 mAngleAppName = appName;
444 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
445 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600446
Tim Van Patten5f744f12018-12-12 11:46:21 -0700447 lseek(rulesFd, rulesOffset, SEEK_SET);
448 mRulesBuffer = std::vector<char>(rulesLength + 1);
449 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
450 if (numBytesRead < 0) {
451 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
452 numBytesRead = 0;
453 } else if (numBytesRead == 0) {
454 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600455 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700456 if (numBytesRead != rulesLength) {
457 ALOGW("Did not read all of the necessary bytes from the rules file."
458 "expected: %ld, got: %zd",
459 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700460 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700461 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 10:34:58 -0600462
Tim Van Patten5f744f12018-12-12 11:46:21 -0700463 // Update the current status of whether we should use ANGLE or not
464 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600465}
466
Victor Khimenko4819b522018-07-13 17:24:18 +0200467void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600468 if (mLayerPaths.empty()) {
469 mLayerPaths = layerPaths;
470 mAppNamespace = appNamespace;
471 } else {
472 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800473 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600474 }
475}
476
Victor Khimenko4819b522018-07-13 17:24:18 +0200477NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600478 return mAppNamespace;
479}
480
Tim Van Patten5f744f12018-12-12 11:46:21 -0700481std::string& GraphicsEnv::getAngleAppName() {
482 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600483}
484
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600485const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600486 return mLayerPaths;
487}
488
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600489const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600490 return mDebugLayers;
491}
492
Cody Northropb9b01b62018-10-23 13:13:10 -0600493const std::string& GraphicsEnv::getDebugLayersGLES() {
494 return mDebugLayersGLES;
495}
496
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600497void GraphicsEnv::setDebugLayers(const std::string layers) {
498 mDebugLayers = layers;
499}
500
Cody Northropb9b01b62018-10-23 13:13:10 -0600501void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
502 mDebugLayersGLES = layers;
503}
504
Jesse Hall53457db2016-12-14 16:54:06 -0800505android_namespace_t* GraphicsEnv::getDriverNamespace() {
506 static std::once_flag once;
507 std::call_once(once, [this]() {
Yiwei Zhang64d89212018-11-27 19:58:29 -0800508 if (mDriverPath.empty()) return;
509
510 auto vndkNamespace = android_get_exported_namespace("vndk");
511 if (!vndkNamespace) return;
512
Jesse Hall57de0ff2017-05-05 16:41:35 -0700513 mDriverNamespace = android_create_namespace("gfx driver",
Peiyong Lin2e9c74c2018-10-24 11:18:07 -0700514 mDriverPath.c_str(), // ld_library_path
Jesse Hall57de0ff2017-05-05 16:41:35 -0700515 mDriverPath.c_str(), // default_library_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800516 ANDROID_NAMESPACE_TYPE_ISOLATED,
Jesse Hall57de0ff2017-05-05 16:41:35 -0700517 nullptr, // permitted_when_isolated_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800518 nullptr);
519
520 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
521 if (llndkLibraries.empty()) {
522 mDriverNamespace = nullptr;
523 return;
524 }
525 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
526 ALOGE("Failed to link default namespace[%s]", dlerror());
527 mDriverNamespace = nullptr;
528 return;
529 }
530
531 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
532 if (vndkspLibraries.empty()) {
533 mDriverNamespace = nullptr;
534 return;
535 }
536 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
537 ALOGE("Failed to link vndk namespace[%s]", dlerror());
538 mDriverNamespace = nullptr;
539 return;
540 }
Jesse Hall53457db2016-12-14 16:54:06 -0800541 });
Yiwei Zhang64d89212018-11-27 19:58:29 -0800542
Jesse Hall53457db2016-12-14 16:54:06 -0800543 return mDriverNamespace;
544}
545
Cody Northrop1f00e172018-04-02 11:23:31 -0600546android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700547 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600548
Cody Northrop3892cfa2019-01-30 10:03:12 -0700549 if (mAngleNamespace) {
550 return mAngleNamespace;
551 }
552
553 if (mAnglePath.empty()) {
554 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
555 return nullptr;
556 }
557
558 mAngleNamespace = android_create_namespace("ANGLE",
559 nullptr, // ld_library_path
560 mAnglePath.c_str(), // default_library_path
561 ANDROID_NAMESPACE_TYPE_SHARED |
562 ANDROID_NAMESPACE_TYPE_ISOLATED,
563 nullptr, // permitted_when_isolated_path
564 nullptr);
565
566 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600567
568 return mAngleNamespace;
569}
570
Jesse Hall90b25ed2016-12-12 12:56:46 -0800571} // namespace android