blob: b8c6cfe1f6e42ebf8d732001a8dffe2fdc3466cf [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
Tim Van Patten5f744f12018-12-12 11:46:21 -0700155void* GraphicsEnv::loadLibrary(std::string name) {
156 const android_dlextinfo dlextinfo = {
157 .flags = ANDROID_DLEXT_USE_NAMESPACE,
158 .library_namespace = getAngleNamespace(),
159 };
160
161 std::string libName = std::string("lib") + name + "_angle.so";
162
163 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
164
165 if (so) {
166 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
167 return so;
168 } else {
169 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
170 }
171
172 return nullptr;
173}
174
175bool GraphicsEnv::checkAngleRules(void* so) {
176 char manufacturer[PROPERTY_VALUE_MAX];
177 char model[PROPERTY_VALUE_MAX];
178 property_get("ro.product.manufacturer", manufacturer, "UNSET");
179 property_get("ro.product.model", model, "UNSET");
180
181 auto ANGLEGetFeatureSupportUtilAPIVersion =
182 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
183 "ANGLEGetFeatureSupportUtilAPIVersion");
184
185 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
186 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
187 return false;
188 }
189
190 // Negotiate the interface version by requesting most recent known to the platform
191 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
192 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
193 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
194 "requested version %u",
195 versionToUse);
196 return false;
197 }
198
199 // Add and remove versions below as needed
200 bool useAngle = false;
201 switch (versionToUse) {
202 case 2: {
203 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
204 void* rulesHandle = nullptr;
205 int rulesVersion = 0;
206 void* systemInfoHandle = nullptr;
207
208 // Get the symbols for the feature-support-utility library:
209#define GET_SYMBOL(symbol) \
210 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
211 if (!symbol) { \
212 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
213 break; \
214 }
215 GET_SYMBOL(ANGLEAndroidParseRulesString);
216 GET_SYMBOL(ANGLEGetSystemInfo);
217 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
218 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
219 GET_SYMBOL(ANGLEFreeRulesHandle);
220 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
221
222 // Parse the rules, obtain the SystemInfo, and evaluate the
223 // application against the rules:
224 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
225 ALOGW("ANGLE feature-support library cannot parse rules file");
226 break;
227 }
228 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
229 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
230 break;
231 }
232 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) {
233 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
234 break;
235 }
236 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
237 systemInfoHandle, mAngleAppName.c_str());
238 (ANGLEFreeRulesHandle)(rulesHandle);
239 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
240 } break;
241
242 default:
243 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
244 }
245
246 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
247 return useAngle;
248}
249
250bool GraphicsEnv::shouldUseAngle(std::string appName) {
251 if (appName != mAngleAppName) {
252 // Make sure we are checking the app we were init'ed for
253 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
254 appName.c_str());
255 return false;
256 }
257
258 return shouldUseAngle();
259}
260
261bool GraphicsEnv::shouldUseAngle() {
262 // Make sure we are init'ed
263 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700264 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700265 return false;
266 }
267
268 return mUseAngle;
269}
270
271void GraphicsEnv::updateUseAngle() {
272 mUseAngle = false;
273
274 const char* ANGLE_PREFER_ANGLE = "angle";
275 const char* ANGLE_PREFER_NATIVE = "native";
276
277 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
278 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
279 mUseAngle = true;
280 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
281 ALOGV("User set \"Developer Options\" to force the use of Native");
282 mUseAngle = false;
283 } else {
284 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
285 // load ANGLE and call the updatable opt-in/out logic:
Cody Northropc15d3822019-01-17 10:26:47 -0700286 void* featureSo = loadLibrary("feature_support");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700287 if (featureSo) {
288 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
289 mUseAngle = checkAngleRules(featureSo);
290 dlclose(featureSo);
291 featureSo = nullptr;
292 } else {
293 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
294 }
295 }
296}
297
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600298void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700299 const std::string developerOptIn, const int rulesFd,
300 const long rulesOffset, const long rulesLength) {
Tim Van Patten5f744f12018-12-12 11:46:21 -0700301 ALOGV("setting ANGLE path to '%s'", path.c_str());
302 mAnglePath = path;
303 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
304 mAngleAppName = appName;
305 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
306 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600307
Tim Van Patten5f744f12018-12-12 11:46:21 -0700308 lseek(rulesFd, rulesOffset, SEEK_SET);
309 mRulesBuffer = std::vector<char>(rulesLength + 1);
310 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
311 if (numBytesRead < 0) {
312 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
313 numBytesRead = 0;
314 } else if (numBytesRead == 0) {
315 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600316 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700317 if (numBytesRead != rulesLength) {
318 ALOGW("Did not read all of the necessary bytes from the rules file."
319 "expected: %ld, got: %zd",
320 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700321 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700322 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 10:34:58 -0600323
Tim Van Patten5f744f12018-12-12 11:46:21 -0700324 // Update the current status of whether we should use ANGLE or not
325 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600326}
327
Victor Khimenko4819b522018-07-13 17:24:18 +0200328void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600329 if (mLayerPaths.empty()) {
330 mLayerPaths = layerPaths;
331 mAppNamespace = appNamespace;
332 } else {
333 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800334 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600335 }
336}
337
Victor Khimenko4819b522018-07-13 17:24:18 +0200338NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600339 return mAppNamespace;
340}
341
Tim Van Patten5f744f12018-12-12 11:46:21 -0700342std::string& GraphicsEnv::getAngleAppName() {
343 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600344}
345
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600346const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600347 return mLayerPaths;
348}
349
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600350const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600351 return mDebugLayers;
352}
353
Cody Northropb9b01b62018-10-23 13:13:10 -0600354const std::string& GraphicsEnv::getDebugLayersGLES() {
355 return mDebugLayersGLES;
356}
357
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600358void GraphicsEnv::setDebugLayers(const std::string layers) {
359 mDebugLayers = layers;
360}
361
Cody Northropb9b01b62018-10-23 13:13:10 -0600362void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
363 mDebugLayersGLES = layers;
364}
365
Jesse Hall53457db2016-12-14 16:54:06 -0800366android_namespace_t* GraphicsEnv::getDriverNamespace() {
367 static std::once_flag once;
368 std::call_once(once, [this]() {
Yiwei Zhang64d89212018-11-27 19:58:29 -0800369 if (mDriverPath.empty()) return;
370
371 auto vndkNamespace = android_get_exported_namespace("vndk");
372 if (!vndkNamespace) return;
373
Jesse Hall57de0ff2017-05-05 16:41:35 -0700374 mDriverNamespace = android_create_namespace("gfx driver",
Peiyong Lin2e9c74c2018-10-24 11:18:07 -0700375 mDriverPath.c_str(), // ld_library_path
Jesse Hall57de0ff2017-05-05 16:41:35 -0700376 mDriverPath.c_str(), // default_library_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800377 ANDROID_NAMESPACE_TYPE_ISOLATED,
Jesse Hall57de0ff2017-05-05 16:41:35 -0700378 nullptr, // permitted_when_isolated_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800379 nullptr);
380
381 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
382 if (llndkLibraries.empty()) {
383 mDriverNamespace = nullptr;
384 return;
385 }
386 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
387 ALOGE("Failed to link default namespace[%s]", dlerror());
388 mDriverNamespace = nullptr;
389 return;
390 }
391
392 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
393 if (vndkspLibraries.empty()) {
394 mDriverNamespace = nullptr;
395 return;
396 }
397 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
398 ALOGE("Failed to link vndk namespace[%s]", dlerror());
399 mDriverNamespace = nullptr;
400 return;
401 }
Jesse Hall53457db2016-12-14 16:54:06 -0800402 });
Yiwei Zhang64d89212018-11-27 19:58:29 -0800403
Jesse Hall53457db2016-12-14 16:54:06 -0800404 return mDriverNamespace;
405}
406
Cody Northrop1f00e172018-04-02 11:23:31 -0600407android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700408 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600409
Cody Northrop3892cfa2019-01-30 10:03:12 -0700410 if (mAngleNamespace) {
411 return mAngleNamespace;
412 }
413
414 if (mAnglePath.empty()) {
415 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
416 return nullptr;
417 }
418
419 mAngleNamespace = android_create_namespace("ANGLE",
420 nullptr, // ld_library_path
421 mAnglePath.c_str(), // default_library_path
422 ANDROID_NAMESPACE_TYPE_SHARED |
423 ANDROID_NAMESPACE_TYPE_ISOLATED,
424 nullptr, // permitted_when_isolated_path
425 nullptr);
426
427 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600428
429 return mAngleNamespace;
430}
431
Jesse Hall90b25ed2016-12-12 12:56:46 -0800432} // namespace android