blob: 9dc74317144542c2fd4b6a1d7da0e351d6345508 [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()) {
265 ALOGE("App name is empty. setAngleInfo() must be called first to enable ANGLE.");
266 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:
287
288 // Check if ANGLE is enabled. Workaround for several bugs:
289 // b/119305693 b/119322355 b/119305887
290 // Something is not working correctly in the feature library
291 char prop[PROPERTY_VALUE_MAX];
292 property_get("debug.angle.enable", prop, "0");
293 void* featureSo = nullptr;
294 if (atoi(prop)) {
295 featureSo = loadLibrary("feature_support");
296 }
297 if (featureSo) {
298 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
299 mUseAngle = checkAngleRules(featureSo);
300 dlclose(featureSo);
301 featureSo = nullptr;
302 } else {
303 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
304 }
305 }
306}
307
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600308void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700309 const std::string developerOptIn, const int rulesFd,
310 const long rulesOffset, const long rulesLength) {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700311 ALOGV("setting ANGLE path to '%s'", path.c_str());
312 mAnglePath = path;
313 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
314 mAngleAppName = appName;
315 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
316 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600317
Tim Van Patten5f744f12018-12-12 11:46:21 -0700318 lseek(rulesFd, rulesOffset, SEEK_SET);
319 mRulesBuffer = std::vector<char>(rulesLength + 1);
320 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
321 if (numBytesRead < 0) {
322 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
323 numBytesRead = 0;
324 } else if (numBytesRead == 0) {
325 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600326 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700327 if (numBytesRead != rulesLength) {
328 ALOGW("Did not read all of the necessary bytes from the rules file."
329 "expected: %ld, got: %zd",
330 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700331 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700332 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 10:34:58 -0600333
Tim Van Patten5f744f12018-12-12 11:46:21 -0700334 // Update the current status of whether we should use ANGLE or not
335 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600336}
337
Victor Khimenko4819b522018-07-13 17:24:18 +0200338void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600339 if (mLayerPaths.empty()) {
340 mLayerPaths = layerPaths;
341 mAppNamespace = appNamespace;
342 } else {
343 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800344 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600345 }
346}
347
Victor Khimenko4819b522018-07-13 17:24:18 +0200348NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600349 return mAppNamespace;
350}
351
Tim Van Patten5f744f12018-12-12 11:46:21 -0700352std::string& GraphicsEnv::getAngleAppName() {
353 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600354}
355
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600356const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600357 return mLayerPaths;
358}
359
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600360const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600361 return mDebugLayers;
362}
363
Cody Northropb9b01b62018-10-23 13:13:10 -0600364const std::string& GraphicsEnv::getDebugLayersGLES() {
365 return mDebugLayersGLES;
366}
367
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600368void GraphicsEnv::setDebugLayers(const std::string layers) {
369 mDebugLayers = layers;
370}
371
Cody Northropb9b01b62018-10-23 13:13:10 -0600372void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
373 mDebugLayersGLES = layers;
374}
375
Jesse Hall53457db2016-12-14 16:54:06 -0800376android_namespace_t* GraphicsEnv::getDriverNamespace() {
377 static std::once_flag once;
378 std::call_once(once, [this]() {
Yiwei Zhang64d89212018-11-27 19:58:29 -0800379 if (mDriverPath.empty()) return;
380
381 auto vndkNamespace = android_get_exported_namespace("vndk");
382 if (!vndkNamespace) return;
383
Jesse Hall57de0ff2017-05-05 16:41:35 -0700384 mDriverNamespace = android_create_namespace("gfx driver",
Peiyong Lin2e9c74c2018-10-24 11:18:07 -0700385 mDriverPath.c_str(), // ld_library_path
Jesse Hall57de0ff2017-05-05 16:41:35 -0700386 mDriverPath.c_str(), // default_library_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800387 ANDROID_NAMESPACE_TYPE_ISOLATED,
Jesse Hall57de0ff2017-05-05 16:41:35 -0700388 nullptr, // permitted_when_isolated_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800389 nullptr);
390
391 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
392 if (llndkLibraries.empty()) {
393 mDriverNamespace = nullptr;
394 return;
395 }
396 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
397 ALOGE("Failed to link default namespace[%s]", dlerror());
398 mDriverNamespace = nullptr;
399 return;
400 }
401
402 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
403 if (vndkspLibraries.empty()) {
404 mDriverNamespace = nullptr;
405 return;
406 }
407 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
408 ALOGE("Failed to link vndk namespace[%s]", dlerror());
409 mDriverNamespace = nullptr;
410 return;
411 }
Jesse Hall53457db2016-12-14 16:54:06 -0800412 });
Yiwei Zhang64d89212018-11-27 19:58:29 -0800413
Jesse Hall53457db2016-12-14 16:54:06 -0800414 return mDriverNamespace;
415}
416
Cody Northrop1f00e172018-04-02 11:23:31 -0600417android_namespace_t* GraphicsEnv::getAngleNamespace() {
418 static std::once_flag once;
419 std::call_once(once, [this]() {
420 if (mAnglePath.empty()) return;
421
422 mAngleNamespace = android_create_namespace("ANGLE",
423 nullptr, // ld_library_path
424 mAnglePath.c_str(), // default_library_path
425 ANDROID_NAMESPACE_TYPE_SHARED |
426 ANDROID_NAMESPACE_TYPE_ISOLATED,
427 nullptr, // permitted_when_isolated_path
428 nullptr);
429 if (!mAngleNamespace) ALOGD("Could not create ANGLE namespace from default");
430 });
431
432 return mAngleNamespace;
433}
434
Jesse Hall90b25ed2016-12-12 12:56:46 -0800435} // namespace android