blob: d54de4999cc464c5e19b776532c9249be049ff8a [file] [log] [blame]
Jesse Hall90b25ed2016-12-12 20:56:461/*
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-07 04:22:5917#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Jesse Hall90b25ed2016-12-12 20:56:4619//#define LOG_NDEBUG 1
20#define LOG_TAG "GraphicsEnv"
Yiwei Zhangcb9d4e42019-02-07 04:22:5921
Jiyong Park27c39e12017-05-08 04:00:0222#include <graphicsenv/GraphicsEnv.h>
Jesse Hall90b25ed2016-12-12 20:56:4623
Yiwei Zhang64d89212018-11-28 03:58:2924#include <dlfcn.h>
Tim Van Patten5f744f12018-12-12 18:46:2125#include <unistd.h>
Yiwei Zhang64d89212018-11-28 03:58:2926
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-07 04:22:5931#include <binder/IServiceManager.h>
Yiwei Zhangcb9d4e42019-02-07 04:22:5932#include <graphicsenv/IGpuService.h>
Yiwei Zhang64d89212018-11-28 03:58:2933#include <log/log.h>
Yiwei Zhang49b9ac72019-08-05 23:57:1734#include <nativeloader/dlext_namespaces.h>
Cody Northrop629ce4e2018-10-15 13:22:0935#include <sys/prctl.h>
Yiwei Zhangcb9d4e42019-02-07 04:22:5936#include <utils/Trace.h>
Cody Northrop629ce4e2018-10-15 13:22:0937
Tim Van Patten5f744f12018-12-12 18:46:2138#include <memory>
Tim Van Patten5f744f12018-12-12 18:46:2139#include <string>
Yiwei Zhang3c74da92019-06-28 17:16:4940#include <thread>
Tim Van Patten5f744f12018-12-12 18:46:2141
Peiyong Lin0acbfdc2020-06-18 01:47:1242// TODO(b/159240322): Extend this to x86 ABI.
43#if defined(__LP64__)
44#define UPDATABLE_DRIVER_ABI "arm64-v8a"
45#else
46#define UPDATABLE_DRIVER_ABI "armeabi-v7a"
47#endif // defined(__LP64__)
48
Tim Van Patten5f744f12018-12-12 18:46:2149// TODO(ianelliott@): Get the following from an ANGLE header:
50#define CURRENT_ANGLE_API_VERSION 2 // Current API verion we are targetting
51// Version-2 API:
52typedef bool (*fpANGLEGetFeatureSupportUtilAPIVersion)(unsigned int* versionToUse);
53typedef bool (*fpANGLEAndroidParseRulesString)(const char* rulesString, void** rulesHandle,
54 int* rulesVersion);
55typedef bool (*fpANGLEGetSystemInfo)(void** handle);
56typedef bool (*fpANGLEAddDeviceInfoToSystemInfo)(const char* deviceMfr, const char* deviceModel,
57 void* handle);
58typedef bool (*fpANGLEShouldBeUsedForApplication)(void* rulesHandle, int rulesVersion,
59 void* systemInfoHandle, const char* appName);
60typedef bool (*fpANGLEFreeRulesHandle)(void* handle);
61typedef bool (*fpANGLEFreeSystemInfoHandle)(void* handle);
62
Jesse Hall90b25ed2016-12-12 20:56:4663namespace android {
64
Yiwei Zhang64d89212018-11-28 03:58:2965enum NativeLibrary {
66 LLNDK = 0,
67 VNDKSP = 1,
68};
69
Jooyung Han78396802020-02-22 18:02:4370static constexpr const char* kNativeLibrariesSystemConfigPath[] =
71 {"/apex/com.android.vndk.v{}/etc/llndk.libraries.{}.txt",
72 "/apex/com.android.vndk.v{}/etc/vndksp.libraries.{}.txt"};
Yiwei Zhang64d89212018-11-28 03:58:2973
74static std::string vndkVersionStr() {
75#ifdef __BIONIC__
Michael Hoisied1023f22020-03-27 05:52:5876 return base::GetProperty("ro.vndk.version", "");
Yiwei Zhang64d89212018-11-28 03:58:2977#endif
78 return "";
79}
80
81static void insertVndkVersionStr(std::string* fileName) {
82 LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr");
Jooyung Han78396802020-02-22 18:02:4383 std::string version = vndkVersionStr();
84 size_t pos = fileName->find("{}");
85 while (pos != std::string::npos) {
86 fileName->replace(pos, 2, version);
87 pos = fileName->find("{}", pos + version.size());
Yiwei Zhang64d89212018-11-28 03:58:2988 }
Yiwei Zhang64d89212018-11-28 03:58:2989}
90
91static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) {
92 // Read list of public native libraries from the config file.
93 std::string fileContent;
94 if (!base::ReadFileToString(configFile, &fileContent)) {
95 return false;
96 }
97
98 std::vector<std::string> lines = base::Split(fileContent, "\n");
99
100 for (auto& line : lines) {
101 auto trimmedLine = base::Trim(line);
102 if (!trimmedLine.empty()) {
103 soNames->push_back(trimmedLine);
104 }
105 }
106
107 return true;
108}
109
110static const std::string getSystemNativeLibraries(NativeLibrary type) {
Jooyung Han78396802020-02-22 18:02:43111 std::string nativeLibrariesSystemConfig = kNativeLibrariesSystemConfigPath[type];
Yiwei Zhang64d89212018-11-28 03:58:29112 insertVndkVersionStr(&nativeLibrariesSystemConfig);
113
114 std::vector<std::string> soNames;
115 if (!readConfig(nativeLibrariesSystemConfig, &soNames)) {
116 ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str());
117 return "";
118 }
119
120 return base::Join(soNames, ':');
121}
122
Jesse Hall90b25ed2016-12-12 20:56:46123/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
124 static GraphicsEnv env;
125 return env;
126}
127
Yiwei Zhang6a674c92019-11-08 19:55:36128bool GraphicsEnv::isDebuggable() {
129 return prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) > 0;
Cody Northrop629ce4e2018-10-15 13:22:09130}
131
Yiwei Zhang6ef84942019-02-14 20:28:12132void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string path,
133 const std::string sphalLibraries) {
134 if (!mDriverPath.empty() || !mSphalLibraries.empty()) {
135 ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries "
136 "from '%s' to '%s'",
137 mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 20:56:46138 return;
139 }
Yiwei Zhang6ef84942019-02-14 20:28:12140 ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(),
141 sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 20:56:46142 mDriverPath = path;
Yiwei Zhang6ef84942019-02-14 20:28:12143 mSphalLibraries = sphalLibraries;
Jesse Hall90b25ed2016-12-12 20:56:46144}
145
Yiwei Zhang5c640c12019-05-09 01:29:38146void GraphicsEnv::hintActivityLaunch() {
147 ATRACE_CALL();
148
Yiwei Zhangd3819382020-01-08 03:53:56149 {
150 std::lock_guard<std::mutex> lock(mStatsLock);
151 if (mActivityLaunched) return;
152 mActivityLaunched = true;
153 }
154
Yiwei Zhang3c74da92019-06-28 17:16:49155 std::thread trySendGpuStatsThread([this]() {
156 // If there's already graphics driver preloaded in the process, just send
157 // the stats info to GpuStats directly through async binder.
158 std::lock_guard<std::mutex> lock(mStatsLock);
159 if (mGpuStats.glDriverToSend) {
160 mGpuStats.glDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-03 01:10:55161 sendGpuStatsLocked(GpuStatsInfo::Api::API_GL, true, mGpuStats.glDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 17:16:49162 }
163 if (mGpuStats.vkDriverToSend) {
164 mGpuStats.vkDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-03 01:10:55165 sendGpuStatsLocked(GpuStatsInfo::Api::API_VK, true, mGpuStats.vkDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 17:16:49166 }
167 });
168 trySendGpuStatsThread.detach();
Yiwei Zhang5c640c12019-05-09 01:29:38169}
170
Greg Kaiser210bb7e2019-02-12 20:40:05171void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 19:51:55172 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang794d2952019-05-07 00:43:59173 int64_t driverBuildTime, const std::string& appPackageName,
174 const int vulkanVersion) {
Yiwei Zhangcb9d4e42019-02-07 04:22:59175 ATRACE_CALL();
176
Yiwei Zhangd9861812019-02-13 19:51:55177 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangcb9d4e42019-02-07 04:22:59178 ALOGV("setGpuStats:\n"
179 "\tdriverPackageName[%s]\n"
180 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-20 00:00:25181 "\tdriverVersionCode[%" PRIu64 "]\n"
182 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhang794d2952019-05-07 00:43:59183 "\tappPackageName[%s]\n"
184 "\tvulkanVersion[%d]\n",
Yiwei Zhang96c01712019-02-20 00:00:25185 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-07 00:43:59186 appPackageName.c_str(), vulkanVersion);
Yiwei Zhangcb9d4e42019-02-07 04:22:59187
Yiwei Zhangd9861812019-02-13 19:51:55188 mGpuStats.driverPackageName = driverPackageName;
189 mGpuStats.driverVersionName = driverVersionName;
190 mGpuStats.driverVersionCode = driverVersionCode;
Yiwei Zhang96c01712019-02-20 00:00:25191 mGpuStats.driverBuildTime = driverBuildTime;
Yiwei Zhangd9861812019-02-13 19:51:55192 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang794d2952019-05-07 00:43:59193 mGpuStats.vulkanVersion = vulkanVersion;
Yiwei Zhang8c8c1812019-02-05 02:56:38194}
195
Yiwei Zhang27ab3ac2019-07-03 01:10:55196void GraphicsEnv::setDriverToLoad(GpuStatsInfo::Driver driver) {
Yiwei Zhangd9861812019-02-13 19:51:55197 ATRACE_CALL();
198
199 std::lock_guard<std::mutex> lock(mStatsLock);
200 switch (driver) {
Yiwei Zhang27ab3ac2019-07-03 01:10:55201 case GpuStatsInfo::Driver::GL:
202 case GpuStatsInfo::Driver::GL_UPDATED:
203 case GpuStatsInfo::Driver::ANGLE: {
Yiwei Zhang472cab02019-08-06 00:57:41204 if (mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::NONE ||
205 mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::GL) {
Yiwei Zhangd9861812019-02-13 19:51:55206 mGpuStats.glDriverToLoad = driver;
207 break;
208 }
209
Yiwei Zhang27ab3ac2019-07-03 01:10:55210 if (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 19:51:55211 mGpuStats.glDriverFallback = driver;
212 }
213 break;
214 }
Yiwei Zhang27ab3ac2019-07-03 01:10:55215 case GpuStatsInfo::Driver::VULKAN:
216 case GpuStatsInfo::Driver::VULKAN_UPDATED: {
Yiwei Zhang472cab02019-08-06 00:57:41217 if (mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::NONE ||
218 mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::VULKAN) {
Yiwei Zhangd9861812019-02-13 19:51:55219 mGpuStats.vkDriverToLoad = driver;
220 break;
221 }
222
Yiwei Zhang27ab3ac2019-07-03 01:10:55223 if (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 19:51:55224 mGpuStats.vkDriverFallback = driver;
225 }
226 break;
227 }
228 default:
229 break;
230 }
231}
232
Yiwei Zhang27ab3ac2019-07-03 01:10:55233void GraphicsEnv::setDriverLoaded(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhang5c640c12019-05-09 01:29:38234 int64_t driverLoadingTime) {
Yiwei Zhangd9861812019-02-13 19:51:55235 ATRACE_CALL();
236
237 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhang27ab3ac2019-07-03 01:10:55238 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhangd3819382020-01-08 03:53:56239 mGpuStats.glDriverToSend = true;
Yiwei Zhang5c640c12019-05-09 01:29:38240 mGpuStats.glDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 19:51:55241 } else {
Yiwei Zhangd3819382020-01-08 03:53:56242 mGpuStats.vkDriverToSend = true;
Yiwei Zhang5c640c12019-05-09 01:29:38243 mGpuStats.vkDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 19:51:55244 }
245
Yiwei Zhang5c640c12019-05-09 01:29:38246 sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime);
Yiwei Zhangd9861812019-02-13 19:51:55247}
248
Yiwei Zhangd9861812019-02-13 19:51:55249static sp<IGpuService> getGpuService() {
Yiwei Zhang8e097302019-07-08 23:11:12250 static const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
Yiwei Zhangd9861812019-02-13 19:51:55251 if (!binder) {
252 ALOGE("Failed to get gpu service");
253 return nullptr;
254 }
255
256 return interface_cast<IGpuService>(binder);
257}
258
Yiwei Zhangd3819382020-01-08 03:53:56259bool GraphicsEnv::readyToSendGpuStatsLocked() {
260 // Only send stats for processes having at least one activity launched and that process doesn't
261 // skip the GraphicsEnvironment setup.
262 return mActivityLaunched && !mGpuStats.appPackageName.empty();
263}
264
Yiwei Zhangbcba4112019-07-03 20:39:32265void GraphicsEnv::setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value) {
Yiwei Zhang8c5e3bd2019-05-09 21:34:19266 ATRACE_CALL();
267
Yiwei Zhang8c5e3bd2019-05-09 21:34:19268 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangd3819382020-01-08 03:53:56269 if (!readyToSendGpuStatsLocked()) return;
270
Yiwei Zhang8c5e3bd2019-05-09 21:34:19271 const sp<IGpuService> gpuService = getGpuService();
272 if (gpuService) {
Yiwei Zhangbcba4112019-07-03 20:39:32273 gpuService->setTargetStats(mGpuStats.appPackageName, mGpuStats.driverVersionCode, stats,
274 value);
Yiwei Zhang8c5e3bd2019-05-09 21:34:19275 }
276}
277
Yiwei Zhang27ab3ac2019-07-03 01:10:55278void GraphicsEnv::sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhangd9861812019-02-13 19:51:55279 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-07 04:22:59280 ATRACE_CALL();
281
Yiwei Zhangd3819382020-01-08 03:53:56282 if (!readyToSendGpuStatsLocked()) return;
Yiwei Zhangcb9d4e42019-02-07 04:22:59283
284 ALOGV("sendGpuStats:\n"
285 "\tdriverPackageName[%s]\n"
286 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-20 00:00:25287 "\tdriverVersionCode[%" PRIu64 "]\n"
288 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 19:51:55289 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-07 00:43:59290 "\tvulkanVersion[%d]\n"
Yiwei Zhang5c640c12019-05-09 01:29:38291 "\tapi[%d]\n"
Yiwei Zhangd9861812019-02-13 19:51:55292 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-20 00:00:25293 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-07 04:22:59294 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-20 00:00:25295 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
Yiwei Zhang5c640c12019-05-09 01:29:38296 mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime);
297
Yiwei Zhang27ab3ac2019-07-03 01:10:55298 GpuStatsInfo::Driver driver = GpuStatsInfo::Driver::NONE;
Yiwei Zhang5c640c12019-05-09 01:29:38299 bool isIntendedDriverLoaded = false;
Yiwei Zhang27ab3ac2019-07-03 01:10:55300 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhang5c640c12019-05-09 01:29:38301 driver = mGpuStats.glDriverToLoad;
302 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-03 01:10:55303 isDriverLoaded && (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-09 01:29:38304 } else {
305 driver = mGpuStats.vkDriverToLoad;
306 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-03 01:10:55307 isDriverLoaded && (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-09 01:29:38308 }
Yiwei Zhangcb9d4e42019-02-07 04:22:59309
Yiwei Zhangd9861812019-02-13 19:51:55310 const sp<IGpuService> gpuService = getGpuService();
311 if (gpuService) {
312 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang96c01712019-02-20 00:00:25313 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang794d2952019-05-07 00:43:59314 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
Yiwei Zhang5c640c12019-05-09 01:29:38315 isIntendedDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-07 04:22:59316 }
Yiwei Zhangcb9d4e42019-02-07 04:22:59317}
318
Adam Bodnar0afcca02019-09-17 20:23:17319bool GraphicsEnv::setInjectLayersPrSetDumpable() {
320 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
321 return false;
322 }
323 return true;
324}
325
Tim Van Patten5f744f12018-12-12 18:46:21326void* GraphicsEnv::loadLibrary(std::string name) {
327 const android_dlextinfo dlextinfo = {
328 .flags = ANDROID_DLEXT_USE_NAMESPACE,
329 .library_namespace = getAngleNamespace(),
330 };
331
332 std::string libName = std::string("lib") + name + "_angle.so";
333
334 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
335
336 if (so) {
337 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
338 return so;
339 } else {
340 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
341 }
342
343 return nullptr;
344}
345
346bool GraphicsEnv::checkAngleRules(void* so) {
Michael Hoisied1023f22020-03-27 05:52:58347 auto manufacturer = base::GetProperty("ro.product.manufacturer", "UNSET");
348 auto model = base::GetProperty("ro.product.model", "UNSET");
Tim Van Patten5f744f12018-12-12 18:46:21349
350 auto ANGLEGetFeatureSupportUtilAPIVersion =
351 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
352 "ANGLEGetFeatureSupportUtilAPIVersion");
353
354 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
355 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
356 return false;
357 }
358
359 // Negotiate the interface version by requesting most recent known to the platform
360 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
361 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
362 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
363 "requested version %u",
364 versionToUse);
365 return false;
366 }
367
368 // Add and remove versions below as needed
369 bool useAngle = false;
370 switch (versionToUse) {
371 case 2: {
372 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
373 void* rulesHandle = nullptr;
374 int rulesVersion = 0;
375 void* systemInfoHandle = nullptr;
376
377 // Get the symbols for the feature-support-utility library:
378#define GET_SYMBOL(symbol) \
379 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
380 if (!symbol) { \
381 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
382 break; \
383 }
384 GET_SYMBOL(ANGLEAndroidParseRulesString);
385 GET_SYMBOL(ANGLEGetSystemInfo);
386 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
387 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
388 GET_SYMBOL(ANGLEFreeRulesHandle);
389 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
390
391 // Parse the rules, obtain the SystemInfo, and evaluate the
392 // application against the rules:
393 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
394 ALOGW("ANGLE feature-support library cannot parse rules file");
395 break;
396 }
397 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
398 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
399 break;
400 }
Michael Hoisied1023f22020-03-27 05:52:58401 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer.c_str(), model.c_str(),
402 systemInfoHandle)) {
Tim Van Patten5f744f12018-12-12 18:46:21403 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
404 break;
405 }
406 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
407 systemInfoHandle, mAngleAppName.c_str());
408 (ANGLEFreeRulesHandle)(rulesHandle);
409 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
410 } break;
411
412 default:
413 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
414 }
415
416 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
417 return useAngle;
418}
419
420bool GraphicsEnv::shouldUseAngle(std::string appName) {
421 if (appName != mAngleAppName) {
422 // Make sure we are checking the app we were init'ed for
423 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
424 appName.c_str());
425 return false;
426 }
427
428 return shouldUseAngle();
429}
430
431bool GraphicsEnv::shouldUseAngle() {
432 // Make sure we are init'ed
433 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 23:55:03434 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 18:46:21435 return false;
436 }
437
Tim Van Patten8bd24e92019-02-08 17:16:40438 return (mUseAngle == YES) ? true : false;
Tim Van Patten5f744f12018-12-12 18:46:21439}
440
441void GraphicsEnv::updateUseAngle() {
Tim Van Patten8bd24e92019-02-08 17:16:40442 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 18:46:21443
444 const char* ANGLE_PREFER_ANGLE = "angle";
445 const char* ANGLE_PREFER_NATIVE = "native";
446
447 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
448 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Tim Van Patten8bd24e92019-02-08 17:16:40449 mUseAngle = YES;
Tim Van Patten5f744f12018-12-12 18:46:21450 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
451 ALOGV("User set \"Developer Options\" to force the use of Native");
Tim Van Patten8bd24e92019-02-08 17:16:40452 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 18:46:21453 } else {
454 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
455 // load ANGLE and call the updatable opt-in/out logic:
Cody Northropc15d3822019-01-17 17:26:47456 void* featureSo = loadLibrary("feature_support");
Tim Van Patten5f744f12018-12-12 18:46:21457 if (featureSo) {
458 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Tim Van Patten8bd24e92019-02-08 17:16:40459 mUseAngle = checkAngleRules(featureSo) ? YES : NO;
Tim Van Patten5f744f12018-12-12 18:46:21460 dlclose(featureSo);
461 featureSo = nullptr;
462 } else {
463 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
464 }
465 }
466}
467
Courtney Goeltzenleuchterd41ef252018-09-26 20:37:42468void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Peiyong Lin2f707e62020-09-26 20:52:10469 const std::string developerOptIn,
470 const std::vector<std::string> eglFeatures, const int rulesFd,
Tim Van Pattena2a60a02018-11-09 23:51:15471 const long rulesOffset, const long rulesLength) {
Tim Van Patten8bd24e92019-02-08 17:16:40472 if (mUseAngle != UNKNOWN) {
473 // We've already figured out an answer for this app, so just return.
474 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
475 (mUseAngle == YES) ? "true" : "false");
476 return;
477 }
478
Peiyong Lin2f707e62020-09-26 20:52:10479 mAngleEglFeatures = std::move(eglFeatures);
480
Tim Van Patten5f744f12018-12-12 18:46:21481 ALOGV("setting ANGLE path to '%s'", path.c_str());
482 mAnglePath = path;
483 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
484 mAngleAppName = appName;
485 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
486 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 20:37:42487
Tim Van Patten5f744f12018-12-12 18:46:21488 lseek(rulesFd, rulesOffset, SEEK_SET);
489 mRulesBuffer = std::vector<char>(rulesLength + 1);
490 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
491 if (numBytesRead < 0) {
492 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
493 numBytesRead = 0;
494 } else if (numBytesRead == 0) {
495 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 20:37:42496 }
Tim Van Patten5f744f12018-12-12 18:46:21497 if (numBytesRead != rulesLength) {
498 ALOGW("Did not read all of the necessary bytes from the rules file."
499 "expected: %ld, got: %zd",
500 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 23:51:15501 }
Tim Van Patten5f744f12018-12-12 18:46:21502 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 16:34:58503
Tim Van Patten5f744f12018-12-12 18:46:21504 // Update the current status of whether we should use ANGLE or not
505 updateUseAngle();
Cody Northrop1f00e172018-04-02 17:23:31506}
507
Victor Khimenko4819b522018-07-13 15:24:18508void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 15:01:53509 if (mLayerPaths.empty()) {
510 mLayerPaths = layerPaths;
511 mAppNamespace = appNamespace;
512 } else {
513 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-28 03:58:29514 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 15:01:53515 }
516}
517
Victor Khimenko4819b522018-07-13 15:24:18518NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 15:01:53519 return mAppNamespace;
520}
521
Tim Van Patten5f744f12018-12-12 18:46:21522std::string& GraphicsEnv::getAngleAppName() {
523 return mAngleAppName;
Cody Northrop04e70432018-09-06 16:34:58524}
525
Peiyong Lin2f707e62020-09-26 20:52:10526const std::vector<std::string>& GraphicsEnv::getAngleEglFeatures() {
527 return mAngleEglFeatures;
528}
529
Courtney Goeltzenleuchter30ad2ab2018-10-30 14:20:44530const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 15:01:53531 return mLayerPaths;
532}
533
Courtney Goeltzenleuchter30ad2ab2018-10-30 14:20:44534const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 15:01:53535 return mDebugLayers;
536}
537
Cody Northropb9b01b62018-10-23 19:13:10538const std::string& GraphicsEnv::getDebugLayersGLES() {
539 return mDebugLayersGLES;
540}
541
Cody Northropd2aa3ab2017-10-20 15:01:53542void GraphicsEnv::setDebugLayers(const std::string layers) {
543 mDebugLayers = layers;
544}
545
Cody Northropb9b01b62018-10-23 19:13:10546void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
547 mDebugLayersGLES = layers;
548}
549
Yiwei Zhang5e21eb32019-06-05 07:26:03550// Return true if all the required libraries from vndk and sphal namespace are
Yiwei Zhang9058b8f2020-11-12 20:23:00551// linked to the updatable gfx driver namespace correctly.
Yiwei Zhang5e21eb32019-06-05 07:26:03552bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* vndkNamespace) {
553 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
554 if (llndkLibraries.empty()) {
555 return false;
556 }
557 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
558 ALOGE("Failed to link default namespace[%s]", dlerror());
559 return false;
560 }
561
562 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
563 if (vndkspLibraries.empty()) {
564 return false;
565 }
566 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
567 ALOGE("Failed to link vndk namespace[%s]", dlerror());
568 return false;
569 }
570
571 if (mSphalLibraries.empty()) {
572 return true;
573 }
574
575 // Make additional libraries in sphal to be accessible
576 auto sphalNamespace = android_get_exported_namespace("sphal");
577 if (!sphalNamespace) {
578 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
579 mSphalLibraries.c_str());
580 return false;
581 }
582
583 if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) {
584 ALOGE("Failed to link sphal namespace[%s]", dlerror());
585 return false;
586 }
587
588 return true;
589}
590
Jesse Hall53457db2016-12-15 00:54:06591android_namespace_t* GraphicsEnv::getDriverNamespace() {
Yiwei Zhang5e21eb32019-06-05 07:26:03592 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Yiwei Zhang64d89212018-11-28 03:58:29593
Yiwei Zhang5e21eb32019-06-05 07:26:03594 if (mDriverNamespace) {
595 return mDriverNamespace;
596 }
Yiwei Zhang64d89212018-11-28 03:58:29597
Yiwei Zhang5e21eb32019-06-05 07:26:03598 if (mDriverPath.empty()) {
Peiyong Lin0acbfdc2020-06-18 01:47:12599 // For an application process, driver path is empty means this application is not opted in
600 // to use updatable driver. Application process doesn't have the ability to set up
601 // environment variables and hence before `getenv` call will return.
602 // For a process that is not an application process, if it's run from an environment,
603 // for example shell, where environment variables can be set, then it can opt into using
604 // udpatable driver by setting UPDATABLE_GFX_DRIVER to 1. By setting to 1 the developer
605 // driver will be used currently.
606 // TODO(b/159240322) Support the production updatable driver.
607 const char* id = getenv("UPDATABLE_GFX_DRIVER");
608 if (id == nullptr || std::strcmp(id, "1")) {
609 return nullptr;
610 }
611 const sp<IGpuService> gpuService = getGpuService();
612 if (!gpuService) {
613 return nullptr;
614 }
615 mDriverPath = gpuService->getUpdatableDriverPath();
616 if (mDriverPath.empty()) {
617 return nullptr;
618 }
619 mDriverPath.append(UPDATABLE_DRIVER_ABI);
620 ALOGI("Driver path is setup via UPDATABLE_GFX_DRIVER: %s", mDriverPath.c_str());
Yiwei Zhang5e21eb32019-06-05 07:26:03621 }
Yiwei Zhang64d89212018-11-28 03:58:29622
Yiwei Zhang5e21eb32019-06-05 07:26:03623 auto vndkNamespace = android_get_exported_namespace("vndk");
624 if (!vndkNamespace) {
625 return nullptr;
626 }
Yiwei Zhang64d89212018-11-28 03:58:29627
Yiwei Zhang5e21eb32019-06-05 07:26:03628 mDriverNamespace = android_create_namespace("gfx driver",
629 mDriverPath.c_str(), // ld_library_path
630 mDriverPath.c_str(), // default_library_path
631 ANDROID_NAMESPACE_TYPE_ISOLATED,
632 nullptr, // permitted_when_isolated_path
633 nullptr);
Yiwei Zhang6ef84942019-02-14 20:28:12634
Yiwei Zhang5e21eb32019-06-05 07:26:03635 if (!linkDriverNamespaceLocked(vndkNamespace)) {
636 mDriverNamespace = nullptr;
637 }
Yiwei Zhang64d89212018-11-28 03:58:29638
Jesse Hall53457db2016-12-15 00:54:06639 return mDriverNamespace;
640}
641
Peiyong Lindb3ed6e2020-01-10 02:43:27642std::string GraphicsEnv::getDriverPath() const {
643 return mDriverPath;
644}
645
Cody Northrop1f00e172018-04-02 17:23:31646android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 17:03:12647 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 17:23:31648
Cody Northrop3892cfa2019-01-30 17:03:12649 if (mAngleNamespace) {
650 return mAngleNamespace;
651 }
652
653 if (mAnglePath.empty()) {
654 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
655 return nullptr;
656 }
657
658 mAngleNamespace = android_create_namespace("ANGLE",
659 nullptr, // ld_library_path
660 mAnglePath.c_str(), // default_library_path
Yiwei Zhang5b7604f2020-05-08 21:23:34661 ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED,
Cody Northrop3892cfa2019-01-30 17:03:12662 nullptr, // permitted_when_isolated_path
663 nullptr);
664
665 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 17:23:31666
667 return mAngleNamespace;
668}
669
Jesse Hall90b25ed2016-12-12 20:56:46670} // namespace android