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