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