1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-08 17:46:03 -05:00
sdrangel/sdrgui/gui/tvscreen.cpp

245 lines
6.7 KiB
C++
Raw Normal View History

2018-02-22 16:52:49 -05: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 //
// //
// 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 <QPainter>
#include <QMouseEvent>
#include <QOpenGLContext>
#include <QOpenGLFunctions>
#include <QSurface>
2018-03-11 11:39:02 -04:00
#include "tvscreen.h"
2018-02-22 16:52:49 -05:00
#include <algorithm>
#include <QDebug>
2018-03-11 11:39:02 -04:00
TVScreen::TVScreen(bool blnColor, QWidget* parent) :
QGLWidget(parent), m_objMutex(QMutex::NonRecursive), m_objGLShaderArray(blnColor)
2018-02-22 16:52:49 -05:00
{
setAttribute(Qt::WA_OpaquePaintEvent);
connect(&m_objTimer, SIGNAL(timeout()), this, SLOT(tick()));
m_objTimer.start(40); // capped at 25 FPS
m_chrLastData = NULL;
m_blnConfigChanged = false;
m_blnDataChanged = false;
m_blnGLContextInitialized = false;
//Par défaut
2018-03-11 11:39:02 -04:00
m_intAskedCols = TV_COLS;
m_intAskedRows = TV_ROWS;
m_cols = TV_COLS;
m_rows = TV_ROWS;
2018-02-22 16:52:49 -05:00
}
2018-03-11 11:39:02 -04:00
TVScreen::~TVScreen()
2018-02-22 16:52:49 -05:00
{
cleanup();
}
2018-03-11 14:43:40 -04:00
void TVScreen::setColor(bool blnColor)
{
m_objGLShaderArray.setColor(blnColor);
}
2018-03-11 11:39:02 -04:00
QRgb* TVScreen::getRowBuffer(int intRow)
2018-02-22 16:52:49 -05:00
{
2018-02-25 19:04:45 -05:00
if (!m_blnGLContextInitialized)
2018-02-22 16:52:49 -05:00
{
return NULL;
}
return m_objGLShaderArray.GetRowBuffer(intRow);
}
2018-03-11 11:39:02 -04:00
void TVScreen::renderImage(unsigned char * objData)
2018-02-22 16:52:49 -05:00
{
m_chrLastData = objData;
m_blnDataChanged = true;
}
2018-03-11 11:39:02 -04:00
void TVScreen::resetImage()
2018-02-22 16:52:49 -05:00
{
m_objGLShaderArray.ResetPixels();
}
void TVScreen::resetImage(int alpha)
{
m_objGLShaderArray.ResetPixels(alpha);
}
2018-03-11 11:39:02 -04:00
void TVScreen::resizeTVScreen(int intCols, int intRows)
2018-02-22 16:52:49 -05:00
{
qDebug("TVScreen::resizeTVScreen: cols: %d, rows: %d", intCols, intRows);
2018-02-22 16:52:49 -05:00
m_intAskedCols = intCols;
m_intAskedRows = intRows;
m_cols = intCols;
m_rows = intRows;
2018-02-22 16:52:49 -05:00
}
void TVScreen::getSize(int& intCols, int& intRows) const
{
intCols = m_cols;
intRows = m_rows;
}
2018-03-11 11:39:02 -04:00
void TVScreen::initializeGL()
2018-02-22 16:52:49 -05:00
{
m_objMutex.lock();
QOpenGLContext *objGlCurrentContext = QOpenGLContext::currentContext();
if (objGlCurrentContext)
{
if (QOpenGLContext::currentContext()->isValid())
{
2018-03-16 05:11:32 -04:00
qDebug() << "TVScreen::initializeGL: context:"
2018-02-22 16:52:49 -05:00
<< " major: " << (QOpenGLContext::currentContext()->format()).majorVersion()
<< " minor: " << (QOpenGLContext::currentContext()->format()).minorVersion()
<< " ES: " << (QOpenGLContext::currentContext()->isOpenGLES() ? "yes" : "no");
}
else
{
2018-03-16 05:11:32 -04:00
qDebug() << "TVScreen::initializeGL: current context is invalid";
2018-02-22 16:52:49 -05:00
}
}
else
{
2018-03-16 05:11:32 -04:00
qCritical() << "TVScreen::initializeGL: no current context";
2018-02-22 16:52:49 -05:00
return;
}
QSurface *objSurface = objGlCurrentContext->surface();
if (objSurface == NULL)
{
2018-03-16 05:11:32 -04:00
qCritical() << "TVScreen::initializeGL: no surface attached";
2018-02-22 16:52:49 -05:00
return;
}
else
{
if (objSurface->surfaceType() != QSurface::OpenGLSurface)
{
2018-03-16 05:11:32 -04:00
qCritical() << "TVScreen::initializeGL: surface is not an OpenGLSurface: "
2018-02-22 16:52:49 -05:00
<< objSurface->surfaceType()
<< " cannot use an OpenGL context";
return;
}
else
{
2018-03-16 05:11:32 -04:00
qDebug() << "TVScreen::initializeGL: OpenGL surface:"
2018-02-22 16:52:49 -05:00
<< " class: " << (objSurface->surfaceClass() == QSurface::Window ? "Window" : "Offscreen");
}
}
connect(objGlCurrentContext, &QOpenGLContext::aboutToBeDestroyed, this,
2018-03-11 11:39:02 -04:00
&TVScreen::cleanup); // TODO: when migrating to QOpenGLWidget
2018-02-22 16:52:49 -05:00
m_blnGLContextInitialized = true;
m_objMutex.unlock();
}
2018-03-11 11:39:02 -04:00
void TVScreen::resizeGL(int intWidth, int intHeight)
2018-02-22 16:52:49 -05:00
{
QOpenGLFunctions *ptrF = QOpenGLContext::currentContext()->functions();
ptrF->glViewport(0, 0, intWidth, intHeight);
m_blnConfigChanged = true;
}
2018-03-11 11:39:02 -04:00
void TVScreen::paintGL()
2018-02-22 16:52:49 -05:00
{
if (!m_objMutex.tryLock(2))
return;
m_blnDataChanged = false;
if ((m_intAskedCols != 0) && (m_intAskedRows != 0))
{
m_objGLShaderArray.InitializeGL(m_intAskedCols, m_intAskedRows);
m_intAskedCols = 0;
m_intAskedRows = 0;
}
m_objGLShaderArray.RenderPixels(m_chrLastData);
m_objMutex.unlock();
}
2018-03-11 11:39:02 -04:00
void TVScreen::mousePressEvent(QMouseEvent* event __attribute__((unused)))
2018-02-22 16:52:49 -05:00
{
}
2018-03-11 11:39:02 -04:00
void TVScreen::tick()
2018-02-22 16:52:49 -05:00
{
if (m_blnDataChanged) {
update();
}
}
2018-03-11 11:39:02 -04:00
void TVScreen::connectTimer(const QTimer& objTimer)
2018-02-22 16:52:49 -05:00
{
2018-03-16 05:11:32 -04:00
qDebug() << "TVScreen::connectTimer";
2018-02-22 16:52:49 -05:00
disconnect(&m_objTimer, SIGNAL(timeout()), this, SLOT(tick()));
connect(&objTimer, SIGNAL(timeout()), this, SLOT(tick()));
m_objTimer.stop();
}
2018-03-11 11:39:02 -04:00
void TVScreen::cleanup()
2018-02-22 16:52:49 -05:00
{
if (m_blnGLContextInitialized)
{
m_objGLShaderArray.Cleanup();
}
}
2018-03-11 11:39:02 -04:00
bool TVScreen::selectRow(int intLine)
2018-02-22 16:52:49 -05:00
{
if (m_blnGLContextInitialized)
{
return m_objGLShaderArray.SelectRow(intLine);
}
2018-02-25 19:04:45 -05:00
else
{
return false;
}
2018-02-22 16:52:49 -05:00
}
2018-03-11 11:39:02 -04:00
bool TVScreen::setDataColor(int intCol, int intRed, int intGreen, int intBlue)
2018-02-22 16:52:49 -05:00
{
if (m_blnGLContextInitialized)
2018-02-25 19:04:45 -05:00
{
return m_objGLShaderArray.SetDataColor(intCol, qRgb(intBlue, intGreen, intRed)); // FIXME: blue <> red inversion in shader
2018-02-25 19:04:45 -05:00
}
else
{
return false;
}
2018-02-22 16:52:49 -05:00
}
bool TVScreen::setDataColor(int intCol, int intRed, int intGreen, int intBlue, int intAlpha)
{
if (m_blnGLContextInitialized)
{
return m_objGLShaderArray.SetDataColor(intCol, qRgba(intBlue, intGreen, intRed, intAlpha)); // FIXME: blue <> red inversion in shader
}
else
{
return false;
}
}