blob: d1a56635f5371936ccd954aa55d2eba6ee3c8a26 [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
Chris Forbes0cdac9a2024-08-16 15:12:27 +120086// List of libraries that were previously available via VNDK-SP,
87// and are now available via SPHAL.
88// On modern devices that lack the VNDK APEX, the device no longer
89// contains a helpful list of these libraries on the filesystem as above.
90// See system/sepolicy/vendor/file_contexts
91static const char* kFormerlyVndkspLibrariesList =
92 "android.hardware.common-V2-ndk.so:"
93 "android.hardware.common.fmq-V1-ndk.so:"
94 "android.hardware.graphics.allocator-V2-ndk.so:"
95 "android.hardware.graphics.common-V5-ndk.so:"
96 "android.hardware.graphics.common@1.0.so:"
97 "android.hardware.graphics.common@1.1.so:"
98 "android.hardware.graphics.common@1.2.so:"
99 "android.hardware.graphics.composer3-V1-ndk.so:"
100 "android.hardware.graphics.mapper@2.0.so:"
101 "android.hardware.graphics.mapper@2.1.so:"
102 "android.hardware.graphics.mapper@3.0.so:"
103 "android.hardware.graphics.mapper@4.0.so:"
104 "android.hardware.renderscript@1.0.so:"
105 "android.hidl.memory.token@1.0.so:"
106 "android.hidl.memory@1.0-impl.so:"
107 "android.hidl.memory@1.0.so:"
108 "android.hidl.safe_union@1.0.so:"
109 "libRSCpuRef.so:"
110 "libRSDriver.so:"
111 "libRS_internal.so:"
112 "libbacktrace.so:"
113 "libbase.so:"
114 "libbcinfo.so:"
115 "libblas.so:"
116 "libc++.so:"
117 "libcompiler_rt.so:"
118 "libcutils.so:"
119 "libdmabufheap.so:"
120 "libft2.so:"
121 "libgralloctypes.so:"
122 "libhardware.so:"
123 "libhidlbase.so:"
124 "libhidlmemory.so:"
125 "libion.so:"
126 "libjsoncpp.so:"
127 "liblzma.so:"
128 "libpng.so:"
129 "libprocessgroup.so:"
130 "libunwindstack.so:"
131 "libutils.so:"
132 "libutilscallstack.so:"
133 "libz.so";
134
Yiwei Zhang64d89212018-11-27 19:58:29 -0800135static std::string vndkVersionStr() {
136#ifdef __BIONIC__
Michael Hoisied1023f22020-03-26 22:52:58 -0700137 return base::GetProperty("ro.vndk.version", "");
Yiwei Zhang64d89212018-11-27 19:58:29 -0800138#endif
139 return "";
140}
141
142static void insertVndkVersionStr(std::string* fileName) {
143 LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr");
Jooyung Han78396802020-02-23 03:02:43 +0900144 std::string version = vndkVersionStr();
145 size_t pos = fileName->find("{}");
146 while (pos != std::string::npos) {
147 fileName->replace(pos, 2, version);
148 pos = fileName->find("{}", pos + version.size());
Yiwei Zhang64d89212018-11-27 19:58:29 -0800149 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800150}
151
152static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) {
153 // Read list of public native libraries from the config file.
154 std::string fileContent;
155 if (!base::ReadFileToString(configFile, &fileContent)) {
156 return false;
157 }
158
159 std::vector<std::string> lines = base::Split(fileContent, "\n");
160
161 for (auto& line : lines) {
162 auto trimmedLine = base::Trim(line);
163 if (!trimmedLine.empty()) {
164 soNames->push_back(trimmedLine);
165 }
166 }
167
168 return true;
169}
170
171static const std::string getSystemNativeLibraries(NativeLibrary type) {
Kiyoung Kimc0e3ed32023-08-11 11:22:15 +0900172 std::string nativeLibrariesSystemConfig = "";
173
Chris Forbes0cdac9a2024-08-16 15:12:27 +1200174 if (!isVndkEnabled()) {
175 if (type == NativeLibrary::VNDKSP) {
176 return kFormerlyVndkspLibrariesList;
177 } else {
178 nativeLibrariesSystemConfig = kLlndkLibrariesTxtPath;
179 }
Kiyoung Kimc0e3ed32023-08-11 11:22:15 +0900180 } else {
181 nativeLibrariesSystemConfig = kNativeLibrariesSystemConfigPath[type];
182 insertVndkVersionStr(&nativeLibrariesSystemConfig);
183 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800184
185 std::vector<std::string> soNames;
186 if (!readConfig(nativeLibrariesSystemConfig, &soNames)) {
187 ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str());
188 return "";
189 }
190
191 return base::Join(soNames, ':');
192}
193
Peiyong Linef20e4e2023-07-03 02:33:00 +0000194static sp<IGpuService> getGpuService() {
195 static const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
196 if (!binder) {
197 ALOGE("Failed to get gpu service");
198 return nullptr;
199 }
200
201 return interface_cast<IGpuService>(binder);
202}
203
Jesse Hall90b25ed2016-12-12 12:56:46 -0800204/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
205 static GraphicsEnv env;
206 return env;
207}
208
Yiwei Zhang6a674c92019-11-08 11:55:36 -0800209bool GraphicsEnv::isDebuggable() {
Cody Northrop383832f2022-11-01 13:54:57 -0600210 // This flag determines if the application is marked debuggable
211 bool appDebuggable = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) > 0;
212
213 // This flag is set only in `debuggable` builds of the platform
214#if defined(ANDROID_DEBUGGABLE)
215 bool platformDebuggable = true;
216#else
217 bool platformDebuggable = false;
218#endif
219
220 ALOGV("GraphicsEnv::isDebuggable returning appDebuggable=%s || platformDebuggable=%s",
221 appDebuggable ? "true" : "false", platformDebuggable ? "true" : "false");
222
223 return appDebuggable || platformDebuggable;
Cody Northrop629ce4e2018-10-15 07:22:09 -0600224}
225
Peiyong Linef20e4e2023-07-03 02:33:00 +0000226/**
227 * APIs for updatable graphics drivers
228 */
229
Peiyong Lin2f51e752023-06-15 22:35:14 +0000230void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string& path,
231 const std::string& sphalLibraries) {
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800232 if (!mDriverPath.empty() || !mSphalLibraries.empty()) {
233 ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries "
234 "from '%s' to '%s'",
235 mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800236 return;
237 }
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800238 ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(),
239 sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800240 mDriverPath = path;
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800241 mSphalLibraries = sphalLibraries;
Jesse Hall90b25ed2016-12-12 12:56:46 -0800242}
243
Peiyong Linef20e4e2023-07-03 02:33:00 +0000244// Return true if all the required libraries from vndk and sphal namespace are
245// linked to the driver namespace correctly.
246bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* destNamespace,
247 android_namespace_t* vndkNamespace,
248 const std::string& sharedSphalLibraries) {
249 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
250 if (llndkLibraries.empty()) {
251 return false;
252 }
253 if (!android_link_namespaces(destNamespace, nullptr, llndkLibraries.c_str())) {
254 ALOGE("Failed to link default namespace[%s]", dlerror());
255 return false;
256 }
257
258 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
259 if (vndkspLibraries.empty()) {
260 return false;
261 }
262 if (!android_link_namespaces(destNamespace, vndkNamespace, vndkspLibraries.c_str())) {
263 ALOGE("Failed to link vndk namespace[%s]", dlerror());
264 return false;
265 }
266
267 if (sharedSphalLibraries.empty()) {
268 return true;
269 }
270
271 // Make additional libraries in sphal to be accessible
272 auto sphalNamespace = android_get_exported_namespace("sphal");
273 if (!sphalNamespace) {
274 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
275 sharedSphalLibraries.c_str());
276 return false;
277 }
278
279 if (!android_link_namespaces(destNamespace, sphalNamespace, sharedSphalLibraries.c_str())) {
280 ALOGE("Failed to link sphal namespace[%s]", dlerror());
281 return false;
282 }
283
284 return true;
285}
286
287android_namespace_t* GraphicsEnv::getDriverNamespace() {
288 std::lock_guard<std::mutex> lock(mNamespaceMutex);
289
290 if (mDriverNamespace) {
291 return mDriverNamespace;
292 }
293
294 if (mDriverPath.empty()) {
295 // For an application process, driver path is empty means this application is not opted in
296 // to use updatable driver. Application process doesn't have the ability to set up
297 // environment variables and hence before `getenv` call will return.
298 // For a process that is not an application process, if it's run from an environment,
299 // for example shell, where environment variables can be set, then it can opt into using
300 // udpatable driver by setting UPDATABLE_GFX_DRIVER to 1. By setting to 1 the developer
301 // driver will be used currently.
302 // TODO(b/159240322) Support the production updatable driver.
303 const char* id = getenv("UPDATABLE_GFX_DRIVER");
304 if (id == nullptr || std::strcmp(id, "1") != 0) {
305 return nullptr;
306 }
307 const sp<IGpuService> gpuService = getGpuService();
308 if (!gpuService) {
309 return nullptr;
310 }
311 mDriverPath = gpuService->getUpdatableDriverPath();
312 if (mDriverPath.empty()) {
313 return nullptr;
314 }
315 mDriverPath.append(UPDATABLE_DRIVER_ABI);
316 ALOGI("Driver path is setup via UPDATABLE_GFX_DRIVER: %s", mDriverPath.c_str());
317 }
318
Chris Forbes0cdac9a2024-08-16 15:12:27 +1200319 auto vndkNamespace = android_get_exported_namespace(isVndkEnabled() ? "vndk" : "sphal");
Peiyong Linef20e4e2023-07-03 02:33:00 +0000320 if (!vndkNamespace) {
Ethan Chenf5da7ee2024-01-18 19:47:01 -0800321 mDriverNamespace = nullptr;
322 return mDriverNamespace;
Peiyong Linef20e4e2023-07-03 02:33:00 +0000323 }
324
325 mDriverNamespace = android_create_namespace("updatable gfx driver",
326 mDriverPath.c_str(), // ld_library_path
327 mDriverPath.c_str(), // default_library_path
328 ANDROID_NAMESPACE_TYPE_ISOLATED,
329 nullptr, // permitted_when_isolated_path
330 nullptr);
331
332 if (!linkDriverNamespaceLocked(mDriverNamespace, vndkNamespace, mSphalLibraries)) {
333 mDriverNamespace = nullptr;
334 }
335
336 return mDriverNamespace;
337}
338
339std::string GraphicsEnv::getDriverPath() const {
340 return mDriverPath;
341}
342
343/**
344 * APIs for GpuStats
345 */
346
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700347void GraphicsEnv::hintActivityLaunch() {
348 ATRACE_CALL();
349
Yiwei Zhangd3819382020-01-07 19:53:56 -0800350 {
351 std::lock_guard<std::mutex> lock(mStatsLock);
352 if (mActivityLaunched) return;
353 mActivityLaunched = true;
354 }
355
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700356 std::thread trySendGpuStatsThread([this]() {
357 // If there's already graphics driver preloaded in the process, just send
358 // the stats info to GpuStats directly through async binder.
359 std::lock_guard<std::mutex> lock(mStatsLock);
360 if (mGpuStats.glDriverToSend) {
361 mGpuStats.glDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700362 sendGpuStatsLocked(GpuStatsInfo::Api::API_GL, true, mGpuStats.glDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700363 }
364 if (mGpuStats.vkDriverToSend) {
365 mGpuStats.vkDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700366 sendGpuStatsLocked(GpuStatsInfo::Api::API_VK, true, mGpuStats.vkDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700367 }
368 });
369 trySendGpuStatsThread.detach();
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700370}
371
Greg Kaiser210bb7e2019-02-12 12:40:05 -0800372void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800373 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700374 int64_t driverBuildTime, const std::string& appPackageName,
375 const int vulkanVersion) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800376 ATRACE_CALL();
377
Yiwei Zhangd9861812019-02-13 11:51:55 -0800378 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800379 ALOGV("setGpuStats:\n"
380 "\tdriverPackageName[%s]\n"
381 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800382 "\tdriverVersionCode[%" PRIu64 "]\n"
383 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700384 "\tappPackageName[%s]\n"
385 "\tvulkanVersion[%d]\n",
Yiwei Zhang96c01712019-02-19 16:00:25 -0800386 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700387 appPackageName.c_str(), vulkanVersion);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800388
Yiwei Zhangd9861812019-02-13 11:51:55 -0800389 mGpuStats.driverPackageName = driverPackageName;
390 mGpuStats.driverVersionName = driverVersionName;
391 mGpuStats.driverVersionCode = driverVersionCode;
Yiwei Zhang96c01712019-02-19 16:00:25 -0800392 mGpuStats.driverBuildTime = driverBuildTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800393 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang794d2952019-05-06 17:43:59 -0700394 mGpuStats.vulkanVersion = vulkanVersion;
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800395}
396
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700397void GraphicsEnv::setDriverToLoad(GpuStatsInfo::Driver driver) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800398 ATRACE_CALL();
399
400 std::lock_guard<std::mutex> lock(mStatsLock);
401 switch (driver) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700402 case GpuStatsInfo::Driver::GL:
403 case GpuStatsInfo::Driver::GL_UPDATED:
Solti371af852024-08-22 21:22:19 +0000404 case GpuStatsInfo::Driver::ANGLE:
405 mGpuStats.glDriverToLoad = driver;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800406 break;
Solti371af852024-08-22 21:22:19 +0000407
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700408 case GpuStatsInfo::Driver::VULKAN:
409 case GpuStatsInfo::Driver::VULKAN_UPDATED: {
Yiwei Zhang472cab02019-08-05 17:57:41 -0700410 if (mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::NONE ||
411 mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::VULKAN) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800412 mGpuStats.vkDriverToLoad = driver;
413 break;
414 }
415
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700416 if (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800417 mGpuStats.vkDriverFallback = driver;
418 }
419 break;
420 }
421 default:
422 break;
423 }
424}
425
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700426void GraphicsEnv::setDriverLoaded(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700427 int64_t driverLoadingTime) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800428 ATRACE_CALL();
429
430 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700431 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhangd3819382020-01-07 19:53:56 -0800432 mGpuStats.glDriverToSend = true;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700433 mGpuStats.glDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800434 } else {
Yiwei Zhangd3819382020-01-07 19:53:56 -0800435 mGpuStats.vkDriverToSend = true;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700436 mGpuStats.vkDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800437 }
438
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700439 sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800440}
441
Serdar Kocdemirb2901c92022-11-17 00:39:05 +0000442// Hash function to calculate hash for null-terminated Vulkan extension names
443// We store hash values of the extensions, rather than the actual names or
444// indices to be able to support new extensions easily, avoid creating
445// a table of 'known' extensions inside Android and reduce the runtime overhead.
446static uint64_t calculateExtensionHash(const char* word) {
447 if (!word) {
448 return 0;
449 }
450 const size_t wordLen = strlen(word);
451 const uint32_t seed = 167;
452 uint64_t hash = 0;
453 for (size_t i = 0; i < wordLen; i++) {
454 hash = (hash * seed) + word[i];
455 }
456 return hash;
457}
458
459void GraphicsEnv::setVulkanInstanceExtensions(uint32_t enabledExtensionCount,
460 const char* const* ppEnabledExtensionNames) {
461 ATRACE_CALL();
462 if (enabledExtensionCount == 0 || ppEnabledExtensionNames == nullptr) {
463 return;
464 }
465
466 const uint32_t maxNumStats = android::GpuStatsAppInfo::MAX_NUM_EXTENSIONS;
467 uint64_t extensionHashes[maxNumStats];
468 const uint32_t numStats = std::min(enabledExtensionCount, maxNumStats);
469 for(uint32_t i = 0; i < numStats; i++) {
470 extensionHashes[i] = calculateExtensionHash(ppEnabledExtensionNames[i]);
471 }
472 setTargetStatsArray(android::GpuStatsInfo::Stats::VULKAN_INSTANCE_EXTENSION,
473 extensionHashes, numStats);
474}
475
476void GraphicsEnv::setVulkanDeviceExtensions(uint32_t enabledExtensionCount,
477 const char* const* ppEnabledExtensionNames) {
478 ATRACE_CALL();
479 if (enabledExtensionCount == 0 || ppEnabledExtensionNames == nullptr) {
480 return;
481 }
482
483 const uint32_t maxNumStats = android::GpuStatsAppInfo::MAX_NUM_EXTENSIONS;
484 uint64_t extensionHashes[maxNumStats];
485 const uint32_t numStats = std::min(enabledExtensionCount, maxNumStats);
486 for(uint32_t i = 0; i < numStats; i++) {
487 extensionHashes[i] = calculateExtensionHash(ppEnabledExtensionNames[i]);
488 }
489 setTargetStatsArray(android::GpuStatsInfo::Stats::VULKAN_DEVICE_EXTENSION,
490 extensionHashes, numStats);
491}
492
Tom Murphyc23fcd02024-03-13 10:22:06 +0000493void GraphicsEnv::addVulkanEngineName(const char* engineName) {
494 ATRACE_CALL();
495 if (engineName == nullptr) {
496 return;
497 }
498 std::lock_guard<std::mutex> lock(mStatsLock);
499 if (!readyToSendGpuStatsLocked()) return;
500
501 const sp<IGpuService> gpuService = getGpuService();
502 if (gpuService) {
503 gpuService->addVulkanEngineName(mGpuStats.appPackageName, mGpuStats.driverVersionCode,
504 engineName);
505 }
506}
507
Yiwei Zhangd3819382020-01-07 19:53:56 -0800508bool GraphicsEnv::readyToSendGpuStatsLocked() {
509 // Only send stats for processes having at least one activity launched and that process doesn't
510 // skip the GraphicsEnvironment setup.
511 return mActivityLaunched && !mGpuStats.appPackageName.empty();
512}
513
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700514void GraphicsEnv::setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value) {
Serdar Kocdemirb2901c92022-11-17 00:39:05 +0000515 return setTargetStatsArray(stats, &value, 1);
516}
517
518void GraphicsEnv::setTargetStatsArray(const GpuStatsInfo::Stats stats, const uint64_t* values,
519 const uint32_t valueCount) {
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700520 ATRACE_CALL();
521
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700522 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangd3819382020-01-07 19:53:56 -0800523 if (!readyToSendGpuStatsLocked()) return;
524
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700525 const sp<IGpuService> gpuService = getGpuService();
526 if (gpuService) {
Serdar Kocdemirb2901c92022-11-17 00:39:05 +0000527 gpuService->setTargetStatsArray(mGpuStats.appPackageName, mGpuStats.driverVersionCode,
528 stats, values, valueCount);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700529 }
530}
531
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700532void GraphicsEnv::sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800533 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800534 ATRACE_CALL();
535
Yiwei Zhangd3819382020-01-07 19:53:56 -0800536 if (!readyToSendGpuStatsLocked()) return;
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800537
538 ALOGV("sendGpuStats:\n"
539 "\tdriverPackageName[%s]\n"
540 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800541 "\tdriverVersionCode[%" PRIu64 "]\n"
542 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800543 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700544 "\tvulkanVersion[%d]\n"
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700545 "\tapi[%d]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800546 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800547 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800548 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-19 16:00:25 -0800549 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700550 mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime);
551
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700552 GpuStatsInfo::Driver driver = GpuStatsInfo::Driver::NONE;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700553 bool isIntendedDriverLoaded = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700554 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700555 driver = mGpuStats.glDriverToLoad;
Solti371af852024-08-22 21:22:19 +0000556 isIntendedDriverLoaded = isDriverLoaded;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700557 } else {
558 driver = mGpuStats.vkDriverToLoad;
559 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700560 isDriverLoaded && (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700561 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800562
Yiwei Zhangd9861812019-02-13 11:51:55 -0800563 const sp<IGpuService> gpuService = getGpuService();
564 if (gpuService) {
565 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800566 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700567 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700568 isIntendedDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800569 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800570}
571
Adam Bodnar0afcca02019-09-17 13:23:17 -0700572bool GraphicsEnv::setInjectLayersPrSetDumpable() {
573 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
574 return false;
575 }
576 return true;
577}
578
Peiyong Linef20e4e2023-07-03 02:33:00 +0000579/**
580 * APIs for ANGLE
581 */
582
Tim Van Patten5f744f12018-12-12 11:46:21 -0700583bool GraphicsEnv::shouldUseAngle() {
584 // Make sure we are init'ed
Peiyong Lin2f51e752023-06-15 22:35:14 +0000585 if (mPackageName.empty()) {
586 ALOGV("Package name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700587 return false;
588 }
589
Peiyong Lin17190c92023-06-21 07:03:47 +0000590 return mShouldUseAngle;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700591}
592
Peiyong Lin2df5a002023-08-01 23:31:34 +0000593// Set ANGLE information.
594// If path is "system", it means system ANGLE must be used for the process.
595// If shouldUseNativeDriver is true, it means native GLES drivers must be used for the process.
596// If path is set to nonempty and shouldUseNativeDriver is true, ANGLE will be used regardless.
597void GraphicsEnv::setAngleInfo(const std::string& path, const bool shouldUseNativeDriver,
Peiyong Lin17190c92023-06-21 07:03:47 +0000598 const std::string& packageName,
Tim Van Pattena53dcb22021-05-21 17:51:59 -0600599 const std::vector<std::string> eglFeatures) {
Peiyong Lin17190c92023-06-21 07:03:47 +0000600 if (mShouldUseAngle) {
601 // ANGLE is already set up for this application process, even if the application
602 // needs to switch from apk to system or vice versa, the application process must
603 // be killed and relaunch so that the loader can properly load ANGLE again.
604 // The architecture does not support runtime switch between drivers, so just return.
605 ALOGE("ANGLE is already set for %s", packageName.c_str());
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700606 return;
607 }
608
Peiyong Lin2f707e62020-09-26 13:52:10 -0700609 mAngleEglFeatures = std::move(eglFeatures);
Tim Van Patten5f744f12018-12-12 11:46:21 -0700610 ALOGV("setting ANGLE path to '%s'", path.c_str());
Peiyong Lin17190c92023-06-21 07:03:47 +0000611 mAnglePath = std::move(path);
Peiyong Lin2f51e752023-06-15 22:35:14 +0000612 ALOGV("setting app package name to '%s'", packageName.c_str());
Peiyong Lin17190c92023-06-21 07:03:47 +0000613 mPackageName = std::move(packageName);
Peiyong Lin2df5a002023-08-01 23:31:34 +0000614 if (mAnglePath == "system") {
615 mShouldUseSystemAngle = true;
616 }
617 if (!mAnglePath.empty()) {
618 mShouldUseAngle = true;
619 }
620 mShouldUseNativeDriver = shouldUseNativeDriver;
Cody Northrop1f00e172018-04-02 11:23:31 -0600621}
622
Peiyong Lin2f51e752023-06-15 22:35:14 +0000623std::string& GraphicsEnv::getPackageName() {
624 return mPackageName;
Cody Northrop04e70432018-09-06 10:34:58 -0600625}
626
Peiyong Lin2f707e62020-09-26 13:52:10 -0700627const std::vector<std::string>& GraphicsEnv::getAngleEglFeatures() {
628 return mAngleEglFeatures;
629}
630
Cody Northrop1f00e172018-04-02 11:23:31 -0600631android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700632 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600633
Cody Northrop3892cfa2019-01-30 10:03:12 -0700634 if (mAngleNamespace) {
635 return mAngleNamespace;
636 }
637
Peiyong Line4da7a82023-08-08 20:46:53 +0000638 // If ANGLE path is not set, it means ANGLE should not be used for this process;
639 // or if ANGLE path is set and set to use system ANGLE, then a namespace is not needed
640 // because:
641 // 1) if the default OpenGL ES driver is already ANGLE, then the loader will skip;
642 // 2) if the default OpenGL ES driver is native, then there's no symbol conflict;
643 // 3) if there's no OpenGL ES driver is preloaded, then there's no symbol conflict.
644 if (mAnglePath.empty() || mShouldUseSystemAngle) {
645 ALOGV("mAnglePath is empty or use system ANGLE, abort creating ANGLE namespace");
Cody Northrop3892cfa2019-01-30 10:03:12 -0700646 return nullptr;
647 }
648
Peiyong Lin17190c92023-06-21 07:03:47 +0000649 // Construct the search paths for system ANGLE.
650 const char* const defaultLibraryPaths =
651#if defined(__LP64__)
Peiyong Line4da7a82023-08-08 20:46:53 +0000652 "/vendor/lib64/egl:/system/lib64";
Peiyong Lin17190c92023-06-21 07:03:47 +0000653#else
Peiyong Line4da7a82023-08-08 20:46:53 +0000654 "/vendor/lib/egl:/system/lib";
Peiyong Lin17190c92023-06-21 07:03:47 +0000655#endif
656
657 // If the application process will run on top of system ANGLE, construct the namespace
658 // with sphal namespace being the parent namespace so that search paths and libraries
659 // are properly inherited.
660 mAngleNamespace =
661 android_create_namespace("ANGLE",
Peiyong Line88e0ba2023-06-28 15:09:09 +0000662 mShouldUseSystemAngle ? defaultLibraryPaths
663 : mAnglePath.c_str(), // ld_library_path
664 mShouldUseSystemAngle
665 ? defaultLibraryPaths
666 : mAnglePath.c_str(), // default_library_path
Peiyong Lin17190c92023-06-21 07:03:47 +0000667 ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED,
668 nullptr, // permitted_when_isolated_path
Peiyong Line88e0ba2023-06-28 15:09:09 +0000669 mShouldUseSystemAngle ? android_get_exported_namespace("sphal")
670 : nullptr); // parent
Cody Northrop3892cfa2019-01-30 10:03:12 -0700671
672 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600673
Peiyong Line88e0ba2023-06-28 15:09:09 +0000674 if (!mShouldUseSystemAngle) {
Peiyong Lin17190c92023-06-21 07:03:47 +0000675 return mAngleNamespace;
676 }
677
Chris Forbes0cdac9a2024-08-16 15:12:27 +1200678 auto vndkNamespace = android_get_exported_namespace(isVndkEnabled() ? "vndk" : "sphal");
Peiyong Lin17190c92023-06-21 07:03:47 +0000679 if (!vndkNamespace) {
Ethan Chenf5da7ee2024-01-18 19:47:01 -0800680 mAngleNamespace = nullptr;
681 return mAngleNamespace;
Peiyong Lin17190c92023-06-21 07:03:47 +0000682 }
683
684 if (!linkDriverNamespaceLocked(mAngleNamespace, vndkNamespace, "")) {
685 mAngleNamespace = nullptr;
686 }
687
Cody Northrop1f00e172018-04-02 11:23:31 -0600688 return mAngleNamespace;
689}
690
Yuxin Hub69e9882023-04-13 03:51:41 +0000691void GraphicsEnv::nativeToggleAngleAsSystemDriver(bool enabled) {
692 const sp<IGpuService> gpuService = getGpuService();
693 if (!gpuService) {
694 ALOGE("No GPU service");
695 return;
696 }
697 gpuService->toggleAngleAsSystemDriver(enabled);
698}
699
Peiyong Line88e0ba2023-06-28 15:09:09 +0000700bool GraphicsEnv::shouldUseSystemAngle() {
701 return mShouldUseSystemAngle;
702}
703
Peiyong Lin2df5a002023-08-01 23:31:34 +0000704bool GraphicsEnv::shouldUseNativeDriver() {
705 return mShouldUseNativeDriver;
706}
707
Peiyong Linef20e4e2023-07-03 02:33:00 +0000708/**
709 * APIs for debuggable layers
710 */
711
712void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace,
713 const std::string& layerPaths) {
714 if (mLayerPaths.empty()) {
715 mLayerPaths = layerPaths;
716 mAppNamespace = appNamespace;
717 } else {
718 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
719 layerPaths.c_str(), appNamespace);
720 }
721}
722
723NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
724 return mAppNamespace;
725}
726
727const std::string& GraphicsEnv::getLayerPaths() {
728 return mLayerPaths;
729}
730
731const std::string& GraphicsEnv::getDebugLayers() {
732 return mDebugLayers;
733}
734
735const std::string& GraphicsEnv::getDebugLayersGLES() {
736 return mDebugLayersGLES;
737}
738
739void GraphicsEnv::setDebugLayers(const std::string& layers) {
740 mDebugLayers = layers;
741}
742
743void GraphicsEnv::setDebugLayersGLES(const std::string& layers) {
744 mDebugLayersGLES = layers;
745}
746
Jesse Hall90b25ed2016-12-12 12:56:46 -0800747} // namespace android