Skip to content

Commit

Permalink
applied some changes from threadedrender branch
Browse files Browse the repository at this point in the history
  • Loading branch information
m0dB committed Oct 17, 2022
1 parent 3adcf65 commit 60f715f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 138 deletions.
33 changes: 18 additions & 15 deletions src/widget/wglwidgetqopengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
#include "widget/wglwidget.h"

WGLWidget::WGLWidget(QWidget* parent)
: QWidget(parent), m_pOpenGLWindow(new OpenGLWindow(this)) {
QSurfaceFormat format;
// TODO @m0dB should we set any particular format flags?
m_pOpenGLWindow->setFormat(format);
m_pContainerWidget = createWindowContainer(m_pOpenGLWindow, this);
: QWidget(parent) {
}

WGLWidget::~WGLWidget() {
m_pOpenGLWindow->widgetDestroyed();
if (m_pOpenGLWindow) {
m_pOpenGLWindow->widgetDestroyed();
}
}

QPaintDevice* WGLWidget::paintDevice() {
Expand All @@ -21,28 +19,35 @@ QPaintDevice* WGLWidget::paintDevice() {
}

void WGLWidget::resizeEvent(QResizeEvent* event) {
const auto size = event->size();
m_pContainerWidget->resize(size);
if (m_pContainerWidget) {
m_pContainerWidget->resize(event->size())
}
}

void WGLWidget::handleEventFromWindow(QEvent* e) {
event(e);
}

bool WGLWidget::isContextValid() const {
return m_pOpenGLWindow->context() && m_pOpenGLWindow->context()->isValid();
return m_pOpenGLWindow && m_pOpenGLWindow->context() && m_pOpenGLWindow->context()->isValid();
}

bool WGLWidget::isContextSharing() const {
return true;
}

void WGLWidget::makeCurrentIfNeeded() {
if (m_pOpenGLWindow->context() != QOpenGLContext::currentContext()) {
if (m_pOpenGLWindow && m_pOpenGLWindow->context() != QOpenGLContext::currentContext()) {
m_pOpenGLWindow->makeCurrent();
}
}

void WGLWidget::doneCurrent() {
if (m_pOpenGLWindow) {
m_pOpenGLWindow->doneCurrent();
}
}

void WGLWidget::initializeGL() {
// to be implemented in derived widgets if needed
}
Expand All @@ -52,14 +57,12 @@ void WGLWidget::exposed() {
}

void WGLWidget::swapBuffers() {
makeCurrentIfNeeded();
if (m_pOpenGLWindow->context()) {
if (shouldRender()) {
makeCurrentIfNeeded();
m_pOpenGLWindow->context()->swapBuffers(m_pOpenGLWindow->context()->surface());
}
}

bool WGLWidget::shouldRender() const {
// TODO @m0dB this seems sufficient, but maybe there are more things
// to check here?
return m_pOpenGLWindow->isVisible();
return m_pOpenGLWindow && m_pOpenGLWindow->isExposed();
}
5 changes: 4 additions & 1 deletion src/widget/wglwidgetqopengl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ class WGLWidget : public QWidget {
bool shouldRender() const;

void makeCurrentIfNeeded();
void doneCurrent();

void swapBuffers();
void resizeEvent(QResizeEvent* event);
void resizeEvent(QResizeEvent* event) override;

void showEvent(QShowEvent* event) override;

// called by OpenGLWindow
virtual void handleEventFromWindow(QEvent* ev);
Expand Down
127 changes: 5 additions & 122 deletions src/widget/wvumeterglqopengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ WVuMeterGL::WVuMeterGL(QWidget* parent)
m_iPeakHoldTime(0),
m_iPeakFallTime(0),
m_dPeakHoldCountdownMs(0) {
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_OpaquePaintEvent);

setAutoFillBackground(false);
//setAutoBufferSwap(false);

// Not interested in repaint or update calls, as we draw from the vsync thread
setUpdatesEnabled(false);
}

void WVuMeterGL::setup(const QDomNode& node, const SkinContext& context) {
Expand Down Expand Up @@ -162,6 +154,7 @@ void WVuMeterGL::paintEvent(QPaintEvent* e) {
}

void WVuMeterGL::showEvent(QShowEvent* e) {
WGLWidget::showEvent(e);
Q_UNUSED(e);
// Find the base color recursively in parent widget.
m_qBgColor = mixxx::widgethelper::findBaseColor(this);
Expand Down Expand Up @@ -202,11 +195,11 @@ void main()\n\

QString fragmentShaderCode =
"\
uniform sampler2D m_sampler;\n\
uniform sampler2D sampler;\n\
varying vec3 vTexcoor;\n\
void main()\n\
{\n\
vec4 m_tex = texture2D(m_sampler, vec2(vTexcoor.x, vTexcoor.y));\n\
vec4 m_tex = texture2D(sampler, vec2(vTexcoor.x, vTexcoor.y));\n\
gl_FragColor = m_tex;\n\
}\n";

Expand Down Expand Up @@ -243,118 +236,7 @@ void WVuMeterGL::render(VSyncThread* vSyncThread) {

makeCurrentIfNeeded();
drawNativeGL();
}

void WVuMeterGL::draw(QPainter* painter) {
QPainter& p = *painter;
// fill the background, in case the image contains transparency
p.fillRect(rect(), m_qBgColor);

if (!m_pPixmapBack.isNull()) {
// Draw background.
QRectF sourceRect(0, 0, m_pPixmapBack->width(), m_pPixmapBack->height());
m_pPixmapBack->draw(rect(), &p, sourceRect);
}

const double widgetWidth = width();
const double widgetHeight = height();
const double pixmapWidth = m_pPixmapVu.isNull() ? 0 : m_pPixmapVu->width();
const double pixmapHeight = m_pPixmapVu.isNull() ? 0 : m_pPixmapVu->height();

// Draw (part of) vu
if (m_bHorizontal) {
{
const double widgetPosition = math_clamp(widgetWidth * m_dParameter, 0.0, widgetWidth);
QRectF targetRect(0, 0, widgetPosition, widgetHeight);

if (!m_pPixmapVu.isNull()) {
const double pixmapPosition = math_clamp(
pixmapWidth * m_dParameter, 0.0, pixmapWidth);
QRectF sourceRect(0, 0, pixmapPosition, pixmapHeight);
m_pPixmapVu->draw(targetRect, &p, sourceRect);
} else {
// fallback to green rectangle
p.fillRect(targetRect, QColor(0, 255, 0));
}
}

if (m_iPeakHoldSize > 0 && m_dPeakParameter > 0.0 &&
m_dPeakParameter > m_dParameter) {
const double widgetPeakPosition = math_clamp(
widgetWidth * m_dPeakParameter, 0.0, widgetWidth);
const double pixmapPeakHoldSize = static_cast<double>(m_iPeakHoldSize);
const double widgetPeakHoldSize = widgetWidth * pixmapPeakHoldSize / pixmapHeight;

QRectF targetRect(widgetPeakPosition - widgetPeakHoldSize,
0,
widgetPeakHoldSize,
widgetHeight);

if (!m_pPixmapVu.isNull()) {
const double pixmapPeakPosition = math_clamp(
pixmapWidth * m_dPeakParameter, 0.0, pixmapWidth);

QRectF sourceRect =
QRectF(pixmapPeakPosition - pixmapPeakHoldSize,
0,
pixmapPeakHoldSize,
pixmapHeight);
m_pPixmapVu->draw(targetRect, &p, sourceRect);
} else {
// fallback to green rectangle
p.fillRect(targetRect, QColor(0, 255, 0));
}
}
} else {
// vertical
{
const double widgetPosition =
math_clamp(widgetHeight * m_dParameter, 0.0, widgetHeight);
QRectF targetRect(0, widgetHeight - widgetPosition, widgetWidth, widgetPosition);

if (!m_pPixmapVu.isNull()) {
const double pixmapPosition = math_clamp(
pixmapHeight * m_dParameter, 0.0, pixmapHeight);
QRectF sourceRect(0, pixmapHeight - pixmapPosition, pixmapWidth, pixmapPosition);
m_pPixmapVu->draw(targetRect, &p, sourceRect);
} else {
// fallback to green rectangle
p.fillRect(targetRect, QColor(0, 255, 0));
}
}

if (m_iPeakHoldSize > 0 && m_dPeakParameter > 0.0 &&
m_dPeakParameter > m_dParameter) {
const double widgetPeakPosition = math_clamp(
widgetHeight * m_dPeakParameter, 0.0, widgetHeight);
const double pixmapPeakHoldSize = static_cast<double>(m_iPeakHoldSize);
const double widgetPeakHoldSize = widgetHeight * pixmapPeakHoldSize / pixmapHeight;

QRectF targetRect(0,
widgetHeight - widgetPeakPosition,
widgetWidth,
widgetPeakHoldSize);

if (!m_pPixmapVu.isNull()) {
const double pixmapPeakPosition = math_clamp(
pixmapHeight * m_dPeakParameter, 0.0, pixmapHeight);

QRectF sourceRect = QRectF(0,
pixmapHeight - pixmapPeakPosition,
pixmapWidth,
pixmapPeakHoldSize);
m_pPixmapVu->draw(targetRect, &p, sourceRect);
} else {
// fallback to green rectangle
p.fillRect(targetRect, QColor(0, 255, 0));
}
}
}

m_dLastParameter = m_dParameter;
m_dLastPeakParameter = m_dPeakParameter;
m_bHasRendered = true;
m_bSwapNeeded = true;
doneCurrent();
}

void WVuMeterGL::fillRectNativeGL(const QRectF& rect, const QColor& color) {
Expand Down Expand Up @@ -483,6 +365,7 @@ void WVuMeterGL::swap() {
}
makeCurrentIfNeeded();
swapBuffers();
doneCurrent();
m_bSwapNeeded = false;
}

Expand Down

0 comments on commit 60f715f

Please sign in to comment.