1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-18 14:21:49 -05:00
sdrangel/plugins/channelrx/demodatv/atvscreen.cpp

211 lines
5.9 KiB
C++
Raw Normal View History

2017-02-23 02:18:56 -05:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2017 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>
#include "atvscreen.h"
#include <algorithm>
#include <QDebug>
ATVScreen::ATVScreen(QWidget* parent) :
2017-02-24 23:10:54 -05:00
QGLWidget(parent), m_objMutex(QMutex::NonRecursive)
2017-02-23 02:18:56 -05:00
{
2017-02-24 23:10:54 -05:00
setAttribute(Qt::WA_OpaquePaintEvent);
connect(&m_objTimer, SIGNAL(timeout()), this, SLOT(tick()));
m_objTimer.start(40); // capped at 25 FPS
2017-02-23 02:18:56 -05:00
2017-02-24 23:10:54 -05:00
m_chrLastData = NULL;
m_blnConfigChanged = false;
m_blnDataChanged = false;
m_blnRenderImmediate = false;
2017-02-24 23:10:54 -05:00
m_blnGLContextInitialized = false;
2017-02-23 02:18:56 -05:00
//Par défaut
m_intAskedCols = ATV_COLS;
m_intAskedRows = ATV_ROWS;
2017-02-23 02:18:56 -05:00
}
ATVScreen::~ATVScreen()
2017-02-24 23:10:54 -05:00
{
cleanup();
2017-02-23 02:18:56 -05:00
}
QRgb* ATVScreen::getRowBuffer(int intRow)
{
2017-02-24 23:10:54 -05:00
if (m_blnGLContextInitialized == false)
2017-02-23 02:18:56 -05:00
{
return NULL;
}
return m_objGLShaderArray.GetRowBuffer(intRow);
}
void ATVScreen::renderImage(unsigned char * objData)
2017-02-24 23:10:54 -05:00
{
m_chrLastData = objData;
m_blnDataChanged = true;
if (m_blnRenderImmediate) update();
2017-02-23 02:18:56 -05:00
}
void ATVScreen::resetImage()
2017-02-24 23:10:54 -05:00
{
2017-02-23 02:18:56 -05:00
m_objGLShaderArray.ResetPixels();
}
void ATVScreen::resizeATVScreen(int intCols, int intRows)
{
2017-02-24 23:10:54 -05:00
m_intAskedCols = intCols;
m_intAskedRows = intRows;
2017-02-23 02:18:56 -05:00
}
void ATVScreen::initializeGL()
{
m_objMutex.lock();
2017-02-24 23:10:54 -05:00
QOpenGLContext *objGlCurrentContext = QOpenGLContext::currentContext();
2017-02-23 02:18:56 -05:00
if (objGlCurrentContext)
{
if (QOpenGLContext::currentContext()->isValid())
{
2017-02-24 23:10:54 -05:00
qDebug() << "ATVScreen::initializeGL: context:"
<< " major: " << (QOpenGLContext::currentContext()->format()).majorVersion()
<< " minor: " << (QOpenGLContext::currentContext()->format()).minorVersion()
<< " ES: " << (QOpenGLContext::currentContext()->isOpenGLES() ? "yes" : "no");
}
2017-02-23 02:18:56 -05:00
else
{
2017-02-24 23:10:54 -05:00
qDebug() << "ATVScreen::initializeGL: current context is invalid";
}
2017-02-23 02:18:56 -05:00
}
else
{
2017-02-24 23:10:54 -05:00
qCritical() << "ATVScreen::initializeGL: no current context";
return;
}
2017-02-23 02:18:56 -05:00
QSurface *objSurface = objGlCurrentContext->surface();
if (objSurface == NULL)
2017-02-24 23:10:54 -05:00
{
qCritical() << "ATVScreen::initializeGL: no surface attached";
return;
}
else
{
2017-02-23 02:18:56 -05:00
if (objSurface->surfaceType() != QSurface::OpenGLSurface)
2017-02-24 23:10:54 -05:00
{
qCritical() << "ATVScreen::initializeGL: surface is not an OpenGLSurface: "
<< objSurface->surfaceType()
<< " cannot use an OpenGL context";
return;
}
else
{
qDebug() << "ATVScreen::initializeGL: OpenGL surface:"
<< " class: " << (objSurface->surfaceClass() == QSurface::Window ? "Window" : "Offscreen");
}
}
connect(objGlCurrentContext, &QOpenGLContext::aboutToBeDestroyed, this,
&ATVScreen::cleanup); // TODO: when migrating to QOpenGLWidget
2017-02-23 02:18:56 -05:00
2017-02-24 23:10:54 -05:00
m_blnGLContextInitialized = true;
2017-02-23 02:18:56 -05:00
2017-02-24 23:10:54 -05:00
m_objMutex.unlock();
2017-02-23 02:18:56 -05:00
}
void ATVScreen::resizeGL(int intWidth, int intHeight)
2017-02-24 23:10:54 -05:00
{
2017-02-23 02:18:56 -05:00
QOpenGLFunctions *ptrF = QOpenGLContext::currentContext()->functions();
ptrF->glViewport(0, 0, intWidth, intHeight);
m_blnConfigChanged = true;
}
void ATVScreen::paintGL()
2017-02-24 23:10:54 -05:00
{
if (!m_objMutex.tryLock(2))
return;
2017-02-23 02:18:56 -05:00
m_blnDataChanged = false;
if ((m_intAskedCols != 0) && (m_intAskedRows != 0))
2017-02-23 02:18:56 -05:00
{
m_objGLShaderArray.InitializeGL(m_intAskedCols, m_intAskedRows);
m_intAskedCols = 0;
m_intAskedRows = 0;
2017-02-23 02:18:56 -05:00
}
m_objGLShaderArray.RenderPixels(m_chrLastData);
2017-02-23 02:18:56 -05:00
m_objMutex.unlock();
}
2017-05-25 14:13:34 -04:00
void ATVScreen::mousePressEvent(QMouseEvent* event __attribute__((unused)))
2017-02-23 02:18:56 -05:00
{
}
void ATVScreen::tick()
2017-02-24 23:10:54 -05:00
{
if (m_blnDataChanged) {
update();
}
2017-02-23 02:18:56 -05:00
}
void ATVScreen::connectTimer(const QTimer& objTimer)
{
2017-02-24 23:10:54 -05:00
qDebug() << "ATVScreen::connectTimer";
disconnect(&m_objTimer, SIGNAL(timeout()), this, SLOT(tick()));
connect(&objTimer, SIGNAL(timeout()), this, SLOT(tick()));
m_objTimer.stop();
2017-02-23 02:18:56 -05:00
}
void ATVScreen::cleanup()
2017-02-24 23:10:54 -05:00
{
if (m_blnGLContextInitialized)
2017-02-23 02:18:56 -05:00
{
m_objGLShaderArray.Cleanup();
}
}
bool ATVScreen::selectRow(int intLine)
{
2017-02-24 23:10:54 -05:00
if (m_blnGLContextInitialized)
2017-02-23 02:18:56 -05:00
{
return m_objGLShaderArray.SelectRow(intLine);
}
2017-05-05 04:40:45 -04:00
return false;
2017-02-23 02:18:56 -05:00
}
2017-02-24 23:10:54 -05:00
bool ATVScreen::setDataColor(int intCol, int intRed, int intGreen, int intBlue)
2017-02-23 02:18:56 -05:00
{
2017-02-24 23:10:54 -05:00
if (m_blnGLContextInitialized)
2017-02-23 02:18:56 -05:00
{
2017-02-24 23:10:54 -05:00
return m_objGLShaderArray.SetDataColor(intCol,
qRgb(intRed, intGreen, intBlue));
2017-02-23 02:18:56 -05:00
}
2017-05-05 04:40:45 -04:00
return false;
2017-02-23 02:18:56 -05:00
}