blob: 337308b81393471d9627f16df7646c8e8e9d5fa6 [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
17//#define LOG_NDEBUG 1
18#define LOG_TAG "GraphicsEnv"
Jiyong Park27c39e12017-05-08 13:00:02 +090019#include <graphicsenv/GraphicsEnv.h>
Jesse Hall90b25ed2016-12-12 12:56:46 -080020
Yiwei Zhang64d89212018-11-27 19:58:29 -080021#include <dlfcn.h>
Tim Van Patten5f744f12018-12-12 11:46:21 -070022#include <unistd.h>
Yiwei Zhang64d89212018-11-27 19:58:29 -080023
24#include <android-base/file.h>
25#include <android-base/properties.h>
26#include <android-base/strings.h>
27#include <android/dlext.h>
28#include <cutils/properties.h>
29#include <log/log.h>
Cody Northrop629ce4e2018-10-15 07:22:09 -060030#include <sys/prctl.h>
31
Tim Van Patten5f744f12018-12-12 11:46:21 -070032#include <memory>
Tim Van Patten5f744f12018-12-12 11:46:21 -070033#include <string>
34
35#include <dlfcn.h>
Jesse Hall53457db2016-12-14 16:54:06 -080036
Jesse Hall57de0ff2017-05-05 16:41:35 -070037// TODO(b/37049319) Get this from a header once one exists
38extern "C" {
Yiwei Zhang64d89212018-11-27 19:58:29 -080039android_namespace_t* android_get_exported_namespace(const char*);
40android_namespace_t* android_create_namespace(const char* name, const char* ld_library_path,
41 const char* default_library_path, uint64_t type,
42 const char* permitted_when_isolated_path,
43 android_namespace_t* parent);
44bool android_link_namespaces(android_namespace_t* from, android_namespace_t* to,
45 const char* shared_libs_sonames);
Jiyong Park9b816a82018-01-02 17:37:37 +090046
Yiwei Zhang64d89212018-11-27 19:58:29 -080047enum {
48 ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
49 ANDROID_NAMESPACE_TYPE_SHARED = 2,
50};
Jesse Hall57de0ff2017-05-05 16:41:35 -070051}
52
Tim Van Patten5f744f12018-12-12 11:46:21 -070053// TODO(ianelliott@): Get the following from an ANGLE header:
54#define CURRENT_ANGLE_API_VERSION 2 // Current API verion we are targetting
55// Version-2 API:
56typedef bool (*fpANGLEGetFeatureSupportUtilAPIVersion)(unsigned int* versionToUse);
57typedef bool (*fpANGLEAndroidParseRulesString)(const char* rulesString, void** rulesHandle,
58 int* rulesVersion);
59typedef bool (*fpANGLEGetSystemInfo)(void** handle);
60typedef bool (*fpANGLEAddDeviceInfoToSystemInfo)(const char* deviceMfr, const char* deviceModel,
61 void* handle);
62typedef bool (*fpANGLEShouldBeUsedForApplication)(void* rulesHandle, int rulesVersion,
63 void* systemInfoHandle, const char* appName);
64typedef bool (*fpANGLEFreeRulesHandle)(void* handle);
65typedef bool (*fpANGLEFreeSystemInfoHandle)(void* handle);
66
Jesse Hall90b25ed2016-12-12 12:56:46 -080067namespace android {
68
Yiwei Zhang64d89212018-11-27 19:58:29 -080069enum NativeLibrary {
70 LLNDK = 0,
71 VNDKSP = 1,
72};
73
74static constexpr const char* kNativeLibrariesSystemConfigPath[] = {"/etc/llndk.libraries.txt",
75 "/etc/vndksp.libraries.txt"};
76
77static std::string vndkVersionStr() {
78#ifdef __BIONIC__
79 std::string version = android::base::GetProperty("ro.vndk.version", "");
80 if (version != "" && version != "current") {
81 return "." + version;
82 }
83#endif
84 return "";
85}
86
87static void insertVndkVersionStr(std::string* fileName) {
88 LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr");
89 size_t insertPos = fileName->find_last_of(".");
90 if (insertPos == std::string::npos) {
91 insertPos = fileName->length();
92 }
93 fileName->insert(insertPos, vndkVersionStr());
94}
95
96static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) {
97 // Read list of public native libraries from the config file.
98 std::string fileContent;
99 if (!base::ReadFileToString(configFile, &fileContent)) {
100 return false;
101 }
102
103 std::vector<std::string> lines = base::Split(fileContent, "\n");
104
105 for (auto& line : lines) {
106 auto trimmedLine = base::Trim(line);
107 if (!trimmedLine.empty()) {
108 soNames->push_back(trimmedLine);
109 }
110 }
111
112 return true;
113}
114
115static const std::string getSystemNativeLibraries(NativeLibrary type) {
116 static const char* androidRootEnv = getenv("ANDROID_ROOT");
117 static const std::string rootDir = androidRootEnv != nullptr ? androidRootEnv : "/system";
118
119 std::string nativeLibrariesSystemConfig = rootDir + kNativeLibrariesSystemConfigPath[type];
120
121 insertVndkVersionStr(&nativeLibrariesSystemConfig);
122
123 std::vector<std::string> soNames;
124 if (!readConfig(nativeLibrariesSystemConfig, &soNames)) {
125 ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str());
126 return "";
127 }
128
129 return base::Join(soNames, ':');
130}
131
Jesse Hall90b25ed2016-12-12 12:56:46 -0800132/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
133 static GraphicsEnv env;
134 return env;
135}
136
Cody Northrop629ce4e2018-10-15 07:22:09 -0600137int GraphicsEnv::getCanLoadSystemLibraries() {
138 if (property_get_bool("ro.debuggable", false) && prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
139 // Return an integer value since this crosses library boundaries
140 return 1;
141 }
142 return 0;
143}
144
Jesse Hall90b25ed2016-12-12 12:56:46 -0800145void GraphicsEnv::setDriverPath(const std::string path) {
146 if (!mDriverPath.empty()) {
Yiwei Zhang64d89212018-11-27 19:58:29 -0800147 ALOGV("ignoring attempt to change driver path from '%s' to '%s'", mDriverPath.c_str(),
148 path.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800149 return;
150 }
151 ALOGV("setting driver path to '%s'", path.c_str());
152 mDriverPath = path;
153}
154
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800155void GraphicsEnv::setGpuStats(const std::string driverPackageName,
156 const std::string driverVersionName, const uint64_t driverVersionCode,
157 const std::string appPackageName) {
158 ALOGV("setGpuStats: drvPkgName[%s], drvVerName[%s], drvVerCode[%lld], appPkgName[%s]",
159 driverPackageName.c_str(), driverVersionName.c_str(), (long long)driverVersionCode,
160 appPackageName.c_str());
161 mGpuStats = {
162 .driverPackageName = driverPackageName,
163 .driverVersionName = driverVersionName,
164 .driverVersionCode = driverVersionCode,
165 .appPackageName = appPackageName,
166 };
167}
168
Tim Van Patten5f744f12018-12-12 11:46:21 -0700169void* GraphicsEnv::loadLibrary(std::string name) {
170 const android_dlextinfo dlextinfo = {
171 .flags = ANDROID_DLEXT_USE_NAMESPACE,
172 .library_namespace = getAngleNamespace(),
173 };
174
175 std::string libName = std::string("lib") + name + "_angle.so";
176
177 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
178
179 if (so) {
180 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
181 return so;
182 } else {
183 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
184 }
185
186 return nullptr;
187}
188
189bool GraphicsEnv::checkAngleRules(void* so) {
190 char manufacturer[PROPERTY_VALUE_MAX];
191 char model[PROPERTY_VALUE_MAX];
192 property_get("ro.product.manufacturer", manufacturer, "UNSET");
193 property_get("ro.product.model", model, "UNSET");
194
195 auto ANGLEGetFeatureSupportUtilAPIVersion =
196 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
197 "ANGLEGetFeatureSupportUtilAPIVersion");
198
199 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
200 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
201 return false;
202 }
203
204 // Negotiate the interface version by requesting most recent known to the platform
205 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
206 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
207 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
208 "requested version %u",
209 versionToUse);
210 return false;
211 }
212
213 // Add and remove versions below as needed
214 bool useAngle = false;
215 switch (versionToUse) {
216 case 2: {
217 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
218 void* rulesHandle = nullptr;
219 int rulesVersion = 0;
220 void* systemInfoHandle = nullptr;
221
222 // Get the symbols for the feature-support-utility library:
223#define GET_SYMBOL(symbol) \
224 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
225 if (!symbol) { \
226 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
227 break; \
228 }
229 GET_SYMBOL(ANGLEAndroidParseRulesString);
230 GET_SYMBOL(ANGLEGetSystemInfo);
231 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
232 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
233 GET_SYMBOL(ANGLEFreeRulesHandle);
234 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
235
236 // Parse the rules, obtain the SystemInfo, and evaluate the
237 // application against the rules:
238 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
239 ALOGW("ANGLE feature-support library cannot parse rules file");
240 break;
241 }
242 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
243 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
244 break;
245 }
246 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) {
247 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
248 break;
249 }
250 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
251 systemInfoHandle, mAngleAppName.c_str());
252 (ANGLEFreeRulesHandle)(rulesHandle);
253 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
254 } break;
255
256 default:
257 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
258 }
259
260 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
261 return useAngle;
262}
263
264bool GraphicsEnv::shouldUseAngle(std::string appName) {
265 if (appName != mAngleAppName) {
266 // Make sure we are checking the app we were init'ed for
267 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
268 appName.c_str());
269 return false;
270 }
271
272 return shouldUseAngle();
273}
274
275bool GraphicsEnv::shouldUseAngle() {
276 // Make sure we are init'ed
277 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700278 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700279 return false;
280 }
281
282 return mUseAngle;
283}
284
285void GraphicsEnv::updateUseAngle() {
286 mUseAngle = false;
287
288 const char* ANGLE_PREFER_ANGLE = "angle";
289 const char* ANGLE_PREFER_NATIVE = "native";
290
291 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
292 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
293 mUseAngle = true;
294 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
295 ALOGV("User set \"Developer Options\" to force the use of Native");
296 mUseAngle = false;
297 } else {
298 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
299 // load ANGLE and call the updatable opt-in/out logic:
Cody Northropc15d3822019-01-17 10:26:47 -0700300 void* featureSo = loadLibrary("feature_support");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700301 if (featureSo) {
302 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
303 mUseAngle = checkAngleRules(featureSo);
304 dlclose(featureSo);
305 featureSo = nullptr;
306 } else {
307 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
308 }
309 }
310}
311
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600312void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700313 const std::string developerOptIn, const int rulesFd,
314 const long rulesOffset, const long rulesLength) {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700315 ALOGV("setting ANGLE path to '%s'", path.c_str());
316 mAnglePath = path;
317 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
318 mAngleAppName = appName;
319 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
320 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600321
Tim Van Patten5f744f12018-12-12 11:46:21 -0700322 lseek(rulesFd, rulesOffset, SEEK_SET);
323 mRulesBuffer = std::vector<char>(rulesLength + 1);
324 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
325 if (numBytesRead < 0) {
326 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
327 numBytesRead = 0;
328 } else if (numBytesRead == 0) {
329 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600330 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700331 if (numBytesRead != rulesLength) {
332 ALOGW("Did not read all of the necessary bytes from the rules file."
333 "expected: %ld, got: %zd",
334 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700335 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700336 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 10:34:58 -0600337
Tim Van Patten5f744f12018-12-12 11:46:21 -0700338 // Update the current status of whether we should use ANGLE or not
339 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600340}
341
Victor Khimenko4819b522018-07-13 17:24:18 +0200342void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600343 if (mLayerPaths.empty()) {
344 mLayerPaths = layerPaths;
345 mAppNamespace = appNamespace;
346 } else {
347 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800348 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600349 }
350}
351
Victor Khimenko4819b522018-07-13 17:24:18 +0200352NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600353 return mAppNamespace;
354}
355
Tim Van Patten5f744f12018-12-12 11:46:21 -0700356std::string& GraphicsEnv::getAngleAppName() {
357 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600358}
359
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600360const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600361 return mLayerPaths;
362}
363
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600364const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600365 return mDebugLayers;
366}
367
Cody Northropb9b01b62018-10-23 13:13:10 -0600368const std::string& GraphicsEnv::getDebugLayersGLES() {
369 return mDebugLayersGLES;
370}
371
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600372void GraphicsEnv::setDebugLayers(const std::string layers) {
373 mDebugLayers = layers;
374}
375
Cody Northropb9b01b62018-10-23 13:13:10 -0600376void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
377 mDebugLayersGLES = layers;
378}
379
Jesse Hall53457db2016-12-14 16:54:06 -0800380android_namespace_t* GraphicsEnv::getDriverNamespace() {
381 static std::once_flag once;
382 std::call_once(once, [this]() {
Yiwei Zhang64d89212018-11-27 19:58:29 -0800383 if (mDriverPath.empty()) return;
384
385 auto vndkNamespace = android_get_exported_namespace("vndk");
386 if (!vndkNamespace) return;
387
Jesse Hall57de0ff2017-05-05 16:41:35 -0700388 mDriverNamespace = android_create_namespace("gfx driver",
Peiyong Lin2e9c74c2018-10-24 11:18:07 -0700389 mDriverPath.c_str(), // ld_library_path
Jesse Hall57de0ff2017-05-05 16:41:35 -0700390 mDriverPath.c_str(), // default_library_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800391 ANDROID_NAMESPACE_TYPE_ISOLATED,
Jesse Hall57de0ff2017-05-05 16:41:35 -0700392 nullptr, // permitted_when_isolated_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800393 nullptr);
394
395 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
396 if (llndkLibraries.empty()) {
397 mDriverNamespace = nullptr;
398 return;
399 }
400 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
401 ALOGE("Failed to link default namespace[%s]", dlerror());
402 mDriverNamespace = nullptr;
403 return;
404 }
405
406 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
407 if (vndkspLibraries.empty()) {
408 mDriverNamespace = nullptr;
409 return;
410 }
411 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
412 ALOGE("Failed to link vndk namespace[%s]", dlerror());
413 mDriverNamespace = nullptr;
414 return;
415 }
Jesse Hall53457db2016-12-14 16:54:06 -0800416 });
Yiwei Zhang64d89212018-11-27 19:58:29 -0800417
Jesse Hall53457db2016-12-14 16:54:06 -0800418 return mDriverNamespace;
419}
420
Cody Northrop1f00e172018-04-02 11:23:31 -0600421android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700422 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600423
Cody Northrop3892cfa2019-01-30 10:03:12 -0700424 if (mAngleNamespace) {
425 return mAngleNamespace;
426 }
427
428 if (mAnglePath.empty()) {
429 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
430 return nullptr;
431 }
432
433 mAngleNamespace = android_create_namespace("ANGLE",
434 nullptr, // ld_library_path
435 mAnglePath.c_str(), // default_library_path
436 ANDROID_NAMESPACE_TYPE_SHARED |
437 ANDROID_NAMESPACE_TYPE_ISOLATED,
438 nullptr, // permitted_when_isolated_path
439 nullptr);
440
441 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600442
443 return mAngleNamespace;
444}
445
Jesse Hall90b25ed2016-12-12 12:56:46 -0800446} // namespace android