From 2fbb55852397072b85924d4cc20f5067d25f5244 Mon Sep 17 00:00:00 2001 From: ilikethese Date: Wed, 20 Sep 2023 20:45:30 +0800 Subject: [PATCH] feat(dom): set dom thread priority --- .../connector/dom/src/main/cpp/src/dom_jni.cc | 25 ++++++++++++++++++- .../com/openhippy/connector/DomManager.java | 6 +++++ modules/footstone/include/footstone/worker.h | 2 ++ modules/footstone/src/worker.cc | 1 + 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/framework/android/connector/dom/src/main/cpp/src/dom_jni.cc b/framework/android/connector/dom/src/main/cpp/src/dom_jni.cc index 6f35f738053..924ebf27aa1 100644 --- a/framework/android/connector/dom/src/main/cpp/src/dom_jni.cc +++ b/framework/android/connector/dom/src/main/cpp/src/dom_jni.cc @@ -31,6 +31,8 @@ #include "footstone/worker_impl.h" #include "jni/jni_register.h" #include "jni/data_holder.h" +#include "jni/jni_env.h" +#include "jni/scoped_java_ref.h" namespace hippy { inline namespace framework { @@ -130,11 +132,32 @@ void SetDomManager(JNIEnv* j_env, root_node->SetDomManager(dom_manager_object); } -jint CreateDomManager(__unused JNIEnv* j_env, __unused jobject j_obj) { +static void SetThreadPriority(jobject j_object) { + auto j_env = JNIEnvironment::GetInstance()->AttachCurrentThread(); + auto j_class = j_env->GetObjectClass(j_object); + if (!j_class) { + FOOTSTONE_LOG(ERROR) << "CallJavaMethod j_class error"; + return; + } + auto j_cb_id = j_env->GetMethodID(j_class, "setThreadPrority", "()V"); + if (!j_cb_id) { + FOOTSTONE_LOG(ERROR) << "CallJavaMethod j_cb_id error"; + return; + } + j_env->CallVoidMethod(j_object, j_cb_id); + JNIEnvironment::ClearJEnvException(j_env); + j_env->DeleteLocalRef(j_class); +} + +jint CreateDomManager(JNIEnv* j_env, jobject j_obj) { auto dom_manager = std::make_shared(); auto dom_id = hippy::global_data_holder_key.fetch_add(1); hippy::global_data_holder.Insert(dom_id, dom_manager); auto worker = std::make_shared(kDomWorkerName, false); + auto callback = std::make_shared(j_env, j_obj); + worker->BeforeStart([callback]() { + if (callback->GetObj()) SetThreadPriority(callback->GetObj()); + }); worker->Start(); auto runner = std::make_shared(kDomRunnerName); runner->SetWorker(worker); diff --git a/framework/android/connector/dom/src/main/java/com/openhippy/connector/DomManager.java b/framework/android/connector/dom/src/main/java/com/openhippy/connector/DomManager.java index d305a2669d3..76b3f52cdfd 100644 --- a/framework/android/connector/dom/src/main/java/com/openhippy/connector/DomManager.java +++ b/framework/android/connector/dom/src/main/java/com/openhippy/connector/DomManager.java @@ -18,6 +18,7 @@ import android.view.View; import androidx.annotation.NonNull; +import android.os.Process; @SuppressWarnings("JavaJniMissingFunction") public class DomManager implements Connector { @@ -56,6 +57,11 @@ public void attachToRoot(View root) { setDomManager(root.getId(), mInstanceId); } + public void setThreadPrority() { + int tid = Process.myTid(); + Process.setThreadPriority(tid, Thread.MAX_PRIORITY); + } + /** * Attach to renderer with specified id. * diff --git a/modules/footstone/include/footstone/worker.h b/modules/footstone/include/footstone/worker.h index b2b80a7c995..ff560310498 100644 --- a/modules/footstone/include/footstone/worker.h +++ b/modules/footstone/include/footstone/worker.h @@ -77,6 +77,7 @@ class Worker { } static bool IsTaskRunning(); bool RunTask(); + void BeforeStart(std::function before_start) { before_start_ = before_start; } void Start(bool in_new_thread = true); virtual void SetName(const std::string& name) = 0; @@ -108,6 +109,7 @@ class Worker { void UpdateSpecific(uint32_t task_runner_id, std::array array); std::thread thread_; + std::function before_start_; std::string name_; std::list>> running_group_list_; std::list>> pending_group_list_; diff --git a/modules/footstone/src/worker.cc b/modules/footstone/src/worker.cc index 02a81499f13..5628dd72157 100644 --- a/modules/footstone/src/worker.cc +++ b/modules/footstone/src/worker.cc @@ -128,6 +128,7 @@ void Worker::Start(bool in_new_thread) { if (in_new_thread) { thread_ = std::thread([this]() -> void { SetName(name_); + if (before_start_) before_start_(); driver_->Start(); }); } else {