Android: Remove state from EmulationState

This commit is contained in:
JosJuice 2021-08-08 14:33:06 +02:00
parent 2c564a0b9d
commit 446e2d9119
3 changed files with 36 additions and 68 deletions

View File

@ -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
*

View File

@ -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;
}
}

View File

@ -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<jboolean>(Core::IsRunning());
return s_is_booting.IsSet() || static_cast<jboolean>(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<jboolean>(Core::IsRunningAndStarted());
}
JNIEXPORT jboolean JNICALL
Java_org_dolphinemu_dolphinemu_NativeLibrary_IsRunningAndUnpaused(JNIEnv*, jclass)
{
return static_cast<jboolean>(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<std::string>& paths,
}
}
s_is_booting.Clear();
s_need_nonblocking_alert_msg = false;
surface_guard.unlock();