Android: Use Java thread for emulation thread

This commit is contained in:
Connor McLaughlin
2021-01-11 01:03:17 +10:00
parent a5f9aa11e1
commit 5996945b37
5 changed files with 145 additions and 87 deletions

View File

@ -265,39 +265,20 @@ bool AndroidHostInterface::IsEmulationThreadPaused() const
return System::IsValid() && System::IsPaused();
}
bool AndroidHostInterface::StartEmulationThread(jobject emulation_activity, ANativeWindow* initial_surface,
SystemBootParameters boot_params, bool resume_state)
{
Assert(!IsEmulationThreadRunning());
emulation_activity = AndroidHelpers::GetJNIEnv()->NewGlobalRef(emulation_activity);
Log_DevPrintf("Starting emulation thread...");
m_emulation_thread_stop_request.store(false);
m_emulation_thread = std::thread(&AndroidHostInterface::EmulationThreadEntryPoint, this, emulation_activity,
initial_surface, std::move(boot_params), resume_state);
return true;
}
void AndroidHostInterface::PauseEmulationThread(bool paused)
{
Assert(IsEmulationThreadRunning());
RunOnEmulationThread([this, paused]() { PauseSystem(paused); });
}
void AndroidHostInterface::StopEmulationThread()
void AndroidHostInterface::StopEmulationThreadLoop()
{
if (!IsEmulationThreadRunning())
return;
Log_InfoPrint("Stopping emulation thread...");
{
std::unique_lock<std::mutex> lock(m_mutex);
m_emulation_thread_stop_request.store(true);
m_sleep_cv.notify_one();
}
m_emulation_thread.join();
Log_InfoPrint("Emulation thread stopped");
std::unique_lock<std::mutex> lock(m_mutex);
m_emulation_thread_stop_request.store(true);
m_sleep_cv.notify_one();
}
void AndroidHostInterface::RunOnEmulationThread(std::function<void()> function, bool blocking)
@ -329,18 +310,19 @@ void AndroidHostInterface::RunOnEmulationThread(std::function<void()> function,
m_mutex.unlock();
}
void AndroidHostInterface::EmulationThreadEntryPoint(jobject emulation_activity, ANativeWindow* initial_surface,
void AndroidHostInterface::EmulationThreadEntryPoint(JNIEnv* env, jobject emulation_activity, jobject surface,
SystemBootParameters boot_params, bool resume_state)
{
JNIEnv* thread_env;
if (s_jvm->AttachCurrentThread(&thread_env, nullptr) != JNI_OK)
ANativeWindow* native_surface = ANativeWindow_fromSurface(env, surface);
if (!native_surface)
{
ReportError("Failed to attach JNI to thread");
Log_ErrorPrint("ANativeWindow_fromSurface() returned null");
env->CallVoidMethod(m_emulation_activity_object, s_EmulationActivity_method_onEmulationStopped);
return;
}
CreateImGuiContext();
m_surface = initial_surface;
m_surface = native_surface;
m_emulation_activity_object = emulation_activity;
ApplySettings(true);
@ -358,35 +340,36 @@ void AndroidHostInterface::EmulationThreadEntryPoint(jobject emulation_activity,
boot_result = BootSystem(boot_params);
}
if (!boot_result)
if (boot_result)
{
// System is ready to go.
EmulationThreadLoop(env);
if (g_settings.save_state_on_exit)
SaveResumeSaveState();
PowerOffSystem();
}
else
{
ReportFormattedError("Failed to boot system on emulation thread (file:%s).", boot_params.filename.c_str());
DestroyImGuiContext();
thread_env->CallVoidMethod(m_emulation_activity_object, s_EmulationActivity_method_onEmulationStopped);
thread_env->DeleteGlobalRef(m_emulation_activity_object);
m_emulation_activity_object = {};
s_jvm->DetachCurrentThread();
return;
}
// System is ready to go.
thread_env->CallVoidMethod(m_emulation_activity_object, s_EmulationActivity_method_onEmulationStarted);
EmulationThreadLoop();
env->CallVoidMethod(m_emulation_activity_object, s_EmulationActivity_method_onEmulationStopped);
thread_env->CallVoidMethod(m_emulation_activity_object, s_EmulationActivity_method_onEmulationStopped);
if (g_settings.save_state_on_exit)
SaveResumeSaveState();
PowerOffSystem();
DestroyImGuiContext();
thread_env->DeleteGlobalRef(m_emulation_activity_object);
m_emulation_activity_object = {};
s_jvm->DetachCurrentThread();
}
void AndroidHostInterface::EmulationThreadLoop()
void AndroidHostInterface::EmulationThreadLoop(JNIEnv* env)
{
{
std::unique_lock<std::mutex> lock(m_mutex);
m_emulation_thread_running.store(true);
}
env->CallVoidMethod(m_emulation_activity_object, s_EmulationActivity_method_onEmulationStarted);
for (;;)
{
// run any events
@ -408,7 +391,11 @@ void AndroidHostInterface::EmulationThreadLoop()
}
if (m_emulation_thread_stop_request.load())
{
m_emulation_thread_running.store(false);
m_emulation_thread_stop_request.store(false);
return;
}
if (System::IsPaused())
{
@ -475,11 +462,12 @@ bool AndroidHostInterface::AcquireHostDisplay()
!display->InitializeRenderDevice(GetShaderCacheBasePath(), g_settings.gpu_use_debug_device,
g_settings.gpu_threaded_presentation))
{
ReportError("Failed to acquire host display.");
display->DestroyRenderDevice();
return false;
}
// The alignement was set prior to booting.
display->SetDisplayAlignment(m_display_alignment);
m_display = std::move(display);
if (!CreateHostDisplayResources())
@ -591,6 +579,13 @@ void AndroidHostInterface::SurfaceChanged(ANativeWindow* surface, int format, in
}
}
void AndroidHostInterface::SetDisplayAlignment(HostDisplay::Alignment alignment)
{
m_display_alignment = alignment;
if (m_display)
m_display->SetDisplayAlignment(alignment);
}
void AndroidHostInterface::CreateImGuiContext()
{
ImGui::CreateContext();
@ -956,28 +951,21 @@ DEFINE_JNI_ARGS_METHOD(jboolean, AndroidHostInterface_isEmulationThreadRunning,
return AndroidHelpers::GetNativeClass(env, obj)->IsEmulationThreadRunning();
}
DEFINE_JNI_ARGS_METHOD(jboolean, AndroidHostInterface_startEmulationThread, jobject obj, jobject emulationActivity,
DEFINE_JNI_ARGS_METHOD(void, AndroidHostInterface_runEmulationThread, jobject obj, jobject emulationActivity,
jobject surface, jstring filename, jboolean resume_state, jstring state_filename)
{
ANativeWindow* native_surface = ANativeWindow_fromSurface(env, surface);
if (!native_surface)
{
Log_ErrorPrint("ANativeWindow_fromSurface() returned null");
return false;
}
std::string state_filename_str = AndroidHelpers::JStringToString(env, state_filename);
SystemBootParameters boot_params;
boot_params.filename = AndroidHelpers::JStringToString(env, filename);
return AndroidHelpers::GetNativeClass(env, obj)->StartEmulationThread(emulationActivity, native_surface,
std::move(boot_params), resume_state);
AndroidHelpers::GetNativeClass(env, obj)->EmulationThreadEntryPoint(env, emulationActivity, surface,
std::move(boot_params), resume_state);
}
DEFINE_JNI_ARGS_METHOD(void, AndroidHostInterface_stopEmulationThread, jobject obj)
DEFINE_JNI_ARGS_METHOD(void, AndroidHostInterface_stopEmulationThreadLoop, jobject obj)
{
AndroidHelpers::GetNativeClass(env, obj)->StopEmulationThread();
AndroidHelpers::GetNativeClass(env, obj)->StopEmulationThreadLoop();
}
DEFINE_JNI_ARGS_METHOD(void, AndroidHostInterface_surfaceChanged, jobject obj, jobject surface, jint format, jint width,
@ -1310,7 +1298,7 @@ DEFINE_JNI_ARGS_METHOD(void, AndroidHostInterface_setDisplayAlignment, jobject o
{
AndroidHostInterface* hi = AndroidHelpers::GetNativeClass(env, obj);
hi->RunOnEmulationThread(
[hi, alignment]() { hi->GetDisplay()->SetDisplayAlignment(static_cast<HostDisplay::Alignment>(alignment)); });
[hi, alignment]() { hi->SetDisplayAlignment(static_cast<HostDisplay::Alignment>(alignment)); }, false);
}
DEFINE_JNI_ARGS_METHOD(bool, AndroidHostInterface_hasSurface, jobject obj)

View File

@ -3,6 +3,7 @@
#include "common/byte_stream.h"
#include "common/event.h"
#include "common/progress_callback.h"
#include "core/host_display.h"
#include "frontend-common/common_host_interface.h"
#include <array>
#include <atomic>
@ -38,15 +39,17 @@ public:
float GetFloatSettingValue(const char* section, const char* key, float default_value = 0.0f) override;
std::unique_ptr<ByteStream> OpenPackageFile(const char* path, u32 flags) override;
bool IsEmulationThreadRunning() const { return m_emulation_thread.joinable(); }
bool IsEmulationThreadRunning() const { return m_emulation_thread_running.load(); }
bool IsEmulationThreadPaused() const;
bool StartEmulationThread(jobject emulation_activity, ANativeWindow* initial_surface,
SystemBootParameters boot_params, bool resume_state);
void RunOnEmulationThread(std::function<void()> function, bool blocking = false);
void PauseEmulationThread(bool paused);
void StopEmulationThread();
void StopEmulationThreadLoop();
void EmulationThreadEntryPoint(JNIEnv* env, jobject emulation_activity, jobject initial_surface,
SystemBootParameters boot_params, bool resume_state);
void SurfaceChanged(ANativeWindow* surface, int format, int width, int height);
void SetDisplayAlignment(HostDisplay::Alignment alignment);
void SetControllerType(u32 index, std::string_view type_name);
void SetControllerButtonState(u32 index, s32 button_code, bool pressed);
@ -79,9 +82,7 @@ protected:
void OnRunningGameChanged() override;
private:
void EmulationThreadEntryPoint(jobject emulation_activity, ANativeWindow* initial_surface,
SystemBootParameters boot_params, bool resume_state);
void EmulationThreadLoop();
void EmulationThreadLoop(JNIEnv* env);
void CreateImGuiContext();
void DestroyImGuiContext();
@ -102,8 +103,10 @@ private:
std::deque<std::function<void()>> m_callback_queue;
std::atomic_bool m_callbacks_outstanding{false};
std::thread m_emulation_thread;
std::atomic_bool m_emulation_thread_stop_request{false};
std::atomic_bool m_emulation_thread_running{false};
HostDisplay::Alignment m_display_alignment = HostDisplay::Alignment::Center;
u64 m_last_vibration_update_time = 0;
bool m_last_vibration_state = false;
@ -111,11 +114,13 @@ private:
};
namespace AndroidHelpers {
JNIEnv* GetJNIEnv();
AndroidHostInterface* GetNativeClass(JNIEnv* env, jobject obj);
std::string JStringToString(JNIEnv* env, jstring str);
std::unique_ptr<GrowableMemoryByteStream> ReadInputStreamToMemory(JNIEnv* env, jobject obj, u32 chunk_size = 65536);
jclass GetStringClass();
} // namespace AndroidHelpers
template<typename T>