]> git.sesse.net Git - nageru/blob - glwidget.cpp
Fix a small memory leak in GLWidget.
[nageru] / glwidget.cpp
1 #include <qmetatype.h>  // Needs to come before egl.h.
2 #include <qdatastream.h>  // Needs to come before egl.h.
3 #include <qtextstream.h>  // Needs to come before egl.h.
4 #include <qcursor.h>  // Needs to come before egl.h.
5 #include <qcoreevent.h>  // Needs to come before egl.h.
6 #include <qevent.h>  // Needs to come before egl.h.
7 #include <epoxy/gl.h>
8 #include <epoxy/egl.h>
9 #include <QSurfaceFormat>
10
11 #include "glwidget.h"
12
13 #include <stdio.h>
14 #include <functional>
15 #include <mutex>
16
17 #include "context.h"
18 #include "effect_chain.h"
19 #include "flags.h"
20 #include "mainwindow.h"
21 #include "mixer.h"
22 #include "qnamespace.h"
23 #include "ref_counted_gl_sync.h"
24
25 class QMouseEvent;
26 class QWidget;
27
28 #undef Success
29 #include <movit/util.h>
30 #include <string>
31
32 using namespace std;
33
34 GLWidget::GLWidget(QWidget *parent)
35     : QGLWidget(parent, global_share_widget)
36 {
37 }
38
39 GLWidget::~GLWidget()
40 {
41 }
42
43 void GLWidget::initializeGL()
44 {
45         static std::once_flag flag;
46         std::call_once(flag, [this]{
47                 global_mixer = new Mixer(QGLFormat::toSurfaceFormat(format()), global_flags.num_cards);
48                 global_mainwindow->mixer_created(global_mixer);
49                 global_mixer->start();
50         });
51         global_mixer->set_frame_ready_callback(output, [this]{
52                 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
53                 emit transition_names_updated(global_mixer->get_transition_names());
54         });
55
56         glDisable(GL_BLEND);
57         glDisable(GL_DEPTH_TEST);
58         glDepthMask(GL_FALSE);
59 }
60
61 void GLWidget::resizeGL(int width, int height)
62 {
63         glViewport(0, 0, width, height);
64 }
65
66 void GLWidget::paintGL()
67 {
68         Mixer::DisplayFrame frame;
69         if (!global_mixer->get_display_frame(output, &frame)) {
70                 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
71                 check_error();
72                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
73                 check_error();
74                 return;
75         }
76
77         check_error();
78         glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
79         check_error();
80         frame.setup_chain();
81         check_error();
82         frame.chain->render_to_screen();
83         check_error();
84 }
85
86 void GLWidget::mousePressEvent(QMouseEvent *event)
87 {
88         emit clicked();
89 }