2018-02-22 21:52:49 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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 //
|
2019-04-11 12:43:33 +00:00
|
|
|
// (at your option) any later version. //
|
2018-02-22 21:52:49 +00:00
|
|
|
// //
|
|
|
|
// 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/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2018-02-22 21:52:49 +00:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QOpenGLContext>
|
|
|
|
#include <QOpenGLFunctions>
|
|
|
|
#include <QSurface>
|
2021-04-07 02:32:31 +00:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QMutexLocker>
|
|
|
|
|
2018-03-11 15:39:02 +00:00
|
|
|
#include "tvscreen.h"
|
2018-02-22 21:52:49 +00:00
|
|
|
|
|
|
|
|
2020-07-19 11:07:04 +00:00
|
|
|
// Note: When this object is created, QWidget* is converted to bool
|
2021-04-07 02:32:31 +00:00
|
|
|
TVScreen::TVScreen(bool color, QWidget* parent) :
|
2022-03-18 17:13:08 +00:00
|
|
|
QOpenGLWidget(parent),
|
2021-04-07 02:32:31 +00:00
|
|
|
m_glShaderArray(color)
|
2018-02-22 21:52:49 +00:00
|
|
|
{
|
|
|
|
setAttribute(Qt::WA_OpaquePaintEvent);
|
2021-04-07 02:32:31 +00:00
|
|
|
connect(&m_timer, SIGNAL(timeout()), this, SLOT(tick()));
|
|
|
|
m_timer.start(40); // capped at 25 FPS
|
2018-02-22 21:52:49 +00:00
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
m_lastData = nullptr;
|
|
|
|
m_dataChanged = false;
|
|
|
|
m_glContextInitialized = false;
|
2018-02-22 21:52:49 +00:00
|
|
|
|
|
|
|
//Par défaut
|
2021-04-07 02:32:31 +00:00
|
|
|
m_askedCols = TV_COLS;
|
|
|
|
m_askedRows = TV_ROWS;
|
2018-03-14 23:16:50 +00:00
|
|
|
m_cols = TV_COLS;
|
|
|
|
m_rows = TV_ROWS;
|
2018-02-22 21:52:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 15:39:02 +00:00
|
|
|
TVScreen::~TVScreen()
|
2018-02-22 21:52:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
void TVScreen::setColor(bool color)
|
2018-03-11 18:43:40 +00:00
|
|
|
{
|
2021-04-07 02:32:31 +00:00
|
|
|
m_glShaderArray.setColor(color);
|
2018-03-11 18:43:40 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
QRgb* TVScreen::getRowBuffer(int row)
|
2018-02-22 21:52:49 +00:00
|
|
|
{
|
2021-04-07 02:32:31 +00:00
|
|
|
if (!m_glContextInitialized) {
|
2020-12-12 19:04:18 +00:00
|
|
|
return nullptr;
|
2018-02-22 21:52:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
return m_glShaderArray.GetRowBuffer(row);
|
2018-02-22 21:52:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
void TVScreen::renderImage(unsigned char * data)
|
2018-02-22 21:52:49 +00:00
|
|
|
{
|
2021-04-07 02:32:31 +00:00
|
|
|
m_lastData = data;
|
|
|
|
m_dataChanged = true;
|
2018-02-22 21:52:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 15:39:02 +00:00
|
|
|
void TVScreen::resetImage()
|
2018-02-22 21:52:49 +00:00
|
|
|
{
|
2021-04-07 02:32:31 +00:00
|
|
|
m_glShaderArray.ResetPixels();
|
2018-02-22 21:52:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 00:39:43 +00:00
|
|
|
void TVScreen::resetImage(int alpha)
|
|
|
|
{
|
2021-04-07 02:32:31 +00:00
|
|
|
m_glShaderArray.ResetPixels(alpha);
|
2018-03-13 00:39:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
void TVScreen::resizeTVScreen(int cols, int intRows)
|
2018-02-22 21:52:49 +00:00
|
|
|
{
|
2021-04-07 02:32:31 +00:00
|
|
|
qDebug("TVScreen::resizeTVScreen: cols: %d, rows: %d", cols, intRows);
|
|
|
|
QMutexLocker mlock(&m_mutex);
|
|
|
|
m_askedCols = cols;
|
|
|
|
m_askedRows = intRows;
|
|
|
|
m_cols = cols;
|
2018-03-14 23:16:50 +00:00
|
|
|
m_rows = intRows;
|
2018-02-22 21:52:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
void TVScreen::getSize(int& cols, int& intRows) const
|
2018-03-12 04:07:51 +00:00
|
|
|
{
|
2021-04-07 02:32:31 +00:00
|
|
|
cols = m_cols;
|
2018-03-14 23:16:50 +00:00
|
|
|
intRows = m_rows;
|
2018-03-12 04:07:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 15:39:02 +00:00
|
|
|
void TVScreen::initializeGL()
|
2018-02-22 21:52:49 +00:00
|
|
|
{
|
2021-04-07 02:32:31 +00:00
|
|
|
QMutexLocker mlock(&m_mutex);
|
2018-02-22 21:52:49 +00:00
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
QOpenGLContext *glCurrentContext = QOpenGLContext::currentContext();
|
2018-02-22 21:52:49 +00:00
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
if (glCurrentContext)
|
2018-02-22 21:52:49 +00:00
|
|
|
{
|
|
|
|
if (QOpenGLContext::currentContext()->isValid())
|
|
|
|
{
|
2018-03-16 09:11:32 +00:00
|
|
|
qDebug() << "TVScreen::initializeGL: context:"
|
2021-04-07 02:32:31 +00:00
|
|
|
<< " major: " << (QOpenGLContext::currentContext()->format()).majorVersion()
|
|
|
|
<< " minor: " << (QOpenGLContext::currentContext()->format()).minorVersion()
|
|
|
|
<< " ES: " << (QOpenGLContext::currentContext()->isOpenGLES() ? "yes" : "no");
|
2018-02-22 21:52:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-16 09:11:32 +00:00
|
|
|
qDebug() << "TVScreen::initializeGL: current context is invalid";
|
2018-02-22 21:52:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-16 09:11:32 +00:00
|
|
|
qCritical() << "TVScreen::initializeGL: no current context";
|
2018-02-22 21:52:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
QSurface *surface = glCurrentContext->surface();
|
2018-02-22 21:52:49 +00:00
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
if (surface == nullptr)
|
2018-02-22 21:52:49 +00:00
|
|
|
{
|
2018-03-16 09:11:32 +00:00
|
|
|
qCritical() << "TVScreen::initializeGL: no surface attached";
|
2018-02-22 21:52:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-04-07 02:32:31 +00:00
|
|
|
if (surface->surfaceType() != QSurface::OpenGLSurface)
|
2018-02-22 21:52:49 +00:00
|
|
|
{
|
2018-03-16 09:11:32 +00:00
|
|
|
qCritical() << "TVScreen::initializeGL: surface is not an OpenGLSurface: "
|
2021-04-07 02:32:31 +00:00
|
|
|
<< surface->surfaceType()
|
|
|
|
<< " cannot use an OpenGL context";
|
2018-02-22 21:52:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-16 09:11:32 +00:00
|
|
|
qDebug() << "TVScreen::initializeGL: OpenGL surface:"
|
2021-04-07 02:32:31 +00:00
|
|
|
<< " class: " << (surface->surfaceClass() == QSurface::Window ? "Window" : "Offscreen");
|
2018-02-22 21:52:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
connect(
|
|
|
|
glCurrentContext,
|
|
|
|
&QOpenGLContext::aboutToBeDestroyed,
|
|
|
|
this,
|
|
|
|
&TVScreen::cleanup
|
2022-03-18 17:13:08 +00:00
|
|
|
);
|
2018-02-22 21:52:49 +00:00
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
m_glContextInitialized = true;
|
2018-02-22 21:52:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
void TVScreen::resizeGL(int width, int height)
|
2018-02-22 21:52:49 +00:00
|
|
|
{
|
2021-04-07 02:32:31 +00:00
|
|
|
QMutexLocker mlock(&m_mutex);
|
|
|
|
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
|
|
|
|
f->glViewport(0, 0, width, height);
|
2018-02-22 21:52:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 15:39:02 +00:00
|
|
|
void TVScreen::paintGL()
|
2018-02-22 21:52:49 +00:00
|
|
|
{
|
2021-04-07 02:32:31 +00:00
|
|
|
if (!m_mutex.tryLock(2)) {
|
2018-02-22 21:52:49 +00:00
|
|
|
return;
|
2021-04-07 02:32:31 +00:00
|
|
|
}
|
2018-02-22 21:52:49 +00:00
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
m_dataChanged = false;
|
2018-02-22 21:52:49 +00:00
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
if ((m_askedCols != 0) && (m_askedRows != 0))
|
2018-02-22 21:52:49 +00:00
|
|
|
{
|
2022-06-19 22:22:43 +00:00
|
|
|
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);
|
2021-04-07 02:32:31 +00:00
|
|
|
m_askedCols = 0;
|
|
|
|
m_askedRows = 0;
|
2018-02-22 21:52:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
m_glShaderArray.RenderPixels(m_lastData);
|
|
|
|
m_mutex.unlock();
|
2018-02-22 21:52:49 +00:00
|
|
|
}
|
|
|
|
|
2018-11-12 17:36:27 +00:00
|
|
|
void TVScreen::mousePressEvent(QMouseEvent* event)
|
2018-02-22 21:52:49 +00:00
|
|
|
{
|
2018-11-12 17:36:27 +00:00
|
|
|
(void) event;
|
2018-02-22 21:52:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 15:39:02 +00:00
|
|
|
void TVScreen::tick()
|
2018-02-22 21:52:49 +00:00
|
|
|
{
|
2021-04-07 02:32:31 +00:00
|
|
|
if (m_dataChanged) {
|
2018-02-22 21:52:49 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
void TVScreen::connectTimer(const QTimer& timer)
|
2018-02-22 21:52:49 +00:00
|
|
|
{
|
2018-03-16 09:11:32 +00:00
|
|
|
qDebug() << "TVScreen::connectTimer";
|
2021-04-07 02:32:31 +00:00
|
|
|
disconnect(&m_timer, SIGNAL(timeout()), this, SLOT(tick()));
|
|
|
|
connect(&timer, SIGNAL(timeout()), this, SLOT(tick()));
|
|
|
|
m_timer.stop();
|
2018-02-22 21:52:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 15:39:02 +00:00
|
|
|
void TVScreen::cleanup()
|
2018-02-22 21:52:49 +00:00
|
|
|
{
|
2021-04-07 02:32:31 +00:00
|
|
|
QMutexLocker mlock(&m_mutex);
|
|
|
|
|
|
|
|
if (m_glContextInitialized) {
|
|
|
|
m_glShaderArray.cleanup();
|
2018-02-22 21:52:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
bool TVScreen::selectRow(int line)
|
2018-02-22 21:52:49 +00:00
|
|
|
{
|
2021-04-07 02:32:31 +00:00
|
|
|
if (m_glContextInitialized) {
|
|
|
|
return m_glShaderArray.SelectRow(line);
|
|
|
|
} else {
|
2018-02-26 00:04:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-02-22 21:52:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
bool TVScreen::setDataColor(int col, int red, int green, int blue)
|
2018-02-22 21:52:49 +00:00
|
|
|
{
|
2021-04-07 02:32:31 +00:00
|
|
|
if (m_glContextInitialized) {
|
|
|
|
return m_glShaderArray.SetDataColor(col, qRgb(blue, green, red)); // FIXME: blue <> red inversion in shader
|
|
|
|
} else {
|
2018-02-26 00:04:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-02-22 21:52:49 +00:00
|
|
|
}
|
2018-03-12 19:39:16 +00:00
|
|
|
|
2021-04-07 02:32:31 +00:00
|
|
|
bool TVScreen::setDataColor(int col, int red, int green, int blue, int alpha)
|
2018-03-12 19:39:16 +00:00
|
|
|
{
|
2021-04-07 02:32:31 +00:00
|
|
|
if (m_glContextInitialized) {
|
|
|
|
return m_glShaderArray.SetDataColor(col, qRgba(blue, green, red, alpha)); // FIXME: blue <> red inversion in shader
|
|
|
|
} else {
|
2018-03-12 19:39:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|