MythTV  master
mythrenderopengl.h
Go to the documentation of this file.
1 #ifndef MYTHRENDER_OPENGL_H_
2 #define MYTHRENDER_OPENGL_H_
3 
4 // C++
5 #include <vector>
6 
7 // Qt
8 #include <QObject>
9 #include <QtGlobal>
10 #include <QOpenGLContext>
11 #include <QOpenGLFunctions>
12 #include <QOpenGLExtraFunctions>
13 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
14 #include <QOpenGLShaderProgram>
15 #include <QOpenGLFramebufferObject>
16 #include <QOpenGLTexture>
17 #include <QOpenGLBuffer>
18 #include <QOpenGLDebugLogger>
19 #else
20 #include <QtOpenGL/QOpenGLTexture>
21 #include <QtOpenGL/QOpenGLShaderProgram>
22 #include <QtOpenGL/QOpenGLFramebufferObject>
23 #include <QtOpenGL/QOpenGLBuffer>
24 #include <QtOpenGL/QOpenGLDebugLogger>
25 #endif
26 #include <QHash>
27 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
28 #include <QMutex>
29 #else
30 #include <QRecursiveMutex>
31 #endif
32 #include <QMatrix4x4>
33 #include <QStack>
34 
35 // MythTV
36 #include "mythuiexp.h"
37 #include "mythlogging.h"
38 #include "mythrender_base.h"
39 #include "mythrenderopengldefs.h"
40 #include "mythuianimation.h"
41 #include "mythegl.h"
42 
44 {
45  kGLFeatNone = 0x0000,
46  kGLBufferMap = 0x0001,
47  kGLExtRects = 0x0002,
48  kGLExtSubimage = 0x0004,
49  kGLTiled = 0x0008,
51  kGLNVMemory = 0x0020,
52  kGL16BitFBO = 0x0040,
55 };
56 
57 #define TEX_OFFSET 8
58 
60 {
61  public:
62  explicit MythGLTexture(QOpenGLTexture *Texture);
63  explicit MythGLTexture(GLuint Texture);
64  ~MythGLTexture() = default;
65 
66  unsigned char *m_data { nullptr };
67  int m_bufferSize { 0 } ;
68  GLuint m_textureId { 0 } ;
69  QOpenGLTexture *m_texture { nullptr } ;
70  QOpenGLTexture::PixelFormat m_pixelFormat { QOpenGLTexture::RGBA };
71  QOpenGLTexture::PixelType m_pixelType { QOpenGLTexture::UInt8 };
72  QOpenGLBuffer *m_vbo { nullptr };
73  QSize m_size { QSize() };
74  QSize m_totalSize { QSize() };
75  bool m_flip { true };
76  bool m_crop { false };
77  QRect m_source { QRect() };
78  QRect m_destination { QRect() };
79  std::array<GLfloat,16> m_vertexData { 0.0F };
80  GLenum m_target { QOpenGLTexture::Target2D };
81  int m_rotation { 0 };
82 
83  private:
84  Q_DISABLE_COPY(MythGLTexture)
85 };
86 
88 {
94 };
95 
96 class QWindow;
97 class QPaintDevice;
98 
99 class MUI_PUBLIC MythRenderOpenGL : public QOpenGLContext, public QOpenGLFunctions, public MythEGL, public MythRender
100 {
101  Q_OBJECT
102 
103  public:
104  static MythRenderOpenGL* GetOpenGLRender(void);
105  static MythRenderOpenGL* Create(QWidget *Widget);
106 
107  // MythRender
108  void ReleaseResources(void) override;
109  QStringList GetDescription(void) override;
110 
111  bool IsReady(void);
112  void makeCurrent();
113  void doneCurrent();
114  void swapBuffers();
115 
116  bool Init(void);
117  int GetMaxTextureSize(void) const;
118  int GetMaxTextureUnits(void) const;
119  int GetExtraFeatures(void) const;
120  QOpenGLFunctions::OpenGLFeatures GetFeatures(void) const;
121  bool IsRecommendedRenderer(void);
122  void SetViewPort(QRect Rect, bool ViewportOnly = false) override;
123  QRect GetViewPort(void) { return m_viewport; }
124  void PushTransformation(const UIEffects &Fx, QPointF &Center);
125  void PopTransformation(void);
126  void Flush(void);
127  void SetBlend(bool Enable);
128  void SetBackground(uint8_t Red, uint8_t Green, uint8_t Blue, uint8_t Alpha);
129  QFunctionPointer GetProcAddress(const QString &Proc) const;
130  uint64_t GetSwapCount();
131 
132  static const GLuint kVertexSize;
133  QOpenGLBuffer* CreateVBO(int Size, bool Release = true);
134 
135  MythGLTexture* CreateTextureFromQImage(QImage *Image);
136  QSize GetTextureSize(QSize Size, bool Normalised);
137  static int GetTextureDataSize(MythGLTexture *Texture);
138  void SetTextureFilters(MythGLTexture *Texture, QOpenGLTexture::Filter Filter,
139  QOpenGLTexture::WrapMode Wrap = QOpenGLTexture::ClampToEdge);
140  void ActiveTexture(GLuint ActiveTex);
141  void DeleteTexture(MythGLTexture *Texture);
142  static int GetBufferSize(QSize Size, QOpenGLTexture::PixelFormat Format, QOpenGLTexture::PixelType Type);
143 
144  QOpenGLFramebufferObject* CreateFramebuffer(QSize &Size, bool SixteenBit = false);
145  MythGLTexture* CreateFramebufferTexture(QOpenGLFramebufferObject *Framebuffer);
146  void DeleteFramebuffer(QOpenGLFramebufferObject *Framebuffer);
147  void BindFramebuffer(QOpenGLFramebufferObject *Framebuffer);
148  void ClearFramebuffer(void);
149 
150  QOpenGLShaderProgram* CreateShaderProgram(const QString &Vertex, const QString &Fragment);
151  QOpenGLShaderProgram* CreateComputeShader(const QString &Source);
152  void DeleteShaderProgram(QOpenGLShaderProgram* Program);
153  bool EnableShaderProgram(QOpenGLShaderProgram* Program);
154  void SetShaderProgramParams(QOpenGLShaderProgram* Program, const QMatrix4x4 &Value, const char* Uniform);
155  void SetShaderProjection(QOpenGLShaderProgram* Program);
156 
157  void DrawBitmap(MythGLTexture *Texture, QOpenGLFramebufferObject *Target,
158  QRect Source, QRect Destination,
159  QOpenGLShaderProgram *Program, int Alpha = 255, qreal Scale = 1.0);
160  void DrawBitmap(std::vector<MythGLTexture *> &Textures,
161  QOpenGLFramebufferObject *Target,
162  QRect Source, QRect Destination,
163  QOpenGLShaderProgram *Program, int Rotation);
164  void DrawRect(QOpenGLFramebufferObject *Target,
165  QRect Area, const QBrush &FillBrush,
166  const QPen &LinePen, int Alpha);
167  void DrawRoundRect(QOpenGLFramebufferObject *Target,
168  QRect Area, int CornerRadius,
169  const QBrush &FillBrush, const QPen &LinePen, int Alpha);
170  void ClearRect(QOpenGLFramebufferObject *Target, QRect Area, int Color, int Alpha);
171 
172  std::tuple<int,int,int> GetGPUMemory();
173 
174  public slots:
175  void MessageLogged (const QOpenGLDebugMessage &Message);
176  void logDebugMarker (const QString &Message);
177  void contextToBeDestroyed(void);
178 
179  protected:
180  MythRenderOpenGL(const QSurfaceFormat &Format, QWidget *Widget);
181  ~MythRenderOpenGL() override;
182  void SetWidget(QWidget *Widget);
183  void Init2DState(void);
184  void SetMatrixView(void);
185  void DeleteFramebuffers(void);
186  static bool UpdateTextureVertices(MythGLTexture *Texture, QRect Source,
187  QRect Destination, int Rotation, qreal Scale = 1.0);
188  GLfloat* GetCachedVertices(GLuint Type, QRect Area);
189  void ExpireVertices(int Max = 0);
190  void GetCachedVBO(GLuint Type, QRect Area);
191  void ExpireVBOS(int Max = 0);
192  bool CreateDefaultShaders(void);
193  void DeleteDefaultShaders(void);
194  void Check16BitFBO(void);
195 
196  protected:
197  // Prevent compiler complaints about using 0 as a null pointer.
198  inline void glVertexAttribPointerI(GLuint Index, GLint Size, GLenum Type,
199  GLboolean Normalize, GLsizei Stride,
200  GLuint Value);
201 
202  bool m_ready { false };
203 
204  // Framebuffers
205  GLuint m_activeFramebuffer { 0 };
206 
207  // Synchronisation
208  GLuint m_fence { 0 };
209 
210  // Shaders
211  std::array<QOpenGLShaderProgram*,kShaderCount> m_defaultPrograms { nullptr };
212  QOpenGLShaderProgram* m_activeProgram { nullptr };
213 
214  // Vertices
215  QMap<uint64_t,GLfloat*> m_cachedVertices;
216  QList<uint64_t> m_vertexExpiry;
217  QMap<uint64_t,QOpenGLBuffer*>m_cachedVBOS;
218  QList<uint64_t> m_vboExpiry;
219 
220  // Locking
221 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
222  QMutex m_lock { QMutex::Recursive };
223 #else
224  QRecursiveMutex m_lock;
225 #endif
226  int m_lockLevel { 0 };
227 
228  // profile
229  QOpenGLFunctions::OpenGLFeatures m_features { Multitexture };
230  int m_extraFeatures { kGLFeatNone };
231  int m_extraFeaturesUsed { kGLFeatNone };
232  int m_maxTextureSize { 0 };
233  int m_maxTextureUnits { 0 };
234 
235  // State
236  uint64_t m_swapCount { 0 };
237  QRect m_viewport;
238  GLuint m_activeTexture { 0 };
239  bool m_blend { false };
240  int32_t m_background { 0x00000001 };
241  bool m_fullRange { true };
242  QMatrix4x4 m_projection;
243  QStack<QMatrix4x4> m_transforms;
244  QMatrix4x4 m_parameters;
245  QHash<QString,QMatrix4x4> m_cachedMatrixUniforms;
246  QHash<QOpenGLShaderProgram*, QHash<QByteArray, GLint> > m_cachedUniformLocations;
247  GLuint m_vao { 0 }; // core profile only
248 
249  // For Performance improvement set false to disable glFlush.
250  // Needed for Raspberry pi
251  bool m_flushEnabled { true };
252 
253  private:
254  Q_DISABLE_COPY(MythRenderOpenGL)
255  void DebugFeatures (void);
256  QOpenGLDebugLogger *m_openglDebugger { nullptr };
257  QOpenGLDebugMessage::Types m_openGLDebuggerFilter { QOpenGLDebugMessage::InvalidType };
258  QWindow *m_window { nullptr };
259 };
260 
262 {
263  public:
264  explicit OpenGLLocker(MythRenderOpenGL *Render);
265  ~OpenGLLocker();
266  private:
268 };
269 
270 #endif
MythRender::GetDescription
virtual QStringList GetDescription()
Definition: mythrender.cpp:17
MythGLTexture
Definition: mythrenderopengl.h:59
kGLNVMemory
@ kGLNVMemory
Definition: mythrenderopengl.h:51
MUI_PUBLIC
#define MUI_PUBLIC
Definition: mythuiexp.h:9
MythRenderOpenGL::m_cachedVBOS
QMap< uint64_t, QOpenGLBuffer * > m_cachedVBOS
Definition: mythrenderopengl.h:217
MythRenderOpenGL::m_parameters
QMatrix4x4 m_parameters
Definition: mythrenderopengl.h:244
MythDate::Format
Format
Definition: mythdate.h:15
kGLLegacyTextures
@ kGLLegacyTextures
Definition: mythrenderopengl.h:50
MythRenderOpenGL::m_vertexExpiry
QList< uint64_t > m_vertexExpiry
Definition: mythrenderopengl.h:216
kGLExtRects
@ kGLExtRects
Definition: mythrenderopengl.h:47
kGLExtSubimage
@ kGLExtSubimage
Definition: mythrenderopengl.h:48
MythRenderOpenGL::m_lock
QRecursiveMutex m_lock
Definition: mythrenderopengl.h:224
OpenGLLocker::m_render
MythRenderOpenGL * m_render
Definition: mythrenderopengl.h:267
DefaultShaders
DefaultShaders
Definition: mythrenderopengl.h:87
MythRenderOpenGL::m_transforms
QStack< QMatrix4x4 > m_transforms
Definition: mythrenderopengl.h:243
MythRender::SetViewPort
virtual void SetViewPort(const QRect, bool=false)
Definition: mythrender_base.h:36
MythRenderOpenGL::m_cachedUniformLocations
QHash< QOpenGLShaderProgram *, QHash< QByteArray, GLint > > m_cachedUniformLocations
Definition: mythrenderopengl.h:246
mythrender_base.h
mythrenderopengldefs.h
mythlogging.h
kGL16BitFBO
@ kGL16BitFBO
Definition: mythrenderopengl.h:52
Source
Definition: channelsettings.cpp:68
MythRenderOpenGL::kVertexSize
static const GLuint kVertexSize
Definition: mythrenderopengl.h:132
MythRenderOpenGL::m_vboExpiry
QList< uint64_t > m_vboExpiry
Definition: mythrenderopengl.h:218
MythRenderOpenGL::GetViewPort
QRect GetViewPort(void)
Definition: mythrenderopengl.h:123
kGLGeometryShaders
@ kGLGeometryShaders
Definition: mythrenderopengl.h:54
kShaderEdge
@ kShaderEdge
Definition: mythrenderopengl.h:92
kGLBufferMap
@ kGLBufferMap
Definition: mythrenderopengl.h:46
MythRender::ReleaseResources
virtual void ReleaseResources(void)
Definition: mythrender_base.h:40
kGLFeatNone
@ kGLFeatNone
Definition: mythrenderopengl.h:45
kShaderSimple
@ kShaderSimple
Definition: mythrenderopengl.h:89
MythRenderOpenGL
Definition: mythrenderopengl.h:99
MythEGL
Definition: mythegl.h:17
Max
const T & Max(const T &x, const T &y)
Definition: upnputil.h:35
MythRenderOpenGL::m_projection
QMatrix4x4 m_projection
Definition: mythrenderopengl.h:242
kShaderRect
@ kShaderRect
Definition: mythrenderopengl.h:91
MythRenderOpenGL::m_cachedMatrixUniforms
QHash< QString, QMatrix4x4 > m_cachedMatrixUniforms
Definition: mythrenderopengl.h:245
MythRender
Definition: mythrender_base.h:23
UIEffects
Definition: mythuianimation.h:12
kGLComputeShaders
@ kGLComputeShaders
Definition: mythrenderopengl.h:53
mythegl.h
GLFeatures
GLFeatures
Definition: mythrenderopengl.h:43
mythuianimation.h
MythRenderOpenGL::m_viewport
QRect m_viewport
Definition: mythrenderopengl.h:237
kShaderCount
@ kShaderCount
Definition: mythrenderopengl.h:93
kGLTiled
@ kGLTiled
Definition: mythrenderopengl.h:49
MythRenderOpenGL::m_cachedVertices
QMap< uint64_t, GLfloat * > m_cachedVertices
Definition: mythrenderopengl.h:215
mythuiexp.h
Color
Definition: graphic.h:6
kShaderDefault
@ kShaderDefault
Definition: mythrenderopengl.h:90
OpenGLLocker
Definition: mythrenderopengl.h:261