blob: 2bfd908a651f652fd994471c786214e3fd51888d [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 Zhang794d2952019-05-06 17:43:59 -0700165 int64_t driverBuildTime, const std::string& appPackageName,
166 const int vulkanVersion) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800167 ATRACE_CALL();
168
Yiwei Zhangd9861812019-02-13 11:51:55 -0800169 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800170 ALOGV("setGpuStats:\n"
171 "\tdriverPackageName[%s]\n"
172 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800173 "\tdriverVersionCode[%" PRIu64 "]\n"
174 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700175 "\tappPackageName[%s]\n"
176 "\tvulkanVersion[%d]\n",
Yiwei Zhang96c01712019-02-19 16:00:25 -0800177 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700178 appPackageName.c_str(), vulkanVersion);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800179
Yiwei Zhangd9861812019-02-13 11:51:55 -0800180 mGpuStats.driverPackageName = driverPackageName;
181 mGpuStats.driverVersionName = driverVersionName;
182 mGpuStats.driverVersionCode = driverVersionCode;
Yiwei Zhang96c01712019-02-19 16:00:25 -0800183 mGpuStats.driverBuildTime = driverBuildTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800184 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang794d2952019-05-06 17:43:59 -0700185 mGpuStats.vulkanVersion = vulkanVersion;
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800186}
187
Yiwei Zhangd9861812019-02-13 11:51:55 -0800188void GraphicsEnv::setDriverToLoad(GraphicsEnv::Driver driver) {
189 ATRACE_CALL();
190
191 std::lock_guard<std::mutex> lock(mStatsLock);
192 switch (driver) {
193 case GraphicsEnv::Driver::GL:
194 case GraphicsEnv::Driver::GL_UPDATED:
195 case GraphicsEnv::Driver::ANGLE: {
196 if (mGpuStats.glDriverToLoad == GraphicsEnv::Driver::NONE) {
197 mGpuStats.glDriverToLoad = driver;
198 break;
199 }
200
201 if (mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE) {
202 mGpuStats.glDriverFallback = driver;
203 }
204 break;
205 }
206 case Driver::VULKAN:
207 case Driver::VULKAN_UPDATED: {
208 if (mGpuStats.vkDriverToLoad == GraphicsEnv::Driver::NONE) {
209 mGpuStats.vkDriverToLoad = driver;
210 break;
211 }
212
213 if (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE) {
214 mGpuStats.vkDriverFallback = driver;
215 }
216 break;
217 }
218 default:
219 break;
220 }
221}
222
223void GraphicsEnv::setDriverLoaded(GraphicsEnv::Api api, bool isLoaded, int64_t driverLoadingTime) {
224 ATRACE_CALL();
225
226 std::lock_guard<std::mutex> lock(mStatsLock);
227 GraphicsEnv::Driver driver = GraphicsEnv::Driver::NONE;
228 bool isIntendedDriverLoaded = false;
229 if (api == GraphicsEnv::Api::API_GL) {
230 driver = mGpuStats.glDriverToLoad;
Yiwei Zhang8e7c4b62019-05-08 15:57:59 -0700231 isIntendedDriverLoaded =
232 isLoaded && (mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800233 } else {
234 driver = mGpuStats.vkDriverToLoad;
235 isIntendedDriverLoaded =
236 isLoaded && (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE);
237 }
238
239 sendGpuStatsLocked(driver, isIntendedDriverLoaded, driverLoadingTime);
240}
241
Yiwei Zhangd9861812019-02-13 11:51:55 -0800242static sp<IGpuService> getGpuService() {
243 const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
244 if (!binder) {
245 ALOGE("Failed to get gpu service");
246 return nullptr;
247 }
248
249 return interface_cast<IGpuService>(binder);
250}
251
252void GraphicsEnv::sendGpuStatsLocked(GraphicsEnv::Driver driver, bool isDriverLoaded,
253 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800254 ATRACE_CALL();
255
256 // Do not sendGpuStats for those skipping the GraphicsEnvironment setup
257 if (mGpuStats.appPackageName.empty()) return;
258
259 ALOGV("sendGpuStats:\n"
260 "\tdriverPackageName[%s]\n"
261 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800262 "\tdriverVersionCode[%" PRIu64 "]\n"
263 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800264 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700265 "\tvulkanVersion[%d]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800266 "\tdriver[%d]\n"
267 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800268 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800269 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-19 16:00:25 -0800270 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
Yiwei Zhang794d2952019-05-06 17:43:59 -0700271 mGpuStats.vulkanVersion, static_cast<int32_t>(driver), isDriverLoaded, 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,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800276 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700277 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
278 isDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800279 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800280}
281
Tim Van Patten5f744f12018-12-12 11:46:21 -0700282void* GraphicsEnv::loadLibrary(std::string name) {
283 const android_dlextinfo dlextinfo = {
284 .flags = ANDROID_DLEXT_USE_NAMESPACE,
285 .library_namespace = getAngleNamespace(),
286 };
287
288 std::string libName = std::string("lib") + name + "_angle.so";
289
290 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
291
292 if (so) {
293 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
294 return so;
295 } else {
296 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
297 }
298
299 return nullptr;
300}
301
302bool GraphicsEnv::checkAngleRules(void* so) {
303 char manufacturer[PROPERTY_VALUE_MAX];
304 char model[PROPERTY_VALUE_MAX];
305 property_get("ro.product.manufacturer", manufacturer, "UNSET");
306 property_get("ro.product.model", model, "UNSET");
307
308 auto ANGLEGetFeatureSupportUtilAPIVersion =
309 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
310 "ANGLEGetFeatureSupportUtilAPIVersion");
311
312 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
313 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
314 return false;
315 }
316
317 // Negotiate the interface version by requesting most recent known to the platform
318 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
319 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
320 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
321 "requested version %u",
322 versionToUse);
323 return false;
324 }
325
326 // Add and remove versions below as needed
327 bool useAngle = false;
328 switch (versionToUse) {
329 case 2: {
330 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
331 void* rulesHandle = nullptr;
332 int rulesVersion = 0;
333 void* systemInfoHandle = nullptr;
334
335 // Get the symbols for the feature-support-utility library:
336#define GET_SYMBOL(symbol) \
337 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
338 if (!symbol) { \
339 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
340 break; \
341 }
342 GET_SYMBOL(ANGLEAndroidParseRulesString);
343 GET_SYMBOL(ANGLEGetSystemInfo);
344 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
345 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
346 GET_SYMBOL(ANGLEFreeRulesHandle);
347 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
348
349 // Parse the rules, obtain the SystemInfo, and evaluate the
350 // application against the rules:
351 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
352 ALOGW("ANGLE feature-support library cannot parse rules file");
353 break;
354 }
355 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
356 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
357 break;
358 }
359 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) {
360 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
361 break;
362 }
363 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
364 systemInfoHandle, mAngleAppName.c_str());
365 (ANGLEFreeRulesHandle)(rulesHandle);
366 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
367 } break;
368
369 default:
370 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
371 }
372
373 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
374 return useAngle;
375}
376
377bool GraphicsEnv::shouldUseAngle(std::string appName) {
378 if (appName != mAngleAppName) {
379 // Make sure we are checking the app we were init'ed for
380 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
381 appName.c_str());
382 return false;
383 }
384
385 return shouldUseAngle();
386}
387
388bool GraphicsEnv::shouldUseAngle() {
389 // Make sure we are init'ed
390 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700391 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700392 return false;
393 }
394
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700395 return (mUseAngle == YES) ? true : false;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700396}
397
398void GraphicsEnv::updateUseAngle() {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700399 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700400
401 const char* ANGLE_PREFER_ANGLE = "angle";
402 const char* ANGLE_PREFER_NATIVE = "native";
403
404 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
405 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700406 mUseAngle = YES;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700407 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
408 ALOGV("User set \"Developer Options\" to force the use of Native");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700409 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700410 } else {
411 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
412 // load ANGLE and call the updatable opt-in/out logic:
Cody Northropc15d3822019-01-17 10:26:47 -0700413 void* featureSo = loadLibrary("feature_support");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700414 if (featureSo) {
415 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700416 mUseAngle = checkAngleRules(featureSo) ? YES : NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700417 dlclose(featureSo);
418 featureSo = nullptr;
419 } else {
420 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
421 }
422 }
423}
424
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600425void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700426 const std::string developerOptIn, const int rulesFd,
427 const long rulesOffset, const long rulesLength) {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700428 if (mUseAngle != UNKNOWN) {
429 // We've already figured out an answer for this app, so just return.
430 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
431 (mUseAngle == YES) ? "true" : "false");
432 return;
433 }
434
Tim Van Patten5f744f12018-12-12 11:46:21 -0700435 ALOGV("setting ANGLE path to '%s'", path.c_str());
436 mAnglePath = path;
437 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
438 mAngleAppName = appName;
439 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
440 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600441
Tim Van Patten5f744f12018-12-12 11:46:21 -0700442 lseek(rulesFd, rulesOffset, SEEK_SET);
443 mRulesBuffer = std::vector<char>(rulesLength + 1);
444 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
445 if (numBytesRead < 0) {
446 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
447 numBytesRead = 0;
448 } else if (numBytesRead == 0) {
449 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600450 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700451 if (numBytesRead != rulesLength) {
452 ALOGW("Did not read all of the necessary bytes from the rules file."
453 "expected: %ld, got: %zd",
454 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700455 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700456 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 10:34:58 -0600457
Tim Van Patten5f744f12018-12-12 11:46:21 -0700458 // Update the current status of whether we should use ANGLE or not
459 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600460}
461
Victor Khimenko4819b522018-07-13 17:24:18 +0200462void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600463 if (mLayerPaths.empty()) {
464 mLayerPaths = layerPaths;
465 mAppNamespace = appNamespace;
466 } else {
467 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800468 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600469 }
470}
471
Victor Khimenko4819b522018-07-13 17:24:18 +0200472NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600473 return mAppNamespace;
474}
475
Tim Van Patten5f744f12018-12-12 11:46:21 -0700476std::string& GraphicsEnv::getAngleAppName() {
477 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600478}
479
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600480const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600481 return mLayerPaths;
482}
483
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600484const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600485 return mDebugLayers;
486}
487
Cody Northropb9b01b62018-10-23 13:13:10 -0600488const std::string& GraphicsEnv::getDebugLayersGLES() {
489 return mDebugLayersGLES;
490}
491
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600492void GraphicsEnv::setDebugLayers(const std::string layers) {
493 mDebugLayers = layers;
494}
495
Cody Northropb9b01b62018-10-23 13:13:10 -0600496void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
497 mDebugLayersGLES = layers;
498}
499
Jesse Hall53457db2016-12-14 16:54:06 -0800500android_namespace_t* GraphicsEnv::getDriverNamespace() {
501 static std::once_flag once;
502 std::call_once(once, [this]() {
Yiwei Zhang64d89212018-11-27 19:58:29 -0800503 if (mDriverPath.empty()) return;
504
505 auto vndkNamespace = android_get_exported_namespace("vndk");
506 if (!vndkNamespace) return;
507
Jesse Hall57de0ff2017-05-05 16:41:35 -0700508 mDriverNamespace = android_create_namespace("gfx driver",
Peiyong Lin2e9c74c2018-10-24 11:18:07 -0700509 mDriverPath.c_str(), // ld_library_path
Jesse Hall57de0ff2017-05-05 16:41:35 -0700510 mDriverPath.c_str(), // default_library_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800511 ANDROID_NAMESPACE_TYPE_ISOLATED,
Jesse Hall57de0ff2017-05-05 16:41:35 -0700512 nullptr, // permitted_when_isolated_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800513 nullptr);
514
515 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
516 if (llndkLibraries.empty()) {
517 mDriverNamespace = nullptr;
518 return;
519 }
520 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
521 ALOGE("Failed to link default namespace[%s]", dlerror());
522 mDriverNamespace = nullptr;
523 return;
524 }
525
526 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
527 if (vndkspLibraries.empty()) {
528 mDriverNamespace = nullptr;
529 return;
530 }
531 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
532 ALOGE("Failed to link vndk namespace[%s]", dlerror());
533 mDriverNamespace = nullptr;
534 return;
535 }
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800536
537 if (mSphalLibraries.empty()) return;
538
539 // Make additional libraries in sphal to be accessible
540 auto sphalNamespace = android_get_exported_namespace("sphal");
541 if (!sphalNamespace) {
542 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
543 mSphalLibraries.c_str());
544 mDriverNamespace = nullptr;
545 return;
546 }
547
548 if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) {
549 ALOGE("Failed to link sphal namespace[%s]", dlerror());
550 mDriverNamespace = nullptr;
551 return;
552 }
Jesse Hall53457db2016-12-14 16:54:06 -0800553 });
Yiwei Zhang64d89212018-11-27 19:58:29 -0800554
Jesse Hall53457db2016-12-14 16:54:06 -0800555 return mDriverNamespace;
556}
557
Cody Northrop1f00e172018-04-02 11:23:31 -0600558android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700559 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600560
Cody Northrop3892cfa2019-01-30 10:03:12 -0700561 if (mAngleNamespace) {
562 return mAngleNamespace;
563 }
564
565 if (mAnglePath.empty()) {
566 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
567 return nullptr;
568 }
569
570 mAngleNamespace = android_create_namespace("ANGLE",
571 nullptr, // ld_library_path
572 mAnglePath.c_str(), // default_library_path
573 ANDROID_NAMESPACE_TYPE_SHARED |
574 ANDROID_NAMESPACE_TYPE_ISOLATED,
575 nullptr, // permitted_when_isolated_path
576 nullptr);
577
578 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600579
580 return mAngleNamespace;
581}
582
Jesse Hall90b25ed2016-12-12 12:56:46 -0800583} // namespace android