From 446e2d9119a3c4281e72feefca9bbcd2b1eb21e3 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 8 Aug 2021 14:33:06 +0200 Subject: [PATCH] Android: Remove state from EmulationState --- .../dolphinemu/dolphinemu/NativeLibrary.java | 9 +++ .../fragments/EmulationFragment.java | 79 +++---------------- Source/Android/jni/MainAndroid.cpp | 16 +++- 3 files changed, 36 insertions(+), 68 deletions(-) diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.java index 5ab4e15993..123cd6c066 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.java @@ -410,6 +410,13 @@ public final class NativeLibrary */ public static native void StopEmulation(); + /** + * Ensures that IsRunning will return true from now on until emulation exits. + * (If this is not called, IsRunning will start returning true at some point + * after calling Run.) + */ + public static native void SetIsBooting(); + /** * Returns true if emulation is running (or is paused). */ @@ -417,6 +424,8 @@ public final class NativeLibrary public static native boolean IsRunningAndStarted(); + public static native boolean IsRunningAndUnpaused(); + /** * Enables or disables CPU block profiling * diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/EmulationFragment.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/EmulationFragment.java index 2ea106cb4a..ce35f1e98e 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/EmulationFragment.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/EmulationFragment.java @@ -123,8 +123,12 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C @Override public void onPause() { - if (mEmulationState.isRunning() && !NativeLibrary.IsShowingAlertMessage()) - mEmulationState.pause(); + if (NativeLibrary.IsRunningAndUnpaused() && !NativeLibrary.IsShowingAlertMessage()) + { + Log.debug("[EmulationFragment] Pausing emulation."); + NativeLibrary.PauseEmulation(); + } + super.onPause(); } @@ -186,7 +190,8 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C public void stopEmulation() { - mEmulationState.stop(); + Log.debug("[EmulationFragment] Stopping emulation."); + NativeLibrary.StopEmulation(); } public void startConfiguringControls() @@ -214,13 +219,7 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C private static class EmulationState { - private enum State - { - STOPPED, RUNNING, PAUSED - } - private final String[] mGamePaths; - private State state; private boolean mRunWhenSurfaceIsValid; private boolean loadPreviousTemporaryState; private final String temporaryStatePath; @@ -229,56 +228,6 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C { mGamePaths = gamePaths; this.temporaryStatePath = temporaryStatePath; - // Starting state is stopped. - state = State.STOPPED; - } - - // Getters for the current state - - public synchronized boolean isStopped() - { - return state == State.STOPPED; - } - - public synchronized boolean isPaused() - { - return state == State.PAUSED; - } - - public synchronized boolean isRunning() - { - return state == State.RUNNING; - } - - // State changing methods - - public synchronized void stop() - { - if (state != State.STOPPED) - { - Log.debug("[EmulationFragment] Stopping emulation."); - state = State.STOPPED; - NativeLibrary.StopEmulation(); - } - else - { - Log.warning("[EmulationFragment] Stop called while already stopped."); - } - } - - public synchronized void pause() - { - if (state != State.PAUSED) - { - state = State.PAUSED; - Log.debug("[EmulationFragment] Pausing emulation."); - - NativeLibrary.PauseEmulation(); - } - else - { - Log.warning("[EmulationFragment] Pause called while already paused."); - } } public synchronized void run(boolean isActivityRecreated) @@ -288,7 +237,6 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C if (NativeLibrary.IsRunning()) { loadPreviousTemporaryState = false; - state = State.PAUSED; deleteFile(temporaryStatePath); } else @@ -326,8 +274,10 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C private void runWithValidSurface() { mRunWhenSurfaceIsValid = false; - if (state == State.STOPPED) + if (!NativeLibrary.IsRunning()) { + NativeLibrary.SetIsBooting(); + Thread emulationThread = new Thread(() -> { if (loadPreviousTemporaryState) @@ -344,7 +294,7 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C }, "NativeEmulation"); emulationThread.start(); } - else if (state == State.PAUSED) + else { if (!EmulationActivity.getHasUserPausedEmulation() && !NativeLibrary.IsShowingAlertMessage()) @@ -353,11 +303,6 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C NativeLibrary.UnPauseEmulation(); } } - else - { - Log.debug("[EmulationFragment] Bug, run called while already running."); - } - state = State.RUNNING; } } diff --git a/Source/Android/jni/MainAndroid.cpp b/Source/Android/jni/MainAndroid.cpp index eedc728ad9..1b54cd592c 100644 --- a/Source/Android/jni/MainAndroid.cpp +++ b/Source/Android/jni/MainAndroid.cpp @@ -23,6 +23,7 @@ #include "Common/CommonTypes.h" #include "Common/Event.h" #include "Common/FileUtil.h" +#include "Common/Flag.h" #include "Common/IniFile.h" #include "Common/Logging/LogManager.h" #include "Common/MsgHandler.h" @@ -81,6 +82,7 @@ Common::Event s_update_main_frame_event; std::mutex s_surface_lock; bool s_need_nonblocking_alert_msg; +Common::Flag s_is_booting; bool s_have_wm_user_stop = false; bool s_game_metadata_is_valid = false; } // Anonymous namespace @@ -247,9 +249,14 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_StopEmulatio s_update_main_frame_event.Set(); } +JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SetIsBooting(JNIEnv*, jclass) +{ + s_is_booting.Set(); +} + JNIEXPORT jboolean JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_IsRunning(JNIEnv*, jclass) { - return static_cast(Core::IsRunning()); + return s_is_booting.IsSet() || static_cast(Core::IsRunning()); } JNIEXPORT jboolean JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_IsRunningAndStarted(JNIEnv*, @@ -258,6 +265,12 @@ JNIEXPORT jboolean JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_IsRunnin return static_cast(Core::IsRunningAndStarted()); } +JNIEXPORT jboolean JNICALL +Java_org_dolphinemu_dolphinemu_NativeLibrary_IsRunningAndUnpaused(JNIEnv*, jclass) +{ + return static_cast(Core::GetState() == Core::State::Running); +} + JNIEXPORT jboolean JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_onGamePadEvent( JNIEnv* env, jclass, jstring jDevice, jint Button, jint Action) { @@ -561,6 +574,7 @@ static void Run(JNIEnv* env, const std::vector& paths, } } + s_is_booting.Clear(); s_need_nonblocking_alert_msg = false; surface_guard.unlock();