mirror of
https://github.com/YACReader/yacreader
synced 2025-06-03 17:18:23 -04:00
Pdfium isn't threadsafe? Pdfium gets mutexed!
This commit is contained in:
parent
760c1b3847
commit
e935281b47
@ -22,19 +22,31 @@ int pdfRead(void* param,
|
||||
return 0;
|
||||
}
|
||||
|
||||
// pdfium is not threadsafe
|
||||
// We need to use mutex locking & refcounting to avoid crashes
|
||||
|
||||
int PdfiumComic::refcount = 0;
|
||||
QMutex PdfiumComic::pdfmutex;
|
||||
|
||||
PdfiumComic::PdfiumComic()
|
||||
{
|
||||
QMutexLocker locker(&pdfmutex);
|
||||
if (++refcount == 1) {
|
||||
FPDF_InitLibrary();
|
||||
}
|
||||
}
|
||||
|
||||
PdfiumComic::~PdfiumComic()
|
||||
{
|
||||
QMutexLocker locker(&pdfmutex);
|
||||
if (doc)
|
||||
{
|
||||
FPDF_CloseDocument(doc);
|
||||
}
|
||||
if (--refcount == 0) {
|
||||
FPDF_DestroyLibrary();
|
||||
}
|
||||
}
|
||||
|
||||
bool PdfiumComic::openComic(const QString & path)
|
||||
{
|
||||
@ -50,6 +62,7 @@ bool PdfiumComic::openComic(const QString & path)
|
||||
fileAccess.m_GetBlock = pdfRead;
|
||||
fileAccess.m_Param = &pdfFile;
|
||||
|
||||
QMutexLocker lock(&pdfmutex);
|
||||
doc = FPDF_LoadCustomDocument(&fileAccess, NULL);
|
||||
if (doc)
|
||||
{
|
||||
@ -64,6 +77,7 @@ bool PdfiumComic::openComic(const QString & path)
|
||||
|
||||
void PdfiumComic::closeComic()
|
||||
{
|
||||
QMutexLocker locker(&pdfmutex);
|
||||
FPDF_CloseDocument(doc);
|
||||
}
|
||||
|
||||
@ -71,6 +85,7 @@ unsigned int PdfiumComic::numPages()
|
||||
{
|
||||
if (doc)
|
||||
{
|
||||
QMutexLocker locker(&pdfmutex);
|
||||
return FPDF_GetPageCount(doc);
|
||||
}
|
||||
else
|
||||
@ -90,10 +105,13 @@ QImage PdfiumComic::getPage(const int page)
|
||||
FPDF_PAGE pdfpage;
|
||||
FPDF_BITMAP bitmap;
|
||||
|
||||
QMutexLocker locker(&pdfmutex);
|
||||
pdfpage = FPDF_LoadPage(doc, page);
|
||||
|
||||
if (!pdfpage)
|
||||
{
|
||||
// TODO report error
|
||||
qDebug() << FPDF_GetLastError();
|
||||
return QImage();
|
||||
}
|
||||
|
||||
@ -104,6 +122,8 @@ QImage PdfiumComic::getPage(const int page)
|
||||
image = QImage(width, height, QImage::Format_ARGB32);// QImage::Format_RGBX8888);
|
||||
if (image.isNull())
|
||||
{
|
||||
// TODO report OOM error
|
||||
qDebug() << "Image too large, OOM";
|
||||
return image;
|
||||
}
|
||||
image.fill(0xFFFFFFFF);
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <QObject>
|
||||
#include <QImage>
|
||||
#include <QFile>
|
||||
#include <QMutex>
|
||||
|
||||
#if defined Q_OS_MAC && defined USE_PDFKIT
|
||||
class MacOSXPDFComic
|
||||
@ -36,6 +37,8 @@ class PdfiumComic
|
||||
QImage getPage(const int page);
|
||||
|
||||
private:
|
||||
static int refcount;
|
||||
static QMutex pdfmutex;
|
||||
FPDF_LIBRARY_CONFIG config;
|
||||
FPDF_DOCUMENT doc;
|
||||
FPDF_FILEACCESS fileAccess;
|
||||
|
Loading…
x
Reference in New Issue
Block a user