blob: 5d51d2b253c7b6548cb441810a13d17960f6cbec [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
Jooyung Han78396802020-02-23 03:02:43 +090079static constexpr const char* kNativeLibrariesSystemConfigPath[] =
80 {"/apex/com.android.vndk.v{}/etc/llndk.libraries.{}.txt",
81 "/apex/com.android.vndk.v{}/etc/vndksp.libraries.{}.txt"};
Yiwei Zhang64d89212018-11-27 19:58:29 -080082
83static std::string vndkVersionStr() {
84#ifdef __BIONIC__
Jooyung Han78396802020-02-23 03:02:43 +090085 return android::base::GetProperty("ro.vndk.version", "");
Yiwei Zhang64d89212018-11-27 19:58:29 -080086#endif
87 return "";
88}
89
90static void insertVndkVersionStr(std::string* fileName) {
91 LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr");
Jooyung Han78396802020-02-23 03:02:43 +090092 std::string version = vndkVersionStr();
93 size_t pos = fileName->find("{}");
94 while (pos != std::string::npos) {
95 fileName->replace(pos, 2, version);
96 pos = fileName->find("{}", pos + version.size());
Yiwei Zhang64d89212018-11-27 19:58:29 -080097 }
Yiwei Zhang64d89212018-11-27 19:58:29 -080098}
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) {
Jooyung Han78396802020-02-23 03:02:43 +0900120 std::string nativeLibrariesSystemConfig = kNativeLibrariesSystemConfigPath[type];
Yiwei Zhang64d89212018-11-27 19:58:29 -0800121 insertVndkVersionStr(&nativeLibrariesSystemConfig);
122
123 std::vector<std::string> soNames;
124 if (!readConfig(nativeLibrariesSystemConfig, &soNames)) {
125 ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str());
126 return "";
127 }
128
129 return base::Join(soNames, ':');
130}
131
Jesse Hall90b25ed2016-12-12 12:56:46 -0800132/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
133 static GraphicsEnv env;
134 return env;
135}
136
Yiwei Zhangfe622712019-11-08 11:55:36 -0800137bool GraphicsEnv::isDebuggable() {
138 return prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) > 0;
Cody Northrop629ce4e2018-10-15 07:22:09 -0600139}
140
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800141void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string path,
142 const std::string sphalLibraries) {
143 if (!mDriverPath.empty() || !mSphalLibraries.empty()) {
144 ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries "
145 "from '%s' to '%s'",
146 mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800147 return;
148 }
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800149 ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(),
150 sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800151 mDriverPath = path;
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800152 mSphalLibraries = sphalLibraries;
Jesse Hall90b25ed2016-12-12 12:56:46 -0800153}
154
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700155void GraphicsEnv::hintActivityLaunch() {
156 ATRACE_CALL();
157
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700158 std::thread trySendGpuStatsThread([this]() {
159 // If there's already graphics driver preloaded in the process, just send
160 // the stats info to GpuStats directly through async binder.
161 std::lock_guard<std::mutex> lock(mStatsLock);
162 if (mGpuStats.glDriverToSend) {
163 mGpuStats.glDriverToSend = false;
164 sendGpuStatsLocked(GraphicsEnv::Api::API_GL, true, mGpuStats.glDriverLoadingTime);
165 }
166 if (mGpuStats.vkDriverToSend) {
167 mGpuStats.vkDriverToSend = false;
168 sendGpuStatsLocked(GraphicsEnv::Api::API_VK, true, mGpuStats.vkDriverLoadingTime);
169 }
170 });
171 trySendGpuStatsThread.detach();
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700172}
173
Greg Kaiser210bb7e2019-02-12 12:40:05 -0800174void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800175 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700176 int64_t driverBuildTime, const std::string& appPackageName,
177 const int vulkanVersion) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800178 ATRACE_CALL();
179
Yiwei Zhangd9861812019-02-13 11:51:55 -0800180 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800181 ALOGV("setGpuStats:\n"
182 "\tdriverPackageName[%s]\n"
183 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800184 "\tdriverVersionCode[%" PRIu64 "]\n"
185 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700186 "\tappPackageName[%s]\n"
187 "\tvulkanVersion[%d]\n",
Yiwei Zhang96c01712019-02-19 16:00:25 -0800188 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700189 appPackageName.c_str(), vulkanVersion);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800190
Yiwei Zhangd9861812019-02-13 11:51:55 -0800191 mGpuStats.driverPackageName = driverPackageName;
192 mGpuStats.driverVersionName = driverVersionName;
193 mGpuStats.driverVersionCode = driverVersionCode;
Yiwei Zhang96c01712019-02-19 16:00:25 -0800194 mGpuStats.driverBuildTime = driverBuildTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800195 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang794d2952019-05-06 17:43:59 -0700196 mGpuStats.vulkanVersion = vulkanVersion;
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800197}
198
Yiwei Zhangd9861812019-02-13 11:51:55 -0800199void GraphicsEnv::setDriverToLoad(GraphicsEnv::Driver driver) {
200 ATRACE_CALL();
201
202 std::lock_guard<std::mutex> lock(mStatsLock);
203 switch (driver) {
204 case GraphicsEnv::Driver::GL:
205 case GraphicsEnv::Driver::GL_UPDATED:
206 case GraphicsEnv::Driver::ANGLE: {
Yiwei Zhang8238fa02019-08-05 17:57:41 -0700207 if (mGpuStats.glDriverToLoad == GraphicsEnv::Driver::NONE ||
208 mGpuStats.glDriverToLoad == GraphicsEnv::Driver::GL) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800209 mGpuStats.glDriverToLoad = driver;
210 break;
211 }
212
213 if (mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE) {
214 mGpuStats.glDriverFallback = driver;
215 }
216 break;
217 }
218 case Driver::VULKAN:
219 case Driver::VULKAN_UPDATED: {
Yiwei Zhang8238fa02019-08-05 17:57:41 -0700220 if (mGpuStats.vkDriverToLoad == GraphicsEnv::Driver::NONE ||
221 mGpuStats.vkDriverToLoad == GraphicsEnv::Driver::VULKAN) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800222 mGpuStats.vkDriverToLoad = driver;
223 break;
224 }
225
226 if (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE) {
227 mGpuStats.vkDriverFallback = driver;
228 }
229 break;
230 }
231 default:
232 break;
233 }
234}
235
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700236void GraphicsEnv::setDriverLoaded(GraphicsEnv::Api api, bool isDriverLoaded,
237 int64_t driverLoadingTime) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800238 ATRACE_CALL();
239
240 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700241 const bool doNotSend = mGpuStats.appPackageName.empty();
Yiwei Zhangd9861812019-02-13 11:51:55 -0800242 if (api == GraphicsEnv::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700243 if (doNotSend) mGpuStats.glDriverToSend = true;
244 mGpuStats.glDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800245 } else {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700246 if (doNotSend) mGpuStats.vkDriverToSend = true;
247 mGpuStats.vkDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800248 }
249
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700250 sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800251}
252
Yiwei Zhangd9861812019-02-13 11:51:55 -0800253static sp<IGpuService> getGpuService() {
254 const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
255 if (!binder) {
256 ALOGE("Failed to get gpu service");
257 return nullptr;
258 }
259
260 return interface_cast<IGpuService>(binder);
261}
262
Yiwei Zhangb1c1a372019-07-03 13:39:32 -0700263void GraphicsEnv::setTargetStats(const Stats stats, const uint64_t value) {
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700264 ATRACE_CALL();
265
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700266 std::lock_guard<std::mutex> lock(mStatsLock);
267 const sp<IGpuService> gpuService = getGpuService();
268 if (gpuService) {
Yiwei Zhangb1c1a372019-07-03 13:39:32 -0700269 gpuService->setTargetStats(mGpuStats.appPackageName, mGpuStats.driverVersionCode, stats,
270 value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700271 }
272}
273
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700274void GraphicsEnv::sendGpuStatsLocked(GraphicsEnv::Api api, bool isDriverLoaded,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800275 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800276 ATRACE_CALL();
277
278 // Do not sendGpuStats for those skipping the GraphicsEnvironment setup
279 if (mGpuStats.appPackageName.empty()) return;
280
281 ALOGV("sendGpuStats:\n"
282 "\tdriverPackageName[%s]\n"
283 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800284 "\tdriverVersionCode[%" PRIu64 "]\n"
285 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800286 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700287 "\tvulkanVersion[%d]\n"
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700288 "\tapi[%d]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800289 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800290 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800291 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-19 16:00:25 -0800292 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700293 mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime);
294
295 GraphicsEnv::Driver driver = GraphicsEnv::Driver::NONE;
296 bool isIntendedDriverLoaded = false;
297 if (api == GraphicsEnv::Api::API_GL) {
298 driver = mGpuStats.glDriverToLoad;
299 isIntendedDriverLoaded =
300 isDriverLoaded && (mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE);
301 } else {
302 driver = mGpuStats.vkDriverToLoad;
303 isIntendedDriverLoaded =
304 isDriverLoaded && (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE);
305 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800306
Yiwei Zhangd9861812019-02-13 11:51:55 -0800307 const sp<IGpuService> gpuService = getGpuService();
308 if (gpuService) {
309 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800310 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700311 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700312 isIntendedDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800313 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800314}
315
Tim Van Patten5f744f12018-12-12 11:46:21 -0700316void* GraphicsEnv::loadLibrary(std::string name) {
317 const android_dlextinfo dlextinfo = {
318 .flags = ANDROID_DLEXT_USE_NAMESPACE,
319 .library_namespace = getAngleNamespace(),
320 };
321
322 std::string libName = std::string("lib") + name + "_angle.so";
323
324 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
325
326 if (so) {
327 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
328 return so;
329 } else {
330 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
331 }
332
333 return nullptr;
334}
335
336bool GraphicsEnv::checkAngleRules(void* so) {
337 char manufacturer[PROPERTY_VALUE_MAX];
338 char model[PROPERTY_VALUE_MAX];
339 property_get("ro.product.manufacturer", manufacturer, "UNSET");
340 property_get("ro.product.model", model, "UNSET");
341
342 auto ANGLEGetFeatureSupportUtilAPIVersion =
343 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
344 "ANGLEGetFeatureSupportUtilAPIVersion");
345
346 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
347 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
348 return false;
349 }
350
351 // Negotiate the interface version by requesting most recent known to the platform
352 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
353 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
354 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
355 "requested version %u",
356 versionToUse);
357 return false;
358 }
359
360 // Add and remove versions below as needed
361 bool useAngle = false;
362 switch (versionToUse) {
363 case 2: {
364 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
365 void* rulesHandle = nullptr;
366 int rulesVersion = 0;
367 void* systemInfoHandle = nullptr;
368
369 // Get the symbols for the feature-support-utility library:
370#define GET_SYMBOL(symbol) \
371 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
372 if (!symbol) { \
373 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
374 break; \
375 }
376 GET_SYMBOL(ANGLEAndroidParseRulesString);
377 GET_SYMBOL(ANGLEGetSystemInfo);
378 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
379 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
380 GET_SYMBOL(ANGLEFreeRulesHandle);
381 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
382
383 // Parse the rules, obtain the SystemInfo, and evaluate the
384 // application against the rules:
385 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
386 ALOGW("ANGLE feature-support library cannot parse rules file");
387 break;
388 }
389 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
390 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
391 break;
392 }
393 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) {
394 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
395 break;
396 }
397 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
398 systemInfoHandle, mAngleAppName.c_str());
399 (ANGLEFreeRulesHandle)(rulesHandle);
400 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
401 } break;
402
403 default:
404 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
405 }
406
407 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
408 return useAngle;
409}
410
411bool GraphicsEnv::shouldUseAngle(std::string appName) {
412 if (appName != mAngleAppName) {
413 // Make sure we are checking the app we were init'ed for
414 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
415 appName.c_str());
416 return false;
417 }
418
419 return shouldUseAngle();
420}
421
422bool GraphicsEnv::shouldUseAngle() {
423 // Make sure we are init'ed
424 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700425 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700426 return false;
427 }
428
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700429 return (mUseAngle == YES) ? true : false;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700430}
431
432void GraphicsEnv::updateUseAngle() {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700433 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700434
435 const char* ANGLE_PREFER_ANGLE = "angle";
436 const char* ANGLE_PREFER_NATIVE = "native";
437
438 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
439 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700440 mUseAngle = YES;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700441 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
442 ALOGV("User set \"Developer Options\" to force the use of Native");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700443 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700444 } else {
445 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
446 // load ANGLE and call the updatable opt-in/out logic:
Cody Northropc15d3822019-01-17 10:26:47 -0700447 void* featureSo = loadLibrary("feature_support");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700448 if (featureSo) {
449 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700450 mUseAngle = checkAngleRules(featureSo) ? YES : NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700451 dlclose(featureSo);
452 featureSo = nullptr;
453 } else {
454 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
455 }
456 }
457}
458
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600459void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700460 const std::string developerOptIn, const int rulesFd,
461 const long rulesOffset, const long rulesLength) {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700462 if (mUseAngle != UNKNOWN) {
463 // We've already figured out an answer for this app, so just return.
464 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
465 (mUseAngle == YES) ? "true" : "false");
466 return;
467 }
468
Tim Van Patten5f744f12018-12-12 11:46:21 -0700469 ALOGV("setting ANGLE path to '%s'", path.c_str());
470 mAnglePath = path;
471 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
472 mAngleAppName = appName;
473 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
474 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600475
Tim Van Patten5f744f12018-12-12 11:46:21 -0700476 lseek(rulesFd, rulesOffset, SEEK_SET);
477 mRulesBuffer = std::vector<char>(rulesLength + 1);
478 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
479 if (numBytesRead < 0) {
480 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
481 numBytesRead = 0;
482 } else if (numBytesRead == 0) {
483 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600484 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700485 if (numBytesRead != rulesLength) {
486 ALOGW("Did not read all of the necessary bytes from the rules file."
487 "expected: %ld, got: %zd",
488 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700489 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700490 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 10:34:58 -0600491
Tim Van Patten5f744f12018-12-12 11:46:21 -0700492 // Update the current status of whether we should use ANGLE or not
493 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600494}
495
Victor Khimenko4819b522018-07-13 17:24:18 +0200496void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600497 if (mLayerPaths.empty()) {
498 mLayerPaths = layerPaths;
499 mAppNamespace = appNamespace;
500 } else {
501 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800502 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600503 }
504}
505
Victor Khimenko4819b522018-07-13 17:24:18 +0200506NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600507 return mAppNamespace;
508}
509
Tim Van Patten5f744f12018-12-12 11:46:21 -0700510std::string& GraphicsEnv::getAngleAppName() {
511 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600512}
513
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600514const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600515 return mLayerPaths;
516}
517
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600518const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600519 return mDebugLayers;
520}
521
Cody Northropb9b01b62018-10-23 13:13:10 -0600522const std::string& GraphicsEnv::getDebugLayersGLES() {
523 return mDebugLayersGLES;
524}
525
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600526void GraphicsEnv::setDebugLayers(const std::string layers) {
527 mDebugLayers = layers;
528}
529
Cody Northropb9b01b62018-10-23 13:13:10 -0600530void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
531 mDebugLayersGLES = layers;
532}
533
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700534// Return true if all the required libraries from vndk and sphal namespace are
535// linked to the Game Driver namespace correctly.
536bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* vndkNamespace) {
537 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
538 if (llndkLibraries.empty()) {
539 return false;
540 }
541 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
542 ALOGE("Failed to link default namespace[%s]", dlerror());
543 return false;
544 }
545
546 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
547 if (vndkspLibraries.empty()) {
548 return false;
549 }
550 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
551 ALOGE("Failed to link vndk namespace[%s]", dlerror());
552 return false;
553 }
554
555 if (mSphalLibraries.empty()) {
556 return true;
557 }
558
559 // Make additional libraries in sphal to be accessible
560 auto sphalNamespace = android_get_exported_namespace("sphal");
561 if (!sphalNamespace) {
562 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
563 mSphalLibraries.c_str());
564 return false;
565 }
566
567 if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) {
568 ALOGE("Failed to link sphal namespace[%s]", dlerror());
569 return false;
570 }
571
572 return true;
573}
574
Jesse Hall53457db2016-12-14 16:54:06 -0800575android_namespace_t* GraphicsEnv::getDriverNamespace() {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700576 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Yiwei Zhang64d89212018-11-27 19:58:29 -0800577
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700578 if (mDriverNamespace) {
579 return mDriverNamespace;
580 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800581
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700582 if (mDriverPath.empty()) {
583 return nullptr;
584 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800585
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700586 auto vndkNamespace = android_get_exported_namespace("vndk");
587 if (!vndkNamespace) {
588 return nullptr;
589 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800590
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700591 mDriverNamespace = android_create_namespace("gfx driver",
592 mDriverPath.c_str(), // ld_library_path
593 mDriverPath.c_str(), // default_library_path
594 ANDROID_NAMESPACE_TYPE_ISOLATED,
595 nullptr, // permitted_when_isolated_path
596 nullptr);
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800597
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700598 if (!linkDriverNamespaceLocked(vndkNamespace)) {
599 mDriverNamespace = nullptr;
600 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800601
Jesse Hall53457db2016-12-14 16:54:06 -0800602 return mDriverNamespace;
603}
604
Cody Northrop1f00e172018-04-02 11:23:31 -0600605android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700606 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600607
Cody Northrop3892cfa2019-01-30 10:03:12 -0700608 if (mAngleNamespace) {
609 return mAngleNamespace;
610 }
611
612 if (mAnglePath.empty()) {
613 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
614 return nullptr;
615 }
616
617 mAngleNamespace = android_create_namespace("ANGLE",
618 nullptr, // ld_library_path
619 mAnglePath.c_str(), // default_library_path
620 ANDROID_NAMESPACE_TYPE_SHARED |
621 ANDROID_NAMESPACE_TYPE_ISOLATED,
622 nullptr, // permitted_when_isolated_path
623 nullptr);
624
625 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600626
627 return mAngleNamespace;
628}
629
Jesse Hall90b25ed2016-12-12 12:56:46 -0800630} // namespace android