SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
tvscreen.cpp
Go to the documentation of this file.
1 // Copyright (C) 2018 F4HKW //
3 // for F4EXB / SDRAngel //
4 // //
5 // OpenGL interface modernization. //
6 // //
7 // This program is free software; you can redistribute it and/or modify //
8 // it under the terms of the GNU General Public License as published by //
9 // the Free Software Foundation as version 3 of the License, or //
10 // (at your option) any later version. //
11 // //
12 // This program is distributed in the hope that it will be useful, //
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
15 // GNU General Public License V3 for more details. //
16 // //
17 // You should have received a copy of the GNU General Public License //
18 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
20 
21 #include <QPainter>
22 #include <QMouseEvent>
23 #include <QOpenGLContext>
24 #include <QOpenGLFunctions>
25 #include <QSurface>
26 #include "tvscreen.h"
27 
28 #include <algorithm>
29 #include <QDebug>
30 
31 TVScreen::TVScreen(bool blnColor, QWidget* parent) :
32  QGLWidget(parent), m_objMutex(QMutex::NonRecursive), m_objGLShaderArray(blnColor)
33 {
34  setAttribute(Qt::WA_OpaquePaintEvent);
35  connect(&m_objTimer, SIGNAL(timeout()), this, SLOT(tick()));
36  m_objTimer.start(40); // capped at 25 FPS
37 
38  m_chrLastData = NULL;
39  m_blnConfigChanged = false;
40  m_blnDataChanged = false;
42 
43  //Par défaut
46  m_cols = TV_COLS;
47  m_rows = TV_ROWS;
48 }
49 
51 {
52  cleanup();
53 }
54 
55 void TVScreen::setColor(bool blnColor)
56 {
57  m_objGLShaderArray.setColor(blnColor);
58 }
59 
60 QRgb* TVScreen::getRowBuffer(int intRow)
61 {
63  {
64  return NULL;
65  }
66 
67  return m_objGLShaderArray.GetRowBuffer(intRow);
68 }
69 
70 void TVScreen::renderImage(unsigned char * objData)
71 {
72  m_chrLastData = objData;
73  m_blnDataChanged = true;
74 }
75 
77 {
79 }
80 
81 void TVScreen::resetImage(int alpha)
82 {
84 }
85 
86 void TVScreen::resizeTVScreen(int intCols, int intRows)
87 {
88  qDebug("TVScreen::resizeTVScreen: cols: %d, rows: %d", intCols, intRows);
89  m_intAskedCols = intCols;
90  m_intAskedRows = intRows;
91  m_cols = intCols;
92  m_rows = intRows;
93 }
94 
95 void TVScreen::getSize(int& intCols, int& intRows) const
96 {
97  intCols = m_cols;
98  intRows = m_rows;
99 }
100 
102 {
103  m_objMutex.lock();
104 
105  QOpenGLContext *objGlCurrentContext = QOpenGLContext::currentContext();
106 
107  if (objGlCurrentContext)
108  {
109  if (QOpenGLContext::currentContext()->isValid())
110  {
111  qDebug() << "TVScreen::initializeGL: context:"
112  << " major: " << (QOpenGLContext::currentContext()->format()).majorVersion()
113  << " minor: " << (QOpenGLContext::currentContext()->format()).minorVersion()
114  << " ES: " << (QOpenGLContext::currentContext()->isOpenGLES() ? "yes" : "no");
115  }
116  else
117  {
118  qDebug() << "TVScreen::initializeGL: current context is invalid";
119  }
120  }
121  else
122  {
123  qCritical() << "TVScreen::initializeGL: no current context";
124  return;
125  }
126 
127  QSurface *objSurface = objGlCurrentContext->surface();
128 
129  if (objSurface == NULL)
130  {
131  qCritical() << "TVScreen::initializeGL: no surface attached";
132  return;
133  }
134  else
135  {
136  if (objSurface->surfaceType() != QSurface::OpenGLSurface)
137  {
138  qCritical() << "TVScreen::initializeGL: surface is not an OpenGLSurface: "
139  << objSurface->surfaceType()
140  << " cannot use an OpenGL context";
141  return;
142  }
143  else
144  {
145  qDebug() << "TVScreen::initializeGL: OpenGL surface:"
146  << " class: " << (objSurface->surfaceClass() == QSurface::Window ? "Window" : "Offscreen");
147  }
148  }
149 
150  connect(objGlCurrentContext, &QOpenGLContext::aboutToBeDestroyed, this,
151  &TVScreen::cleanup); // TODO: when migrating to QOpenGLWidget
152 
154 
155  m_objMutex.unlock();
156 }
157 
158 void TVScreen::resizeGL(int intWidth, int intHeight)
159 {
160  QOpenGLFunctions *ptrF = QOpenGLContext::currentContext()->functions();
161  ptrF->glViewport(0, 0, intWidth, intHeight);
162  m_blnConfigChanged = true;
163 }
164 
166 {
167  if (!m_objMutex.tryLock(2))
168  return;
169 
170  m_blnDataChanged = false;
171 
172  if ((m_intAskedCols != 0) && (m_intAskedRows != 0))
173  {
175  m_intAskedCols = 0;
176  m_intAskedRows = 0;
177  }
178 
180 
181  m_objMutex.unlock();
182 }
183 
184 void TVScreen::mousePressEvent(QMouseEvent* event)
185 {
186  (void) event;
187 }
188 
190 {
191  if (m_blnDataChanged) {
192  update();
193  }
194 }
195 
196 void TVScreen::connectTimer(const QTimer& objTimer)
197 {
198  qDebug() << "TVScreen::connectTimer";
199  disconnect(&m_objTimer, SIGNAL(timeout()), this, SLOT(tick()));
200  connect(&objTimer, SIGNAL(timeout()), this, SLOT(tick()));
201  m_objTimer.stop();
202 }
203 
205 {
207  {
209  }
210 }
211 
212 bool TVScreen::selectRow(int intLine)
213 {
215  {
216  return m_objGLShaderArray.SelectRow(intLine);
217  }
218  else
219  {
220  return false;
221  }
222 }
223 
224 bool TVScreen::setDataColor(int intCol, int intRed, int intGreen, int intBlue)
225 {
227  {
228  return m_objGLShaderArray.SetDataColor(intCol, qRgb(intBlue, intGreen, intRed)); // FIXME: blue <> red inversion in shader
229  }
230  else
231  {
232  return false;
233  }
234 }
235 
236 bool TVScreen::setDataColor(int intCol, int intRed, int intGreen, int intBlue, int intAlpha)
237 {
239  {
240  return m_objGLShaderArray.SetDataColor(intCol, qRgba(intBlue, intGreen, intRed, intAlpha)); // FIXME: blue <> red inversion in shader
241  }
242  else
243  {
244  return false;
245  }
246 }
bool setDataColor(int intCol, int intRed, int intGreen, int intBlue)
Definition: tvscreen.cpp:224
void resetImage()
Definition: tvscreen.cpp:76
int m_rows
Definition: tvscreen.h:87
void initializeGL()
Definition: tvscreen.cpp:101
QRgb * getRowBuffer(int intRow)
Definition: tvscreen.cpp:60
void connectTimer(const QTimer &timer)
Definition: tvscreen.cpp:196
void renderImage(unsigned char *objData)
Definition: tvscreen.cpp:70
unsigned char * m_chrLastData
Definition: tvscreen.h:95
QRgb * GetRowBuffer(int intRow)
bool SelectRow(int intLine)
bool m_blnGLContextInitialized
Definition: tvscreen.h:73
bool m_blnDataChanged
Definition: tvscreen.h:81
void getSize(int &intCols, int &intRows) const
Definition: tvscreen.cpp:95
void RenderPixels(unsigned char *chrData)
bool SetDataColor(int intCol, QRgb objColor)
void resizeTVScreen(int intCols, int intRows)
Definition: tvscreen.cpp:86
static const int TV_COLS
Definition: tvscreen.h:65
void InitializeGL(int intCols, int intRows)
void setColor(bool blnColor)
Definition: tvscreen.cpp:55
QMutex m_objMutex
Definition: tvscreen.h:80
int m_intAskedRows
Definition: tvscreen.h:75
GLShaderTVArray m_objGLShaderArray
Definition: tvscreen.h:84
void tick()
Definition: tvscreen.cpp:189
int m_cols
Definition: tvscreen.h:86
static const int TV_ROWS
Definition: tvscreen.h:66
TVScreen(bool blnColor, QWidget *parent=0)
Definition: tvscreen.cpp:31
bool m_blnConfigChanged
Definition: tvscreen.h:82
void cleanup()
Definition: tvscreen.cpp:204
void setColor(bool blnColor)
void paintGL()
Definition: tvscreen.cpp:165
int m_intAskedCols
Definition: tvscreen.h:74
void resizeGL(int width, int height)
Definition: tvscreen.cpp:158
virtual ~TVScreen()
Definition: tvscreen.cpp:50
QTimer m_objTimer
Definition: tvscreen.h:79
bool selectRow(int intLine)
Definition: tvscreen.cpp:212
void mousePressEvent(QMouseEvent *)
Definition: tvscreen.cpp:184