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