blob: 3d0f8bbb117bdfdb636ef146b4b47a249040c6ed [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
Peiyong Lin6b388252020-06-17 18:47:12 -070043// TODO(b/159240322): Extend this to x86 ABI.
44#if defined(__LP64__)
45#define UPDATABLE_DRIVER_ABI "arm64-v8a"
46#else
47#define UPDATABLE_DRIVER_ABI "armeabi-v7a"
48#endif // defined(__LP64__)
49
Tim Van Patten5f744f12018-12-12 11:46:21 -070050// TODO(ianelliott@): Get the following from an ANGLE header:
51#define CURRENT_ANGLE_API_VERSION 2 // Current API verion we are targetting
52// Version-2 API:
53typedef bool (*fpANGLEGetFeatureSupportUtilAPIVersion)(unsigned int* versionToUse);
54typedef bool (*fpANGLEAndroidParseRulesString)(const char* rulesString, void** rulesHandle,
55 int* rulesVersion);
56typedef bool (*fpANGLEGetSystemInfo)(void** handle);
57typedef bool (*fpANGLEAddDeviceInfoToSystemInfo)(const char* deviceMfr, const char* deviceModel,
58 void* handle);
59typedef bool (*fpANGLEShouldBeUsedForApplication)(void* rulesHandle, int rulesVersion,
60 void* systemInfoHandle, const char* appName);
61typedef bool (*fpANGLEFreeRulesHandle)(void* handle);
62typedef bool (*fpANGLEFreeSystemInfoHandle)(void* handle);
63
Jesse Hall90b25ed2016-12-12 12:56:46 -080064namespace android {
65
Yiwei Zhang64d89212018-11-27 19:58:29 -080066enum NativeLibrary {
67 LLNDK = 0,
68 VNDKSP = 1,
69};
70
Jooyung Han1f7c37f2020-02-23 03:02:43 +090071static constexpr const char* kNativeLibrariesSystemConfigPath[] =
72 {"/apex/com.android.vndk.v{}/etc/llndk.libraries.{}.txt",
73 "/apex/com.android.vndk.v{}/etc/vndksp.libraries.{}.txt"};
Yiwei Zhang64d89212018-11-27 19:58:29 -080074
75static std::string vndkVersionStr() {
76#ifdef __BIONIC__
Jooyung Han1f7c37f2020-02-23 03:02:43 +090077 return android::base::GetProperty("ro.vndk.version", "");
Yiwei Zhang64d89212018-11-27 19:58:29 -080078#endif
79 return "";
80}
81
82static void insertVndkVersionStr(std::string* fileName) {
83 LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr");
Jooyung Han1f7c37f2020-02-23 03:02:43 +090084 std::string version = vndkVersionStr();
85 size_t pos = fileName->find("{}");
86 while (pos != std::string::npos) {
87 fileName->replace(pos, 2, version);
88 pos = fileName->find("{}", pos + version.size());
Yiwei Zhang64d89212018-11-27 19:58:29 -080089 }
Yiwei Zhang64d89212018-11-27 19:58:29 -080090}
91
92static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) {
93 // Read list of public native libraries from the config file.
94 std::string fileContent;
95 if (!base::ReadFileToString(configFile, &fileContent)) {
96 return false;
97 }
98
99 std::vector<std::string> lines = base::Split(fileContent, "\n");
100
101 for (auto& line : lines) {
102 auto trimmedLine = base::Trim(line);
103 if (!trimmedLine.empty()) {
104 soNames->push_back(trimmedLine);
105 }
106 }
107
108 return true;
109}
110
111static const std::string getSystemNativeLibraries(NativeLibrary type) {
Jooyung Han1f7c37f2020-02-23 03:02:43 +0900112 std::string nativeLibrariesSystemConfig = kNativeLibrariesSystemConfigPath[type];
Yiwei Zhang64d89212018-11-27 19:58:29 -0800113 insertVndkVersionStr(&nativeLibrariesSystemConfig);
114
115 std::vector<std::string> soNames;
116 if (!readConfig(nativeLibrariesSystemConfig, &soNames)) {
117 ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str());
118 return "";
119 }
120
121 return base::Join(soNames, ':');
122}
123
Jesse Hall90b25ed2016-12-12 12:56:46 -0800124/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
125 static GraphicsEnv env;
126 return env;
127}
128
Yiwei Zhang6a674c92019-11-08 11:55:36 -0800129bool GraphicsEnv::isDebuggable() {
130 return prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) > 0;
Cody Northrop629ce4e2018-10-15 07:22:09 -0600131}
132
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800133void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string path,
134 const std::string sphalLibraries) {
135 if (!mDriverPath.empty() || !mSphalLibraries.empty()) {
136 ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries "
137 "from '%s' to '%s'",
138 mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800139 return;
140 }
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800141 ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(),
142 sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800143 mDriverPath = path;
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800144 mSphalLibraries = sphalLibraries;
Jesse Hall90b25ed2016-12-12 12:56:46 -0800145}
146
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700147void GraphicsEnv::hintActivityLaunch() {
148 ATRACE_CALL();
149
Yiwei Zhangd3819382020-01-07 19:53:56 -0800150 {
151 std::lock_guard<std::mutex> lock(mStatsLock);
152 if (mActivityLaunched) return;
153 mActivityLaunched = true;
154 }
155
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700156 std::thread trySendGpuStatsThread([this]() {
157 // If there's already graphics driver preloaded in the process, just send
158 // the stats info to GpuStats directly through async binder.
159 std::lock_guard<std::mutex> lock(mStatsLock);
160 if (mGpuStats.glDriverToSend) {
161 mGpuStats.glDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700162 sendGpuStatsLocked(GpuStatsInfo::Api::API_GL, true, mGpuStats.glDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700163 }
164 if (mGpuStats.vkDriverToSend) {
165 mGpuStats.vkDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700166 sendGpuStatsLocked(GpuStatsInfo::Api::API_VK, true, mGpuStats.vkDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700167 }
168 });
169 trySendGpuStatsThread.detach();
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700170}
171
Greg Kaiser210bb7e2019-02-12 12:40:05 -0800172void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800173 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700174 int64_t driverBuildTime, const std::string& appPackageName,
175 const int vulkanVersion) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800176 ATRACE_CALL();
177
Yiwei Zhangd9861812019-02-13 11:51:55 -0800178 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800179 ALOGV("setGpuStats:\n"
180 "\tdriverPackageName[%s]\n"
181 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800182 "\tdriverVersionCode[%" PRIu64 "]\n"
183 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700184 "\tappPackageName[%s]\n"
185 "\tvulkanVersion[%d]\n",
Yiwei Zhang96c01712019-02-19 16:00:25 -0800186 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700187 appPackageName.c_str(), vulkanVersion);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800188
Yiwei Zhangd9861812019-02-13 11:51:55 -0800189 mGpuStats.driverPackageName = driverPackageName;
190 mGpuStats.driverVersionName = driverVersionName;
191 mGpuStats.driverVersionCode = driverVersionCode;
Yiwei Zhang96c01712019-02-19 16:00:25 -0800192 mGpuStats.driverBuildTime = driverBuildTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800193 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang794d2952019-05-06 17:43:59 -0700194 mGpuStats.vulkanVersion = vulkanVersion;
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800195}
196
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700197void GraphicsEnv::setDriverToLoad(GpuStatsInfo::Driver driver) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800198 ATRACE_CALL();
199
200 std::lock_guard<std::mutex> lock(mStatsLock);
201 switch (driver) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700202 case GpuStatsInfo::Driver::GL:
203 case GpuStatsInfo::Driver::GL_UPDATED:
204 case GpuStatsInfo::Driver::ANGLE: {
Yiwei Zhang472cab02019-08-05 17:57:41 -0700205 if (mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::NONE ||
206 mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::GL) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800207 mGpuStats.glDriverToLoad = driver;
208 break;
209 }
210
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700211 if (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800212 mGpuStats.glDriverFallback = driver;
213 }
214 break;
215 }
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700216 case GpuStatsInfo::Driver::VULKAN:
217 case GpuStatsInfo::Driver::VULKAN_UPDATED: {
Yiwei Zhang472cab02019-08-05 17:57:41 -0700218 if (mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::NONE ||
219 mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::VULKAN) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800220 mGpuStats.vkDriverToLoad = driver;
221 break;
222 }
223
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700224 if (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800225 mGpuStats.vkDriverFallback = driver;
226 }
227 break;
228 }
229 default:
230 break;
231 }
232}
233
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700234void GraphicsEnv::setDriverLoaded(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700235 int64_t driverLoadingTime) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800236 ATRACE_CALL();
237
238 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700239 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhangd3819382020-01-07 19:53:56 -0800240 mGpuStats.glDriverToSend = true;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700241 mGpuStats.glDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800242 } else {
Yiwei Zhangd3819382020-01-07 19:53:56 -0800243 mGpuStats.vkDriverToSend = true;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700244 mGpuStats.vkDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800245 }
246
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700247 sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800248}
249
Yiwei Zhangd9861812019-02-13 11:51:55 -0800250static sp<IGpuService> getGpuService() {
Yiwei Zhang8e097302019-07-08 16:11:12 -0700251 static const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
Yiwei Zhangd9861812019-02-13 11:51:55 -0800252 if (!binder) {
253 ALOGE("Failed to get gpu service");
254 return nullptr;
255 }
256
257 return interface_cast<IGpuService>(binder);
258}
259
Yiwei Zhangd3819382020-01-07 19:53:56 -0800260bool GraphicsEnv::readyToSendGpuStatsLocked() {
261 // Only send stats for processes having at least one activity launched and that process doesn't
262 // skip the GraphicsEnvironment setup.
263 return mActivityLaunched && !mGpuStats.appPackageName.empty();
264}
265
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700266void GraphicsEnv::setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value) {
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700267 ATRACE_CALL();
268
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700269 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangd3819382020-01-07 19:53:56 -0800270 if (!readyToSendGpuStatsLocked()) return;
271
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700272 const sp<IGpuService> gpuService = getGpuService();
273 if (gpuService) {
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700274 gpuService->setTargetStats(mGpuStats.appPackageName, mGpuStats.driverVersionCode, stats,
275 value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700276 }
277}
278
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700279void GraphicsEnv::sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800280 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800281 ATRACE_CALL();
282
Yiwei Zhangd3819382020-01-07 19:53:56 -0800283 if (!readyToSendGpuStatsLocked()) return;
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800284
285 ALOGV("sendGpuStats:\n"
286 "\tdriverPackageName[%s]\n"
287 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800288 "\tdriverVersionCode[%" PRIu64 "]\n"
289 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800290 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700291 "\tvulkanVersion[%d]\n"
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700292 "\tapi[%d]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800293 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800294 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800295 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-19 16:00:25 -0800296 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700297 mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime);
298
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700299 GpuStatsInfo::Driver driver = GpuStatsInfo::Driver::NONE;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700300 bool isIntendedDriverLoaded = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700301 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700302 driver = mGpuStats.glDriverToLoad;
303 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700304 isDriverLoaded && (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700305 } else {
306 driver = mGpuStats.vkDriverToLoad;
307 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700308 isDriverLoaded && (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700309 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800310
Yiwei Zhangd9861812019-02-13 11:51:55 -0800311 const sp<IGpuService> gpuService = getGpuService();
312 if (gpuService) {
313 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800314 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700315 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700316 isIntendedDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800317 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800318}
319
Adam Bodnar0afcca02019-09-17 13:23:17 -0700320bool GraphicsEnv::setInjectLayersPrSetDumpable() {
321 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
322 return false;
323 }
324 return true;
325}
326
Tim Van Patten5f744f12018-12-12 11:46:21 -0700327void* GraphicsEnv::loadLibrary(std::string name) {
328 const android_dlextinfo dlextinfo = {
329 .flags = ANDROID_DLEXT_USE_NAMESPACE,
330 .library_namespace = getAngleNamespace(),
331 };
332
333 std::string libName = std::string("lib") + name + "_angle.so";
334
335 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
336
337 if (so) {
338 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
339 return so;
340 } else {
341 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
342 }
343
344 return nullptr;
345}
346
347bool GraphicsEnv::checkAngleRules(void* so) {
348 char manufacturer[PROPERTY_VALUE_MAX];
349 char model[PROPERTY_VALUE_MAX];
350 property_get("ro.product.manufacturer", manufacturer, "UNSET");
351 property_get("ro.product.model", model, "UNSET");
352
353 auto ANGLEGetFeatureSupportUtilAPIVersion =
354 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
355 "ANGLEGetFeatureSupportUtilAPIVersion");
356
357 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
358 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
359 return false;
360 }
361
362 // Negotiate the interface version by requesting most recent known to the platform
363 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
364 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
365 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
366 "requested version %u",
367 versionToUse);
368 return false;
369 }
370
371 // Add and remove versions below as needed
372 bool useAngle = false;
373 switch (versionToUse) {
374 case 2: {
375 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
376 void* rulesHandle = nullptr;
377 int rulesVersion = 0;
378 void* systemInfoHandle = nullptr;
379
380 // Get the symbols for the feature-support-utility library:
381#define GET_SYMBOL(symbol) \
382 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
383 if (!symbol) { \
384 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
385 break; \
386 }
387 GET_SYMBOL(ANGLEAndroidParseRulesString);
388 GET_SYMBOL(ANGLEGetSystemInfo);
389 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
390 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
391 GET_SYMBOL(ANGLEFreeRulesHandle);
392 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
393
394 // Parse the rules, obtain the SystemInfo, and evaluate the
395 // application against the rules:
396 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
397 ALOGW("ANGLE feature-support library cannot parse rules file");
398 break;
399 }
400 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
401 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
402 break;
403 }
404 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) {
405 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
406 break;
407 }
408 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
409 systemInfoHandle, mAngleAppName.c_str());
410 (ANGLEFreeRulesHandle)(rulesHandle);
411 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
412 } break;
413
414 default:
415 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
416 }
417
418 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
419 return useAngle;
420}
421
422bool GraphicsEnv::shouldUseAngle(std::string appName) {
423 if (appName != mAngleAppName) {
424 // Make sure we are checking the app we were init'ed for
425 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
426 appName.c_str());
427 return false;
428 }
429
430 return shouldUseAngle();
431}
432
433bool GraphicsEnv::shouldUseAngle() {
434 // Make sure we are init'ed
435 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700436 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700437 return false;
438 }
439
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700440 return (mUseAngle == YES) ? true : false;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700441}
442
443void GraphicsEnv::updateUseAngle() {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700444 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700445
446 const char* ANGLE_PREFER_ANGLE = "angle";
447 const char* ANGLE_PREFER_NATIVE = "native";
448
449 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
450 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700451 mUseAngle = YES;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700452 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
453 ALOGV("User set \"Developer Options\" to force the use of Native");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700454 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700455 } else {
456 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
457 // load ANGLE and call the updatable opt-in/out logic:
Cody Northropc15d3822019-01-17 10:26:47 -0700458 void* featureSo = loadLibrary("feature_support");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700459 if (featureSo) {
460 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700461 mUseAngle = checkAngleRules(featureSo) ? YES : NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700462 dlclose(featureSo);
463 featureSo = nullptr;
464 } else {
465 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
466 }
467 }
468}
469
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600470void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700471 const std::string developerOptIn, const int rulesFd,
472 const long rulesOffset, const long rulesLength) {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700473 if (mUseAngle != UNKNOWN) {
474 // We've already figured out an answer for this app, so just return.
475 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
476 (mUseAngle == YES) ? "true" : "false");
477 return;
478 }
479
Tim Van Patten5f744f12018-12-12 11:46:21 -0700480 ALOGV("setting ANGLE path to '%s'", path.c_str());
481 mAnglePath = path;
482 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
483 mAngleAppName = appName;
484 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
485 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600486
Tim Van Patten5f744f12018-12-12 11:46:21 -0700487 lseek(rulesFd, rulesOffset, SEEK_SET);
488 mRulesBuffer = std::vector<char>(rulesLength + 1);
489 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
490 if (numBytesRead < 0) {
491 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
492 numBytesRead = 0;
493 } else if (numBytesRead == 0) {
494 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600495 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700496 if (numBytesRead != rulesLength) {
497 ALOGW("Did not read all of the necessary bytes from the rules file."
498 "expected: %ld, got: %zd",
499 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700500 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700501 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 10:34:58 -0600502
Tim Van Patten5f744f12018-12-12 11:46:21 -0700503 // Update the current status of whether we should use ANGLE or not
504 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600505}
506
Victor Khimenko4819b522018-07-13 17:24:18 +0200507void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600508 if (mLayerPaths.empty()) {
509 mLayerPaths = layerPaths;
510 mAppNamespace = appNamespace;
511 } else {
512 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800513 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600514 }
515}
516
Victor Khimenko4819b522018-07-13 17:24:18 +0200517NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600518 return mAppNamespace;
519}
520
Tim Van Patten5f744f12018-12-12 11:46:21 -0700521std::string& GraphicsEnv::getAngleAppName() {
522 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600523}
524
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600525const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600526 return mLayerPaths;
527}
528
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600529const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600530 return mDebugLayers;
531}
532
Cody Northropb9b01b62018-10-23 13:13:10 -0600533const std::string& GraphicsEnv::getDebugLayersGLES() {
534 return mDebugLayersGLES;
535}
536
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600537void GraphicsEnv::setDebugLayers(const std::string layers) {
538 mDebugLayers = layers;
539}
540
Cody Northropb9b01b62018-10-23 13:13:10 -0600541void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
542 mDebugLayersGLES = layers;
543}
544
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700545// Return true if all the required libraries from vndk and sphal namespace are
546// linked to the Game Driver namespace correctly.
547bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* vndkNamespace) {
548 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
549 if (llndkLibraries.empty()) {
550 return false;
551 }
552 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
553 ALOGE("Failed to link default namespace[%s]", dlerror());
554 return false;
555 }
556
557 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
558 if (vndkspLibraries.empty()) {
559 return false;
560 }
561 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
562 ALOGE("Failed to link vndk namespace[%s]", dlerror());
563 return false;
564 }
565
566 if (mSphalLibraries.empty()) {
567 return true;
568 }
569
570 // Make additional libraries in sphal to be accessible
571 auto sphalNamespace = android_get_exported_namespace("sphal");
572 if (!sphalNamespace) {
573 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
574 mSphalLibraries.c_str());
575 return false;
576 }
577
578 if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) {
579 ALOGE("Failed to link sphal namespace[%s]", dlerror());
580 return false;
581 }
582
583 return true;
584}
585
Jesse Hall53457db2016-12-14 16:54:06 -0800586android_namespace_t* GraphicsEnv::getDriverNamespace() {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700587 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Yiwei Zhang64d89212018-11-27 19:58:29 -0800588
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700589 if (mDriverNamespace) {
590 return mDriverNamespace;
591 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800592
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700593 if (mDriverPath.empty()) {
Peiyong Lin6b388252020-06-17 18:47:12 -0700594 // For an application process, driver path is empty means this application is not opted in
595 // to use updatable driver. Application process doesn't have the ability to set up
596 // environment variables and hence before `getenv` call will return.
597 // For a process that is not an application process, if it's run from an environment,
598 // for example shell, where environment variables can be set, then it can opt into using
599 // udpatable driver by setting UPDATABLE_GFX_DRIVER to 1. By setting to 1 the developer
600 // driver will be used currently.
601 // TODO(b/159240322) Support the production updatable driver.
602 const char* id = getenv("UPDATABLE_GFX_DRIVER");
603 if (id == nullptr || std::strcmp(id, "1")) {
604 return nullptr;
605 }
606 const sp<IGpuService> gpuService = getGpuService();
607 if (!gpuService) {
608 return nullptr;
609 }
610 mDriverPath = gpuService->getUpdatableDriverPath();
611 if (mDriverPath.empty()) {
612 return nullptr;
613 }
614 mDriverPath.append(UPDATABLE_DRIVER_ABI);
615 ALOGI("Driver path is setup via UPDATABLE_GFX_DRIVER: %s", mDriverPath.c_str());
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700616 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800617
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700618 auto vndkNamespace = android_get_exported_namespace("vndk");
619 if (!vndkNamespace) {
620 return nullptr;
621 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800622
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700623 mDriverNamespace = android_create_namespace("gfx driver",
624 mDriverPath.c_str(), // ld_library_path
625 mDriverPath.c_str(), // default_library_path
626 ANDROID_NAMESPACE_TYPE_ISOLATED,
627 nullptr, // permitted_when_isolated_path
628 nullptr);
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800629
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700630 if (!linkDriverNamespaceLocked(vndkNamespace)) {
631 mDriverNamespace = nullptr;
632 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800633
Jesse Hall53457db2016-12-14 16:54:06 -0800634 return mDriverNamespace;
635}
636
Peiyong Lindb3ed6e2020-01-09 18:43:27 -0800637std::string GraphicsEnv::getDriverPath() const {
638 return mDriverPath;
639}
640
Cody Northrop1f00e172018-04-02 11:23:31 -0600641android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700642 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600643
Cody Northrop3892cfa2019-01-30 10:03:12 -0700644 if (mAngleNamespace) {
645 return mAngleNamespace;
646 }
647
648 if (mAnglePath.empty()) {
649 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
650 return nullptr;
651 }
652
653 mAngleNamespace = android_create_namespace("ANGLE",
654 nullptr, // ld_library_path
655 mAnglePath.c_str(), // default_library_path
656 ANDROID_NAMESPACE_TYPE_SHARED |
657 ANDROID_NAMESPACE_TYPE_ISOLATED,
658 nullptr, // permitted_when_isolated_path
659 nullptr);
660
661 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600662
663 return mAngleNamespace;
664}
665
Jesse Hall90b25ed2016-12-12 12:56:46 -0800666} // namespace android