From fb7e4acb06100fe44ab16b000cdcd1404d5ad9e4 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 13 Feb 2021 12:49:48 -0800 Subject: [PATCH] Android: Ensure shutdown waits for render. We apparently have a case where render is busy during shutdown, based on crash reports. --- UI/NativeApp.cpp | 3 +++ android/jni/app-android.cpp | 26 +++++++++++++++++--------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index 350496e627..f7b7b13f06 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -1060,6 +1060,9 @@ void RenderOverlays(UIContext *dc, void *userdata) { } void NativeRender(GraphicsContext *graphicsContext) { + _assert_(graphicsContext != nullptr); + _assert_(screenManager != nullptr); + g_GameManager.Update(); if (GetUIState() != UISTATE_INGAME) { diff --git a/android/jni/app-android.cpp b/android/jni/app-android.cpp index a5a1724325..10fdf97e57 100644 --- a/android/jni/app-android.cpp +++ b/android/jni/app-android.cpp @@ -174,6 +174,7 @@ static float dp_yscale = 1.0f; static bool renderer_inited = false; static bool sustainedPerfSupported = false; +static std::mutex renderLock; // See NativeQueryConfig("androidJavaGL") to change this value. static bool javaGL = true; @@ -567,6 +568,7 @@ extern "C" void Java_org_ppsspp_ppsspp_NativeApp_init EARLY_LOG("NativeApp.init() -- begin"); PROFILE_INIT(); + std::lock_guard guard(renderLock); renderer_inited = false; androidVersion = jAndroidVersion; deviceType = jdeviceType; @@ -776,9 +778,14 @@ extern "C" void Java_org_ppsspp_ppsspp_NativeApp_shutdown(JNIEnv *, jclass) { INFO_LOG(G3D, "Not shutting down renderer - not initialized"); } - inputBoxCallbacks.clear(); - NativeShutdown(); - VFSShutdown(); + { + std::lock_guard guard(renderLock); + inputBoxCallbacks.clear(); + NativeShutdown(); + VFSShutdown(); + } + + std::lock_guard guard(frameCommandLock); while (frameCommands.size()) frameCommands.pop(); INFO_LOG(SYSTEM, "NativeApp.shutdown() -- end"); @@ -929,10 +936,14 @@ extern "C" void JNICALL Java_org_ppsspp_ppsspp_NativeApp_sendInputBox(JNIEnv *en NativeInputBoxReceived(entry->second, result, value); } -void UpdateRunLoopAndroid(JNIEnv *env) { +void LockedNativeUpdateRender() { + std::lock_guard renderGuard(renderLock); NativeUpdate(); - NativeRender(graphicsContext); +} + +void UpdateRunLoopAndroid(JNIEnv *env) { + LockedNativeUpdateRender(); std::lock_guard guard(frameCommandLock); if (!nativeActivity) { @@ -1352,10 +1363,7 @@ extern "C" bool JNICALL Java_org_ppsspp_ppsspp_NativeActivity_runEGLRenderLoop(J } } else { while (!exitRenderLoop) { - NativeUpdate(); - - NativeRender(graphicsContext); - + LockedNativeUpdateRender(); graphicsContext->SwapBuffers(); ProcessFrameCommands(env);