Skip to content

Commit

Permalink
feat(dom): set dom thread priority
Browse files Browse the repository at this point in the history
  • Loading branch information
ilikethese authored and siguangli committed Sep 21, 2023
1 parent 1e22e89 commit 2fbb558
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
25 changes: 24 additions & 1 deletion framework/android/connector/dom/src/main/cpp/src/dom_jni.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<DomManager>();
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<WorkerImpl>(kDomWorkerName, false);
auto callback = std::make_shared<JavaRef>(j_env, j_obj);
worker->BeforeStart([callback]() {
if (callback->GetObj()) SetThreadPriority(callback->GetObj());
});
worker->Start();
auto runner = std::make_shared<TaskRunner>(kDomRunnerName);
runner->SetWorker(worker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import android.view.View;
import androidx.annotation.NonNull;
import android.os.Process;

@SuppressWarnings("JavaJniMissingFunction")
public class DomManager implements Connector {
Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 2 additions & 0 deletions modules/footstone/include/footstone/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class Worker {
}
static bool IsTaskRunning();
bool RunTask();
void BeforeStart(std::function<void()> before_start) { before_start_ = before_start; }
void Start(bool in_new_thread = true);

virtual void SetName(const std::string& name) = 0;
Expand Down Expand Up @@ -108,6 +109,7 @@ class Worker {
void UpdateSpecific(uint32_t task_runner_id, std::array<void *, kWorkerKeysMax> array);

std::thread thread_;
std::function<void()> before_start_;
std::string name_;
std::list<std::vector<std::shared_ptr<TaskRunner>>> running_group_list_;
std::list<std::vector<std::shared_ptr<TaskRunner>>> pending_group_list_;
Expand Down
1 change: 1 addition & 0 deletions modules/footstone/src/worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2fbb558

Please sign in to comment.