blob: 13c0d876c09b01306a9f7d3623c7e36b4ef2c4a4 [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
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800149void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string path,
150 const std::string sphalLibraries) {
151 if (!mDriverPath.empty() || !mSphalLibraries.empty()) {
152 ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries "
153 "from '%s' to '%s'",
154 mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800155 return;
156 }
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800157 ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(),
158 sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800159 mDriverPath = path;
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800160 mSphalLibraries = sphalLibraries;
Jesse Hall90b25ed2016-12-12 12:56:46 -0800161}
162
Greg Kaiser210bb7e2019-02-12 12:40:05 -0800163void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800164 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800165 int64_t driverBuildTime, const std::string& appPackageName) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800166 ATRACE_CALL();
167
Yiwei Zhangd9861812019-02-13 11:51:55 -0800168 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800169 ALOGV("setGpuStats:\n"
170 "\tdriverPackageName[%s]\n"
171 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800172 "\tdriverVersionCode[%" PRIu64 "]\n"
173 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800174 "\tappPackageName[%s]\n",
Yiwei Zhang96c01712019-02-19 16:00:25 -0800175 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
176 appPackageName.c_str());
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800177
Yiwei Zhangd9861812019-02-13 11:51:55 -0800178 mGpuStats.driverPackageName = driverPackageName;
179 mGpuStats.driverVersionName = driverVersionName;
180 mGpuStats.driverVersionCode = driverVersionCode;
Yiwei Zhang96c01712019-02-19 16:00:25 -0800181 mGpuStats.driverBuildTime = driverBuildTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800182 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800183}
184
Yiwei Zhangd9861812019-02-13 11:51:55 -0800185void GraphicsEnv::setDriverToLoad(GraphicsEnv::Driver driver) {
186 ATRACE_CALL();
187
188 std::lock_guard<std::mutex> lock(mStatsLock);
189 switch (driver) {
190 case GraphicsEnv::Driver::GL:
191 case GraphicsEnv::Driver::GL_UPDATED:
192 case GraphicsEnv::Driver::ANGLE: {
193 if (mGpuStats.glDriverToLoad == GraphicsEnv::Driver::NONE) {
194 mGpuStats.glDriverToLoad = driver;
195 break;
196 }
197
198 if (mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE) {
199 mGpuStats.glDriverFallback = driver;
200 }
201 break;
202 }
203 case Driver::VULKAN:
204 case Driver::VULKAN_UPDATED: {
205 if (mGpuStats.vkDriverToLoad == GraphicsEnv::Driver::NONE) {
206 mGpuStats.vkDriverToLoad = driver;
207 break;
208 }
209
210 if (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE) {
211 mGpuStats.vkDriverFallback = driver;
212 }
213 break;
214 }
215 default:
216 break;
217 }
218}
219
220void GraphicsEnv::setDriverLoaded(GraphicsEnv::Api api, bool isLoaded, int64_t driverLoadingTime) {
221 ATRACE_CALL();
222
223 std::lock_guard<std::mutex> lock(mStatsLock);
224 GraphicsEnv::Driver driver = GraphicsEnv::Driver::NONE;
225 bool isIntendedDriverLoaded = false;
226 if (api == GraphicsEnv::Api::API_GL) {
227 driver = mGpuStats.glDriverToLoad;
228 isIntendedDriverLoaded = isLoaded &&
229 ((mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE) ||
230 (mGpuStats.glDriverToLoad == mGpuStats.glDriverFallback));
231 } else {
232 driver = mGpuStats.vkDriverToLoad;
233 isIntendedDriverLoaded =
234 isLoaded && (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE);
235 }
236
237 sendGpuStatsLocked(driver, isIntendedDriverLoaded, driverLoadingTime);
238}
239
240void GraphicsEnv::clearDriverLoadingInfo(GraphicsEnv::Api api) {
241 ATRACE_CALL();
242
243 std::lock_guard<std::mutex> lock(mStatsLock);
244 if (api == GraphicsEnv::Api::API_GL) {
245 mGpuStats.glDriverToLoad = GraphicsEnv::Driver::NONE;
246 mGpuStats.glDriverFallback = GraphicsEnv::Driver::NONE;
247 }
248}
249
250static sp<IGpuService> getGpuService() {
251 const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
252 if (!binder) {
253 ALOGE("Failed to get gpu service");
254 return nullptr;
255 }
256
257 return interface_cast<IGpuService>(binder);
258}
259
260void GraphicsEnv::sendGpuStatsLocked(GraphicsEnv::Driver driver, bool isDriverLoaded,
261 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800262 ATRACE_CALL();
263
264 // Do not sendGpuStats for those skipping the GraphicsEnvironment setup
265 if (mGpuStats.appPackageName.empty()) return;
266
267 ALOGV("sendGpuStats:\n"
268 "\tdriverPackageName[%s]\n"
269 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800270 "\tdriverVersionCode[%" PRIu64 "]\n"
271 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800272 "\tappPackageName[%s]\n"
273 "\tdriver[%d]\n"
274 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800275 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800276 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-19 16:00:25 -0800277 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
278 static_cast<int32_t>(driver), isDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800279
Yiwei Zhangd9861812019-02-13 11:51:55 -0800280 const sp<IGpuService> gpuService = getGpuService();
281 if (gpuService) {
282 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800283 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang512a7232019-02-15 16:04:41 -0800284 mGpuStats.appPackageName, driver, isDriverLoaded,
285 driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800286 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800287}
288
Tim Van Patten5f744f12018-12-12 11:46:21 -0700289void* GraphicsEnv::loadLibrary(std::string name) {
290 const android_dlextinfo dlextinfo = {
291 .flags = ANDROID_DLEXT_USE_NAMESPACE,
292 .library_namespace = getAngleNamespace(),
293 };
294
295 std::string libName = std::string("lib") + name + "_angle.so";
296
297 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
298
299 if (so) {
300 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
301 return so;
302 } else {
303 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
304 }
305
306 return nullptr;
307}
308
309bool GraphicsEnv::checkAngleRules(void* so) {
310 char manufacturer[PROPERTY_VALUE_MAX];
311 char model[PROPERTY_VALUE_MAX];
312 property_get("ro.product.manufacturer", manufacturer, "UNSET");
313 property_get("ro.product.model", model, "UNSET");
314
315 auto ANGLEGetFeatureSupportUtilAPIVersion =
316 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
317 "ANGLEGetFeatureSupportUtilAPIVersion");
318
319 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
320 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
321 return false;
322 }
323
324 // Negotiate the interface version by requesting most recent known to the platform
325 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
326 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
327 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
328 "requested version %u",
329 versionToUse);
330 return false;
331 }
332
333 // Add and remove versions below as needed
334 bool useAngle = false;
335 switch (versionToUse) {
336 case 2: {
337 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
338 void* rulesHandle = nullptr;
339 int rulesVersion = 0;
340 void* systemInfoHandle = nullptr;
341
342 // Get the symbols for the feature-support-utility library:
343#define GET_SYMBOL(symbol) \
344 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
345 if (!symbol) { \
346 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
347 break; \
348 }
349 GET_SYMBOL(ANGLEAndroidParseRulesString);
350 GET_SYMBOL(ANGLEGetSystemInfo);
351 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
352 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
353 GET_SYMBOL(ANGLEFreeRulesHandle);
354 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
355
356 // Parse the rules, obtain the SystemInfo, and evaluate the
357 // application against the rules:
358 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
359 ALOGW("ANGLE feature-support library cannot parse rules file");
360 break;
361 }
362 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
363 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
364 break;
365 }
366 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) {
367 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
368 break;
369 }
370 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
371 systemInfoHandle, mAngleAppName.c_str());
372 (ANGLEFreeRulesHandle)(rulesHandle);
373 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
374 } break;
375
376 default:
377 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
378 }
379
380 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
381 return useAngle;
382}
383
384bool GraphicsEnv::shouldUseAngle(std::string appName) {
385 if (appName != mAngleAppName) {
386 // Make sure we are checking the app we were init'ed for
387 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
388 appName.c_str());
389 return false;
390 }
391
392 return shouldUseAngle();
393}
394
395bool GraphicsEnv::shouldUseAngle() {
396 // Make sure we are init'ed
397 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700398 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700399 return false;
400 }
401
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700402 return (mUseAngle == YES) ? true : false;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700403}
404
405void GraphicsEnv::updateUseAngle() {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700406 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700407
408 const char* ANGLE_PREFER_ANGLE = "angle";
409 const char* ANGLE_PREFER_NATIVE = "native";
410
411 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
412 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700413 mUseAngle = YES;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700414 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
415 ALOGV("User set \"Developer Options\" to force the use of Native");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700416 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700417 } else {
418 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
419 // load ANGLE and call the updatable opt-in/out logic:
Cody Northropc15d3822019-01-17 10:26:47 -0700420 void* featureSo = loadLibrary("feature_support");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700421 if (featureSo) {
422 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700423 mUseAngle = checkAngleRules(featureSo) ? YES : NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700424 dlclose(featureSo);
425 featureSo = nullptr;
426 } else {
427 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
428 }
429 }
430}
431
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600432void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700433 const std::string developerOptIn, const int rulesFd,
434 const long rulesOffset, const long rulesLength) {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700435 if (mUseAngle != UNKNOWN) {
436 // We've already figured out an answer for this app, so just return.
437 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
438 (mUseAngle == YES) ? "true" : "false");
439 return;
440 }
441
Tim Van Patten5f744f12018-12-12 11:46:21 -0700442 ALOGV("setting ANGLE path to '%s'", path.c_str());
443 mAnglePath = path;
444 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
445 mAngleAppName = appName;
446 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
447 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600448
Tim Van Patten5f744f12018-12-12 11:46:21 -0700449 lseek(rulesFd, rulesOffset, SEEK_SET);
450 mRulesBuffer = std::vector<char>(rulesLength + 1);
451 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
452 if (numBytesRead < 0) {
453 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
454 numBytesRead = 0;
455 } else if (numBytesRead == 0) {
456 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600457 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700458 if (numBytesRead != rulesLength) {
459 ALOGW("Did not read all of the necessary bytes from the rules file."
460 "expected: %ld, got: %zd",
461 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700462 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700463 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 10:34:58 -0600464
Tim Van Patten5f744f12018-12-12 11:46:21 -0700465 // Update the current status of whether we should use ANGLE or not
466 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600467}
468
Victor Khimenko4819b522018-07-13 17:24:18 +0200469void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600470 if (mLayerPaths.empty()) {
471 mLayerPaths = layerPaths;
472 mAppNamespace = appNamespace;
473 } else {
474 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800475 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600476 }
477}
478
Victor Khimenko4819b522018-07-13 17:24:18 +0200479NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600480 return mAppNamespace;
481}
482
Tim Van Patten5f744f12018-12-12 11:46:21 -0700483std::string& GraphicsEnv::getAngleAppName() {
484 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600485}
486
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600487const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600488 return mLayerPaths;
489}
490
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600491const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600492 return mDebugLayers;
493}
494
Cody Northropb9b01b62018-10-23 13:13:10 -0600495const std::string& GraphicsEnv::getDebugLayersGLES() {
496 return mDebugLayersGLES;
497}
498
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600499void GraphicsEnv::setDebugLayers(const std::string layers) {
500 mDebugLayers = layers;
501}
502
Cody Northropb9b01b62018-10-23 13:13:10 -0600503void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
504 mDebugLayersGLES = layers;
505}
506
Jesse Hall53457db2016-12-14 16:54:06 -0800507android_namespace_t* GraphicsEnv::getDriverNamespace() {
508 static std::once_flag once;
509 std::call_once(once, [this]() {
Yiwei Zhang64d89212018-11-27 19:58:29 -0800510 if (mDriverPath.empty()) return;
511
512 auto vndkNamespace = android_get_exported_namespace("vndk");
513 if (!vndkNamespace) return;
514
Jesse Hall57de0ff2017-05-05 16:41:35 -0700515 mDriverNamespace = android_create_namespace("gfx driver",
Peiyong Lin2e9c74c2018-10-24 11:18:07 -0700516 mDriverPath.c_str(), // ld_library_path
Jesse Hall57de0ff2017-05-05 16:41:35 -0700517 mDriverPath.c_str(), // default_library_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800518 ANDROID_NAMESPACE_TYPE_ISOLATED,
Jesse Hall57de0ff2017-05-05 16:41:35 -0700519 nullptr, // permitted_when_isolated_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800520 nullptr);
521
522 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
523 if (llndkLibraries.empty()) {
524 mDriverNamespace = nullptr;
525 return;
526 }
527 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
528 ALOGE("Failed to link default namespace[%s]", dlerror());
529 mDriverNamespace = nullptr;
530 return;
531 }
532
533 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
534 if (vndkspLibraries.empty()) {
535 mDriverNamespace = nullptr;
536 return;
537 }
538 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
539 ALOGE("Failed to link vndk namespace[%s]", dlerror());
540 mDriverNamespace = nullptr;
541 return;
542 }
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800543
544 if (mSphalLibraries.empty()) return;
545
546 // Make additional libraries in sphal to be accessible
547 auto sphalNamespace = android_get_exported_namespace("sphal");
548 if (!sphalNamespace) {
549 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
550 mSphalLibraries.c_str());
551 mDriverNamespace = nullptr;
552 return;
553 }
554
555 if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) {
556 ALOGE("Failed to link sphal namespace[%s]", dlerror());
557 mDriverNamespace = nullptr;
558 return;
559 }
Jesse Hall53457db2016-12-14 16:54:06 -0800560 });
Yiwei Zhang64d89212018-11-27 19:58:29 -0800561
Jesse Hall53457db2016-12-14 16:54:06 -0800562 return mDriverNamespace;
563}
564
Cody Northrop1f00e172018-04-02 11:23:31 -0600565android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700566 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600567
Cody Northrop3892cfa2019-01-30 10:03:12 -0700568 if (mAngleNamespace) {
569 return mAngleNamespace;
570 }
571
572 if (mAnglePath.empty()) {
573 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
574 return nullptr;
575 }
576
577 mAngleNamespace = android_create_namespace("ANGLE",
578 nullptr, // ld_library_path
579 mAnglePath.c_str(), // default_library_path
580 ANDROID_NAMESPACE_TYPE_SHARED |
581 ANDROID_NAMESPACE_TYPE_ISOLATED,
582 nullptr, // permitted_when_isolated_path
583 nullptr);
584
585 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600586
587 return mAngleNamespace;
588}
589
Jesse Hall90b25ed2016-12-12 12:56:46 -0800590} // namespace android