diff --git a/Common/GPU/OpenGL/GLRenderManager.h b/Common/GPU/OpenGL/GLRenderManager.h index 8a7ddf0a9c..707aa94e58 100644 --- a/Common/GPU/OpenGL/GLRenderManager.h +++ b/Common/GPU/OpenGL/GLRenderManager.h @@ -249,6 +249,11 @@ public: void ThreadStart(Draw::DrawContext *draw); void ThreadEnd(); bool ThreadFrame(); // False means it's time to exit. + // True if ThreadFrame() has queued work and will not block on an empty queue. + bool HasQueuedThreadWork() { + std::lock_guard lock(pushMutex_); + return !renderThreadQueue_.empty(); + } void NotifyEmuThreadExit(); diff --git a/Common/Serialize/Serializer.h b/Common/Serialize/Serializer.h index e583391f28..3b519fd461 100644 --- a/Common/Serialize/Serializer.h +++ b/Common/Serialize/Serializer.h @@ -227,7 +227,8 @@ public: } // If *saved is null, will allocate storage using malloc. - // If it's not null, it will be used, but only hope can save you from overruns at the end. For libretro. + // If it's not null, it will be used, and *savedSize is its capacity: a state that doesn't fit fails with + // ERROR_BAD_ALLOC. For libretro. template static Error MeasureAndSavePtr(T &_class, u8 **saved, size_t *savedSize) { @@ -239,6 +240,8 @@ public: size_t measuredSize = p.Offset(); u8 *data; if (*saved) { + if (measuredSize > *savedSize) + return ERROR_BAD_ALLOC; data = *saved; } else { data = (u8 *)malloc(measuredSize); diff --git a/libretro/LibretroGLContext.cpp b/libretro/LibretroGLContext.cpp index 3ac3765a97..48cfc1bba8 100644 --- a/libretro/LibretroGLContext.cpp +++ b/libretro/LibretroGLContext.cpp @@ -35,7 +35,6 @@ void LibretroGLContext::CreateDrawContext() { renderManager_ = (GLRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER); renderManager_->SetInflightFrames(g_Config.iInflightFrames); SetGPUBackend(GPUBackend::OPENGL); - draw_->CreatePresets(); } void LibretroGLContext::DestroyDrawContext() { diff --git a/libretro/LibretroGLContext.h b/libretro/LibretroGLContext.h index 0ae24937d0..508ec266ef 100644 --- a/libretro/LibretroGLContext.h +++ b/libretro/LibretroGLContext.h @@ -28,6 +28,7 @@ public: void ThreadStart() override { renderManager_->ThreadStart(draw_); } bool ThreadFrame() override { return renderManager_->ThreadFrame(); } + bool ThreadFramePending() override { return renderManager_->HasQueuedThreadWork(); } void ThreadEnd() override { renderManager_->ThreadEnd(); } GPUCore GetGPUCore() override { return GPUCORE_GLES; } diff --git a/libretro/LibretroGLCoreContext.cpp b/libretro/LibretroGLCoreContext.cpp index de549e0380..7417a60434 100644 --- a/libretro/LibretroGLCoreContext.cpp +++ b/libretro/LibretroGLCoreContext.cpp @@ -37,7 +37,6 @@ void LibretroGLCoreContext::CreateDrawContext() { renderManager_ = (GLRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER); renderManager_->SetInflightFrames(g_Config.iInflightFrames); SetGPUBackend(GPUBackend::OPENGL); - draw_->CreatePresets(); } void LibretroGLCoreContext::DestroyDrawContext() { diff --git a/libretro/LibretroGLCoreContext.h b/libretro/LibretroGLCoreContext.h index d91c20164d..5fb4da3ce4 100644 --- a/libretro/LibretroGLCoreContext.h +++ b/libretro/LibretroGLCoreContext.h @@ -22,6 +22,7 @@ public: void ThreadStart() override { renderManager_->ThreadStart(draw_); } bool ThreadFrame() override { return renderManager_->ThreadFrame(); } + bool ThreadFramePending() override { return renderManager_->HasQueuedThreadWork(); } void ThreadEnd() override { renderManager_->ThreadEnd(); } // Call from emu thread diff --git a/libretro/LibretroGraphicsContext.h b/libretro/LibretroGraphicsContext.h index 99d61a6405..8a5c318d50 100644 --- a/libretro/LibretroGraphicsContext.h +++ b/libretro/LibretroGraphicsContext.h @@ -31,6 +31,8 @@ public: } virtual void SwapBuffers() = 0; void Resize() override {} + // True if ThreadFrame() has queued work to run (emu thread backends only). + virtual bool ThreadFramePending() { return false; } virtual void GotBackbuffer(); virtual void LostBackbuffer(); diff --git a/libretro/libretro.cpp b/libretro/libretro.cpp index ce785a3bb0..b108b2f851 100644 --- a/libretro/libretro.cpp +++ b/libretro/libretro.cpp @@ -1,4 +1,5 @@ #include "ppsspp_config.h" +#include #include #include #include @@ -131,15 +132,24 @@ namespace Libretro static s64 runTicksLast = 0; // Must be called with output_audio_buffer_mutex held. - static void ensure_output_audio_buffer_capacity(int32_t capacity) + static bool ensure_output_audio_buffer_capacity(int32_t capacity) { if (capacity <= output_audio_buffer.capacity) { - return; + return true; } - output_audio_buffer.data = (int16_t*)realloc(output_audio_buffer.data, capacity * sizeof(*output_audio_buffer.data)); + int16_t *data = (int16_t*)realloc(output_audio_buffer.data, capacity * sizeof(*output_audio_buffer.data)); + if (!data) { + // Keep the old buffer, drop the new samples. + if (log_cb) + log_cb(RETRO_LOG_ERROR, "Failed to grow output audio buffer to %d samples\n", capacity); + return false; + } + output_audio_buffer.data = data; output_audio_buffer.capacity = capacity; - log_cb(RETRO_LOG_DEBUG, "Output audio buffer capacity set to %d\n", capacity); + if (log_cb) + log_cb(RETRO_LOG_DEBUG, "Output audio buffer capacity set to %d\n", capacity); + return true; } static void init_output_audio_buffer(int32_t capacity) @@ -163,7 +173,8 @@ namespace Libretro static void upload_output_audio_buffer() { std::lock_guard lock(output_audio_buffer_mutex); - audio_batch_cb(output_audio_buffer.data, output_audio_buffer.size / 2); + if (output_audio_buffer.data && output_audio_buffer.size > 0) + audio_batch_cb(output_audio_buffer.data, output_audio_buffer.size / 2); output_audio_buffer.size = 0; } @@ -1387,14 +1398,16 @@ namespace Libretro { SetCurrentThreadName("EmuThread"); for (;;) { - switch ((EmuThreadState)emuThreadState) + EmuThreadState state = emuThreadState; + switch (state) { case EmuThreadState::RUNNING: EmuFrame(); break; case EmuThreadState::PAUSE_REQUESTED: - emuThreadState = EmuThreadState::PAUSED; - [[fallthrough]]; + // CAS so a concurrent QUIT_REQUESTED is not clobbered; the loop re-reads the state. + emuThreadState.compare_exchange_strong(state, EmuThreadState::PAUSED); + break; case EmuThreadState::PAUSED: sleep_ms(1, "libretro-paused"); break; @@ -1427,16 +1440,19 @@ namespace Libretro { } void EmuThreadStop() { - if (emuThreadState != EmuThreadState::RUNNING) + // Stop and join from any live state, not just RUNNING: unloading while + // PAUSED (the state retro_serialize_size() leaves behind) must not leave + // a live thread over freed engine state or a joinable std::thread. + if (!emuThread.joinable()) return; - emuThreadState = EmuThreadState::QUIT_REQUESTED; + if (emuThreadState != EmuThreadState::STOPPED) + emuThreadState = EmuThreadState::QUIT_REQUESTED; // Eat remaining frames. while (ctx->ThreadFrame()) {} emuThread.join(); - emuThread = std::thread(); ctx->ThreadEnd(); } @@ -1446,11 +1462,31 @@ namespace Libretro { emuThreadState = EmuThreadState::PAUSE_REQUESTED; - // Is this safe? - ctx->ThreadFrame(); // Eat 1 frame + // Drain queued frames until the emu thread parks at a frame boundary. + // A single frame is not enough when the emu thread is blocked on a + // readback sync in the following frame, and ThreadFrame() blocks on an + // empty queue, so only pump it when work is queued. + while (emuThreadState != EmuThreadState::PAUSED) { + if (ctx->ThreadFramePending()) + ctx->ThreadFrame(); + else + sleep_ms(1, "libretro-pause-poll"); + } + } - while (emuThreadState != EmuThreadState::PAUSED) - sleep_ms(1, "libretro-pause-poll"); + // Unload and reset can come while the loader thread is still booting. Let it finish + // before shutting down, since it uses the memory and file systems PSP_Shutdown tears down. + static void FinishPendingBoot() { + if (g_pendingBoot) { + std::string errorString; + while (PSP_InitUpdate(&errorString) == BootState::Booting) + sleep_ms(1, "libretro-boot-drain"); + g_pendingBoot = false; + } + // A state queued for the boot is stale now. + free(unserialize_data); + unserialize_data = nullptr; + unserialize_size = 0; } } // namespace Libretro @@ -1553,7 +1589,10 @@ void retro_unload_game(void) { Libretro::EmuThreadStop(); } - PSP_Shutdown(true); + Libretro::FinishPendingBoot(); + // A failed boot has already shut down. + if (PollBootState() != BootState::Off) + PSP_Shutdown(true); g_VFS.Clear(); delete ctx; @@ -1562,7 +1601,14 @@ void retro_unload_game(void) { } void retro_reset(void) { - PSP_Shutdown(true); + // EmuFrame() must not run while the core is torn down; retro_run() + // restarts the thread. + if (Libretro::useEmuThread) + Libretro::EmuThreadStop(); + + Libretro::FinishPendingBoot(); + if (PollBootState() != BootState::Off) + PSP_Shutdown(true); if (BootState::Complete != PSP_Init(PSP_CoreParameter(), &g_bootErrorString)) { ERROR_LOG(Log::Boot, "%s", g_bootErrorString.c_str()); @@ -1605,14 +1651,15 @@ static void retro_input(void) { ret |= (1 << i); } + u32 buttons = 0; + u32 controlledButtons = 0; for (i = 0; i < sizeof(map) / sizeof(*map); i++) { - bool pressed = ret & (1 << map[i].retro); - if (pressed) { - __CtrlUpdateButtons(map[i].sceCtrl, 0); - } else { - __CtrlUpdateButtons(0, map[i].sceCtrl); - } + controlledButtons |= map[i].sceCtrl; + if (ret & (1 << map[i].retro)) + buttons |= map[i].sceCtrl; } + // One update, so the emu thread can't sample a half-updated set of buttons. + __CtrlUpdateButtons(buttons, controlledButtons & ~buttons); float x_left = input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X) / 32767.0f; float y_left = input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y) / -32767.0f; @@ -1708,10 +1755,13 @@ void retro_run(void) { } // Update setting if any have changed. - bool updated; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated) + bool updated = false; + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated) { + // The emu thread reads g_Config mid-frame; it is resumed below. + if (useEmuThread) + EmuThreadPause(); check_variables(PSP_CoreParameter()); - else + } else check_dynamic_variables(PSP_CoreParameter()); // Process input. @@ -1719,13 +1769,8 @@ void retro_run(void) { // Handle thread pumping. if (useEmuThread) { - if (emuThreadState == EmuThreadState::PAUSED || - emuThreadState == EmuThreadState::PAUSE_REQUESTED) { - VsyncSwapIntervalDetect(); - ctx->SwapBuffers(); - return; - } - + // Also resumes from PAUSED, so a retro_serialize_size() without a + // following retro_serialize() cannot freeze emulation. if (emuThreadState != EmuThreadState::RUNNING) { EmuThreadStart(); } @@ -1779,15 +1824,18 @@ bool retro_serialize(void *data, size_t size) { EmuThreadPause(); // Does nothing if already paused } - size_t measuredSize; SaveState::SaveStart state; - auto err = CChunkFileReader::MeasureAndSavePtr(state, (u8 **)&data, &measuredSize); + // The buffer was sized by an earlier retro_serialize_size(); the state + // may have grown since, so never write past 'size'. + u8 *buffer = (u8 *)data; + size_t savedSize = size; + auto err = CChunkFileReader::MeasureAndSavePtr(state, &buffer, &savedSize); + if (err == CChunkFileReader::ERROR_BAD_ALLOC) + ERROR_LOG(Log::SaveState, "Savestate does not fit in the frontend's buffer (%d bytes)", (int)size); bool retVal = err == CChunkFileReader::ERROR_NONE; - if (useEmuThread) { + if (useEmuThread) EmuThreadStart(); - sleep_ms(4, "libretro-serialize"); - } return retVal; } @@ -1796,9 +1844,13 @@ bool retro_unserialize(const void *data, size_t size) { // The HW renderer isn't ready on first pass. // So we save the data until we are ready to use it. if (!gpu) { - unserialize_data = malloc(size); - memcpy(unserialize_data, data, size); + void *buffer = malloc(size); + if (!buffer) + return false; + free(unserialize_data); + unserialize_data = buffer; unserialize_size = size; + memcpy(unserialize_data, data, size); return true; } @@ -1811,31 +1863,32 @@ bool retro_unserialize(const void *data, size_t size) { bool retVal = CChunkFileReader::LoadPtr((u8 *)data, size, state, &errorString) == CChunkFileReader::ERROR_NONE; - if (useEmuThread) { + if (useEmuThread) EmuThreadStart(); - sleep_ms(4, "libretro-unserialize"); - } return retVal; } void *retro_get_memory_data(unsigned id) { - if ( id == RETRO_MEMORY_SYSTEM_RAM ) - return Memory::GetPointerWriteUnchecked(PSP_GetKernelMemoryBase()) ; + // Frontends query this right after retro_load_game(), before the + // deferred boot in retro_run() has mapped PSP memory. + if (id == RETRO_MEMORY_SYSTEM_RAM && Memory::IsActive()) + return Memory::GetPointerWriteUnchecked(PSP_GetKernelMemoryBase()); return NULL; } size_t retro_get_memory_size(unsigned id) { - if ( id == RETRO_MEMORY_SYSTEM_RAM ) - return Memory::g_MemorySize ; + if (id == RETRO_MEMORY_SYSTEM_RAM && Memory::IsActive()) + return Memory::g_MemorySize; return 0; } void retro_cheat_reset(void) { // Init Cheat Engine - CWCheatEngine *cheatEngine = new CWCheatEngine(g_paramSFO.GetDiscID()); + CWCheatEngine cheatEngineObj(g_paramSFO.GetDiscID()); + CWCheatEngine *cheatEngine = &cheatEngineObj; Path file=cheatEngine->CheatFilename(); // Output cheats to cheat file @@ -1857,7 +1910,8 @@ void retro_cheat_reset(void) { void retro_cheat_set(unsigned index, bool enabled, const char *code) { // Initialize Cheat Engine - CWCheatEngine *cheatEngine = new CWCheatEngine(g_paramSFO.GetDiscID()); + CWCheatEngine cheatEngineObj(g_paramSFO.GetDiscID()); + CWCheatEngine *cheatEngine = &cheatEngineObj; cheatEngine->CreateCheatFile(); Path file=cheatEngine->CheatFilename(); @@ -2002,26 +2056,22 @@ inline int16_t Clamp16(int32_t sample) { void System_AudioPushSamples(const int32_t *audio, int numSamples, float volume) { // We ignore volume here, because it's handled by libretro presumably. + if (!audio || numSamples <= 0) + return; + std::lock_guard lock(output_audio_buffer_mutex); - // Convert to 16-bit audio for further processing. - int16_t buffer[1024 * 2]; - int origSamples = numSamples * 2; - - while (numSamples > 0) { - int blockSize = std::min(1024, numSamples); - for (int i = 0; i < blockSize; i++) { - buffer[i * 2] = Clamp16(audio[i * 2]); - buffer[i * 2 + 1] = Clamp16(audio[i * 2 + 1]); - } - - numSamples -= blockSize; + int32_t values = numSamples * 2; // stereo + if (output_audio_buffer.capacity - output_audio_buffer.size < values) { + if (!ensure_output_audio_buffer_capacity((int32_t)((output_audio_buffer.capacity + values) * 1.5f))) + return; } - if (output_audio_buffer.capacity - output_audio_buffer.size < origSamples) - ensure_output_audio_buffer_capacity((output_audio_buffer.capacity + origSamples) * 1.5); - memcpy(output_audio_buffer.data + output_audio_buffer.size, buffer, origSamples * sizeof(*output_audio_buffer.data)); - output_audio_buffer.size += origSamples; + // Convert straight into the output buffer. + int16_t *dst = output_audio_buffer.data + output_audio_buffer.size; + for (int32_t i = 0; i < values; i++) + dst[i] = Clamp16(audio[i]); + output_audio_buffer.size += values; } void System_AudioGetDebugStats(char *buf, size_t bufSize) { if (buf) buf[0] = '\0'; }