blob: 701a3b2f779005689194f030b42fff8ef4abc311 [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
Peiyong Lin0acbfdc2020-06-17 18:47:12 -070042// 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 11:46:21 -070049// 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
Kiyoung Kimc0e3ed32023-08-11 11:22:15 +090063namespace {
64static bool isVndkEnabled() {
65#ifdef __BIONIC__
Kiyoung Kim77f37092023-09-26 10:41:47 +090066 static bool isVndkEnabled = android::base::GetProperty("ro.vndk.version", "") != "";
Kiyoung Kimc0e3ed32023-08-11 11:22:15 +090067 return isVndkEnabled;
68#endif
69 return false;
70}
71} // namespace
72
Jesse Hall90b25ed2016-12-12 12:56:46 -080073namespace android {
74
Yiwei Zhang64d89212018-11-27 19:58:29 -080075enum NativeLibrary {
76 LLNDK = 0,
77 VNDKSP = 1,
78};
79
Jooyung Han78396802020-02-23 03:02:43 +090080static constexpr const char* kNativeLibrariesSystemConfigPath[] =
81 {"/apex/com.android.vndk.v{}/etc/llndk.libraries.{}.txt",
82 "/apex/com.android.vndk.v{}/etc/vndksp.libraries.{}.txt"};
Yiwei Zhang64d89212018-11-27 19:58:29 -080083
Kiyoung Kimc0e3ed32023-08-11 11:22:15 +090084static const char* kLlndkLibrariesTxtPath = "/system/etc/llndk.libraries.txt";
85
Yiwei Zhang64d89212018-11-27 19:58:29 -080086static std::string vndkVersionStr() {
87#ifdef __BIONIC__
Michael Hoisied1023f22020-03-26 22:52:58 -070088 return base::GetProperty("ro.vndk.version", "");
Yiwei Zhang64d89212018-11-27 19:58:29 -080089#endif
90 return "";
91}
92
93static void insertVndkVersionStr(std::string* fileName) {
94 LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr");
Jooyung Han78396802020-02-23 03:02:43 +090095 std::string version = vndkVersionStr();
96 size_t pos = fileName->find("{}");
97 while (pos != std::string::npos) {
98 fileName->replace(pos, 2, version);
99 pos = fileName->find("{}", pos + version.size());
Yiwei Zhang64d89212018-11-27 19:58:29 -0800100 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800101}
102
103static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) {
104 // Read list of public native libraries from the config file.
105 std::string fileContent;
106 if (!base::ReadFileToString(configFile, &fileContent)) {
107 return false;
108 }
109
110 std::vector<std::string> lines = base::Split(fileContent, "\n");
111
112 for (auto& line : lines) {
113 auto trimmedLine = base::Trim(line);
114 if (!trimmedLine.empty()) {
115 soNames->push_back(trimmedLine);
116 }
117 }
118
119 return true;
120}
121
122static const std::string getSystemNativeLibraries(NativeLibrary type) {
Kiyoung Kimc0e3ed32023-08-11 11:22:15 +0900123 std::string nativeLibrariesSystemConfig = "";
124
125 if (!isVndkEnabled() && type == NativeLibrary::LLNDK) {
126 nativeLibrariesSystemConfig = kLlndkLibrariesTxtPath;
127 } else {
128 nativeLibrariesSystemConfig = kNativeLibrariesSystemConfigPath[type];
129 insertVndkVersionStr(&nativeLibrariesSystemConfig);
130 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800131
132 std::vector<std::string> soNames;
133 if (!readConfig(nativeLibrariesSystemConfig, &soNames)) {
134 ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str());
135 return "";
136 }
137
138 return base::Join(soNames, ':');
139}
140
Jesse Hall90b25ed2016-12-12 12:56:46 -0800141/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
142 static GraphicsEnv env;
143 return env;
144}
145
Yiwei Zhang6a674c92019-11-08 11:55:36 -0800146bool GraphicsEnv::isDebuggable() {
Cody Northrop383832f2022-11-01 13:54:57 -0600147 // This flag determines if the application is marked debuggable
148 bool appDebuggable = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) > 0;
149
150 // This flag is set only in `debuggable` builds of the platform
151#if defined(ANDROID_DEBUGGABLE)
152 bool platformDebuggable = true;
153#else
154 bool platformDebuggable = false;
155#endif
156
157 ALOGV("GraphicsEnv::isDebuggable returning appDebuggable=%s || platformDebuggable=%s",
158 appDebuggable ? "true" : "false", platformDebuggable ? "true" : "false");
159
160 return appDebuggable || platformDebuggable;
Cody Northrop629ce4e2018-10-15 07:22:09 -0600161}
162
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800163void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string path,
164 const std::string sphalLibraries) {
165 if (!mDriverPath.empty() || !mSphalLibraries.empty()) {
166 ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries "
167 "from '%s' to '%s'",
168 mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800169 return;
170 }
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800171 ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(),
172 sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800173 mDriverPath = path;
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800174 mSphalLibraries = sphalLibraries;
Jesse Hall90b25ed2016-12-12 12:56:46 -0800175}
176
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700177void GraphicsEnv::hintActivityLaunch() {
178 ATRACE_CALL();
179
Yiwei Zhangd3819382020-01-07 19:53:56 -0800180 {
181 std::lock_guard<std::mutex> lock(mStatsLock);
182 if (mActivityLaunched) return;
183 mActivityLaunched = true;
184 }
185
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700186 std::thread trySendGpuStatsThread([this]() {
187 // If there's already graphics driver preloaded in the process, just send
188 // the stats info to GpuStats directly through async binder.
189 std::lock_guard<std::mutex> lock(mStatsLock);
190 if (mGpuStats.glDriverToSend) {
191 mGpuStats.glDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700192 sendGpuStatsLocked(GpuStatsInfo::Api::API_GL, true, mGpuStats.glDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700193 }
194 if (mGpuStats.vkDriverToSend) {
195 mGpuStats.vkDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700196 sendGpuStatsLocked(GpuStatsInfo::Api::API_VK, true, mGpuStats.vkDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700197 }
198 });
199 trySendGpuStatsThread.detach();
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700200}
201
Greg Kaiser210bb7e2019-02-12 12:40:05 -0800202void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800203 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700204 int64_t driverBuildTime, const std::string& appPackageName,
205 const int vulkanVersion) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800206 ATRACE_CALL();
207
Yiwei Zhangd9861812019-02-13 11:51:55 -0800208 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800209 ALOGV("setGpuStats:\n"
210 "\tdriverPackageName[%s]\n"
211 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800212 "\tdriverVersionCode[%" PRIu64 "]\n"
213 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700214 "\tappPackageName[%s]\n"
215 "\tvulkanVersion[%d]\n",
Yiwei Zhang96c01712019-02-19 16:00:25 -0800216 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700217 appPackageName.c_str(), vulkanVersion);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800218
Yiwei Zhangd9861812019-02-13 11:51:55 -0800219 mGpuStats.driverPackageName = driverPackageName;
220 mGpuStats.driverVersionName = driverVersionName;
221 mGpuStats.driverVersionCode = driverVersionCode;
Yiwei Zhang96c01712019-02-19 16:00:25 -0800222 mGpuStats.driverBuildTime = driverBuildTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800223 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang794d2952019-05-06 17:43:59 -0700224 mGpuStats.vulkanVersion = vulkanVersion;
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800225}
226
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700227void GraphicsEnv::setDriverToLoad(GpuStatsInfo::Driver driver) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800228 ATRACE_CALL();
229
230 std::lock_guard<std::mutex> lock(mStatsLock);
231 switch (driver) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700232 case GpuStatsInfo::Driver::GL:
233 case GpuStatsInfo::Driver::GL_UPDATED:
234 case GpuStatsInfo::Driver::ANGLE: {
Yiwei Zhang472cab02019-08-05 17:57:41 -0700235 if (mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::NONE ||
236 mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::GL) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800237 mGpuStats.glDriverToLoad = driver;
238 break;
239 }
240
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700241 if (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800242 mGpuStats.glDriverFallback = driver;
243 }
244 break;
245 }
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700246 case GpuStatsInfo::Driver::VULKAN:
247 case GpuStatsInfo::Driver::VULKAN_UPDATED: {
Yiwei Zhang472cab02019-08-05 17:57:41 -0700248 if (mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::NONE ||
249 mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::VULKAN) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800250 mGpuStats.vkDriverToLoad = driver;
251 break;
252 }
253
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700254 if (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800255 mGpuStats.vkDriverFallback = driver;
256 }
257 break;
258 }
259 default:
260 break;
261 }
262}
263
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700264void GraphicsEnv::setDriverLoaded(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700265 int64_t driverLoadingTime) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800266 ATRACE_CALL();
267
268 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700269 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhangd3819382020-01-07 19:53:56 -0800270 mGpuStats.glDriverToSend = true;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700271 mGpuStats.glDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800272 } else {
Yiwei Zhangd3819382020-01-07 19:53:56 -0800273 mGpuStats.vkDriverToSend = true;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700274 mGpuStats.vkDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800275 }
276
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700277 sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800278}
279
Yiwei Zhangd9861812019-02-13 11:51:55 -0800280static sp<IGpuService> getGpuService() {
Yiwei Zhang8e097302019-07-08 16:11:12 -0700281 static const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
Yiwei Zhangd9861812019-02-13 11:51:55 -0800282 if (!binder) {
283 ALOGE("Failed to get gpu service");
284 return nullptr;
285 }
286
287 return interface_cast<IGpuService>(binder);
288}
289
Yiwei Zhangd3819382020-01-07 19:53:56 -0800290bool GraphicsEnv::readyToSendGpuStatsLocked() {
291 // Only send stats for processes having at least one activity launched and that process doesn't
292 // skip the GraphicsEnvironment setup.
293 return mActivityLaunched && !mGpuStats.appPackageName.empty();
294}
295
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700296void GraphicsEnv::setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value) {
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700297 ATRACE_CALL();
298
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700299 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangd3819382020-01-07 19:53:56 -0800300 if (!readyToSendGpuStatsLocked()) return;
301
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700302 const sp<IGpuService> gpuService = getGpuService();
303 if (gpuService) {
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700304 gpuService->setTargetStats(mGpuStats.appPackageName, mGpuStats.driverVersionCode, stats,
305 value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700306 }
307}
308
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700309void GraphicsEnv::sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800310 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800311 ATRACE_CALL();
312
Yiwei Zhangd3819382020-01-07 19:53:56 -0800313 if (!readyToSendGpuStatsLocked()) return;
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800314
315 ALOGV("sendGpuStats:\n"
316 "\tdriverPackageName[%s]\n"
317 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800318 "\tdriverVersionCode[%" PRIu64 "]\n"
319 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800320 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700321 "\tvulkanVersion[%d]\n"
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700322 "\tapi[%d]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800323 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800324 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800325 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-19 16:00:25 -0800326 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700327 mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime);
328
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700329 GpuStatsInfo::Driver driver = GpuStatsInfo::Driver::NONE;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700330 bool isIntendedDriverLoaded = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700331 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700332 driver = mGpuStats.glDriverToLoad;
333 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700334 isDriverLoaded && (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700335 } else {
336 driver = mGpuStats.vkDriverToLoad;
337 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700338 isDriverLoaded && (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700339 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800340
Yiwei Zhangd9861812019-02-13 11:51:55 -0800341 const sp<IGpuService> gpuService = getGpuService();
342 if (gpuService) {
343 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800344 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700345 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700346 isIntendedDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800347 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800348}
349
Adam Bodnar0afcca02019-09-17 13:23:17 -0700350bool GraphicsEnv::setInjectLayersPrSetDumpable() {
351 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
352 return false;
353 }
354 return true;
355}
356
Tim Van Patten5f744f12018-12-12 11:46:21 -0700357void* GraphicsEnv::loadLibrary(std::string name) {
358 const android_dlextinfo dlextinfo = {
359 .flags = ANDROID_DLEXT_USE_NAMESPACE,
360 .library_namespace = getAngleNamespace(),
361 };
362
363 std::string libName = std::string("lib") + name + "_angle.so";
364
365 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
366
367 if (so) {
368 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
369 return so;
370 } else {
371 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
372 }
373
374 return nullptr;
375}
376
Tim Van Patten5f744f12018-12-12 11:46:21 -0700377bool GraphicsEnv::shouldUseAngle(std::string appName) {
378 if (appName != mAngleAppName) {
379 // Make sure we are checking the app we were init'ed for
380 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
381 appName.c_str());
382 return false;
383 }
384
385 return shouldUseAngle();
386}
387
388bool GraphicsEnv::shouldUseAngle() {
389 // Make sure we are init'ed
390 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700391 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700392 return false;
393 }
394
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700395 return (mUseAngle == YES) ? true : false;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700396}
397
Ian Elliott7c0a7ee2022-04-21 12:41:03 -0600398bool GraphicsEnv::angleIsSystemDriver() {
399 // Make sure we are init'ed
400 if (mAngleAppName.empty()) {
401 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
402 return false;
403 }
404
405 return (mAngleIsSystemDriver == YES) ? true : false;
406}
407
408bool GraphicsEnv::shouldForceLegacyDriver() {
409 // Make sure we are init'ed
410 if (mAngleAppName.empty()) {
411 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
412 return false;
413 }
414
415 return (mAngleIsSystemDriver == YES && mUseAngle == NO) ? true : false;
416}
417
418std::string GraphicsEnv::getLegacySuffix() {
419 return mLegacyDriverSuffix;
420}
421
Tim Van Patten5f744f12018-12-12 11:46:21 -0700422void GraphicsEnv::updateUseAngle() {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700423 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700424
425 const char* ANGLE_PREFER_ANGLE = "angle";
Ian Elliott7c0a7ee2022-04-21 12:41:03 -0600426 const char* ANGLE_PREFER_LEGACY = "legacy";
427 // The following is a deprecated version of "legacy"
Tim Van Patten5f744f12018-12-12 11:46:21 -0700428 const char* ANGLE_PREFER_NATIVE = "native";
429
Tim Van Pattena53dcb22021-05-21 17:51:59 -0600430 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700431 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
Ian Elliott7c0a7ee2022-04-21 12:41:03 -0600432 ALOGI("Using ANGLE, the %s GLES driver for package '%s'",
433 mAngleIsSystemDriver == YES ? "system" : "optional", mAngleAppName.c_str());
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700434 mUseAngle = YES;
Ian Elliott7c0a7ee2022-04-21 12:41:03 -0600435 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_LEGACY ||
436 mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
437 ALOGI("Using the (%s) Legacy GLES driver for package '%s'",
438 mAngleIsSystemDriver == YES ? "optional" : "system", mAngleAppName.c_str());
Tim Van Patten5f744f12018-12-12 11:46:21 -0700439 } else {
Tim Van Pattena53dcb22021-05-21 17:51:59 -0600440 ALOGV("User set invalid \"Developer Options\": '%s'", mAngleDeveloperOptIn.c_str());
Tim Van Patten5f744f12018-12-12 11:46:21 -0700441 }
442}
443
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600444void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Ian Elliott7c0a7ee2022-04-21 12:41:03 -0600445 const bool angleIsSystemDriver, const std::string developerOptIn,
Tim Van Pattena53dcb22021-05-21 17:51:59 -0600446 const std::vector<std::string> eglFeatures) {
Ian Elliott7c0a7ee2022-04-21 12:41:03 -0600447 // Set whether ANGLE is the system driver:
448 mAngleIsSystemDriver = angleIsSystemDriver ? YES : NO;
449
450 // Note: Given the current logic and lack of the old rules file processing,
451 // there seems to be little chance that mUseAngle != UNKNOWN. Leave this
452 // for now, even though it seems outdated.
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700453 if (mUseAngle != UNKNOWN) {
454 // We've already figured out an answer for this app, so just return.
455 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
456 (mUseAngle == YES) ? "true" : "false");
457 return;
458 }
459
Peiyong Lin2f707e62020-09-26 13:52:10 -0700460 mAngleEglFeatures = std::move(eglFeatures);
461
Tim Van Patten5f744f12018-12-12 11:46:21 -0700462 ALOGV("setting ANGLE path to '%s'", path.c_str());
463 mAnglePath = path;
464 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
465 mAngleAppName = appName;
466 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
467 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600468
Tim Van Patten5f744f12018-12-12 11:46:21 -0700469 // Update the current status of whether we should use ANGLE or not
470 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600471}
472
Ian Elliott7c0a7ee2022-04-21 12:41:03 -0600473void GraphicsEnv::setLegacyDriverInfo(const std::string appName, const bool angleIsSystemDriver,
474 const std::string legacyDriverName) {
475 ALOGV("setting legacy app name to '%s'", appName.c_str());
476 mAngleAppName = appName;
477
478 // Force the use of the legacy driver instead of ANGLE
479 const char* ANGLE_PREFER_LEGACY = "legacy";
480 mAngleDeveloperOptIn = ANGLE_PREFER_LEGACY;
481 ALOGV("setting ANGLE application opt-in to 'legacy'");
482
483 // Set whether ANGLE is the system driver:
484 mAngleIsSystemDriver = angleIsSystemDriver ? YES : NO;
485
486 mLegacyDriverSuffix = legacyDriverName;
487
488 // Update the current status of whether we should use ANGLE or not
489 updateUseAngle();
490}
491
Victor Khimenko4819b522018-07-13 17:24:18 +0200492void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600493 if (mLayerPaths.empty()) {
494 mLayerPaths = layerPaths;
495 mAppNamespace = appNamespace;
496 } else {
497 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800498 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600499 }
500}
501
Victor Khimenko4819b522018-07-13 17:24:18 +0200502NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600503 return mAppNamespace;
504}
505
Tim Van Patten5f744f12018-12-12 11:46:21 -0700506std::string& GraphicsEnv::getAngleAppName() {
507 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600508}
509
Peiyong Lin2f707e62020-09-26 13:52:10 -0700510const std::vector<std::string>& GraphicsEnv::getAngleEglFeatures() {
511 return mAngleEglFeatures;
512}
513
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600514const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600515 return mLayerPaths;
516}
517
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600518const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600519 return mDebugLayers;
520}
521
Cody Northropb9b01b62018-10-23 13:13:10 -0600522const std::string& GraphicsEnv::getDebugLayersGLES() {
523 return mDebugLayersGLES;
524}
525
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600526void GraphicsEnv::setDebugLayers(const std::string layers) {
527 mDebugLayers = layers;
528}
529
Cody Northropb9b01b62018-10-23 13:13:10 -0600530void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
531 mDebugLayersGLES = layers;
532}
533
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700534// Return true if all the required libraries from vndk and sphal namespace are
Yiwei Zhang9058b8f2020-11-12 20:23:00 +0000535// linked to the updatable gfx driver namespace correctly.
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700536bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* vndkNamespace) {
537 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
538 if (llndkLibraries.empty()) {
539 return false;
540 }
541 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
542 ALOGE("Failed to link default namespace[%s]", dlerror());
543 return false;
544 }
545
546 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
547 if (vndkspLibraries.empty()) {
548 return false;
549 }
550 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
551 ALOGE("Failed to link vndk namespace[%s]", dlerror());
552 return false;
553 }
554
555 if (mSphalLibraries.empty()) {
556 return true;
557 }
558
559 // Make additional libraries in sphal to be accessible
560 auto sphalNamespace = android_get_exported_namespace("sphal");
561 if (!sphalNamespace) {
562 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
563 mSphalLibraries.c_str());
564 return false;
565 }
566
567 if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) {
568 ALOGE("Failed to link sphal namespace[%s]", dlerror());
569 return false;
570 }
571
572 return true;
573}
574
Jesse Hall53457db2016-12-14 16:54:06 -0800575android_namespace_t* GraphicsEnv::getDriverNamespace() {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700576 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Yiwei Zhang64d89212018-11-27 19:58:29 -0800577
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700578 if (mDriverNamespace) {
579 return mDriverNamespace;
580 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800581
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700582 if (mDriverPath.empty()) {
Peiyong Lin0acbfdc2020-06-17 18:47:12 -0700583 // For an application process, driver path is empty means this application is not opted in
584 // to use updatable driver. Application process doesn't have the ability to set up
585 // environment variables and hence before `getenv` call will return.
586 // For a process that is not an application process, if it's run from an environment,
587 // for example shell, where environment variables can be set, then it can opt into using
588 // udpatable driver by setting UPDATABLE_GFX_DRIVER to 1. By setting to 1 the developer
589 // driver will be used currently.
590 // TODO(b/159240322) Support the production updatable driver.
591 const char* id = getenv("UPDATABLE_GFX_DRIVER");
592 if (id == nullptr || std::strcmp(id, "1")) {
593 return nullptr;
594 }
595 const sp<IGpuService> gpuService = getGpuService();
596 if (!gpuService) {
597 return nullptr;
598 }
599 mDriverPath = gpuService->getUpdatableDriverPath();
600 if (mDriverPath.empty()) {
601 return nullptr;
602 }
603 mDriverPath.append(UPDATABLE_DRIVER_ABI);
604 ALOGI("Driver path is setup via UPDATABLE_GFX_DRIVER: %s", mDriverPath.c_str());
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700605 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800606
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700607 auto vndkNamespace = android_get_exported_namespace("vndk");
608 if (!vndkNamespace) {
609 return nullptr;
610 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800611
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700612 mDriverNamespace = android_create_namespace("gfx driver",
613 mDriverPath.c_str(), // ld_library_path
614 mDriverPath.c_str(), // default_library_path
615 ANDROID_NAMESPACE_TYPE_ISOLATED,
616 nullptr, // permitted_when_isolated_path
617 nullptr);
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800618
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700619 if (!linkDriverNamespaceLocked(vndkNamespace)) {
620 mDriverNamespace = nullptr;
621 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800622
Jesse Hall53457db2016-12-14 16:54:06 -0800623 return mDriverNamespace;
624}
625
Peiyong Lindb3ed6e2020-01-09 18:43:27 -0800626std::string GraphicsEnv::getDriverPath() const {
627 return mDriverPath;
628}
629
Cody Northrop1f00e172018-04-02 11:23:31 -0600630android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700631 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600632
Cody Northrop3892cfa2019-01-30 10:03:12 -0700633 if (mAngleNamespace) {
634 return mAngleNamespace;
635 }
636
637 if (mAnglePath.empty()) {
638 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
639 return nullptr;
640 }
641
642 mAngleNamespace = android_create_namespace("ANGLE",
643 nullptr, // ld_library_path
644 mAnglePath.c_str(), // default_library_path
Yiwei Zhang5b7604f2020-05-08 14:23:34 -0700645 ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED,
Cody Northrop3892cfa2019-01-30 10:03:12 -0700646 nullptr, // permitted_when_isolated_path
647 nullptr);
648
649 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600650
651 return mAngleNamespace;
652}
653
Jesse Hall90b25ed2016-12-12 12:56:46 -0800654} // namespace android