changeset 1079:7ebfb61b1701 spectrogram-minor-refactor

More filling in render & cache code
author Chris Cannam
date Thu, 30 Jun 2016 15:46:14 +0100
parents ee01a4062747
children 2e5945b87aca
files layer/Colour3DPlotRenderer.cpp layer/Colour3DPlotRenderer.h layer/ColourScale.cpp layer/ColourScale.h layer/ScrollableImageCache.cpp layer/ScrollableImageCache.h
diffstat 6 files changed, 270 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/layer/Colour3DPlotRenderer.cpp	Thu Jun 30 12:40:22 2016 +0100
+++ b/layer/Colour3DPlotRenderer.cpp	Thu Jun 30 15:46:14 2016 +0100
@@ -22,6 +22,10 @@
 
 #include "LayerGeometryProvider.h"
 
+#include <vector>
+
+using namespace std;
+
 Colour3DPlotRenderer::RenderResult
 Colour3DPlotRenderer::render(QPainter &paint, QRect rect)
 {
@@ -42,14 +46,103 @@
 	throw std::logic_error("no LayerGeometryProvider provided");
     }
 
-    DenseThreeDimensionalModel *model = m_sources.source;
-    if (!model || !model->isOK() || !model->isReady()) {
-	throw std::logic_error("no source model provided, or model not ready");
+    sv_frame_t startFrame = v->getStartFrame();
+    int zoomLevel = v->getZoomLevel();
+    
+    int x0 = v->getXForViewX(rect.x());
+    int x1 = v->getXForViewX(rect.x() + rect.width());
+    if (x0 < 0) x0 = 0;
+    if (x1 > v->getPaintWidth()) x1 = v->getPaintWidth();
+
+    m_cache.resize(v->getPaintSize());
+    m_cache.setZoomLevel(v->getZoomLevel());
+
+    if (m_cache.isValid()) { // some part of the cache is valid
+
+        if (v->getXForFrame(m_cache.getStartFrame()) ==
+            v->getXForFrame(startFrame) &&
+            m_cache.getValidLeft() <= x0 &&
+            m_cache.getValidRight() >= x1) {
+                
+            // cache is valid for the complete requested area
+            paint.drawImage(rect, m_cache.getImage(), rect);
+            return { rect, {} };
+
+        } else {
+            // cache doesn't begin at the right frame or doesn't
+            // contain the complete view, but might be scrollable or
+            // partially usable
+            m_cache.scrollTo(startFrame);
+
+            // if we are not time-constrained, then we want to paint
+            // the whole area in one go, and we're not going to
+            // provide the more complex logic to handle that if there
+            // are discontiguous areas involved. So if the only valid
+            // part of cache is in the middle, just make the whole
+            // thing invalid and start again.
+            if (!timeConstrained) {
+                if (m_cache.getValidLeft() > x0 &&
+                    m_cache.getValidRight() < x1) {
+                    m_cache.invalidate();
+                }
+            }
+        }
     }
-	
-    sv_frame_t startFrame = v->getStartFrame();
 
+    bool rightToLeft = false;
+
+    if (!m_cache.isValid() && timeConstrained) {
+        // When rendering the whole thing in a context where we
+        // might not be able to complete the work, start from
+        // somewhere near the middle so that the region of
+        // interest appears first
+
+        //!!! (perhaps we should avoid doing this if past repaints
+        //!!! have been fast enough to do the whole in one shot)
+        if (x0 == 0 && x1 == v->getPaintWidth()) {
+            x0 = int(x1 * 0.3);
+        }
+    }
+
+    if (m_cache.isValid()) {
+        // When rendering only a part of the cache, we need to make
+        // sure that the part we're rendering is adjacent to (or
+        // overlapping) a valid area of cache, if we have one. The
+        // alternative is to ditch the valid area of cache and render
+        // only the requested area, but that's risky because this can
+        // happen when just waving the pointer over a small part of
+        // the view -- if we lose the partly-built cache every time
+        // the user does that, we'll never finish building it.
+        int left = x0;
+        int width = x1 - x0;
+        bool isLeftOfValidArea = false;
+        m_cache.adjustToTouchValidArea(left, width, isLeftOfValidArea);
+        x0 = left;
+        x1 = x0 + width;
+
+        // That call also told us whether we should be painting
+        // sub-regions of our target region in right-to-left order in
+        // order to ensure contiguity
+        rightToLeft = isLeftOfValidArea;
+    }
     
+    int repaintWidth = x1 - x0;
+
+    QRect rendered = renderToCache(x0, repaintWidth, timeConstrained);
+
+    QRect pr = rect & m_cache.getValidArea();
+    paint.drawImage(pr.x(), pr.y(), m_cache.getImage(),
+                    pr.x(), pr.y(), pr.width(), pr.height());
+
+    if (!timeConstrained && (pr != rect)) {
+        //!!! on a first cut, there is a risk that this will happen
+        //!!! when we are at start/end of model -- trap, report, and
+        //!!! then fix
+        throw std::logic_error("internal error: failed to render entire requested rect even when not time-constrained");
+    }
+    
+    return { pr, {} };
+
     //!!! todo: timing/incomplete paint
 
     //!!! todo: peak frequency style
@@ -58,6 +151,140 @@
 
     //!!! todo: bin boundary alignment when in BinResolution
 
-    return { rect, {} };
+    //!!! todo: view magnitudes / normalise visible area
+
+    //!!! todo: alter documentation for view mag stuff (cached paints
+    //!!! do not update MagnitudeRange)
+
+    //!!! todo, here or in caller: illuminateLocalFeatures
+
+    //!!! fft model scaling?
+    
+    //!!! should we own the Dense3DModelPeakCache here? or should it persist
 }
 
