From 80dbc35555d21f65fcfe4ede5d7b406cc4190a97 Mon Sep 17 00:00:00 2001 From: Stan Iliev Date: Tue, 5 Feb 2019 15:31:28 -0500 Subject: Tell JVM to not wait for HWUI worker threads upon shutdown RenderThread is setup as a daemon thread, which allows JVM to exit without waiting on it. This CL does same setup for HWUI worker threads, which offload work from the RenderThread. This fixes an issue exposed by Vulkan pipeline, which is pushing different loads to the worker threads and causing some java tests to hang on exit. This is not a Vulkan specific issue, because GL also hangs if worker thread is started. Bug: 123374538 Test: Ran DismissDialogsInstrumentation test Change-Id: Ie4ee94737ced975323a0792f57f8426c958e8056 --- core/jni/android_view_ThreadedRenderer.cpp | 2 +- libs/hwui/renderthread/RenderThread.cpp | 8 ++++++-- libs/hwui/renderthread/RenderThread.h | 5 ++++- libs/hwui/thread/TaskManager.cpp | 6 ++++++ libs/hwui/thread/TaskManager.h | 3 ++- 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/core/jni/android_view_ThreadedRenderer.cpp b/core/jni/android_view_ThreadedRenderer.cpp index 40529191a42c..2c058cd0d6fd 100644 --- a/core/jni/android_view_ThreadedRenderer.cpp +++ b/core/jni/android_view_ThreadedRenderer.cpp @@ -1207,7 +1207,7 @@ static void attachRenderThreadToJvm() { JavaVMAttachArgs args; args.version = JNI_VERSION_1_4; - args.name = (char*) "RenderThread"; + args.name = NULL; args.group = NULL; JNIEnv* env; mJvm->AttachCurrentThreadAsDaemon(&env, (void*) &args); diff --git a/libs/hwui/renderthread/RenderThread.cpp b/libs/hwui/renderthread/RenderThread.cpp index 8bef35915c4d..caad80651395 100644 --- a/libs/hwui/renderthread/RenderThread.cpp +++ b/libs/hwui/renderthread/RenderThread.cpp @@ -56,7 +56,7 @@ static const nsecs_t DISPATCH_FRAME_CALLBACKS_DELAY = milliseconds_to_nanosecond static bool gHasRenderThreadInstance = false; -static void (*gOnStartHook)() = nullptr; +static JVMAttachHook gOnStartHook = nullptr; class DisplayEventReceiverWrapper : public VsyncSource { public: @@ -111,11 +111,15 @@ bool RenderThread::hasInstance() { return gHasRenderThreadInstance; } -void RenderThread::setOnStartHook(void (*onStartHook)()) { +void RenderThread::setOnStartHook(JVMAttachHook onStartHook) { LOG_ALWAYS_FATAL_IF(hasInstance(), "can't set an onStartHook after we've started..."); gOnStartHook = onStartHook; } +JVMAttachHook RenderThread::getOnStartHook() { + return gOnStartHook; +} + RenderThread& RenderThread::getInstance() { // This is a pointer because otherwise __cxa_finalize // will try to delete it like a Good Citizen but that causes us to crash diff --git a/libs/hwui/renderthread/RenderThread.h b/libs/hwui/renderthread/RenderThread.h index 1ef83fb26c14..d062dba288fc 100644 --- a/libs/hwui/renderthread/RenderThread.h +++ b/libs/hwui/renderthread/RenderThread.h @@ -75,12 +75,15 @@ struct VsyncSource { class DummyVsyncSource; +typedef void (*JVMAttachHook)(); + class RenderThread : private ThreadBase { PREVENT_COPY_AND_ASSIGN(RenderThread); public: // Sets a callback that fires before any RenderThread setup has occurred. - ANDROID_API static void setOnStartHook(void (*onStartHook)()); + ANDROID_API static void setOnStartHook(JVMAttachHook onStartHook); + static JVMAttachHook getOnStartHook(); WorkQueue& queue() { return ThreadBase::queue(); } diff --git a/libs/hwui/thread/TaskManager.cpp b/libs/hwui/thread/TaskManager.cpp index 26ff6ebad3b4..6493d495716e 100644 --- a/libs/hwui/thread/TaskManager.cpp +++ b/libs/hwui/thread/TaskManager.cpp @@ -21,6 +21,7 @@ #include "TaskManager.h" #include "TaskProcessor.h" #include "utils/MathUtils.h" +#include "renderthread/RenderThread.h" namespace android { namespace uirenderer { @@ -84,6 +85,11 @@ bool TaskManager::addTaskBase(const sp& task, const sp