blob: 6e59cc50403c7db5066756ffe80801ff23cf489a [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
Yiwei Zhangfe622712019-11-08 11:55:36 -0800142bool GraphicsEnv::isDebuggable() {
143 return prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) > 0;
Cody Northrop629ce4e2018-10-15 07:22:09 -0600144}
145
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800146void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string path,
147 const std::string sphalLibraries) {
148 if (!mDriverPath.empty() || !mSphalLibraries.empty()) {
149 ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries "
150 "from '%s' to '%s'",
151 mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800152 return;
153 }
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800154 ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(),
155 sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800156 mDriverPath = path;
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800157 mSphalLibraries = sphalLibraries;
Jesse Hall90b25ed2016-12-12 12:56:46 -0800158}
159
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700160void GraphicsEnv::hintActivityLaunch() {
161 ATRACE_CALL();
162
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700163 std::thread trySendGpuStatsThread([this]() {
164 // If there's already graphics driver preloaded in the process, just send
165 // the stats info to GpuStats directly through async binder.
166 std::lock_guard<std::mutex> lock(mStatsLock);
167 if (mGpuStats.glDriverToSend) {
168 mGpuStats.glDriverToSend = false;
169 sendGpuStatsLocked(GraphicsEnv::Api::API_GL, true, mGpuStats.glDriverLoadingTime);
170 }
171 if (mGpuStats.vkDriverToSend) {
172 mGpuStats.vkDriverToSend = false;
173 sendGpuStatsLocked(GraphicsEnv::Api::API_VK, true, mGpuStats.vkDriverLoadingTime);
174 }
175 });
176 trySendGpuStatsThread.detach();
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700177}
178
Greg Kaiser210bb7e2019-02-12 12:40:05 -0800179void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800180 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700181 int64_t driverBuildTime, const std::string& appPackageName,
182 const int vulkanVersion) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800183 ATRACE_CALL();
184
Yiwei Zhangd9861812019-02-13 11:51:55 -0800185 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800186 ALOGV("setGpuStats:\n"
187 "\tdriverPackageName[%s]\n"
188 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800189 "\tdriverVersionCode[%" PRIu64 "]\n"
190 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700191 "\tappPackageName[%s]\n"
192 "\tvulkanVersion[%d]\n",
Yiwei Zhang96c01712019-02-19 16:00:25 -0800193 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700194 appPackageName.c_str(), vulkanVersion);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800195
Yiwei Zhangd9861812019-02-13 11:51:55 -0800196 mGpuStats.driverPackageName = driverPackageName;
197 mGpuStats.driverVersionName = driverVersionName;
198 mGpuStats.driverVersionCode = driverVersionCode;
Yiwei Zhang96c01712019-02-19 16:00:25 -0800199 mGpuStats.driverBuildTime = driverBuildTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800200 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang794d2952019-05-06 17:43:59 -0700201 mGpuStats.vulkanVersion = vulkanVersion;
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800202}
203
Yiwei Zhangd9861812019-02-13 11:51:55 -0800204void GraphicsEnv::setDriverToLoad(GraphicsEnv::Driver driver) {
205 ATRACE_CALL();
206
207 std::lock_guard<std::mutex> lock(mStatsLock);
208 switch (driver) {
209 case GraphicsEnv::Driver::GL:
210 case GraphicsEnv::Driver::GL_UPDATED:
211 case GraphicsEnv::Driver::ANGLE: {
Yiwei Zhang8238fa02019-08-05 17:57:41 -0700212 if (mGpuStats.glDriverToLoad == GraphicsEnv::Driver::NONE ||
213 mGpuStats.glDriverToLoad == GraphicsEnv::Driver::GL) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800214 mGpuStats.glDriverToLoad = driver;
215 break;
216 }
217
218 if (mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE) {
219 mGpuStats.glDriverFallback = driver;
220 }
221 break;
222 }
223 case Driver::VULKAN:
224 case Driver::VULKAN_UPDATED: {
Yiwei Zhang8238fa02019-08-05 17:57:41 -0700225 if (mGpuStats.vkDriverToLoad == GraphicsEnv::Driver::NONE ||
226 mGpuStats.vkDriverToLoad == GraphicsEnv::Driver::VULKAN) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800227 mGpuStats.vkDriverToLoad = driver;
228 break;
229 }
230
231 if (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE) {
232 mGpuStats.vkDriverFallback = driver;
233 }
234 break;
235 }
236 default:
237 break;
238 }
239}
240
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700241void GraphicsEnv::setDriverLoaded(GraphicsEnv::Api api, bool isDriverLoaded,
242 int64_t driverLoadingTime) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800243 ATRACE_CALL();
244
245 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700246 const bool doNotSend = mGpuStats.appPackageName.empty();
Yiwei Zhangd9861812019-02-13 11:51:55 -0800247 if (api == GraphicsEnv::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700248 if (doNotSend) mGpuStats.glDriverToSend = true;
249 mGpuStats.glDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800250 } else {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700251 if (doNotSend) mGpuStats.vkDriverToSend = true;
252 mGpuStats.vkDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800253 }
254
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700255 sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800256}
257
Yiwei Zhangd9861812019-02-13 11:51:55 -0800258static sp<IGpuService> getGpuService() {
259 const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
260 if (!binder) {
261 ALOGE("Failed to get gpu service");
262 return nullptr;
263 }
264
265 return interface_cast<IGpuService>(binder);
266}
267
Yiwei Zhangb1c1a372019-07-03 13:39:32 -0700268void GraphicsEnv::setTargetStats(const Stats stats, const uint64_t value) {
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700269 ATRACE_CALL();
270
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700271 std::lock_guard<std::mutex> lock(mStatsLock);
272 const sp<IGpuService> gpuService = getGpuService();
273 if (gpuService) {
Yiwei Zhangb1c1a372019-07-03 13:39:32 -0700274 gpuService->setTargetStats(mGpuStats.appPackageName, mGpuStats.driverVersionCode, stats,
275 value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700276 }
277}
278
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700279void GraphicsEnv::sendGpuStatsLocked(GraphicsEnv::Api api, bool isDriverLoaded,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800280 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800281 ATRACE_CALL();
282
283 // Do not sendGpuStats for those skipping the GraphicsEnvironment setup
284 if (mGpuStats.appPackageName.empty()) return;
285
286 ALOGV("sendGpuStats:\n"
287 "\tdriverPackageName[%s]\n"
288 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800289 "\tdriverVersionCode[%" PRIu64 "]\n"
290 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800291 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700292 "\tvulkanVersion[%d]\n"
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700293 "\tapi[%d]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800294 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800295 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800296 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-19 16:00:25 -0800297 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700298 mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime);
299
300 GraphicsEnv::Driver driver = GraphicsEnv::Driver::NONE;
301 bool isIntendedDriverLoaded = false;
302 if (api == GraphicsEnv::Api::API_GL) {
303 driver = mGpuStats.glDriverToLoad;
304 isIntendedDriverLoaded =
305 isDriverLoaded && (mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE);
306 } else {
307 driver = mGpuStats.vkDriverToLoad;
308 isIntendedDriverLoaded =
309 isDriverLoaded && (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE);
310 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800311
Yiwei Zhangd9861812019-02-13 11:51:55 -0800312 const sp<IGpuService> gpuService = getGpuService();
313 if (gpuService) {
314 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800315 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700316 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700317 isIntendedDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800318 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800319}
320
Tim Van Patten5f744f12018-12-12 11:46:21 -0700321void* GraphicsEnv::loadLibrary(std::string name) {
322 const android_dlextinfo dlextinfo = {
323 .flags = ANDROID_DLEXT_USE_NAMESPACE,
324 .library_namespace = getAngleNamespace(),
325 };
326
327 std::string libName = std::string("lib") + name + "_angle.so";
328
329 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
330
331 if (so) {
332 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
333 return so;
334 } else {
335 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
336 }
337
338 return nullptr;
339}
340
341bool GraphicsEnv::checkAngleRules(void* so) {
342 char manufacturer[PROPERTY_VALUE_MAX];
343 char model[PROPERTY_VALUE_MAX];
344 property_get("ro.product.manufacturer", manufacturer, "UNSET");
345 property_get("ro.product.model", model, "UNSET");
346
347 auto ANGLEGetFeatureSupportUtilAPIVersion =
348 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
349 "ANGLEGetFeatureSupportUtilAPIVersion");
350
351 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
352 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
353 return false;
354 }
355
356 // Negotiate the interface version by requesting most recent known to the platform
357 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
358 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
359 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
360 "requested version %u",
361 versionToUse);
362 return false;
363 }
364
365 // Add and remove versions below as needed
366 bool useAngle = false;
367 switch (versionToUse) {
368 case 2: {
369 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
370 void* rulesHandle = nullptr;
371 int rulesVersion = 0;
372 void* systemInfoHandle = nullptr;
373
374 // Get the symbols for the feature-support-utility library:
375#define GET_SYMBOL(symbol) \
376 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
377 if (!symbol) { \
378 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
379 break; \
380 }
381 GET_SYMBOL(ANGLEAndroidParseRulesString);
382 GET_SYMBOL(ANGLEGetSystemInfo);
383 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
384 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
385 GET_SYMBOL(ANGLEFreeRulesHandle);
386 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
387
388 // Parse the rules, obtain the SystemInfo, and evaluate the
389 // application against the rules:
390 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
391 ALOGW("ANGLE feature-support library cannot parse rules file");
392 break;
393 }
394 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
395 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
396 break;
397 }
398 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) {
399 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
400 break;
401 }
402 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
403 systemInfoHandle, mAngleAppName.c_str());
404 (ANGLEFreeRulesHandle)(rulesHandle);
405 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
406 } break;
407
408 default:
409 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
410 }
411
412 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
413 return useAngle;
414}
415
416bool GraphicsEnv::shouldUseAngle(std::string appName) {
417 if (appName != mAngleAppName) {
418 // Make sure we are checking the app we were init'ed for
419 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
420 appName.c_str());
421 return false;
422 }
423
424 return shouldUseAngle();
425}
426
427bool GraphicsEnv::shouldUseAngle() {
428 // Make sure we are init'ed
429 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700430 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700431 return false;
432 }
433
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700434 return (mUseAngle == YES) ? true : false;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700435}
436
437void GraphicsEnv::updateUseAngle() {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700438 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700439
440 const char* ANGLE_PREFER_ANGLE = "angle";
441 const char* ANGLE_PREFER_NATIVE = "native";
442
443 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
444 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700445 mUseAngle = YES;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700446 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
447 ALOGV("User set \"Developer Options\" to force the use of Native");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700448 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700449 } else {
450 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
451 // load ANGLE and call the updatable opt-in/out logic:
Cody Northropc15d3822019-01-17 10:26:47 -0700452 void* featureSo = loadLibrary("feature_support");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700453 if (featureSo) {
454 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700455 mUseAngle = checkAngleRules(featureSo) ? YES : NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700456 dlclose(featureSo);
457 featureSo = nullptr;
458 } else {
459 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
460 }
461 }
462}
463
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600464void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700465 const std::string developerOptIn, const int rulesFd,
466 const long rulesOffset, const long rulesLength) {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700467 if (mUseAngle != UNKNOWN) {
468 // We've already figured out an answer for this app, so just return.
469 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
470 (mUseAngle == YES) ? "true" : "false");
471 return;
472 }
473
Tim Van Patten5f744f12018-12-12 11:46:21 -0700474 ALOGV("setting ANGLE path to '%s'", path.c_str());
475 mAnglePath = path;
476 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
477 mAngleAppName = appName;
478 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
479 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600480
Tim Van Patten5f744f12018-12-12 11:46:21 -0700481 lseek(rulesFd, rulesOffset, SEEK_SET);
482 mRulesBuffer = std::vector<char>(rulesLength + 1);
483 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
484 if (numBytesRead < 0) {
485 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
486 numBytesRead = 0;
487 } else if (numBytesRead == 0) {
488 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600489 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700490 if (numBytesRead != rulesLength) {
491 ALOGW("Did not read all of the necessary bytes from the rules file."
492 "expected: %ld, got: %zd",
493 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700494 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700495 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 10:34:58 -0600496
Tim Van Patten5f744f12018-12-12 11:46:21 -0700497 // Update the current status of whether we should use ANGLE or not
498 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600499}
500
Victor Khimenko4819b522018-07-13 17:24:18 +0200501void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600502 if (mLayerPaths.empty()) {
503 mLayerPaths = layerPaths;
504 mAppNamespace = appNamespace;
505 } else {
506 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800507 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600508 }
509}
510
Victor Khimenko4819b522018-07-13 17:24:18 +0200511NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600512 return mAppNamespace;
513}
514
Tim Van Patten5f744f12018-12-12 11:46:21 -0700515std::string& GraphicsEnv::getAngleAppName() {
516 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600517}
518
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600519const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600520 return mLayerPaths;
521}
522
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600523const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600524 return mDebugLayers;
525}
526
Cody Northropb9b01b62018-10-23 13:13:10 -0600527const std::string& GraphicsEnv::getDebugLayersGLES() {
528 return mDebugLayersGLES;
529}
530
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600531void GraphicsEnv::setDebugLayers(const std::string layers) {
532 mDebugLayers = layers;
533}
534
Cody Northropb9b01b62018-10-23 13:13:10 -0600535void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
536 mDebugLayersGLES = layers;
537}
538
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700539// Return true if all the required libraries from vndk and sphal namespace are
540// linked to the Game Driver namespace correctly.
541bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* vndkNamespace) {
542 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
543 if (llndkLibraries.empty()) {
544 return false;
545 }
546 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
547 ALOGE("Failed to link default namespace[%s]", dlerror());
548 return false;
549 }
550
551 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
552 if (vndkspLibraries.empty()) {
553 return false;
554 }
555 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
556 ALOGE("Failed to link vndk namespace[%s]", dlerror());
557 return false;
558 }
559
560 if (mSphalLibraries.empty()) {
561 return true;
562 }
563
564 // Make additional libraries in sphal to be accessible
565 auto sphalNamespace = android_get_exported_namespace("sphal");
566 if (!sphalNamespace) {
567 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
568 mSphalLibraries.c_str());
569 return false;
570 }
571
572 if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) {
573 ALOGE("Failed to link sphal namespace[%s]", dlerror());
574 return false;
575 }
576
577 return true;
578}
579
Jesse Hall53457db2016-12-14 16:54:06 -0800580android_namespace_t* GraphicsEnv::getDriverNamespace() {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700581 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Yiwei Zhang64d89212018-11-27 19:58:29 -0800582
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700583 if (mDriverNamespace) {
584 return mDriverNamespace;
585 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800586
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700587 if (mDriverPath.empty()) {
588 return nullptr;
589 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800590
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700591 auto vndkNamespace = android_get_exported_namespace("vndk");
592 if (!vndkNamespace) {
593 return nullptr;
594 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800595
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700596 mDriverNamespace = android_create_namespace("gfx driver",
597 mDriverPath.c_str(), // ld_library_path
598 mDriverPath.c_str(), // default_library_path
599 ANDROID_NAMESPACE_TYPE_ISOLATED,
600 nullptr, // permitted_when_isolated_path
601 nullptr);
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800602
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700603 if (!linkDriverNamespaceLocked(vndkNamespace)) {
604 mDriverNamespace = nullptr;
605 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800606
Jesse Hall53457db2016-12-14 16:54:06 -0800607 return mDriverNamespace;
608}
609
Cody Northrop1f00e172018-04-02 11:23:31 -0600610android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700611 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600612
Cody Northrop3892cfa2019-01-30 10:03:12 -0700613 if (mAngleNamespace) {
614 return mAngleNamespace;
615 }
616
617 if (mAnglePath.empty()) {
618 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
619 return nullptr;
620 }
621
622 mAngleNamespace = android_create_namespace("ANGLE",
623 nullptr, // ld_library_path
624 mAnglePath.c_str(), // default_library_path
625 ANDROID_NAMESPACE_TYPE_SHARED |
626 ANDROID_NAMESPACE_TYPE_ISOLATED,
627 nullptr, // permitted_when_isolated_path
628 nullptr);
629
630 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600631
632 return mAngleNamespace;
633}
634
Jesse Hall90b25ed2016-12-12 12:56:46 -0800635} // namespace android