You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
304 lines
9.4 KiB
304 lines
9.4 KiB
/////////////////////////////////////////////////////////////////////////////////// |
|
// Copyright (C) 2017-2022 Edouard Griffiths, F4EXB <f4exb06@gmail.com> // |
|
// Copyright (C) 2020 Vort <vvort@yandex.ru> // |
|
// Copyright (C) 2022 Jiří Pinkava <jiri.pinkava@rossum.ai> // |
|
// Copyright (C) 2022 Jon Beniston, M7RCE <jon@beniston.com> // |
|
// Copyright (C) 2018 F4HKW // |
|
// for F4EXB / SDRAngel // |
|
// // |
|
// OpenGL interface modernization. // |
|
// // |
|
// This program is free software; you can redistribute it and/or modify // |
|
// it under the terms of the GNU General Public License as published by // |
|
// the Free Software Foundation as version 3 of the License, or // |
|
// (at your option) any later version. // |
|
// // |
|
// This program is distributed in the hope that it will be useful, // |
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of // |
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
|
// GNU General Public License V3 for more details. // |
|
// // |
|
// You should have received a copy of the GNU General Public License // |
|
// along with this program. If not, see <http://www.gnu.org/licenses/>. // |
|
/////////////////////////////////////////////////////////////////////////////////// |
|
|
|
#include <algorithm> |
|
|
|
#include <QPainter> |
|
#include <QMouseEvent> |
|
#include <QOpenGLContext> |
|
#include <QOpenGLFunctions> |
|
#include <QSurface> |
|
#include <QDebug> |
|
#include <QMutexLocker> |
|
|
|
#include "tvscreen.h" |
|
|
|
|
|
// Note: When this object is created, QWidget* is converted to bool |
|
TVScreen::TVScreen(bool color, QWidget* parent) : |
|
QOpenGLWidget(parent), |
|
m_alphaBlend(false), |
|
m_glShaderArray(color) |
|
{ |
|
setAttribute(Qt::WA_OpaquePaintEvent); |
|
connect(&m_timer, SIGNAL(timeout()), this, SLOT(tick())); |
|
m_timer.start(40); // capped at 25 FPS |
|
|
|
m_lastData = nullptr; |
|
m_dataChanged = false; |
|
m_glContextInitialized = false; |
|
m_framebufferNeedsPaint = true; |
|
|
|
//Par défaut |
|
m_askedCols = TV_COLS; |
|
m_askedRows = TV_ROWS; |
|
m_cols = TV_COLS; |
|
m_rows = TV_ROWS; |
|
} |
|
|
|
TVScreen::~TVScreen() |
|
{ |
|
} |
|
|
|
void TVScreen::setColor(bool color) |
|
{ |
|
m_glShaderArray.setColor(color); |
|
} |
|
|
|
void TVScreen::setAlphaBlend(bool alphaBlend) |
|
{ |
|
// Alpha blending is used to fade scope traces by drawing each frame over |
|
// the previous one. QOpenGLWidget discards its framebuffer after paintGL() |
|
// by default, which leaves undefined colours to blend with on the next |
|
// frame. Preserve it while blending is enabled. |
|
QMutexLocker mlock(&m_mutex); |
|
setUpdateBehavior(alphaBlend ? QOpenGLWidget::PartialUpdate : QOpenGLWidget::NoPartialUpdate); |
|
m_alphaBlend = alphaBlend; |
|
// Framebuffer contents may have been invalidated with NoPartialUpdate |
|
m_framebufferNeedsPaint = true; |
|
m_glShaderArray.setAlphaBlend(alphaBlend); |
|
} |
|
|
|
QRgb* TVScreen::getRowBuffer(int row) |
|
{ |
|
if (!m_glContextInitialized) { |
|
return nullptr; |
|
} |
|
|
|
return m_glShaderArray.GetRowBuffer(row); |
|
} |
|
|
|
void TVScreen::renderImage(unsigned char * data) |
|
{ |
|
{ |
|
QMutexLocker mlock(&m_mutex); |
|
m_lastData = data; |
|
m_dataChanged.store(true, std::memory_order_release); |
|
} |
|
|
|
// renderImage() is normally called by a DSP worker. Schedule the repaint |
|
// on the widget's thread instead of relying on QWidget::update() being |
|
// called from that worker. |
|
QMetaObject::invokeMethod(this, "update", Qt::QueuedConnection); |
|
} |
|
|
|
void TVScreen::resetImage() |
|
{ |
|
QMutexLocker mlock(&m_mutex); |
|
m_glShaderArray.ResetPixels(); |
|
} |
|
|
|
void TVScreen::resetImage(int alpha) |
|
{ |
|
QMutexLocker mlock(&m_mutex); |
|
m_glShaderArray.ResetPixels(alpha); |
|
} |
|
|
|
void TVScreen::resizeTVScreen(int cols, int intRows) |
|
{ |
|
qDebug("TVScreen::resizeTVScreen: cols: %d, rows: %d", cols, intRows); |
|
QMutexLocker mlock(&m_mutex); |
|
m_askedCols = cols; |
|
m_askedRows = intRows; |
|
m_cols = cols; |
|
m_rows = intRows; |
|
m_framebufferNeedsPaint = true; |
|
} |
|
|
|
void TVScreen::getSize(int& cols, int& intRows) const |
|
{ |
|
cols = m_cols; |
|
intRows = m_rows; |
|
} |
|
|
|
void TVScreen::initializeGL() |
|
{ |
|
QMutexLocker mlock(&m_mutex); |
|
|
|
QOpenGLContext *glCurrentContext = QOpenGLContext::currentContext(); |
|
|
|
if (glCurrentContext) |
|
{ |
|
if (QOpenGLContext::currentContext()->isValid()) |
|
{ |
|
qDebug() << "TVScreen::initializeGL: context:" |
|
<< " major: " << (QOpenGLContext::currentContext()->format()).majorVersion() |
|
<< " minor: " << (QOpenGLContext::currentContext()->format()).minorVersion() |
|
<< " ES: " << (QOpenGLContext::currentContext()->isOpenGLES() ? "yes" : "no"); |
|
} |
|
else |
|
{ |
|
qDebug() << "TVScreen::initializeGL: current context is invalid"; |
|
} |
|
} |
|
else |
|
{ |
|
qCritical() << "TVScreen::initializeGL: no current context"; |
|
return; |
|
} |
|
|
|
QSurface *surface = glCurrentContext->surface(); |
|
|
|
if (surface == nullptr) |
|
{ |
|
qCritical() << "TVScreen::initializeGL: no surface attached"; |
|
return; |
|
} |
|
else |
|
{ |
|
if (surface->surfaceType() != QSurface::OpenGLSurface) |
|
{ |
|
qCritical() << "TVScreen::initializeGL: surface is not an OpenGLSurface: " |
|
<< surface->surfaceType() |
|
<< " cannot use an OpenGL context"; |
|
return; |
|
} |
|
else |
|
{ |
|
qDebug() << "TVScreen::initializeGL: OpenGL surface:" |
|
<< " class: " << (surface->surfaceClass() == QSurface::Window ? "Window" : "Offscreen"); |
|
} |
|
} |
|
|
|
connect( |
|
glCurrentContext, |
|
&QOpenGLContext::aboutToBeDestroyed, |
|
this, |
|
&TVScreen::cleanup |
|
); |
|
|
|
m_glContextInitialized = true; |
|
m_framebufferNeedsPaint = true; |
|
} |
|
|
|
void TVScreen::resizeGL(int width, int height) |
|
{ |
|
QMutexLocker mlock(&m_mutex); |
|
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); |
|
f->glViewport(0, 0, width, height); |
|
m_framebufferNeedsPaint = true; |
|
} |
|
|
|
void TVScreen::paintGL() |
|
{ |
|
// Other threads only hold the mutex briefly, so wait for it rather than |
|
// skipping the paint, which would leave Qt compositing a stale or |
|
// invalidated framebuffer. |
|
m_mutex.lock(); |
|
|
|
if ((m_askedCols != 0) && (m_askedRows != 0)) |
|
{ |
|
int major = 0, minor = 0; |
|
if (QOpenGLContext::currentContext()) |
|
{ |
|
major = QOpenGLContext::currentContext()->format().majorVersion(); |
|
minor = QOpenGLContext::currentContext()->format().minorVersion(); |
|
} |
|
m_glShaderArray.initializeGL(major, minor, m_askedCols, m_askedRows); |
|
m_askedCols = 0; |
|
m_askedRows = 0; |
|
} |
|
|
|
if (m_framebufferNeedsPaint) |
|
{ |
|
// QOpenGLWidget backing stores have an alpha channel. Initialize a new |
|
// or resized one even when no constellation or video frame is ready, |
|
// otherwise Qt can composite the uninitialized buffer as transparent. |
|
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); |
|
f->glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
|
f->glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
|
f->glClear(GL_COLOR_BUFFER_BIT); |
|
} |
|
|
|
// With PartialUpdate the previous framebuffer is deliberately retained. |
|
// Do not blend the same fading image again for incidental repaints, such |
|
// as mouse hover or moving another overlay over this widget. |
|
const bool dataChanged = m_dataChanged.exchange(false, std::memory_order_acq_rel); |
|
|
|
if (m_alphaBlend && !dataChanged && !m_framebufferNeedsPaint) |
|
{ |
|
m_mutex.unlock(); |
|
return; |
|
} |
|
|
|
m_glShaderArray.RenderPixels(m_lastData); |
|
m_framebufferNeedsPaint = false; |
|
m_mutex.unlock(); |
|
} |
|
|
|
void TVScreen::mousePressEvent(QMouseEvent* event) |
|
{ |
|
(void) event; |
|
} |
|
|
|
void TVScreen::tick() |
|
{ |
|
if (m_dataChanged.load(std::memory_order_acquire)) { |
|
update(); |
|
} |
|
} |
|
|
|
void TVScreen::connectTimer(const QTimer& timer) |
|
{ |
|
qDebug() << "TVScreen::connectTimer"; |
|
disconnect(&m_timer, SIGNAL(timeout()), this, SLOT(tick())); |
|
connect(&timer, SIGNAL(timeout()), this, SLOT(tick())); |
|
m_timer.stop(); |
|
} |
|
|
|
void TVScreen::cleanup() |
|
{ |
|
QMutexLocker mlock(&m_mutex); |
|
|
|
if (m_glContextInitialized) { |
|
m_glShaderArray.cleanup(); |
|
} |
|
} |
|
|
|
bool TVScreen::selectRow(int line) |
|
{ |
|
if (m_glContextInitialized) { |
|
return m_glShaderArray.SelectRow(line); |
|
} else { |
|
return false; |
|
} |
|
} |
|
|
|
bool TVScreen::setDataColor(int col, int red, int green, int blue) |
|
{ |
|
if (m_glContextInitialized) { |
|
return m_glShaderArray.SetDataColor(col, qRgb(blue, green, red)); // FIXME: blue <> red inversion in shader |
|
} else { |
|
return false; |
|
} |
|
} |
|
|
|
bool TVScreen::setDataColor(int col, int red, int green, int blue, int alpha) |
|
{ |
|
if (m_glContextInitialized) { |
|
return m_glShaderArray.SetDataColor(col, qRgba(blue, green, red, alpha)); // FIXME: blue <> red inversion in shader |
|
} else { |
|
return false; |
|
} |
|
}
|
|
|