CDVD: Make CHD file reader threaded

This commit is contained in:
TellowKrinkle 2021-03-24 20:38:47 -05:00 committed by lightningterror
parent ae945f49e3
commit c39598b98d
2 changed files with 44 additions and 79 deletions

View File

@ -30,7 +30,7 @@ bool ChdFileReader::CanHandle(const wxString& fileName)
return true; return true;
} }
bool ChdFileReader::Open(const wxString& fileName) bool ChdFileReader::Open2(const wxString& fileName)
{ {
m_filename = fileName; m_filename = fileName;
@ -119,65 +119,49 @@ bool ChdFileReader::Open(const wxString& fileName)
} }
// const chd_header *header = chd_get_header(ChdFile); // const chd_header *header = chd_get_header(ChdFile);
sector_size = header->unitbytes; file_size = static_cast<u64>(header->unitbytes) * header->unitcount;
sector_count = header->unitcount; hunk_size = header->hunkbytes;
sectors_per_hunk = header->hunkbytes / sector_size; // CHD likes to use full 2448 byte blocks, but keeps the +24 offset of source ISOs
hunk_buffer = new u8[header->hunkbytes]; // The rest of PCSX2 likes to use 2448 byte buffers, which can't fit that so trim blocks instead
current_hunk = -1; m_internalBlockSize = header->unitbytes;
delete header; delete header;
return true; return true;
} }
int ChdFileReader::ReadSync(void* pBuffer, uint sector, uint count) ThreadedFileReader::Chunk ChdFileReader::ChunkForOffset(u64 offset)
{ {
u8* dst = (u8*)pBuffer; Chunk chunk = {0};
u32 hunk = sector / sectors_per_hunk; if (offset >= file_size)
u32 sector_in_hunk = sector % sectors_per_hunk;
chd_error error;
for (uint i = 0; i < count; i++)
{ {
if (current_hunk != hunk) chunk.chunkID = -1;
{
error = chd_read(ChdFile, hunk, hunk_buffer);
if (error != CHDERR_NONE)
{
Console.Error(L"chd_read return error: %s", chd_error_string(error));
// return i * m_blocksize;
}
current_hunk = hunk;
}
memcpy(dst + i * m_blocksize, hunk_buffer + sector_in_hunk * sector_size, m_blocksize);
sector_in_hunk++;
if (sector_in_hunk >= sectors_per_hunk)
{
hunk++;
sector_in_hunk = 0;
}
} }
return m_blocksize * count; else
}
void ChdFileReader::BeginRead(void* pBuffer, uint sector, uint count)
{
// TODO: Check if libchdr can read asynchronously
async_read = ReadSync(pBuffer, sector, count);
}
int ChdFileReader::FinishRead()
{
return async_read;
}
void ChdFileReader::Close()
{
if (hunk_buffer != NULL)
{ {
//free(hunk_buffer); chunk.chunkID = offset / hunk_size;
delete[] hunk_buffer; chunk.length = hunk_size;
hunk_buffer = NULL; chunk.offset = chunk.chunkID * hunk_size;
} }
return chunk;
}
int ChdFileReader::ReadChunk(void *dst, s64 chunkID)
{
if (chunkID < 0)
return -1;
chd_error error = chd_read(ChdFile, chunkID, dst);
if (error != CHDERR_NONE)
{
Console.Error(L"chd_read returned error: %s", chd_error_string(error));
return 0;
}
return hunk_size;
}
void ChdFileReader::Close2()
{
if (ChdFile != NULL) if (ChdFile != NULL)
{ {
chd_close(ChdFile); chd_close(ChdFile);
@ -185,22 +169,12 @@ void ChdFileReader::Close()
} }
} }
uint ChdFileReader::GetBlockSize() const
{
return m_blocksize;
}
void ChdFileReader::SetBlockSize(uint blocksize)
{
m_blocksize = blocksize;
}
u32 ChdFileReader::GetBlockCount() const u32 ChdFileReader::GetBlockCount() const
{ {
return sector_count; return (file_size - m_dataoffset) / m_internalBlockSize;
} }
ChdFileReader::ChdFileReader(void) ChdFileReader::ChdFileReader(void)
{ {
m_blocksize = 2048;
ChdFile = NULL; ChdFile = NULL;
hunk_buffer = NULL;
}; };

View File

@ -14,10 +14,10 @@
*/ */
#pragma once #pragma once
#include "AsyncFileReader.h" #include "ThreadedFileReader.h"
#include "libchdr/chd.h" #include "libchdr/chd.h"
class ChdFileReader : public AsyncFileReader class ChdFileReader : public ThreadedFileReader
{ {
DeclareNoncopyableObject(ChdFileReader); DeclareNoncopyableObject(ChdFileReader);
@ -25,26 +25,17 @@ public:
virtual ~ChdFileReader(void) { Close(); }; virtual ~ChdFileReader(void) { Close(); };
static bool CanHandle(const wxString& fileName); static bool CanHandle(const wxString& fileName);
bool Open(const wxString& fileName) override; bool Open2(const wxString& fileName) override;
int ReadSync(void* pBuffer, uint sector, uint count) override; Chunk ChunkForOffset(u64 offset) override;
int ReadChunk(void *dst, s64 blockID) override;
void BeginRead(void* pBuffer, uint sector, uint count) override; void Close2(void) override;
int FinishRead(void) override;
void CancelRead(void) override{};
void Close(void) override;
void SetBlockSize(uint blocksize);
uint GetBlockSize() const;
uint GetBlockCount(void) const override; uint GetBlockCount(void) const override;
ChdFileReader(void); ChdFileReader(void);
private: private:
chd_file* ChdFile; chd_file* ChdFile;
u8* hunk_buffer; u64 file_size;
u32 sector_size; u32 hunk_size;
u32 sector_count;
u32 sectors_per_hunk;
u32 current_hunk;
u32 async_read;
}; };