mirror of
https://github.com/YACReader/yacreader
synced 2025-06-04 01:28:55 -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;
|
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()
|
PdfiumComic::PdfiumComic()
|
||||||
{
|
{
|
||||||
|
QMutexLocker locker(&pdfmutex);
|
||||||
|
if (++refcount == 1) {
|
||||||
FPDF_InitLibrary();
|
FPDF_InitLibrary();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
PdfiumComic::~PdfiumComic()
|
PdfiumComic::~PdfiumComic()
|
||||||
{
|
{
|
||||||
|
QMutexLocker locker(&pdfmutex);
|
||||||
if (doc)
|
if (doc)
|
||||||
{
|
{
|
||||||
FPDF_CloseDocument(doc);
|
FPDF_CloseDocument(doc);
|
||||||
}
|
}
|
||||||
|
if (--refcount == 0) {
|
||||||
FPDF_DestroyLibrary();
|
FPDF_DestroyLibrary();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool PdfiumComic::openComic(const QString & path)
|
bool PdfiumComic::openComic(const QString & path)
|
||||||
{
|
{
|
||||||
@ -50,6 +62,7 @@ bool PdfiumComic::openComic(const QString & path)
|
|||||||
fileAccess.m_GetBlock = pdfRead;
|
fileAccess.m_GetBlock = pdfRead;
|
||||||
fileAccess.m_Param = &pdfFile;
|
fileAccess.m_Param = &pdfFile;
|
||||||
|
|
||||||
|
QMutexLocker lock(&pdfmutex);
|
||||||
doc = FPDF_LoadCustomDocument(&fileAccess, NULL);
|
doc = FPDF_LoadCustomDocument(&fileAccess, NULL);
|
||||||
if (doc)
|
if (doc)
|
||||||
{
|
{
|
||||||
@ -64,6 +77,7 @@ bool PdfiumComic::openComic(const QString & path)
|
|||||||
|
|
||||||
void PdfiumComic::closeComic()
|
void PdfiumComic::closeComic()
|
||||||
{
|
{
|
||||||
|
QMutexLocker locker(&pdfmutex);
|
||||||
FPDF_CloseDocument(doc);
|
FPDF_CloseDocument(doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,6 +85,7 @@ unsigned int PdfiumComic::numPages()
|
|||||||
{
|
{
|
||||||
if (doc)
|
if (doc)
|
||||||
{
|
{
|
||||||
|
QMutexLocker locker(&pdfmutex);
|
||||||
return FPDF_GetPageCount(doc);
|
return FPDF_GetPageCount(doc);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -90,10 +105,13 @@ QImage PdfiumComic::getPage(const int page)
|
|||||||
FPDF_PAGE pdfpage;
|
FPDF_PAGE pdfpage;
|
||||||
FPDF_BITMAP bitmap;
|
FPDF_BITMAP bitmap;
|
||||||
|
|
||||||
|
QMutexLocker locker(&pdfmutex);
|
||||||
pdfpage = FPDF_LoadPage(doc, page);
|
pdfpage = FPDF_LoadPage(doc, page);
|
||||||
|
|
||||||
if (!pdfpage)
|
if (!pdfpage)
|
||||||
{
|
{
|
||||||
|
// TODO report error
|
||||||
|
qDebug() << FPDF_GetLastError();
|
||||||
return QImage();
|
return QImage();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,6 +122,8 @@ QImage PdfiumComic::getPage(const int page)
|
|||||||
image = QImage(width, height, QImage::Format_ARGB32);// QImage::Format_RGBX8888);
|
image = QImage(width, height, QImage::Format_ARGB32);// QImage::Format_RGBX8888);
|
||||||
if (image.isNull())
|
if (image.isNull())
|
||||||
{
|
{
|
||||||
|
// TODO report OOM error
|
||||||
|
qDebug() << "Image too large, OOM";
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
image.fill(0xFFFFFFFF);
|
image.fill(0xFFFFFFFF);
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QImage>
|
#include <QImage>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
|
#include <QMutex>
|
||||||
|
|
||||||
#if defined Q_OS_MAC && defined USE_PDFKIT
|
#if defined Q_OS_MAC && defined USE_PDFKIT
|
||||||
class MacOSXPDFComic
|
class MacOSXPDFComic
|
||||||
@ -36,6 +37,8 @@ class PdfiumComic
|
|||||||
QImage getPage(const int page);
|
QImage getPage(const int page);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
static int refcount;
|
||||||
|
static QMutex pdfmutex;
|
||||||
FPDF_LIBRARY_CONFIG config;
|
FPDF_LIBRARY_CONFIG config;
|
||||||
FPDF_DOCUMENT doc;
|
FPDF_DOCUMENT doc;
|
||||||
FPDF_FILEACCESS fileAccess;
|
FPDF_FILEACCESS fileAccess;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user