sdrangel/sdrgui/gui/tvscreen.cpp

245 wiersze
6.6 KiB
C++
Czysty Zwykły widok Historia

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 //
// (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/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
2018-02-22 21:52:49 +00:00
#include <QPainter>
#include <QMouseEvent>
#include <QOpenGLContext>
#include <QOpenGLFunctions>
#include <QSurface>
#include <QDebug>
#include <QMutexLocker>
2018-03-11 15:39:02 +00:00
#include "tvscreen.h"
2018-02-22 21:52:49 +00:00
// Note: When this object is created, QWidget* is converted to bool
TVScreen::TVScreen(bool color, QWidget* parent) :
QGLWidget(parent),
m_mutex(QMutex::Recursive),
m_glShaderArray(color)
2018-02-22 21:52:49 +00:00
{
setAttribute(Qt::WA_OpaquePaintEvent);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(tick()));
m_timer.start(40); // capped at 25 FPS
2018-02-22 21:52:49 +00:00
m_lastData = nullptr;
m_dataChanged = false;
m_glContextInitialized = false;
2018-02-22 21:52:49 +00:00
//Par défaut
m_askedCols = TV_COLS;
m_askedRows = TV_ROWS;
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
{
}
void TVScreen::setColor(bool color)
2018-03-11 18:43:40 +00:00
{
m_glShaderArray.setColor(color);
2018-03-11 18:43:40 +00:00
}
QRgb* TVScreen::getRowBuffer(int row)
2018-02-22 21:52:49 +00:00
{
if (!m_glContextInitialized) {
return nullptr;
2018-02-22 21:52:49 +00:00
}
return m_glShaderArray.GetRowBuffer(row);
2018-02-22 21:52:49 +00:00
}
void TVScreen::renderImage(unsigned char * data)
2018-02-22 21:52:49 +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
{
m_glShaderArray.ResetPixels();
2018-02-22 21:52:49 +00:00
}
void TVScreen::resetImage(int alpha)
{
m_glShaderArray.ResetPixels(alpha);
}
void TVScreen::resizeTVScreen(int cols, int intRows)
2018-02-22 21:52:49 +00:00
{
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;
2018-02-22 21:52:49 +00:00
}
void TVScreen::getSize(int& cols, int& intRows) const
{
cols = m_cols;
intRows = m_rows;
}
2018-03-11 15:39:02 +00:00
void TVScreen::initializeGL()
2018-02-22 21:52:49 +00:00
{
QMutexLocker mlock(&m_mutex);
2018-02-22 21:52:49 +00:00
QOpenGLContext *glCurrentContext = QOpenGLContext::currentContext();
2018-02-22 21:52:49 +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:"
<< " 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;
}
QSurface *surface = glCurrentContext->surface();
2018-02-22 21:52:49 +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
{
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: "
<< 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:"
<< " class: " << (surface->surfaceClass() == QSurface::Window ? "Window" : "Offscreen");
2018-02-22 21:52:49 +00:00
}
}
connect(
glCurrentContext,
&QOpenGLContext::aboutToBeDestroyed,
this,
&TVScreen::cleanup
); // TODO: when migrating to QOpenGLWidget
2018-02-22 21:52:49 +00:00
m_glContextInitialized = true;
2018-02-22 21:52:49 +00:00
}
void TVScreen::resizeGL(int width, int height)
2018-02-22 21:52:49 +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
{
if (!m_mutex.tryLock(2)) {
2018-02-22 21:52:49 +00:00
return;
}
2018-02-22 21:52:49 +00:00
m_dataChanged = false;
2018-02-22 21:52:49 +00:00
if ((m_askedCols != 0) && (m_askedRows != 0))
2018-02-22 21:52:49 +00:00
{
m_glShaderArray.initializeGL(m_askedCols, m_askedRows);
m_askedCols = 0;
m_askedRows = 0;
2018-02-22 21:52:49 +00:00
}
m_glShaderArray.RenderPixels(m_lastData);
m_mutex.unlock();
2018-02-22 21:52:49 +00:00
}
void TVScreen::mousePressEvent(QMouseEvent* event)
2018-02-22 21:52:49 +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
{
if (m_dataChanged) {
2018-02-22 21:52:49 +00:00
update();
}
}
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";
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
{
QMutexLocker mlock(&m_mutex);
if (m_glContextInitialized) {
m_glShaderArray.cleanup();
2018-02-22 21:52:49 +00:00
}
}
bool TVScreen::selectRow(int line)
2018-02-22 21:52:49 +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
}
bool TVScreen::setDataColor(int col, int red, int green, int blue)
2018-02-22 21:52:49 +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
}
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;
}
}