blob: c5d5f71800e7a4f884658402b32bf6f85ab378d1 [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>
Yiwei Zhang3c74da92019-06-28 10:16:49 -070040#include <thread>
Tim Van Patten5f744f12018-12-12 11:46:21 -070041
Jesse Hall57de0ff2017-05-05 16:41:35 -070042// TODO(b/37049319) Get this from a header once one exists
43extern "C" {
Yiwei Zhang64d89212018-11-27 19:58:29 -080044android_namespace_t* android_get_exported_namespace(const char*);
45android_namespace_t* android_create_namespace(const char* name, const char* ld_library_path,
46 const char* default_library_path, uint64_t type,
47 const char* permitted_when_isolated_path,
48 android_namespace_t* parent);
49bool android_link_namespaces(android_namespace_t* from, android_namespace_t* to,
50 const char* shared_libs_sonames);
Jiyong Park9b816a82018-01-02 17:37:37 +090051
Yiwei Zhang64d89212018-11-27 19:58:29 -080052enum {
53 ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
54 ANDROID_NAMESPACE_TYPE_SHARED = 2,
55};
Jesse Hall57de0ff2017-05-05 16:41:35 -070056}
57
Tim Van Patten5f744f12018-12-12 11:46:21 -070058// TODO(ianelliott@): Get the following from an ANGLE header:
59#define CURRENT_ANGLE_API_VERSION 2 // Current API verion we are targetting
60// Version-2 API:
61typedef bool (*fpANGLEGetFeatureSupportUtilAPIVersion)(unsigned int* versionToUse);
62typedef bool (*fpANGLEAndroidParseRulesString)(const char* rulesString, void** rulesHandle,
63 int* rulesVersion);
64typedef bool (*fpANGLEGetSystemInfo)(void** handle);
65typedef bool (*fpANGLEAddDeviceInfoToSystemInfo)(const char* deviceMfr, const char* deviceModel,
66 void* handle);
67typedef bool (*fpANGLEShouldBeUsedForApplication)(void* rulesHandle, int rulesVersion,
68 void* systemInfoHandle, const char* appName);
69typedef bool (*fpANGLEFreeRulesHandle)(void* handle);
70typedef bool (*fpANGLEFreeSystemInfoHandle)(void* handle);
71
Jesse Hall90b25ed2016-12-12 12:56:46 -080072namespace android {
73
Yiwei Zhang64d89212018-11-27 19:58:29 -080074enum NativeLibrary {
75 LLNDK = 0,
76 VNDKSP = 1,
77};
78
79static constexpr const char* kNativeLibrariesSystemConfigPath[] = {"/etc/llndk.libraries.txt",
80 "/etc/vndksp.libraries.txt"};
81
82static std::string vndkVersionStr() {
83#ifdef __BIONIC__
84 std::string version = android::base::GetProperty("ro.vndk.version", "");
85 if (version != "" && version != "current") {
86 return "." + version;
87 }
88#endif
89 return "";
90}
91
92static void insertVndkVersionStr(std::string* fileName) {
93 LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr");
94 size_t insertPos = fileName->find_last_of(".");
95 if (insertPos == std::string::npos) {
96 insertPos = fileName->length();
97 }
98 fileName->insert(insertPos, vndkVersionStr());
99}
100
101static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) {
102 // Read list of public native libraries from the config file.
103 std::string fileContent;
104 if (!base::ReadFileToString(configFile, &fileContent)) {
105 return false;
106 }
107
108 std::vector<std::string> lines = base::Split(fileContent, "\n");
109
110 for (auto& line : lines) {
111 auto trimmedLine = base::Trim(line);
112 if (!trimmedLine.empty()) {
113 soNames->push_back(trimmedLine);
114 }
115 }
116
117 return true;
118}
119
120static const std::string getSystemNativeLibraries(NativeLibrary type) {
121 static const char* androidRootEnv = getenv("ANDROID_ROOT");
122 static const std::string rootDir = androidRootEnv != nullptr ? androidRootEnv : "/system";
123
124 std::string nativeLibrariesSystemConfig = rootDir + kNativeLibrariesSystemConfigPath[type];
125
126 insertVndkVersionStr(&nativeLibrariesSystemConfig);
127
128 std::vector<std::string> soNames;
129 if (!readConfig(nativeLibrariesSystemConfig, &soNames)) {
130 ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str());
131 return "";
132 }
133
134 return base::Join(soNames, ':');
135}
136
Jesse Hall90b25ed2016-12-12 12:56:46 -0800137/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
138 static GraphicsEnv env;
139 return env;
140}
141
Cody Northrop629ce4e2018-10-15 07:22:09 -0600142int GraphicsEnv::getCanLoadSystemLibraries() {
143 if (property_get_bool("ro.debuggable", false) && prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
144 // Return an integer value since this crosses library boundaries
145 return 1;
146 }
147 return 0;
148}
149
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800150void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string path,
151 const std::string sphalLibraries) {
152 if (!mDriverPath.empty() || !mSphalLibraries.empty()) {
153 ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries "
154 "from '%s' to '%s'",
155 mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800156 return;
157 }
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800158 ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(),
159 sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800160 mDriverPath = path;
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800161 mSphalLibraries = sphalLibraries;
Jesse Hall90b25ed2016-12-12 12:56:46 -0800162}
163
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700164void GraphicsEnv::hintActivityLaunch() {
165 ATRACE_CALL();
166
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700167 std::thread trySendGpuStatsThread([this]() {
168 // If there's already graphics driver preloaded in the process, just send
169 // the stats info to GpuStats directly through async binder.
170 std::lock_guard<std::mutex> lock(mStatsLock);
171 if (mGpuStats.glDriverToSend) {
172 mGpuStats.glDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700173 sendGpuStatsLocked(GpuStatsInfo::Api::API_GL, true, mGpuStats.glDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700174 }
175 if (mGpuStats.vkDriverToSend) {
176 mGpuStats.vkDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700177 sendGpuStatsLocked(GpuStatsInfo::Api::API_VK, true, mGpuStats.vkDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700178 }
179 });
180 trySendGpuStatsThread.detach();
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700181}
182
Greg Kaiser210bb7e2019-02-12 12:40:05 -0800183void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800184 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700185 int64_t driverBuildTime, const std::string& appPackageName,
186 const int vulkanVersion) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800187 ATRACE_CALL();
188
Yiwei Zhangd9861812019-02-13 11:51:55 -0800189 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800190 ALOGV("setGpuStats:\n"
191 "\tdriverPackageName[%s]\n"
192 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800193 "\tdriverVersionCode[%" PRIu64 "]\n"
194 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700195 "\tappPackageName[%s]\n"
196 "\tvulkanVersion[%d]\n",
Yiwei Zhang96c01712019-02-19 16:00:25 -0800197 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700198 appPackageName.c_str(), vulkanVersion);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800199
Yiwei Zhangd9861812019-02-13 11:51:55 -0800200 mGpuStats.driverPackageName = driverPackageName;
201 mGpuStats.driverVersionName = driverVersionName;
202 mGpuStats.driverVersionCode = driverVersionCode;
Yiwei Zhang96c01712019-02-19 16:00:25 -0800203 mGpuStats.driverBuildTime = driverBuildTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800204 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang794d2952019-05-06 17:43:59 -0700205 mGpuStats.vulkanVersion = vulkanVersion;
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800206}
207
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700208void GraphicsEnv::setDriverToLoad(GpuStatsInfo::Driver driver) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800209 ATRACE_CALL();
210
211 std::lock_guard<std::mutex> lock(mStatsLock);
212 switch (driver) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700213 case GpuStatsInfo::Driver::GL:
214 case GpuStatsInfo::Driver::GL_UPDATED:
215 case GpuStatsInfo::Driver::ANGLE: {
216 if (mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800217 mGpuStats.glDriverToLoad = driver;
218 break;
219 }
220
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700221 if (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800222 mGpuStats.glDriverFallback = driver;
223 }
224 break;
225 }
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700226 case GpuStatsInfo::Driver::VULKAN:
227 case GpuStatsInfo::Driver::VULKAN_UPDATED: {
228 if (mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800229 mGpuStats.vkDriverToLoad = driver;
230 break;
231 }
232
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700233 if (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800234 mGpuStats.vkDriverFallback = driver;
235 }
236 break;
237 }
238 default:
239 break;
240 }
241}
242
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700243void GraphicsEnv::setDriverLoaded(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700244 int64_t driverLoadingTime) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800245 ATRACE_CALL();
246
247 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700248 const bool doNotSend = mGpuStats.appPackageName.empty();
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700249 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700250 if (doNotSend) mGpuStats.glDriverToSend = true;
251 mGpuStats.glDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800252 } else {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700253 if (doNotSend) mGpuStats.vkDriverToSend = true;
254 mGpuStats.vkDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800255 }
256
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700257 sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800258}
259
Yiwei Zhangd9861812019-02-13 11:51:55 -0800260static sp<IGpuService> getGpuService() {
261 const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
262 if (!binder) {
263 ALOGE("Failed to get gpu service");
264 return nullptr;
265 }
266
267 return interface_cast<IGpuService>(binder);
268}
269
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700270void GraphicsEnv::setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value) {
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700271 ATRACE_CALL();
272
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700273 std::lock_guard<std::mutex> lock(mStatsLock);
274 const sp<IGpuService> gpuService = getGpuService();
275 if (gpuService) {
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700276 gpuService->setTargetStats(mGpuStats.appPackageName, mGpuStats.driverVersionCode, stats,
277 value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700278 }
279}
280
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700281void GraphicsEnv::sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800282 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800283 ATRACE_CALL();
284
285 // Do not sendGpuStats for those skipping the GraphicsEnvironment setup
286 if (mGpuStats.appPackageName.empty()) return;
287
288 ALOGV("sendGpuStats:\n"
289 "\tdriverPackageName[%s]\n"
290 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800291 "\tdriverVersionCode[%" PRIu64 "]\n"
292 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800293 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700294 "\tvulkanVersion[%d]\n"
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700295 "\tapi[%d]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800296 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800297 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800298 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-19 16:00:25 -0800299 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700300 mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime);
301
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700302 GpuStatsInfo::Driver driver = GpuStatsInfo::Driver::NONE;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700303 bool isIntendedDriverLoaded = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700304 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700305 driver = mGpuStats.glDriverToLoad;
306 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700307 isDriverLoaded && (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700308 } else {
309 driver = mGpuStats.vkDriverToLoad;
310 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700311 isDriverLoaded && (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700312 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800313
Yiwei Zhangd9861812019-02-13 11:51:55 -0800314 const sp<IGpuService> gpuService = getGpuService();
315 if (gpuService) {
316 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800317 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700318 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700319 isIntendedDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800320 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800321}
322
Tim Van Patten5f744f12018-12-12 11:46:21 -0700323void* GraphicsEnv::loadLibrary(std::string name) {
324 const android_dlextinfo dlextinfo = {
325 .flags = ANDROID_DLEXT_USE_NAMESPACE,
326 .library_namespace = getAngleNamespace(),
327 };
328
329 std::string libName = std::string("lib") + name + "_angle.so";
330
331 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
332
333 if (so) {
334 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
335 return so;
336 } else {
337 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
338 }
339
340 return nullptr;
341}
342
343bool GraphicsEnv::checkAngleRules(void* so) {
344 char manufacturer[PROPERTY_VALUE_MAX];
345 char model[PROPERTY_VALUE_MAX];
346 property_get("ro.product.manufacturer", manufacturer, "UNSET");
347 property_get("ro.product.model", model, "UNSET");
348
349 auto ANGLEGetFeatureSupportUtilAPIVersion =
350 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
351 "ANGLEGetFeatureSupportUtilAPIVersion");
352
353 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
354 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
355 return false;
356 }
357
358 // Negotiate the interface version by requesting most recent known to the platform
359 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
360 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
361 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
362 "requested version %u",
363 versionToUse);
364 return false;
365 }
366
367 // Add and remove versions below as needed
368 bool useAngle = false;
369 switch (versionToUse) {
370 case 2: {
371 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
372 void* rulesHandle = nullptr;
373 int rulesVersion = 0;
374 void* systemInfoHandle = nullptr;
375
376 // Get the symbols for the feature-support-utility library:
377#define GET_SYMBOL(symbol) \
378 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
379 if (!symbol) { \
380 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
381 break; \
382 }
383 GET_SYMBOL(ANGLEAndroidParseRulesString);
384 GET_SYMBOL(ANGLEGetSystemInfo);
385 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
386 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
387 GET_SYMBOL(ANGLEFreeRulesHandle);
388 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
389
390 // Parse the rules, obtain the SystemInfo, and evaluate the
391 // application against the rules:
392 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
393 ALOGW("ANGLE feature-support library cannot parse rules file");
394 break;
395 }
396 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
397 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
398 break;
399 }
400 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) {
401 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
402 break;
403 }
404 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
405 systemInfoHandle, mAngleAppName.c_str());
406 (ANGLEFreeRulesHandle)(rulesHandle);
407 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
408 } break;
409
410 default:
411 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
412 }
413
414 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
415 return useAngle;
416}
417
418bool GraphicsEnv::shouldUseAngle(std::string appName) {
419 if (appName != mAngleAppName) {
420 // Make sure we are checking the app we were init'ed for
421 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
422 appName.c_str());
423 return false;
424 }
425
426 return shouldUseAngle();
427}
428
429bool GraphicsEnv::shouldUseAngle() {
430 // Make sure we are init'ed
431 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700432 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700433 return false;
434 }
435
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700436 return (mUseAngle == YES) ? true : false;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700437}
438
439void GraphicsEnv::updateUseAngle() {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700440 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700441
442 const char* ANGLE_PREFER_ANGLE = "angle";
443 const char* ANGLE_PREFER_NATIVE = "native";
444
445 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
446 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700447 mUseAngle = YES;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700448 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
449 ALOGV("User set \"Developer Options\" to force the use of Native");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700450 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700451 } else {
452 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
453 // load ANGLE and call the updatable opt-in/out logic:
Cody Northropc15d3822019-01-17 10:26:47 -0700454 void* featureSo = loadLibrary("feature_support");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700455 if (featureSo) {
456 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700457 mUseAngle = checkAngleRules(featureSo) ? YES : NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700458 dlclose(featureSo);
459 featureSo = nullptr;
460 } else {
461 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
462 }
463 }
464}
465
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600466void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700467 const std::string developerOptIn, const int rulesFd,
468 const long rulesOffset, const long rulesLength) {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700469 if (mUseAngle != UNKNOWN) {
470 // We've already figured out an answer for this app, so just return.
471 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
472 (mUseAngle == YES) ? "true" : "false");
473 return;
474 }
475
Tim Van Patten5f744f12018-12-12 11:46:21 -0700476 ALOGV("setting ANGLE path to '%s'", path.c_str());
477 mAnglePath = path;
478 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
479 mAngleAppName = appName;
480 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
481 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600482
Tim Van Patten5f744f12018-12-12 11:46:21 -0700483 lseek(rulesFd, rulesOffset, SEEK_SET);
484 mRulesBuffer = std::vector<char>(rulesLength + 1);
485 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
486 if (numBytesRead < 0) {
487 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
488 numBytesRead = 0;
489 } else if (numBytesRead == 0) {
490 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600491 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700492 if (numBytesRead != rulesLength) {
493 ALOGW("Did not read all of the necessary bytes from the rules file."
494 "expected: %ld, got: %zd",
495 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700496 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700497 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 10:34:58 -0600498
Tim Van Patten5f744f12018-12-12 11:46:21 -0700499 // Update the current status of whether we should use ANGLE or not
500 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600501}
502
Victor Khimenko4819b522018-07-13 17:24:18 +0200503void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600504 if (mLayerPaths.empty()) {
505 mLayerPaths = layerPaths;
506 mAppNamespace = appNamespace;
507 } else {
508 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800509 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600510 }
511}
512
Victor Khimenko4819b522018-07-13 17:24:18 +0200513NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600514 return mAppNamespace;
515}
516
Tim Van Patten5f744f12018-12-12 11:46:21 -0700517std::string& GraphicsEnv::getAngleAppName() {
518 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600519}
520
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600521const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600522 return mLayerPaths;
523}
524
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600525const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600526 return mDebugLayers;
527}
528
Cody Northropb9b01b62018-10-23 13:13:10 -0600529const std::string& GraphicsEnv::getDebugLayersGLES() {
530 return mDebugLayersGLES;
531}
532
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600533void GraphicsEnv::setDebugLayers(const std::string layers) {
534 mDebugLayers = layers;
535}
536
Cody Northropb9b01b62018-10-23 13:13:10 -0600537void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
538 mDebugLayersGLES = layers;
539}
540
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700541// Return true if all the required libraries from vndk and sphal namespace are
542// linked to the Game Driver namespace correctly.
543bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* vndkNamespace) {
544 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
545 if (llndkLibraries.empty()) {
546 return false;
547 }
548 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
549 ALOGE("Failed to link default namespace[%s]", dlerror());
550 return false;
551 }
552
553 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
554 if (vndkspLibraries.empty()) {
555 return false;
556 }
557 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
558 ALOGE("Failed to link vndk namespace[%s]", dlerror());
559 return false;
560 }
561
562 if (mSphalLibraries.empty()) {
563 return true;
564 }
565
566 // Make additional libraries in sphal to be accessible
567 auto sphalNamespace = android_get_exported_namespace("sphal");
568 if (!sphalNamespace) {
569 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
570 mSphalLibraries.c_str());
571 return false;
572 }
573
574 if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) {
575 ALOGE("Failed to link sphal namespace[%s]", dlerror());
576 return false;
577 }
578
579 return true;
580}
581
Jesse Hall53457db2016-12-14 16:54:06 -0800582android_namespace_t* GraphicsEnv::getDriverNamespace() {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700583 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Yiwei Zhang64d89212018-11-27 19:58:29 -0800584
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700585 if (mDriverNamespace) {
586 return mDriverNamespace;
587 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800588
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700589 if (mDriverPath.empty()) {
590 return nullptr;
591 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800592
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700593 auto vndkNamespace = android_get_exported_namespace("vndk");
594 if (!vndkNamespace) {
595 return nullptr;
596 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800597
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700598 mDriverNamespace = android_create_namespace("gfx driver",
599 mDriverPath.c_str(), // ld_library_path
600 mDriverPath.c_str(), // default_library_path
601 ANDROID_NAMESPACE_TYPE_ISOLATED,
602 nullptr, // permitted_when_isolated_path
603 nullptr);
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800604
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700605 if (!linkDriverNamespaceLocked(vndkNamespace)) {
606 mDriverNamespace = nullptr;
607 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800608
Jesse Hall53457db2016-12-14 16:54:06 -0800609 return mDriverNamespace;
610}
611
Cody Northrop1f00e172018-04-02 11:23:31 -0600612android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700613 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600614
Cody Northrop3892cfa2019-01-30 10:03:12 -0700615 if (mAngleNamespace) {
616 return mAngleNamespace;
617 }
618
619 if (mAnglePath.empty()) {
620 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
621 return nullptr;
622 }
623
624 mAngleNamespace = android_create_namespace("ANGLE",
625 nullptr, // ld_library_path
626 mAnglePath.c_str(), // default_library_path
627 ANDROID_NAMESPACE_TYPE_SHARED |
628 ANDROID_NAMESPACE_TYPE_ISOLATED,
629 nullptr, // permitted_when_isolated_path
630 nullptr);
631
632 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600633
634 return mAngleNamespace;
635}
636
Jesse Hall90b25ed2016-12-12 12:56:46 -0800637} // namespace android