mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-10-21 10:19:04 +02:00
A rewind used up the state it restored, so rewinding, playing on a bit and rewinding again went further back than the state you just came from. Now the state stays, and only a rewind within 0.8 seconds of the last one steps back past it. "Seconds ago" was wall-clock time since the snapshot, which counted pauses, fast-forward and time spent in states rewound to earlier. It's now emulated time from where the rewinding started. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
98 lines
2.9 KiB
C++
98 lines
2.9 KiB
C++
#pragma once
|
|
|
|
#include <mutex>
|
|
#include <thread>
|
|
#include "Common/Serialize/Serializer.h"
|
|
#include "Common/CommonTypes.h"
|
|
#include "Common/TimeUtil.h"
|
|
|
|
namespace SaveState {
|
|
|
|
// This ring buffer of states is for rewind save states, which are kept in RAM.
|
|
// Save states are compressed against one of two reference saves (bases_), and the reference
|
|
// is switched to a fresh save every N saves, where N is BASE_USAGE_INTERVAL.
|
|
// The compression is a simple block based scheme where 0 means to copy a block from the base,
|
|
// and 1 means that the following bytes are the next block. See Compress/LockedDecompress.
|
|
class StateRingbuffer {
|
|
public:
|
|
StateRingbuffer() {
|
|
size_ = REWIND_NUM_STATES;
|
|
states_.resize(size_);
|
|
baseMapping_.resize(size_);
|
|
}
|
|
|
|
~StateRingbuffer() {
|
|
if (compressThread_.joinable()) {
|
|
compressThread_.join();
|
|
}
|
|
}
|
|
|
|
CChunkFileReader::Error Save();
|
|
CChunkFileReader::Error Restore(std::string *errorString, std::string *metadata);
|
|
void ScheduleCompress(std::vector<u8> *result, const std::vector<u8> *state, const std::vector<u8> *base);
|
|
void Compress(std::vector<u8> &result, const std::vector<u8> &state, const std::vector<u8> &base);
|
|
void LockedDecompress(std::vector<u8> &result, const std::vector<u8> &compressed, const std::vector<u8> &base);
|
|
void Clear();
|
|
|
|
bool Empty() const {
|
|
return count_ == 0;
|
|
}
|
|
|
|
void Process();
|
|
void NotifyState();
|
|
|
|
double NextStateTimestamp() const;
|
|
|
|
private:
|
|
bool BaseAlive(int generation) const;
|
|
void DropNewest();
|
|
|
|
const int BLOCK_SIZE = 8192;
|
|
const int REWIND_NUM_STATES = 20;
|
|
// TODO: Instead, based on size of compressed state?
|
|
const int BASE_USAGE_INTERVAL = 15;
|
|
// A rewind this soon after the last one steps back past the state that one restored.
|
|
const double STEP_BACK_SECONDS = 0.8;
|
|
|
|
typedef std::vector<u8> StateBuffer;
|
|
|
|
struct RewindState {
|
|
StateBuffer stateBuffer;
|
|
// CoreTiming's clock when the state was taken, in seconds.
|
|
double emuTime = 0.0;
|
|
|
|
bool empty() const { return stateBuffer.empty(); }
|
|
void clear() { stateBuffer.clear(); emuTime = 0.0; }
|
|
};
|
|
|
|
// The slot the next save goes in, and how many slots before it hold states we can restore.
|
|
int next_ = 0;
|
|
int count_ = 0;
|
|
int size_;
|
|
|
|
std::vector<RewindState> states_;
|
|
StateBuffer bases_[2];
|
|
// Which generation each base slot currently holds, and which generation each state was
|
|
// compressed against. There are more states than bases, so states do go stale.
|
|
int baseGeneration_[2] = {-1, -1};
|
|
int nextBaseGeneration_ = 0;
|
|
std::vector<int> baseMapping_;
|
|
std::mutex lock_;
|
|
std::thread compressThread_;
|
|
std::vector<u8> buffer_;
|
|
|
|
int base_ = -1;
|
|
int baseUsage_ = 0;
|
|
|
|
double rewindLastTime_ = 0.0f;
|
|
|
|
// Whether the newest state is the one the last rewind restored, and when that was.
|
|
bool newestRestored_ = false;
|
|
double lastRestoreTime_ = 0.0;
|
|
// CoreTiming's clock when the current run of rewinds started, for "seconds ago".
|
|
double rewindFromEmuTime_ = 0.0;
|
|
bool retryingAfterFailure_ = false;
|
|
};
|
|
|
|
} // namespace SaveState
|