Performance improvements
This commit is contained in:
@@ -2,12 +2,7 @@
|
||||
|
||||
#include <QObject>
|
||||
|
||||
template<typename T>
|
||||
struct SamplePairT {
|
||||
T x, y;
|
||||
};
|
||||
|
||||
typedef SamplePairT<qint16> SamplePair;
|
||||
#include "oscicommon.h"
|
||||
|
||||
class BaseDevice : public QObject
|
||||
{
|
||||
|
@@ -34,8 +34,8 @@ void FakeDevice::timerEvent(QTimerEvent *event)
|
||||
pair != m_buffer.get() + m_bufferSize;
|
||||
pair++)
|
||||
{
|
||||
pair->x = std::sin(m_dingsDesHaltHochZaehlt) * std::numeric_limits<qint16>::max();
|
||||
pair->y = std::cos(m_dingsDesHaltHochZaehlt) * std::numeric_limits<qint16>::max();
|
||||
pair->first = std::sin(m_dingsDesHaltHochZaehlt) * std::numeric_limits<qint16>::max();
|
||||
pair->second = std::cos(m_dingsDesHaltHochZaehlt) * std::numeric_limits<qint16>::max();
|
||||
m_dingsDesHaltHochZaehlt += 0.05;
|
||||
}
|
||||
|
||||
|
@@ -46,14 +46,6 @@ MainWindow::MainWindow(QWidget *parent) :
|
||||
m_ui->widget->setFps(m_ui->comboBoxFps->currentData().toInt());
|
||||
});
|
||||
|
||||
m_ui->spinBoxBlend->setValue(m_ui->widget->blend());
|
||||
|
||||
connect(m_ui->spinBoxBlend, qOverload<int>(&QSpinBox::valueChanged), m_ui->widget, &OsciWidget::setBlend);
|
||||
|
||||
m_ui->spinBoxGlow->setValue(m_ui->widget->glow());
|
||||
|
||||
connect(m_ui->spinBoxGlow, qOverload<int>(&QSpinBox::valueChanged), m_ui->widget, &OsciWidget::setGlow);
|
||||
|
||||
auto buttonGroup = new QButtonGroup;
|
||||
buttonGroup->setExclusive(true);
|
||||
for (auto factor : { .5f, 1.f, 2.f, 4.f, 8.f })
|
||||
|
@@ -52,39 +52,6 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="horizontalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QSpinBox" name="spinBoxBlend">
|
||||
<property name="suffix">
|
||||
<string> blend</string>
|
||||
</property>
|
||||
<property name="maximum">
|
||||
<number>255</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QSpinBox" name="spinBoxGlow">
|
||||
<property name="suffix">
|
||||
<string> glow</string>
|
||||
</property>
|
||||
<property name="maximum">
|
||||
<number>65535</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="horizontalSpacer_2">
|
||||
<property name="orientation">
|
||||
|
11
oscicommon.h
Normal file
11
oscicommon.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
// Qt includes
|
||||
#include <QtGlobal>
|
||||
|
||||
template<typename T>
|
||||
struct SamplePairT {
|
||||
T first, second;
|
||||
};
|
||||
|
||||
using SamplePair = SamplePairT<qint16>;
|
@@ -16,6 +16,7 @@ HEADERS += \
|
||||
basedevice.h \
|
||||
fakedevice.h \
|
||||
mainwindow.h \
|
||||
oscicommon.h \
|
||||
osciwidget.h
|
||||
|
||||
OTHER_FILES += \
|
||||
|
157
osciwidget.cpp
157
osciwidget.cpp
@@ -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{});
|
||||
}
|
||||
|
36
osciwidget.h
36
osciwidget.h
@@ -1,52 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include <QWidget>
|
||||
// Qt includes
|
||||
#include <QOpenGLWidget>
|
||||
#include <QPixmap>
|
||||
#include <QPointF>
|
||||
#include <QElapsedTimer>
|
||||
|
||||
#include "audiodevice.h"
|
||||
// system includes
|
||||
#include <vector>
|
||||
|
||||
class OsciWidget : public QWidget
|
||||
// local includes
|
||||
#include "oscicommon.h"
|
||||
|
||||
class OsciWidget : public QOpenGLWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit OsciWidget(QWidget *parent = nullptr);
|
||||
|
||||
int blend() const;
|
||||
float factor() const;
|
||||
float glow() const;
|
||||
int fps() const;
|
||||
float factor() const { return m_factor; }
|
||||
int fps() const { return m_fps; }
|
||||
|
||||
public slots:
|
||||
void setBlend(int blend);
|
||||
void setFactor(float factor);
|
||||
void setGlow(float glow);
|
||||
void setFactor(float factor) { m_factor = factor; }
|
||||
void setFps(int fps);
|
||||
|
||||
void renderSamples(const SamplePair *begin, const SamplePair *end);
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *event) override;
|
||||
void resizeEvent(QResizeEvent *event) override;
|
||||
void timerEvent(QTimerEvent *event) override;
|
||||
|
||||
private:
|
||||
void resizePixmap();
|
||||
void createBlendPixmap();
|
||||
|
||||
int m_blend{150};
|
||||
float m_factor{4.f};
|
||||
float m_glow{512.f};
|
||||
|
||||
QPixmap m_pixmap;
|
||||
float m_factor{2.f};
|
||||
|
||||
QPointF m_lastPoint;
|
||||
|
||||
int m_frameCounter{0}, m_displayFrameCounter{0};
|
||||
int m_frameCounter{0}, m_callbacksCounter{0};
|
||||
QString m_statsDisplay;
|
||||
QElapsedTimer m_fpsTimer;
|
||||
|
||||
int m_fps{15};
|
||||
int m_redrawTimerId;
|
||||
|
||||
std::vector<SamplePair> m_buffer;
|
||||
};
|
||||
|
Reference in New Issue
Block a user