Refactor for Better Resource Management
The GraphicsEnvironment/GraphicsEnv code is being refactored to allow
for better resource management related to open files and file
descriptors and reducing the amount of times the rules files are parsed.
This is also laying the groundwork for other modules to query if ANGLE
will be loaded or not, for example to display a dialog box to the user
when ANGLE is selected for an app.
Bug: 120910315
Test: Verify CtsAngleIntegrationHostTestCases passes.
Change-Id: If985df7d84d043942d5eccdc575a53cb2a08a0ba
diff --git a/libs/graphicsenv/GraphicsEnv.cpp b/libs/graphicsenv/GraphicsEnv.cpp
index dfdda0c..9dc7431 100644
--- a/libs/graphicsenv/GraphicsEnv.cpp
+++ b/libs/graphicsenv/GraphicsEnv.cpp
@@ -19,6 +19,7 @@
#include <graphicsenv/GraphicsEnv.h>
#include <dlfcn.h>
+#include <unistd.h>
#include <android-base/file.h>
#include <android-base/properties.h>
@@ -28,7 +29,11 @@
#include <log/log.h>
#include <sys/prctl.h>
+#include <memory>
#include <mutex>
+#include <string>
+
+#include <dlfcn.h>
// TODO(b/37049319) Get this from a header once one exists
extern "C" {
@@ -46,6 +51,20 @@
};
}
+// TODO(ianelliott@): Get the following from an ANGLE header:
+#define CURRENT_ANGLE_API_VERSION 2 // Current API verion we are targetting
+// Version-2 API:
+typedef bool (*fpANGLEGetFeatureSupportUtilAPIVersion)(unsigned int* versionToUse);
+typedef bool (*fpANGLEAndroidParseRulesString)(const char* rulesString, void** rulesHandle,
+ int* rulesVersion);
+typedef bool (*fpANGLEGetSystemInfo)(void** handle);
+typedef bool (*fpANGLEAddDeviceInfoToSystemInfo)(const char* deviceMfr, const char* deviceModel,
+ void* handle);
+typedef bool (*fpANGLEShouldBeUsedForApplication)(void* rulesHandle, int rulesVersion,
+ void* systemInfoHandle, const char* appName);
+typedef bool (*fpANGLEFreeRulesHandle)(void* handle);
+typedef bool (*fpANGLEFreeSystemInfoHandle)(void* handle);
+
namespace android {
enum NativeLibrary {
@@ -134,39 +153,186 @@
mDriverPath = path;
}
+void* GraphicsEnv::loadLibrary(std::string name) {
+ const android_dlextinfo dlextinfo = {
+ .flags = ANDROID_DLEXT_USE_NAMESPACE,
+ .library_namespace = getAngleNamespace(),
+ };
+
+ std::string libName = std::string("lib") + name + "_angle.so";
+
+ void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
+
+ if (so) {
+ ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
+ return so;
+ } else {
+ ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
+ }
+
+ return nullptr;
+}
+
+bool GraphicsEnv::checkAngleRules(void* so) {
+ char manufacturer[PROPERTY_VALUE_MAX];
+ char model[PROPERTY_VALUE_MAX];
+ property_get("ro.product.manufacturer", manufacturer, "UNSET");
+ property_get("ro.product.model", model, "UNSET");
+
+ auto ANGLEGetFeatureSupportUtilAPIVersion =
+ (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
+ "ANGLEGetFeatureSupportUtilAPIVersion");
+
+ if (!ANGLEGetFeatureSupportUtilAPIVersion) {
+ ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
+ return false;
+ }
+
+ // Negotiate the interface version by requesting most recent known to the platform
+ unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
+ if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
+ ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
+ "requested version %u",
+ versionToUse);
+ return false;
+ }
+
+ // Add and remove versions below as needed
+ bool useAngle = false;
+ switch (versionToUse) {
+ case 2: {
+ ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
+ void* rulesHandle = nullptr;
+ int rulesVersion = 0;
+ void* systemInfoHandle = nullptr;
+
+ // Get the symbols for the feature-support-utility library:
+#define GET_SYMBOL(symbol) \
+ fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
+ if (!symbol) { \
+ ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
+ break; \
+ }
+ GET_SYMBOL(ANGLEAndroidParseRulesString);
+ GET_SYMBOL(ANGLEGetSystemInfo);
+ GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
+ GET_SYMBOL(ANGLEShouldBeUsedForApplication);
+ GET_SYMBOL(ANGLEFreeRulesHandle);
+ GET_SYMBOL(ANGLEFreeSystemInfoHandle);
+
+ // Parse the rules, obtain the SystemInfo, and evaluate the
+ // application against the rules:
+ if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
+ ALOGW("ANGLE feature-support library cannot parse rules file");
+ break;
+ }
+ if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
+ ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
+ break;
+ }
+ if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) {
+ ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
+ break;
+ }
+ useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
+ systemInfoHandle, mAngleAppName.c_str());
+ (ANGLEFreeRulesHandle)(rulesHandle);
+ (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
+ } break;
+
+ default:
+ ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
+ }
+
+ ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
+ return useAngle;
+}
+
+bool GraphicsEnv::shouldUseAngle(std::string appName) {
+ if (appName != mAngleAppName) {
+ // Make sure we are checking the app we were init'ed for
+ ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
+ appName.c_str());
+ return false;
+ }
+
+ return shouldUseAngle();
+}
+
+bool GraphicsEnv::shouldUseAngle() {
+ // Make sure we are init'ed
+ if (mAngleAppName.empty()) {
+ ALOGE("App name is empty. setAngleInfo() must be called first to enable ANGLE.");
+ return false;
+ }
+
+ return mUseAngle;
+}
+
+void GraphicsEnv::updateUseAngle() {
+ mUseAngle = false;
+
+ const char* ANGLE_PREFER_ANGLE = "angle";
+ const char* ANGLE_PREFER_NATIVE = "native";
+
+ if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
+ ALOGV("User set \"Developer Options\" to force the use of ANGLE");
+ mUseAngle = true;
+ } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
+ ALOGV("User set \"Developer Options\" to force the use of Native");
+ mUseAngle = false;
+ } else {
+ // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
+ // load ANGLE and call the updatable opt-in/out logic:
+
+ // Check if ANGLE is enabled. Workaround for several bugs:
+ // b/119305693 b/119322355 b/119305887
+ // Something is not working correctly in the feature library
+ char prop[PROPERTY_VALUE_MAX];
+ property_get("debug.angle.enable", prop, "0");
+ void* featureSo = nullptr;
+ if (atoi(prop)) {
+ featureSo = loadLibrary("feature_support");
+ }
+ if (featureSo) {
+ ALOGV("loaded ANGLE's opt-in/out logic from namespace");
+ mUseAngle = checkAngleRules(featureSo);
+ dlclose(featureSo);
+ featureSo = nullptr;
+ } else {
+ ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
+ }
+ }
+}
+
void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
const std::string developerOptIn, const int rulesFd,
const long rulesOffset, const long rulesLength) {
- if (!mAnglePath.empty()) {
- ALOGV("ignoring attempt to change ANGLE path from '%s' to '%s'", mAnglePath.c_str(),
- path.c_str());
- } else {
- ALOGV("setting ANGLE path to '%s'", path.c_str());
- mAnglePath = path;
- }
+ ALOGV("setting ANGLE path to '%s'", path.c_str());
+ mAnglePath = path;
+ ALOGV("setting ANGLE app name to '%s'", appName.c_str());
+ mAngleAppName = appName;
+ ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
+ mAngleDeveloperOptIn = developerOptIn;
- if (!mAngleAppName.empty()) {
- ALOGV("ignoring attempt to change ANGLE app name from '%s' to '%s'", mAngleAppName.c_str(),
- appName.c_str());
- } else {
- ALOGV("setting ANGLE app name to '%s'", appName.c_str());
- mAngleAppName = appName;
+ lseek(rulesFd, rulesOffset, SEEK_SET);
+ mRulesBuffer = std::vector<char>(rulesLength + 1);
+ ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
+ if (numBytesRead < 0) {
+ ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
+ numBytesRead = 0;
+ } else if (numBytesRead == 0) {
+ ALOGW("Empty rules file");
}
-
- if (!mAngleDeveloperOptIn.empty()) {
- ALOGV("ignoring attempt to change ANGLE application opt-in from '%s' to '%s'",
- mAngleDeveloperOptIn.c_str(), developerOptIn.c_str());
- } else {
- ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
- mAngleDeveloperOptIn = developerOptIn;
+ if (numBytesRead != rulesLength) {
+ ALOGW("Did not read all of the necessary bytes from the rules file."
+ "expected: %ld, got: %zd",
+ rulesLength, numBytesRead);
}
+ mRulesBuffer[numBytesRead] = '\0';
- ALOGV("setting ANGLE rules file descriptor to '%i'", rulesFd);
- mAngleRulesFd = rulesFd;
- ALOGV("setting ANGLE rules offset to '%li'", rulesOffset);
- mAngleRulesOffset = rulesOffset;
- ALOGV("setting ANGLE rules length to '%li'", rulesLength);
- mAngleRulesLength = rulesLength;
+ // Update the current status of whether we should use ANGLE or not
+ updateUseAngle();
}
void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
@@ -183,25 +349,8 @@
return mAppNamespace;
}
-const char* GraphicsEnv::getAngleAppName() {
- if (mAngleAppName.empty()) return nullptr;
- return mAngleAppName.c_str();
-}
-
-const char* GraphicsEnv::getAngleDeveloperOptIn() {
- return mAngleDeveloperOptIn.c_str();
-}
-
-int GraphicsEnv::getAngleRulesFd() {
- return mAngleRulesFd;
-}
-
-long GraphicsEnv::getAngleRulesOffset() {
- return mAngleRulesOffset;
-}
-
-long GraphicsEnv::getAngleRulesLength() {
- return mAngleRulesLength;
+std::string& GraphicsEnv::getAngleAppName() {
+ return mAngleAppName;
}
const std::string& GraphicsEnv::getLayerPaths() {