CuteLogger
Fast and simple logging solution for Qt based applications
glwidget.h
1 /*
2  * Copyright (c) 2011-2017 Meltytech, LLC
3  * Author: Dan Dennedy <dan@dennedy.org>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef GLWIDGET_H
20 #define GLWIDGET_H
21 
22 #include <QSemaphore>
23 #include <QQuickWidget>
24 #include <QOpenGLFunctions>
25 #include <QOpenGLShaderProgram>
26 #include <QOpenGLFramebufferObject>
27 #include <QOpenGLContext>
28 #include <QOffscreenSurface>
29 #include <QMutex>
30 #include <QThread>
31 #include <QRect>
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(QRect 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 pause() {
77  Controller::pause();
78  emit paused();
79  }
80  int displayWidth() const { return m_rect.width(); }
81  int displayHeight() const { return m_rect.height(); }
82 
83  QObject* videoWidget() { return this; }
84  Filter* glslManager() const { return m_glslManager; }
85  QRect rect() const { return m_rect; }
86  int grid() const { return m_grid; }
87  float zoom() const { return m_zoom * MLT.profile().width() / m_rect.width(); }
88  QPoint offset() const;
89  QImage image() const;
90  void requestImage() const;
91  bool snapToGrid() const { return m_snapToGrid; }
92 
93 public slots:
94  void onFrameDisplayed(const SharedFrame& frame);
95  void setGrid(int grid);
96  void setZoom(float zoom);
97  void setOffsetX(int x);
98  void setOffsetY(int y);
99  void setBlankScene();
100  void setCurrentFilter(QmlFilter* filter, QmlMetadata* meta);
101  void setSnapToGrid(bool snap);
102 
103 signals:
104  void frameDisplayed(const SharedFrame& frame);
105  void dragStarted();
106  void seekTo(int x);
107  void gpuNotSupported();
108  void started();
109  void paused();
110  void playing();
111  void rectChanged();
112  void gridChanged();
113  void zoomChanged();
114  void offsetChanged();
115  void imageReady();
116  void snapToGridChanged();
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  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
Definition: encodedock.h:35
The SharedFrame provides thread safe access to Mlt::Frame data.
Definition: sharedframe.h:48