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