blob: 354703b9e4a0d88763ee2eabebfd53e09865f12d [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
Yiwei Zhang6a674c92019-11-08 11:55:36 -0800127bool GraphicsEnv::isDebuggable() {
128 return prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) > 0;
Cody Northrop629ce4e2018-10-15 07:22:09 -0600129}
130
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800131void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string path,
132 const std::string sphalLibraries) {
133 if (!mDriverPath.empty() || !mSphalLibraries.empty()) {
134 ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries "
135 "from '%s' to '%s'",
136 mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800137 return;
138 }
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800139 ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(),
140 sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800141 mDriverPath = path;
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800142 mSphalLibraries = sphalLibraries;
Jesse Hall90b25ed2016-12-12 12:56:46 -0800143}
144
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700145void GraphicsEnv::hintActivityLaunch() {
146 ATRACE_CALL();
147
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700148 std::thread trySendGpuStatsThread([this]() {
149 // If there's already graphics driver preloaded in the process, just send
150 // the stats info to GpuStats directly through async binder.
151 std::lock_guard<std::mutex> lock(mStatsLock);
152 if (mGpuStats.glDriverToSend) {
153 mGpuStats.glDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700154 sendGpuStatsLocked(GpuStatsInfo::Api::API_GL, true, mGpuStats.glDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700155 }
156 if (mGpuStats.vkDriverToSend) {
157 mGpuStats.vkDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700158 sendGpuStatsLocked(GpuStatsInfo::Api::API_VK, true, mGpuStats.vkDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700159 }
160 });
161 trySendGpuStatsThread.detach();
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700162}
163
Greg Kaiser210bb7e2019-02-12 12:40:05 -0800164void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800165 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700166 int64_t driverBuildTime, const std::string& appPackageName,
167 const int vulkanVersion) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800168 ATRACE_CALL();
169
Yiwei Zhangd9861812019-02-13 11:51:55 -0800170 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800171 ALOGV("setGpuStats:\n"
172 "\tdriverPackageName[%s]\n"
173 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800174 "\tdriverVersionCode[%" PRIu64 "]\n"
175 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700176 "\tappPackageName[%s]\n"
177 "\tvulkanVersion[%d]\n",
Yiwei Zhang96c01712019-02-19 16:00:25 -0800178 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700179 appPackageName.c_str(), vulkanVersion);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800180
Yiwei Zhangd9861812019-02-13 11:51:55 -0800181 mGpuStats.driverPackageName = driverPackageName;
182 mGpuStats.driverVersionName = driverVersionName;
183 mGpuStats.driverVersionCode = driverVersionCode;
Yiwei Zhang96c01712019-02-19 16:00:25 -0800184 mGpuStats.driverBuildTime = driverBuildTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800185 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang794d2952019-05-06 17:43:59 -0700186 mGpuStats.vulkanVersion = vulkanVersion;
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800187}
188
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700189void GraphicsEnv::setDriverToLoad(GpuStatsInfo::Driver driver) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800190 ATRACE_CALL();
191
192 std::lock_guard<std::mutex> lock(mStatsLock);
193 switch (driver) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700194 case GpuStatsInfo::Driver::GL:
195 case GpuStatsInfo::Driver::GL_UPDATED:
196 case GpuStatsInfo::Driver::ANGLE: {
Yiwei Zhang472cab02019-08-05 17:57:41 -0700197 if (mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::NONE ||
198 mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::GL) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800199 mGpuStats.glDriverToLoad = driver;
200 break;
201 }
202
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700203 if (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800204 mGpuStats.glDriverFallback = driver;
205 }
206 break;
207 }
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700208 case GpuStatsInfo::Driver::VULKAN:
209 case GpuStatsInfo::Driver::VULKAN_UPDATED: {
Yiwei Zhang472cab02019-08-05 17:57:41 -0700210 if (mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::NONE ||
211 mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::VULKAN) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800212 mGpuStats.vkDriverToLoad = driver;
213 break;
214 }
215
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700216 if (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800217 mGpuStats.vkDriverFallback = driver;
218 }
219 break;
220 }
221 default:
222 break;
223 }
224}
225
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700226void GraphicsEnv::setDriverLoaded(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700227 int64_t driverLoadingTime) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800228 ATRACE_CALL();
229
230 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700231 const bool doNotSend = mGpuStats.appPackageName.empty();
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700232 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700233 if (doNotSend) mGpuStats.glDriverToSend = true;
234 mGpuStats.glDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800235 } else {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700236 if (doNotSend) mGpuStats.vkDriverToSend = true;
237 mGpuStats.vkDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800238 }
239
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700240 sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800241}
242
Yiwei Zhangd9861812019-02-13 11:51:55 -0800243static sp<IGpuService> getGpuService() {
Yiwei Zhang8e097302019-07-08 16:11:12 -0700244 static const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
Yiwei Zhangd9861812019-02-13 11:51:55 -0800245 if (!binder) {
246 ALOGE("Failed to get gpu service");
247 return nullptr;
248 }
249
250 return interface_cast<IGpuService>(binder);
251}
252
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700253void GraphicsEnv::setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value) {
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700254 ATRACE_CALL();
255
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700256 std::lock_guard<std::mutex> lock(mStatsLock);
257 const sp<IGpuService> gpuService = getGpuService();
258 if (gpuService) {
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700259 gpuService->setTargetStats(mGpuStats.appPackageName, mGpuStats.driverVersionCode, stats,
260 value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700261 }
262}
263
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700264void GraphicsEnv::sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800265 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800266 ATRACE_CALL();
267
268 // Do not sendGpuStats for those skipping the GraphicsEnvironment setup
269 if (mGpuStats.appPackageName.empty()) return;
270
271 ALOGV("sendGpuStats:\n"
272 "\tdriverPackageName[%s]\n"
273 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800274 "\tdriverVersionCode[%" PRIu64 "]\n"
275 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800276 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700277 "\tvulkanVersion[%d]\n"
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700278 "\tapi[%d]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800279 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800280 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800281 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-19 16:00:25 -0800282 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700283 mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime);
284
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700285 GpuStatsInfo::Driver driver = GpuStatsInfo::Driver::NONE;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700286 bool isIntendedDriverLoaded = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700287 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700288 driver = mGpuStats.glDriverToLoad;
289 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700290 isDriverLoaded && (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700291 } else {
292 driver = mGpuStats.vkDriverToLoad;
293 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700294 isDriverLoaded && (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700295 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800296
Yiwei Zhangd9861812019-02-13 11:51:55 -0800297 const sp<IGpuService> gpuService = getGpuService();
298 if (gpuService) {
299 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800300 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700301 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700302 isIntendedDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800303 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800304}
305
Adam Bodnar0afcca02019-09-17 13:23:17 -0700306bool GraphicsEnv::setInjectLayersPrSetDumpable() {
307 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
308 return false;
309 }
310 return true;
311}
312
Tim Van Patten5f744f12018-12-12 11:46:21 -0700313void* GraphicsEnv::loadLibrary(std::string name) {
314 const android_dlextinfo dlextinfo = {
315 .flags = ANDROID_DLEXT_USE_NAMESPACE,
316 .library_namespace = getAngleNamespace(),
317 };
318
319 std::string libName = std::string("lib") + name + "_angle.so";
320
321 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
322
323 if (so) {
324 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
325 return so;
326 } else {
327 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
328 }
329
330 return nullptr;
331}
332
333bool GraphicsEnv::checkAngleRules(void* so) {
334 char manufacturer[PROPERTY_VALUE_MAX];
335 char model[PROPERTY_VALUE_MAX];
336 property_get("ro.product.manufacturer", manufacturer, "UNSET");
337 property_get("ro.product.model", model, "UNSET");
338
339 auto ANGLEGetFeatureSupportUtilAPIVersion =
340 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
341 "ANGLEGetFeatureSupportUtilAPIVersion");
342
343 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
344 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
345 return false;
346 }
347
348 // Negotiate the interface version by requesting most recent known to the platform
349 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
350 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
351 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
352 "requested version %u",
353 versionToUse);
354 return false;
355 }
356
357 // Add and remove versions below as needed
358 bool useAngle = false;
359 switch (versionToUse) {
360 case 2: {
361 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
362 void* rulesHandle = nullptr;
363 int rulesVersion = 0;
364 void* systemInfoHandle = nullptr;
365
366 // Get the symbols for the feature-support-utility library:
367#define GET_SYMBOL(symbol) \
368 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
369 if (!symbol) { \
370 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
371 break; \
372 }
373 GET_SYMBOL(ANGLEAndroidParseRulesString);
374 GET_SYMBOL(ANGLEGetSystemInfo);
375 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
376 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
377 GET_SYMBOL(ANGLEFreeRulesHandle);
378 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
379
380 // Parse the rules, obtain the SystemInfo, and evaluate the
381 // application against the rules:
382 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
383 ALOGW("ANGLE feature-support library cannot parse rules file");
384 break;
385 }
386 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
387 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
388 break;
389 }
390 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) {
391 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
392 break;
393 }
394 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
395 systemInfoHandle, mAngleAppName.c_str());
396 (ANGLEFreeRulesHandle)(rulesHandle);
397 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
398 } break;
399
400 default:
401 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
402 }
403
404 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
405 return useAngle;
406}
407
408bool GraphicsEnv::shouldUseAngle(std::string appName) {
409 if (appName != mAngleAppName) {
410 // Make sure we are checking the app we were init'ed for
411 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
412 appName.c_str());
413 return false;
414 }
415
416 return shouldUseAngle();
417}
418
419bool GraphicsEnv::shouldUseAngle() {
420 // Make sure we are init'ed
421 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700422 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700423 return false;
424 }
425
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700426 return (mUseAngle == YES) ? true : false;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700427}
428
429void GraphicsEnv::updateUseAngle() {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700430 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700431
432 const char* ANGLE_PREFER_ANGLE = "angle";
433 const char* ANGLE_PREFER_NATIVE = "native";
434
435 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
436 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700437 mUseAngle = YES;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700438 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
439 ALOGV("User set \"Developer Options\" to force the use of Native");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700440 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700441 } else {
442 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
443 // load ANGLE and call the updatable opt-in/out logic:
Cody Northropc15d3822019-01-17 10:26:47 -0700444 void* featureSo = loadLibrary("feature_support");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700445 if (featureSo) {
446 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700447 mUseAngle = checkAngleRules(featureSo) ? YES : NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700448 dlclose(featureSo);
449 featureSo = nullptr;
450 } else {
451 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
452 }
453 }
454}
455
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600456void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700457 const std::string developerOptIn, const int rulesFd,
458 const long rulesOffset, const long rulesLength) {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700459 if (mUseAngle != UNKNOWN) {
460 // We've already figured out an answer for this app, so just return.
461 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
462 (mUseAngle == YES) ? "true" : "false");
463 return;
464 }
465
Tim Van Patten5f744f12018-12-12 11:46:21 -0700466 ALOGV("setting ANGLE path to '%s'", path.c_str());
467 mAnglePath = path;
468 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
469 mAngleAppName = appName;
470 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
471 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600472
Tim Van Patten5f744f12018-12-12 11:46:21 -0700473 lseek(rulesFd, rulesOffset, SEEK_SET);
474 mRulesBuffer = std::vector<char>(rulesLength + 1);
475 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
476 if (numBytesRead < 0) {
477 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
478 numBytesRead = 0;
479 } else if (numBytesRead == 0) {
480 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600481 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700482 if (numBytesRead != rulesLength) {
483 ALOGW("Did not read all of the necessary bytes from the rules file."
484 "expected: %ld, got: %zd",
485 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700486 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700487 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 10:34:58 -0600488
Tim Van Patten5f744f12018-12-12 11:46:21 -0700489 // Update the current status of whether we should use ANGLE or not
490 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600491}
492
Victor Khimenko4819b522018-07-13 17:24:18 +0200493void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600494 if (mLayerPaths.empty()) {
495 mLayerPaths = layerPaths;
496 mAppNamespace = appNamespace;
497 } else {
498 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800499 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600500 }
501}
502
Victor Khimenko4819b522018-07-13 17:24:18 +0200503NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600504 return mAppNamespace;
505}
506
Tim Van Patten5f744f12018-12-12 11:46:21 -0700507std::string& GraphicsEnv::getAngleAppName() {
508 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600509}
510
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600511const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600512 return mLayerPaths;
513}
514
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600515const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600516 return mDebugLayers;
517}
518
Cody Northropb9b01b62018-10-23 13:13:10 -0600519const std::string& GraphicsEnv::getDebugLayersGLES() {
520 return mDebugLayersGLES;
521}
522
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600523void GraphicsEnv::setDebugLayers(const std::string layers) {
524 mDebugLayers = layers;
525}
526
Cody Northropb9b01b62018-10-23 13:13:10 -0600527void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
528 mDebugLayersGLES = layers;
529}
530
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700531// Return true if all the required libraries from vndk and sphal namespace are
532// linked to the Game Driver namespace correctly.
533bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* vndkNamespace) {
534 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
535 if (llndkLibraries.empty()) {
536 return false;
537 }
538 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
539 ALOGE("Failed to link default namespace[%s]", dlerror());
540 return false;
541 }
542
543 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
544 if (vndkspLibraries.empty()) {
545 return false;
546 }
547 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
548 ALOGE("Failed to link vndk namespace[%s]", dlerror());
549 return false;
550 }
551
552 if (mSphalLibraries.empty()) {
553 return true;
554 }
555
556 // Make additional libraries in sphal to be accessible
557 auto sphalNamespace = android_get_exported_namespace("sphal");
558 if (!sphalNamespace) {
559 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
560 mSphalLibraries.c_str());
561 return false;
562 }
563
564 if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) {
565 ALOGE("Failed to link sphal namespace[%s]", dlerror());
566 return false;
567 }
568
569 return true;
570}
571
Jesse Hall53457db2016-12-14 16:54:06 -0800572android_namespace_t* GraphicsEnv::getDriverNamespace() {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700573 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Yiwei Zhang64d89212018-11-27 19:58:29 -0800574
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700575 if (mDriverNamespace) {
576 return mDriverNamespace;
577 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800578
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700579 if (mDriverPath.empty()) {
580 return nullptr;
581 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800582
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700583 auto vndkNamespace = android_get_exported_namespace("vndk");
584 if (!vndkNamespace) {
585 return nullptr;
586 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800587
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700588 mDriverNamespace = android_create_namespace("gfx driver",
589 mDriverPath.c_str(), // ld_library_path
590 mDriverPath.c_str(), // default_library_path
591 ANDROID_NAMESPACE_TYPE_ISOLATED,
592 nullptr, // permitted_when_isolated_path
593 nullptr);
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800594
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700595 if (!linkDriverNamespaceLocked(vndkNamespace)) {
596 mDriverNamespace = nullptr;
597 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800598
Jesse Hall53457db2016-12-14 16:54:06 -0800599 return mDriverNamespace;
600}
601
Cody Northrop1f00e172018-04-02 11:23:31 -0600602android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700603 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600604
Cody Northrop3892cfa2019-01-30 10:03:12 -0700605 if (mAngleNamespace) {
606 return mAngleNamespace;
607 }
608
609 if (mAnglePath.empty()) {
610 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
611 return nullptr;
612 }
613
614 mAngleNamespace = android_create_namespace("ANGLE",
615 nullptr, // ld_library_path
616 mAnglePath.c_str(), // default_library_path
617 ANDROID_NAMESPACE_TYPE_SHARED |
618 ANDROID_NAMESPACE_TYPE_ISOLATED,
619 nullptr, // permitted_when_isolated_path
620 nullptr);
621
622 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600623
624 return mAngleNamespace;
625}
626
Jesse Hall90b25ed2016-12-12 12:56:46 -0800627} // namespace android