blob: aa2f3948352bdba6fc9f7030fde55c5ede7dc607 [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>
Jesse Hall53457db2016-12-14 16:54:06 -080033#include <mutex>
Tim Van Patten5f744f12018-12-12 11:46:21 -070034#include <string>
35
36#include <dlfcn.h>
Jesse Hall53457db2016-12-14 16:54:06 -080037
Jesse Hall57de0ff2017-05-05 16:41:35 -070038// TODO(b/37049319) Get this from a header once one exists
39extern "C" {
Yiwei Zhang64d89212018-11-27 19:58:29 -080040android_namespace_t* android_get_exported_namespace(const char*);
41android_namespace_t* android_create_namespace(const char* name, const char* ld_library_path,
42 const char* default_library_path, uint64_t type,
43 const char* permitted_when_isolated_path,
44 android_namespace_t* parent);
45bool android_link_namespaces(android_namespace_t* from, android_namespace_t* to,
46 const char* shared_libs_sonames);
Jiyong Park9b816a82018-01-02 17:37:37 +090047
Yiwei Zhang64d89212018-11-27 19:58:29 -080048enum {
49 ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
50 ANDROID_NAMESPACE_TYPE_SHARED = 2,
51};
Jesse Hall57de0ff2017-05-05 16:41:35 -070052}
53
Tim Van Patten5f744f12018-12-12 11:46:21 -070054// TODO(ianelliott@): Get the following from an ANGLE header:
55#define CURRENT_ANGLE_API_VERSION 2 // Current API verion we are targetting
56// Version-2 API:
57typedef bool (*fpANGLEGetFeatureSupportUtilAPIVersion)(unsigned int* versionToUse);
58typedef bool (*fpANGLEAndroidParseRulesString)(const char* rulesString, void** rulesHandle,
59 int* rulesVersion);
60typedef bool (*fpANGLEGetSystemInfo)(void** handle);
61typedef bool (*fpANGLEAddDeviceInfoToSystemInfo)(const char* deviceMfr, const char* deviceModel,
62 void* handle);
63typedef bool (*fpANGLEShouldBeUsedForApplication)(void* rulesHandle, int rulesVersion,
64 void* systemInfoHandle, const char* appName);
65typedef bool (*fpANGLEFreeRulesHandle)(void* handle);
66typedef bool (*fpANGLEFreeSystemInfoHandle)(void* handle);
67
Jesse Hall90b25ed2016-12-12 12:56:46 -080068namespace android {
69
Yiwei Zhang64d89212018-11-27 19:58:29 -080070enum NativeLibrary {
71 LLNDK = 0,
72 VNDKSP = 1,
73};
74
75static constexpr const char* kNativeLibrariesSystemConfigPath[] = {"/etc/llndk.libraries.txt",
76 "/etc/vndksp.libraries.txt"};
77
78static std::string vndkVersionStr() {
79#ifdef __BIONIC__
80 std::string version = android::base::GetProperty("ro.vndk.version", "");
81 if (version != "" && version != "current") {
82 return "." + version;
83 }
84#endif
85 return "";
86}
87
88static void insertVndkVersionStr(std::string* fileName) {
89 LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr");
90 size_t insertPos = fileName->find_last_of(".");
91 if (insertPos == std::string::npos) {
92 insertPos = fileName->length();
93 }
94 fileName->insert(insertPos, vndkVersionStr());
95}
96
97static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) {
98 // Read list of public native libraries from the config file.
99 std::string fileContent;
100 if (!base::ReadFileToString(configFile, &fileContent)) {
101 return false;
102 }
103
104 std::vector<std::string> lines = base::Split(fileContent, "\n");
105
106 for (auto& line : lines) {
107 auto trimmedLine = base::Trim(line);
108 if (!trimmedLine.empty()) {
109 soNames->push_back(trimmedLine);
110 }
111 }
112
113 return true;
114}
115
116static const std::string getSystemNativeLibraries(NativeLibrary type) {
117 static const char* androidRootEnv = getenv("ANDROID_ROOT");
118 static const std::string rootDir = androidRootEnv != nullptr ? androidRootEnv : "/system";
119
120 std::string nativeLibrariesSystemConfig = rootDir + kNativeLibrariesSystemConfigPath[type];
121
122 insertVndkVersionStr(&nativeLibrariesSystemConfig);
123
124 std::vector<std::string> soNames;
125 if (!readConfig(nativeLibrariesSystemConfig, &soNames)) {
126 ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str());
127 return "";
128 }
129
130 return base::Join(soNames, ':');
131}
132
Jesse Hall90b25ed2016-12-12 12:56:46 -0800133/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
134 static GraphicsEnv env;
135 return env;
136}
137
Cody Northrop629ce4e2018-10-15 07:22:09 -0600138int GraphicsEnv::getCanLoadSystemLibraries() {
139 if (property_get_bool("ro.debuggable", false) && prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
140 // Return an integer value since this crosses library boundaries
141 return 1;
142 }
143 return 0;
144}
145
Jesse Hall90b25ed2016-12-12 12:56:46 -0800146void GraphicsEnv::setDriverPath(const std::string path) {
147 if (!mDriverPath.empty()) {
Yiwei Zhang64d89212018-11-27 19:58:29 -0800148 ALOGV("ignoring attempt to change driver path from '%s' to '%s'", mDriverPath.c_str(),
149 path.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800150 return;
151 }
152 ALOGV("setting driver path to '%s'", path.c_str());
153 mDriverPath = path;
154}
155
Tim Van Patten5f744f12018-12-12 11:46:21 -0700156void* GraphicsEnv::loadLibrary(std::string name) {
157 const android_dlextinfo dlextinfo = {
158 .flags = ANDROID_DLEXT_USE_NAMESPACE,
159 .library_namespace = getAngleNamespace(),
160 };
161
162 std::string libName = std::string("lib") + name + "_angle.so";
163
164 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
165
166 if (so) {
167 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
168 return so;
169 } else {
170 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
171 }
172
173 return nullptr;
174}
175
176bool GraphicsEnv::checkAngleRules(void* so) {
177 char manufacturer[PROPERTY_VALUE_MAX];
178 char model[PROPERTY_VALUE_MAX];
179 property_get("ro.product.manufacturer", manufacturer, "UNSET");
180 property_get("ro.product.model", model, "UNSET");
181
182 auto ANGLEGetFeatureSupportUtilAPIVersion =
183 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
184 "ANGLEGetFeatureSupportUtilAPIVersion");
185
186 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
187 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
188 return false;
189 }
190
191 // Negotiate the interface version by requesting most recent known to the platform
192 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
193 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
194 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
195 "requested version %u",
196 versionToUse);
197 return false;
198 }
199
200 // Add and remove versions below as needed
201 bool useAngle = false;
202 switch (versionToUse) {
203 case 2: {
204 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
205 void* rulesHandle = nullptr;
206 int rulesVersion = 0;
207 void* systemInfoHandle = nullptr;
208
209 // Get the symbols for the feature-support-utility library:
210#define GET_SYMBOL(symbol) \
211 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
212 if (!symbol) { \
213 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
214 break; \
215 }
216 GET_SYMBOL(ANGLEAndroidParseRulesString);
217 GET_SYMBOL(ANGLEGetSystemInfo);
218 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
219 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
220 GET_SYMBOL(ANGLEFreeRulesHandle);
221 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
222
223 // Parse the rules, obtain the SystemInfo, and evaluate the
224 // application against the rules:
225 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
226 ALOGW("ANGLE feature-support library cannot parse rules file");
227 break;
228 }
229 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
230 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
231 break;
232 }
233 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) {
234 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
235 break;
236 }
237 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
238 systemInfoHandle, mAngleAppName.c_str());
239 (ANGLEFreeRulesHandle)(rulesHandle);
240 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
241 } break;
242
243 default:
244 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
245 }
246
247 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
248 return useAngle;
249}
250
251bool GraphicsEnv::shouldUseAngle(std::string appName) {
252 if (appName != mAngleAppName) {
253 // Make sure we are checking the app we were init'ed for
254 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
255 appName.c_str());
256 return false;
257 }
258
259 return shouldUseAngle();
260}
261
262bool GraphicsEnv::shouldUseAngle() {
263 // Make sure we are init'ed
264 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700265 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700266 return false;
267 }
268
269 return mUseAngle;
270}
271
272void GraphicsEnv::updateUseAngle() {
273 mUseAngle = false;
274
275 const char* ANGLE_PREFER_ANGLE = "angle";
276 const char* ANGLE_PREFER_NATIVE = "native";
277
278 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
279 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
280 mUseAngle = true;
281 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
282 ALOGV("User set \"Developer Options\" to force the use of Native");
283 mUseAngle = false;
284 } else {
285 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
286 // load ANGLE and call the updatable opt-in/out logic:
Cody Northropc15d3822019-01-17 10:26:47 -0700287 void* featureSo = loadLibrary("feature_support");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700288 if (featureSo) {
289 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
290 mUseAngle = checkAngleRules(featureSo);
291 dlclose(featureSo);
292 featureSo = nullptr;
293 } else {
294 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
295 }
296 }
297}
298
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600299void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700300 const std::string developerOptIn, const int rulesFd,
301 const long rulesOffset, const long rulesLength) {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700302 ALOGV("setting ANGLE path to '%s'", path.c_str());
303 mAnglePath = path;
304 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
305 mAngleAppName = appName;
306 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
307 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600308
Tim Van Patten5f744f12018-12-12 11:46:21 -0700309 lseek(rulesFd, rulesOffset, SEEK_SET);
310 mRulesBuffer = std::vector<char>(rulesLength + 1);
311 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
312 if (numBytesRead < 0) {
313 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
314 numBytesRead = 0;
315 } else if (numBytesRead == 0) {
316 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600317 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700318 if (numBytesRead != rulesLength) {
319 ALOGW("Did not read all of the necessary bytes from the rules file."
320 "expected: %ld, got: %zd",
321 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700322 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700323 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 10:34:58 -0600324
Tim Van Patten5f744f12018-12-12 11:46:21 -0700325 // Update the current status of whether we should use ANGLE or not
326 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600327}
328
Victor Khimenko4819b522018-07-13 17:24:18 +0200329void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600330 if (mLayerPaths.empty()) {
331 mLayerPaths = layerPaths;
332 mAppNamespace = appNamespace;
333 } else {
334 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800335 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600336 }
337}
338
Victor Khimenko4819b522018-07-13 17:24:18 +0200339NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600340 return mAppNamespace;
341}
342
Tim Van Patten5f744f12018-12-12 11:46:21 -0700343std::string& GraphicsEnv::getAngleAppName() {
344 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600345}
346
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600347const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600348 return mLayerPaths;
349}
350
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600351const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600352 return mDebugLayers;
353}
354
Cody Northropb9b01b62018-10-23 13:13:10 -0600355const std::string& GraphicsEnv::getDebugLayersGLES() {
356 return mDebugLayersGLES;
357}
358
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600359void GraphicsEnv::setDebugLayers(const std::string layers) {
360 mDebugLayers = layers;
361}
362
Cody Northropb9b01b62018-10-23 13:13:10 -0600363void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
364 mDebugLayersGLES = layers;
365}
366
Jesse Hall53457db2016-12-14 16:54:06 -0800367android_namespace_t* GraphicsEnv::getDriverNamespace() {
368 static std::once_flag once;
369 std::call_once(once, [this]() {
Yiwei Zhang64d89212018-11-27 19:58:29 -0800370 if (mDriverPath.empty()) return;
371
372 auto vndkNamespace = android_get_exported_namespace("vndk");
373 if (!vndkNamespace) return;
374
Jesse Hall57de0ff2017-05-05 16:41:35 -0700375 mDriverNamespace = android_create_namespace("gfx driver",
Peiyong Lin2e9c74c2018-10-24 11:18:07 -0700376 mDriverPath.c_str(), // ld_library_path
Jesse Hall57de0ff2017-05-05 16:41:35 -0700377 mDriverPath.c_str(), // default_library_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800378 ANDROID_NAMESPACE_TYPE_ISOLATED,
Jesse Hall57de0ff2017-05-05 16:41:35 -0700379 nullptr, // permitted_when_isolated_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800380 nullptr);
381
382 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
383 if (llndkLibraries.empty()) {
384 mDriverNamespace = nullptr;
385 return;
386 }
387 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
388 ALOGE("Failed to link default namespace[%s]", dlerror());
389 mDriverNamespace = nullptr;
390 return;
391 }
392
393 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
394 if (vndkspLibraries.empty()) {
395 mDriverNamespace = nullptr;
396 return;
397 }
398 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
399 ALOGE("Failed to link vndk namespace[%s]", dlerror());
400 mDriverNamespace = nullptr;
401 return;
402 }
Jesse Hall53457db2016-12-14 16:54:06 -0800403 });
Yiwei Zhang64d89212018-11-27 19:58:29 -0800404
Jesse Hall53457db2016-12-14 16:54:06 -0800405 return mDriverNamespace;
406}
407
Cody Northrop1f00e172018-04-02 11:23:31 -0600408android_namespace_t* GraphicsEnv::getAngleNamespace() {
409 static std::once_flag once;
410 std::call_once(once, [this]() {
411 if (mAnglePath.empty()) return;
412
413 mAngleNamespace = android_create_namespace("ANGLE",
414 nullptr, // ld_library_path
415 mAnglePath.c_str(), // default_library_path
416 ANDROID_NAMESPACE_TYPE_SHARED |
417 ANDROID_NAMESPACE_TYPE_ISOLATED,
418 nullptr, // permitted_when_isolated_path
419 nullptr);
420 if (!mAngleNamespace) ALOGD("Could not create ANGLE namespace from default");
421 });
422
423 return mAngleNamespace;
424}
425
Jesse Hall90b25ed2016-12-12 12:56:46 -0800426} // namespace android