blob: 5f5f85a2ad6ed152e794472732a6c40dd64be5a5 [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
Jesse Hall90b25ed2016-12-12 12:56:46 -0800123/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
124 static GraphicsEnv env;
125 return env;
126}
127
Yiwei Zhang6a674c92019-11-08 11:55:36 -0800128bool GraphicsEnv::isDebuggable() {
Cody Northrop383832f2022-11-01 13:54:57 -0600129 // This flag determines if the application is marked debuggable
130 bool appDebuggable = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) > 0;
131
132 // This flag is set only in `debuggable` builds of the platform
133#if defined(ANDROID_DEBUGGABLE)
134 bool platformDebuggable = true;
135#else
136 bool platformDebuggable = false;
137#endif
138
139 ALOGV("GraphicsEnv::isDebuggable returning appDebuggable=%s || platformDebuggable=%s",
140 appDebuggable ? "true" : "false", platformDebuggable ? "true" : "false");
141
142 return appDebuggable || platformDebuggable;
Cody Northrop629ce4e2018-10-15 07:22:09 -0600143}
144
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800145void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string path,
146 const std::string sphalLibraries) {
147 if (!mDriverPath.empty() || !mSphalLibraries.empty()) {
148 ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries "
149 "from '%s' to '%s'",
150 mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800151 return;
152 }
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800153 ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(),
154 sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800155 mDriverPath = path;
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800156 mSphalLibraries = sphalLibraries;
Jesse Hall90b25ed2016-12-12 12:56:46 -0800157}
158
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700159void GraphicsEnv::hintActivityLaunch() {
160 ATRACE_CALL();
161
Yiwei Zhangd3819382020-01-07 19:53:56 -0800162 {
163 std::lock_guard<std::mutex> lock(mStatsLock);
164 if (mActivityLaunched) return;
165 mActivityLaunched = true;
166 }
167
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700168 std::thread trySendGpuStatsThread([this]() {
169 // If there's already graphics driver preloaded in the process, just send
170 // the stats info to GpuStats directly through async binder.
171 std::lock_guard<std::mutex> lock(mStatsLock);
172 if (mGpuStats.glDriverToSend) {
173 mGpuStats.glDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700174 sendGpuStatsLocked(GpuStatsInfo::Api::API_GL, true, mGpuStats.glDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700175 }
176 if (mGpuStats.vkDriverToSend) {
177 mGpuStats.vkDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700178 sendGpuStatsLocked(GpuStatsInfo::Api::API_VK, true, mGpuStats.vkDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700179 }
180 });
181 trySendGpuStatsThread.detach();
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700182}
183
Greg Kaiser210bb7e2019-02-12 12:40:05 -0800184void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800185 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700186 int64_t driverBuildTime, const std::string& appPackageName,
187 const int vulkanVersion) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800188 ATRACE_CALL();
189
Yiwei Zhangd9861812019-02-13 11:51:55 -0800190 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800191 ALOGV("setGpuStats:\n"
192 "\tdriverPackageName[%s]\n"
193 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800194 "\tdriverVersionCode[%" PRIu64 "]\n"
195 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700196 "\tappPackageName[%s]\n"
197 "\tvulkanVersion[%d]\n",
Yiwei Zhang96c01712019-02-19 16:00:25 -0800198 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700199 appPackageName.c_str(), vulkanVersion);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800200
Yiwei Zhangd9861812019-02-13 11:51:55 -0800201 mGpuStats.driverPackageName = driverPackageName;
202 mGpuStats.driverVersionName = driverVersionName;
203 mGpuStats.driverVersionCode = driverVersionCode;
Yiwei Zhang96c01712019-02-19 16:00:25 -0800204 mGpuStats.driverBuildTime = driverBuildTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800205 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang794d2952019-05-06 17:43:59 -0700206 mGpuStats.vulkanVersion = vulkanVersion;
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800207}
208
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700209void GraphicsEnv::setDriverToLoad(GpuStatsInfo::Driver driver) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800210 ATRACE_CALL();
211
212 std::lock_guard<std::mutex> lock(mStatsLock);
213 switch (driver) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700214 case GpuStatsInfo::Driver::GL:
215 case GpuStatsInfo::Driver::GL_UPDATED:
216 case GpuStatsInfo::Driver::ANGLE: {
Yiwei Zhang472cab02019-08-05 17:57:41 -0700217 if (mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::NONE ||
218 mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::GL) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800219 mGpuStats.glDriverToLoad = driver;
220 break;
221 }
222
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700223 if (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800224 mGpuStats.glDriverFallback = driver;
225 }
226 break;
227 }
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700228 case GpuStatsInfo::Driver::VULKAN:
229 case GpuStatsInfo::Driver::VULKAN_UPDATED: {
Yiwei Zhang472cab02019-08-05 17:57:41 -0700230 if (mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::NONE ||
231 mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::VULKAN) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800232 mGpuStats.vkDriverToLoad = driver;
233 break;
234 }
235
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700236 if (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800237 mGpuStats.vkDriverFallback = driver;
238 }
239 break;
240 }
241 default:
242 break;
243 }
244}
245
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700246void GraphicsEnv::setDriverLoaded(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700247 int64_t driverLoadingTime) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800248 ATRACE_CALL();
249
250 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700251 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhangd3819382020-01-07 19:53:56 -0800252 mGpuStats.glDriverToSend = true;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700253 mGpuStats.glDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800254 } else {
Yiwei Zhangd3819382020-01-07 19:53:56 -0800255 mGpuStats.vkDriverToSend = true;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700256 mGpuStats.vkDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800257 }
258
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700259 sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800260}
261
Yiwei Zhangd9861812019-02-13 11:51:55 -0800262static sp<IGpuService> getGpuService() {
Yiwei Zhang8e097302019-07-08 16:11:12 -0700263 static const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
Yiwei Zhangd9861812019-02-13 11:51:55 -0800264 if (!binder) {
265 ALOGE("Failed to get gpu service");
266 return nullptr;
267 }
268
269 return interface_cast<IGpuService>(binder);
270}
271
Yiwei Zhangd3819382020-01-07 19:53:56 -0800272bool GraphicsEnv::readyToSendGpuStatsLocked() {
273 // Only send stats for processes having at least one activity launched and that process doesn't
274 // skip the GraphicsEnvironment setup.
275 return mActivityLaunched && !mGpuStats.appPackageName.empty();
276}
277
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700278void GraphicsEnv::setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value) {
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700279 ATRACE_CALL();
280
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700281 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangd3819382020-01-07 19:53:56 -0800282 if (!readyToSendGpuStatsLocked()) return;
283
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700284 const sp<IGpuService> gpuService = getGpuService();
285 if (gpuService) {
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700286 gpuService->setTargetStats(mGpuStats.appPackageName, mGpuStats.driverVersionCode, stats,
287 value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700288 }
289}
290
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700291void GraphicsEnv::sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800292 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800293 ATRACE_CALL();
294
Yiwei Zhangd3819382020-01-07 19:53:56 -0800295 if (!readyToSendGpuStatsLocked()) return;
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800296
297 ALOGV("sendGpuStats:\n"
298 "\tdriverPackageName[%s]\n"
299 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800300 "\tdriverVersionCode[%" PRIu64 "]\n"
301 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800302 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700303 "\tvulkanVersion[%d]\n"
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700304 "\tapi[%d]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800305 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800306 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800307 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-19 16:00:25 -0800308 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700309 mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime);
310
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700311 GpuStatsInfo::Driver driver = GpuStatsInfo::Driver::NONE;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700312 bool isIntendedDriverLoaded = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700313 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700314 driver = mGpuStats.glDriverToLoad;
315 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700316 isDriverLoaded && (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700317 } else {
318 driver = mGpuStats.vkDriverToLoad;
319 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700320 isDriverLoaded && (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700321 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800322
Yiwei Zhangd9861812019-02-13 11:51:55 -0800323 const sp<IGpuService> gpuService = getGpuService();
324 if (gpuService) {
325 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800326 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700327 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700328 isIntendedDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800329 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800330}
331
Adam Bodnar0afcca02019-09-17 13:23:17 -0700332bool GraphicsEnv::setInjectLayersPrSetDumpable() {
333 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
334 return false;
335 }
336 return true;
337}
338
Tim Van Patten5f744f12018-12-12 11:46:21 -0700339void* GraphicsEnv::loadLibrary(std::string name) {
340 const android_dlextinfo dlextinfo = {
341 .flags = ANDROID_DLEXT_USE_NAMESPACE,
342 .library_namespace = getAngleNamespace(),
343 };
344
345 std::string libName = std::string("lib") + name + "_angle.so";
346
347 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
348
349 if (so) {
350 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
351 return so;
352 } else {
353 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
354 }
355
356 return nullptr;
357}
358
Tim Van Patten5f744f12018-12-12 11:46:21 -0700359bool GraphicsEnv::shouldUseAngle(std::string appName) {
360 if (appName != mAngleAppName) {
361 // Make sure we are checking the app we were init'ed for
362 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
363 appName.c_str());
364 return false;
365 }
366
367 return shouldUseAngle();
368}
369
370bool GraphicsEnv::shouldUseAngle() {
371 // Make sure we are init'ed
372 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700373 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700374 return false;
375 }
376
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700377 return (mUseAngle == YES) ? true : false;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700378}
379
Ian Elliott7c0a7ee2022-04-21 12:41:03 -0600380bool GraphicsEnv::angleIsSystemDriver() {
381 // Make sure we are init'ed
382 if (mAngleAppName.empty()) {
383 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
384 return false;
385 }
386
387 return (mAngleIsSystemDriver == YES) ? true : false;
388}
389
390bool GraphicsEnv::shouldForceLegacyDriver() {
391 // Make sure we are init'ed
392 if (mAngleAppName.empty()) {
393 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
394 return false;
395 }
396
397 return (mAngleIsSystemDriver == YES && mUseAngle == NO) ? true : false;
398}
399
400std::string GraphicsEnv::getLegacySuffix() {
401 return mLegacyDriverSuffix;
402}
403
Tim Van Patten5f744f12018-12-12 11:46:21 -0700404void GraphicsEnv::updateUseAngle() {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700405 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700406
407 const char* ANGLE_PREFER_ANGLE = "angle";
Ian Elliott7c0a7ee2022-04-21 12:41:03 -0600408 const char* ANGLE_PREFER_LEGACY = "legacy";
409 // The following is a deprecated version of "legacy"
Tim Van Patten5f744f12018-12-12 11:46:21 -0700410 const char* ANGLE_PREFER_NATIVE = "native";
411
Tim Van Pattena53dcb22021-05-21 17:51:59 -0600412 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700413 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
Ian Elliott7c0a7ee2022-04-21 12:41:03 -0600414 ALOGI("Using ANGLE, the %s GLES driver for package '%s'",
415 mAngleIsSystemDriver == YES ? "system" : "optional", mAngleAppName.c_str());
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700416 mUseAngle = YES;
Ian Elliott7c0a7ee2022-04-21 12:41:03 -0600417 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_LEGACY ||
418 mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
419 ALOGI("Using the (%s) Legacy GLES driver for package '%s'",
420 mAngleIsSystemDriver == YES ? "optional" : "system", mAngleAppName.c_str());
Tim Van Patten5f744f12018-12-12 11:46:21 -0700421 } else {
Tim Van Pattena53dcb22021-05-21 17:51:59 -0600422 ALOGV("User set invalid \"Developer Options\": '%s'", mAngleDeveloperOptIn.c_str());
Tim Van Patten5f744f12018-12-12 11:46:21 -0700423 }
424}
425
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600426void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Ian Elliott7c0a7ee2022-04-21 12:41:03 -0600427 const bool angleIsSystemDriver, const std::string developerOptIn,
Tim Van Pattena53dcb22021-05-21 17:51:59 -0600428 const std::vector<std::string> eglFeatures) {
Ian Elliott7c0a7ee2022-04-21 12:41:03 -0600429 // Set whether ANGLE is the system driver:
430 mAngleIsSystemDriver = angleIsSystemDriver ? YES : NO;
431
432 // Note: Given the current logic and lack of the old rules file processing,
433 // there seems to be little chance that mUseAngle != UNKNOWN. Leave this
434 // for now, even though it seems outdated.
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700435 if (mUseAngle != UNKNOWN) {
436 // We've already figured out an answer for this app, so just return.
437 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
438 (mUseAngle == YES) ? "true" : "false");
439 return;
440 }
441
Peiyong Lin2f707e62020-09-26 13:52:10 -0700442 mAngleEglFeatures = std::move(eglFeatures);
443
Tim Van Patten5f744f12018-12-12 11:46:21 -0700444 ALOGV("setting ANGLE path to '%s'", path.c_str());
445 mAnglePath = path;
446 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
447 mAngleAppName = appName;
448 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
449 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600450
Tim Van Patten5f744f12018-12-12 11:46:21 -0700451 // Update the current status of whether we should use ANGLE or not
452 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600453}
454
Ian Elliott7c0a7ee2022-04-21 12:41:03 -0600455void GraphicsEnv::setLegacyDriverInfo(const std::string appName, const bool angleIsSystemDriver,
456 const std::string legacyDriverName) {
457 ALOGV("setting legacy app name to '%s'", appName.c_str());
458 mAngleAppName = appName;
459
460 // Force the use of the legacy driver instead of ANGLE
461 const char* ANGLE_PREFER_LEGACY = "legacy";
462 mAngleDeveloperOptIn = ANGLE_PREFER_LEGACY;
463 ALOGV("setting ANGLE application opt-in to 'legacy'");
464
465 // Set whether ANGLE is the system driver:
466 mAngleIsSystemDriver = angleIsSystemDriver ? YES : NO;
467
468 mLegacyDriverSuffix = legacyDriverName;
469
470 // Update the current status of whether we should use ANGLE or not
471 updateUseAngle();
472}
473
Victor Khimenko4819b522018-07-13 17:24:18 +0200474void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600475 if (mLayerPaths.empty()) {
476 mLayerPaths = layerPaths;
477 mAppNamespace = appNamespace;
478 } else {
479 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800480 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600481 }
482}
483
Victor Khimenko4819b522018-07-13 17:24:18 +0200484NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600485 return mAppNamespace;
486}
487
Tim Van Patten5f744f12018-12-12 11:46:21 -0700488std::string& GraphicsEnv::getAngleAppName() {
489 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600490}
491
Peiyong Lin2f707e62020-09-26 13:52:10 -0700492const std::vector<std::string>& GraphicsEnv::getAngleEglFeatures() {
493 return mAngleEglFeatures;
494}
495
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600496const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600497 return mLayerPaths;
498}
499
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600500const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600501 return mDebugLayers;
502}
503
Cody Northropb9b01b62018-10-23 13:13:10 -0600504const std::string& GraphicsEnv::getDebugLayersGLES() {
505 return mDebugLayersGLES;
506}
507
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600508void GraphicsEnv::setDebugLayers(const std::string layers) {
509 mDebugLayers = layers;
510}
511
Cody Northropb9b01b62018-10-23 13:13:10 -0600512void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
513 mDebugLayersGLES = layers;
514}
515
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700516// Return true if all the required libraries from vndk and sphal namespace are
Yiwei Zhang9058b8f2020-11-12 20:23:00 +0000517// linked to the updatable gfx driver namespace correctly.
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700518bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* vndkNamespace) {
519 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
520 if (llndkLibraries.empty()) {
521 return false;
522 }
523 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
524 ALOGE("Failed to link default namespace[%s]", dlerror());
525 return false;
526 }
527
528 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
529 if (vndkspLibraries.empty()) {
530 return false;
531 }
532 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
533 ALOGE("Failed to link vndk namespace[%s]", dlerror());
534 return false;
535 }
536
537 if (mSphalLibraries.empty()) {
538 return true;
539 }
540
541 // Make additional libraries in sphal to be accessible
542 auto sphalNamespace = android_get_exported_namespace("sphal");
543 if (!sphalNamespace) {
544 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
545 mSphalLibraries.c_str());
546 return false;
547 }
548
549 if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) {
550 ALOGE("Failed to link sphal namespace[%s]", dlerror());
551 return false;
552 }
553
554 return true;
555}
556
Jesse Hall53457db2016-12-14 16:54:06 -0800557android_namespace_t* GraphicsEnv::getDriverNamespace() {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700558 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Yiwei Zhang64d89212018-11-27 19:58:29 -0800559
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700560 if (mDriverNamespace) {
561 return mDriverNamespace;
562 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800563
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700564 if (mDriverPath.empty()) {
Peiyong Lin0acbfdc2020-06-17 18:47:12 -0700565 // For an application process, driver path is empty means this application is not opted in
566 // to use updatable driver. Application process doesn't have the ability to set up
567 // environment variables and hence before `getenv` call will return.
568 // For a process that is not an application process, if it's run from an environment,
569 // for example shell, where environment variables can be set, then it can opt into using
570 // udpatable driver by setting UPDATABLE_GFX_DRIVER to 1. By setting to 1 the developer
571 // driver will be used currently.
572 // TODO(b/159240322) Support the production updatable driver.
573 const char* id = getenv("UPDATABLE_GFX_DRIVER");
574 if (id == nullptr || std::strcmp(id, "1")) {
575 return nullptr;
576 }
577 const sp<IGpuService> gpuService = getGpuService();
578 if (!gpuService) {
579 return nullptr;
580 }
581 mDriverPath = gpuService->getUpdatableDriverPath();
582 if (mDriverPath.empty()) {
583 return nullptr;
584 }
585 mDriverPath.append(UPDATABLE_DRIVER_ABI);
586 ALOGI("Driver path is setup via UPDATABLE_GFX_DRIVER: %s", mDriverPath.c_str());
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700587 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800588
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700589 auto vndkNamespace = android_get_exported_namespace("vndk");
590 if (!vndkNamespace) {
591 return nullptr;
592 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800593
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700594 mDriverNamespace = android_create_namespace("gfx driver",
595 mDriverPath.c_str(), // ld_library_path
596 mDriverPath.c_str(), // default_library_path
597 ANDROID_NAMESPACE_TYPE_ISOLATED,
598 nullptr, // permitted_when_isolated_path
599 nullptr);
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800600
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700601 if (!linkDriverNamespaceLocked(vndkNamespace)) {
602 mDriverNamespace = nullptr;
603 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800604
Jesse Hall53457db2016-12-14 16:54:06 -0800605 return mDriverNamespace;
606}
607
Peiyong Lindb3ed6e2020-01-09 18:43:27 -0800608std::string GraphicsEnv::getDriverPath() const {
609 return mDriverPath;
610}
611
Cody Northrop1f00e172018-04-02 11:23:31 -0600612android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700613 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600614
Cody Northrop3892cfa2019-01-30 10:03:12 -0700615 if (mAngleNamespace) {
616 return mAngleNamespace;
617 }
618
619 if (mAnglePath.empty()) {
620 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
621 return nullptr;
622 }
623
624 mAngleNamespace = android_create_namespace("ANGLE",
625 nullptr, // ld_library_path
626 mAnglePath.c_str(), // default_library_path
Yiwei Zhang5b7604f2020-05-08 14:23:34 -0700627 ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED,
Cody Northrop3892cfa2019-01-30 10:03:12 -0700628 nullptr, // permitted_when_isolated_path
629 nullptr);
630
631 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600632
633 return mAngleNamespace;
634}
635
Jesse Hall90b25ed2016-12-12 12:56:46 -0800636} // namespace android