blob: 4bc261106a9cdb3b3b2121faf9c94349942a93e1 [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>
Tim Van Patten93ef6362025-01-10 16:47:27 -070032#include <com_android_graphics_graphicsenv_flags.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 Lin0acbfdc2020-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
Kiyoung Kimc0e3ed32023-08-11 11:22:15 +090064namespace {
65static bool isVndkEnabled() {
66#ifdef __BIONIC__
Kiyoung Kim77f37092023-09-26 10:41:47 +090067 static bool isVndkEnabled = android::base::GetProperty("ro.vndk.version", "") != "";
Kiyoung Kimc0e3ed32023-08-11 11:22:15 +090068 return isVndkEnabled;
69#endif
70 return false;
71}
72} // namespace
73
Tim Van Patten93ef6362025-01-10 16:47:27 -070074namespace graphicsenv_flags = com::android::graphics::graphicsenv::flags;
75
Jesse Hall90b25ed2016-12-12 12:56:46 -080076namespace android {
77
Yiwei Zhang64d89212018-11-27 19:58:29 -080078enum NativeLibrary {
79 LLNDK = 0,
80 VNDKSP = 1,
81};
82
Jooyung Han78396802020-02-23 03:02:43 +090083static constexpr const char* kNativeLibrariesSystemConfigPath[] =
84 {"/apex/com.android.vndk.v{}/etc/llndk.libraries.{}.txt",
85 "/apex/com.android.vndk.v{}/etc/vndksp.libraries.{}.txt"};
Yiwei Zhang64d89212018-11-27 19:58:29 -080086
Kiyoung Kimc0e3ed32023-08-11 11:22:15 +090087static const char* kLlndkLibrariesTxtPath = "/system/etc/llndk.libraries.txt";
88
Chris Forbes0cdac9a2024-08-16 15:12:27 +120089// List of libraries that were previously available via VNDK-SP,
90// and are now available via SPHAL.
91// On modern devices that lack the VNDK APEX, the device no longer
92// contains a helpful list of these libraries on the filesystem as above.
93// See system/sepolicy/vendor/file_contexts
94static const char* kFormerlyVndkspLibrariesList =
95 "android.hardware.common-V2-ndk.so:"
96 "android.hardware.common.fmq-V1-ndk.so:"
97 "android.hardware.graphics.allocator-V2-ndk.so:"
Eino-Ville Talvala888300c2024-11-15 21:34:14 +000098 "android.hardware.graphics.common-V6-ndk.so:"
Chris Forbes0cdac9a2024-08-16 15:12:27 +120099 "android.hardware.graphics.common@1.0.so:"
100 "android.hardware.graphics.common@1.1.so:"
101 "android.hardware.graphics.common@1.2.so:"
102 "android.hardware.graphics.composer3-V1-ndk.so:"
103 "android.hardware.graphics.mapper@2.0.so:"
104 "android.hardware.graphics.mapper@2.1.so:"
105 "android.hardware.graphics.mapper@3.0.so:"
106 "android.hardware.graphics.mapper@4.0.so:"
107 "android.hardware.renderscript@1.0.so:"
108 "android.hidl.memory.token@1.0.so:"
109 "android.hidl.memory@1.0-impl.so:"
110 "android.hidl.memory@1.0.so:"
111 "android.hidl.safe_union@1.0.so:"
112 "libRSCpuRef.so:"
113 "libRSDriver.so:"
114 "libRS_internal.so:"
115 "libbacktrace.so:"
116 "libbase.so:"
117 "libbcinfo.so:"
118 "libblas.so:"
119 "libc++.so:"
120 "libcompiler_rt.so:"
121 "libcutils.so:"
122 "libdmabufheap.so:"
123 "libft2.so:"
124 "libgralloctypes.so:"
125 "libhardware.so:"
126 "libhidlbase.so:"
127 "libhidlmemory.so:"
128 "libion.so:"
129 "libjsoncpp.so:"
130 "liblzma.so:"
131 "libpng.so:"
132 "libprocessgroup.so:"
133 "libunwindstack.so:"
134 "libutils.so:"
135 "libutilscallstack.so:"
136 "libz.so";
137
Yiwei Zhang64d89212018-11-27 19:58:29 -0800138static std::string vndkVersionStr() {
139#ifdef __BIONIC__
Michael Hoisied1023f22020-03-26 22:52:58 -0700140 return base::GetProperty("ro.vndk.version", "");
Yiwei Zhang64d89212018-11-27 19:58:29 -0800141#endif
142 return "";
143}
144
145static void insertVndkVersionStr(std::string* fileName) {
146 LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr");
Jooyung Han78396802020-02-23 03:02:43 +0900147 std::string version = vndkVersionStr();
148 size_t pos = fileName->find("{}");
149 while (pos != std::string::npos) {
150 fileName->replace(pos, 2, version);
151 pos = fileName->find("{}", pos + version.size());
Yiwei Zhang64d89212018-11-27 19:58:29 -0800152 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800153}
154
155static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) {
156 // Read list of public native libraries from the config file.
157 std::string fileContent;
158 if (!base::ReadFileToString(configFile, &fileContent)) {
159 return false;
160 }
161
162 std::vector<std::string> lines = base::Split(fileContent, "\n");
163
164 for (auto& line : lines) {
165 auto trimmedLine = base::Trim(line);
166 if (!trimmedLine.empty()) {
167 soNames->push_back(trimmedLine);
168 }
169 }
170
171 return true;
172}
173
174static const std::string getSystemNativeLibraries(NativeLibrary type) {
Kiyoung Kimc0e3ed32023-08-11 11:22:15 +0900175 std::string nativeLibrariesSystemConfig = "";
176
Chris Forbes0cdac9a2024-08-16 15:12:27 +1200177 if (!isVndkEnabled()) {
178 if (type == NativeLibrary::VNDKSP) {
179 return kFormerlyVndkspLibrariesList;
180 } else {
181 nativeLibrariesSystemConfig = kLlndkLibrariesTxtPath;
182 }
Kiyoung Kimc0e3ed32023-08-11 11:22:15 +0900183 } else {
184 nativeLibrariesSystemConfig = kNativeLibrariesSystemConfigPath[type];
185 insertVndkVersionStr(&nativeLibrariesSystemConfig);
186 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800187
188 std::vector<std::string> soNames;
189 if (!readConfig(nativeLibrariesSystemConfig, &soNames)) {
190 ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str());
191 return "";
192 }
193
194 return base::Join(soNames, ':');
195}
196
Peiyong Linef20e4e2023-07-03 02:33:00 +0000197static sp<IGpuService> getGpuService() {
198 static const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
199 if (!binder) {
200 ALOGE("Failed to get gpu service");
201 return nullptr;
202 }
203
204 return interface_cast<IGpuService>(binder);
205}
206
Jesse Hall90b25ed2016-12-12 12:56:46 -0800207/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
208 static GraphicsEnv env;
209 return env;
210}
211
Yiwei Zhang6a674c92019-11-08 11:55:36 -0800212bool GraphicsEnv::isDebuggable() {
Cody Northrop383832f2022-11-01 13:54:57 -0600213 // This flag determines if the application is marked debuggable
214 bool appDebuggable = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) > 0;
215
216 // This flag is set only in `debuggable` builds of the platform
217#if defined(ANDROID_DEBUGGABLE)
218 bool platformDebuggable = true;
219#else
220 bool platformDebuggable = false;
221#endif
222
223 ALOGV("GraphicsEnv::isDebuggable returning appDebuggable=%s || platformDebuggable=%s",
224 appDebuggable ? "true" : "false", platformDebuggable ? "true" : "false");
225
226 return appDebuggable || platformDebuggable;
Cody Northrop629ce4e2018-10-15 07:22:09 -0600227}
228
Peiyong Linef20e4e2023-07-03 02:33:00 +0000229/**
230 * APIs for updatable graphics drivers
231 */
232
Peiyong Lin2f51e752023-06-15 22:35:14 +0000233void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string& path,
234 const std::string& sphalLibraries) {
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800235 if (!mDriverPath.empty() || !mSphalLibraries.empty()) {
236 ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries "
237 "from '%s' to '%s'",
238 mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800239 return;
240 }
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800241 ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(),
242 sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800243 mDriverPath = path;
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800244 mSphalLibraries = sphalLibraries;
Jesse Hall90b25ed2016-12-12 12:56:46 -0800245}
246
Peiyong Linef20e4e2023-07-03 02:33:00 +0000247// Return true if all the required libraries from vndk and sphal namespace are
248// linked to the driver namespace correctly.
249bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* destNamespace,
250 android_namespace_t* vndkNamespace,
251 const std::string& sharedSphalLibraries) {
252 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
253 if (llndkLibraries.empty()) {
254 return false;
255 }
256 if (!android_link_namespaces(destNamespace, nullptr, llndkLibraries.c_str())) {
257 ALOGE("Failed to link default namespace[%s]", dlerror());
258 return false;
259 }
260
261 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
262 if (vndkspLibraries.empty()) {
263 return false;
264 }
265 if (!android_link_namespaces(destNamespace, vndkNamespace, vndkspLibraries.c_str())) {
266 ALOGE("Failed to link vndk namespace[%s]", dlerror());
267 return false;
268 }
269
270 if (sharedSphalLibraries.empty()) {
271 return true;
272 }
273
274 // Make additional libraries in sphal to be accessible
275 auto sphalNamespace = android_get_exported_namespace("sphal");
276 if (!sphalNamespace) {
277 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
278 sharedSphalLibraries.c_str());
279 return false;
280 }
281
282 if (!android_link_namespaces(destNamespace, sphalNamespace, sharedSphalLibraries.c_str())) {
283 ALOGE("Failed to link sphal namespace[%s]", dlerror());
284 return false;
285 }
286
287 return true;
288}
289
290android_namespace_t* GraphicsEnv::getDriverNamespace() {
291 std::lock_guard<std::mutex> lock(mNamespaceMutex);
292
293 if (mDriverNamespace) {
294 return mDriverNamespace;
295 }
296
297 if (mDriverPath.empty()) {
298 // For an application process, driver path is empty means this application is not opted in
299 // to use updatable driver. Application process doesn't have the ability to set up
300 // environment variables and hence before `getenv` call will return.
301 // For a process that is not an application process, if it's run from an environment,
302 // for example shell, where environment variables can be set, then it can opt into using
303 // udpatable driver by setting UPDATABLE_GFX_DRIVER to 1. By setting to 1 the developer
304 // driver will be used currently.
305 // TODO(b/159240322) Support the production updatable driver.
306 const char* id = getenv("UPDATABLE_GFX_DRIVER");
307 if (id == nullptr || std::strcmp(id, "1") != 0) {
308 return nullptr;
309 }
310 const sp<IGpuService> gpuService = getGpuService();
311 if (!gpuService) {
312 return nullptr;
313 }
314 mDriverPath = gpuService->getUpdatableDriverPath();
315 if (mDriverPath.empty()) {
316 return nullptr;
317 }
318 mDriverPath.append(UPDATABLE_DRIVER_ABI);
319 ALOGI("Driver path is setup via UPDATABLE_GFX_DRIVER: %s", mDriverPath.c_str());
320 }
321
Chris Forbes0cdac9a2024-08-16 15:12:27 +1200322 auto vndkNamespace = android_get_exported_namespace(isVndkEnabled() ? "vndk" : "sphal");
Peiyong Linef20e4e2023-07-03 02:33:00 +0000323 if (!vndkNamespace) {
Ethan Chenf5da7ee2024-01-18 19:47:01 -0800324 mDriverNamespace = nullptr;
325 return mDriverNamespace;
Peiyong Linef20e4e2023-07-03 02:33:00 +0000326 }
327
328 mDriverNamespace = android_create_namespace("updatable gfx driver",
329 mDriverPath.c_str(), // ld_library_path
330 mDriverPath.c_str(), // default_library_path
331 ANDROID_NAMESPACE_TYPE_ISOLATED,
332 nullptr, // permitted_when_isolated_path
333 nullptr);
334
335 if (!linkDriverNamespaceLocked(mDriverNamespace, vndkNamespace, mSphalLibraries)) {
336 mDriverNamespace = nullptr;
337 }
338
339 return mDriverNamespace;
340}
341
342std::string GraphicsEnv::getDriverPath() const {
343 return mDriverPath;
344}
345
346/**
347 * APIs for GpuStats
348 */
349
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700350void GraphicsEnv::hintActivityLaunch() {
351 ATRACE_CALL();
352
Yiwei Zhangd3819382020-01-07 19:53:56 -0800353 {
354 std::lock_guard<std::mutex> lock(mStatsLock);
355 if (mActivityLaunched) return;
356 mActivityLaunched = true;
357 }
358
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700359 std::thread trySendGpuStatsThread([this]() {
360 // If there's already graphics driver preloaded in the process, just send
361 // the stats info to GpuStats directly through async binder.
362 std::lock_guard<std::mutex> lock(mStatsLock);
363 if (mGpuStats.glDriverToSend) {
364 mGpuStats.glDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700365 sendGpuStatsLocked(GpuStatsInfo::Api::API_GL, true, mGpuStats.glDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700366 }
367 if (mGpuStats.vkDriverToSend) {
368 mGpuStats.vkDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700369 sendGpuStatsLocked(GpuStatsInfo::Api::API_VK, true, mGpuStats.vkDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700370 }
371 });
372 trySendGpuStatsThread.detach();
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700373}
374
Greg Kaiser210bb7e2019-02-12 12:40:05 -0800375void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800376 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700377 int64_t driverBuildTime, const std::string& appPackageName,
378 const int vulkanVersion) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800379 ATRACE_CALL();
380
Yiwei Zhangd9861812019-02-13 11:51:55 -0800381 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800382 ALOGV("setGpuStats:\n"
383 "\tdriverPackageName[%s]\n"
384 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800385 "\tdriverVersionCode[%" PRIu64 "]\n"
386 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700387 "\tappPackageName[%s]\n"
388 "\tvulkanVersion[%d]\n",
Yiwei Zhang96c01712019-02-19 16:00:25 -0800389 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700390 appPackageName.c_str(), vulkanVersion);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800391
Yiwei Zhangd9861812019-02-13 11:51:55 -0800392 mGpuStats.driverPackageName = driverPackageName;
393 mGpuStats.driverVersionName = driverVersionName;
394 mGpuStats.driverVersionCode = driverVersionCode;
Yiwei Zhang96c01712019-02-19 16:00:25 -0800395 mGpuStats.driverBuildTime = driverBuildTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800396 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang794d2952019-05-06 17:43:59 -0700397 mGpuStats.vulkanVersion = vulkanVersion;
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800398}
399
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700400void GraphicsEnv::setDriverToLoad(GpuStatsInfo::Driver driver) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800401 ATRACE_CALL();
402
403 std::lock_guard<std::mutex> lock(mStatsLock);
404 switch (driver) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700405 case GpuStatsInfo::Driver::GL:
406 case GpuStatsInfo::Driver::GL_UPDATED:
Solti371af852024-08-22 21:22:19 +0000407 case GpuStatsInfo::Driver::ANGLE:
408 mGpuStats.glDriverToLoad = driver;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800409 break;
Solti371af852024-08-22 21:22:19 +0000410
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700411 case GpuStatsInfo::Driver::VULKAN:
412 case GpuStatsInfo::Driver::VULKAN_UPDATED: {
Yiwei Zhang472cab02019-08-05 17:57:41 -0700413 if (mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::NONE ||
414 mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::VULKAN) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800415 mGpuStats.vkDriverToLoad = driver;
416 break;
417 }
418
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700419 if (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800420 mGpuStats.vkDriverFallback = driver;
421 }
422 break;
423 }
424 default:
425 break;
426 }
427}
428
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700429void GraphicsEnv::setDriverLoaded(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700430 int64_t driverLoadingTime) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800431 ATRACE_CALL();
432
433 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700434 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhangd3819382020-01-07 19:53:56 -0800435 mGpuStats.glDriverToSend = true;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700436 mGpuStats.glDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800437 } else {
Yiwei Zhangd3819382020-01-07 19:53:56 -0800438 mGpuStats.vkDriverToSend = true;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700439 mGpuStats.vkDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800440 }
441
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700442 sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800443}
444
Serdar Kocdemirb2901c92022-11-17 00:39:05 +0000445// Hash function to calculate hash for null-terminated Vulkan extension names
446// We store hash values of the extensions, rather than the actual names or
447// indices to be able to support new extensions easily, avoid creating
448// a table of 'known' extensions inside Android and reduce the runtime overhead.
449static uint64_t calculateExtensionHash(const char* word) {
450 if (!word) {
451 return 0;
452 }
453 const size_t wordLen = strlen(word);
454 const uint32_t seed = 167;
455 uint64_t hash = 0;
456 for (size_t i = 0; i < wordLen; i++) {
457 hash = (hash * seed) + word[i];
458 }
459 return hash;
460}
461
462void GraphicsEnv::setVulkanInstanceExtensions(uint32_t enabledExtensionCount,
463 const char* const* ppEnabledExtensionNames) {
464 ATRACE_CALL();
465 if (enabledExtensionCount == 0 || ppEnabledExtensionNames == nullptr) {
466 return;
467 }
468
469 const uint32_t maxNumStats = android::GpuStatsAppInfo::MAX_NUM_EXTENSIONS;
470 uint64_t extensionHashes[maxNumStats];
471 const uint32_t numStats = std::min(enabledExtensionCount, maxNumStats);
472 for(uint32_t i = 0; i < numStats; i++) {
473 extensionHashes[i] = calculateExtensionHash(ppEnabledExtensionNames[i]);
474 }
475 setTargetStatsArray(android::GpuStatsInfo::Stats::VULKAN_INSTANCE_EXTENSION,
476 extensionHashes, numStats);
477}
478
479void GraphicsEnv::setVulkanDeviceExtensions(uint32_t enabledExtensionCount,
480 const char* const* ppEnabledExtensionNames) {
481 ATRACE_CALL();
482 if (enabledExtensionCount == 0 || ppEnabledExtensionNames == nullptr) {
483 return;
484 }
485
486 const uint32_t maxNumStats = android::GpuStatsAppInfo::MAX_NUM_EXTENSIONS;
487 uint64_t extensionHashes[maxNumStats];
488 const uint32_t numStats = std::min(enabledExtensionCount, maxNumStats);
489 for(uint32_t i = 0; i < numStats; i++) {
490 extensionHashes[i] = calculateExtensionHash(ppEnabledExtensionNames[i]);
491 }
492 setTargetStatsArray(android::GpuStatsInfo::Stats::VULKAN_DEVICE_EXTENSION,
493 extensionHashes, numStats);
494}
495
Tom Murphyc23fcd02024-03-13 10:22:06 +0000496void GraphicsEnv::addVulkanEngineName(const char* engineName) {
497 ATRACE_CALL();
498 if (engineName == nullptr) {
499 return;
500 }
501 std::lock_guard<std::mutex> lock(mStatsLock);
502 if (!readyToSendGpuStatsLocked()) return;
503
504 const sp<IGpuService> gpuService = getGpuService();
505 if (gpuService) {
506 gpuService->addVulkanEngineName(mGpuStats.appPackageName, mGpuStats.driverVersionCode,
507 engineName);
508 }
509}
510
Yiwei Zhangd3819382020-01-07 19:53:56 -0800511bool GraphicsEnv::readyToSendGpuStatsLocked() {
512 // Only send stats for processes having at least one activity launched and that process doesn't
513 // skip the GraphicsEnvironment setup.
514 return mActivityLaunched && !mGpuStats.appPackageName.empty();
515}
516
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700517void GraphicsEnv::setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value) {
Serdar Kocdemirb2901c92022-11-17 00:39:05 +0000518 return setTargetStatsArray(stats, &value, 1);
519}
520
521void GraphicsEnv::setTargetStatsArray(const GpuStatsInfo::Stats stats, const uint64_t* values,
522 const uint32_t valueCount) {
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700523 ATRACE_CALL();
524
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700525 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangd3819382020-01-07 19:53:56 -0800526 if (!readyToSendGpuStatsLocked()) return;
527
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700528 const sp<IGpuService> gpuService = getGpuService();
529 if (gpuService) {
Serdar Kocdemirb2901c92022-11-17 00:39:05 +0000530 gpuService->setTargetStatsArray(mGpuStats.appPackageName, mGpuStats.driverVersionCode,
531 stats, values, valueCount);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700532 }
533}
534
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700535void GraphicsEnv::sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800536 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800537 ATRACE_CALL();
538
Yiwei Zhangd3819382020-01-07 19:53:56 -0800539 if (!readyToSendGpuStatsLocked()) return;
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800540
541 ALOGV("sendGpuStats:\n"
542 "\tdriverPackageName[%s]\n"
543 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800544 "\tdriverVersionCode[%" PRIu64 "]\n"
545 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800546 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700547 "\tvulkanVersion[%d]\n"
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700548 "\tapi[%d]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800549 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800550 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800551 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-19 16:00:25 -0800552 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700553 mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime);
554
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700555 GpuStatsInfo::Driver driver = GpuStatsInfo::Driver::NONE;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700556 bool isIntendedDriverLoaded = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700557 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700558 driver = mGpuStats.glDriverToLoad;
Solti371af852024-08-22 21:22:19 +0000559 isIntendedDriverLoaded = isDriverLoaded;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700560 } else {
561 driver = mGpuStats.vkDriverToLoad;
562 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700563 isDriverLoaded && (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700564 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800565
Yiwei Zhangd9861812019-02-13 11:51:55 -0800566 const sp<IGpuService> gpuService = getGpuService();
567 if (gpuService) {
568 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800569 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700570 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700571 isIntendedDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800572 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800573}
574
Adam Bodnar0afcca02019-09-17 13:23:17 -0700575bool GraphicsEnv::setInjectLayersPrSetDumpable() {
576 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
577 return false;
578 }
579 return true;
580}
581
Peiyong Linef20e4e2023-07-03 02:33:00 +0000582/**
583 * APIs for ANGLE
584 */
585
Tim Van Patten5f744f12018-12-12 11:46:21 -0700586bool GraphicsEnv::shouldUseAngle() {
587 // Make sure we are init'ed
Peiyong Lin2f51e752023-06-15 22:35:14 +0000588 if (mPackageName.empty()) {
589 ALOGV("Package name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700590 return false;
591 }
592
Peiyong Lin17190c92023-06-21 07:03:47 +0000593 return mShouldUseAngle;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700594}
595
Peiyong Lin2df5a002023-08-01 23:31:34 +0000596// Set ANGLE information.
597// If path is "system", it means system ANGLE must be used for the process.
598// If shouldUseNativeDriver is true, it means native GLES drivers must be used for the process.
599// If path is set to nonempty and shouldUseNativeDriver is true, ANGLE will be used regardless.
600void GraphicsEnv::setAngleInfo(const std::string& path, const bool shouldUseNativeDriver,
Peiyong Lin17190c92023-06-21 07:03:47 +0000601 const std::string& packageName,
Tim Van Pattenca7ced32025-01-03 22:54:05 +0000602 const std::vector<std::string>& eglFeatures) {
Peiyong Lin17190c92023-06-21 07:03:47 +0000603 if (mShouldUseAngle) {
604 // ANGLE is already set up for this application process, even if the application
605 // needs to switch from apk to system or vice versa, the application process must
606 // be killed and relaunch so that the loader can properly load ANGLE again.
607 // The architecture does not support runtime switch between drivers, so just return.
608 ALOGE("ANGLE is already set for %s", packageName.c_str());
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700609 return;
610 }
611
Tim Van Pattenca7ced32025-01-03 22:54:05 +0000612 mAngleEglFeatures = eglFeatures;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700613 ALOGV("setting ANGLE path to '%s'", path.c_str());
Tim Van Pattenca7ced32025-01-03 22:54:05 +0000614 mAnglePath = path;
Peiyong Lin2f51e752023-06-15 22:35:14 +0000615 ALOGV("setting app package name to '%s'", packageName.c_str());
Tim Van Pattenca7ced32025-01-03 22:54:05 +0000616 mPackageName = packageName;
Peiyong Lin2df5a002023-08-01 23:31:34 +0000617 if (mAnglePath == "system") {
618 mShouldUseSystemAngle = true;
619 }
620 if (!mAnglePath.empty()) {
621 mShouldUseAngle = true;
622 }
623 mShouldUseNativeDriver = shouldUseNativeDriver;
Cody Northrop1f00e172018-04-02 11:23:31 -0600624}
625
Peiyong Lin2f51e752023-06-15 22:35:14 +0000626std::string& GraphicsEnv::getPackageName() {
627 return mPackageName;
Cody Northrop04e70432018-09-06 10:34:58 -0600628}
629
Tim Van Patten93ef6362025-01-10 16:47:27 -0700630// List of ANGLE features to enable, specified in the Global.Settings value "angle_egl_features".
Peiyong Lin2f707e62020-09-26 13:52:10 -0700631const std::vector<std::string>& GraphicsEnv::getAngleEglFeatures() {
632 return mAngleEglFeatures;
633}
634
Tim Van Patten93ef6362025-01-10 16:47:27 -0700635void GraphicsEnv::getAngleFeatureOverrides(std::vector<const char*>& enabled,
636 std::vector<const char*>& disabled) {
637 if (!graphicsenv_flags::feature_overrides()) {
638 return;
639 }
640
641 for (const FeatureConfig& feature : mFeatureOverrides.mGlobalFeatures) {
642 if (feature.mEnabled) {
643 enabled.push_back(feature.mFeatureName.c_str());
644 } else {
645 disabled.push_back(feature.mFeatureName.c_str());
646 }
647 }
648
649 if (mFeatureOverrides.mPackageFeatures.count(mPackageName)) {
650 for (const FeatureConfig& feature : mFeatureOverrides.mPackageFeatures[mPackageName]) {
651 if (feature.mEnabled) {
652 enabled.push_back(feature.mFeatureName.c_str());
653 } else {
654 disabled.push_back(feature.mFeatureName.c_str());
655 }
656 }
657 }
658}
659
Cody Northrop1f00e172018-04-02 11:23:31 -0600660android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700661 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600662
Cody Northrop3892cfa2019-01-30 10:03:12 -0700663 if (mAngleNamespace) {
664 return mAngleNamespace;
665 }
666
Peiyong Line4da7a82023-08-08 20:46:53 +0000667 // If ANGLE path is not set, it means ANGLE should not be used for this process;
668 // or if ANGLE path is set and set to use system ANGLE, then a namespace is not needed
669 // because:
670 // 1) if the default OpenGL ES driver is already ANGLE, then the loader will skip;
671 // 2) if the default OpenGL ES driver is native, then there's no symbol conflict;
672 // 3) if there's no OpenGL ES driver is preloaded, then there's no symbol conflict.
673 if (mAnglePath.empty() || mShouldUseSystemAngle) {
674 ALOGV("mAnglePath is empty or use system ANGLE, abort creating ANGLE namespace");
Cody Northrop3892cfa2019-01-30 10:03:12 -0700675 return nullptr;
676 }
677
Peiyong Lin17190c92023-06-21 07:03:47 +0000678 // Construct the search paths for system ANGLE.
679 const char* const defaultLibraryPaths =
680#if defined(__LP64__)
Peiyong Line4da7a82023-08-08 20:46:53 +0000681 "/vendor/lib64/egl:/system/lib64";
Peiyong Lin17190c92023-06-21 07:03:47 +0000682#else
Peiyong Line4da7a82023-08-08 20:46:53 +0000683 "/vendor/lib/egl:/system/lib";
Peiyong Lin17190c92023-06-21 07:03:47 +0000684#endif
685
686 // If the application process will run on top of system ANGLE, construct the namespace
687 // with sphal namespace being the parent namespace so that search paths and libraries
688 // are properly inherited.
689 mAngleNamespace =
690 android_create_namespace("ANGLE",
Peiyong Line88e0ba2023-06-28 15:09:09 +0000691 mShouldUseSystemAngle ? defaultLibraryPaths
692 : mAnglePath.c_str(), // ld_library_path
693 mShouldUseSystemAngle
694 ? defaultLibraryPaths
695 : mAnglePath.c_str(), // default_library_path
Peiyong Lin17190c92023-06-21 07:03:47 +0000696 ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED,
697 nullptr, // permitted_when_isolated_path
Peiyong Line88e0ba2023-06-28 15:09:09 +0000698 mShouldUseSystemAngle ? android_get_exported_namespace("sphal")
699 : nullptr); // parent
Cody Northrop3892cfa2019-01-30 10:03:12 -0700700
701 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600702
Peiyong Line88e0ba2023-06-28 15:09:09 +0000703 if (!mShouldUseSystemAngle) {
Peiyong Lin17190c92023-06-21 07:03:47 +0000704 return mAngleNamespace;
705 }
706
Chris Forbes0cdac9a2024-08-16 15:12:27 +1200707 auto vndkNamespace = android_get_exported_namespace(isVndkEnabled() ? "vndk" : "sphal");
Peiyong Lin17190c92023-06-21 07:03:47 +0000708 if (!vndkNamespace) {
Ethan Chenf5da7ee2024-01-18 19:47:01 -0800709 mAngleNamespace = nullptr;
710 return mAngleNamespace;
Peiyong Lin17190c92023-06-21 07:03:47 +0000711 }
712
713 if (!linkDriverNamespaceLocked(mAngleNamespace, vndkNamespace, "")) {
714 mAngleNamespace = nullptr;
715 }
716
Cody Northrop1f00e172018-04-02 11:23:31 -0600717 return mAngleNamespace;
718}
719
Yuxin Hub69e9882023-04-13 03:51:41 +0000720void GraphicsEnv::nativeToggleAngleAsSystemDriver(bool enabled) {
721 const sp<IGpuService> gpuService = getGpuService();
722 if (!gpuService) {
723 ALOGE("No GPU service");
724 return;
725 }
726 gpuService->toggleAngleAsSystemDriver(enabled);
727}
728
Peiyong Line88e0ba2023-06-28 15:09:09 +0000729bool GraphicsEnv::shouldUseSystemAngle() {
730 return mShouldUseSystemAngle;
731}
732
Peiyong Lin2df5a002023-08-01 23:31:34 +0000733bool GraphicsEnv::shouldUseNativeDriver() {
734 return mShouldUseNativeDriver;
735}
736
Peiyong Linef20e4e2023-07-03 02:33:00 +0000737/**
738 * APIs for debuggable layers
739 */
740
741void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace,
742 const std::string& layerPaths) {
743 if (mLayerPaths.empty()) {
744 mLayerPaths = layerPaths;
745 mAppNamespace = appNamespace;
746 } else {
747 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
748 layerPaths.c_str(), appNamespace);
749 }
750}
751
752NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
753 return mAppNamespace;
754}
755
756const std::string& GraphicsEnv::getLayerPaths() {
757 return mLayerPaths;
758}
759
760const std::string& GraphicsEnv::getDebugLayers() {
761 return mDebugLayers;
762}
763
764const std::string& GraphicsEnv::getDebugLayersGLES() {
765 return mDebugLayersGLES;
766}
767
768void GraphicsEnv::setDebugLayers(const std::string& layers) {
769 mDebugLayers = layers;
770}
771
772void GraphicsEnv::setDebugLayersGLES(const std::string& layers) {
773 mDebugLayersGLES = layers;
774}
775
Jesse Hall90b25ed2016-12-12 12:56:46 -0800776} // namespace android