Add mutex to the Lensfun library access.

(try to fix #4580)
This commit is contained in:
Hombre 2018-07-14 15:52:43 +02:00
parent c30f03ea09
commit ca4f0e5239
2 changed files with 15 additions and 2 deletions

View File

@ -33,6 +33,7 @@ extern const Settings *settings;
LFModifier::~LFModifier() LFModifier::~LFModifier()
{ {
if (data_) { if (data_) {
MyMutex::MyLock lock(*lfModifierMutex);
data_->Destroy(); data_->Destroy();
} }
} }
@ -56,7 +57,7 @@ void LFModifier::correctDistortion(double &x, double &y, int cx, int cy, double
if (swap_xy_) { if (swap_xy_) {
std::swap(xx, yy); std::swap(xx, yy);
} }
if (data_->ApplyGeometryDistortion(xx, yy, 1, 1, pos)) { if (data_->ApplyGeometryDistortion(xx, yy, 1, 1, pos)) { // This is thread-safe
x = pos[0]; x = pos[0];
y = pos[1]; y = pos[1];
if (swap_xy_) { if (swap_xy_) {
@ -95,7 +96,7 @@ void LFModifier::correctCA(double &x, double &y, int cx, int cy, int channel) co
if (swap_xy_) { if (swap_xy_) {
std::swap(x, y); std::swap(x, y);
} }
data_->ApplySubpixelDistortion(x, y, 1, 1, pos); data_->ApplySubpixelDistortion(x, y, 1, 1, pos); // This is thread-safe
x = pos[2*channel]; x = pos[2*channel];
y = pos[2*channel+1]; y = pos[2*channel+1];
if (swap_xy_) { if (swap_xy_) {
@ -112,12 +113,14 @@ void LFModifier::correctCA(double &x, double &y, int cx, int cy, int channel) co
void LFModifier::processVignetteLine(int width, int y, float *line) const void LFModifier::processVignetteLine(int width, int y, float *line) const
{ {
MyMutex::MyLock lock(*lfModifierMutex);
data_->ApplyColorModification(line, 0, y, width, 1, LF_CR_1(INTENSITY), 0); data_->ApplyColorModification(line, 0, y, width, 1, LF_CR_1(INTENSITY), 0);
} }
void LFModifier::processVignetteLine3Channels(int width, int y, float *line) const void LFModifier::processVignetteLine3Channels(int width, int y, float *line) const
{ {
MyMutex::MyLock lock(*lfModifierMutex);
data_->ApplyColorModification(line, 0, y, width, 1, LF_CR_3(RED, GREEN, BLUE), 0); data_->ApplyColorModification(line, 0, y, width, 1, LF_CR_3(RED, GREEN, BLUE), 0);
} }
@ -157,6 +160,7 @@ LFModifier::LFModifier(lfModifier *m, bool swap_xy, int flags):
swap_xy_(swap_xy), swap_xy_(swap_xy),
flags_(flags) flags_(flags)
{ {
lfModifierMutex = new MyMutex;
} }
@ -374,12 +378,14 @@ bool LFDatabase::LoadDirectory(const char *dirname)
LFDatabase::LFDatabase(): LFDatabase::LFDatabase():
data_(nullptr) data_(nullptr)
{ {
lfDBMutex = new MyMutex;
} }
LFDatabase::~LFDatabase() LFDatabase::~LFDatabase()
{ {
if (data_) { if (data_) {
MyMutex::MyLock lock(*lfDBMutex);
data_->Destroy(); data_->Destroy();
} }
} }
@ -395,6 +401,7 @@ std::vector<LFCamera> LFDatabase::getCameras() const
{ {
std::vector<LFCamera> ret; std::vector<LFCamera> ret;
if (data_) { if (data_) {
MyMutex::MyLock lock(*lfDBMutex);
auto cams = data_->GetCameras(); auto cams = data_->GetCameras();
while (*cams) { while (*cams) {
ret.emplace_back(); ret.emplace_back();
@ -410,6 +417,7 @@ std::vector<LFLens> LFDatabase::getLenses() const
{ {
std::vector<LFLens> ret; std::vector<LFLens> ret;
if (data_) { if (data_) {
MyMutex::MyLock lock(*lfDBMutex);
auto lenses = data_->GetLenses(); auto lenses = data_->GetLenses();
while (*lenses) { while (*lenses) {
ret.emplace_back(); ret.emplace_back();
@ -425,6 +433,7 @@ LFCamera LFDatabase::findCamera(const Glib::ustring &make, const Glib::ustring &
{ {
LFCamera ret; LFCamera ret;
if (data_) { if (data_) {
MyMutex::MyLock lock(*lfDBMutex);
auto found = data_->FindCamerasExt(make.c_str(), model.c_str()); auto found = data_->FindCamerasExt(make.c_str(), model.c_str());
if (found) { if (found) {
ret.data_ = found[0]; ret.data_ = found[0];
@ -439,6 +448,7 @@ LFLens LFDatabase::findLens(const LFCamera &camera, const Glib::ustring &name) c
{ {
LFLens ret; LFLens ret;
if (data_) { if (data_) {
MyMutex::MyLock lock(*lfDBMutex);
auto found = data_->FindLenses(camera.data_, nullptr, name.c_str()); auto found = data_->FindLenses(camera.data_, nullptr, name.c_str());
for (size_t pos = 0; !found && pos < name.size(); ) { for (size_t pos = 0; !found && pos < name.size(); ) {
// try to split the maker from the model of the lens -- we have to // try to split the maker from the model of the lens -- we have to
@ -476,6 +486,7 @@ std::unique_ptr<LFModifier> LFDatabase::getModifier(const LFCamera &camera, cons
{ {
std::unique_ptr<LFModifier> ret; std::unique_ptr<LFModifier> ret;
if (data_) { if (data_) {
MyMutex::MyLock lock(*lfDBMutex);
if (camera && lens) { if (camera && lens) {
lfModifier *mod = lfModifier::Create(lens.data_, camera.getCropFactor(), width, height); lfModifier *mod = lfModifier::Create(lens.data_, camera.getCropFactor(), width, height);
int flags = LF_MODIFY_DISTORTION | LF_MODIFY_SCALE | LF_MODIFY_TCA; int flags = LF_MODIFY_DISTORTION | LF_MODIFY_SCALE | LF_MODIFY_TCA;

View File

@ -57,6 +57,7 @@ private:
lfModifier *data_; lfModifier *data_;
bool swap_xy_; bool swap_xy_;
int flags_; int flags_;
MyMutex *lfModifierMutex;
}; };
class LFCamera final class LFCamera final
@ -121,6 +122,7 @@ private:
LFDatabase(); LFDatabase();
bool LoadDirectory(const char *dirname); bool LoadDirectory(const char *dirname);
MyMutex *lfDBMutex;
static LFDatabase instance_; static LFDatabase instance_;
lfDatabase *data_; lfDatabase *data_;
}; };