blob: 4809c1f0d84233a6a8cfb6a7be6e30b085313f59 [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
Jooyung Han78396802020-02-23 03:02:43 +090064static constexpr const char* kNativeLibrariesSystemConfigPath[] =
65 {"/apex/com.android.vndk.v{}/etc/llndk.libraries.{}.txt",
66 "/apex/com.android.vndk.v{}/etc/vndksp.libraries.{}.txt"};
Yiwei Zhang64d89212018-11-27 19:58:29 -080067
68static std::string vndkVersionStr() {
69#ifdef __BIONIC__
Jooyung Han78396802020-02-23 03:02:43 +090070 return android::base::GetProperty("ro.vndk.version", "");
Yiwei Zhang64d89212018-11-27 19:58:29 -080071#endif
72 return "";
73}
74
75static void insertVndkVersionStr(std::string* fileName) {
76 LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr");
Jooyung Han78396802020-02-23 03:02:43 +090077 std::string version = vndkVersionStr();
78 size_t pos = fileName->find("{}");
79 while (pos != std::string::npos) {
80 fileName->replace(pos, 2, version);
81 pos = fileName->find("{}", pos + version.size());
Yiwei Zhang64d89212018-11-27 19:58:29 -080082 }
Yiwei Zhang64d89212018-11-27 19:58:29 -080083}
84
85static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) {
86 // Read list of public native libraries from the config file.
87 std::string fileContent;
88 if (!base::ReadFileToString(configFile, &fileContent)) {
89 return false;
90 }
91
92 std::vector<std::string> lines = base::Split(fileContent, "\n");
93
94 for (auto& line : lines) {
95 auto trimmedLine = base::Trim(line);
96 if (!trimmedLine.empty()) {
97 soNames->push_back(trimmedLine);
98 }
99 }
100
101 return true;
102}
103
104static const std::string getSystemNativeLibraries(NativeLibrary type) {
Jooyung Han78396802020-02-23 03:02:43 +0900105 std::string nativeLibrariesSystemConfig = kNativeLibrariesSystemConfigPath[type];
Yiwei Zhang64d89212018-11-27 19:58:29 -0800106 insertVndkVersionStr(&nativeLibrariesSystemConfig);
107
108 std::vector<std::string> soNames;
109 if (!readConfig(nativeLibrariesSystemConfig, &soNames)) {
110 ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str());
111 return "";
112 }
113
114 return base::Join(soNames, ':');
115}
116
Jesse Hall90b25ed2016-12-12 12:56:46 -0800117/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
118 static GraphicsEnv env;
119 return env;
120}
121
Yiwei Zhang6a674c92019-11-08 11:55:36 -0800122bool GraphicsEnv::isDebuggable() {
123 return prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) > 0;
Cody Northrop629ce4e2018-10-15 07:22:09 -0600124}
125
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800126void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string path,
127 const std::string sphalLibraries) {
128 if (!mDriverPath.empty() || !mSphalLibraries.empty()) {
129 ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries "
130 "from '%s' to '%s'",
131 mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800132 return;
133 }
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800134 ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(),
135 sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800136 mDriverPath = path;
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800137 mSphalLibraries = sphalLibraries;
Jesse Hall90b25ed2016-12-12 12:56:46 -0800138}
139
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700140void GraphicsEnv::hintActivityLaunch() {
141 ATRACE_CALL();
142
Yiwei Zhangd3819382020-01-07 19:53:56 -0800143 {
144 std::lock_guard<std::mutex> lock(mStatsLock);
145 if (mActivityLaunched) return;
146 mActivityLaunched = true;
147 }
148
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700149 std::thread trySendGpuStatsThread([this]() {
150 // If there's already graphics driver preloaded in the process, just send
151 // the stats info to GpuStats directly through async binder.
152 std::lock_guard<std::mutex> lock(mStatsLock);
153 if (mGpuStats.glDriverToSend) {
154 mGpuStats.glDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700155 sendGpuStatsLocked(GpuStatsInfo::Api::API_GL, true, mGpuStats.glDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700156 }
157 if (mGpuStats.vkDriverToSend) {
158 mGpuStats.vkDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700159 sendGpuStatsLocked(GpuStatsInfo::Api::API_VK, true, mGpuStats.vkDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700160 }
161 });
162 trySendGpuStatsThread.detach();
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700163}
164
Greg Kaiser210bb7e2019-02-12 12:40:05 -0800165void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800166 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700167 int64_t driverBuildTime, const std::string& appPackageName,
168 const int vulkanVersion) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800169 ATRACE_CALL();
170
Yiwei Zhangd9861812019-02-13 11:51:55 -0800171 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800172 ALOGV("setGpuStats:\n"
173 "\tdriverPackageName[%s]\n"
174 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800175 "\tdriverVersionCode[%" PRIu64 "]\n"
176 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700177 "\tappPackageName[%s]\n"
178 "\tvulkanVersion[%d]\n",
Yiwei Zhang96c01712019-02-19 16:00:25 -0800179 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700180 appPackageName.c_str(), vulkanVersion);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800181
Yiwei Zhangd9861812019-02-13 11:51:55 -0800182 mGpuStats.driverPackageName = driverPackageName;
183 mGpuStats.driverVersionName = driverVersionName;
184 mGpuStats.driverVersionCode = driverVersionCode;
Yiwei Zhang96c01712019-02-19 16:00:25 -0800185 mGpuStats.driverBuildTime = driverBuildTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800186 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang794d2952019-05-06 17:43:59 -0700187 mGpuStats.vulkanVersion = vulkanVersion;
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800188}
189
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700190void GraphicsEnv::setDriverToLoad(GpuStatsInfo::Driver driver) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800191 ATRACE_CALL();
192
193 std::lock_guard<std::mutex> lock(mStatsLock);
194 switch (driver) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700195 case GpuStatsInfo::Driver::GL:
196 case GpuStatsInfo::Driver::GL_UPDATED:
197 case GpuStatsInfo::Driver::ANGLE: {
Yiwei Zhang472cab02019-08-05 17:57:41 -0700198 if (mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::NONE ||
199 mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::GL) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800200 mGpuStats.glDriverToLoad = driver;
201 break;
202 }
203
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700204 if (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800205 mGpuStats.glDriverFallback = driver;
206 }
207 break;
208 }
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700209 case GpuStatsInfo::Driver::VULKAN:
210 case GpuStatsInfo::Driver::VULKAN_UPDATED: {
Yiwei Zhang472cab02019-08-05 17:57:41 -0700211 if (mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::NONE ||
212 mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::VULKAN) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800213 mGpuStats.vkDriverToLoad = driver;
214 break;
215 }
216
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700217 if (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800218 mGpuStats.vkDriverFallback = driver;
219 }
220 break;
221 }
222 default:
223 break;
224 }
225}
226
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700227void GraphicsEnv::setDriverLoaded(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700228 int64_t driverLoadingTime) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800229 ATRACE_CALL();
230
231 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700232 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhangd3819382020-01-07 19:53:56 -0800233 mGpuStats.glDriverToSend = true;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700234 mGpuStats.glDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800235 } else {
Yiwei Zhangd3819382020-01-07 19:53:56 -0800236 mGpuStats.vkDriverToSend = true;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700237 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 Zhangd3819382020-01-07 19:53:56 -0800253bool GraphicsEnv::readyToSendGpuStatsLocked() {
254 // Only send stats for processes having at least one activity launched and that process doesn't
255 // skip the GraphicsEnvironment setup.
256 return mActivityLaunched && !mGpuStats.appPackageName.empty();
257}
258
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700259void GraphicsEnv::setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value) {
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700260 ATRACE_CALL();
261
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700262 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangd3819382020-01-07 19:53:56 -0800263 if (!readyToSendGpuStatsLocked()) return;
264
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700265 const sp<IGpuService> gpuService = getGpuService();
266 if (gpuService) {
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700267 gpuService->setTargetStats(mGpuStats.appPackageName, mGpuStats.driverVersionCode, stats,
268 value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700269 }
270}
271
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700272void GraphicsEnv::sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800273 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800274 ATRACE_CALL();
275
Yiwei Zhangd3819382020-01-07 19:53:56 -0800276 if (!readyToSendGpuStatsLocked()) return;
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800277
278 ALOGV("sendGpuStats:\n"
279 "\tdriverPackageName[%s]\n"
280 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800281 "\tdriverVersionCode[%" PRIu64 "]\n"
282 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800283 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700284 "\tvulkanVersion[%d]\n"
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700285 "\tapi[%d]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800286 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800287 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800288 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-19 16:00:25 -0800289 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700290 mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime);
291
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700292 GpuStatsInfo::Driver driver = GpuStatsInfo::Driver::NONE;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700293 bool isIntendedDriverLoaded = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700294 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700295 driver = mGpuStats.glDriverToLoad;
296 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700297 isDriverLoaded && (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700298 } else {
299 driver = mGpuStats.vkDriverToLoad;
300 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700301 isDriverLoaded && (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700302 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800303
Yiwei Zhangd9861812019-02-13 11:51:55 -0800304 const sp<IGpuService> gpuService = getGpuService();
305 if (gpuService) {
306 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800307 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700308 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700309 isIntendedDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800310 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800311}
312
Adam Bodnar0afcca02019-09-17 13:23:17 -0700313bool GraphicsEnv::setInjectLayersPrSetDumpable() {
314 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
315 return false;
316 }
317 return true;
318}
319
Tim Van Patten5f744f12018-12-12 11:46:21 -0700320void* GraphicsEnv::loadLibrary(std::string name) {
321 const android_dlextinfo dlextinfo = {
322 .flags = ANDROID_DLEXT_USE_NAMESPACE,
323 .library_namespace = getAngleNamespace(),
324 };
325
326 std::string libName = std::string("lib") + name + "_angle.so";
327
328 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
329
330 if (so) {
331 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
332 return so;
333 } else {
334 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
335 }
336
337 return nullptr;
338}
339
340bool GraphicsEnv::checkAngleRules(void* so) {
341 char manufacturer[PROPERTY_VALUE_MAX];
342 char model[PROPERTY_VALUE_MAX];
343 property_get("ro.product.manufacturer", manufacturer, "UNSET");
344 property_get("ro.product.model", model, "UNSET");
345
346 auto ANGLEGetFeatureSupportUtilAPIVersion =
347 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
348 "ANGLEGetFeatureSupportUtilAPIVersion");
349
350 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
351 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
352 return false;
353 }
354
355 // Negotiate the interface version by requesting most recent known to the platform
356 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
357 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
358 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
359 "requested version %u",
360 versionToUse);
361 return false;
362 }
363
364 // Add and remove versions below as needed
365 bool useAngle = false;
366 switch (versionToUse) {
367 case 2: {
368 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
369 void* rulesHandle = nullptr;
370 int rulesVersion = 0;
371 void* systemInfoHandle = nullptr;
372
373 // Get the symbols for the feature-support-utility library:
374#define GET_SYMBOL(symbol) \
375 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
376 if (!symbol) { \
377 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
378 break; \
379 }
380 GET_SYMBOL(ANGLEAndroidParseRulesString);
381 GET_SYMBOL(ANGLEGetSystemInfo);
382 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
383 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
384 GET_SYMBOL(ANGLEFreeRulesHandle);
385 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
386
387 // Parse the rules, obtain the SystemInfo, and evaluate the
388 // application against the rules:
389 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
390 ALOGW("ANGLE feature-support library cannot parse rules file");
391 break;
392 }
393 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
394 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
395 break;
396 }
397 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) {
398 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
399 break;
400 }
401 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
402 systemInfoHandle, mAngleAppName.c_str());
403 (ANGLEFreeRulesHandle)(rulesHandle);
404 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
405 } break;
406
407 default:
408 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
409 }
410
411 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
412 return useAngle;
413}
414
415bool GraphicsEnv::shouldUseAngle(std::string appName) {
416 if (appName != mAngleAppName) {
417 // Make sure we are checking the app we were init'ed for
418 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
419 appName.c_str());
420 return false;
421 }
422
423 return shouldUseAngle();
424}
425
426bool GraphicsEnv::shouldUseAngle() {
427 // Make sure we are init'ed
428 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700429 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700430 return false;
431 }
432
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700433 return (mUseAngle == YES) ? true : false;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700434}
435
436void GraphicsEnv::updateUseAngle() {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700437 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700438
439 const char* ANGLE_PREFER_ANGLE = "angle";
440 const char* ANGLE_PREFER_NATIVE = "native";
441
442 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
443 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700444 mUseAngle = YES;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700445 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
446 ALOGV("User set \"Developer Options\" to force the use of Native");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700447 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700448 } else {
449 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
450 // load ANGLE and call the updatable opt-in/out logic:
Cody Northropc15d3822019-01-17 10:26:47 -0700451 void* featureSo = loadLibrary("feature_support");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700452 if (featureSo) {
453 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700454 mUseAngle = checkAngleRules(featureSo) ? YES : NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700455 dlclose(featureSo);
456 featureSo = nullptr;
457 } else {
458 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
459 }
460 }
461}
462
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600463void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700464 const std::string developerOptIn, const int rulesFd,
465 const long rulesOffset, const long rulesLength) {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700466 if (mUseAngle != UNKNOWN) {
467 // We've already figured out an answer for this app, so just return.
468 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
469 (mUseAngle == YES) ? "true" : "false");
470 return;
471 }
472
Tim Van Patten5f744f12018-12-12 11:46:21 -0700473 ALOGV("setting ANGLE path to '%s'", path.c_str());
474 mAnglePath = path;
475 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
476 mAngleAppName = appName;
477 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
478 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600479
Tim Van Patten5f744f12018-12-12 11:46:21 -0700480 lseek(rulesFd, rulesOffset, SEEK_SET);
481 mRulesBuffer = std::vector<char>(rulesLength + 1);
482 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
483 if (numBytesRead < 0) {
484 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
485 numBytesRead = 0;
486 } else if (numBytesRead == 0) {
487 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600488 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700489 if (numBytesRead != rulesLength) {
490 ALOGW("Did not read all of the necessary bytes from the rules file."
491 "expected: %ld, got: %zd",
492 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700493 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700494 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 10:34:58 -0600495
Tim Van Patten5f744f12018-12-12 11:46:21 -0700496 // Update the current status of whether we should use ANGLE or not
497 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600498}
499
Victor Khimenko4819b522018-07-13 17:24:18 +0200500void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600501 if (mLayerPaths.empty()) {
502 mLayerPaths = layerPaths;
503 mAppNamespace = appNamespace;
504 } else {
505 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800506 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600507 }
508}
509
Victor Khimenko4819b522018-07-13 17:24:18 +0200510NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600511 return mAppNamespace;
512}
513
Tim Van Patten5f744f12018-12-12 11:46:21 -0700514std::string& GraphicsEnv::getAngleAppName() {
515 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600516}
517
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600518const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600519 return mLayerPaths;
520}
521
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600522const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600523 return mDebugLayers;
524}
525
Cody Northropb9b01b62018-10-23 13:13:10 -0600526const std::string& GraphicsEnv::getDebugLayersGLES() {
527 return mDebugLayersGLES;
528}
529
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600530void GraphicsEnv::setDebugLayers(const std::string layers) {
531 mDebugLayers = layers;
532}
533
Cody Northropb9b01b62018-10-23 13:13:10 -0600534void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
535 mDebugLayersGLES = layers;
536}
537
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700538// Return true if all the required libraries from vndk and sphal namespace are
539// linked to the Game Driver namespace correctly.
540bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* vndkNamespace) {
541 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
542 if (llndkLibraries.empty()) {
543 return false;
544 }
545 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
546 ALOGE("Failed to link default namespace[%s]", dlerror());
547 return false;
548 }
549
550 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
551 if (vndkspLibraries.empty()) {
552 return false;
553 }
554 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
555 ALOGE("Failed to link vndk namespace[%s]", dlerror());
556 return false;
557 }
558
559 if (mSphalLibraries.empty()) {
560 return true;
561 }
562
563 // Make additional libraries in sphal to be accessible
564 auto sphalNamespace = android_get_exported_namespace("sphal");
565 if (!sphalNamespace) {
566 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
567 mSphalLibraries.c_str());
568 return false;
569 }
570
571 if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) {
572 ALOGE("Failed to link sphal namespace[%s]", dlerror());
573 return false;
574 }
575
576 return true;
577}
578
Jesse Hall53457db2016-12-14 16:54:06 -0800579android_namespace_t* GraphicsEnv::getDriverNamespace() {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700580 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Yiwei Zhang64d89212018-11-27 19:58:29 -0800581
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700582 if (mDriverNamespace) {
583 return mDriverNamespace;
584 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800585
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700586 if (mDriverPath.empty()) {
587 return nullptr;
588 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800589
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700590 auto vndkNamespace = android_get_exported_namespace("vndk");
591 if (!vndkNamespace) {
592 return nullptr;
593 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800594
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700595 mDriverNamespace = android_create_namespace("gfx driver",
596 mDriverPath.c_str(), // ld_library_path
597 mDriverPath.c_str(), // default_library_path
598 ANDROID_NAMESPACE_TYPE_ISOLATED,
599 nullptr, // permitted_when_isolated_path
600 nullptr);
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800601
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700602 if (!linkDriverNamespaceLocked(vndkNamespace)) {
603 mDriverNamespace = nullptr;
604 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800605
Jesse Hall53457db2016-12-14 16:54:06 -0800606 return mDriverNamespace;
607}
608
Peiyong Lindb3ed6e2020-01-09 18:43:27 -0800609std::string GraphicsEnv::getDriverPath() const {
610 return mDriverPath;
611}
612
Cody Northrop1f00e172018-04-02 11:23:31 -0600613android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700614 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600615
Cody Northrop3892cfa2019-01-30 10:03:12 -0700616 if (mAngleNamespace) {
617 return mAngleNamespace;
618 }
619
620 if (mAnglePath.empty()) {
621 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
622 return nullptr;
623 }
624
625 mAngleNamespace = android_create_namespace("ANGLE",
626 nullptr, // ld_library_path
627 mAnglePath.c_str(), // default_library_path
628 ANDROID_NAMESPACE_TYPE_SHARED |
629 ANDROID_NAMESPACE_TYPE_ISOLATED,
630 nullptr, // permitted_when_isolated_path
631 nullptr);
632
633 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600634
635 return mAngleNamespace;
636}
637
Jesse Hall90b25ed2016-12-12 12:56:46 -0800638} // namespace android