MythTV  master
mythrenderopengl.h
Go to the documentation of this file.
1 #ifndef MYTHRENDER_OPENGL_H_
2 #define MYTHRENDER_OPENGL_H_
3 
4 // Std
5 #include <cstdint>
6 
7 // Qt
8 #include <QObject>
9 #include <QtGlobal>
10 #include <QOpenGLContext>
11 #include <QOpenGLFunctions>
12 #include <QOpenGLShaderProgram>
13 #include <QOpenGLFramebufferObject>
14 #include <QOpenGLTexture>
15 #include <QOpenGLBuffer>
16 #include <QOpenGLDebugLogger>
17 #include <QHash>
18 #include <QMutex>
19 #include <QMatrix4x4>
20 #include <QStack>
21 
22 // MythTV
23 #include "mythuiexp.h"
24 #include "mythlogging.h"
25 #include "mythrender_base.h"
26 #include "mythrenderopengldefs.h"
27 #include "mythuianimation.h"
28 #include "mythegl.h"
29 
31 {
32  kGLFeatNone = 0x0000,
33  kGLBufferMap = 0x0001,
34  kGLExtRects = 0x0002,
35  kGLExtRGBA16 = 0x0004,
36  kGLExtSubimage = 0x0008,
37  kGLTiled = 0x0010,
39  kGLNVMemory = 0x0040
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 r, int g, int b, int a);
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, GLenum InternalFormat = 0);
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  void DeleteShaderProgram(QOpenGLShaderProgram* Program);
139  bool EnableShaderProgram(QOpenGLShaderProgram* Program);
140  void SetShaderProgramParams(QOpenGLShaderProgram* Program, const QMatrix4x4 &Value, const char* Uniform);
141  void SetShaderProjection(QOpenGLShaderProgram* Program);
142 
143  void DrawBitmap(MythGLTexture *Texture, QOpenGLFramebufferObject *Target,
144  const QRect &Source, const QRect &Destination,
145  QOpenGLShaderProgram *Program, int Alpha = 255);
146  void DrawBitmap(MythGLTexture **Textures, uint TextureCount,
147  QOpenGLFramebufferObject *Target,
148  const QRect &Source, const QRect &Destination,
149  QOpenGLShaderProgram *Program, int Rotation);
150  void DrawRect(QOpenGLFramebufferObject *Target,
151  const QRect &Area, const QBrush &FillBrush,
152  const QPen &LinePen, int Alpha);
153  void DrawRoundRect(QOpenGLFramebufferObject *Target,
154  const QRect &Area, int CornerRadius,
155  const QBrush &FillBrush, const QPen &LinePen, int Alpha);
156  void ClearRect(QOpenGLFramebufferObject *Target, const QRect &Area, int Color);
157 
158  bool GetGPUMemory(int &Available, int &Total);
159 
160  public slots:
161  void messageLogged (const QOpenGLDebugMessage &Message);
162  void logDebugMarker (const QString &Message);
163  void contextToBeDestroyed(void);
164 
165  protected:
166  MythRenderOpenGL(const QSurfaceFormat &Format, QWidget *Widget);
167  ~MythRenderOpenGL() override;
168  void SetWidget(QWidget *Widget);
169  void Init2DState(void);
170  void SetMatrixView(void);
171  void DeleteFramebuffers(void);
172  static bool UpdateTextureVertices(MythGLTexture *Texture, const QRect &Source,
173  const QRect &Destination, int Rotation);
174  GLfloat* GetCachedVertices(GLuint Type, const QRect &Area);
175  void ExpireVertices(int Max = 0);
176  void GetCachedVBO(GLuint Type, const QRect &Area);
177  void ExpireVBOS(int Max = 0);
178  bool CreateDefaultShaders(void);
179  void DeleteDefaultShaders(void);
180 
181  protected:
182  // Prevent compiler complaints about using 0 as a null pointer.
183  inline void glVertexAttribPointerI(GLuint Index, GLint Size, GLenum Type,
184  GLboolean Normalize, GLsizei Stride,
185  GLuint Value);
186 
187  bool m_ready { false };
188 
189  // Framebuffers
190  GLuint m_activeFramebuffer { 0 };
191 
192  // Synchronisation
193  GLuint m_fence { 0 };
194 
195  // Shaders
196  QOpenGLShaderProgram* m_defaultPrograms[kShaderCount] {};
197  QOpenGLShaderProgram* m_activeProgram { nullptr };
198 
199  // Vertices
200  QMap<uint64_t,GLfloat*> m_cachedVertices;
201  QList<uint64_t> m_vertexExpiry;
202  QMap<uint64_t,QOpenGLBuffer*>m_cachedVBOS;
203  QList<uint64_t> m_vboExpiry;
204 
205  // Locking
206  QMutex m_lock { QMutex::Recursive };
207  int m_lockLevel { 0 };
208 
209  // profile
210  QOpenGLFunctions::OpenGLFeatures m_features { Multitexture };
211  int m_extraFeatures { kGLFeatNone };
212  int m_extraFeaturesUsed { kGLFeatNone };
213  int m_maxTextureSize { 0 };
214  int m_maxTextureUnits { 0 };
215  int m_colorDepth { 0 };
216  bool m_coreProfile { false };
217 
218  // State
219  QRect m_viewport;
220  GLuint m_activeTexture { 0 };
221  bool m_blend { false };
222  int32_t m_background { 0x00000000 };
223  bool m_fullRange { true };
224  QMatrix4x4 m_projection;
225  QStack<QMatrix4x4> m_transforms;
226  QMatrix4x4 m_parameters;
227  QHash<QString,QMatrix4x4> m_cachedMatrixUniforms;
228  QHash<QOpenGLShaderProgram*, QHash<QByteArray, GLint> > m_cachedUniformLocations;
229  GLuint m_vao { 0 }; // core profile only
230 
231  // For Performance improvement set false to disable glFlush.
232  // Needed for Raspberry pi
233  bool m_flushEnabled { true };
234 
235  private:
236  Q_DISABLE_COPY(MythRenderOpenGL)
237  void DebugFeatures (void);
238  QOpenGLDebugLogger *m_openglDebugger { nullptr };
239  QOpenGLDebugMessage::Types m_openGLDebuggerFilter { QOpenGLDebugMessage::InvalidType };
240  QWindow *m_window { nullptr };
241 };
242 
244 {
245  public:
246  explicit OpenGLLocker(MythRenderOpenGL *Render);
247  ~OpenGLLocker();
248  private:
250 };
251 
252 #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:655
QHash< QOpenGLShaderProgram *, QHash< QByteArray, GLint > > m_cachedUniformLocations