blob: e49fb0859e5af7c9e6b1ce5e434e6290351eceec [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 Zhangcb9d4e42019-02-06 20:22:59 -080032#include <graphicsenv/IGpuService.h>
Yiwei Zhang64d89212018-11-27 19:58:29 -080033#include <log/log.h>
Yiwei Zhang49b9ac72019-08-05 16:57:17 -070034#include <nativeloader/dlext_namespaces.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
Tim Van Patten5f744f12018-12-12 11:46:21 -070042// TODO(ianelliott@): Get the following from an ANGLE header:
43#define CURRENT_ANGLE_API_VERSION 2 // Current API verion we are targetting
44// Version-2 API:
45typedef bool (*fpANGLEGetFeatureSupportUtilAPIVersion)(unsigned int* versionToUse);
46typedef bool (*fpANGLEAndroidParseRulesString)(const char* rulesString, void** rulesHandle,
47 int* rulesVersion);
48typedef bool (*fpANGLEGetSystemInfo)(void** handle);
49typedef bool (*fpANGLEAddDeviceInfoToSystemInfo)(const char* deviceMfr, const char* deviceModel,
50 void* handle);
51typedef bool (*fpANGLEShouldBeUsedForApplication)(void* rulesHandle, int rulesVersion,
52 void* systemInfoHandle, const char* appName);
53typedef bool (*fpANGLEFreeRulesHandle)(void* handle);
54typedef bool (*fpANGLEFreeSystemInfoHandle)(void* handle);
55
Jesse Hall90b25ed2016-12-12 12:56:46 -080056namespace android {
57
Yiwei Zhang64d89212018-11-27 19:58:29 -080058enum NativeLibrary {
59 LLNDK = 0,
60 VNDKSP = 1,
61};
62
Jooyung Han78396802020-02-23 03:02:43 +090063static constexpr const char* kNativeLibrariesSystemConfigPath[] =
64 {"/apex/com.android.vndk.v{}/etc/llndk.libraries.{}.txt",
65 "/apex/com.android.vndk.v{}/etc/vndksp.libraries.{}.txt"};
Yiwei Zhang64d89212018-11-27 19:58:29 -080066
67static std::string vndkVersionStr() {
68#ifdef __BIONIC__
Michael Hoisied1023f22020-03-26 22:52:58 -070069 return base::GetProperty("ro.vndk.version", "");
Yiwei Zhang64d89212018-11-27 19:58:29 -080070#endif
71 return "";
72}
73
74static void insertVndkVersionStr(std::string* fileName) {
75 LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr");
Jooyung Han78396802020-02-23 03:02:43 +090076 std::string version = vndkVersionStr();
77 size_t pos = fileName->find("{}");
78 while (pos != std::string::npos) {
79 fileName->replace(pos, 2, version);
80 pos = fileName->find("{}", pos + version.size());
Yiwei Zhang64d89212018-11-27 19:58:29 -080081 }
Yiwei Zhang64d89212018-11-27 19:58:29 -080082}
83
84static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) {
85 // Read list of public native libraries from the config file.
86 std::string fileContent;
87 if (!base::ReadFileToString(configFile, &fileContent)) {
88 return false;
89 }
90
91 std::vector<std::string> lines = base::Split(fileContent, "\n");
92
93 for (auto& line : lines) {
94 auto trimmedLine = base::Trim(line);
95 if (!trimmedLine.empty()) {
96 soNames->push_back(trimmedLine);
97 }
98 }
99
100 return true;
101}
102
103static const std::string getSystemNativeLibraries(NativeLibrary type) {
Jooyung Han78396802020-02-23 03:02:43 +0900104 std::string nativeLibrariesSystemConfig = kNativeLibrariesSystemConfigPath[type];
Yiwei Zhang64d89212018-11-27 19:58:29 -0800105 insertVndkVersionStr(&nativeLibrariesSystemConfig);
106
107 std::vector<std::string> soNames;
108 if (!readConfig(nativeLibrariesSystemConfig, &soNames)) {
109 ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str());
110 return "";
111 }
112
113 return base::Join(soNames, ':');
114}
115
Jesse Hall90b25ed2016-12-12 12:56:46 -0800116/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
117 static GraphicsEnv env;
118 return env;
119}
120
Yiwei Zhang6a674c92019-11-08 11:55:36 -0800121bool GraphicsEnv::isDebuggable() {
122 return prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) > 0;
Cody Northrop629ce4e2018-10-15 07:22:09 -0600123}
124
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800125void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string path,
126 const std::string sphalLibraries) {
127 if (!mDriverPath.empty() || !mSphalLibraries.empty()) {
128 ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries "
129 "from '%s' to '%s'",
130 mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800131 return;
132 }
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800133 ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(),
134 sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800135 mDriverPath = path;
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800136 mSphalLibraries = sphalLibraries;
Jesse Hall90b25ed2016-12-12 12:56:46 -0800137}
138
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700139void GraphicsEnv::hintActivityLaunch() {
140 ATRACE_CALL();
141
Yiwei Zhangd3819382020-01-07 19:53:56 -0800142 {
143 std::lock_guard<std::mutex> lock(mStatsLock);
144 if (mActivityLaunched) return;
145 mActivityLaunched = true;
146 }
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 Zhang27ab3ac2019-07-02 18:10:55 -0700231 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhangd3819382020-01-07 19:53:56 -0800232 mGpuStats.glDriverToSend = true;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700233 mGpuStats.glDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800234 } else {
Yiwei Zhangd3819382020-01-07 19:53:56 -0800235 mGpuStats.vkDriverToSend = true;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700236 mGpuStats.vkDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800237 }
238
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700239 sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800240}
241
Yiwei Zhangd9861812019-02-13 11:51:55 -0800242static sp<IGpuService> getGpuService() {
Yiwei Zhang8e097302019-07-08 16:11:12 -0700243 static const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
Yiwei Zhangd9861812019-02-13 11:51:55 -0800244 if (!binder) {
245 ALOGE("Failed to get gpu service");
246 return nullptr;
247 }
248
249 return interface_cast<IGpuService>(binder);
250}
251
Yiwei Zhangd3819382020-01-07 19:53:56 -0800252bool GraphicsEnv::readyToSendGpuStatsLocked() {
253 // Only send stats for processes having at least one activity launched and that process doesn't
254 // skip the GraphicsEnvironment setup.
255 return mActivityLaunched && !mGpuStats.appPackageName.empty();
256}
257
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700258void GraphicsEnv::setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value) {
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700259 ATRACE_CALL();
260
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700261 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangd3819382020-01-07 19:53:56 -0800262 if (!readyToSendGpuStatsLocked()) return;
263
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700264 const sp<IGpuService> gpuService = getGpuService();
265 if (gpuService) {
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700266 gpuService->setTargetStats(mGpuStats.appPackageName, mGpuStats.driverVersionCode, stats,
267 value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700268 }
269}
270
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700271void GraphicsEnv::sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800272 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800273 ATRACE_CALL();
274
Yiwei Zhangd3819382020-01-07 19:53:56 -0800275 if (!readyToSendGpuStatsLocked()) return;
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800276
277 ALOGV("sendGpuStats:\n"
278 "\tdriverPackageName[%s]\n"
279 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800280 "\tdriverVersionCode[%" PRIu64 "]\n"
281 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800282 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700283 "\tvulkanVersion[%d]\n"
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700284 "\tapi[%d]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800285 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800286 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800287 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-19 16:00:25 -0800288 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700289 mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime);
290
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700291 GpuStatsInfo::Driver driver = GpuStatsInfo::Driver::NONE;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700292 bool isIntendedDriverLoaded = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700293 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700294 driver = mGpuStats.glDriverToLoad;
295 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700296 isDriverLoaded && (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700297 } else {
298 driver = mGpuStats.vkDriverToLoad;
299 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700300 isDriverLoaded && (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700301 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800302
Yiwei Zhangd9861812019-02-13 11:51:55 -0800303 const sp<IGpuService> gpuService = getGpuService();
304 if (gpuService) {
305 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800306 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700307 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700308 isIntendedDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800309 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800310}
311
Adam Bodnar0afcca02019-09-17 13:23:17 -0700312bool GraphicsEnv::setInjectLayersPrSetDumpable() {
313 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
314 return false;
315 }
316 return true;
317}
318
Tim Van Patten5f744f12018-12-12 11:46:21 -0700319void* GraphicsEnv::loadLibrary(std::string name) {
320 const android_dlextinfo dlextinfo = {
321 .flags = ANDROID_DLEXT_USE_NAMESPACE,
322 .library_namespace = getAngleNamespace(),
323 };
324
325 std::string libName = std::string("lib") + name + "_angle.so";
326
327 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
328
329 if (so) {
330 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
331 return so;
332 } else {
333 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
334 }
335
336 return nullptr;
337}
338
339bool GraphicsEnv::checkAngleRules(void* so) {
Michael Hoisied1023f22020-03-26 22:52:58 -0700340 auto manufacturer = base::GetProperty("ro.product.manufacturer", "UNSET");
341 auto model = base::GetProperty("ro.product.model", "UNSET");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700342
343 auto ANGLEGetFeatureSupportUtilAPIVersion =
344 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
345 "ANGLEGetFeatureSupportUtilAPIVersion");
346
347 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
348 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
349 return false;
350 }
351
352 // Negotiate the interface version by requesting most recent known to the platform
353 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
354 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
355 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
356 "requested version %u",
357 versionToUse);
358 return false;
359 }
360
361 // Add and remove versions below as needed
362 bool useAngle = false;
363 switch (versionToUse) {
364 case 2: {
365 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
366 void* rulesHandle = nullptr;
367 int rulesVersion = 0;
368 void* systemInfoHandle = nullptr;
369
370 // Get the symbols for the feature-support-utility library:
371#define GET_SYMBOL(symbol) \
372 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
373 if (!symbol) { \
374 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
375 break; \
376 }
377 GET_SYMBOL(ANGLEAndroidParseRulesString);
378 GET_SYMBOL(ANGLEGetSystemInfo);
379 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
380 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
381 GET_SYMBOL(ANGLEFreeRulesHandle);
382 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
383
384 // Parse the rules, obtain the SystemInfo, and evaluate the
385 // application against the rules:
386 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
387 ALOGW("ANGLE feature-support library cannot parse rules file");
388 break;
389 }
390 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
391 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
392 break;
393 }
Michael Hoisied1023f22020-03-26 22:52:58 -0700394 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer.c_str(), model.c_str(),
395 systemInfoHandle)) {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700396 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
397 break;
398 }
399 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
400 systemInfoHandle, mAngleAppName.c_str());
401 (ANGLEFreeRulesHandle)(rulesHandle);
402 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
403 } break;
404
405 default:
406 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
407 }
408
409 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
410 return useAngle;
411}
412
413bool GraphicsEnv::shouldUseAngle(std::string appName) {
414 if (appName != mAngleAppName) {
415 // Make sure we are checking the app we were init'ed for
416 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
417 appName.c_str());
418 return false;
419 }
420
421 return shouldUseAngle();
422}
423
424bool GraphicsEnv::shouldUseAngle() {
425 // Make sure we are init'ed
426 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700427 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700428 return false;
429 }
430
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700431 return (mUseAngle == YES) ? true : false;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700432}
433
434void GraphicsEnv::updateUseAngle() {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700435 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700436
437 const char* ANGLE_PREFER_ANGLE = "angle";
438 const char* ANGLE_PREFER_NATIVE = "native";
439
440 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
441 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700442 mUseAngle = YES;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700443 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
444 ALOGV("User set \"Developer Options\" to force the use of Native");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700445 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700446 } else {
447 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
448 // load ANGLE and call the updatable opt-in/out logic:
Cody Northropc15d3822019-01-17 10:26:47 -0700449 void* featureSo = loadLibrary("feature_support");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700450 if (featureSo) {
451 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700452 mUseAngle = checkAngleRules(featureSo) ? YES : NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700453 dlclose(featureSo);
454 featureSo = nullptr;
455 } else {
456 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
457 }
458 }
459}
460
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600461void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700462 const std::string developerOptIn, const int rulesFd,
463 const long rulesOffset, const long rulesLength) {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700464 if (mUseAngle != UNKNOWN) {
465 // We've already figured out an answer for this app, so just return.
466 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
467 (mUseAngle == YES) ? "true" : "false");
468 return;
469 }
470
Tim Van Patten5f744f12018-12-12 11:46:21 -0700471 ALOGV("setting ANGLE path to '%s'", path.c_str());
472 mAnglePath = path;
473 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
474 mAngleAppName = appName;
475 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
476 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600477
Tim Van Patten5f744f12018-12-12 11:46:21 -0700478 lseek(rulesFd, rulesOffset, SEEK_SET);
479 mRulesBuffer = std::vector<char>(rulesLength + 1);
480 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
481 if (numBytesRead < 0) {
482 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
483 numBytesRead = 0;
484 } else if (numBytesRead == 0) {
485 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600486 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700487 if (numBytesRead != rulesLength) {
488 ALOGW("Did not read all of the necessary bytes from the rules file."
489 "expected: %ld, got: %zd",
490 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700491 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700492 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 10:34:58 -0600493
Tim Van Patten5f744f12018-12-12 11:46:21 -0700494 // Update the current status of whether we should use ANGLE or not
495 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600496}
497
Victor Khimenko4819b522018-07-13 17:24:18 +0200498void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600499 if (mLayerPaths.empty()) {
500 mLayerPaths = layerPaths;
501 mAppNamespace = appNamespace;
502 } else {
503 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800504 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600505 }
506}
507
Victor Khimenko4819b522018-07-13 17:24:18 +0200508NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600509 return mAppNamespace;
510}
511
Tim Van Patten5f744f12018-12-12 11:46:21 -0700512std::string& GraphicsEnv::getAngleAppName() {
513 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600514}
515
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600516const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600517 return mLayerPaths;
518}
519
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600520const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600521 return mDebugLayers;
522}
523
Cody Northropb9b01b62018-10-23 13:13:10 -0600524const std::string& GraphicsEnv::getDebugLayersGLES() {
525 return mDebugLayersGLES;
526}
527
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600528void GraphicsEnv::setDebugLayers(const std::string layers) {
529 mDebugLayers = layers;
530}
531
Cody Northropb9b01b62018-10-23 13:13:10 -0600532void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
533 mDebugLayersGLES = layers;
534}
535
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700536// Return true if all the required libraries from vndk and sphal namespace are
537// linked to the Game Driver namespace correctly.
538bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* vndkNamespace) {
539 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
540 if (llndkLibraries.empty()) {
541 return false;
542 }
543 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
544 ALOGE("Failed to link default namespace[%s]", dlerror());
545 return false;
546 }
547
548 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
549 if (vndkspLibraries.empty()) {
550 return false;
551 }
552 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
553 ALOGE("Failed to link vndk namespace[%s]", dlerror());
554 return false;
555 }
556
557 if (mSphalLibraries.empty()) {
558 return true;
559 }
560
561 // Make additional libraries in sphal to be accessible
562 auto sphalNamespace = android_get_exported_namespace("sphal");
563 if (!sphalNamespace) {
564 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
565 mSphalLibraries.c_str());
566 return false;
567 }
568
569 if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) {
570 ALOGE("Failed to link sphal namespace[%s]", dlerror());
571 return false;
572 }
573
574 return true;
575}
576
Jesse Hall53457db2016-12-14 16:54:06 -0800577android_namespace_t* GraphicsEnv::getDriverNamespace() {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700578 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Yiwei Zhang64d89212018-11-27 19:58:29 -0800579
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700580 if (mDriverNamespace) {
581 return mDriverNamespace;
582 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800583
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700584 if (mDriverPath.empty()) {
585 return nullptr;
586 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800587
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700588 auto vndkNamespace = android_get_exported_namespace("vndk");
589 if (!vndkNamespace) {
590 return nullptr;
591 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800592
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700593 mDriverNamespace = android_create_namespace("gfx driver",
594 mDriverPath.c_str(), // ld_library_path
595 mDriverPath.c_str(), // default_library_path
596 ANDROID_NAMESPACE_TYPE_ISOLATED,
597 nullptr, // permitted_when_isolated_path
598 nullptr);
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800599
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700600 if (!linkDriverNamespaceLocked(vndkNamespace)) {
601 mDriverNamespace = nullptr;
602 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800603
Jesse Hall53457db2016-12-14 16:54:06 -0800604 return mDriverNamespace;
605}
606
Peiyong Lindb3ed6e2020-01-09 18:43:27 -0800607std::string GraphicsEnv::getDriverPath() const {
608 return mDriverPath;
609}
610
Cody Northrop1f00e172018-04-02 11:23:31 -0600611android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700612 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600613
Cody Northrop3892cfa2019-01-30 10:03:12 -0700614 if (mAngleNamespace) {
615 return mAngleNamespace;
616 }
617
618 if (mAnglePath.empty()) {
619 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
620 return nullptr;
621 }
622
623 mAngleNamespace = android_create_namespace("ANGLE",
624 nullptr, // ld_library_path
625 mAnglePath.c_str(), // default_library_path
626 ANDROID_NAMESPACE_TYPE_SHARED |
627 ANDROID_NAMESPACE_TYPE_ISOLATED,
628 nullptr, // permitted_when_isolated_path
629 nullptr);
630
631 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600632
633 return mAngleNamespace;
634}
635
Jesse Hall90b25ed2016-12-12 12:56:46 -0800636} // namespace android