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 #include <QMutex>
28 #include <QMatrix4x4>
29 #include <QStack>
30 
31 // MythTV
32 #include "mythuiexp.h"
33 #include "mythlogging.h"
34 #include "mythrender_base.h"
35 #include "mythrenderopengldefs.h"
36 #include "mythuianimation.h"
37 #include "mythegl.h"
38 
40 {
41  kGLFeatNone = 0x0000,
42  kGLBufferMap = 0x0001,
43  kGLExtRects = 0x0002,
44  kGLExtRGBA16 = 0x0004, // TODO remove
45  kGLExtSubimage = 0x0008,
46  kGLTiled = 0x0010,
48  kGLNVMemory = 0x0040,
49  kGL16BitFBO = 0x0080,
52 };
53 
54 #define 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 
128  static const GLuint kVertexSize;
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 
168  std::tuple<int,int,int> GetGPUMemory();
169 
170  public slots:
171  void MessageLogged (const QOpenGLDebugMessage &Message);
172  void logDebugMarker (const QString &Message);
173  void contextToBeDestroyed(void);
174 
175  protected:
176  MythRenderOpenGL(const QSurfaceFormat &Format, QWidget *Widget);
177  ~MythRenderOpenGL() override;
178  void SetWidget(QWidget *Widget);
179  void Init2DState(void);
180  void SetMatrixView(void);
181  void DeleteFramebuffers(void);
182  static bool UpdateTextureVertices(MythGLTexture *Texture, QRect Source,
183  QRect Destination, int Rotation, qreal Scale = 1.0);
184  GLfloat* GetCachedVertices(GLuint Type, QRect Area);
185  void ExpireVertices(int Max = 0);
186  void GetCachedVBO(GLuint Type, QRect Area);
187  void ExpireVBOS(int Max = 0);
188  bool CreateDefaultShaders(void);
189  void DeleteDefaultShaders(void);
190  void Check16BitFBO(void);
191 
192  protected:
193  // Prevent compiler complaints about using 0 as a null pointer.
194  inline void glVertexAttribPointerI(GLuint Index, GLint Size, GLenum Type,
195  GLboolean Normalize, GLsizei Stride,
196  GLuint Value);
197 
198  bool m_ready { false };
199 
200  // Framebuffers
201  GLuint m_activeFramebuffer { 0 };
202 
203  // Synchronisation
204  GLuint m_fence { 0 };
205 
206  // Shaders
207  std::array<QOpenGLShaderProgram*,kShaderCount> m_defaultPrograms { nullptr };
208  QOpenGLShaderProgram* m_activeProgram { nullptr };
209 
210  // Vertices
211  QMap<uint64_t,GLfloat*> m_cachedVertices;
212  QList<uint64_t> m_vertexExpiry;
213  QMap<uint64_t,QOpenGLBuffer*>m_cachedVBOS;
214  QList<uint64_t> m_vboExpiry;
215 
216  // Locking
217  QMutex m_lock { QMutex::Recursive };
218  int m_lockLevel { 0 };
219 
220  // profile
221  QOpenGLFunctions::OpenGLFeatures m_features { Multitexture };
222  int m_extraFeatures { kGLFeatNone };
223  int m_extraFeaturesUsed { kGLFeatNone };
224  int m_maxTextureSize { 0 };
225  int m_maxTextureUnits { 0 };
226 
227  // State
228  QRect m_viewport;
229  GLuint m_activeTexture { 0 };
230  bool m_blend { false };
231  int32_t m_background { 0x00000001 };
232  bool m_fullRange { true };
233  QMatrix4x4 m_projection;
234  QStack<QMatrix4x4> m_transforms;
235  QMatrix4x4 m_parameters;
236  QHash<QString,QMatrix4x4> m_cachedMatrixUniforms;
237  QHash<QOpenGLShaderProgram*, QHash<QByteArray, GLint> > m_cachedUniformLocations;
238  GLuint m_vao { 0 }; // core profile only
239 
240  // For Performance improvement set false to disable glFlush.
241  // Needed for Raspberry pi
242  bool m_flushEnabled { true };
243 
244  private:
245  Q_DISABLE_COPY(MythRenderOpenGL)
246  void DebugFeatures (void);
247  QOpenGLDebugLogger *m_openglDebugger { nullptr };
248  QOpenGLDebugMessage::Types m_openGLDebuggerFilter { QOpenGLDebugMessage::InvalidType };
249  QWindow *m_window { nullptr };
250 };
251 
253 {
254  public:
255  explicit OpenGLLocker(MythRenderOpenGL *Render);
256  ~OpenGLLocker();
257  private:
259 };
260 
261 #endif
MythRender::GetDescription
virtual QStringList GetDescription()
Definition: mythrender.cpp:17
MythGLTexture
Definition: mythrenderopengl.h:56
kGLNVMemory
@ kGLNVMemory
Definition: mythrenderopengl.h:48
kGLExtRGBA16
@ kGLExtRGBA16
Definition: mythrenderopengl.h:44
MUI_PUBLIC
#define MUI_PUBLIC
Definition: mythuiexp.h:9
MythRenderOpenGL::m_cachedVBOS
QMap< uint64_t, QOpenGLBuffer * > m_cachedVBOS
Definition: mythrenderopengl.h:213
MythRenderOpenGL::m_parameters
QMatrix4x4 m_parameters
Definition: mythrenderopengl.h:235
MythDate::Format
Format
Definition: mythdate.h:12
kGLLegacyTextures
@ kGLLegacyTextures
Definition: mythrenderopengl.h:47
MythRenderOpenGL::m_vertexExpiry
QList< uint64_t > m_vertexExpiry
Definition: mythrenderopengl.h:212
Image
Definition: image.h:30
kGLExtRects
@ kGLExtRects
Definition: mythrenderopengl.h:43
kGLExtSubimage
@ kGLExtSubimage
Definition: mythrenderopengl.h:45
OpenGLLocker::m_render
MythRenderOpenGL * m_render
Definition: mythrenderopengl.h:258
DefaultShaders
DefaultShaders
Definition: mythrenderopengl.h:84
MythRenderOpenGL::m_transforms
QStack< QMatrix4x4 > m_transforms
Definition: mythrenderopengl.h:234
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:237
mythrender_base.h
mythrenderopengldefs.h
mythlogging.h
kGL16BitFBO
@ kGL16BitFBO
Definition: mythrenderopengl.h:49
Source
Definition: channelsettings.cpp:68
MythRenderOpenGL::kVertexSize
static const GLuint kVertexSize
Definition: mythrenderopengl.h:128
MythRenderOpenGL::m_vboExpiry
QList< uint64_t > m_vboExpiry
Definition: mythrenderopengl.h:214
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:42
MythRender::ReleaseResources
virtual void ReleaseResources(void)
Definition: mythrender_base.h:40
kGLFeatNone
@ kGLFeatNone
Definition: mythrenderopengl.h:41
kShaderSimple
@ kShaderSimple
Definition: mythrenderopengl.h:86
MythRenderOpenGL
Definition: mythrenderopengl.h:96
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:233
kShaderRect
@ kShaderRect
Definition: mythrenderopengl.h:88
MythRenderOpenGL::m_cachedMatrixUniforms
QHash< QString, QMatrix4x4 > m_cachedMatrixUniforms
Definition: mythrenderopengl.h:236
MythRender
Definition: mythrender_base.h:23
UIEffects
Definition: mythuianimation.h:10
kGLComputeShaders
@ kGLComputeShaders
Definition: mythrenderopengl.h:50
mythegl.h
GLFeatures
GLFeatures
Definition: mythrenderopengl.h:39
mythuianimation.h
MythRenderOpenGL::m_viewport
QRect m_viewport
Definition: mythrenderopengl.h:228
kShaderCount
@ kShaderCount
Definition: mythrenderopengl.h:90
kGLTiled
@ kGLTiled
Definition: mythrenderopengl.h:46
MythRenderOpenGL::m_cachedVertices
QMap< uint64_t, GLfloat * > m_cachedVertices
Definition: mythrenderopengl.h:211
mythuiexp.h
Color
Definition: graphic.h:6
kShaderDefault
@ kShaderDefault
Definition: mythrenderopengl.h:87
OpenGLLocker
Definition: mythrenderopengl.h:252