Files
ppsspp/libretro/LibretroGraphicsContext.h
T
libretroadmin f1208a7a5a Make threading lock-free
The core and the headless tools no longer use mutexes, condition variables,
spinlocks, std::atomic, std::thread or volatile flags. Threads hand off through
libretro-common's retro_atomic and rthreads plus a few small pieces on top of
them in Common/Thread: Thread, MpscQueue, EventCounter, ParkingLot waits and
ReaderGate. Every wait blocks until it's woken; nothing polls on a timer.

State that only one thread touches just loses its lock. The PSP filesystem,
sceIo and the savedata dialog now belong to the emulation thread: async reads
and writes run when they're queued and complete later in emulated time, and
the IO and savedata threads are gone. The friend finder only talks to the
adhoc server and hands what it gets to the emulation thread. GPU init always
runs on the thread that starts the game.

The Vulkan render and compile queues, pipeline and render pass caches, delete
list and SPIR-V cache, the GL render queue, the software renderer's jit caches,
the file loader caches, the web debugger, UPnP, input, savestates, logging and
the GE debugger all hand off through queues and atomics. The IR jit's spinning
profiler thread goes.

A CPU thread with nothing to run now sleeps until work is queued instead of
spinning. Headless with --debugger waits for a client while stopped, and ends
the run once every client that connected has left. Web server shutdown keeps
servicing the CPU queue until the server has finished.
2026-10-09 04:56:16 +02:00

129 lines
3.6 KiB
C++

#pragma once
#include <retro_atomic.h>
#include <libretro.h>
#include "Common/GPU/GraphicsContext.h"
#include "Common/GPU/thin3d_create.h"
#include "Common/CommonTypes.h"
#include "Core/Config.h"
#include "Core/System.h"
#include "GPU/GPUState.h"
#include "GPU/Software/SoftGpu.h"
#include "headless/Compare.h"
#include "Common/Data/Convert/ColorConv.h"
#define NATIVEWIDTH 480
#define NATIVEHEIGHT 272
class LibretroGraphicsContext : public GraphicsContext {
public:
LibretroGraphicsContext() {}
~LibretroGraphicsContext() override { ShutdownAPI(); }
virtual void SetRenderTarget() {}
virtual GPUCore GetGPUCore() = 0;
virtual const char *Ident() = 0;
void ShutdownAPI() override {
DestroyDrawContext();
}
virtual void SwapBuffers() = 0;
void Resize() override {}
// Emu thread backends only: the emu thread queues a marker once parked, and
// the frontend thread runs the queue up to it. False means the emu thread exited.
virtual void NotifyEmuThreadPaused() {}
virtual bool ThreadRunUntilPaused() { return true; }
virtual void GotBackbuffer();
virtual void LostBackbuffer();
virtual void CreateDrawContext() {}
virtual void DestroyDrawContext() {
if (!draw_) {
return;
}
delete draw_;
draw_ = nullptr;
}
Draw::DrawContext *GetDrawContext() override { return draw_; }
static LibretroGraphicsContext *CreateGraphicsContext();
static retro_video_refresh_t video_cb;
protected:
Draw::DrawContext *draw_ = nullptr;
};
class LibretroHWRenderContext : public LibretroGraphicsContext {
public:
LibretroHWRenderContext(retro_hw_context_type context_type, unsigned version_major = 0, unsigned version_minor = 0);
bool InitHW(bool cache_context);
void SetRenderTarget() override {}
void SwapBuffers() override {
video_cb(RETRO_HW_FRAME_BUFFER_VALID, PSP_CoreParameter().pixelWidth, PSP_CoreParameter().pixelHeight, 0);
}
virtual void ContextReset();
virtual void ContextDestroy();
protected:
retro_hw_render_callback hw_render_ = {};
};
std::vector<u32> ConvertFramebufferForLibretro(const GPUDebugBuffer *buffer, u32 stride, u32 h);
class LibretroSoftwareContext : public LibretroGraphicsContext {
public:
LibretroSoftwareContext() {}
void SwapBuffers() override {
u32 w = NATIVEWIDTH;
u32 h = NATIVEHEIGHT;
if (!gpu) {
// Nothing rendered yet, dupe the previous frame.
video_cb(NULL, w, h, w * sizeof(u32));
return;
}
GPUDebugBuffer buf;
gpu->GetOutputFramebuffer(buf);
// video_cb is synchronous, so the converted vector can be handed over directly.
const std::vector<u32> pixels = ConvertFramebufferForLibretro(&buf, w, h);
if (pixels.size() < (size_t)w * h) {
// Unsupported debug buffer format, conversion returned nothing.
video_cb(NULL, w, h, w * sizeof(u32));
return;
}
const u32 *data = pixels.data();
if (g_Config.bDisplayCropTo16x9) {
// 480x272 -> 480x270: skip the first row, drop the last.
data += w;
h -= 2;
}
video_cb(data, w, h, w * sizeof(u32));
}
GPUCore GetGPUCore() override { return GPUCORE_SOFTWARE; }
const char *Ident() override { return "Software"; }
};
namespace Libretro {
extern LibretroGraphicsContext *ctx;
extern retro_environment_t environ_cb;
extern retro_hw_context_type backend;
enum class EmuThreadState {
DISABLED,
RUNNING,
PAUSE_REQUESTED,
PAUSED,
QUIT_REQUESTED,
STOPPED,
};
extern bool useEmuThread;
extern retro_atomic_int_t emuThreadState;
inline EmuThreadState EmuThreadStateGet() { return (EmuThreadState)retro_atomic_load_acquire_int(&emuThreadState); }
inline void EmuThreadStateSet(EmuThreadState state) { retro_atomic_store_release_int(&emuThreadState, (int)state); }
void EmuThreadStart();
void EmuThreadStop();
void EmuThreadPause();
} // namespace Libretro