+QRect
+Colour3DPlotRenderer::renderToCache(int x0, int repaintWidth, bool timeConstrained)
+{
+    // Draw to the draw buffer, and then scale-copy from there.
+
+    DenseThreeDimensionalModel *model = m_sources.source;
+    if (!model || !model->isOK() || !model->isReady()) {
+	throw std::logic_error("no source model provided, or model not ready");
+    }
+
+    LayerGeometryProvider *v = m_sources.geometryProvider; // already checked
+
+    // The draw buffer contains a fragment at either our pixel
+    // resolution (if there is more than one time-bin per pixel) or
+    // time-bin resolution (if a time-bin spans more than one pixel).
+    // We need to ensure that it starts and ends at points where a
+    // time-bin boundary occurs at an exact pixel boundary, and with a
+    // certain amount of overlap across existing pixels so that we can
+    // scale and draw from it without smoothing errors at the edges.
+
+    // If (getFrameForX(x) / increment) * increment ==
+    // getFrameForX(x), then x is a time-bin boundary.  We want two
+    // such boundaries at either side of the draw buffer -- one which
+    // we draw up to, and one which we subsequently crop at.
+
+    bool bufferIsBinResolution = false;
+    int binResolution = model->getResolution();
+    int zoomLevel = v->getZoomLevel();
+    if (binResolution > zoomLevel) bufferIsBinResolution = true;
+
+    sv_frame_t leftBoundaryFrame = -1, leftCropFrame = -1;
+    sv_frame_t rightBoundaryFrame = -1, rightCropFrame = -1;
+
+    int drawWidth;
+
+    if (bufferIsBinResolution) {
+        for (int x = x0; ; --x) {
+            sv_frame_t f = v->getFrameForX(x);
+            if ((f / binResolution) * binResolution == f) {
+                if (leftCropFrame == -1) leftCropFrame = f;
+                else if (x < x0 - 2) {
+                    leftBoundaryFrame = f;
+                    break;
+                }
+            }
+        }
+        for (int x = x0 + repaintWidth; ; ++x) {
+            sv_frame_t f = v->getFrameForX(x);
+            if ((f / binResolution) * binResolution == f) {
+                if (rightCropFrame == -1) rightCropFrame = f;
+                else if (x > x0 + repaintWidth + 2) {
+                    rightBoundaryFrame = f;
+                    break;
+                }
+            }
+        }
+        drawWidth = int((rightBoundaryFrame - leftBoundaryFrame) / binResolution);
+    } else {
+        drawWidth = repaintWidth;
+    }
+    
+    // We always paint the full height.  Smaller heights can be used
+    // when painting direct from cache (outside this function), but we
+    // want to ensure the cache is coherent without having to worry
+    // about vertical matching of required and valid areas as well as
+    // horizontal. That's why this function didn't take any y/height
+    // parameters.
+    int h = v->getPaintHeight();
+
+    clearDrawBuffer(drawWidth, h);
+
+    vector<int> binforx(drawWidth);
+    vector<double> binfory(h);
+    
+    bool usePeaksCache = false;
+    int binsPerPeak = 1;
+
+    if (bufferIsBinResolution) {
+        for (int x = 0; x < drawWidth; ++x) {
+            binforx[x] = int(leftBoundaryFrame / binResolution) + x;
+        }
+    } else {
+        for (int x = 0; x < drawWidth; ++x) {
+            sv_frame_t f0 = v->getFrameForX(x);
+            double s0 = double(f0 - model->getStartFrame()) / binResolution;
+            binforx[x] = int(s0 + 0.0001);
+        }
+
+        if (m_sources.peaks) { // peaks cache exists
+
+            binsPerPeak = m_sources.peaks->getColumnsPerPeak();
+            usePeaksCache = (binResolution * binsPerPeak) < zoomLevel;
+
+            if (m_params.colourScale.getScale() ==
+                ColourScale::PhaseColourScale) {
+                usePeaksCache = false;
+            }
+        }
+    }
+
+    int attainedDrawWidth = drawWidth;
+
+    //!!! todo: all
+    
+}
+ 
+void
+Colour3DPlotRenderer::clearDrawBuffer(int w, int h)
+{
+    if (m_drawBuffer.width() < w || m_drawBuffer.height() != h) {
+
+        m_drawBuffer = QImage(w, h, QImage::Format_Indexed8);
+
+        for (int pixel = 0; pixel < 256; ++pixel) {
+            //!!! todo: colour rotation (here 0)
+            m_drawBuffer.setColor
+                ((unsigned char)pixel,
+                 m_params.colourScale.getColourForPixel(pixel, 0).rgb());
+        }
+    }
+
+    m_drawBuffer.fill(0);
+}
+
+
--- a/layer/Colour3DPlotRenderer.h	Thu Jun 30 12:40:22 2016 +0100
+++ b/layer/Colour3DPlotRenderer.h	Thu Jun 30 15:46:14 2016 +0100
@@ -76,8 +76,7 @@
     
     Colour3DPlotRenderer(Sources sources, Parameters parameters) :
         m_sources(sources),
-	m_params(parameters),
-        m_bufferResolution(PixelResolution)
+	m_params(parameters)
     { }
 
     struct RenderResult {
@@ -143,17 +142,6 @@
     // member is to avoid reallocation.
     QImage m_drawBuffer;
 
-    // Indicates whether the draw buffer is rendered at bin resolution
-    // or at pixel resolution. Pixel resolution is used when the zoom
-    // level is such that each pixel is backed by more than one bin;
-    // bin resolution is used when the zoom level is such that each
-    // bin is drawn to more than one pixel.
-    enum BufferResolution {
-        BinResolution,
-        PixelResolution
-    };
-    BufferResolution m_bufferResolution;
-
     // Image cache is our persistent record of the visible area. It is
     // always the same size as the view (i.e. the paint size reported
     // by the LayerGeometryProvider) and is scrolled and partially
@@ -164,10 +152,8 @@
     ScrollableImageCache m_cache;
 
     RenderResult render(QPainter &paint, QRect rect, bool timeConstrained);
-                      
-    //!!! fft model scaling?
-    
-    //!!! should we own the Dense3DModelPeakCache here? or should it persist
+    QRect renderToCache(int x0, int repaintWidth, bool timeConstrained);
+    void clearDrawBuffer(int w, int h);
 };
 
 #endif
--- a/layer/ColourScale.cpp	Thu Jun 30 12:40:22 2016 +0100
+++ b/layer/ColourScale.cpp	Thu Jun 30 15:46:14 2016 +0100
@@ -60,8 +60,14 @@
 {
 }
 
+ColourScale::Scale
+ColourScale::getScale() const
+{
+    return m_params.scale;
+}
+
 int
-ColourScale::getPixel(double value)
+ColourScale::getPixel(double value) const
 {
     double maxPixF = m_maxPixel;
 
@@ -112,7 +118,7 @@
 }
 
 QColor
-ColourScale::getColourForPixel(int pixel, int rotation)
+ColourScale::getColourForPixel(int pixel, int rotation) const
 {
     if (pixel < 0) {
 	pixel = 0;
--- a/layer/ColourScale.h	Thu Jun 30 12:40:22 2016 +0100
+++ b/layer/ColourScale.h	Thu Jun 30 15:46:14 2016 +0100
@@ -72,6 +72,11 @@
 
     ColourScale(const ColourScale &) = default;
     ColourScale &operator=(const ColourScale &) = default;
+
+    /**
+     * Return the general type of scale this is.
+     */
+    Scale getScale() const;
     
     /**
      * Return a pixel number (in the range 0-255 inclusive)
@@ -79,7 +84,7 @@
      * values below the threshold supplied in the constructor. All
      * other values are mapped onto the range 1-255.
      */
-    int getPixel(double value);
+    int getPixel(double value) const;
 
     /**
      * Return the colour for the given pixel number (which must be in
@@ -87,13 +92,13 @@
      * colour. Other pixels are mapped taking into account the given
      * colourmap rotation (which is also a value in the range 0-255).
      */
-    QColor getColourForPixel(int pixel, int rotation);
+    QColor getColourForPixel(int pixel, int rotation) const;
     
     /**
      * Return the colour corresponding to the given value. This is
      * equivalent to getColourForPixel(getPixel(value), rotation).
      */
-    QColor getColour(double value, int rotation) {
+    QColor getColour(double value, int rotation) const {
 	return getColourForPixel(getPixel(value), rotation);
     }
 
--- a/layer/ScrollableImageCache.cpp	Thu Jun 30 12:40:22 2016 +0100
+++ b/layer/ScrollableImageCache.cpp	Thu Jun 30 15:46:14 2016 +0100
@@ -23,6 +23,11 @@
 ScrollableImageCache::scrollTo(sv_frame_t newStartFrame)
 {
     if (!m_v) throw std::logic_error("ScrollableImageCache: not associated with a LayerGeometryProvider");
+
+    if (m_startFrame == newStartFrame) {
+	// haven't moved
+        return;
+    }
 	
     int dx = (m_v->getXForFrame(m_startFrame) -
 	      m_v->getXForFrame(newStartFrame));
@@ -41,7 +46,7 @@
     int w = m_image.width();
 
     if (dx == 0) {
-	// haven't moved
+	// haven't moved visibly
 	return;
     }
 
--- a/layer/ScrollableImageCache.h	Thu Jun 30 12:40:22 2016 +0100
+++ b/layer/ScrollableImageCache.h	Thu Jun 30 15:46:14 2016 +0100
@@ -58,8 +58,10 @@
     }
     
     void resize(QSize newSize) {
-	m_image = QImage(newSize, QImage::Format_ARGB32_Premultiplied);
-	invalidate();
+        if (getSize() != newSize) {
+            m_image = QImage(newSize, QImage::Format_ARGB32_Premultiplied);
+            invalidate();
+        }
     }
 	
     int getValidLeft() const {
@@ -83,8 +85,10 @@
     }
     
     void setZoomLevel(int zoom) {
-	m_zoomLevel = zoom;
-	invalidate();
+        if (m_zoomLevel != zoom) {
+            m_zoomLevel = zoom;
+            invalidate();
+        }
     }
 
     sv_frame_t getStartFrame() const {
@@ -97,8 +101,10 @@
      * where possible, use scrollTo() instead.
      */
     void setStartFrame(sv_frame_t frame) {
-	m_startFrame = frame;
-	invalidate();
+        if (m_startFrame != frame) {
+            m_startFrame = frame;
+            invalidate();
+        }
     }
     
     const QImage &getImage() const {