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  void toggleZoom(bool);
117 
118 private:
119  QRect m_rect;
120  int m_grid;
121  GLuint m_texture[3];
122  QOpenGLShaderProgram* m_shader;
123  QPoint m_dragStart;
124  Filter* m_glslManager;
125  QSemaphore m_initSem;
126  bool m_isInitialized;
127  Event* m_threadStartEvent;
128  Event* m_threadStopEvent;
129  Event* m_threadCreateEvent;
130  Event* m_threadJoinEvent;
131  FrameRenderer* m_frameRenderer;
132  int m_projectionLocation;
133  int m_modelViewLocation;
134  int m_vertexLocation;
135  int m_texCoordLocation;
136  int m_colorspaceLocation;
137  int m_textureLocation[3];
138  float m_zoom;
139  QPoint m_offset;
140  QOffscreenSurface m_offscreenSurface;
141  QOpenGLContext* m_shareContext;
142  SharedFrame m_sharedFrame;
143  QMutex m_mutex;
144  QUrl m_savedQmlSource;
145  bool m_snapToGrid;
146 
147  static void on_frame_show(mlt_consumer, void* self, mlt_frame frame);
148 
149 private slots:
150  void initializeGL();
151  void resizeGL(int width, int height);
152  void updateTexture(GLuint yName, GLuint uName, GLuint vName);
153  void paintGL();
154 
155 protected:
156  void resizeEvent(QResizeEvent* event);
157  void mousePressEvent(QMouseEvent *);
158  void mouseMoveEvent(QMouseEvent *);
159  void keyPressEvent(QKeyEvent* event);
160  bool event(QEvent* event);
161  void createShader();
162 };
163 
164 class RenderThread : public QThread
165 {
166  Q_OBJECT
167 public:
168  RenderThread(thread_function_t function, void* data, QOpenGLContext *context, QSurface* surface);
169 
170 protected:
171  void run();
172 
173 private:
174  thread_function_t m_function;
175  void* m_data;
176  QOpenGLContext* m_context;
177  QSurface* m_surface;
178 };
179 
180 class FrameRenderer : public QThread
181 {
182  Q_OBJECT
183 public:
184  FrameRenderer(QOpenGLContext* shareContext, QSurface* surface);
185  ~FrameRenderer();
186  QSemaphore* semaphore() { return &m_semaphore; }
187  QOpenGLContext* context() const { return m_context; }
188  SharedFrame getDisplayFrame();
189  Q_INVOKABLE void showFrame(Mlt::Frame frame);
190  void requestImage();
191  QImage image() const { return m_image; }
192 
193 public slots:
194  void cleanup();
195 
196 signals:
197  void textureReady(GLuint yName, GLuint uName = 0, GLuint vName = 0);
198  void frameDisplayed(const SharedFrame& frame);
199  void imageReady();
200 
201 private:
202  QSemaphore m_semaphore;
203  SharedFrame m_displayFrame;
204  QOpenGLContext* m_context;
205  QSurface* m_surface;
206  qint64 m_previousMSecs;
207  bool m_imageRequested;
208  QImage m_image;
209 
210 public:
211  GLuint m_renderTexture[3];
212  GLuint m_displayTexture[3];
213  QOpenGLFunctions_3_2_Core* m_gl32;
214 };
215 
216 } // namespace
217 
218 #endif
The SharedFrame provides thread safe access to Mlt::Frame data.
Definition: sharedframe.h:48