CuteLogger
Fast and simple logging solution for Qt based applications
glwidget.h
1 /*
2  * Copyright (c) 2011-2019 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 
94 public slots:
95  void onFrameDisplayed(const SharedFrame& frame);
96  void setGrid(int grid);
97  void setZoom(float zoom);
98  void setOffsetX(int x);
99  void setOffsetY(int y);
100  void setBlankScene();
101  void setCurrentFilter(QmlFilter* filter, QmlMetadata* meta);
102  void setSnapToGrid(bool snap);
103 
104 signals:
105  void frameDisplayed(const SharedFrame& frame);
106  void dragStarted();
107  void seekTo(int x);
108  void gpuNotSupported();
109  void started();
110  void paused();
111  void playing();
112  void rectChanged();
113  void gridChanged();
114  void zoomChanged();
115  void offsetChanged();
116  void imageReady();
117  void snapToGridChanged();
118  void toggleZoom(bool);
119 
120 private:
121  QRectF m_rect;
122  int m_grid;
123  GLuint m_texture[3];
124  QOpenGLShaderProgram* m_shader;
125  QPoint m_dragStart;
126  Filter* m_glslManager;
127  QSemaphore m_initSem;
128  bool m_isInitialized;
129  Event* m_threadStartEvent;
130  Event* m_threadStopEvent;
131  Event* m_threadCreateEvent;
132  Event* m_threadJoinEvent;
133  FrameRenderer* m_frameRenderer;
134  int m_projectionLocation;
135  int m_modelViewLocation;
136  int m_vertexLocation;
137  int m_texCoordLocation;
138  int m_colorspaceLocation;
139  int m_textureLocation[3];
140  float m_zoom;
141  QPoint m_offset;
142  QOffscreenSurface m_offscreenSurface;
143  QOpenGLContext* m_shareContext;
144  SharedFrame m_sharedFrame;
145  QMutex m_mutex;
146  QUrl m_savedQmlSource;
147  bool m_snapToGrid;
148  QTimer m_refreshTimer;
149  bool m_scrubAudio;
150 
151  static void on_frame_show(mlt_consumer, void* self, mlt_frame frame);
152 
153 private slots:
154  void initializeGL();
155  void resizeGL(int width, int height);
156  void updateTexture(GLuint yName, GLuint uName, GLuint vName);
157  void paintGL();
158  void onRefreshTimeout();
159 
160 protected:
161  void resizeEvent(QResizeEvent* event);
162  void mousePressEvent(QMouseEvent *);
163  void mouseMoveEvent(QMouseEvent *);
164  void keyPressEvent(QKeyEvent* event);
165  bool event(QEvent* event);
166  void createShader();
167 };
168 
169 class RenderThread : public QThread
170 {
171  Q_OBJECT
172 public:
173  RenderThread(thread_function_t function, void* data, QOpenGLContext *context, QSurface* surface);
174 
175 protected:
176  void run();
177 
178 private:
179  thread_function_t m_function;
180  void* m_data;
181  QOpenGLContext* m_context;
182  QSurface* m_surface;
183 };
184 
185 class FrameRenderer : public QThread
186 {
187  Q_OBJECT
188 public:
189  FrameRenderer(QOpenGLContext* shareContext, QSurface* surface);
190  ~FrameRenderer();
191  QSemaphore* semaphore() { return &m_semaphore; }
192  QOpenGLContext* context() const { return m_context; }
193  SharedFrame getDisplayFrame();
194  Q_INVOKABLE void showFrame(Mlt::Frame frame);
195  void requestImage();
196  QImage image() const { return m_image; }
197 
198 public slots:
199  void cleanup();
200 
201 signals:
202  void textureReady(GLuint yName, GLuint uName = 0, GLuint vName = 0);
203  void frameDisplayed(const SharedFrame& frame);
204  void imageReady();
205 
206 private:
207  QSemaphore m_semaphore;
208  SharedFrame m_displayFrame;
209  QOpenGLContext* m_context;
210  QSurface* m_surface;
211  qint64 m_previousMSecs;
212  bool m_imageRequested;
213  QImage m_image;
214 
215 public:
216  GLuint m_renderTexture[3];
217  GLuint m_displayTexture[3];
218  QOpenGLFunctions_3_2_Core* m_gl32;
219 };
220 
221 } // namespace
222 
223 #endif
SharedFrame
The SharedFrame provides thread safe access to Mlt::Frame data.
Definition: sharedframe.h:48