]> git.sesse.net Git - nageru/blob - glwidget.cpp
Make NUM_CARDS into a command-line flag.
[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 #include "mainwindow.h"
13
14 #include <movit/resource_pool.h>
15 #include <stdio.h>
16 #include <mutex>
17
18 #include "context.h"
19 #include "flags.h"
20 #include "mixer.h"
21 #include "ref_counted_gl_sync.h"
22 #include "vumeter.h"
23
24 class MainWindow;
25 class QSurface;
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       resource_pool(new movit::ResourcePool)
37 {
38 }
39
40 GLWidget::~GLWidget()
41 {
42 }
43
44 void GLWidget::initializeGL()
45 {
46         printf("egl context=%p\n", eglGetCurrentContext());
47         //printf("threads: %p %p\n", QThread::currentThread(), qGuiApp->thread());
48
49         static std::once_flag flag;
50         std::call_once(flag, [this]{
51                 global_mixer = new Mixer(QGLFormat::toSurfaceFormat(format()), global_flags.num_cards);
52                 global_mainwindow->mixer_created(global_mixer);
53                 global_mixer->start();
54         });
55         global_mixer->set_frame_ready_callback(output, [this]{
56                 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
57                 emit transition_names_updated(global_mixer->get_transition_names());
58         });
59
60         glDisable(GL_BLEND);
61         glDisable(GL_DEPTH_TEST);
62         glDepthMask(GL_FALSE);
63 }
64
65 void GLWidget::resizeGL(int width, int height)
66 {
67         glViewport(0, 0, width, height);
68 }
69
70 void GLWidget::paintGL()
71 {
72         Mixer::DisplayFrame frame;
73         if (!global_mixer->get_display_frame(output, &frame)) {
74                 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
75                 check_error();
76                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
77                 check_error();
78                 return;
79         }
80
81         check_error();
82         glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
83         check_error();
84         frame.setup_chain();
85         check_error();
86         frame.chain->render_to_screen();
87         check_error();
88 }
89
90 void GLWidget::mousePressEvent(QMouseEvent *event)
91 {
92         emit clicked();
93 }