From 0ba32f7add97daf1c5ea52cf77b75e81d862676f Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sat, 26 Apr 2025 10:33:01 +0200 Subject: Android: Detect when native code should flush unsaved data --- .../java/org/dolphinemu/dolphinemu/utils/ActivityTracker.kt | 11 +++++++++++ Source/Android/jni/ActivityTracker.cpp | 9 +++++++++ 2 files changed, 20 insertions(+) (limited to 'Source/Android') diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/ActivityTracker.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/ActivityTracker.kt index 8bcd1bdecb..62de1f4161 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/ActivityTracker.kt +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/ActivityTracker.kt @@ -56,10 +56,21 @@ class ActivityTracker : ActivityLifecycleCallbacks { override fun onActivitySaveInstanceState(activity: Activity, bundle: Bundle) {} + override fun onActivityPostSaveInstanceState(activity: Activity, bundle: Bundle) { + if (DirectoryInitialization.areDolphinDirectoriesReady() && + !activity.isChangingConfigurations + ) { + flushUnsavedData() + } + } + override fun onActivityDestroyed(activity: Activity) {} companion object { @JvmStatic external fun setBackgroundExecutionAllowedNative(allowed: Boolean) + + @JvmStatic + external fun flushUnsavedData() } } diff --git a/Source/Android/jni/ActivityTracker.cpp b/Source/Android/jni/ActivityTracker.cpp index b871890576..20fc7df79a 100644 --- a/Source/Android/jni/ActivityTracker.cpp +++ b/Source/Android/jni/ActivityTracker.cpp @@ -5,6 +5,8 @@ #include "Common/Logging/Log.h" #include "Core/AchievementManager.h" +#include "UICommon/UICommon.h" +#include "jni/Host.h" extern "C" { @@ -18,4 +20,11 @@ Java_org_dolphinemu_dolphinemu_utils_ActivityTracker_setBackgroundExecutionAllow INFO_LOG_FMT(CORE, "SetBackgroundExecutionAllowed {}", allowed); AchievementManager::GetInstance().SetBackgroundExecutionAllowed(allowed); } + +JNIEXPORT void JNICALL +Java_org_dolphinemu_dolphinemu_utils_ActivityTracker_flushUnsavedData(JNIEnv*, jclass) +{ + HostThreadLock guard; + UICommon::FlushUnsavedData(); +} } -- cgit v1.2.3 From 2322437f96bc53f1edcd9100daedb5b54c6e0fc4 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sun, 2 Nov 2025 22:21:15 -0600 Subject: State: Simplify interthread communication and cleanups. Save/Load calls are now always non-blocking for the caller, but appropriately block the CPU thread as needed. --- .../main/java/org/dolphinemu/dolphinemu/NativeLibrary.kt | 6 ++---- .../dolphinemu/dolphinemu/activities/EmulationActivity.kt | 14 +++++++------- .../dolphinemu/dolphinemu/fragments/EmulationFragment.kt | 2 +- Source/Android/jni/MainAndroid.cpp | 10 ++++------ 4 files changed, 14 insertions(+), 18 deletions(-) (limited to 'Source/Android') diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.kt index 0b6490b290..0edc0f91f9 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.kt +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.kt @@ -233,19 +233,17 @@ object NativeLibrary { * Saves a game state to the slot number. * * @param slot The slot location to save state to. - * @param wait If false, returns as early as possible. If true, returns once the savestate has been written to disk. */ @JvmStatic - external fun SaveState(slot: Int, wait: Boolean) + external fun SaveState(slot: Int) /** * Saves a game state to the specified path. * * @param path The path to save state to. - * @param wait If false, returns as early as possible. If true, returns once the savestate has been written to disk. */ @JvmStatic - external fun SaveStateAs(path: String, wait: Boolean) + external fun SaveStateAs(path: String) /** * Loads a game state from the slot number. diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.kt index 1b23118de3..8a1d8addbd 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.kt +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.kt @@ -494,16 +494,16 @@ class EmulationActivity : AppCompatActivity(), ThemeProvider { } MENU_ACTION_TAKE_SCREENSHOT -> NativeLibrary.SaveScreenShot() - MENU_ACTION_QUICK_SAVE -> NativeLibrary.SaveState(9, false) + MENU_ACTION_QUICK_SAVE -> NativeLibrary.SaveState(9) MENU_ACTION_QUICK_LOAD -> NativeLibrary.LoadState(9) MENU_ACTION_SAVE_ROOT -> showSubMenu(SaveOrLoad.SAVE) MENU_ACTION_LOAD_ROOT -> showSubMenu(SaveOrLoad.LOAD) - MENU_ACTION_SAVE_SLOT1 -> NativeLibrary.SaveState(0, false) - MENU_ACTION_SAVE_SLOT2 -> NativeLibrary.SaveState(1, false) - MENU_ACTION_SAVE_SLOT3 -> NativeLibrary.SaveState(2, false) - MENU_ACTION_SAVE_SLOT4 -> NativeLibrary.SaveState(3, false) - MENU_ACTION_SAVE_SLOT5 -> NativeLibrary.SaveState(4, false) - MENU_ACTION_SAVE_SLOT6 -> NativeLibrary.SaveState(5, false) + MENU_ACTION_SAVE_SLOT1 -> NativeLibrary.SaveState(0) + MENU_ACTION_SAVE_SLOT2 -> NativeLibrary.SaveState(1) + MENU_ACTION_SAVE_SLOT3 -> NativeLibrary.SaveState(2) + MENU_ACTION_SAVE_SLOT4 -> NativeLibrary.SaveState(3) + MENU_ACTION_SAVE_SLOT5 -> NativeLibrary.SaveState(4) + MENU_ACTION_SAVE_SLOT6 -> NativeLibrary.SaveState(5) MENU_ACTION_LOAD_SLOT1 -> NativeLibrary.LoadState(0) MENU_ACTION_LOAD_SLOT2 -> NativeLibrary.LoadState(1) MENU_ACTION_LOAD_SLOT3 -> NativeLibrary.LoadState(2) diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/EmulationFragment.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/EmulationFragment.kt index 9cf87c3401..e4698ba43c 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/EmulationFragment.kt +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/EmulationFragment.kt @@ -232,7 +232,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { } } - fun saveTemporaryState() = NativeLibrary.SaveStateAs(temporaryStateFilePath, true) + fun saveTemporaryState() = NativeLibrary.SaveStateAs(temporaryStateFilePath) private val temporaryStateFilePath: String get() = "${requireContext().filesDir}${File.separator}temp.sav" diff --git a/Source/Android/jni/MainAndroid.cpp b/Source/Android/jni/MainAndroid.cpp index 7c78f49045..7112833403 100644 --- a/Source/Android/jni/MainAndroid.cpp +++ b/Source/Android/jni/MainAndroid.cpp @@ -310,19 +310,17 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_eglBindAPI(J } JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SaveState(JNIEnv*, jclass, - jint slot, - jboolean wait) + jint slot) { HostThreadLock guard; - State::Save(Core::System::GetInstance(), slot, wait); + State::Save(Core::System::GetInstance(), slot); } JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SaveStateAs(JNIEnv* env, jclass, - jstring path, - jboolean wait) + jstring path) { HostThreadLock guard; - State::SaveAs(Core::System::GetInstance(), GetJString(env, path), wait); + State::SaveAs(Core::System::GetInstance(), GetJString(env, path)); } JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_LoadState(JNIEnv*, jclass, -- cgit v1.2.3