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 void ReleaseResources(void) override;
105 QStringList GetDescription(void) override;
106
107 bool IsReady(void);
108 void makeCurrent();
109 void doneCurrent();
110 void swapBuffers();
111
112 bool Init(void);
113 int GetMaxTextureSize(void) const;
114 int GetMaxTextureUnits(void) const;
115 int GetExtraFeatures(void) const;
116 QOpenGLFunctions::OpenGLFeatures GetFeatures(void) const;
117 bool IsRecommendedRenderer(void);
118 void SetViewPort(QRect Rect, bool ViewportOnly = false) override;
119 QRect GetViewPort(void) { return m_viewport; }
120 void PushTransformation(const UIEffects &Fx, QPointF &Center);
121 void PopTransformation(void);
122 void Flush(void);
123 void SetBlend(bool Enable);
124 void SetBackground(uint8_t Red, uint8_t Green, uint8_t Blue, uint8_t Alpha);
125 QFunctionPointer GetProcAddress(const QString &Proc) const;
126 uint64_t GetSwapCount();
127
128 static constexpr GLuint kVertexSize { 16 * sizeof(GLfloat) };
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 void DrawProcedural(QRect Area, int Alpha, QOpenGLFramebufferObject* Target,
168 QOpenGLShaderProgram* Program, float TimeVal);
169 std::tuple<int,int,int> GetGPUMemory();
170
171 public slots:
172 void MessageLogged (const QOpenGLDebugMessage &Message);
173 void logDebugMarker (const QString &Message);
174 void contextToBeDestroyed(void);
175
176 protected:
177 MythRenderOpenGL(const QSurfaceFormat &Format, QWidget *Widget);
178 ~MythRenderOpenGL() override;
179 void SetWidget(QWidget *Widget);
180 void Init2DState(void);
181 void SetMatrixView(void);
183 static bool UpdateTextureVertices(MythGLTexture *Texture, QRect Source,
184 QRect Destination, int Rotation, qreal Scale = 1.0);
185 GLfloat* GetCachedVertices(GLuint Type, QRect Area);
186 void ExpireVertices(int Max = 0);
187 void GetCachedVBO(GLuint Type, QRect Area);
188 void ExpireVBOS(int Max = 0);
189 bool CreateDefaultShaders(void);
190 void DeleteDefaultShaders(void);
191 void Check16BitFBO(void);
192
193 protected:
194 // Prevent compiler complaints about using 0 as a null pointer.
195 inline void glVertexAttribPointerI(GLuint Index, GLint Size, GLenum Type,
196 GLboolean Normalize, GLsizei Stride,
197 GLuint Value);
198
199 bool m_ready { false };
200
201 // Framebuffers
202 GLuint m_activeFramebuffer { 0 };
203
204 // Synchronisation
205 GLuint m_fence { 0 };
206
207 // Shaders
208 std::array<QOpenGLShaderProgram*,kShaderCount> m_defaultPrograms { nullptr };
209 QOpenGLShaderProgram* m_activeProgram { nullptr };
210
211 // Vertices
212 QMap<uint64_t,GLfloat*> m_cachedVertices;
213 QList<uint64_t> m_vertexExpiry;
214 QMap<uint64_t,QOpenGLBuffer*>m_cachedVBOS;
215 QList<uint64_t> m_vboExpiry;
216
217 // Locking
218 QRecursiveMutex m_lock;
219 int m_lockLevel { 0 };
220
221 // profile
222 QOpenGLFunctions::OpenGLFeatures m_features { Multitexture };
223 int m_extraFeatures { kGLFeatNone };
224 int m_extraFeaturesUsed { kGLFeatNone };
225 int m_maxTextureSize { 0 };
226 int m_maxTextureUnits { 0 };
227
228 // State
229 uint64_t m_swapCount { 0 };
231 GLuint m_activeTexture { 0 };
232 bool m_blend { false };
233 int32_t m_background { 0x00000001 };
234 bool m_fullRange { true };
235 QMatrix4x4 m_projection;
236 QStack<QMatrix4x4> m_transforms;
237 QMatrix4x4 m_parameters;
238 QHash<QString,QMatrix4x4> m_cachedMatrixUniforms;
239 QHash<QOpenGLShaderProgram*, QHash<QByteArray, GLint> > m_cachedUniformLocations;
240 GLuint m_vao { 0 }; // core profile only
241
242 // For Performance improvement set false to disable glFlush.
243 // Needed for Raspberry pi
244 bool m_flushEnabled { true };
245
246 private:
247 Q_DISABLE_COPY(MythRenderOpenGL)
248 void DebugFeatures (void);
249 QOpenGLDebugLogger *m_openglDebugger { nullptr };
250 QOpenGLDebugMessage::Types m_openGLDebuggerFilter { QOpenGLDebugMessage::InvalidType };
251 QWindow *m_window { nullptr };
252};
253
255{
256 public:
257 explicit OpenGLLocker(MythRenderOpenGL *Render);
259 private:
261};
262
263#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:7