blob: 705a0a4b03c53865fb0fd0f9bb3512aee1e647c7 [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: {
Yiwei Zhang472cab02019-08-05 17:57:41 -0700216 if (mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::NONE ||
217 mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::GL) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800218 mGpuStats.glDriverToLoad = driver;
219 break;
220 }
221
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700222 if (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800223 mGpuStats.glDriverFallback = driver;
224 }
225 break;
226 }
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700227 case GpuStatsInfo::Driver::VULKAN:
228 case GpuStatsInfo::Driver::VULKAN_UPDATED: {
Yiwei Zhang472cab02019-08-05 17:57:41 -0700229 if (mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::NONE ||
230 mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::VULKAN) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800231 mGpuStats.vkDriverToLoad = driver;
232 break;
233 }
234
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700235 if (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800236 mGpuStats.vkDriverFallback = driver;
237 }
238 break;
239 }
240 default:
241 break;
242 }
243}
244
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700245void GraphicsEnv::setDriverLoaded(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700246 int64_t driverLoadingTime) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800247 ATRACE_CALL();
248
249 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700250 const bool doNotSend = mGpuStats.appPackageName.empty();
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700251 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700252 if (doNotSend) mGpuStats.glDriverToSend = true;
253 mGpuStats.glDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800254 } else {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700255 if (doNotSend) mGpuStats.vkDriverToSend = true;
256 mGpuStats.vkDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800257 }
258
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700259 sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800260}
261
Yiwei Zhangd9861812019-02-13 11:51:55 -0800262static sp<IGpuService> getGpuService() {
Yiwei Zhang8e097302019-07-08 16:11:12 -0700263 static const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
Yiwei Zhangd9861812019-02-13 11:51:55 -0800264 if (!binder) {
265 ALOGE("Failed to get gpu service");
266 return nullptr;
267 }
268
269 return interface_cast<IGpuService>(binder);
270}
271
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700272void GraphicsEnv::setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value) {
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700273 ATRACE_CALL();
274
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700275 std::lock_guard<std::mutex> lock(mStatsLock);
276 const sp<IGpuService> gpuService = getGpuService();
277 if (gpuService) {
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700278 gpuService->setTargetStats(mGpuStats.appPackageName, mGpuStats.driverVersionCode, stats,
279 value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700280 }
281}
282
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700283void GraphicsEnv::sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800284 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800285 ATRACE_CALL();
286
287 // Do not sendGpuStats for those skipping the GraphicsEnvironment setup
288 if (mGpuStats.appPackageName.empty()) return;
289
290 ALOGV("sendGpuStats:\n"
291 "\tdriverPackageName[%s]\n"
292 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800293 "\tdriverVersionCode[%" PRIu64 "]\n"
294 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800295 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700296 "\tvulkanVersion[%d]\n"
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700297 "\tapi[%d]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800298 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800299 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800300 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-19 16:00:25 -0800301 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700302 mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime);
303
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700304 GpuStatsInfo::Driver driver = GpuStatsInfo::Driver::NONE;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700305 bool isIntendedDriverLoaded = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700306 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700307 driver = mGpuStats.glDriverToLoad;
308 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700309 isDriverLoaded && (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700310 } else {
311 driver = mGpuStats.vkDriverToLoad;
312 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700313 isDriverLoaded && (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700314 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800315
Yiwei Zhangd9861812019-02-13 11:51:55 -0800316 const sp<IGpuService> gpuService = getGpuService();
317 if (gpuService) {
318 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800319 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700320 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700321 isIntendedDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800322 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800323}
324
Tim Van Patten5f744f12018-12-12 11:46:21 -0700325void* GraphicsEnv::loadLibrary(std::string name) {
326 const android_dlextinfo dlextinfo = {
327 .flags = ANDROID_DLEXT_USE_NAMESPACE,
328 .library_namespace = getAngleNamespace(),
329 };
330
331 std::string libName = std::string("lib") + name + "_angle.so";
332
333 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
334
335 if (so) {
336 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
337 return so;
338 } else {
339 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
340 }
341
342 return nullptr;
343}
344
345bool GraphicsEnv::checkAngleRules(void* so) {
346 char manufacturer[PROPERTY_VALUE_MAX];
347 char model[PROPERTY_VALUE_MAX];
348 property_get("ro.product.manufacturer", manufacturer, "UNSET");
349 property_get("ro.product.model", model, "UNSET");
350
351 auto ANGLEGetFeatureSupportUtilAPIVersion =
352 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
353 "ANGLEGetFeatureSupportUtilAPIVersion");
354
355 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
356 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
357 return false;
358 }
359
360 // Negotiate the interface version by requesting most recent known to the platform
361 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
362 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
363 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
364 "requested version %u",
365 versionToUse);
366 return false;
367 }
368
369 // Add and remove versions below as needed
370 bool useAngle = false;
371 switch (versionToUse) {
372 case 2: {
373 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
374 void* rulesHandle = nullptr;
375 int rulesVersion = 0;
376 void* systemInfoHandle = nullptr;
377
378 // Get the symbols for the feature-support-utility library:
379#define GET_SYMBOL(symbol) \
380 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
381 if (!symbol) { \
382 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
383 break; \
384 }
385 GET_SYMBOL(ANGLEAndroidParseRulesString);
386 GET_SYMBOL(ANGLEGetSystemInfo);
387 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
388 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
389 GET_SYMBOL(ANGLEFreeRulesHandle);
390 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
391
392 // Parse the rules, obtain the SystemInfo, and evaluate the
393 // application against the rules:
394 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
395 ALOGW("ANGLE feature-support library cannot parse rules file");
396 break;
397 }
398 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
399 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
400 break;
401 }
402 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) {
403 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
404 break;
405 }
406 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
407 systemInfoHandle, mAngleAppName.c_str());
408 (ANGLEFreeRulesHandle)(rulesHandle);
409 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
410 } break;
411
412 default:
413 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
414 }
415
416 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
417 return useAngle;
418}
419
420bool GraphicsEnv::shouldUseAngle(std::string appName) {
421 if (appName != mAngleAppName) {
422 // Make sure we are checking the app we were init'ed for
423 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
424 appName.c_str());
425 return false;
426 }
427
428 return shouldUseAngle();
429}
430
431bool GraphicsEnv::shouldUseAngle() {
432 // Make sure we are init'ed
433 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700434 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700435 return false;
436 }
437
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700438 return (mUseAngle == YES) ? true : false;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700439}
440
441void GraphicsEnv::updateUseAngle() {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700442 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700443
444 const char* ANGLE_PREFER_ANGLE = "angle";
445 const char* ANGLE_PREFER_NATIVE = "native";
446
447 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
448 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700449 mUseAngle = YES;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700450 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
451 ALOGV("User set \"Developer Options\" to force the use of Native");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700452 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700453 } else {
454 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
455 // load ANGLE and call the updatable opt-in/out logic:
Cody Northropc15d3822019-01-17 10:26:47 -0700456 void* featureSo = loadLibrary("feature_support");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700457 if (featureSo) {
458 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700459 mUseAngle = checkAngleRules(featureSo) ? YES : NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700460 dlclose(featureSo);
461 featureSo = nullptr;
462 } else {
463 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
464 }
465 }
466}
467
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600468void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700469 const std::string developerOptIn, const int rulesFd,
470 const long rulesOffset, const long rulesLength) {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700471 if (mUseAngle != UNKNOWN) {
472 // We've already figured out an answer for this app, so just return.
473 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
474 (mUseAngle == YES) ? "true" : "false");
475 return;
476 }
477
Tim Van Patten5f744f12018-12-12 11:46:21 -0700478 ALOGV("setting ANGLE path to '%s'", path.c_str());
479 mAnglePath = path;
480 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
481 mAngleAppName = appName;
482 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
483 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600484
Tim Van Patten5f744f12018-12-12 11:46:21 -0700485 lseek(rulesFd, rulesOffset, SEEK_SET);
486 mRulesBuffer = std::vector<char>(rulesLength + 1);
487 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
488 if (numBytesRead < 0) {
489 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
490 numBytesRead = 0;
491 } else if (numBytesRead == 0) {
492 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600493 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700494 if (numBytesRead != rulesLength) {
495 ALOGW("Did not read all of the necessary bytes from the rules file."
496 "expected: %ld, got: %zd",
497 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700498 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700499 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 10:34:58 -0600500
Tim Van Patten5f744f12018-12-12 11:46:21 -0700501 // Update the current status of whether we should use ANGLE or not
502 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600503}
504
Victor Khimenko4819b522018-07-13 17:24:18 +0200505void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600506 if (mLayerPaths.empty()) {
507 mLayerPaths = layerPaths;
508 mAppNamespace = appNamespace;
509 } else {
510 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800511 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600512 }
513}
514
Victor Khimenko4819b522018-07-13 17:24:18 +0200515NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600516 return mAppNamespace;
517}
518
Tim Van Patten5f744f12018-12-12 11:46:21 -0700519std::string& GraphicsEnv::getAngleAppName() {
520 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600521}
522
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600523const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600524 return mLayerPaths;
525}
526
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600527const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600528 return mDebugLayers;
529}
530
Cody Northropb9b01b62018-10-23 13:13:10 -0600531const std::string& GraphicsEnv::getDebugLayersGLES() {
532 return mDebugLayersGLES;
533}
534
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600535void GraphicsEnv::setDebugLayers(const std::string layers) {
536 mDebugLayers = layers;
537}
538
Cody Northropb9b01b62018-10-23 13:13:10 -0600539void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
540 mDebugLayersGLES = layers;
541}
542
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700543// Return true if all the required libraries from vndk and sphal namespace are
544// linked to the Game Driver namespace correctly.
545bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* vndkNamespace) {
546 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
547 if (llndkLibraries.empty()) {
548 return false;
549 }
550 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
551 ALOGE("Failed to link default namespace[%s]", dlerror());
552 return false;
553 }
554
555 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
556 if (vndkspLibraries.empty()) {
557 return false;
558 }
559 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
560 ALOGE("Failed to link vndk namespace[%s]", dlerror());
561 return false;
562 }
563
564 if (mSphalLibraries.empty()) {
565 return true;
566 }
567
568 // Make additional libraries in sphal to be accessible
569 auto sphalNamespace = android_get_exported_namespace("sphal");
570 if (!sphalNamespace) {
571 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
572 mSphalLibraries.c_str());
573 return false;
574 }
575
576 if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) {
577 ALOGE("Failed to link sphal namespace[%s]", dlerror());
578 return false;
579 }
580
581 return true;
582}
583
Jesse Hall53457db2016-12-14 16:54:06 -0800584android_namespace_t* GraphicsEnv::getDriverNamespace() {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700585 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Yiwei Zhang64d89212018-11-27 19:58:29 -0800586
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700587 if (mDriverNamespace) {
588 return mDriverNamespace;
589 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800590
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700591 if (mDriverPath.empty()) {
592 return nullptr;
593 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800594
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700595 auto vndkNamespace = android_get_exported_namespace("vndk");
596 if (!vndkNamespace) {
597 return nullptr;
598 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800599
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700600 mDriverNamespace = android_create_namespace("gfx driver",
601 mDriverPath.c_str(), // ld_library_path
602 mDriverPath.c_str(), // default_library_path
603 ANDROID_NAMESPACE_TYPE_ISOLATED,
604 nullptr, // permitted_when_isolated_path
605 nullptr);
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800606
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700607 if (!linkDriverNamespaceLocked(vndkNamespace)) {
608 mDriverNamespace = nullptr;
609 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800610
Jesse Hall53457db2016-12-14 16:54:06 -0800611 return mDriverNamespace;
612}
613
Cody Northrop1f00e172018-04-02 11:23:31 -0600614android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700615 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600616
Cody Northrop3892cfa2019-01-30 10:03:12 -0700617 if (mAngleNamespace) {
618 return mAngleNamespace;
619 }
620
621 if (mAnglePath.empty()) {
622 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
623 return nullptr;
624 }
625
626 mAngleNamespace = android_create_namespace("ANGLE",
627 nullptr, // ld_library_path
628 mAnglePath.c_str(), // default_library_path
629 ANDROID_NAMESPACE_TYPE_SHARED |
630 ANDROID_NAMESPACE_TYPE_ISOLATED,
631 nullptr, // permitted_when_isolated_path
632 nullptr);
633
634 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600635
636 return mAngleNamespace;
637}
638
Jesse Hall90b25ed2016-12-12 12:56:46 -0800639} // namespace android