]> git.sesse.net Git - nageru/blob - analyzer.cpp
Fix crashes on exit.
[nageru] / analyzer.cpp
1 #include "analyzer.h"
2
3 #include <QDialogButtonBox>
4 #include <QMouseEvent>
5 #include <QPen>
6 #include <QSurface>
7
8 #include <movit/resource_pool.h>
9 #include <movit/util.h>
10
11 #include "context.h"
12 #include "flags.h"
13 #include "mixer.h"
14 #include "ui_analyzer.h"
15
16 // QCustomPlot includes qopenglfunctions.h, which #undefs all of the epoxy
17 // definitions (ugh) and doesn't put back any others (ugh). Add the ones we
18 // need back.
19
20 #define glBindBuffer epoxy_glBindBuffer
21 #define glBindFramebuffer epoxy_glBindFramebuffer
22 #define glBufferData epoxy_glBufferData
23 #define glDeleteBuffers epoxy_glDeleteBuffers
24 #define glDisable epoxy_glDisable
25 #define glGenBuffers epoxy_glGenBuffers
26 #define glGetError epoxy_glGetError
27 #define glReadPixels epoxy_glReadPixels
28 #define glUnmapBuffer epoxy_glUnmapBuffer
29 #define glWaitSync epoxy_glWaitSync
30
31 using namespace std;
32
33 Analyzer::Analyzer()
34         : ui(new Ui::Analyzer),
35           grabbed_image(global_flags.width, global_flags.height, QImage::Format_ARGB32_Premultiplied)
36 {
37         ui->setupUi(this);
38
39         //connect(ui->button_box, &QDialogButtonBox::accepted, [this]{ this->close(); });
40
41         ui->input_box->addItem("Live", Mixer::OUTPUT_LIVE);
42         ui->input_box->addItem("Preview", Mixer::OUTPUT_PREVIEW);
43         unsigned num_channels = global_mixer->get_num_channels();
44         for (unsigned channel_idx = 0; channel_idx < num_channels; ++channel_idx) {
45                 Mixer::Output channel = static_cast<Mixer::Output>(Mixer::OUTPUT_INPUT0 + channel_idx); 
46                 string name = global_mixer->get_channel_name(channel);
47                 ui->input_box->addItem(QString::fromStdString(name), channel);
48         }
49
50         connect(ui->grab_btn, &QPushButton::clicked, bind(&Analyzer::grab_clicked, this));
51         connect(ui->input_box, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), bind(&Analyzer::signal_changed, this));
52         signal_changed();
53         ui->grabbed_frame_label->installEventFilter(this);
54
55         surface = create_surface(QSurfaceFormat::defaultFormat());
56         context = create_context(surface);
57
58         if (!make_current(context, surface)) {
59                 printf("oops\n");
60                 exit(1);
61         }
62
63         glGenBuffers(1, &pbo);
64         glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo);
65         glBufferData(GL_PIXEL_PACK_BUFFER_ARB, global_flags.width * global_flags.height * 4, NULL, GL_STREAM_READ);
66
67         ui->histogram->xAxis->setVisible(true);
68         ui->histogram->yAxis->setVisible(false);
69         ui->histogram->xAxis->setRange(0, 255);
70 }
71
72 Analyzer::~Analyzer()
73 {
74         if (!make_current(context, surface)) {
75                 printf("oops\n");
76                 exit(1);
77         }
78         glDeleteBuffers(1, &pbo);
79         check_error();
80         ui->display->shutdown();
81         if (resource_pool != nullptr) {
82                 resource_pool->clean_context();
83         }
84         delete_context(context);
85         delete surface;
86 }
87
88 void Analyzer::grab_clicked()
89 {
90         Mixer::Output channel = static_cast<Mixer::Output>(ui->input_box->currentData().value<int>());
91
92         if (!make_current(context, surface)) {
93                 printf("oops\n");
94                 exit(1);
95         }
96
97         Mixer::DisplayFrame frame;
98         if (!global_mixer->get_display_frame(channel, &frame)) {
99                 printf("Not ready yet\n");
100                 return;
101         }
102
103         // Set up an FBO to render into.
104         if (resource_pool == nullptr) {
105                 resource_pool = frame.chain->get_resource_pool();
106         } else {
107                 assert(resource_pool == frame.chain->get_resource_pool());
108         }
109         GLuint fbo_tex = resource_pool->create_2d_texture(GL_RGBA8, global_flags.width, global_flags.height);
110         check_error();
111         GLuint fbo = resource_pool->create_fbo(fbo_tex);
112         check_error();
113
114         glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
115         check_error();
116         frame.setup_chain();
117         check_error();
118         glDisable(GL_FRAMEBUFFER_SRGB);
119         check_error();
120         frame.chain->render_to_fbo(fbo, global_flags.width, global_flags.height);
121         check_error();
122
123         // Read back to memory.
124         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
125         check_error();
126         glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo);
127         check_error();
128         glReadPixels(0, 0, global_flags.width, global_flags.height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
129         check_error();
130
131         unsigned char *buf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
132         check_error();
133
134         size_t pitch = global_flags.width * 4;
135         for (int y = 0; y < global_flags.height; ++y) {
136                 memcpy(grabbed_image.scanLine(global_flags.height - y - 1), buf + y * pitch, pitch);
137         }
138
139         {
140                 char buf[256];
141                 snprintf(buf, sizeof(buf), "Grabbed frame (%dx%d)", global_flags.width, global_flags.height);
142                 ui->grabbed_frame_sublabel->setText(buf);
143         }
144
145         QPixmap pixmap;
146         pixmap.convertFromImage(grabbed_image);
147         ui->grabbed_frame_label->setPixmap(pixmap);
148
149         int r_hist[256] = {0}, g_hist[256] = {0}, b_hist[256] = {0};
150         const unsigned char *ptr = buf;
151         for (int i = 0; i < global_flags.height * global_flags.width; ++i) {
152                 uint8_t b = *ptr++;
153                 uint8_t g = *ptr++;
154                 uint8_t r = *ptr++;
155                 ++ptr;
156
157                 ++r_hist[r];
158                 ++g_hist[g];
159                 ++b_hist[b];
160         }
161
162         glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
163         check_error();
164         glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
165         check_error();
166         glBindFramebuffer(GL_FRAMEBUFFER, 0);
167         check_error();
168
169         QVector<double> r_vec(256), g_vec(256), b_vec(256), x_vec(256);
170         double max = 0.0;
171         for (unsigned i = 0; i < 256; ++i) {
172                 x_vec[i] = i;
173                 r_vec[i] = log(r_hist[i] + 1.0);
174                 g_vec[i] = log(g_hist[i] + 1.0);
175                 b_vec[i] = log(b_hist[i] + 1.0);
176
177                 max = std::max(max, r_vec[i]);
178                 max = std::max(max, g_vec[i]);
179                 max = std::max(max, b_vec[i]);
180         }
181
182         ui->histogram->clearGraphs();
183         ui->histogram->addGraph();
184         ui->histogram->graph(0)->setData(x_vec, r_vec);
185         ui->histogram->graph(0)->setPen(QPen(Qt::red));
186         ui->histogram->graph(0)->setBrush(QBrush(QColor(255, 127, 127, 80)));
187         ui->histogram->addGraph();
188         ui->histogram->graph(1)->setData(x_vec, g_vec);
189         ui->histogram->graph(1)->setPen(QPen(Qt::green));
190         ui->histogram->graph(1)->setBrush(QBrush(QColor(127, 255, 127, 80)));
191         ui->histogram->addGraph();
192         ui->histogram->graph(2)->setData(x_vec, b_vec);
193         ui->histogram->graph(2)->setPen(QPen(Qt::blue));
194         ui->histogram->graph(2)->setBrush(QBrush(QColor(127, 127, 255, 80)));
195
196         ui->histogram->xAxis->setVisible(true);
197         ui->histogram->yAxis->setVisible(false);
198         ui->histogram->xAxis->setRange(0, 255);
199         ui->histogram->yAxis->setRange(0, max);
200         ui->histogram->replot();
201
202         resource_pool->release_2d_texture(fbo_tex);
203         check_error();
204         resource_pool->release_fbo(fbo);
205         check_error();
206 }
207
208 void Analyzer::signal_changed()
209 {
210         Mixer::Output channel = static_cast<Mixer::Output>(ui->input_box->currentData().value<int>());
211         ui->display->set_output(channel);
212 }
213
214 bool Analyzer::eventFilter(QObject *watched, QEvent *event)
215 {
216         if (event->type() == QEvent::MouseMove &&
217             watched->isWidgetType()) {
218                 const QMouseEvent *mouse_event = (QMouseEvent *)event;
219                 const QPixmap *pixmap = ui->grabbed_frame_label->pixmap();
220                 if (pixmap != nullptr) {
221                         int x = lrint(mouse_event->x() * double(pixmap->width()) / ui->grabbed_frame_label->width());
222                         int y = lrint(mouse_event->y() * double(pixmap->height()) / ui->grabbed_frame_label->height());
223                         x = std::min(x, pixmap->width() - 1);
224                         y = std::min(y, pixmap->height() - 1);
225                         QRgb pixel = grabbed_image.pixel(x, y);
226                         ui->red_label->setText(QString::fromStdString(to_string(qRed(pixel))));
227                         ui->green_label->setText(QString::fromStdString(to_string(qGreen(pixel))));
228                         ui->blue_label->setText(QString::fromStdString(to_string(qBlue(pixel))));
229
230                         char buf[256];
231                         snprintf(buf, sizeof(buf), "#%02x%02x%02x", qRed(pixel), qGreen(pixel), qBlue(pixel));
232                         ui->hex_label->setText(buf);
233                 }
234         }
235         return false;
236 }