OSDN Git Service

Switch RenderThread to a Java daemon thread
authorJohn Reck <jreck@google.com>
Sat, 2 Dec 2017 00:18:53 +0000 (16:18 -0800)
committerJohn Reck <jreck@google.com>
Sat, 2 Dec 2017 00:35:06 +0000 (16:35 -0800)
Bug: 69962494
Test: device boots, hwui unit tests pass
Change-Id: I9d5f2fccebf845328914c82aa97285cf605a4354

core/jni/android_view_ThreadedRenderer.cpp
libs/hwui/renderthread/RenderThread.cpp
libs/hwui/renderthread/RenderThread.h
libs/hwui/thread/ThreadBase.h

index 870a0c2..519a885 100644 (file)
@@ -1043,7 +1043,22 @@ static const JNINativeMethod gMethods[] = {
     { "nSetHighContrastText", "(Z)V", (void*)android_view_ThreadedRenderer_setHighContrastText },
 };
 
+static JavaVM* mJvm = nullptr;
+
+static void attachRenderThreadToJvm() {
+    LOG_ALWAYS_FATAL_IF(!mJvm, "No jvm but we set the hook??");
+
+    JavaVMAttachArgs args;
+    args.version = JNI_VERSION_1_4;
+    args.name = (char*) "RenderThread";
+    args.group = NULL;
+    JNIEnv* env;
+    mJvm->AttachCurrentThreadAsDaemon(&env, (void*) &args);
+}
+
 int register_android_view_ThreadedRenderer(JNIEnv* env) {
+    env->GetJavaVM(&mJvm);
+    RenderThread::setOnStartHook(&attachRenderThreadToJvm);
     jclass observerClass = FindClassOrDie(env, "android/view/FrameMetricsObserver");
     gFrameMetricsObserverClassInfo.frameMetrics = GetFieldIDOrDie(
             env, observerClass, "mFrameMetrics", "Landroid/view/FrameMetrics;");
index 05a9b75..20443ec 100644 (file)
@@ -51,10 +51,17 @@ static const nsecs_t DISPATCH_FRAME_CALLBACKS_DELAY = milliseconds_to_nanosecond
 
 static bool gHasRenderThreadInstance = false;
 
+static void (*gOnStartHook)() = nullptr;
+
 bool RenderThread::hasInstance() {
     return gHasRenderThreadInstance;
 }
 
+void RenderThread::setOnStartHook(void (*onStartHook)()) {
+    LOG_ALWAYS_FATAL_IF(hasInstance(), "can't set an onStartHook after we've started...");
+    gOnStartHook = onStartHook;
+}
+
 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
@@ -256,6 +263,9 @@ void RenderThread::requestVsync() {
 
 bool RenderThread::threadLoop() {
     setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
+    if (gOnStartHook) {
+        gOnStartHook();
+    }
     initThreadLocals();
 
     while (true) {
index d17a509..970537b 100644 (file)
@@ -67,6 +67,9 @@ class RenderThread : private ThreadBase {
     PREVENT_COPY_AND_ASSIGN(RenderThread);
 
 public:
+    // Sets a callback that fires before any RenderThread setup has occured.
+    ANDROID_API static void setOnStartHook(void (*onStartHook)());
+
     WorkQueue& queue() { return ThreadBase::queue(); }
 
     // Mimics android.view.Choreographer
index b3fec1f..8068121 100644 (file)
@@ -31,7 +31,10 @@ class ThreadBase : protected Thread {
     PREVENT_COPY_AND_ASSIGN(ThreadBase);
 
 public:
-    ThreadBase() : mLooper(new Looper(false)), mQueue([this]() { mLooper->wake(); }, mLock) {}
+    ThreadBase()
+            : Thread(false)
+            , mLooper(new Looper(false))
+            , mQueue([this]() { mLooper->wake(); }, mLock) {}
 
     WorkQueue& queue() { return mQueue; }