]> git.sesse.net Git - nageru/blob - glwidget.cpp
Add a peak display.
[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 "mixer.h"
20 #include "ref_counted_gl_sync.h"
21 #include "vumeter.h"
22
23 class MainWindow;
24 class QSurface;
25 class QWidget;
26
27 #undef Success
28 #include <movit/util.h>
29 #include <string>
30
31 using namespace std;
32
33 GLWidget::GLWidget(QWidget *parent)
34     : QGLWidget(parent, global_share_widget),
35       resource_pool(new movit::ResourcePool)
36 {
37 }
38
39 GLWidget::~GLWidget()
40 {
41 }
42
43 void GLWidget::initializeGL()
44 {
45         printf("egl context=%p\n", eglGetCurrentContext());
46         //printf("threads: %p %p\n", QThread::currentThread(), qGuiApp->thread());
47
48         static std::once_flag flag;
49         std::call_once(flag, [this]{
50                 global_mixer = new Mixer(QGLFormat::toSurfaceFormat(format()));
51                 global_mixer->set_audio_level_callback([this](float level_lufs, float peak_db){
52                         global_vu_meter->set_level(level_lufs);
53
54                         char buf[256];
55                         snprintf(buf, sizeof(buf), "%.1f", peak_db);
56                         global_peak_display->setText(buf);
57                 });
58                 global_mixer->start();
59         });
60         global_mixer->set_frame_ready_callback(output, [this]{
61                 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
62                 emit transition_names_updated(global_mixer->get_transition_names());
63         });
64
65         glDisable(GL_BLEND);
66         glDisable(GL_DEPTH_TEST);
67         glDepthMask(GL_FALSE);
68 }
69
70 void GLWidget::resizeGL(int width, int height)
71 {
72         glViewport(0, 0, width, height);
73 }
74
75 void GLWidget::paintGL()
76 {
77         Mixer::DisplayFrame frame;
78         if (!global_mixer->get_display_frame(output, &frame)) {
79                 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
80                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
81                 return;
82         }
83
84         glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
85         frame.setup_chain();
86         frame.chain->render_to_screen();
87         check_error();
88 }
89
90 void GLWidget::mousePressEvent(QMouseEvent *event)
91 {
92         emit clicked();
93 }