blob: 7f8d3a66b5a1a66743156d4077863f92a547cf66 [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>
Yiwei Zhang49b9ac72019-08-05 16:57:17 -070035#include <nativeloader/dlext_namespaces.h>
Cody Northrop629ce4e2018-10-15 07:22:09 -060036#include <sys/prctl.h>
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080037#include <utils/Trace.h>
Cody Northrop629ce4e2018-10-15 07:22:09 -060038
Tim Van Patten5f744f12018-12-12 11:46:21 -070039#include <memory>
Tim Van Patten5f744f12018-12-12 11:46:21 -070040#include <string>
Yiwei Zhang3c74da92019-06-28 10:16:49 -070041#include <thread>
Tim Van Patten5f744f12018-12-12 11:46:21 -070042
Tim Van Patten5f744f12018-12-12 11:46:21 -070043// TODO(ianelliott@): Get the following from an ANGLE header:
44#define CURRENT_ANGLE_API_VERSION 2 // Current API verion we are targetting
45// Version-2 API:
46typedef bool (*fpANGLEGetFeatureSupportUtilAPIVersion)(unsigned int* versionToUse);
47typedef bool (*fpANGLEAndroidParseRulesString)(const char* rulesString, void** rulesHandle,
48 int* rulesVersion);
49typedef bool (*fpANGLEGetSystemInfo)(void** handle);
50typedef bool (*fpANGLEAddDeviceInfoToSystemInfo)(const char* deviceMfr, const char* deviceModel,
51 void* handle);
52typedef bool (*fpANGLEShouldBeUsedForApplication)(void* rulesHandle, int rulesVersion,
53 void* systemInfoHandle, const char* appName);
54typedef bool (*fpANGLEFreeRulesHandle)(void* handle);
55typedef bool (*fpANGLEFreeSystemInfoHandle)(void* handle);
56
Jesse Hall90b25ed2016-12-12 12:56:46 -080057namespace android {
58
Yiwei Zhang64d89212018-11-27 19:58:29 -080059enum NativeLibrary {
60 LLNDK = 0,
61 VNDKSP = 1,
62};
63
64static constexpr const char* kNativeLibrariesSystemConfigPath[] = {"/etc/llndk.libraries.txt",
65 "/etc/vndksp.libraries.txt"};
66
67static std::string vndkVersionStr() {
68#ifdef __BIONIC__
69 std::string version = android::base::GetProperty("ro.vndk.version", "");
70 if (version != "" && version != "current") {
71 return "." + version;
72 }
73#endif
74 return "";
75}
76
77static void insertVndkVersionStr(std::string* fileName) {
78 LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr");
79 size_t insertPos = fileName->find_last_of(".");
80 if (insertPos == std::string::npos) {
81 insertPos = fileName->length();
82 }
83 fileName->insert(insertPos, vndkVersionStr());
84}
85
86static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) {
87 // Read list of public native libraries from the config file.
88 std::string fileContent;
89 if (!base::ReadFileToString(configFile, &fileContent)) {
90 return false;
91 }
92
93 std::vector<std::string> lines = base::Split(fileContent, "\n");
94
95 for (auto& line : lines) {
96 auto trimmedLine = base::Trim(line);
97 if (!trimmedLine.empty()) {
98 soNames->push_back(trimmedLine);
99 }
100 }
101
102 return true;
103}
104
105static const std::string getSystemNativeLibraries(NativeLibrary type) {
106 static const char* androidRootEnv = getenv("ANDROID_ROOT");
107 static const std::string rootDir = androidRootEnv != nullptr ? androidRootEnv : "/system";
108
109 std::string nativeLibrariesSystemConfig = rootDir + kNativeLibrariesSystemConfigPath[type];
110
111 insertVndkVersionStr(&nativeLibrariesSystemConfig);
112
113 std::vector<std::string> soNames;
114 if (!readConfig(nativeLibrariesSystemConfig, &soNames)) {
115 ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str());
116 return "";
117 }
118
119 return base::Join(soNames, ':');
120}
121
Jesse Hall90b25ed2016-12-12 12:56:46 -0800122/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
123 static GraphicsEnv env;
124 return env;
125}
126
Cody Northrop629ce4e2018-10-15 07:22:09 -0600127int GraphicsEnv::getCanLoadSystemLibraries() {
128 if (property_get_bool("ro.debuggable", false) && prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
129 // Return an integer value since this crosses library boundaries
130 return 1;
131 }
132 return 0;
133}
134
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800135void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string path,
136 const std::string sphalLibraries) {
137 if (!mDriverPath.empty() || !mSphalLibraries.empty()) {
138 ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries "
139 "from '%s' to '%s'",
140 mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800141 return;
142 }
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800143 ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(),
144 sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800145 mDriverPath = path;
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800146 mSphalLibraries = sphalLibraries;
Jesse Hall90b25ed2016-12-12 12:56:46 -0800147}
148
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700149void GraphicsEnv::hintActivityLaunch() {
150 ATRACE_CALL();
151
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700152 std::thread trySendGpuStatsThread([this]() {
153 // If there's already graphics driver preloaded in the process, just send
154 // the stats info to GpuStats directly through async binder.
155 std::lock_guard<std::mutex> lock(mStatsLock);
156 if (mGpuStats.glDriverToSend) {
157 mGpuStats.glDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700158 sendGpuStatsLocked(GpuStatsInfo::Api::API_GL, true, mGpuStats.glDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700159 }
160 if (mGpuStats.vkDriverToSend) {
161 mGpuStats.vkDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700162 sendGpuStatsLocked(GpuStatsInfo::Api::API_VK, true, mGpuStats.vkDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700163 }
164 });
165 trySendGpuStatsThread.detach();
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700166}
167
Greg Kaiser210bb7e2019-02-12 12:40:05 -0800168void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800169 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700170 int64_t driverBuildTime, const std::string& appPackageName,
171 const int vulkanVersion) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800172 ATRACE_CALL();
173
Yiwei Zhangd9861812019-02-13 11:51:55 -0800174 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800175 ALOGV("setGpuStats:\n"
176 "\tdriverPackageName[%s]\n"
177 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800178 "\tdriverVersionCode[%" PRIu64 "]\n"
179 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700180 "\tappPackageName[%s]\n"
181 "\tvulkanVersion[%d]\n",
Yiwei Zhang96c01712019-02-19 16:00:25 -0800182 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700183 appPackageName.c_str(), vulkanVersion);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800184
Yiwei Zhangd9861812019-02-13 11:51:55 -0800185 mGpuStats.driverPackageName = driverPackageName;
186 mGpuStats.driverVersionName = driverVersionName;
187 mGpuStats.driverVersionCode = driverVersionCode;
Yiwei Zhang96c01712019-02-19 16:00:25 -0800188 mGpuStats.driverBuildTime = driverBuildTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800189 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang794d2952019-05-06 17:43:59 -0700190 mGpuStats.vulkanVersion = vulkanVersion;
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800191}
192
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700193void GraphicsEnv::setDriverToLoad(GpuStatsInfo::Driver driver) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800194 ATRACE_CALL();
195
196 std::lock_guard<std::mutex> lock(mStatsLock);
197 switch (driver) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700198 case GpuStatsInfo::Driver::GL:
199 case GpuStatsInfo::Driver::GL_UPDATED:
200 case GpuStatsInfo::Driver::ANGLE: {
201 if (mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800202 mGpuStats.glDriverToLoad = driver;
203 break;
204 }
205
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700206 if (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800207 mGpuStats.glDriverFallback = driver;
208 }
209 break;
210 }
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700211 case GpuStatsInfo::Driver::VULKAN:
212 case GpuStatsInfo::Driver::VULKAN_UPDATED: {
213 if (mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800214 mGpuStats.vkDriverToLoad = driver;
215 break;
216 }
217
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700218 if (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800219 mGpuStats.vkDriverFallback = driver;
220 }
221 break;
222 }
223 default:
224 break;
225 }
226}
227
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700228void GraphicsEnv::setDriverLoaded(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700229 int64_t driverLoadingTime) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800230 ATRACE_CALL();
231
232 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700233 const bool doNotSend = mGpuStats.appPackageName.empty();
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700234 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700235 if (doNotSend) mGpuStats.glDriverToSend = true;
236 mGpuStats.glDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800237 } else {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700238 if (doNotSend) mGpuStats.vkDriverToSend = true;
239 mGpuStats.vkDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800240 }
241
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700242 sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800243}
244
Yiwei Zhangd9861812019-02-13 11:51:55 -0800245static sp<IGpuService> getGpuService() {
Yiwei Zhang8e097302019-07-08 16:11:12 -0700246 static const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
Yiwei Zhangd9861812019-02-13 11:51:55 -0800247 if (!binder) {
248 ALOGE("Failed to get gpu service");
249 return nullptr;
250 }
251
252 return interface_cast<IGpuService>(binder);
253}
254
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700255void GraphicsEnv::setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value) {
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700256 ATRACE_CALL();
257
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700258 std::lock_guard<std::mutex> lock(mStatsLock);
259 const sp<IGpuService> gpuService = getGpuService();
260 if (gpuService) {
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700261 gpuService->setTargetStats(mGpuStats.appPackageName, mGpuStats.driverVersionCode, stats,
262 value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700263 }
264}
265
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700266void GraphicsEnv::sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800267 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800268 ATRACE_CALL();
269
270 // Do not sendGpuStats for those skipping the GraphicsEnvironment setup
271 if (mGpuStats.appPackageName.empty()) return;
272
273 ALOGV("sendGpuStats:\n"
274 "\tdriverPackageName[%s]\n"
275 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800276 "\tdriverVersionCode[%" PRIu64 "]\n"
277 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800278 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700279 "\tvulkanVersion[%d]\n"
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700280 "\tapi[%d]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800281 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800282 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800283 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-19 16:00:25 -0800284 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700285 mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime);
286
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700287 GpuStatsInfo::Driver driver = GpuStatsInfo::Driver::NONE;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700288 bool isIntendedDriverLoaded = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700289 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700290 driver = mGpuStats.glDriverToLoad;
291 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700292 isDriverLoaded && (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700293 } else {
294 driver = mGpuStats.vkDriverToLoad;
295 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700296 isDriverLoaded && (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700297 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800298
Yiwei Zhangd9861812019-02-13 11:51:55 -0800299 const sp<IGpuService> gpuService = getGpuService();
300 if (gpuService) {
301 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800302 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700303 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700304 isIntendedDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800305 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800306}
307
Tim Van Patten5f744f12018-12-12 11:46:21 -0700308void* GraphicsEnv::loadLibrary(std::string name) {
309 const android_dlextinfo dlextinfo = {
310 .flags = ANDROID_DLEXT_USE_NAMESPACE,
311 .library_namespace = getAngleNamespace(),
312 };
313
314 std::string libName = std::string("lib") + name + "_angle.so";
315
316 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
317
318 if (so) {
319 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
320 return so;
321 } else {
322 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
323 }
324
325 return nullptr;
326}
327
328bool GraphicsEnv::checkAngleRules(void* so) {
329 char manufacturer[PROPERTY_VALUE_MAX];
330 char model[PROPERTY_VALUE_MAX];
331 property_get("ro.product.manufacturer", manufacturer, "UNSET");
332 property_get("ro.product.model", model, "UNSET");
333
334 auto ANGLEGetFeatureSupportUtilAPIVersion =
335 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
336 "ANGLEGetFeatureSupportUtilAPIVersion");
337
338 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
339 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
340 return false;
341 }
342
343 // Negotiate the interface version by requesting most recent known to the platform
344 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
345 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
346 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
347 "requested version %u",
348 versionToUse);
349 return false;
350 }
351
352 // Add and remove versions below as needed
353 bool useAngle = false;
354 switch (versionToUse) {
355 case 2: {
356 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
357 void* rulesHandle = nullptr;
358 int rulesVersion = 0;
359 void* systemInfoHandle = nullptr;
360
361 // Get the symbols for the feature-support-utility library:
362#define GET_SYMBOL(symbol) \
363 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
364 if (!symbol) { \
365 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
366 break; \
367 }
368 GET_SYMBOL(ANGLEAndroidParseRulesString);
369 GET_SYMBOL(ANGLEGetSystemInfo);
370 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
371 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
372 GET_SYMBOL(ANGLEFreeRulesHandle);
373 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
374
375 // Parse the rules, obtain the SystemInfo, and evaluate the
376 // application against the rules:
377 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
378 ALOGW("ANGLE feature-support library cannot parse rules file");
379 break;
380 }
381 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
382 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
383 break;
384 }
385 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) {
386 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
387 break;
388 }
389 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
390 systemInfoHandle, mAngleAppName.c_str());
391 (ANGLEFreeRulesHandle)(rulesHandle);
392 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
393 } break;
394
395 default:
396 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
397 }
398
399 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
400 return useAngle;
401}
402
403bool GraphicsEnv::shouldUseAngle(std::string appName) {
404 if (appName != mAngleAppName) {
405 // Make sure we are checking the app we were init'ed for
406 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
407 appName.c_str());
408 return false;
409 }
410
411 return shouldUseAngle();
412}
413
414bool GraphicsEnv::shouldUseAngle() {
415 // Make sure we are init'ed
416 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700417 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700418 return false;
419 }
420
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700421 return (mUseAngle == YES) ? true : false;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700422}
423
424void GraphicsEnv::updateUseAngle() {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700425 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700426
427 const char* ANGLE_PREFER_ANGLE = "angle";
428 const char* ANGLE_PREFER_NATIVE = "native";
429
430 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
431 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700432 mUseAngle = YES;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700433 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
434 ALOGV("User set \"Developer Options\" to force the use of Native");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700435 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700436 } else {
437 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
438 // load ANGLE and call the updatable opt-in/out logic:
Cody Northropc15d3822019-01-17 10:26:47 -0700439 void* featureSo = loadLibrary("feature_support");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700440 if (featureSo) {
441 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700442 mUseAngle = checkAngleRules(featureSo) ? YES : NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700443 dlclose(featureSo);
444 featureSo = nullptr;
445 } else {
446 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
447 }
448 }
449}
450
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600451void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700452 const std::string developerOptIn, const int rulesFd,
453 const long rulesOffset, const long rulesLength) {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700454 if (mUseAngle != UNKNOWN) {
455 // We've already figured out an answer for this app, so just return.
456 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
457 (mUseAngle == YES) ? "true" : "false");
458 return;
459 }
460
Tim Van Patten5f744f12018-12-12 11:46:21 -0700461 ALOGV("setting ANGLE path to '%s'", path.c_str());
462 mAnglePath = path;
463 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
464 mAngleAppName = appName;
465 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
466 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600467
Tim Van Patten5f744f12018-12-12 11:46:21 -0700468 lseek(rulesFd, rulesOffset, SEEK_SET);
469 mRulesBuffer = std::vector<char>(rulesLength + 1);
470 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
471 if (numBytesRead < 0) {
472 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
473 numBytesRead = 0;
474 } else if (numBytesRead == 0) {
475 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600476 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700477 if (numBytesRead != rulesLength) {
478 ALOGW("Did not read all of the necessary bytes from the rules file."
479 "expected: %ld, got: %zd",
480 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700481 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700482 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 10:34:58 -0600483
Tim Van Patten5f744f12018-12-12 11:46:21 -0700484 // Update the current status of whether we should use ANGLE or not
485 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600486}
487
Victor Khimenko4819b522018-07-13 17:24:18 +0200488void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600489 if (mLayerPaths.empty()) {
490 mLayerPaths = layerPaths;
491 mAppNamespace = appNamespace;
492 } else {
493 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800494 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600495 }
496}
497
Victor Khimenko4819b522018-07-13 17:24:18 +0200498NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600499 return mAppNamespace;
500}
501
Tim Van Patten5f744f12018-12-12 11:46:21 -0700502std::string& GraphicsEnv::getAngleAppName() {
503 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600504}
505
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600506const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600507 return mLayerPaths;
508}
509
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600510const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600511 return mDebugLayers;
512}
513
Cody Northropb9b01b62018-10-23 13:13:10 -0600514const std::string& GraphicsEnv::getDebugLayersGLES() {
515 return mDebugLayersGLES;
516}
517
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600518void GraphicsEnv::setDebugLayers(const std::string layers) {
519 mDebugLayers = layers;
520}
521
Cody Northropb9b01b62018-10-23 13:13:10 -0600522void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
523 mDebugLayersGLES = layers;
524}
525
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700526// Return true if all the required libraries from vndk and sphal namespace are
527// linked to the Game Driver namespace correctly.
528bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* vndkNamespace) {
529 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
530 if (llndkLibraries.empty()) {
531 return false;
532 }
533 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
534 ALOGE("Failed to link default namespace[%s]", dlerror());
535 return false;
536 }
537
538 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
539 if (vndkspLibraries.empty()) {
540 return false;
541 }
542 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
543 ALOGE("Failed to link vndk namespace[%s]", dlerror());
544 return false;
545 }
546
547 if (mSphalLibraries.empty()) {
548 return true;
549 }
550
551 // Make additional libraries in sphal to be accessible
552 auto sphalNamespace = android_get_exported_namespace("sphal");
553 if (!sphalNamespace) {
554 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
555 mSphalLibraries.c_str());
556 return false;
557 }
558
559 if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) {
560 ALOGE("Failed to link sphal namespace[%s]", dlerror());
561 return false;
562 }
563
564 return true;
565}
566
Jesse Hall53457db2016-12-14 16:54:06 -0800567android_namespace_t* GraphicsEnv::getDriverNamespace() {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700568 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Yiwei Zhang64d89212018-11-27 19:58:29 -0800569
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700570 if (mDriverNamespace) {
571 return mDriverNamespace;
572 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800573
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700574 if (mDriverPath.empty()) {
575 return nullptr;
576 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800577
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700578 auto vndkNamespace = android_get_exported_namespace("vndk");
579 if (!vndkNamespace) {
580 return nullptr;
581 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800582
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700583 mDriverNamespace = android_create_namespace("gfx driver",
584 mDriverPath.c_str(), // ld_library_path
585 mDriverPath.c_str(), // default_library_path
586 ANDROID_NAMESPACE_TYPE_ISOLATED,
587 nullptr, // permitted_when_isolated_path
588 nullptr);
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800589
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700590 if (!linkDriverNamespaceLocked(vndkNamespace)) {
591 mDriverNamespace = nullptr;
592 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800593
Jesse Hall53457db2016-12-14 16:54:06 -0800594 return mDriverNamespace;
595}
596
Cody Northrop1f00e172018-04-02 11:23:31 -0600597android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700598 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600599
Cody Northrop3892cfa2019-01-30 10:03:12 -0700600 if (mAngleNamespace) {
601 return mAngleNamespace;
602 }
603
604 if (mAnglePath.empty()) {
605 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
606 return nullptr;
607 }
608
609 mAngleNamespace = android_create_namespace("ANGLE",
610 nullptr, // ld_library_path
611 mAnglePath.c_str(), // default_library_path
612 ANDROID_NAMESPACE_TYPE_SHARED |
613 ANDROID_NAMESPACE_TYPE_ISOLATED,
614 nullptr, // permitted_when_isolated_path
615 nullptr);
616
617 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600618
619 return mAngleNamespace;
620}
621
Jesse Hall90b25ed2016-12-12 12:56:46 -0800622} // namespace android