[Cherry-pick] Avoid unloading ANGLE.

Previously when ANGLE is the default GLES driver and preloaded, by
setting an application to use ANGLE when ANGLE apk doesn't present, the
system ANGLE should be used. However, the loader will unload the default
driver and load ANGLE. And hence when ANGLE is the default GLES driver,
it will be unloaded and then reloaded. This patch makes sure the loader
skips unloading and immediately return in this case.

Minor: Only unload the drivers when there are preloaded drivers.

Bug: b/283858001
Test: verified with camera
Test: verified by forcing GLES driver preloading
Change-Id: I82b6408405ef7c507e50ab259204bdce95fda110
Merged-In: I82b6408405ef7c507e50ab259204bdce95fda110
diff --git a/libs/graphicsenv/GraphicsEnv.cpp b/libs/graphicsenv/GraphicsEnv.cpp
index 2c274be..5bdffe0 100644
--- a/libs/graphicsenv/GraphicsEnv.cpp
+++ b/libs/graphicsenv/GraphicsEnv.cpp
@@ -420,7 +420,7 @@
     return mShouldUseAngle;
 }
 
-void GraphicsEnv::setAngleInfo(const std::string& path, const bool useSystemAngle,
+void GraphicsEnv::setAngleInfo(const std::string& path, const bool shouldUseSystemAngle,
                                const std::string& packageName,
                                const std::vector<std::string> eglFeatures) {
     if (mShouldUseAngle) {
@@ -438,7 +438,7 @@
     ALOGV("setting app package name to '%s'", packageName.c_str());
     mPackageName = std::move(packageName);
     mShouldUseAngle = true;
-    mUseSystemAngle = useSystemAngle;
+    mShouldUseSystemAngle = shouldUseSystemAngle;
 }
 
 void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace,
@@ -589,7 +589,7 @@
         return mAngleNamespace;
     }
 
-    if (mAnglePath.empty() && !mUseSystemAngle) {
+    if (mAnglePath.empty() && !mShouldUseSystemAngle) {
         ALOGV("mAnglePath is empty and not using system ANGLE, abort creating ANGLE namespace");
         return nullptr;
     }
@@ -607,18 +607,19 @@
     // are properly inherited.
     mAngleNamespace =
             android_create_namespace("ANGLE",
-                                     mUseSystemAngle ? defaultLibraryPaths
-                                                     : mAnglePath.c_str(), // ld_library_path
-                                     mUseSystemAngle ? defaultLibraryPaths
-                                                     : mAnglePath.c_str(), // default_library_path
+                                     mShouldUseSystemAngle ? defaultLibraryPaths
+                                                           : mAnglePath.c_str(), // ld_library_path
+                                     mShouldUseSystemAngle
+                                             ? defaultLibraryPaths
+                                             : mAnglePath.c_str(), // default_library_path
                                      ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED,
                                      nullptr, // permitted_when_isolated_path
-                                     mUseSystemAngle ? android_get_exported_namespace("sphal")
-                                                     : nullptr); // parent
+                                     mShouldUseSystemAngle ? android_get_exported_namespace("sphal")
+                                                           : nullptr); // parent
 
     ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
 
-    if (!mUseSystemAngle) {
+    if (!mShouldUseSystemAngle) {
         return mAngleNamespace;
     }
 
@@ -643,4 +644,8 @@
     gpuService->toggleAngleAsSystemDriver(enabled);
 }
 
+bool GraphicsEnv::shouldUseSystemAngle() {
+    return mShouldUseSystemAngle;
+}
+
 } // namespace android
diff --git a/libs/graphicsenv/include/graphicsenv/GraphicsEnv.h b/libs/graphicsenv/include/graphicsenv/GraphicsEnv.h
index e78d038..fbf2902 100644
--- a/libs/graphicsenv/include/graphicsenv/GraphicsEnv.h
+++ b/libs/graphicsenv/include/graphicsenv/GraphicsEnv.h
@@ -117,6 +117,7 @@
     const std::vector<std::string>& getAngleEglFeatures();
     // Set the persist.graphics.egl system property value.
     void nativeToggleAngleAsSystemDriver(bool enabled);
+    bool shouldUseSystemAngle();
 
     /*
      * Apis for debug layer
@@ -173,7 +174,7 @@
     // Whether ANGLE should be used.
     bool mShouldUseAngle = false;
     // Whether loader should load system ANGLE.
-    bool mUseSystemAngle = false;
+    bool mShouldUseSystemAngle = false;
     // ANGLE namespace.
     android_namespace_t* mAngleNamespace = nullptr;
 
diff --git a/opengl/libs/EGL/Loader.cpp b/opengl/libs/EGL/Loader.cpp
index b4fc5f0..8d0eb59 100644
--- a/opengl/libs/EGL/Loader.cpp
+++ b/opengl/libs/EGL/Loader.cpp
@@ -161,7 +161,12 @@
     // Return true if ANGLE namespace is set.
     android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
     if (ns) {
-        return true;
+        // Unless the default GLES driver is ANGLE and the process should use system ANGLE, since
+        // the intended GLES driver is already loaded.
+        // This should be updated in a later patch that cleans up namespaces
+        if (!(cnx->angleLoaded && android::GraphicsEnv::getInstance().shouldUseSystemAngle())) {
+            return true;
+        }
     }
 
     // Return true if updated driver namespace is set.
@@ -206,17 +211,17 @@
             do_android_unload_sphal_library(hnd->dso[0]);
         }
         cnx->dso = nullptr;
+        cnx->angleLoaded = false;
     }
 
     cnx->systemDriverUnloaded = true;
 }
 
-void* Loader::open(egl_connection_t* cnx)
-{
+void* Loader::open(egl_connection_t* cnx) {
     ATRACE_CALL();
     const nsecs_t openTime = systemTime();
 
-    if (should_unload_system_driver(cnx)) {
+    if (cnx->dso && should_unload_system_driver(cnx)) {
         unload_system_driver(cnx);
     }
 
@@ -225,8 +230,12 @@
         return cnx->dso;
     }
 
-    // Firstly, try to load ANGLE driver.
-    driver_t* hnd = attempt_to_load_angle(cnx);
+    driver_t* hnd = nullptr;
+    // Firstly, try to load ANGLE driver, if ANGLE should be loaded and fail, abort.
+    if (android::GraphicsEnv::getInstance().shouldUseAngle()) {
+        hnd = attempt_to_load_angle(cnx);
+        LOG_ALWAYS_FATAL_IF(!hnd, "Failed to load ANGLE.");
+    }
 
     if (!hnd) {
         // Secondly, try to load from driver apk.
@@ -541,10 +550,6 @@
 Loader::driver_t* Loader::attempt_to_load_angle(egl_connection_t* cnx) {
     ATRACE_CALL();
 
-    if (!android::GraphicsEnv::getInstance().shouldUseAngle()) {
-        return nullptr;
-    }
-
     android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
     if (!ns) {
         return nullptr;