blob: 715822b4e4922b72d0f0e14a125343800e8cfe25 [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
Jesse Hall90b25ed2016-12-12 12:56:46 -080063namespace android {
64
Yiwei Zhang64d89212018-11-27 19:58:29 -080065enum NativeLibrary {
66 LLNDK = 0,
67 VNDKSP = 1,
68};
69
Jooyung Han78396802020-02-23 03:02:43 +090070static constexpr const char* kNativeLibrariesSystemConfigPath[] =
71 {"/apex/com.android.vndk.v{}/etc/llndk.libraries.{}.txt",
72 "/apex/com.android.vndk.v{}/etc/vndksp.libraries.{}.txt"};
Yiwei Zhang64d89212018-11-27 19:58:29 -080073
74static std::string vndkVersionStr() {
75#ifdef __BIONIC__
Michael Hoisied1023f22020-03-26 22:52:58 -070076 return base::GetProperty("ro.vndk.version", "");
Yiwei Zhang64d89212018-11-27 19:58:29 -080077#endif
78 return "";
79}
80
81static void insertVndkVersionStr(std::string* fileName) {
82 LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr");
Jooyung Han78396802020-02-23 03:02:43 +090083 std::string version = vndkVersionStr();
84 size_t pos = fileName->find("{}");
85 while (pos != std::string::npos) {
86 fileName->replace(pos, 2, version);
87 pos = fileName->find("{}", pos + version.size());
Yiwei Zhang64d89212018-11-27 19:58:29 -080088 }
Yiwei Zhang64d89212018-11-27 19:58:29 -080089}
90
91static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) {
92 // Read list of public native libraries from the config file.
93 std::string fileContent;
94 if (!base::ReadFileToString(configFile, &fileContent)) {
95 return false;
96 }
97
98 std::vector<std::string> lines = base::Split(fileContent, "\n");
99
100 for (auto& line : lines) {
101 auto trimmedLine = base::Trim(line);
102 if (!trimmedLine.empty()) {
103 soNames->push_back(trimmedLine);
104 }
105 }
106
107 return true;
108}
109
110static const std::string getSystemNativeLibraries(NativeLibrary type) {
Jooyung Han78396802020-02-23 03:02:43 +0900111 std::string nativeLibrariesSystemConfig = kNativeLibrariesSystemConfigPath[type];
Yiwei Zhang64d89212018-11-27 19:58:29 -0800112 insertVndkVersionStr(&nativeLibrariesSystemConfig);
113
114 std::vector<std::string> soNames;
115 if (!readConfig(nativeLibrariesSystemConfig, &soNames)) {
116 ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str());
117 return "";
118 }
119
120 return base::Join(soNames, ':');
121}
122
Peiyong Lin433c8002023-07-03 02:33:00 +0000123static sp<IGpuService> getGpuService() {
124 static const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
125 if (!binder) {
126 ALOGE("Failed to get gpu service");
127 return nullptr;
128 }
129
130 return interface_cast<IGpuService>(binder);
131}
132
Jesse Hall90b25ed2016-12-12 12:56:46 -0800133/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
134 static GraphicsEnv env;
135 return env;
136}
137
Yiwei Zhang6a674c92019-11-08 11:55:36 -0800138bool GraphicsEnv::isDebuggable() {
Cody Northrop5d32e622022-11-01 13:54:57 -0600139 // This flag determines if the application is marked debuggable
140 bool appDebuggable = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) > 0;
141
142 // This flag is set only in `debuggable` builds of the platform
143#if defined(ANDROID_DEBUGGABLE)
144 bool platformDebuggable = true;
145#else
146 bool platformDebuggable = false;
147#endif
148
149 ALOGV("GraphicsEnv::isDebuggable returning appDebuggable=%s || platformDebuggable=%s",
150 appDebuggable ? "true" : "false", platformDebuggable ? "true" : "false");
151
152 return appDebuggable || platformDebuggable;
Cody Northrop629ce4e2018-10-15 07:22:09 -0600153}
154
Peiyong Lin433c8002023-07-03 02:33:00 +0000155/**
156 * APIs for updatable graphics drivers
157 */
158
Peiyong Lin54926182023-06-15 22:35:14 +0000159void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string& path,
160 const std::string& sphalLibraries) {
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800161 if (!mDriverPath.empty() || !mSphalLibraries.empty()) {
162 ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries "
163 "from '%s' to '%s'",
164 mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800165 return;
166 }
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800167 ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(),
168 sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800169 mDriverPath = path;
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800170 mSphalLibraries = sphalLibraries;
Jesse Hall90b25ed2016-12-12 12:56:46 -0800171}
172
Peiyong Lin433c8002023-07-03 02:33:00 +0000173// Return true if all the required libraries from vndk and sphal namespace are
174// linked to the driver namespace correctly.
175bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* destNamespace,
176 android_namespace_t* vndkNamespace,
177 const std::string& sharedSphalLibraries) {
178 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
179 if (llndkLibraries.empty()) {
180 return false;
181 }
182 if (!android_link_namespaces(destNamespace, nullptr, llndkLibraries.c_str())) {
183 ALOGE("Failed to link default namespace[%s]", dlerror());
184 return false;
185 }
186
187 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
188 if (vndkspLibraries.empty()) {
189 return false;
190 }
191 if (!android_link_namespaces(destNamespace, vndkNamespace, vndkspLibraries.c_str())) {
192 ALOGE("Failed to link vndk namespace[%s]", dlerror());
193 return false;
194 }
195
196 if (sharedSphalLibraries.empty()) {
197 return true;
198 }
199
200 // Make additional libraries in sphal to be accessible
201 auto sphalNamespace = android_get_exported_namespace("sphal");
202 if (!sphalNamespace) {
203 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
204 sharedSphalLibraries.c_str());
205 return false;
206 }
207
208 if (!android_link_namespaces(destNamespace, sphalNamespace, sharedSphalLibraries.c_str())) {
209 ALOGE("Failed to link sphal namespace[%s]", dlerror());
210 return false;
211 }
212
213 return true;
214}
215
216android_namespace_t* GraphicsEnv::getDriverNamespace() {
217 std::lock_guard<std::mutex> lock(mNamespaceMutex);
218
219 if (mDriverNamespace) {
220 return mDriverNamespace;
221 }
222
223 if (mDriverPath.empty()) {
224 // For an application process, driver path is empty means this application is not opted in
225 // to use updatable driver. Application process doesn't have the ability to set up
226 // environment variables and hence before `getenv` call will return.
227 // For a process that is not an application process, if it's run from an environment,
228 // for example shell, where environment variables can be set, then it can opt into using
229 // udpatable driver by setting UPDATABLE_GFX_DRIVER to 1. By setting to 1 the developer
230 // driver will be used currently.
231 // TODO(b/159240322) Support the production updatable driver.
232 const char* id = getenv("UPDATABLE_GFX_DRIVER");
233 if (id == nullptr || std::strcmp(id, "1") != 0) {
234 return nullptr;
235 }
236 const sp<IGpuService> gpuService = getGpuService();
237 if (!gpuService) {
238 return nullptr;
239 }
240 mDriverPath = gpuService->getUpdatableDriverPath();
241 if (mDriverPath.empty()) {
242 return nullptr;
243 }
244 mDriverPath.append(UPDATABLE_DRIVER_ABI);
245 ALOGI("Driver path is setup via UPDATABLE_GFX_DRIVER: %s", mDriverPath.c_str());
246 }
247
248 auto vndkNamespace = android_get_exported_namespace("vndk");
249 if (!vndkNamespace) {
250 return nullptr;
251 }
252
253 mDriverNamespace = android_create_namespace("updatable gfx driver",
254 mDriverPath.c_str(), // ld_library_path
255 mDriverPath.c_str(), // default_library_path
256 ANDROID_NAMESPACE_TYPE_ISOLATED,
257 nullptr, // permitted_when_isolated_path
258 nullptr);
259
260 if (!linkDriverNamespaceLocked(mDriverNamespace, vndkNamespace, mSphalLibraries)) {
261 mDriverNamespace = nullptr;
262 }
263
264 return mDriverNamespace;
265}
266
267std::string GraphicsEnv::getDriverPath() const {
268 return mDriverPath;
269}
270
271/**
272 * APIs for GpuStats
273 */
274
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700275void GraphicsEnv::hintActivityLaunch() {
276 ATRACE_CALL();
277
Yiwei Zhangd3819382020-01-07 19:53:56 -0800278 {
279 std::lock_guard<std::mutex> lock(mStatsLock);
280 if (mActivityLaunched) return;
281 mActivityLaunched = true;
282 }
283
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700284 std::thread trySendGpuStatsThread([this]() {
285 // If there's already graphics driver preloaded in the process, just send
286 // the stats info to GpuStats directly through async binder.
287 std::lock_guard<std::mutex> lock(mStatsLock);
288 if (mGpuStats.glDriverToSend) {
289 mGpuStats.glDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700290 sendGpuStatsLocked(GpuStatsInfo::Api::API_GL, true, mGpuStats.glDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700291 }
292 if (mGpuStats.vkDriverToSend) {
293 mGpuStats.vkDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700294 sendGpuStatsLocked(GpuStatsInfo::Api::API_VK, true, mGpuStats.vkDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700295 }
296 });
297 trySendGpuStatsThread.detach();
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700298}
299
Greg Kaiser210bb7e2019-02-12 12:40:05 -0800300void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800301 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700302 int64_t driverBuildTime, const std::string& appPackageName,
303 const int vulkanVersion) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800304 ATRACE_CALL();
305
Yiwei Zhangd9861812019-02-13 11:51:55 -0800306 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800307 ALOGV("setGpuStats:\n"
308 "\tdriverPackageName[%s]\n"
309 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800310 "\tdriverVersionCode[%" PRIu64 "]\n"
311 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700312 "\tappPackageName[%s]\n"
313 "\tvulkanVersion[%d]\n",
Yiwei Zhang96c01712019-02-19 16:00:25 -0800314 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700315 appPackageName.c_str(), vulkanVersion);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800316
Yiwei Zhangd9861812019-02-13 11:51:55 -0800317 mGpuStats.driverPackageName = driverPackageName;
318 mGpuStats.driverVersionName = driverVersionName;
319 mGpuStats.driverVersionCode = driverVersionCode;
Yiwei Zhang96c01712019-02-19 16:00:25 -0800320 mGpuStats.driverBuildTime = driverBuildTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800321 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang794d2952019-05-06 17:43:59 -0700322 mGpuStats.vulkanVersion = vulkanVersion;
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800323}
324
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700325void GraphicsEnv::setDriverToLoad(GpuStatsInfo::Driver driver) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800326 ATRACE_CALL();
327
328 std::lock_guard<std::mutex> lock(mStatsLock);
329 switch (driver) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700330 case GpuStatsInfo::Driver::GL:
331 case GpuStatsInfo::Driver::GL_UPDATED:
332 case GpuStatsInfo::Driver::ANGLE: {
Yiwei Zhang472cab02019-08-05 17:57:41 -0700333 if (mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::NONE ||
334 mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::GL) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800335 mGpuStats.glDriverToLoad = driver;
336 break;
337 }
338
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700339 if (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800340 mGpuStats.glDriverFallback = driver;
341 }
342 break;
343 }
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700344 case GpuStatsInfo::Driver::VULKAN:
345 case GpuStatsInfo::Driver::VULKAN_UPDATED: {
Yiwei Zhang472cab02019-08-05 17:57:41 -0700346 if (mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::NONE ||
347 mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::VULKAN) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800348 mGpuStats.vkDriverToLoad = driver;
349 break;
350 }
351
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700352 if (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800353 mGpuStats.vkDriverFallback = driver;
354 }
355 break;
356 }
357 default:
358 break;
359 }
360}
361
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700362void GraphicsEnv::setDriverLoaded(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700363 int64_t driverLoadingTime) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800364 ATRACE_CALL();
365
366 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700367 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhangd3819382020-01-07 19:53:56 -0800368 mGpuStats.glDriverToSend = true;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700369 mGpuStats.glDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800370 } else {
Yiwei Zhangd3819382020-01-07 19:53:56 -0800371 mGpuStats.vkDriverToSend = true;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700372 mGpuStats.vkDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800373 }
374
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700375 sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800376}
377
Serdar Kocdemirb2901c92022-11-17 00:39:05 +0000378// Hash function to calculate hash for null-terminated Vulkan extension names
379// We store hash values of the extensions, rather than the actual names or
380// indices to be able to support new extensions easily, avoid creating
381// a table of 'known' extensions inside Android and reduce the runtime overhead.
382static uint64_t calculateExtensionHash(const char* word) {
383 if (!word) {
384 return 0;
385 }
386 const size_t wordLen = strlen(word);
387 const uint32_t seed = 167;
388 uint64_t hash = 0;
389 for (size_t i = 0; i < wordLen; i++) {
390 hash = (hash * seed) + word[i];
391 }
392 return hash;
393}
394
395void GraphicsEnv::setVulkanInstanceExtensions(uint32_t enabledExtensionCount,
396 const char* const* ppEnabledExtensionNames) {
397 ATRACE_CALL();
398 if (enabledExtensionCount == 0 || ppEnabledExtensionNames == nullptr) {
399 return;
400 }
401
402 const uint32_t maxNumStats = android::GpuStatsAppInfo::MAX_NUM_EXTENSIONS;
403 uint64_t extensionHashes[maxNumStats];
404 const uint32_t numStats = std::min(enabledExtensionCount, maxNumStats);
405 for(uint32_t i = 0; i < numStats; i++) {
406 extensionHashes[i] = calculateExtensionHash(ppEnabledExtensionNames[i]);
407 }
408 setTargetStatsArray(android::GpuStatsInfo::Stats::VULKAN_INSTANCE_EXTENSION,
409 extensionHashes, numStats);
410}
411
412void GraphicsEnv::setVulkanDeviceExtensions(uint32_t enabledExtensionCount,
413 const char* const* ppEnabledExtensionNames) {
414 ATRACE_CALL();
415 if (enabledExtensionCount == 0 || ppEnabledExtensionNames == nullptr) {
416 return;
417 }
418
419 const uint32_t maxNumStats = android::GpuStatsAppInfo::MAX_NUM_EXTENSIONS;
420 uint64_t extensionHashes[maxNumStats];
421 const uint32_t numStats = std::min(enabledExtensionCount, maxNumStats);
422 for(uint32_t i = 0; i < numStats; i++) {
423 extensionHashes[i] = calculateExtensionHash(ppEnabledExtensionNames[i]);
424 }
425 setTargetStatsArray(android::GpuStatsInfo::Stats::VULKAN_DEVICE_EXTENSION,
426 extensionHashes, numStats);
427}
428
Yiwei Zhangd3819382020-01-07 19:53:56 -0800429bool GraphicsEnv::readyToSendGpuStatsLocked() {
430 // Only send stats for processes having at least one activity launched and that process doesn't
431 // skip the GraphicsEnvironment setup.
432 return mActivityLaunched && !mGpuStats.appPackageName.empty();
433}
434
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700435void GraphicsEnv::setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value) {
Serdar Kocdemirb2901c92022-11-17 00:39:05 +0000436 return setTargetStatsArray(stats, &value, 1);
437}
438
439void GraphicsEnv::setTargetStatsArray(const GpuStatsInfo::Stats stats, const uint64_t* values,
440 const uint32_t valueCount) {
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700441 ATRACE_CALL();
442
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700443 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangd3819382020-01-07 19:53:56 -0800444 if (!readyToSendGpuStatsLocked()) return;
445
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700446 const sp<IGpuService> gpuService = getGpuService();
447 if (gpuService) {
Serdar Kocdemirb2901c92022-11-17 00:39:05 +0000448 gpuService->setTargetStatsArray(mGpuStats.appPackageName, mGpuStats.driverVersionCode,
449 stats, values, valueCount);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700450 }
451}
452
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700453void GraphicsEnv::sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800454 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800455 ATRACE_CALL();
456
Yiwei Zhangd3819382020-01-07 19:53:56 -0800457 if (!readyToSendGpuStatsLocked()) return;
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800458
459 ALOGV("sendGpuStats:\n"
460 "\tdriverPackageName[%s]\n"
461 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800462 "\tdriverVersionCode[%" PRIu64 "]\n"
463 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800464 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700465 "\tvulkanVersion[%d]\n"
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700466 "\tapi[%d]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800467 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800468 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800469 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-19 16:00:25 -0800470 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700471 mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime);
472
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700473 GpuStatsInfo::Driver driver = GpuStatsInfo::Driver::NONE;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700474 bool isIntendedDriverLoaded = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700475 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700476 driver = mGpuStats.glDriverToLoad;
477 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700478 isDriverLoaded && (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700479 } else {
480 driver = mGpuStats.vkDriverToLoad;
481 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700482 isDriverLoaded && (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700483 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800484
Yiwei Zhangd9861812019-02-13 11:51:55 -0800485 const sp<IGpuService> gpuService = getGpuService();
486 if (gpuService) {
487 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800488 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700489 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700490 isIntendedDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800491 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800492}
493
Adam Bodnar0afcca02019-09-17 13:23:17 -0700494bool GraphicsEnv::setInjectLayersPrSetDumpable() {
495 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
496 return false;
497 }
498 return true;
499}
500
Peiyong Lin433c8002023-07-03 02:33:00 +0000501/**
502 * APIs for ANGLE
503 */
504
Tim Van Patten5f744f12018-12-12 11:46:21 -0700505bool GraphicsEnv::shouldUseAngle() {
506 // Make sure we are init'ed
Peiyong Lin54926182023-06-15 22:35:14 +0000507 if (mPackageName.empty()) {
508 ALOGV("Package name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700509 return false;
510 }
511
Peiyong Lin9b8ec2f2023-06-21 07:03:47 +0000512 return mShouldUseAngle;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700513}
514
Peiyong Lina069f032023-07-31 15:35:42 +0000515void GraphicsEnv::setAngleInfo(const std::string& path, const bool shouldUseSystemAngle,
Peiyong Lin9b8ec2f2023-06-21 07:03:47 +0000516 const std::string& packageName,
Tim Van Patten9b5fe582021-05-21 17:51:59 -0600517 const std::vector<std::string> eglFeatures) {
Peiyong Lin9b8ec2f2023-06-21 07:03:47 +0000518 if (mShouldUseAngle) {
519 // ANGLE is already set up for this application process, even if the application
520 // needs to switch from apk to system or vice versa, the application process must
521 // be killed and relaunch so that the loader can properly load ANGLE again.
522 // The architecture does not support runtime switch between drivers, so just return.
523 ALOGE("ANGLE is already set for %s", packageName.c_str());
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700524 return;
525 }
526
Peiyong Lin2f707e62020-09-26 13:52:10 -0700527 mAngleEglFeatures = std::move(eglFeatures);
Tim Van Patten5f744f12018-12-12 11:46:21 -0700528 ALOGV("setting ANGLE path to '%s'", path.c_str());
Peiyong Lin9b8ec2f2023-06-21 07:03:47 +0000529 mAnglePath = std::move(path);
Peiyong Lin54926182023-06-15 22:35:14 +0000530 ALOGV("setting app package name to '%s'", packageName.c_str());
Peiyong Lin9b8ec2f2023-06-21 07:03:47 +0000531 mPackageName = std::move(packageName);
Peiyong Lina069f032023-07-31 15:35:42 +0000532 mShouldUseAngle = true;
533 mShouldUseSystemAngle = shouldUseSystemAngle;
Cody Northrop1f00e172018-04-02 11:23:31 -0600534}
535
Peiyong Lin54926182023-06-15 22:35:14 +0000536std::string& GraphicsEnv::getPackageName() {
537 return mPackageName;
Cody Northrop04e70432018-09-06 10:34:58 -0600538}
539
Peiyong Lin2f707e62020-09-26 13:52:10 -0700540const std::vector<std::string>& GraphicsEnv::getAngleEglFeatures() {
541 return mAngleEglFeatures;
542}
543
Cody Northrop1f00e172018-04-02 11:23:31 -0600544android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700545 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600546
Cody Northrop3892cfa2019-01-30 10:03:12 -0700547 if (mAngleNamespace) {
548 return mAngleNamespace;
549 }
550
Peiyong Lin1224faa2023-06-28 15:09:09 +0000551 if (mAnglePath.empty() && !mShouldUseSystemAngle) {
Peiyong Lin9b8ec2f2023-06-21 07:03:47 +0000552 ALOGV("mAnglePath is empty and not using system ANGLE, abort creating ANGLE namespace");
Cody Northrop3892cfa2019-01-30 10:03:12 -0700553 return nullptr;
554 }
555
Peiyong Lin9b8ec2f2023-06-21 07:03:47 +0000556 // Construct the search paths for system ANGLE.
557 const char* const defaultLibraryPaths =
558#if defined(__LP64__)
559 "/vendor/lib64/egl:/system/lib64/egl";
560#else
561 "/vendor/lib/egl:/system/lib/egl";
562#endif
563
564 // If the application process will run on top of system ANGLE, construct the namespace
565 // with sphal namespace being the parent namespace so that search paths and libraries
566 // are properly inherited.
567 mAngleNamespace =
568 android_create_namespace("ANGLE",
Peiyong Lin1224faa2023-06-28 15:09:09 +0000569 mShouldUseSystemAngle ? defaultLibraryPaths
570 : mAnglePath.c_str(), // ld_library_path
571 mShouldUseSystemAngle
572 ? defaultLibraryPaths
573 : mAnglePath.c_str(), // default_library_path
Peiyong Lin9b8ec2f2023-06-21 07:03:47 +0000574 ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED,
575 nullptr, // permitted_when_isolated_path
Peiyong Lin1224faa2023-06-28 15:09:09 +0000576 mShouldUseSystemAngle ? android_get_exported_namespace("sphal")
577 : nullptr); // parent
Cody Northrop3892cfa2019-01-30 10:03:12 -0700578
579 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600580
Peiyong Lin1224faa2023-06-28 15:09:09 +0000581 if (!mShouldUseSystemAngle) {
Peiyong Lin9b8ec2f2023-06-21 07:03:47 +0000582 return mAngleNamespace;
583 }
584
585 auto vndkNamespace = android_get_exported_namespace("vndk");
586 if (!vndkNamespace) {
587 return nullptr;
588 }
589
590 if (!linkDriverNamespaceLocked(mAngleNamespace, vndkNamespace, "")) {
591 mAngleNamespace = nullptr;
592 }
593
Cody Northrop1f00e172018-04-02 11:23:31 -0600594 return mAngleNamespace;
595}
596
Yuxin Hub69e9882023-04-13 03:51:41 +0000597void GraphicsEnv::nativeToggleAngleAsSystemDriver(bool enabled) {
598 const sp<IGpuService> gpuService = getGpuService();
599 if (!gpuService) {
600 ALOGE("No GPU service");
601 return;
602 }
603 gpuService->toggleAngleAsSystemDriver(enabled);
604}
605
Peiyong Lin1224faa2023-06-28 15:09:09 +0000606bool GraphicsEnv::shouldUseSystemAngle() {
607 return mShouldUseSystemAngle;
608}
609
Peiyong Lin433c8002023-07-03 02:33:00 +0000610/**
611 * APIs for debuggable layers
612 */
613
614void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace,
615 const std::string& layerPaths) {
616 if (mLayerPaths.empty()) {
617 mLayerPaths = layerPaths;
618 mAppNamespace = appNamespace;
619 } else {
620 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
621 layerPaths.c_str(), appNamespace);
622 }
623}
624
625NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
626 return mAppNamespace;
627}
628
629const std::string& GraphicsEnv::getLayerPaths() {
630 return mLayerPaths;
631}
632
633const std::string& GraphicsEnv::getDebugLayers() {
634 return mDebugLayers;
635}
636
637const std::string& GraphicsEnv::getDebugLayersGLES() {
638 return mDebugLayersGLES;
639}
640
641void GraphicsEnv::setDebugLayers(const std::string& layers) {
642 mDebugLayers = layers;
643}
644
645void GraphicsEnv::setDebugLayersGLES(const std::string& layers) {
646 mDebugLayersGLES = layers;
647}
648
Jesse Hall90b25ed2016-12-12 12:56:46 -0800649} // namespace android