1 /*
2     SPDX-FileCopyrightText: 2003 Fabrice Bellard
3     SPDX-FileCopyrightText: 2020 Mladen Milinkovic <max@smoothware.net>
4 
5     SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #ifndef GLRENDERER_H
9 #define GLRENDERER_H
10 
11 #include <QOpenGLWidget>
12 #include <QOpenGLFunctions>
13 #include <QOpenGLVertexArrayObject>
14 #include <QMatrix4x4>
15 #include <QMutex>
16 
17 extern "C" {
18 #include <libavutil/frame.h>
19 }
20 
21 QT_FORWARD_DECLARE_CLASS(QOpenGLShader)
QT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram)22 QT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram)
23 
24 namespace SubtitleComposer {
25 class SubtitleTextOverlay;
26 
27 class GLRenderer : public QOpenGLWidget, private QOpenGLFunctions
28 {
29 	Q_OBJECT
30 
31 	friend class FFPlayer;
32 
33 	explicit GLRenderer(QWidget *parent = nullptr);
34 
35 public:
36 	~GLRenderer();
37 
38 	static void setupProfile();
39 	void reset();
40 
41 	void setFrameFormat(int width, int height, int compBits, int crWidthShift, int crHeightShift);
42 	void setColorspace(const AVFrame *frame);
43 	void setFrameY(quint8 *buf, quint32 pitch);
44 	void setFrameU(quint8 *buf, quint32 pitch);
45 	void setFrameV(quint8 *buf, quint32 pitch);
46 	void setOverlay(SubtitleTextOverlay *overlay);
47 
48 	inline QMutex * mutex() { return &m_texMutex; }
49 
50 signals:
51 	void resolutionChanged();
52 
53 protected:
54     void initializeGL() override;
55 	void initShader();
56     void resizeGL(int width, int height) override;
57     void paintGL() override;
58 
59 private:
60 	template<class T, int D> void uploadMM(int texWidth, int texHeight, T *texBuf, const T *texSrc);
61 	void uploadYUV();
62 	void uploadSubtitle();
63 
64 private:
65 	SubtitleTextOverlay *m_overlay;
66 	GLfloat m_overlayPos[8];
67 	quint8 *m_mmOvr;
68 
69 	QOpenGLVertexArrayObject m_vao;
70 
71 	quint8 *m_bufYUV, *m_mmYUV;
72 	quint32 m_bufSize;
73 	GLsizei m_bufWidth, m_bufHeight;
74 	GLsizei m_crWidth, m_crHeight;
75 	quint8 *m_pixels[3];
76 	quint32 m_pitch[3];
77 	QMutex m_texMutex;
78 
79 	bool m_csNeedInit;
80 	QString m_ctfOut, m_ctfIn;
81 	QMatrix4x4 m_csCM;
82 
83 	QOpenGLShader *m_vertShader;
84 	QOpenGLShader *m_fragShader;
85 	QOpenGLShaderProgram *m_shaderProg;
86 
87 	bool m_texNeedInit;
88 	int m_vpWidth, m_vpHeight;
89 	int m_texY, m_texU, m_texV, m_texOvr;
90 	GLuint *m_idTex;
91 	GLuint *m_vaBuf;
92 
93 	GLenum m_glType, m_glFormat;
94 };
95 }
96 
97 #endif // GLRENDERER_H
98