blob: 9d3e674d6cc1222fc8feb3d16ad8a672a50eaed1 [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__
66 // TODO(b/290159430) Use ro.vndk.version to check if VNDK is enabled instead
67 static bool isVndkEnabled = !android::base::GetBoolProperty("ro.vndk.deprecate", false);
68 return isVndkEnabled;
69#endif
70 return false;
71}
72} // namespace
73
Jesse Hall90b25ed2016-12-12 12:56:46 -080074namespace android {
75
Yiwei Zhang64d89212018-11-27 19:58:29 -080076enum NativeLibrary {
77 LLNDK = 0,
78 VNDKSP = 1,
79};
80
Jooyung Han78396802020-02-23 03:02:43 +090081static constexpr const char* kNativeLibrariesSystemConfigPath[] =
82 {"/apex/com.android.vndk.v{}/etc/llndk.libraries.{}.txt",
83 "/apex/com.android.vndk.v{}/etc/vndksp.libraries.{}.txt"};
Yiwei Zhang64d89212018-11-27 19:58:29 -080084
Kiyoung Kimc0e3ed32023-08-11 11:22:15 +090085static const char* kLlndkLibrariesTxtPath = "/system/etc/llndk.libraries.txt";
86
Yiwei Zhang64d89212018-11-27 19:58:29 -080087static std::string vndkVersionStr() {
88#ifdef __BIONIC__
Michael Hoisied1023f22020-03-26 22:52:58 -070089 return base::GetProperty("ro.vndk.version", "");
Yiwei Zhang64d89212018-11-27 19:58:29 -080090#endif
91 return "";
92}
93
94static void insertVndkVersionStr(std::string* fileName) {
95 LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr");
Jooyung Han78396802020-02-23 03:02:43 +090096 std::string version = vndkVersionStr();
97 size_t pos = fileName->find("{}");
98 while (pos != std::string::npos) {
99 fileName->replace(pos, 2, version);
100 pos = fileName->find("{}", pos + version.size());
Yiwei Zhang64d89212018-11-27 19:58:29 -0800101 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800102}
103
104static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) {
105 // Read list of public native libraries from the config file.
106 std::string fileContent;
107 if (!base::ReadFileToString(configFile, &fileContent)) {
108 return false;
109 }
110
111 std::vector<std::string> lines = base::Split(fileContent, "\n");
112
113 for (auto& line : lines) {
114 auto trimmedLine = base::Trim(line);
115 if (!trimmedLine.empty()) {
116 soNames->push_back(trimmedLine);
117 }
118 }
119
120 return true;
121}
122
123static const std::string getSystemNativeLibraries(NativeLibrary type) {
Kiyoung Kimc0e3ed32023-08-11 11:22:15 +0900124 std::string nativeLibrariesSystemConfig = "";
125
126 if (!isVndkEnabled() && type == NativeLibrary::LLNDK) {
127 nativeLibrariesSystemConfig = kLlndkLibrariesTxtPath;
128 } else {
129 nativeLibrariesSystemConfig = kNativeLibrariesSystemConfigPath[type];
130 insertVndkVersionStr(&nativeLibrariesSystemConfig);
131 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800132
133 std::vector<std::string> soNames;
134 if (!readConfig(nativeLibrariesSystemConfig, &soNames)) {
135 ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str());
136 return "";
137 }
138
139 return base::Join(soNames, ':');
140}
141
Jesse Hall90b25ed2016-12-12 12:56:46 -0800142/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
143 static GraphicsEnv env;
144 return env;
145}
146
Yiwei Zhang6a674c92019-11-08 11:55:36 -0800147bool GraphicsEnv::isDebuggable() {
Cody Northrop383832f2022-11-01 13:54:57 -0600148 // This flag determines if the application is marked debuggable
149 bool appDebuggable = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) > 0;
150
151 // This flag is set only in `debuggable` builds of the platform
152#if defined(ANDROID_DEBUGGABLE)
153 bool platformDebuggable = true;
154#else
155 bool platformDebuggable = false;
156#endif
157
158 ALOGV("GraphicsEnv::isDebuggable returning appDebuggable=%s || platformDebuggable=%s",
159 appDebuggable ? "true" : "false", platformDebuggable ? "true" : "false");
160
161 return appDebuggable || platformDebuggable;
Cody Northrop629ce4e2018-10-15 07:22:09 -0600162}
163
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800164void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string path,
165 const std::string sphalLibraries) {
166 if (!mDriverPath.empty() || !mSphalLibraries.empty()) {
167 ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries "
168 "from '%s' to '%s'",
169 mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800170 return;
171 }
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800172 ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(),
173 sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800174 mDriverPath = path;
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800175 mSphalLibraries = sphalLibraries;
Jesse Hall90b25ed2016-12-12 12:56:46 -0800176}
177
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700178void GraphicsEnv::hintActivityLaunch() {
179 ATRACE_CALL();
180
Yiwei Zhangd3819382020-01-07 19:53:56 -0800181 {
182 std::lock_guard<std::mutex> lock(mStatsLock);
183 if (mActivityLaunched) return;
184 mActivityLaunched = true;
185 }
186
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700187 std::thread trySendGpuStatsThread([this]() {
188 // If there's already graphics driver preloaded in the process, just send
189 // the stats info to GpuStats directly through async binder.
190 std::lock_guard<std::mutex> lock(mStatsLock);
191 if (mGpuStats.glDriverToSend) {
192 mGpuStats.glDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700193 sendGpuStatsLocked(GpuStatsInfo::Api::API_GL, true, mGpuStats.glDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700194 }
195 if (mGpuStats.vkDriverToSend) {
196 mGpuStats.vkDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700197 sendGpuStatsLocked(GpuStatsInfo::Api::API_VK, true, mGpuStats.vkDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700198 }
199 });
200 trySendGpuStatsThread.detach();
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700201}
202
Greg Kaiser210bb7e2019-02-12 12:40:05 -0800203void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800204 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700205 int64_t driverBuildTime, const std::string& appPackageName,
206 const int vulkanVersion) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800207 ATRACE_CALL();
208
Yiwei Zhangd9861812019-02-13 11:51:55 -0800209 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800210 ALOGV("setGpuStats:\n"
211 "\tdriverPackageName[%s]\n"
212 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800213 "\tdriverVersionCode[%" PRIu64 "]\n"
214 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700215 "\tappPackageName[%s]\n"
216 "\tvulkanVersion[%d]\n",
Yiwei Zhang96c01712019-02-19 16:00:25 -0800217 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700218 appPackageName.c_str(), vulkanVersion);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800219
Yiwei Zhangd9861812019-02-13 11:51:55 -0800220 mGpuStats.driverPackageName = driverPackageName;
221 mGpuStats.driverVersionName = driverVersionName;
222 mGpuStats.driverVersionCode = driverVersionCode;
Yiwei Zhang96c01712019-02-19 16:00:25 -0800223 mGpuStats.driverBuildTime = driverBuildTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800224 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang794d2952019-05-06 17:43:59 -0700225 mGpuStats.vulkanVersion = vulkanVersion;
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800226}
227
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700228void GraphicsEnv::setDriverToLoad(GpuStatsInfo::Driver driver) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800229 ATRACE_CALL();
230
231 std::lock_guard<std::mutex> lock(mStatsLock);
232 switch (driver) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700233 case GpuStatsInfo::Driver::GL:
234 case GpuStatsInfo::Driver::GL_UPDATED:
235 case GpuStatsInfo::Driver::ANGLE: {
Yiwei Zhang472cab02019-08-05 17:57:41 -0700236 if (mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::NONE ||
237 mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::GL) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800238 mGpuStats.glDriverToLoad = driver;
239 break;
240 }
241
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700242 if (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800243 mGpuStats.glDriverFallback = driver;
244 }
245 break;
246 }
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700247 case GpuStatsInfo::Driver::VULKAN:
248 case GpuStatsInfo::Driver::VULKAN_UPDATED: {
Yiwei Zhang472cab02019-08-05 17:57:41 -0700249 if (mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::NONE ||
250 mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::VULKAN) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800251 mGpuStats.vkDriverToLoad = driver;
252 break;
253 }
254
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700255 if (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800256 mGpuStats.vkDriverFallback = driver;
257 }
258 break;
259 }
260 default:
261 break;
262 }
263}
264
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700265void GraphicsEnv::setDriverLoaded(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700266 int64_t driverLoadingTime) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800267 ATRACE_CALL();
268
269 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700270 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhangd3819382020-01-07 19:53:56 -0800271 mGpuStats.glDriverToSend = true;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700272 mGpuStats.glDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800273 } else {
Yiwei Zhangd3819382020-01-07 19:53:56 -0800274 mGpuStats.vkDriverToSend = true;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700275 mGpuStats.vkDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800276 }
277
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700278 sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800279}
280
Serdar Kocdemirb2901c92022-11-17 00:39:05 +0000281// Hash function to calculate hash for null-terminated Vulkan extension names
282// We store hash values of the extensions, rather than the actual names or
283// indices to be able to support new extensions easily, avoid creating
284// a table of 'known' extensions inside Android and reduce the runtime overhead.
285static uint64_t calculateExtensionHash(const char* word) {
286 if (!word) {
287 return 0;
288 }
289 const size_t wordLen = strlen(word);
290 const uint32_t seed = 167;
291 uint64_t hash = 0;
292 for (size_t i = 0; i < wordLen; i++) {
293 hash = (hash * seed) + word[i];
294 }
295 return hash;
296}
297
298void GraphicsEnv::setVulkanInstanceExtensions(uint32_t enabledExtensionCount,
299 const char* const* ppEnabledExtensionNames) {
300 ATRACE_CALL();
301 if (enabledExtensionCount == 0 || ppEnabledExtensionNames == nullptr) {
302 return;
303 }
304
305 const uint32_t maxNumStats = android::GpuStatsAppInfo::MAX_NUM_EXTENSIONS;
306 uint64_t extensionHashes[maxNumStats];
307 const uint32_t numStats = std::min(enabledExtensionCount, maxNumStats);
308 for(uint32_t i = 0; i < numStats; i++) {
309 extensionHashes[i] = calculateExtensionHash(ppEnabledExtensionNames[i]);
310 }
311 setTargetStatsArray(android::GpuStatsInfo::Stats::VULKAN_INSTANCE_EXTENSION,
312 extensionHashes, numStats);
313}
314
315void GraphicsEnv::setVulkanDeviceExtensions(uint32_t enabledExtensionCount,
316 const char* const* ppEnabledExtensionNames) {
317 ATRACE_CALL();
318 if (enabledExtensionCount == 0 || ppEnabledExtensionNames == nullptr) {
319 return;
320 }
321
322 const uint32_t maxNumStats = android::GpuStatsAppInfo::MAX_NUM_EXTENSIONS;
323 uint64_t extensionHashes[maxNumStats];
324 const uint32_t numStats = std::min(enabledExtensionCount, maxNumStats);
325 for(uint32_t i = 0; i < numStats; i++) {
326 extensionHashes[i] = calculateExtensionHash(ppEnabledExtensionNames[i]);
327 }
328 setTargetStatsArray(android::GpuStatsInfo::Stats::VULKAN_DEVICE_EXTENSION,
329 extensionHashes, numStats);
330}
331
Yiwei Zhangd9861812019-02-13 11:51:55 -0800332static sp<IGpuService> getGpuService() {
Yiwei Zhang8e097302019-07-08 16:11:12 -0700333 static const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
Yiwei Zhangd9861812019-02-13 11:51:55 -0800334 if (!binder) {
335 ALOGE("Failed to get gpu service");
336 return nullptr;
337 }
338
339 return interface_cast<IGpuService>(binder);
340}
341
Yiwei Zhangd3819382020-01-07 19:53:56 -0800342bool GraphicsEnv::readyToSendGpuStatsLocked() {
343 // Only send stats for processes having at least one activity launched and that process doesn't
344 // skip the GraphicsEnvironment setup.
345 return mActivityLaunched && !mGpuStats.appPackageName.empty();
346}
347
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700348void GraphicsEnv::setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value) {
Serdar Kocdemirb2901c92022-11-17 00:39:05 +0000349 return setTargetStatsArray(stats, &value, 1);
350}
351
352void GraphicsEnv::setTargetStatsArray(const GpuStatsInfo::Stats stats, const uint64_t* values,
353 const uint32_t valueCount) {
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700354 ATRACE_CALL();
355
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700356 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangd3819382020-01-07 19:53:56 -0800357 if (!readyToSendGpuStatsLocked()) return;
358
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700359 const sp<IGpuService> gpuService = getGpuService();
360 if (gpuService) {
Serdar Kocdemirb2901c92022-11-17 00:39:05 +0000361 gpuService->setTargetStatsArray(mGpuStats.appPackageName, mGpuStats.driverVersionCode,
362 stats, values, valueCount);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700363 }
364}
365
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700366void GraphicsEnv::sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800367 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800368 ATRACE_CALL();
369
Yiwei Zhangd3819382020-01-07 19:53:56 -0800370 if (!readyToSendGpuStatsLocked()) return;
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800371
372 ALOGV("sendGpuStats:\n"
373 "\tdriverPackageName[%s]\n"
374 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800375 "\tdriverVersionCode[%" PRIu64 "]\n"
376 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800377 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700378 "\tvulkanVersion[%d]\n"
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700379 "\tapi[%d]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800380 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800381 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800382 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-19 16:00:25 -0800383 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700384 mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime);
385
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700386 GpuStatsInfo::Driver driver = GpuStatsInfo::Driver::NONE;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700387 bool isIntendedDriverLoaded = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700388 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700389 driver = mGpuStats.glDriverToLoad;
390 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700391 isDriverLoaded && (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700392 } else {
393 driver = mGpuStats.vkDriverToLoad;
394 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700395 isDriverLoaded && (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700396 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800397
Yiwei Zhangd9861812019-02-13 11:51:55 -0800398 const sp<IGpuService> gpuService = getGpuService();
399 if (gpuService) {
400 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800401 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700402 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700403 isIntendedDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800404 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800405}
406
Adam Bodnar0afcca02019-09-17 13:23:17 -0700407bool GraphicsEnv::setInjectLayersPrSetDumpable() {
408 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
409 return false;
410 }
411 return true;
412}
413
Tim Van Patten5f744f12018-12-12 11:46:21 -0700414void* GraphicsEnv::loadLibrary(std::string name) {
415 const android_dlextinfo dlextinfo = {
416 .flags = ANDROID_DLEXT_USE_NAMESPACE,
417 .library_namespace = getAngleNamespace(),
418 };
419
420 std::string libName = std::string("lib") + name + "_angle.so";
421
422 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
423
424 if (so) {
425 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
426 return so;
427 } else {
428 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
429 }
430
431 return nullptr;
432}
433
Tim Van Patten5f744f12018-12-12 11:46:21 -0700434bool GraphicsEnv::shouldUseAngle(std::string appName) {
435 if (appName != mAngleAppName) {
436 // Make sure we are checking the app we were init'ed for
437 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
438 appName.c_str());
439 return false;
440 }
441
442 return shouldUseAngle();
443}
444
445bool GraphicsEnv::shouldUseAngle() {
446 // Make sure we are init'ed
447 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700448 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700449 return false;
450 }
451
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700452 return (mUseAngle == YES) ? true : false;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700453}
454
455void GraphicsEnv::updateUseAngle() {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700456 const char* ANGLE_PREFER_ANGLE = "angle";
457 const char* ANGLE_PREFER_NATIVE = "native";
458
Tim Van Pattena53dcb22021-05-21 17:51:59 -0600459 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700460 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
Peiyong Lin9679a982023-04-10 22:00:30 +0000461 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700462 mUseAngle = YES;
Peiyong Lin9679a982023-04-10 22:00:30 +0000463 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
464 ALOGV("User set \"Developer Options\" to force the use of Native");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700465 } else {
Tim Van Pattena53dcb22021-05-21 17:51:59 -0600466 ALOGV("User set invalid \"Developer Options\": '%s'", mAngleDeveloperOptIn.c_str());
Tim Van Patten5f744f12018-12-12 11:46:21 -0700467 }
468}
469
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600470void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Peiyong Lin9679a982023-04-10 22:00:30 +0000471 const std::string developerOptIn,
Tim Van Pattena53dcb22021-05-21 17:51:59 -0600472 const std::vector<std::string> eglFeatures) {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700473 if (mUseAngle != UNKNOWN) {
474 // We've already figured out an answer for this app, so just return.
475 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
476 (mUseAngle == YES) ? "true" : "false");
477 return;
478 }
479
Peiyong Lin2f707e62020-09-26 13:52:10 -0700480 mAngleEglFeatures = std::move(eglFeatures);
481
Tim Van Patten5f744f12018-12-12 11:46:21 -0700482 ALOGV("setting ANGLE path to '%s'", path.c_str());
483 mAnglePath = path;
484 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
485 mAngleAppName = appName;
486 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
487 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600488
Tim Van Patten5f744f12018-12-12 11:46:21 -0700489 // Update the current status of whether we should use ANGLE or not
490 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600491}
492
Victor Khimenko4819b522018-07-13 17:24:18 +0200493void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600494 if (mLayerPaths.empty()) {
495 mLayerPaths = layerPaths;
496 mAppNamespace = appNamespace;
497 } else {
498 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800499 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600500 }
501}
502
Victor Khimenko4819b522018-07-13 17:24:18 +0200503NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600504 return mAppNamespace;
505}
506
Tim Van Patten5f744f12018-12-12 11:46:21 -0700507std::string& GraphicsEnv::getAngleAppName() {
508 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600509}
510
Peiyong Lin2f707e62020-09-26 13:52:10 -0700511const std::vector<std::string>& GraphicsEnv::getAngleEglFeatures() {
512 return mAngleEglFeatures;
513}
514
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600515const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600516 return mLayerPaths;
517}
518
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600519const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600520 return mDebugLayers;
521}
522
Cody Northropb9b01b62018-10-23 13:13:10 -0600523const std::string& GraphicsEnv::getDebugLayersGLES() {
524 return mDebugLayersGLES;
525}
526
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600527void GraphicsEnv::setDebugLayers(const std::string layers) {
528 mDebugLayers = layers;
529}
530
Cody Northropb9b01b62018-10-23 13:13:10 -0600531void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
532 mDebugLayersGLES = layers;
533}
534
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700535// Return true if all the required libraries from vndk and sphal namespace are
Yiwei Zhang9058b8f2020-11-12 20:23:00 +0000536// linked to the updatable gfx driver namespace correctly.
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700537bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* vndkNamespace) {
538 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
539 if (llndkLibraries.empty()) {
540 return false;
541 }
542 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
543 ALOGE("Failed to link default namespace[%s]", dlerror());
544 return false;
545 }
546
547 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
548 if (vndkspLibraries.empty()) {
549 return false;
550 }
551 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
552 ALOGE("Failed to link vndk namespace[%s]", dlerror());
553 return false;
554 }
555
556 if (mSphalLibraries.empty()) {
557 return true;
558 }
559
560 // Make additional libraries in sphal to be accessible
561 auto sphalNamespace = android_get_exported_namespace("sphal");
562 if (!sphalNamespace) {
563 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
564 mSphalLibraries.c_str());
565 return false;
566 }
567
568 if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) {
569 ALOGE("Failed to link sphal namespace[%s]", dlerror());
570 return false;
571 }
572
573 return true;
574}
575
Jesse Hall53457db2016-12-14 16:54:06 -0800576android_namespace_t* GraphicsEnv::getDriverNamespace() {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700577 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Yiwei Zhang64d89212018-11-27 19:58:29 -0800578
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700579 if (mDriverNamespace) {
580 return mDriverNamespace;
581 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800582
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700583 if (mDriverPath.empty()) {
Peiyong Lin0acbfdc2020-06-17 18:47:12 -0700584 // For an application process, driver path is empty means this application is not opted in
585 // to use updatable driver. Application process doesn't have the ability to set up
586 // environment variables and hence before `getenv` call will return.
587 // For a process that is not an application process, if it's run from an environment,
588 // for example shell, where environment variables can be set, then it can opt into using
589 // udpatable driver by setting UPDATABLE_GFX_DRIVER to 1. By setting to 1 the developer
590 // driver will be used currently.
591 // TODO(b/159240322) Support the production updatable driver.
592 const char* id = getenv("UPDATABLE_GFX_DRIVER");
593 if (id == nullptr || std::strcmp(id, "1")) {
594 return nullptr;
595 }
596 const sp<IGpuService> gpuService = getGpuService();
597 if (!gpuService) {
598 return nullptr;
599 }
600 mDriverPath = gpuService->getUpdatableDriverPath();
601 if (mDriverPath.empty()) {
602 return nullptr;
603 }
604 mDriverPath.append(UPDATABLE_DRIVER_ABI);
605 ALOGI("Driver path is setup via UPDATABLE_GFX_DRIVER: %s", mDriverPath.c_str());
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700606 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800607
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700608 auto vndkNamespace = android_get_exported_namespace("vndk");
609 if (!vndkNamespace) {
610 return nullptr;
611 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800612
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700613 mDriverNamespace = android_create_namespace("gfx driver",
614 mDriverPath.c_str(), // ld_library_path
615 mDriverPath.c_str(), // default_library_path
616 ANDROID_NAMESPACE_TYPE_ISOLATED,
617 nullptr, // permitted_when_isolated_path
618 nullptr);
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800619
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700620 if (!linkDriverNamespaceLocked(vndkNamespace)) {
621 mDriverNamespace = nullptr;
622 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800623
Jesse Hall53457db2016-12-14 16:54:06 -0800624 return mDriverNamespace;
625}
626
Peiyong Lindb3ed6e2020-01-09 18:43:27 -0800627std::string GraphicsEnv::getDriverPath() const {
628 return mDriverPath;
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
638 if (mAnglePath.empty()) {
639 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
640 return nullptr;
641 }
642
643 mAngleNamespace = android_create_namespace("ANGLE",
644 nullptr, // ld_library_path
645 mAnglePath.c_str(), // default_library_path
Yiwei Zhang5b7604f2020-05-08 14:23:34 -0700646 ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED,
Cody Northrop3892cfa2019-01-30 10:03:12 -0700647 nullptr, // permitted_when_isolated_path
648 nullptr);
649
650 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600651
652 return mAngleNamespace;
653}
654
Yuxin Hub69e9882023-04-13 03:51:41 +0000655void GraphicsEnv::nativeToggleAngleAsSystemDriver(bool enabled) {
656 const sp<IGpuService> gpuService = getGpuService();
657 if (!gpuService) {
658 ALOGE("No GPU service");
659 return;
660 }
661 gpuService->toggleAngleAsSystemDriver(enabled);
662}
663
Jesse Hall90b25ed2016-12-12 12:56:46 -0800664} // namespace android