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