blob: 6e5324ee47f2ac6ef9b89faac16c0caa38ee56cc [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 Zhang96c01712019-02-19 16:00:25 -0800161 int64_t driverBuildTime, 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"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800168 "\tdriverVersionCode[%" PRIu64 "]\n"
169 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800170 "\tappPackageName[%s]\n",
Yiwei Zhang96c01712019-02-19 16:00:25 -0800171 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
172 appPackageName.c_str());
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800173
Yiwei Zhangd9861812019-02-13 11:51:55 -0800174 mGpuStats.driverPackageName = driverPackageName;
175 mGpuStats.driverVersionName = driverVersionName;
176 mGpuStats.driverVersionCode = driverVersionCode;
Yiwei Zhang96c01712019-02-19 16:00:25 -0800177 mGpuStats.driverBuildTime = driverBuildTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800178 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800179}
180
Yiwei Zhangd9861812019-02-13 11:51:55 -0800181void GraphicsEnv::setDriverToLoad(GraphicsEnv::Driver driver) {
182 ATRACE_CALL();
183
184 std::lock_guard<std::mutex> lock(mStatsLock);
185 switch (driver) {
186 case GraphicsEnv::Driver::GL:
187 case GraphicsEnv::Driver::GL_UPDATED:
188 case GraphicsEnv::Driver::ANGLE: {
189 if (mGpuStats.glDriverToLoad == GraphicsEnv::Driver::NONE) {
190 mGpuStats.glDriverToLoad = driver;
191 break;
192 }
193
194 if (mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE) {
195 mGpuStats.glDriverFallback = driver;
196 }
197 break;
198 }
199 case Driver::VULKAN:
200 case Driver::VULKAN_UPDATED: {
201 if (mGpuStats.vkDriverToLoad == GraphicsEnv::Driver::NONE) {
202 mGpuStats.vkDriverToLoad = driver;
203 break;
204 }
205
206 if (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE) {
207 mGpuStats.vkDriverFallback = driver;
208 }
209 break;
210 }
211 default:
212 break;
213 }
214}
215
216void GraphicsEnv::setDriverLoaded(GraphicsEnv::Api api, bool isLoaded, int64_t driverLoadingTime) {
217 ATRACE_CALL();
218
219 std::lock_guard<std::mutex> lock(mStatsLock);
220 GraphicsEnv::Driver driver = GraphicsEnv::Driver::NONE;
221 bool isIntendedDriverLoaded = false;
222 if (api == GraphicsEnv::Api::API_GL) {
223 driver = mGpuStats.glDriverToLoad;
224 isIntendedDriverLoaded = isLoaded &&
225 ((mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE) ||
226 (mGpuStats.glDriverToLoad == mGpuStats.glDriverFallback));
227 } else {
228 driver = mGpuStats.vkDriverToLoad;
229 isIntendedDriverLoaded =
230 isLoaded && (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE);
231 }
232
233 sendGpuStatsLocked(driver, isIntendedDriverLoaded, driverLoadingTime);
234}
235
236void GraphicsEnv::clearDriverLoadingInfo(GraphicsEnv::Api api) {
237 ATRACE_CALL();
238
239 std::lock_guard<std::mutex> lock(mStatsLock);
240 if (api == GraphicsEnv::Api::API_GL) {
241 mGpuStats.glDriverToLoad = GraphicsEnv::Driver::NONE;
242 mGpuStats.glDriverFallback = GraphicsEnv::Driver::NONE;
243 }
244}
245
246static sp<IGpuService> getGpuService() {
247 const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
248 if (!binder) {
249 ALOGE("Failed to get gpu service");
250 return nullptr;
251 }
252
253 return interface_cast<IGpuService>(binder);
254}
255
256void GraphicsEnv::sendGpuStatsLocked(GraphicsEnv::Driver driver, bool isDriverLoaded,
257 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800258 ATRACE_CALL();
259
260 // Do not sendGpuStats for those skipping the GraphicsEnvironment setup
261 if (mGpuStats.appPackageName.empty()) return;
262
263 ALOGV("sendGpuStats:\n"
264 "\tdriverPackageName[%s]\n"
265 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800266 "\tdriverVersionCode[%" PRIu64 "]\n"
267 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800268 "\tappPackageName[%s]\n"
269 "\tdriver[%d]\n"
270 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800271 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800272 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-19 16:00:25 -0800273 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
274 static_cast<int32_t>(driver), isDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800275
Yiwei Zhangd9861812019-02-13 11:51:55 -0800276 const sp<IGpuService> gpuService = getGpuService();
277 if (gpuService) {
278 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800279 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang512a7232019-02-15 16:04:41 -0800280 mGpuStats.appPackageName, driver, isDriverLoaded,
281 driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800282 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800283}
284
Tim Van Patten5f744f12018-12-12 11:46:21 -0700285void* GraphicsEnv::loadLibrary(std::string name) {
286 const android_dlextinfo dlextinfo = {
287 .flags = ANDROID_DLEXT_USE_NAMESPACE,
288 .library_namespace = getAngleNamespace(),
289 };
290
291 std::string libName = std::string("lib") + name + "_angle.so";
292
293 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
294
295 if (so) {
296 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
297 return so;
298 } else {
299 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
300 }
301
302 return nullptr;
303}
304
305bool GraphicsEnv::checkAngleRules(void* so) {
306 char manufacturer[PROPERTY_VALUE_MAX];
307 char model[PROPERTY_VALUE_MAX];
308 property_get("ro.product.manufacturer", manufacturer, "UNSET");
309 property_get("ro.product.model", model, "UNSET");
310
311 auto ANGLEGetFeatureSupportUtilAPIVersion =
312 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
313 "ANGLEGetFeatureSupportUtilAPIVersion");
314
315 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
316 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
317 return false;
318 }
319
320 // Negotiate the interface version by requesting most recent known to the platform
321 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
322 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
323 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
324 "requested version %u",
325 versionToUse);
326 return false;
327 }
328
329 // Add and remove versions below as needed
330 bool useAngle = false;
331 switch (versionToUse) {
332 case 2: {
333 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
334 void* rulesHandle = nullptr;
335 int rulesVersion = 0;
336 void* systemInfoHandle = nullptr;
337
338 // Get the symbols for the feature-support-utility library:
339#define GET_SYMBOL(symbol) \
340 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
341 if (!symbol) { \
342 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
343 break; \
344 }
345 GET_SYMBOL(ANGLEAndroidParseRulesString);
346 GET_SYMBOL(ANGLEGetSystemInfo);
347 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
348 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
349 GET_SYMBOL(ANGLEFreeRulesHandle);
350 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
351
352 // Parse the rules, obtain the SystemInfo, and evaluate the
353 // application against the rules:
354 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
355 ALOGW("ANGLE feature-support library cannot parse rules file");
356 break;
357 }
358 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
359 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
360 break;
361 }
362 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) {
363 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
364 break;
365 }
366 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
367 systemInfoHandle, mAngleAppName.c_str());
368 (ANGLEFreeRulesHandle)(rulesHandle);
369 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
370 } break;
371
372 default:
373 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
374 }
375
376 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
377 return useAngle;
378}
379
380bool GraphicsEnv::shouldUseAngle(std::string appName) {
381 if (appName != mAngleAppName) {
382 // Make sure we are checking the app we were init'ed for
383 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
384 appName.c_str());
385 return false;
386 }
387
388 return shouldUseAngle();
389}
390
391bool GraphicsEnv::shouldUseAngle() {
392 // Make sure we are init'ed
393 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700394 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700395 return false;
396 }
397
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700398 return (mUseAngle == YES) ? true : false;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700399}
400
401void GraphicsEnv::updateUseAngle() {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700402 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700403
404 const char* ANGLE_PREFER_ANGLE = "angle";
405 const char* ANGLE_PREFER_NATIVE = "native";
406
407 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
408 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700409 mUseAngle = YES;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700410 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
411 ALOGV("User set \"Developer Options\" to force the use of Native");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700412 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700413 } else {
414 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
415 // load ANGLE and call the updatable opt-in/out logic:
Cody Northropc15d3822019-01-17 10:26:47 -0700416 void* featureSo = loadLibrary("feature_support");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700417 if (featureSo) {
418 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700419 mUseAngle = checkAngleRules(featureSo) ? YES : NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700420 dlclose(featureSo);
421 featureSo = nullptr;
422 } else {
423 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
424 }
425 }
426}
427
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600428void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700429 const std::string developerOptIn, const int rulesFd,
430 const long rulesOffset, const long rulesLength) {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700431 if (mUseAngle != UNKNOWN) {
432 // We've already figured out an answer for this app, so just return.
433 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
434 (mUseAngle == YES) ? "true" : "false");
435 return;
436 }
437
Tim Van Patten5f744f12018-12-12 11:46:21 -0700438 ALOGV("setting ANGLE path to '%s'", path.c_str());
439 mAnglePath = path;
440 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
441 mAngleAppName = appName;
442 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
443 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600444
Tim Van Patten5f744f12018-12-12 11:46:21 -0700445 lseek(rulesFd, rulesOffset, SEEK_SET);
446 mRulesBuffer = std::vector<char>(rulesLength + 1);
447 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
448 if (numBytesRead < 0) {
449 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
450 numBytesRead = 0;
451 } else if (numBytesRead == 0) {
452 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600453 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700454 if (numBytesRead != rulesLength) {
455 ALOGW("Did not read all of the necessary bytes from the rules file."
456 "expected: %ld, got: %zd",
457 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700458 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700459 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 10:34:58 -0600460
Tim Van Patten5f744f12018-12-12 11:46:21 -0700461 // Update the current status of whether we should use ANGLE or not
462 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600463}
464
Victor Khimenko4819b522018-07-13 17:24:18 +0200465void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600466 if (mLayerPaths.empty()) {
467 mLayerPaths = layerPaths;
468 mAppNamespace = appNamespace;
469 } else {
470 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800471 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600472 }
473}
474
Victor Khimenko4819b522018-07-13 17:24:18 +0200475NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600476 return mAppNamespace;
477}
478
Tim Van Patten5f744f12018-12-12 11:46:21 -0700479std::string& GraphicsEnv::getAngleAppName() {
480 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600481}
482
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600483const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600484 return mLayerPaths;
485}
486
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600487const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600488 return mDebugLayers;
489}
490
Cody Northropb9b01b62018-10-23 13:13:10 -0600491const std::string& GraphicsEnv::getDebugLayersGLES() {
492 return mDebugLayersGLES;
493}
494
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600495void GraphicsEnv::setDebugLayers(const std::string layers) {
496 mDebugLayers = layers;
497}
498
Cody Northropb9b01b62018-10-23 13:13:10 -0600499void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
500 mDebugLayersGLES = layers;
501}
502
Jesse Hall53457db2016-12-14 16:54:06 -0800503android_namespace_t* GraphicsEnv::getDriverNamespace() {
504 static std::once_flag once;
505 std::call_once(once, [this]() {
Yiwei Zhang64d89212018-11-27 19:58:29 -0800506 if (mDriverPath.empty()) return;
507
508 auto vndkNamespace = android_get_exported_namespace("vndk");
509 if (!vndkNamespace) return;
510
Jesse Hall57de0ff2017-05-05 16:41:35 -0700511 mDriverNamespace = android_create_namespace("gfx driver",
Peiyong Lin2e9c74c2018-10-24 11:18:07 -0700512 mDriverPath.c_str(), // ld_library_path
Jesse Hall57de0ff2017-05-05 16:41:35 -0700513 mDriverPath.c_str(), // default_library_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800514 ANDROID_NAMESPACE_TYPE_ISOLATED,
Jesse Hall57de0ff2017-05-05 16:41:35 -0700515 nullptr, // permitted_when_isolated_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800516 nullptr);
517
518 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
519 if (llndkLibraries.empty()) {
520 mDriverNamespace = nullptr;
521 return;
522 }
523 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
524 ALOGE("Failed to link default namespace[%s]", dlerror());
525 mDriverNamespace = nullptr;
526 return;
527 }
528
529 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
530 if (vndkspLibraries.empty()) {
531 mDriverNamespace = nullptr;
532 return;
533 }
534 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
535 ALOGE("Failed to link vndk namespace[%s]", dlerror());
536 mDriverNamespace = nullptr;
537 return;
538 }
Jesse Hall53457db2016-12-14 16:54:06 -0800539 });
Yiwei Zhang64d89212018-11-27 19:58:29 -0800540
Jesse Hall53457db2016-12-14 16:54:06 -0800541 return mDriverNamespace;
542}
543
Cody Northrop1f00e172018-04-02 11:23:31 -0600544android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700545 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600546
Cody Northrop3892cfa2019-01-30 10:03:12 -0700547 if (mAngleNamespace) {
548 return mAngleNamespace;
549 }
550
551 if (mAnglePath.empty()) {
552 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
553 return nullptr;
554 }
555
556 mAngleNamespace = android_create_namespace("ANGLE",
557 nullptr, // ld_library_path
558 mAnglePath.c_str(), // default_library_path
559 ANDROID_NAMESPACE_TYPE_SHARED |
560 ANDROID_NAMESPACE_TYPE_ISOLATED,
561 nullptr, // permitted_when_isolated_path
562 nullptr);
563
564 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600565
566 return mAngleNamespace;
567}
568
Jesse Hall90b25ed2016-12-12 12:56:46 -0800569} // namespace android