From f09b71582ee357ef47f8608a5b0fd848e404b453 Mon Sep 17 00:00:00 2001 From: mitaclaw <140017135+mitaclaw@users.noreply.github.com> Date: Mon, 18 Mar 2024 01:35:42 -0700 Subject: [PATCH] Core: Avoid (Some) Global System Accessor --- Source/Android/jni/MainAndroid.cpp | 6 +-- Source/Core/Core/BootManager.cpp | 2 +- Source/Core/Core/ConfigManager.cpp | 2 +- Source/Core/Core/Core.cpp | 38 +++++++++---------- Source/Core/Core/Core.h | 14 +++---- Source/Core/Core/HW/VideoInterface.cpp | 2 +- Source/Core/Core/IOS/USB/Bluetooth/BTReal.cpp | 2 +- Source/Core/Core/Movie.cpp | 10 ++--- Source/Core/DolphinNoGUI/MainNoGUI.cpp | 4 +- Source/Core/DolphinNoGUI/PlatformFBDev.cpp | 3 +- Source/Core/DolphinNoGUI/PlatformHeadless.cpp | 4 +- Source/Core/DolphinNoGUI/PlatformMacos.mm | 3 +- Source/Core/DolphinNoGUI/PlatformWin32.cpp | 3 +- Source/Core/DolphinNoGUI/PlatformX11.cpp | 3 +- Source/Core/DolphinQt/Main.cpp | 5 ++- Source/Core/DolphinQt/MainWindow.cpp | 4 +- 16 files changed, 54 insertions(+), 51 deletions(-) diff --git a/Source/Android/jni/MainAndroid.cpp b/Source/Android/jni/MainAndroid.cpp index 1ea795de0b..455f5f30af 100644 --- a/Source/Android/jni/MainAndroid.cpp +++ b/Source/Android/jni/MainAndroid.cpp @@ -258,7 +258,7 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_PauseEmulati JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_StopEmulation(JNIEnv*, jclass) { HostThreadLock guard; - Core::Stop(); + Core::Stop(Core::System::GetInstance()); // Kick the waiting event s_update_main_frame_event.Set(); @@ -586,11 +586,11 @@ static void Run(JNIEnv* env, std::unique_ptr&& boot, bool riivol host_identity_guard.Unlock(); s_update_main_frame_event.Wait(); host_identity_guard.Lock(); - Core::HostDispatchJobs(); + Core::HostDispatchJobs(Core::System::GetInstance()); } s_game_metadata_is_valid = false; - Core::Shutdown(); + Core::Shutdown(Core::System::GetInstance()); host_identity_guard.Unlock(); env->CallStaticVoidMethod(IDCache::GetNativeLibraryClass(), diff --git a/Source/Core/Core/BootManager.cpp b/Source/Core/Core/BootManager.cpp index 1c87994bf9..9e9fb3b58c 100644 --- a/Source/Core/Core/BootManager.cpp +++ b/Source/Core/Core/BootManager.cpp @@ -146,7 +146,7 @@ bool BootCore(Core::System& system, std::unique_ptr boot, system.Initialize(); - Core::UpdateWantDeterminism(/*initial*/ true); + Core::UpdateWantDeterminism(system, /*initial*/ true); if (system.IsWii()) { diff --git a/Source/Core/Core/ConfigManager.cpp b/Source/Core/Core/ConfigManager.cpp index 693b2fb439..fb6fda4754 100644 --- a/Source/Core/Core/ConfigManager.cpp +++ b/Source/Core/Core/ConfigManager.cpp @@ -190,7 +190,7 @@ void SConfig::SetRunningGameMetadata(const std::string& game_id, const std::stri Host_TitleChanged(); if (Core::IsRunning()) { - Core::UpdateTitle(); + Core::UpdateTitle(system); } Config::AddLayer(ConfigLoaders::GenerateGlobalGameConfigLoader(game_id, revision)); diff --git a/Source/Core/Core/Core.cpp b/Source/Core/Core/Core.cpp index c7c392fa0e..bb4e1e1a26 100644 --- a/Source/Core/Core/Core.cpp +++ b/Source/Core/Core/Core.cpp @@ -120,7 +120,7 @@ static std::unique_ptr s_memory_watcher; struct HostJob { - std::function job; + std::function job; bool run_after_stop; }; static std::mutex s_host_jobs_lock; @@ -166,13 +166,13 @@ void FrameUpdateOnCPUThread() NetPlay::NetPlayClient::SendTimeBase(); } -void OnFrameEnd() +void OnFrameEnd(Core::System& system) { #ifdef USE_MEMORYWATCHER if (s_memory_watcher) { ASSERT(IsCPUThread()); - CPUThreadGuard guard(Core::System::GetInstance()); + const CPUThreadGuard guard(system); s_memory_watcher->Step(guard); } @@ -252,7 +252,7 @@ bool Init(Core::System& system, std::unique_ptr boot, const Wind } // Drain any left over jobs - HostDispatchJobs(); + HostDispatchJobs(system); INFO_LOG_FMT(BOOT, "Starting core = {} mode", system.IsWii() ? "Wii" : "GameCube"); INFO_LOG_FMT(BOOT, "CPU Thread separate = {}", system.IsDualCoreMode() ? "Yes" : "No"); @@ -284,7 +284,7 @@ static void ResetRumble() } // Called from GUI thread -void Stop() // - Hammertime! +void Stop(Core::System& system) // - Hammertime! { if (GetState() == State::Stopping || GetState() == State::Uninitialized) return; @@ -299,9 +299,7 @@ void Stop() // - Hammertime! CallOnStateChangedCallbacks(State::Stopping); // Dump left over jobs - HostDispatchJobs(); - - auto& system = Core::System::GetInstance(); + HostDispatchJobs(system); system.GetFifo().EmulatorState(false); @@ -359,7 +357,7 @@ static void CPUSetInitialExecutionState(bool force_paused = false) { // The CPU starts in stepping state, and will wait until a new state is set before executing. // SetState must be called on the host thread, so we defer it for later. - QueueHostJob([force_paused]() { + QueueHostJob([force_paused](Core::System&) { bool paused = SConfig::GetInstance().bBootToPause || force_paused; SetState(paused ? State::Paused : State::Running); Host_UpdateDisasmDialog(); @@ -663,7 +661,7 @@ static void EmuThread(Core::System& system, std::unique_ptr boot system.GetPowerPC().SetMode(PowerPC::CoreMode::Interpreter); } - UpdateTitle(); + UpdateTitle(system); // ENTER THE VIDEO THREAD LOOP if (system.IsDualCoreMode()) @@ -936,13 +934,12 @@ void Callback_NewField(Core::System& system) #endif // USE_RETRO_ACHIEVEMENTS } -void UpdateTitle() +void UpdateTitle(Core::System& system) { // Settings are shown the same for both extended and summary info const std::string SSettings = fmt::format( - "{} {} | {} | {}", Core::System::GetInstance().GetPowerPC().GetCPUName(), - Core::System::GetInstance().IsDualCoreMode() ? "DC" : "SC", g_video_backend->GetDisplayName(), - Config::Get(Config::MAIN_DSP_HLE) ? "HLE" : "LLE"); + "{} {} | {} | {}", system.GetPowerPC().GetCPUName(), system.IsDualCoreMode() ? "DC" : "SC", + g_video_backend->GetDisplayName(), Config::Get(Config::MAIN_DSP_HLE) ? "HLE" : "LLE"); std::string message = fmt::format("{} | {}", Common::GetScmRevStr(), SSettings); if (Config::Get(Config::MAIN_SHOW_ACTIVE_TITLE)) @@ -955,7 +952,7 @@ void UpdateTitle() Host_UpdateTitle(message); } -void Shutdown() +void Shutdown(Core::System& system) { // During shutdown DXGI expects us to handle some messages on the UI thread. // Therefore we can't immediately block and wait for the emu thread to shut @@ -967,7 +964,7 @@ void Shutdown() s_emu_thread.join(); // Make sure there's nothing left over in case we're about to exit. - HostDispatchJobs(); + HostDispatchJobs(system); } int AddOnStateChangedCallback(StateChangedCallbackFunc callback) @@ -1004,12 +1001,11 @@ void CallOnStateChangedCallbacks(Core::State state) } } -void UpdateWantDeterminism(bool initial) +void UpdateWantDeterminism(Core::System& system, bool initial) { // For now, this value is not itself configurable. Instead, individual // settings that depend on it, such as GPU determinism mode. should have // override options for testing, - auto& system = Core::System::GetInstance(); bool new_want_determinism = system.GetMovie().IsMovieActive() || NetPlay::IsNetPlayRunning(); if (new_want_determinism != s_wants_determinism || initial) { @@ -1030,7 +1026,7 @@ void UpdateWantDeterminism(bool initial) } } -void QueueHostJob(std::function job, bool run_during_stop) +void QueueHostJob(std::function job, bool run_during_stop) { if (!job) return; @@ -1046,7 +1042,7 @@ void QueueHostJob(std::function job, bool run_during_stop) Host_Message(HostMessageID::WMUserJobDispatch); } -void HostDispatchJobs() +void HostDispatchJobs(Core::System& system) { // WARNING: This should only run on the Host Thread. // NOTE: This function is potentially re-entrant. If a job calls @@ -1066,7 +1062,7 @@ void HostDispatchJobs() continue; guard.unlock(); - job.job(); + job.job(system); guard.lock(); } } diff --git a/Source/Core/Core/Core.h b/Source/Core/Core/Core.h index 90346d2cac..d4311792a4 100644 --- a/Source/Core/Core/Core.h +++ b/Source/Core/Core/Core.h @@ -125,8 +125,8 @@ private: }; bool Init(Core::System& system, std::unique_ptr boot, const WindowSystemInfo& wsi); -void Stop(); -void Shutdown(); +void Stop(Core::System& system); +void Shutdown(Core::System& system); void DeclareAsCPUThread(); void UndeclareAsCPUThread(); @@ -157,7 +157,7 @@ void SaveScreenShot(std::string_view name); void DisplayMessage(std::string message, int time_in_ms); void FrameUpdateOnCPUThread(); -void OnFrameEnd(); +void OnFrameEnd(Core::System& system); // Run a function as the CPU thread. // @@ -181,7 +181,7 @@ bool RemoveOnStateChangedCallback(int* handle); void CallOnStateChangedCallbacks(Core::State state); // Run on the Host thread when the factors change. [NOT THREADSAFE] -void UpdateWantDeterminism(bool initial = false); +void UpdateWantDeterminism(Core::System& system, bool initial = false); // Queue an arbitrary function to asynchronously run once on the Host thread later. // Threadsafe. Can be called by any thread, including the Host itself. @@ -192,16 +192,16 @@ void UpdateWantDeterminism(bool initial = false); // NOTE: Make sure the jobs check the global state instead of assuming everything is // still the same as when the job was queued. // NOTE: Jobs that are not set to run during stop will be discarded instead. -void QueueHostJob(std::function job, bool run_during_stop = false); +void QueueHostJob(std::function job, bool run_during_stop = false); // Should be called periodically by the Host to run pending jobs. // WMUserJobDispatch will be sent when something is added to the queue. -void HostDispatchJobs(); +void HostDispatchJobs(Core::System& system); void DoFrameStep(); void UpdateInputGate(bool require_focus, bool require_full_focus = false); -void UpdateTitle(); +void UpdateTitle(Core::System& system); } // namespace Core diff --git a/Source/Core/Core/HW/VideoInterface.cpp b/Source/Core/Core/HW/VideoInterface.cpp index 83a30cbbcf..be197f4915 100644 --- a/Source/Core/Core/HW/VideoInterface.cpp +++ b/Source/Core/Core/HW/VideoInterface.cpp @@ -841,7 +841,7 @@ void VideoInterfaceManager::EndField(FieldType field, u64 ticks) g_perf_metrics.CountVBlank(); VIEndFieldEvent::Trigger(); - Core::OnFrameEnd(); + Core::OnFrameEnd(m_system); } // Purpose: Send VI interrupt when triggered diff --git a/Source/Core/Core/IOS/USB/Bluetooth/BTReal.cpp b/Source/Core/Core/IOS/USB/Bluetooth/BTReal.cpp index e48ef3bf81..9d853d1f48 100644 --- a/Source/Core/Core/IOS/USB/Bluetooth/BTReal.cpp +++ b/Source/Core/Core/IOS/USB/Bluetooth/BTReal.cpp @@ -177,7 +177,7 @@ std::optional BluetoothRealDevice::Open(const OpenRequest& request) "The emulated console will now stop.", m_last_open_error); } - Core::QueueHostJob(Core::Stop); + Core::QueueHostJob(&Core::Stop); return IPCReply(IPC_ENOENT); } diff --git a/Source/Core/Core/Movie.cpp b/Source/Core/Core/Movie.cpp index ea6e6c0e5a..e576afc1b7 100644 --- a/Source/Core/Core/Movie.cpp +++ b/Source/Core/Core/Movie.cpp @@ -572,7 +572,7 @@ bool MovieManager::BeginRecordingInput(const ControllerTypeArray& controllers, Config::AddLayer(ConfigLoaders::GenerateMovieConfigLoader(&header)); if (Core::IsRunning()) - Core::UpdateWantDeterminism(); + Core::UpdateWantDeterminism(m_system); }; Core::RunOnCPUThread(start_recording, true); @@ -958,7 +958,7 @@ bool MovieManager::PlayInput(const std::string& movie_path, // Wiimotes cause desync issues if they're not reset before launching the game Wiimote::ResetAllWiimotes(); - Core::UpdateWantDeterminism(); + Core::UpdateWantDeterminism(m_system); m_temp_input.resize(recording_file.GetSize() - 256); recording_file.ReadBytes(m_temp_input.data(), m_temp_input.size()); @@ -1152,7 +1152,7 @@ void MovieManager::LoadInput(const std::string& movie_path) if (m_play_mode != PlayMode::Playing) { m_play_mode = PlayMode::Playing; - Core::UpdateWantDeterminism(); + Core::UpdateWantDeterminism(m_system); Core::DisplayMessage("Switched to playback", 2000); } } @@ -1161,7 +1161,7 @@ void MovieManager::LoadInput(const std::string& movie_path) if (m_play_mode != PlayMode::Recording) { m_play_mode = PlayMode::Recording; - Core::UpdateWantDeterminism(); + Core::UpdateWantDeterminism(m_system); Core::DisplayMessage("Switched to recording", 2000); } } @@ -1355,7 +1355,7 @@ void MovieManager::EndPlayInput(bool cont) // delete tmpInput; // tmpInput = nullptr; - Core::QueueHostJob([=] { Core::UpdateWantDeterminism(); }); + Core::QueueHostJob([](Core::System& system) { Core::UpdateWantDeterminism(system); }); } } diff --git a/Source/Core/DolphinNoGUI/MainNoGUI.cpp b/Source/Core/DolphinNoGUI/MainNoGUI.cpp index 9db8ef8a47..6a0dfd630f 100644 --- a/Source/Core/DolphinNoGUI/MainNoGUI.cpp +++ b/Source/Core/DolphinNoGUI/MainNoGUI.cpp @@ -316,9 +316,9 @@ int main(int argc, char* argv[]) #endif s_platform->MainLoop(); - Core::Stop(); + Core::Stop(Core::System::GetInstance()); - Core::Shutdown(); + Core::Shutdown(Core::System::GetInstance()); s_platform.reset(); return 0; diff --git a/Source/Core/DolphinNoGUI/PlatformFBDev.cpp b/Source/Core/DolphinNoGUI/PlatformFBDev.cpp index 552a2aa9c4..53312bfa08 100644 --- a/Source/Core/DolphinNoGUI/PlatformFBDev.cpp +++ b/Source/Core/DolphinNoGUI/PlatformFBDev.cpp @@ -9,6 +9,7 @@ #include "Core/ConfigManager.h" #include "Core/Core.h" #include "Core/State.h" +#include "Core/System.h" #include #include @@ -78,7 +79,7 @@ void PlatformFBDev::MainLoop() while (IsRunning()) { UpdateRunningFlag(); - Core::HostDispatchJobs(); + Core::HostDispatchJobs(Core::System::GetInstance()); // TODO: Is this sleep appropriate? std::this_thread::sleep_for(std::chrono::milliseconds(1)); diff --git a/Source/Core/DolphinNoGUI/PlatformHeadless.cpp b/Source/Core/DolphinNoGUI/PlatformHeadless.cpp index 8e3c82bca1..2f6ea2e7ac 100644 --- a/Source/Core/DolphinNoGUI/PlatformHeadless.cpp +++ b/Source/Core/DolphinNoGUI/PlatformHeadless.cpp @@ -3,7 +3,9 @@ #include #include + #include "Core/Core.h" +#include "Core/System.h" #include "DolphinNoGUI/Platform.h" namespace @@ -27,7 +29,7 @@ void PlatformHeadless::MainLoop() while (m_running.IsSet()) { UpdateRunningFlag(); - Core::HostDispatchJobs(); + Core::HostDispatchJobs(Core::System::GetInstance()); std::this_thread::sleep_for(std::chrono::milliseconds(100)); } } diff --git a/Source/Core/DolphinNoGUI/PlatformMacos.mm b/Source/Core/DolphinNoGUI/PlatformMacos.mm index 633d27908c..0bdfd44356 100644 --- a/Source/Core/DolphinNoGUI/PlatformMacos.mm +++ b/Source/Core/DolphinNoGUI/PlatformMacos.mm @@ -7,6 +7,7 @@ #include "Core/Config/MainSettings.h" #include "Core/Core.h" #include "Core/State.h" +#include "Core/System.h" #include "VideoCommon/Present.h" #include "VideoCommon/RenderBase.h" @@ -225,7 +226,7 @@ void PlatformMacOS::MainLoop() while (IsRunning()) { UpdateRunningFlag(); - Core::HostDispatchJobs(); + Core::HostDispatchJobs(Core::System::GetInstance()); ProcessEvents(); UpdateWindowPosition(); } diff --git a/Source/Core/DolphinNoGUI/PlatformWin32.cpp b/Source/Core/DolphinNoGUI/PlatformWin32.cpp index 4b5e5aa1ef..b92849a3b6 100644 --- a/Source/Core/DolphinNoGUI/PlatformWin32.cpp +++ b/Source/Core/DolphinNoGUI/PlatformWin32.cpp @@ -8,6 +8,7 @@ #include "Core/ConfigManager.h" #include "Core/Core.h" #include "Core/State.h" +#include "Core/System.h" #include #include @@ -123,7 +124,7 @@ void PlatformWin32::MainLoop() while (IsRunning()) { UpdateRunningFlag(); - Core::HostDispatchJobs(); + Core::HostDispatchJobs(Core::System::GetInstance()); ProcessEvents(); UpdateWindowPosition(); diff --git a/Source/Core/DolphinNoGUI/PlatformX11.cpp b/Source/Core/DolphinNoGUI/PlatformX11.cpp index 18784499e4..16cfaa3683 100644 --- a/Source/Core/DolphinNoGUI/PlatformX11.cpp +++ b/Source/Core/DolphinNoGUI/PlatformX11.cpp @@ -15,6 +15,7 @@ static constexpr auto X_None = None; #include "Core/Config/MainSettings.h" #include "Core/Core.h" #include "Core/State.h" +#include "Core/System.h" #include #include @@ -151,7 +152,7 @@ void PlatformX11::MainLoop() while (IsRunning()) { UpdateRunningFlag(); - Core::HostDispatchJobs(); + Core::HostDispatchJobs(Core::System::GetInstance()); ProcessEvents(); UpdateWindowPosition(); diff --git a/Source/Core/DolphinQt/Main.cpp b/Source/Core/DolphinQt/Main.cpp index 30922f1c63..52f1583309 100644 --- a/Source/Core/DolphinQt/Main.cpp +++ b/Source/Core/DolphinQt/Main.cpp @@ -28,6 +28,7 @@ #include "Core/Config/MainSettings.h" #include "Core/Core.h" #include "Core/DolphinAnalytics.h" +#include "Core/System.h" #include "DolphinQt/Host.h" #include "DolphinQt/MainWindow.h" @@ -179,7 +180,7 @@ int main(int argc, char* argv[]) // Whenever the event loop is about to go to sleep, dispatch the jobs // queued in the Core first. QObject::connect(QAbstractEventDispatcher::instance(), &QAbstractEventDispatcher::aboutToBlock, - &app, &Core::HostDispatchJobs); + &app, [] { Core::HostDispatchJobs(Core::System::GetInstance()); }); std::optional save_state_path; if (options.is_set("save_state")) @@ -293,7 +294,7 @@ int main(int argc, char* argv[]) retval = app.exec(); } - Core::Shutdown(); + Core::Shutdown(Core::System::GetInstance()); UICommon::Shutdown(); Host::GetInstance()->deleteLater(); diff --git a/Source/Core/DolphinQt/MainWindow.cpp b/Source/Core/DolphinQt/MainWindow.cpp index 83f75340f4..7e7086e5af 100644 --- a/Source/Core/DolphinQt/MainWindow.cpp +++ b/Source/Core/DolphinQt/MainWindow.cpp @@ -909,7 +909,7 @@ bool MainWindow::RequestStop() { if (!Core::IsRunning()) { - Core::QueueHostJob([this] { OnStopComplete(); }, true); + Core::QueueHostJob([this](Core::System&) { OnStopComplete(); }, true); return true; } @@ -1009,7 +1009,7 @@ bool MainWindow::RequestStop() void MainWindow::ForceStop() { - Core::Stop(); + Core::Stop(Core::System::GetInstance()); } void MainWindow::Reset()