| 
									
										
										
										
											2018-02-22 22:52:49 +01: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 14:43:33 +02:00
										 |  |  | // (at your option) any later version.                                           //
 | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01: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 <QPainter>
 | 
					
						
							|  |  |  | #include <QMouseEvent>
 | 
					
						
							|  |  |  | #include <QOpenGLContext>
 | 
					
						
							|  |  |  | #include <QOpenGLFunctions>
 | 
					
						
							|  |  |  | #include <QSurface>
 | 
					
						
							| 
									
										
										
										
											2018-03-11 16:39:02 +01:00
										 |  |  | #include "tvscreen.h"
 | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <algorithm>
 | 
					
						
							|  |  |  | #include <QDebug>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-19 14:07:04 +03:00
										 |  |  | // Note: When this object is created, QWidget* is converted to bool
 | 
					
						
							| 
									
										
										
										
											2018-03-11 16:39:02 +01:00
										 |  |  | TVScreen::TVScreen(bool blnColor, QWidget* parent) : | 
					
						
							|  |  |  |         QGLWidget(parent), m_objMutex(QMutex::NonRecursive), m_objGLShaderArray(blnColor) | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01: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 16:39:02 +01:00
										 |  |  |     m_intAskedCols = TV_COLS; | 
					
						
							|  |  |  |     m_intAskedRows = TV_ROWS; | 
					
						
							| 
									
										
										
										
											2018-03-15 00:16:50 +01:00
										 |  |  |     m_cols = TV_COLS; | 
					
						
							|  |  |  |     m_rows = TV_ROWS; | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-11 16:39:02 +01:00
										 |  |  | TVScreen::~TVScreen() | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     cleanup(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-11 19:43:40 +01:00
										 |  |  | void TVScreen::setColor(bool blnColor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	m_objGLShaderArray.setColor(blnColor); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-11 16:39:02 +01:00
										 |  |  | QRgb* TVScreen::getRowBuffer(int intRow) | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-02-26 01:04:45 +01:00
										 |  |  |     if (!m_blnGLContextInitialized) | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return m_objGLShaderArray.GetRowBuffer(intRow); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 19:31:47 +03:00
										 |  |  | void TVScreen::renderImage(unsigned char * objData) | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     m_chrLastData = objData; | 
					
						
							|  |  |  |     m_blnDataChanged = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-11 16:39:02 +01:00
										 |  |  | void TVScreen::resetImage() | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     m_objGLShaderArray.ResetPixels(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-13 01:39:43 +01:00
										 |  |  | void TVScreen::resetImage(int alpha) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_objGLShaderArray.ResetPixels(alpha); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-11 16:39:02 +01:00
										 |  |  | void TVScreen::resizeTVScreen(int intCols, int intRows) | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-15 00:16:50 +01:00
										 |  |  |     qDebug("TVScreen::resizeTVScreen: cols: %d, rows: %d", intCols, intRows); | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  |     m_intAskedCols = intCols; | 
					
						
							|  |  |  |     m_intAskedRows = intRows; | 
					
						
							| 
									
										
										
										
											2018-03-15 00:16:50 +01:00
										 |  |  |     m_cols = intCols; | 
					
						
							|  |  |  |     m_rows = intRows; | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-12 05:07:51 +01:00
										 |  |  | void TVScreen::getSize(int& intCols, int& intRows) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-15 00:16:50 +01:00
										 |  |  |     intCols = m_cols; | 
					
						
							|  |  |  |     intRows = m_rows; | 
					
						
							| 
									
										
										
										
											2018-03-12 05:07:51 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-11 16:39:02 +01:00
										 |  |  | void TVScreen::initializeGL() | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     m_objMutex.lock(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QOpenGLContext *objGlCurrentContext = QOpenGLContext::currentContext(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (objGlCurrentContext) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (QOpenGLContext::currentContext()->isValid()) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-03-16 10:11:32 +01:00
										 |  |  |             qDebug() << "TVScreen::initializeGL: context:" | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  |                     << " major: " << (QOpenGLContext::currentContext()->format()).majorVersion() | 
					
						
							|  |  |  |                     << " minor: " << (QOpenGLContext::currentContext()->format()).minorVersion() | 
					
						
							|  |  |  |                     << " ES: " << (QOpenGLContext::currentContext()->isOpenGLES() ? "yes" : "no"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-03-16 10:11:32 +01:00
										 |  |  |             qDebug() << "TVScreen::initializeGL: current context is invalid"; | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-03-16 10:11:32 +01:00
										 |  |  |         qCritical() << "TVScreen::initializeGL: no current context"; | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QSurface *objSurface = objGlCurrentContext->surface(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (objSurface == NULL) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-03-16 10:11:32 +01:00
										 |  |  |         qCritical() << "TVScreen::initializeGL: no surface attached"; | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (objSurface->surfaceType() != QSurface::OpenGLSurface) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-03-16 10:11:32 +01:00
										 |  |  |             qCritical() << "TVScreen::initializeGL: surface is not an OpenGLSurface: " | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  |                     << objSurface->surfaceType() | 
					
						
							|  |  |  |                     << " cannot use an OpenGL context"; | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-03-16 10:11:32 +01:00
										 |  |  |             qDebug() << "TVScreen::initializeGL: OpenGL surface:" | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  |                     << " class: " << (objSurface->surfaceClass() == QSurface::Window ? "Window" : "Offscreen"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     connect(objGlCurrentContext, &QOpenGLContext::aboutToBeDestroyed, this, | 
					
						
							| 
									
										
										
										
											2018-03-11 16:39:02 +01:00
										 |  |  |             &TVScreen::cleanup); // TODO: when migrating to QOpenGLWidget
 | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     m_blnGLContextInitialized = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_objMutex.unlock(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-11 16:39:02 +01:00
										 |  |  | void TVScreen::resizeGL(int intWidth, int intHeight) | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     QOpenGLFunctions *ptrF = QOpenGLContext::currentContext()->functions(); | 
					
						
							|  |  |  |     ptrF->glViewport(0, 0, intWidth, intHeight); | 
					
						
							|  |  |  |     m_blnConfigChanged = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-11 16:39:02 +01:00
										 |  |  | void TVScreen::paintGL() | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01: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-11-12 18:36:27 +01:00
										 |  |  | void TVScreen::mousePressEvent(QMouseEvent* event) | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-11-12 18:36:27 +01:00
										 |  |  |     (void) event; | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-11 16:39:02 +01:00
										 |  |  | void TVScreen::tick() | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_blnDataChanged) { | 
					
						
							|  |  |  |         update(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-11 16:39:02 +01:00
										 |  |  | void TVScreen::connectTimer(const QTimer& objTimer) | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-16 10:11:32 +01:00
										 |  |  |      qDebug() << "TVScreen::connectTimer"; | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  |      disconnect(&m_objTimer, SIGNAL(timeout()), this, SLOT(tick())); | 
					
						
							|  |  |  |      connect(&objTimer, SIGNAL(timeout()), this, SLOT(tick())); | 
					
						
							|  |  |  |      m_objTimer.stop(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-11 16:39:02 +01:00
										 |  |  | void TVScreen::cleanup() | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_blnGLContextInitialized) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_objGLShaderArray.Cleanup(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-11 16:39:02 +01:00
										 |  |  | bool TVScreen::selectRow(int intLine) | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_blnGLContextInitialized) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return m_objGLShaderArray.SelectRow(intLine); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-26 01:04:45 +01:00
										 |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-11 16:39:02 +01:00
										 |  |  | bool TVScreen::setDataColor(int intCol, int intRed, int intGreen, int intBlue) | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_blnGLContextInitialized) | 
					
						
							| 
									
										
										
										
											2018-02-26 01:04:45 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-03-12 05:07:51 +01:00
										 |  |  |         return m_objGLShaderArray.SetDataColor(intCol, qRgb(intBlue, intGreen, intRed)); // FIXME: blue <> red inversion in shader
 | 
					
						
							| 
									
										
										
										
											2018-02-26 01:04:45 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-22 22:52:49 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-12 20:39:16 +01: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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |