From 5e21eb3591534febb178572b149f6d5ccf6b1337 Mon Sep 17 00:00:00 2001 From: Yiwei Zhang Date: Wed, 5 Jun 2019 00:26:03 -0700 Subject: GL: unload system driver if needed Both ANGLE and Game Driver originally require the GL driver to be not loaded before the App launches. However, extra memory overhead from loading GL driver in each process requires us to enable gl driver preloading in Zygote again. So this CL adds the logic to unload the system driver if the App chooses to use ANGLE or Game Driver. As long as nobody is using GL api in Zygote to create any context, the unloading at App launch time should be safe. eglGetDisplay will always ask the driver for the display handle after this change, otherwise it will still use the cached display handle even we unload the system driver and load the ANGLE or Game Driver. This means we no longer cache it because eglGetDisplay is rarely called and trivial for the driver. Bug: 134526352 Test: build, flash and boot. Then manually test with ANGLE and Game Driver. Change-Id: I7c068ce9f630347a5d94823bbe6cfbac0f280e91 --- libs/graphicsenv/GraphicsEnv.cpp | 111 ++++++++++++--------- libs/graphicsenv/include/graphicsenv/GraphicsEnv.h | 1 + 2 files changed, 63 insertions(+), 49 deletions(-) (limited to 'libs') diff --git a/libs/graphicsenv/GraphicsEnv.cpp b/libs/graphicsenv/GraphicsEnv.cpp index 407f77d88b..1c5fa52855 100644 --- a/libs/graphicsenv/GraphicsEnv.cpp +++ b/libs/graphicsenv/GraphicsEnv.cpp @@ -534,60 +534,73 @@ void GraphicsEnv::setDebugLayersGLES(const std::string layers) { mDebugLayersGLES = layers; } +// Return true if all the required libraries from vndk and sphal namespace are +// linked to the Game Driver namespace correctly. +bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* vndkNamespace) { + const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK); + if (llndkLibraries.empty()) { + return false; + } + if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) { + ALOGE("Failed to link default namespace[%s]", dlerror()); + return false; + } + + const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP); + if (vndkspLibraries.empty()) { + return false; + } + if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) { + ALOGE("Failed to link vndk namespace[%s]", dlerror()); + return false; + } + + if (mSphalLibraries.empty()) { + return true; + } + + // Make additional libraries in sphal to be accessible + auto sphalNamespace = android_get_exported_namespace("sphal"); + if (!sphalNamespace) { + ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace", + mSphalLibraries.c_str()); + return false; + } + + if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) { + ALOGE("Failed to link sphal namespace[%s]", dlerror()); + return false; + } + + return true; +} + android_namespace_t* GraphicsEnv::getDriverNamespace() { - static std::once_flag once; - std::call_once(once, [this]() { - if (mDriverPath.empty()) return; - - auto vndkNamespace = android_get_exported_namespace("vndk"); - if (!vndkNamespace) return; - - mDriverNamespace = android_create_namespace("gfx driver", - mDriverPath.c_str(), // ld_library_path - mDriverPath.c_str(), // default_library_path - ANDROID_NAMESPACE_TYPE_ISOLATED, - nullptr, // permitted_when_isolated_path - nullptr); - - const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK); - if (llndkLibraries.empty()) { - mDriverNamespace = nullptr; - return; - } - if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) { - ALOGE("Failed to link default namespace[%s]", dlerror()); - mDriverNamespace = nullptr; - return; - } + std::lock_guard lock(mNamespaceMutex); - const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP); - if (vndkspLibraries.empty()) { - mDriverNamespace = nullptr; - return; - } - if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) { - ALOGE("Failed to link vndk namespace[%s]", dlerror()); - mDriverNamespace = nullptr; - return; - } + if (mDriverNamespace) { + return mDriverNamespace; + } - if (mSphalLibraries.empty()) return; + if (mDriverPath.empty()) { + return nullptr; + } - // Make additional libraries in sphal to be accessible - auto sphalNamespace = android_get_exported_namespace("sphal"); - if (!sphalNamespace) { - ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace", - mSphalLibraries.c_str()); - mDriverNamespace = nullptr; - return; - } + auto vndkNamespace = android_get_exported_namespace("vndk"); + if (!vndkNamespace) { + return nullptr; + } - if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) { - ALOGE("Failed to link sphal namespace[%s]", dlerror()); - mDriverNamespace = nullptr; - return; - } - }); + mDriverNamespace = android_create_namespace("gfx driver", + mDriverPath.c_str(), // ld_library_path + mDriverPath.c_str(), // default_library_path + ANDROID_NAMESPACE_TYPE_ISOLATED, + nullptr, // permitted_when_isolated_path + nullptr); + + if (!linkDriverNamespaceLocked(vndkNamespace)) { + mDriverNamespace = nullptr; + } return mDriverNamespace; } diff --git a/libs/graphicsenv/include/graphicsenv/GraphicsEnv.h b/libs/graphicsenv/include/graphicsenv/GraphicsEnv.h index 7d627500fd..f5d19db493 100644 --- a/libs/graphicsenv/include/graphicsenv/GraphicsEnv.h +++ b/libs/graphicsenv/include/graphicsenv/GraphicsEnv.h @@ -129,6 +129,7 @@ private: void* loadLibrary(std::string name); bool checkAngleRules(void* so); void updateUseAngle(); + bool linkDriverNamespaceLocked(android_namespace_t* vndkNamespace); GraphicsEnv() = default; std::string mDriverPath; -- cgit v1.2.3-59-g8ed1b