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 <QRect>
31 #include "mltcontroller.h"
32 #include "sharedframe.h"
33 
34 class QOpenGLFunctions_3_2_Core;
35 class QOpenGLTexture;
36 class QmlFilter;
37 class QmlMetadata;
38 
39 namespace Mlt {
40 
41 class Filter;
42 class RenderThread;
43 class FrameRenderer;
44 
45 typedef void* ( *thread_function_t )( void* );
46 
47 class GLWidget : public QQuickWidget, public Controller, protected QOpenGLFunctions
48 {
49  Q_OBJECT
50  Q_PROPERTY(QRect rect READ rect NOTIFY rectChanged)
51  Q_PROPERTY(int grid READ grid NOTIFY gridChanged)
52  Q_PROPERTY(bool snapToGrid READ snapToGrid NOTIFY snapToGridChanged)
53  Q_PROPERTY(float zoom READ zoom NOTIFY zoomChanged)
54  Q_PROPERTY(QPoint offset READ offset NOTIFY offsetChanged)
55 
56 public:
57  GLWidget(QObject *parent = 0);
58  ~GLWidget();
59 
60  void createThread(RenderThread** thread, thread_function_t function, void* data);
61  void startGlsl();
62  void stopGlsl();
63  int setProducer(Mlt::Producer*, bool isMulti = false);
64  int reconfigure(bool isMulti);
65 
66  void play(double speed = 1.0) {
67  Controller::play(speed);
68  if (speed == 0) emit paused();
69  else emit playing();
70  }
71  void seek(int position) {
72  Controller::seek(position);
73  emit paused();
74  }
75  void pause() {
76  Controller::pause();
77  emit paused();
78  }
79  int displayWidth() const { return m_rect.width(); }
80  int displayHeight() const { return m_rect.height(); }
81 
82  QObject* videoWidget() { return this; }
83  Filter* glslManager() const { return m_glslManager; }
84  QRect rect() const { return m_rect; }
85  int grid() const { return m_grid; }
86  float zoom() const { return m_zoom * MLT.profile().width() / m_rect.width(); }
87  QPoint offset() const;
88  QImage image() const;
89  void requestImage() const;
90  bool snapToGrid() const { return m_snapToGrid; }
91 
92 public slots:
93  void onFrameDisplayed(const SharedFrame& frame);
94  void setGrid(int grid);
95  void setZoom(float zoom);
96  void setOffsetX(int x);
97  void setOffsetY(int y);
98  void setBlankScene();
99  void setCurrentFilter(QmlFilter* filter, QmlMetadata* meta);
100  void setSnapToGrid(bool snap);
101 
102 signals:
103  void frameDisplayed(const SharedFrame& frame);
104  void dragStarted();
105  void seekTo(int x);
106  void gpuNotSupported();
107  void started();
108  void paused();
109  void playing();
110  void rectChanged();
111  void gridChanged();
112  void zoomChanged();
113  void offsetChanged();
114  void imageReady();
115  void snapToGridChanged();
116 
117 private:
118  QRect m_rect;
119  int m_grid;
120  GLuint m_texture[3];
121  QOpenGLShaderProgram* m_shader;
122  QPoint m_dragStart;
123  Filter* m_glslManager;
124  QSemaphore m_initSem;
125  bool m_isInitialized;
126  Event* m_threadStartEvent;
127  Event* m_threadStopEvent;
128  Event* m_threadCreateEvent;
129  Event* m_threadJoinEvent;
130  FrameRenderer* m_frameRenderer;
131  int m_projectionLocation;
132  int m_modelViewLocation;
133  int m_vertexLocation;
134  int m_texCoordLocation;
135  int m_colorspaceLocation;
136  int m_textureLocation[3];
137  float m_zoom;
138  QPoint m_offset;
139  QOffscreenSurface m_offscreenSurface;
140  QOpenGLContext* m_shareContext;
141  SharedFrame m_sharedFrame;
142  QMutex m_mutex;
143  QUrl m_savedQmlSource;
144  bool m_snapToGrid;
145 
146  static void on_frame_show(mlt_consumer, void* self, mlt_frame frame);
147 
148 private slots:
149  void initializeGL();
150  void resizeGL(int width, int height);
151  void updateTexture(GLuint yName, GLuint uName, GLuint vName);
152  void paintGL();
153 
154 protected:
155  void resizeEvent(QResizeEvent* event);
156  void mousePressEvent(QMouseEvent *);
157  void mouseMoveEvent(QMouseEvent *);
158  void keyPressEvent(QKeyEvent* event);
159  bool event(QEvent* event);
160  void createShader();
161 };
162 
163 class RenderThread : public QThread
164 {
165  Q_OBJECT
166 public:
167  RenderThread(thread_function_t function, void* data, QOpenGLContext *context, QSurface* surface);
168 
169 protected:
170  void run();
171 
172 private:
173  thread_function_t m_function;
174  void* m_data;
175  QOpenGLContext* m_context;
176  QSurface* m_surface;
177 };
178 
179 class FrameRenderer : public QThread
180 {
181  Q_OBJECT
182 public:
183  FrameRenderer(QOpenGLContext* shareContext, QSurface* surface);
184  ~FrameRenderer();
185  QSemaphore* semaphore() { return &m_semaphore; }
186  QOpenGLContext* context() const { return m_context; }
187  SharedFrame getDisplayFrame();
188  Q_INVOKABLE void showFrame(Mlt::Frame frame);
189  void requestImage();
190  QImage image() const { return m_image; }
191 
192 public slots:
193  void cleanup();
194 
195 signals:
196  void textureReady(GLuint yName, GLuint uName = 0, GLuint vName = 0);
197  void frameDisplayed(const SharedFrame& frame);
198  void imageReady();
199 
200 private:
201  QSemaphore m_semaphore;
202  SharedFrame m_displayFrame;
203  QOpenGLContext* m_context;
204  QSurface* m_surface;
205  qint64 m_previousMSecs;
206  bool m_imageRequested;
207  QImage m_image;
208 
209 public:
210  GLuint m_renderTexture[3];
211  GLuint m_displayTexture[3];
212  QOpenGLFunctions_3_2_Core* m_gl32;
213 };
214 
215 } // namespace
216 
217 #endif
The SharedFrame provides thread safe access to Mlt::Frame data.
Definition: sharedframe.h:48