Performance improvements

This commit is contained in:
feedc0de committed 2019-09-07 12:00:14 +02:00
1 parent 2298436c13
commit 8076271edc
8 files changed
+81 -176

No files matched your search

+50 -107
View File
@@ -8,110 +8,26 @@
#include <QTimerEvent>
OsciWidget::OsciWidget(QWidget *parent) :
QWidget{parent},
QOpenGLWidget{parent},
m_redrawTimerId(startTimer(1000/m_fps))
{
resizePixmap();
m_fpsTimer.start();
}
int OsciWidget::blend() const
{
return m_blend;
}
float OsciWidget::factor() const
{
return m_factor;
}
float OsciWidget::glow() const
{
return m_glow;
}
int OsciWidget::fps() const
{
return m_fps;
}
void OsciWidget::setBlend(int blend)
{
if (blend == m_blend)
return;
qDebug() << blend;
m_blend = blend;
}
void OsciWidget::setFactor(float factor)
{
qDebug() << factor;
m_factor = factor;
}
void OsciWidget::setGlow(float glow)
{
qDebug() << glow;
m_glow = glow;
}
void OsciWidget::setFps(int fps)
{
qDebug() << fps;
killTimer(m_redrawTimerId);
m_fps = fps;
m_redrawTimerId = startTimer(1000/m_fps);
}
void OsciWidget::renderSamples(const SamplePair *begin, const SamplePair *end)
{
QPainter painter;
painter.begin(&m_pixmap);
painter.setCompositionMode(QPainter::CompositionMode_Plus);
m_callbacksCounter++;
QPen pen;
pen.setWidth(2);
pen.setColor(QColor(0, 255, 0));
painter.setPen(pen);
// Paint from center
painter.translate(m_pixmap.width()/2, m_pixmap.height()/2);
const auto pointToCoordinates = [width=m_pixmap.width()/2,height=m_pixmap.height()/2,factor=m_factor](const QPointF &point)
{
return QPoint{
int(point.x() * factor * width),
int(point.y() * factor * height)
};
};
for (auto i = begin; i < end; i++)
{
const QPointF p{
float(i->x) / std::numeric_limits<qint16>::max(),
float(-i->y) / std::numeric_limits<qint16>::max()
};
const QLineF line(m_lastPoint, p);
auto brightness = 1.f / line.length() / m_glow;
if (line.length() == 0.f || brightness > 255.f)
brightness = 255.f;
painter.setOpacity(brightness);
painter.drawLine(pointToCoordinates(m_lastPoint), pointToCoordinates(p));
m_lastPoint = p;
}
painter.resetTransform();
painter.setCompositionMode(QPainter::CompositionMode_Multiply);
painter.fillRect(m_pixmap.rect(), QColor(m_blend,m_blend,m_blend));
painter.end();
m_buffer.insert(m_buffer.end(), begin, end);
}
void OsciWidget::paintEvent(QPaintEvent *event)
@@ -121,25 +37,64 @@ void OsciWidget::paintEvent(QPaintEvent *event)
m_frameCounter++;
if (m_fpsTimer.hasExpired(1000))
{
m_displayFrameCounter = m_frameCounter;
m_statsDisplay = QString("%0FPS (%1 callbacks)").arg(m_frameCounter).arg(m_callbacksCounter);
m_frameCounter = 0;
m_callbacksCounter = 0;
m_fpsTimer.restart();
}
QPainter painter;
painter.begin(this);
painter.drawPixmap(0, 0, m_pixmap);
// draw background
painter.setBrush(Qt::black);
painter.drawRect(rect());
// drawing new lines ontop
QPen pen;
pen.setWidth(2);
pen.setColor(QColor(0, 255, 0));
painter.setPen(pen);
painter.translate(width()/2, height()/2);
painter.setCompositionMode(QPainter::CompositionMode_Plus);
const auto pointToCoordinates = [width=width()/2,height=height()/2,factor=m_factor](const QPointF &point)
{
return QPoint{
int(point.x() * factor * width),
int(point.y() * factor * height)
};
};
for (const auto &i : m_buffer)
{
const QPointF p{
float(i.first) / std::numeric_limits<qint16>::max(),
float(-i.second) / std::numeric_limits<qint16>::max()
};
const QLineF line(m_lastPoint, p);
painter.setOpacity(std::min(1.0, 1. / ((line.length() * 100) + 1)));
painter.drawLine(pointToCoordinates(m_lastPoint), pointToCoordinates(p));
m_lastPoint = p;
}
painter.resetTransform();
painter.setOpacity(1);
m_buffer.clear();
// draw stats
painter.setPen(Qt::white);
painter.setBrush(Qt::white);
QFont font;
font.setPixelSize(24);
painter.drawText(20, 20, QString("%0FPS").arg(m_displayFrameCounter));
painter.drawText(20, 20, m_statsDisplay);
painter.end();
if (m_pixmap.rect() != rect())
qDebug() << m_pixmap.rect() << rect();
}
void OsciWidget::timerEvent(QTimerEvent *event)
@@ -148,15 +103,3 @@ void OsciWidget::timerEvent(QTimerEvent *event)
if (event->timerId() == m_redrawTimerId)
repaint();
}
void OsciWidget::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
resizePixmap();
}
void OsciWidget::resizePixmap()
{
m_pixmap = QPixmap(size());
m_pixmap.fill(QColor{});
}