CuteLogger
Fast and simple logging solution for Qt based applications
glwidget.h
1 /*
2  * Copyright (c) 2011-2020 Meltytech, LLC
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef GLWIDGET_H
19 #define GLWIDGET_H
20 
21 #include <QSemaphore>
22 #include <QQuickWidget>
23 #include <QOpenGLFunctions>
24 #include <QOpenGLShaderProgram>
25 #include <QOpenGLFramebufferObject>
26 #include <QOpenGLContext>
27 #include <QOffscreenSurface>
28 #include <QMutex>
29 #include <QThread>
30 #include <QRectF>
31 #include <QTimer>
32 #include "mltcontroller.h"
33 #include "sharedframe.h"
34 
35 class QOpenGLFunctions_3_2_Core;
36 class QOpenGLTexture;
37 class QmlFilter;
38 class QmlMetadata;
39 
40 namespace Mlt {
41 
42 class Filter;
43 class RenderThread;
44 class FrameRenderer;
45 
46 typedef void* ( *thread_function_t )( void* );
47 
48 class GLWidget : public QQuickWidget, public Controller, protected QOpenGLFunctions
49 {
50  Q_OBJECT
51  Q_PROPERTY(QRectF rect READ rect NOTIFY rectChanged)
52  Q_PROPERTY(int grid READ grid NOTIFY gridChanged)
53  Q_PROPERTY(bool snapToGrid READ snapToGrid NOTIFY snapToGridChanged)
54  Q_PROPERTY(float zoom READ zoom NOTIFY zoomChanged)
55  Q_PROPERTY(QPoint offset READ offset NOTIFY offsetChanged)
56 
57 public:
58  GLWidget(QObject *parent = 0);
59  ~GLWidget();
60 
61  void createThread(RenderThread** thread, thread_function_t function, void* data);
62  void startGlsl();
63  void stopGlsl();
64  int setProducer(Mlt::Producer*, bool isMulti = false);
65  int reconfigure(bool isMulti);
66 
67  void play(double speed = 1.0) {
68  Controller::play(speed);
69  if (speed == 0) emit paused();
70  else emit playing();
71  }
72  void seek(int position) {
73  Controller::seek(position);
74  emit paused();
75  }
76  void refreshConsumer(bool scrubAudio = false);
77  void pause() {
78  Controller::pause();
79  emit paused();
80  }
81  int displayWidth() const { return m_rect.width(); }
82  int displayHeight() const { return m_rect.height(); }
83 
84  QObject* videoWidget() { return this; }
85  Filter* glslManager() const { return m_glslManager; }
86  QRectF rect() const { return m_rect; }
87  int grid() const { return m_grid; }
88  float zoom() const { return m_zoom * MLT.profile().width() / m_rect.width(); }
89  QPoint offset() const;
90  QImage image() const;
91  void requestImage() const;
92  bool snapToGrid() const { return m_snapToGrid; }
93  int maxTextureSize() const { return m_maxTextureSize; }
94 
95 public slots:
96  void onFrameDisplayed(const SharedFrame& frame);
97  void setGrid(int grid);
98  void setZoom(float zoom);
99  void setOffsetX(int x);
100  void setOffsetY(int y);
101  void setBlankScene();
102  void setCurrentFilter(QmlFilter* filter, QmlMetadata* meta);
103  void setSnapToGrid(bool snap);
104 
105 signals:
106  void frameDisplayed(const SharedFrame& frame);
107  void dragStarted();
108  void seekTo(int x);
109  void gpuNotSupported();
110  void started();
111  void paused();
112  void playing();
113  void rectChanged();
114  void gridChanged();
115  void zoomChanged();
116  void offsetChanged();
117  void imageReady();
118  void snapToGridChanged();
119  void toggleZoom(bool);
120 
121 private:
122  QRectF m_rect;
123  int m_grid;
124  GLuint m_texture[3];
125  QOpenGLShaderProgram* m_shader;
126  QPoint m_dragStart;
127  Filter* m_glslManager;
128  QSemaphore m_initSem;
129  bool m_isInitialized;
130  Event* m_threadStartEvent;
131  Event* m_threadStopEvent;
132  Event* m_threadCreateEvent;
133  Event* m_threadJoinEvent;
134  FrameRenderer* m_frameRenderer;
135  int m_projectionLocation;
136  int m_modelViewLocation;
137  int m_vertexLocation;
138  int m_texCoordLocation;
139  int m_colorspaceLocation;
140  int m_textureLocation[3];
141  float m_zoom;
142  QPoint m_offset;
143  QOffscreenSurface m_offscreenSurface;
144  QOpenGLContext* m_shareContext;
145  SharedFrame m_sharedFrame;
146  QMutex m_mutex;
147  QUrl m_savedQmlSource;
148  bool m_snapToGrid;
149  QTimer m_refreshTimer;
150  bool m_scrubAudio;
151  GLint m_maxTextureSize;
152 
153  static void on_frame_show(mlt_consumer, void* self, mlt_frame frame);
154 
155 private slots:
156  void initializeGL();
157  void resizeGL(int width, int height);
158  void updateTexture(GLuint yName, GLuint uName, GLuint vName);
159  void paintGL();
160  void onRefreshTimeout();
161 
162 protected:
163  void resizeEvent(QResizeEvent* event);
164  void mousePressEvent(QMouseEvent *);
165  void mouseMoveEvent(QMouseEvent *);
166  void keyPressEvent(QKeyEvent* event);
167  bool event(QEvent* event);
168  void createShader();
169 };
170 
171 class RenderThread : public QThread
172 {
173  Q_OBJECT
174 public:
175  RenderThread(thread_function_t function, void* data, QOpenGLContext *context, QSurface* surface);
176 
177 protected:
178  void run();
179 
180 private:
181  thread_function_t m_function;
182  void* m_data;
183  QOpenGLContext* m_context;
184  QSurface* m_surface;
185 };
186 
187 class FrameRenderer : public QThread
188 {
189  Q_OBJECT
190 public:
191  FrameRenderer(QOpenGLContext* shareContext, QSurface* surface);
192  ~FrameRenderer();
193  QSemaphore* semaphore() { return &m_semaphore; }
194  QOpenGLContext* context() const { return m_context; }
195  SharedFrame getDisplayFrame();
196  Q_INVOKABLE void showFrame(Mlt::Frame frame);
197  void requestImage();
198  QImage image() const { return m_image; }
199 
200 public slots:
201  void cleanup();
202 
203 signals:
204  void textureReady(GLuint yName, GLuint uName = 0, GLuint vName = 0);
205  void frameDisplayed(const SharedFrame& frame);
206  void imageReady();
207 
208 private:
209  QSemaphore m_semaphore;
210  SharedFrame m_displayFrame;
211  QOpenGLContext* m_context;
212  QSurface* m_surface;
213  qint64 m_previousMSecs;
214  bool m_imageRequested;
215  QImage m_image;
216 
217 public:
218  GLuint m_renderTexture[3];
219  GLuint m_displayTexture[3];
220  QOpenGLFunctions_3_2_Core* m_gl32;
221 };
222 
223 } // namespace
224 
225 #endif
The SharedFrame provides thread safe access to Mlt::Frame data.
Definition: sharedframe.h:49