CDImage: Add Precache() method

This commit is contained in:
Connor McLaughlin 2022-04-03 20:55:27 +10:00
parent ec9411c0a7
commit 9ab64ecdb3
3 changed files with 31 additions and 5 deletions

View file

@ -324,6 +324,11 @@ std::string CDImage::GetSubImageMetadata(u32 index, const std::string_view& type
return {};
}
CDImage::PrecacheResult CDImage::Precache(ProgressCallback* progress /*= ProgressCallback::NullProgressCallback*/)
{
return PrecacheResult::Unsupported;
}
void CDImage::ClearTOC()
{
m_lba_count = 0;

View file

@ -57,6 +57,13 @@ public:
Mode2Raw // 2352 bytes per sector
};
enum class PrecacheResult : u8
{
Unsupported,
ReadError,
Success,
};
struct SectorHeader
{
u8 minute;
@ -294,6 +301,9 @@ public:
// Retrieve sub-image metadata.
virtual std::string GetSubImageMetadata(u32 index, const std::string_view& type) const;
// Returns true if the source supports precaching, which may be more optimal than an in-memory copy.
virtual PrecacheResult Precache(ProgressCallback* progress = ProgressCallback::NullProgressCallback);
protected:
void ClearTOC();
void CopyTOC(const CDImage* image);

View file

@ -683,11 +683,22 @@ std::unique_ptr<CDImage> OpenCDImage(const char* path, Common::Error* error, boo
else
{
HostInterfaceProgressCallback callback;
std::unique_ptr<CDImage> memory_image = CDImage::CreateMemoryImage(media.get(), &callback);
if (memory_image)
media = std::move(memory_image);
else
Log_WarningPrintf("Failed to preload image '%s' to RAM", path);
const CDImage::PrecacheResult res = media->Precache(&callback);
if (res == CDImage::PrecacheResult::Unsupported)
{
// fall back to copy precaching
std::unique_ptr<CDImage> memory_image = CDImage::CreateMemoryImage(media.get(), &callback);
if (memory_image)
media = std::move(memory_image);
else
Log_WarningPrintf("Failed to preload image '%s' to RAM", path);
}
else if (res != CDImage::PrecacheResult::Success)
{
g_host_interface->AddOSDMessage(
g_host_interface->TranslateStdString("OSDMessage", "Precaching CD image failed, it may be unreliable."),
15.0f);
}
}
}