]> git.sesse.net Git - kdenlive/blobdiff - src/kthumb.cpp
Small thumbs optimization
[kdenlive] / src / kthumb.cpp
index 2369dc37e0ac7f5813edcce0f7cbfd737b4a2a51..56ec4a5ed7080cf654b259d4f53a63a12e14496c 100644 (file)
@@ -70,20 +70,19 @@ KThumb::~KThumb()
 
 void KThumb::setProducer(Mlt::Producer *producer)
 {
+    m_mutex.lock();
     m_requestedThumbs.clear();
     m_intraFramesQueue.clear();
     m_future.waitForFinished();
     m_intra.waitForFinished();
-    m_mutex.lock();
     m_producer = producer;
-    m_mutex.unlock();
     // FIXME: the profile() call leaks an object, but trying to free
     // it leads to a double-free in Profile::~Profile()
     if (producer) {
         m_dar = producer->profile()->dar();
         m_ratio = (double) producer->profile()->width() / producer->profile()->height();
     }
-        
+    m_mutex.unlock();
 }
 
 void KThumb::clearProducer()
@@ -121,7 +120,12 @@ void KThumb::extractImage(int frame, int frame2)
     if (!KdenliveSettings::videothumbnails() || m_producer == NULL) return;
     if (frame != -1 && !m_requestedThumbs.contains(frame)) m_requestedThumbs.append(frame);
     if (frame2 != -1 && !m_requestedThumbs.contains(frame2)) m_requestedThumbs.append(frame2);
-    if (!m_future.isRunning()) m_future = QtConcurrent::run(this, &KThumb::doGetThumbs);
+    qSort(m_requestedThumbs);
+    if (!m_future.isRunning()) {
+        m_mutex.lock();
+        m_future = QtConcurrent::run(this, &KThumb::doGetThumbs);
+        m_mutex.unlock();
+    }
 }
 
 void KThumb::doGetThumbs()
@@ -133,10 +137,7 @@ void KThumb::doGetThumbs()
     while (!m_requestedThumbs.isEmpty()) {
         int frame = m_requestedThumbs.takeFirst();
         if (frame != -1) {
-            m_mutex.lock();
-            QImage img = getFrame(m_producer, frame, swidth, dwidth, theight);
-            m_mutex.unlock();
-            emit thumbReady(frame, img);
+            emit thumbReady(frame, getFrame(m_producer, frame, swidth, dwidth, theight));
         }
     }
 }
@@ -168,20 +169,21 @@ QPixmap KThumb::getImage(KUrl url, int frame, int width, int height)
 //static
 QImage KThumb::getFrame(Mlt::Producer *producer, int framepos, int frameWidth, int displayWidth, int height)
 {
-    QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
     if (producer == NULL || !producer->is_valid()) {
-        p.fill(Qt::red);
+        QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
+        p.fill(QColor(Qt::red).rgb());
         return p;
     }
 
     if (producer->is_blank()) {
-        p.fill(Qt::black);
+        QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
+        p.fill(QColor(Qt::black).rgb());
         return p;
     }
 
     producer->seek(framepos);
     Mlt::Frame *frame = producer->get_frame();
-    p = getFrame(frame, frameWidth, displayWidth, height);
+    QImage p = getFrame(frame, frameWidth, displayWidth, height);
     delete frame;
     return p;
 }
@@ -192,7 +194,7 @@ QImage KThumb::getFrame(Mlt::Frame *frame, int frameWidth, int displayWidth, int
 {
     QImage p(displayWidth, height, QImage::Format_ARGB32_Premultiplied);
     if (frame == NULL || !frame->is_valid()) {
-        p.fill(Qt::red);
+        p.fill(QColor(Qt::red).rgb());
         return p;
     }
 
@@ -209,13 +211,12 @@ QImage KThumb::getFrame(Mlt::Frame *frame, int frameWidth, int displayWidth, int
         } else {
             image = image.scaled(displayWidth, height, Qt::IgnoreAspectRatio).rgbSwapped();
         }
+        p.fill(QColor(Qt::black).rgb());
         QPainter painter(&p);
-        painter.fillRect(p.rect(), Qt::black);
-        painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
         painter.drawImage(p.rect(), image);
         painter.end();
     } else
-        p.fill(Qt::red);
+        p.fill(QColor(Qt::red).rgb());
     return p;
 }
 
@@ -476,7 +477,11 @@ void KThumb::queryIntraThumbs(QList <int> missingFrames)
         if (!m_intraFramesQueue.contains(i)) m_intraFramesQueue.append(i);
     }
     qSort(m_intraFramesQueue);
-    if (!m_intra.isRunning()) m_intra = QtConcurrent::run(this, &KThumb::slotGetIntraThumbs);
+    if (!m_intra.isRunning()) {
+        m_mutex.lock();
+        m_intra = QtConcurrent::run(this, &KThumb::slotGetIntraThumbs);
+        m_mutex.unlock();
+    }
 }
 
 void KThumb::slotGetIntraThumbs()
@@ -490,12 +495,10 @@ void KThumb::slotGetIntraThumbs()
     while (!m_intraFramesQueue.isEmpty()) {
         int pos = m_intraFramesQueue.takeFirst();
         if (!m_clipManager->pixmapCache->contains(path + QString::number(pos))) {
-            m_mutex.lock();
             if (m_clipManager->pixmapCache->insertImage(path + QString::number(pos), getFrame(m_producer, pos, frameWidth, displayWidth, theight))) {
                 addedThumbs = true;
             }
             else kDebug()<<"// INSERT FAILD FOR: "<<pos;
-            m_mutex.unlock();
         }
         m_intraFramesQueue.removeAll(pos);
     }