Commit b2ce0204 authored by Eric Karl's avatar Eric Karl Committed by Commit Bot

Android OOP-D: Use JavaHandlerThread for Display Compositor

Android OOP-D requires running a Java Choreographer based
BeginFrameSource on the display compositor thread. In order to do this,
this thread must have a Java looper.

This CL adds a looper by converting this thread from a base::Thread
to a base::android::JavaHandlerThread on Android. As we also want to
be able to specify a thread priority for this thread, this patch adds
thread priority support to the JavaHandlerThread class.

Bug: 811967
Cq-Include-Trybots: luci.chromium.try:android_optional_gpu_tests_rel
Change-Id: I12d39edd5925f33629e91ad17eaf575ed49b0296
Reviewed-on: https://chromium-review.googlesource.com/1109020Reviewed-by: default avatarKhushal <khushalsagar@chromium.org>
Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Commit-Queue: Eric Karl <ericrk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#569333}
parent db3a2456
......@@ -27,13 +27,13 @@ public class JavaHandlerThread {
* Construct a java-only instance. Can be connected with native side later.
* Useful for cases where a java thread is needed before native library is loaded.
*/
public JavaHandlerThread(String name) {
mThread = new HandlerThread(name);
public JavaHandlerThread(String name, int priority) {
mThread = new HandlerThread(name, priority);
}
@CalledByNative
private static JavaHandlerThread create(String name) {
return new JavaHandlerThread(name);
private static JavaHandlerThread create(String name, int priority) {
return new JavaHandlerThread(name, priority);
}
public Looper getLooper() {
......
......@@ -10,6 +10,7 @@
#include "base/android/jni_string.h"
#include "base/run_loop.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/platform_thread_internal_posix.h"
#include "base/threading/thread_restrictions.h"
#include "jni/JavaHandlerThread_jni.h"
......@@ -19,10 +20,12 @@ namespace base {
namespace android {
JavaHandlerThread::JavaHandlerThread(const char* name)
JavaHandlerThread::JavaHandlerThread(const char* name,
base::ThreadPriority priority)
: JavaHandlerThread(Java_JavaHandlerThread_create(
AttachCurrentThread(),
ConvertUTF8ToJavaString(AttachCurrentThread(), name))) {}
ConvertUTF8ToJavaString(AttachCurrentThread(), name),
base::internal::ThreadPriorityToNiceValue(priority))) {}
JavaHandlerThread::JavaHandlerThread(
const base::android::ScopedJavaLocalRef<jobject>& obj)
......
......@@ -27,7 +27,9 @@ namespace android {
class BASE_EXPORT JavaHandlerThread {
public:
// Create new thread.
explicit JavaHandlerThread(const char* name);
explicit JavaHandlerThread(
const char* name,
base::ThreadPriority priority = base::ThreadPriority::NORMAL);
// Wrap and connect to an existing JavaHandlerThread.
// |obj| is an instance of JavaHandlerThread.
explicit JavaHandlerThread(
......
......@@ -5,6 +5,7 @@
package org.chromium.base;
import android.os.Handler;
import android.os.Process;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.CalledByNativeUnchecked;
......@@ -32,7 +33,8 @@ class JavaHandlerThreadHelpers {
}
};
JavaHandlerThread thread = new JavaHandlerThread("base_unittests_java");
JavaHandlerThread thread =
new JavaHandlerThread("base_unittests_java", Process.THREAD_PRIORITY_DEFAULT);
thread.maybeStart();
Handler handler = new Handler(thread.getLooper());
......
......@@ -45,8 +45,16 @@
namespace {
std::unique_ptr<base::Thread> CreateAndStartCompositorThread() {
auto thread = std::make_unique<base::Thread>("VizCompositorThread");
std::unique_ptr<viz::CompositorThreadType> CreateAndStartCompositorThread() {
const char* thread_name = "VizCompositorThread";
#if defined(OS_ANDROID)
auto thread = std::make_unique<base::android::JavaHandlerThread>(
thread_name, base::ThreadPriority::DISPLAY);
thread->Start();
return thread;
#else
auto thread = std::make_unique<base::Thread>(thread_name);
base::Thread::Options thread_options;
#if defined(OS_WIN)
// Windows needs a UI message loop for child HWND. Other platforms can use the
......@@ -54,11 +62,12 @@ std::unique_ptr<base::Thread> CreateAndStartCompositorThread() {
thread_options.message_loop_type = base::MessageLoop::TYPE_UI;
#endif
#if defined(OS_ANDROID) || defined(OS_CHROMEOS)
#if defined(OS_CHROMEOS)
thread_options.priority = base::ThreadPriority::DISPLAY;
#endif
CHECK(thread->StartWithOptions(thread_options));
return thread;
#endif
}
std::unique_ptr<base::Thread> CreateAndStartIOThread() {
......
......@@ -8,6 +8,7 @@
#include "base/power_monitor/power_monitor.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "components/discardable_memory/client/client_discardable_shared_memory_manager.h"
#include "gpu/ipc/in_process_command_buffer.h"
#include "gpu/ipc/service/gpu_init.h"
......@@ -17,6 +18,10 @@
#include "services/viz/privileged/interfaces/viz_main.mojom.h"
#include "ui/gfx/font_render_params.h"
#if defined(OS_ANDROID)
#include "base/android/java_handler_thread.h"
#endif
namespace gpu {
class SyncPointManager;
} // namespace gpu
......@@ -35,6 +40,12 @@ class FrameSinkManagerImpl;
class GpuServiceImpl;
class ServerSharedBitmapManager;
#if defined(OS_ANDROID)
using CompositorThreadType = base::android::JavaHandlerThread;
#else
using CompositorThreadType = base::Thread;
#endif
class VizMainImpl : public gpu::GpuSandboxHelper, public mojom::VizMain {
public:
struct LogMessage {
......@@ -164,7 +175,7 @@ class VizMainImpl : public gpu::GpuSandboxHelper, public mojom::VizMain {
const scoped_refptr<base::SingleThreadTaskRunner> gpu_thread_task_runner_;
// The main thread for the display compositor.
std::unique_ptr<base::Thread> compositor_thread_;
std::unique_ptr<CompositorThreadType> compositor_thread_;
scoped_refptr<base::SingleThreadTaskRunner> compositor_thread_task_runner_;
std::unique_ptr<ukm::MojoUkmRecorder> ukm_recorder_;
......
......@@ -6,6 +6,7 @@ package org.chromium.content.browser;
import android.os.Handler;
import android.os.Looper;
import android.os.Process;
import org.chromium.base.JavaHandlerThread;
import org.chromium.base.VisibleForTesting;
......@@ -16,7 +17,7 @@ import org.chromium.base.annotations.JNINamespace;
@JNINamespace("content::android")
public final class LauncherThread {
private static final JavaHandlerThread sThread =
new JavaHandlerThread("Chrome_ProcessLauncherThread");
new JavaHandlerThread("Chrome_ProcessLauncherThread", Process.THREAD_PRIORITY_DEFAULT);
private static final Handler sThreadHandler;
// Can be overritten in tests.
private static Handler sHandler;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment