comparison layer/SpectrogramLayer.cpp @ 381:44670ce11a0c spectrogram-cache-rejig

* Some debug output and tweaks
author Chris Cannam
date Thu, 08 May 2008 09:23:16 +0000
parents 64e84e5efb76
children
comparison
equal deleted inserted replaced
374:64e84e5efb76 381:44670ce11a0c
1714 1714
1715 1715
1716 // Plan: 1716 // Plan:
1717 // 1717 //
1718 // - the QImage cache is managed by the GUI thread (which creates, 1718 // - the QImage cache is managed by the GUI thread (which creates,
1719 // sizes and destroys it). 1719 // sizes and destroys it)
1720 // 1720 //
1721 // - but the cache is drawn on by another thread (paint thread) 1721 // - but the cache is drawn on by another thread (paint thread)
1722 // 1722 //
1723 // - the GUI thread paint method should always just draw straight from 1723 // - the GUI thread paint method should always just draw straight from
1724 // cache without any lock, but it needs to check whether the cache is 1724 // cache without any lock, but it needs to check whether the cache is
1756 // cache mutex only long enough to look up the cache object in the 1756 // cache mutex only long enough to look up the cache object in the
1757 // map, and then release it, retaining the specific mutex while it 1757 // map, and then release it, retaining the specific mutex while it
1758 // paints. The GUI thread should then never modify the cache 1758 // paints. The GUI thread should then never modify the cache
1759 // image without holding its specific mutex.) 1759 // image without holding its specific mutex.)
1760 1760
1761 Profiler profiler("SpectrogramLayer::paint", false); 1761 Profiler profiler("SpectrogramLayer::paint", true);
1762
1763 std::cerr << "SpectrogramLayer::paint entering" << std::endl;
1762 1764
1763 QMutexLocker locker(&m_pixmapCacheMutex); 1765 QMutexLocker locker(&m_pixmapCacheMutex);
1764 1766
1765 rect = v->rect(); //!!! 1767 rect = v->rect(); //!!!
1766 1768
1767 float ratio = float(v->getZoomLevel()) / float(getWindowIncrement()); 1769 float ratio = float(v->getZoomLevel()) / float(getWindowIncrement());
1768 int imageWidth = lrintf(v->width() * ratio); 1770 int imageWidth = lrintf(v->width() * ratio);
1771
1772 std::cerr << "SpectrogramLayer::paint: view width = " << v->width() << ", ratio = " << ratio << ", imageWidth = " << imageWidth << std::endl;
1769 1773
1770 PixmapCache *cache = m_pixmapCaches[v]; 1774 PixmapCache *cache = m_pixmapCaches[v];
1771 if (!cache) { 1775 if (!cache) {
1772 cache = new PixmapCache; 1776 cache = new PixmapCache;
1773 cache->pixmap = 0; 1777 cache->pixmap = 0;
1776 cache->pixmap = new QImage(imageWidth, v->height(), 1780 cache->pixmap = new QImage(imageWidth, v->height(),
1777 QImage::Format_RGB32); 1781 QImage::Format_RGB32);
1778 cache->mutex.unlock(); 1782 cache->mutex.unlock();
1779 #ifdef DEBUG_SPECTROGRAM_REPAINT 1783 #ifdef DEBUG_SPECTROGRAM_REPAINT
1780 std::cerr << "SpectrogramLayer::paint: Created new cache, size " 1784 std::cerr << "SpectrogramLayer::paint: Created new cache, size "
1781 << v->width() << "x" << v->height() << " (" 1785 << imageWidth << "x" << v->height() << " ("
1782 << v->width() * v->height() * 4 << " bytes)" << std::endl; 1786 << imageWidth * v->height() * 4 << " bytes)" << std::endl;
1783 #endif 1787 #endif
1784 return; //!!! prod paint thread 1788 return; //!!! prod paint thread
1785 } 1789 }
1786 1790
1787 long myStartFrame = v->getStartFrame(); 1791 long myStartFrame = v->getStartFrame();
1913 if (m_exiting) break; 1917 if (m_exiting) break;
1914 } 1918 }
1915 1919
1916 if (!m_exiting) { 1920 if (!m_exiting) {
1917 //!!! wait on condition 1921 //!!! wait on condition
1918 if (workToDo) ; // usleep(100); 1922 if (workToDo) {
1919 else sleep(1); 1923 // usleep(100);
1924 std::cerr << "SpectrogramLayer::PaintThread::run: still work to do, continuing" << std::endl;
1925 } else {
1926 sleep(10);
1927 }
1920 } 1928 }
1921 } 1929 }
1922 } 1930 }
1923 1931
1924 // return true if no more work to do, or can do no more 1932 // return true if no more work to do, or can do no more
1925 1933
1926 bool 1934 bool
1927 SpectrogramLayer::paintCache(View *v) const 1935 SpectrogramLayer::paintCache(View *v) const
1928 { 1936 {
1929 Profiler profiler("SpectrogramLayer::paintCache", false); 1937 Profiler profiler("SpectrogramLayer::paintCache", true);
1930 1938
1931 m_pixmapCacheMutex.lock(); 1939 m_pixmapCacheMutex.lock();
1932 1940
1933 PixmapCache *cacheptr = m_pixmapCaches[v]; 1941 PixmapCache *cacheptr = m_pixmapCaches[v];
1934 if (!cacheptr || !cacheptr->pixmap || cacheptr->pixmap->width() == 0) { 1942 if (!cacheptr || !cacheptr->pixmap || cacheptr->pixmap->width() == 0) {
1941 } 1949 }
1942 1950
1943 PixmapCache &cache = *cacheptr; 1951 PixmapCache &cache = *cacheptr;
1944 QMutexLocker locker(&cache.mutex); 1952 QMutexLocker locker(&cache.mutex);
1945 m_pixmapCacheMutex.unlock(); 1953 m_pixmapCacheMutex.unlock();
1954
1955 #ifdef DEBUG_SPECTROGRAM_REPAINT
1956 std::cerr << "SpectrogramLayer::paintCache(): Have cache to paint onto"
1957 << std::endl;
1958 #endif
1946 1959
1947 QImage &image = *cache.pixmap; //!!! rename to cache.image 1960 QImage &image = *cache.pixmap; //!!! rename to cache.image
1948 1961
1949 const DenseTimeValueModel *model = m_model; 1962 const DenseTimeValueModel *model = m_model;
1950 if (!model || !model->isOK() || !model->isReady()) { 1963 if (!model || !model->isOK() || !model->isReady()) {
2142 interpolate = true; 2155 interpolate = true;
2143 } 2156 }
2144 } 2157 }
2145 2158
2146 bool runOutOfData = false; 2159 bool runOutOfData = false;
2160 int runOutOfDataAt = x1;
2147 2161
2148 std::cerr << "painting from " << x0 << " to " << x1 << std::endl; 2162 std::cerr << "painting from " << x0 << " to " << x1 << std::endl;
2149 2163
2150 for (int x = 0; x < (x1 - x0); ++x) { 2164 for (int x = 0; x < (x1 - x0); ++x) {
2151 2165
2167 if (!fft->isColumnAvailable(col)) { 2181 if (!fft->isColumnAvailable(col)) {
2168 #ifdef DEBUG_SPECTROGRAM_REPAINT 2182 #ifdef DEBUG_SPECTROGRAM_REPAINT
2169 std::cerr << "Met unavailable column at col " << col << std::endl; 2183 std::cerr << "Met unavailable column at col " << col << std::endl;
2170 #endif 2184 #endif
2171 runOutOfData = true; 2185 runOutOfData = true;
2186 runOutOfDataAt = x0 + x;
2172 break; 2187 break;
2173 } 2188 }
2174 2189
2175 /* 2190 /*
2176 if (!fftSuspended) { 2191 if (!fftSuspended) {
2355 #ifdef DEBUG_SPECTROGRAM_REPAINT 2370 #ifdef DEBUG_SPECTROGRAM_REPAINT
2356 std::cerr << "Overall mag unchanged at [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "]" << std::endl; 2371 std::cerr << "Overall mag unchanged at [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "]" << std::endl;
2357 #endif 2372 #endif
2358 } 2373 }
2359 */ 2374 */
2375
2376 if (runOutOfData) {
2377 std::cerr << "ran out of data, setting x1 to " << runOutOfDataAt
2378 << std::endl;
2379 x1 = runOutOfDataAt;
2380 }
2381
2360 if (cache.validArea.width() > 0) { 2382 if (cache.validArea.width() > 0) {
2361 2383
2362 int vx0 = 0, vx1 = 0; 2384 int vx0 = 0, vx1 = 0;
2363 vx0 = cache.validArea.x(); 2385 vx0 = cache.validArea.x();
2364 vx1 = cache.validArea.x() + cache.validArea.width(); 2386 vx1 = cache.validArea.x() + cache.validArea.width();
2387
2388 std::cerr << "set cache valid region from "
2389 << cache.validArea.x() << " -> " << cache.validArea.x() + cache.validArea.width();
2365 2390
2366 cache.validArea = QRect 2391 cache.validArea = QRect
2367 (std::min(vx0, x0), cache.validArea.y(), 2392 (std::min(vx0, x0), cache.validArea.y(),
2368 std::max(vx1 - std::min(vx0, x0), 2393 std::max(vx1 - std::min(vx0, x0),
2369 x1 - std::min(vx0, x0)), 2394 x1 - std::min(vx0, x0)),
2370 cache.validArea.height()); 2395 cache.validArea.height());
2371 2396
2397 std::cerr << " to "
2398 << cache.validArea.x() << " -> " << cache.validArea.x() + cache.validArea.width()
2399 << std::endl;
2400
2372 } else { 2401 } else {
2373 2402
2374 cache.validArea = QRect(x0, 0, x1 - x0, h); 2403 cache.validArea = QRect(x0, 0, x1 - x0, h);
2404
2405 std::cerr << "set cache valid region from empty to "
2406 << cache.validArea.x() << " -> " << cache.validArea.x() + cache.validArea.width()
2407 << std::endl;
2375 } 2408 }
2376 2409
2377 /* 2410 /*
2378 Profiler profiler2("SpectrogramLayer::paintCache: draw image", true); 2411 Profiler profiler2("SpectrogramLayer::paintCache: draw image", true);
2379 2412
2618 std::cerr << "x0 " << x0 << ", x1 " << x1 << ", vx0 " << vx0 << ", vx1 " << vx1 << ", paintBlockWidth " << paintBlockWidth << std::endl; 2651 std::cerr << "x0 " << x0 << ", x1 " << x1 << ", vx0 " << vx0 << ", vx1 " << vx1 << ", paintBlockWidth " << paintBlockWidth << std::endl;
2619 #endif 2652 #endif
2620 if (x0 < vx0) { 2653 if (x0 < vx0) {
2621 if (x0 + paintBlockWidth < vx0) { 2654 if (x0 + paintBlockWidth < vx0) {
2622 x0 = vx0 - paintBlockWidth; 2655 x0 = vx0 - paintBlockWidth;
2623 // } else {
2624 // x0 = 0;
2625 } 2656 }
2626 x1 = vx0; 2657 x1 = vx0;
2627 } else if (x0 >= vx1) { 2658 } else if (x0 >= vx1) {
2628 x0 = vx1; 2659 x0 = vx1;
2629 if (x1 > x0 + paintBlockWidth) { 2660 if (x1 > x0 + paintBlockWidth) {
2662 x0 = mid - paintBlockWidth/2; 2693 x0 = mid - paintBlockWidth/2;
2663 x1 = x0 + paintBlockWidth; 2694 x1 = x0 + paintBlockWidth;
2664 // } 2695 // }
2665 } 2696 }
2666 } 2697 }
2698
2699 std::cerr << "SpectrogramLayer::selectPaintStrip: returning " << x0 << " -> " << x1 << std::endl;
2667 } 2700 }
2668 2701
2669 void 2702 void
2670 SpectrogramLayer::illuminateLocalFeatures(View *v, QPainter &paint) const 2703 SpectrogramLayer::illuminateLocalFeatures(View *v, QPainter &paint) const
2671 { 2704 {