Commit a61a8f94 authored by Jean-Baptiste Mardelle's avatar Jean-Baptiste Mardelle
Browse files

Small refactoring of cache get thumbnail

parent 9f51aeb7
......@@ -1154,13 +1154,13 @@ QPoint ProjectClip::zone() const
return ClipController::zone();
}
const QString ProjectClip::hash()
const QString ProjectClip::hash(bool createIfEmpty)
{
QString clipHash = getProducerProperty(QStringLiteral("kdenlive:file_hash"));
if (!clipHash.isEmpty()) {
return clipHash;
}
return getFileHash();
return createIfEmpty ? getFileHash() : QString();
}
const QByteArray ProjectClip::getFolderHash(const QDir &dir, QString fileName)
......@@ -1624,7 +1624,7 @@ const QString ProjectClip::getAudioThumbPath(int stream)
if (!ok) {
return QString();
}
const QString clipHash = hash();
const QString clipHash = hash(false);
if (clipHash.isEmpty()) {
return QString();
}
......@@ -1907,7 +1907,10 @@ void ProjectClip::getThumbFromPercent(int percent, bool storeFrame)
if (percent < 0) {
if (hasProducerProperty(QStringLiteral("kdenlive:thumbnailFrame"))) {
int framePos = qMax(0, getProducerIntProperty(QStringLiteral("kdenlive:thumbnailFrame")));
setThumbnail(ThumbnailCache::get()->getThumbnail(m_binId, framePos), -1, -1);
QImage thumb = ThumbnailCache::get()->getThumbnail(hash(false), m_binId, framePos);
if (!thumb.isNull()) {
setThumbnail(thumb, -1, -1);
}
}
return;
}
......@@ -1915,11 +1918,14 @@ void ProjectClip::getThumbFromPercent(int percent, bool storeFrame)
int steps = qCeil(qMax(pCore->getCurrentFps(), double(duration) / 30));
int framePos = duration * percent / 100;
framePos -= framePos%steps;
if (ThumbnailCache::get()->hasThumbnail(m_binId, framePos)) {
setThumbnail(ThumbnailCache::get()->getThumbnail(m_binId, framePos), -1, -1);
QImage thumb = ThumbnailCache::get()->getThumbnail(hash(false), m_binId, framePos);
if (!thumb.isNull()) {
setThumbnail(thumb, -1, -1);
} else {
// Generate percent thumbs
CacheTask::start({ObjectType::BinClip,m_binId.toInt()}, 30, 0, 0, this);
if (!pCore->taskManager.hasPendingJob({ObjectType::BinClip, m_binId.toInt()}, AbstractTask::CACHEJOB)) {
CacheTask::start({ObjectType::BinClip,m_binId.toInt()}, 30, 0, 0, this);
}
}
if (storeFrame) {
setProducerProperty(QStringLiteral("kdenlive:thumbnailFrame"), framePos);
......
......@@ -144,7 +144,7 @@ public:
QString getToolTip() const override;
/** @brief The clip hash created from the clip's resource. */
const QString hash();
const QString hash(bool createIfEmpty = true);
/** @brief Callculate a file hash from a path. */
static const QPair<QByteArray, qint64> calculateHash(const QString &path);
......
......@@ -232,11 +232,11 @@ void ClipLoadTask::generateThumbnail(std::shared_ptr<ProjectClip>binClip, std::s
qDebug()<<"===== \nREADY FOR THUMB"<<binClip->clipType()<<"\n\n=========";
int frameNumber = m_in > -1 ? m_in : qMax(0, binClip->getProducerIntProperty(QStringLiteral("kdenlive:thumbnailFrame")));
if (producer->get_int("video_index") > -1) {
if (ThumbnailCache::get()->hasThumbnail(QString::number(m_owner.second), frameNumber, false)) {
QImage thumb = ThumbnailCache::get()->getThumbnail(binClip->hash(false), QString::number(m_owner.second), frameNumber);
if (!thumb.isNull()) {
// Thumbnail found in cache
QImage result = ThumbnailCache::get()->getThumbnail(QString::number(m_owner.second), frameNumber);
qDebug()<<"=== FOUND THUMB IN CACHe";
QMetaObject::invokeMethod(binClip.get(), "setThumbnail", Qt::QueuedConnection, Q_ARG(QImage,result), Q_ARG(int,m_in), Q_ARG(int,m_out), Q_ARG(bool,true));
QMetaObject::invokeMethod(binClip.get(), "setThumbnail", Qt::QueuedConnection, Q_ARG(QImage,thumb), Q_ARG(int,m_in), Q_ARG(int,m_out), Q_ARG(bool,true));
} else {
QString mltService = producer->get("mlt_service");
const QString mltResource = producer->get("resource");
......
......@@ -33,13 +33,13 @@ QImage ThumbnailProvider::requestImage(const QString &id, QSize *size, const QSi
bool ok;
int frameNumber = id.section('#', -1).toInt(&ok);
if (ok) {
if (ThumbnailCache::get()->hasThumbnail(binId, frameNumber, false)) {
result = ThumbnailCache::get()->getThumbnail(binId, frameNumber);
*size = result.size();
return result;
}
std::shared_ptr<ProjectClip> binClip = pCore->projectItemModel()->getClipByBinID(binId);
if (binClip) {
result = ThumbnailCache::get()->getThumbnail(binClip->hash(false), binId, frameNumber);
if (!result.isNull()) {
*size = result.size();
return result;
}
std::shared_ptr<Mlt::Producer> prod = binClip->thumbProducer();
if (prod && prod->is_valid()) {
result = makeThumbnail(prod, frameNumber, requestedSize);
......
......@@ -142,6 +142,30 @@ const QList <QUrl> ThumbnailCache::getAudioThumbPath(const QString &binId) const
return pathList;
}
QImage ThumbnailCache::getThumbnail(QString hash, const QString &binId, int pos, bool volatileOnly) const
{
if (hash.isEmpty()) {
return QImage();
}
hash.append(QString("#%1.jpg").arg(pos));
QMutexLocker locker(&m_mutex);
if (m_volatileCache->contains(hash)) {
return m_volatileCache->get(hash);
}
if (volatileOnly) {
return QImage();
}
bool ok = false;
QDir thumbFolder = getDir(false, &ok);
if (ok && thumbFolder.exists(hash)) {
if(m_storedOnDisk.find(binId) == m_storedOnDisk.end() || std::find(m_storedOnDisk[binId].begin(), m_storedOnDisk[binId].end(), pos) == m_storedOnDisk[binId].end()) {
m_storedOnDisk[binId].push_back(pos);
}
return QImage(thumbFolder.absoluteFilePath(hash));
}
return QImage();
}
QImage ThumbnailCache::getThumbnail(const QString &binId, int pos, bool volatileOnly) const
{
QMutexLocker locker(&m_mutex);
......
......@@ -43,6 +43,7 @@ public:
@param pos is the position where we query
@param volatileOnly if true, we only check the volatile cache (no disk access)
*/
QImage getThumbnail(QString hash, const QString &binId, int pos, bool volatileOnly = false) const;
QImage getThumbnail(const QString &binId, int pos, bool volatileOnly = false) const;
QImage getAudioThumbnail(const QString &binId, bool volatileOnly = false) const;
const QList <QUrl> getAudioThumbPath(const QString &binId) const;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment