Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

waterfallplot: Use OpenGL #861

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions qml/Ping1DVisualizer.qml
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,21 @@ Item {
orientation: Qt.Horizontal
anchors.fill: parent

WaterfallPlot {
id: waterfall
ShaderEffect {
id: shader

WaterfallPlot {
id: waterfall
visible: false
anchors.fill: parent
}

property variant src: waterfall
property variant horizontalRatio: waterfall.drawHorizontalRatio
property variant verticalRatio: waterfall.drawVerticalRatio
vertexShader: "qrc:/opengl/waterfallplot/vertex.glsl"
fragmentShader: "qrc:/opengl/waterfallplot/fragment.glsl"

Layout.fillHeight: true
Layout.fillWidth: true
Layout.preferredWidth: 350
Expand Down
11 changes: 11 additions & 0 deletions qml/opengl/waterfallplot/fragment.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
varying vec2 coord;
uniform float horizontalRatio;
uniform float verticalRatio;
uniform sampler2D src;

void main() {
// Mod is used to wrap around the 0-1 scaled x axis
vec2 shiftedCoord = mod(coord + vec2(horizontalRatio, 0), 1.0);
shiftedCoord.y = shiftedCoord.y * verticalRatio;
gl_FragColor = texture2D(src, shiftedCoord);
}
8 changes: 8 additions & 0 deletions qml/opengl/waterfallplot/vertex.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
uniform highp mat4 qt_Matrix;
attribute highp vec4 qt_Vertex;
attribute highp vec2 qt_MultiTexCoord0;
varying highp vec2 coord;
void main() {
coord = qt_MultiTexCoord0;
gl_Position = qt_Matrix * qt_Vertex;
}
2 changes: 2 additions & 0 deletions resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,7 @@
<qresource prefix="/opengl">
<file alias="polarplot/fragment.glsl">qml/opengl/polarplot/fragment.glsl</file>
<file alias="polarplot/vertex.glsl">qml/opengl/polarplot/vertex.glsl</file>
<file alias="waterfallplot/fragment.glsl">qml/opengl/waterfallplot/fragment.glsl</file>
<file alias="waterfallplot/vertex.glsl">qml/opengl/waterfallplot/vertex.glsl</file>
</qresource>
</RCC>
39 changes: 14 additions & 25 deletions src/waterfall/waterfallplot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ uint16_t WaterfallPlot::_displayWidth = 500;

WaterfallPlot::WaterfallPlot(QQuickItem* parent)
: Waterfall(parent)
, _currentDrawIndex(_displayWidth)
, _image(2048, 2500, QImage::Format_RGBA8888)
, _currentDrawIndex(0)
, _image(_displayWidth, 2000, QImage::Format_RGBA8888)
, _maxDepthToDrawInPixels(0)
, _minDepthToDrawInPixels(0)
, _mouseDepth(0)
Expand Down Expand Up @@ -50,20 +50,13 @@ void WaterfallPlot::paint(QPainter* painter)
_painter = painter;
}

static uint16_t first;

if (_currentDrawIndex < _displayWidth) {
first = 0;
} else {
first = _currentDrawIndex - _displayWidth;
}

// http://blog.qt.io/blog/2006/05/13/fast-transformed-pixmapimage-drawing/
pix = QPixmap::fromImage(_image, Qt::NoFormatConversion);
// Code for debug, draw the entire waterfall
//_painter->drawPixmap(_painter->viewport(), pix, QRect(0, 0, _image.width(), _image.height()));
_painter->drawPixmap(QRect(0, 0, width(), height()), pix,
QRect(first, _minDepthToDrawInPixels, _displayWidth, _maxDepthToDrawInPixels));
_painter->drawPixmap(_painter->viewport(), pix, QRect(0, 0, _image.width(), _image.height()));

emit drawHorizontalRatioChanged();
emit drawVerticalRatioChanged();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think about merging those two emits into one? if the horizontal and vertical changes at the same time probably there will be one less redraw

}

void WaterfallPlot::setImage(const QImage& image)
Expand Down Expand Up @@ -137,6 +130,10 @@ void WaterfallPlot::draw(const QVector<double>& points, float confidence, float
// This ring vector will store variables of the last n samples for user access
_DCRing.append({initPoint, length, confidence, distance});

// Limit currentDrawIndex to be inside our image max width
_currentDrawIndex++;
_currentDrawIndex %= _displayWidth;

/**
* @brief Get lastMaxDepth from the last n samples
*/
Expand Down Expand Up @@ -183,7 +180,6 @@ void WaterfallPlot::draw(const QVector<double>& points, float confidence, float
QPainter painter(&_image);
// Clean everything and start from zero
painter.fillRect(_image.rect(), Qt::transparent);
// QRect(0, 0, _image.width(), _image.height()*dynamicPixelsPerMeterScalar), old
painter.drawImage(dst, old, src);
painter.end();
};
Expand Down Expand Up @@ -217,16 +213,6 @@ void WaterfallPlot::draw(const QVector<double>& points, float confidence, float
int virtualFloor = initPoint * _minPixelsPerMeter;
int virtualHeight = length * _minPixelsPerMeter * dynamicPixelsPerMeterScalar;

// Copy tail to head
// TODO: can we get even better and allocate just once at initialization? ie circular buffering
if (_currentDrawIndex >= _image.width()) {
redrawImage(QRect(0, 0, _displayWidth, _image.height()),
QRect(old.width() - _displayWidth, 0, _displayWidth, old.height()));

// Start painting from the beginning
_currentDrawIndex = _displayWidth;
}

// Do up/downsampling
float factor = points.length() / ((float)(virtualHeight));

Expand Down Expand Up @@ -273,7 +259,10 @@ void WaterfallPlot::draw(const QVector<double>& points, float confidence, float
_image.setPixelColor(_currentDrawIndex, i + virtualFloor, valueToRGB(points[factor * i]));
}
}
_currentDrawIndex++; // This can get to be an issue at very fast update rates from ping

for (int i = virtualHeight; i < _image.height(); i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how fast is this but perhaps this is something that could be improved by QConcurrent?

_image.setPixelColor(_currentDrawIndex, i, Qt::transparent);
}

// Fix max update in 20Hz at max
if (!_updateTimer->isActive()) {
Expand Down
21 changes: 21 additions & 0 deletions src/waterfall/waterfallplot.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,28 @@ class WaterfallPlot : public Waterfall {
Q_INVOKABLE float getMinDepthToDraw() { return _minDepthToDraw; }
Q_PROPERTY(float minDepthToDraw READ getMinDepthToDraw NOTIFY minDepthToDrawChanged)

/**
* @brief Return the necessary horizontal ratio value to perform the correct waterfall draw
*
* @return double
*/
Q_INVOKABLE double drawHorizontalRatio() { return static_cast<double>(_currentDrawIndex + 1) / _image.width(); }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const.

Q_PROPERTY(double drawHorizontalRatio READ drawHorizontalRatio NOTIFY drawHorizontalRatioChanged)

/**
* @brief Return the necessary vertical ratio value to perform the correct waterfall draw
*
* @return double
*/
Q_INVOKABLE double drawVerticalRatio()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const.

{
return (_maxDepthToDraw - _minDepthToDraw) * _minPixelsPerMeter / _image.height();
}
Q_PROPERTY(double drawVerticalRatio READ drawVerticalRatio NOTIFY drawVerticalRatioChanged)

signals:
void drawHorizontalRatioChanged();
void drawVerticalRatioChanged();
void imageChanged();
void maxDepthToDrawChanged();
void minDepthToDrawChanged();
Expand Down