3 #include <QDialogButtonBox>
9 #include <movit/resource_pool.h>
10 #include <movit/util.h>
12 #include "shared/context.h"
15 #include "ui_analyzer.h"
17 // QCustomPlot includes qopenglfunctions.h, which #undefs all of the epoxy
18 // definitions (ugh) and doesn't put back any others (ugh). Add the ones we
21 #define glBindBuffer epoxy_glBindBuffer
22 #define glBindFramebuffer epoxy_glBindFramebuffer
23 #define glBufferData epoxy_glBufferData
24 #define glDeleteBuffers epoxy_glDeleteBuffers
25 #define glDisable epoxy_glDisable
26 #define glGenBuffers epoxy_glGenBuffers
27 #define glGetError epoxy_glGetError
28 #define glReadPixels epoxy_glReadPixels
29 #define glUnmapBuffer epoxy_glUnmapBuffer
30 #define glWaitSync epoxy_glWaitSync
35 : ui(new Ui::Analyzer),
36 grabbed_image(global_flags.width, global_flags.height, QImage::Format_ARGB32_Premultiplied)
40 surface = create_surface(QSurfaceFormat::defaultFormat());
41 context = create_context(surface);
42 if (!make_current(context, surface)) {
47 grab_timer.setSingleShot(true);
48 connect(&grab_timer, &QTimer::timeout, bind(&Analyzer::grab_clicked, this));
50 ui->input_box->addItem("Live", Mixer::OUTPUT_LIVE);
51 ui->input_box->addItem("Preview", Mixer::OUTPUT_PREVIEW);
52 unsigned num_channels = global_mixer->get_num_channels();
53 for (unsigned channel_idx = 0; channel_idx < num_channels; ++channel_idx) {
54 Mixer::Output channel = static_cast<Mixer::Output>(Mixer::OUTPUT_INPUT0 + channel_idx);
55 string name = global_mixer->get_channel_name(channel);
56 ui->input_box->addItem(QString::fromStdString(name), channel);
59 ui->grab_frequency_box->addItem("Never", 0);
60 ui->grab_frequency_box->addItem("100 ms", 100);
61 ui->grab_frequency_box->addItem("1 sec", 1000);
62 ui->grab_frequency_box->addItem("10 sec", 10000);
63 ui->grab_frequency_box->setCurrentIndex(2);
65 connect(ui->grab_btn, &QPushButton::clicked, bind(&Analyzer::grab_clicked, this));
66 connect(ui->input_box, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), bind(&Analyzer::signal_changed, this));
68 ui->grabbed_frame_label->installEventFilter(this);
70 glGenBuffers(1, &pbo);
71 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo);
72 glBufferData(GL_PIXEL_PACK_BUFFER_ARB, global_flags.width * global_flags.height * 4, nullptr, GL_STREAM_READ);
74 ui->histogram->xAxis->setVisible(true);
75 ui->histogram->yAxis->setVisible(false);
76 ui->histogram->xAxis->setRange(0, 255);
81 delete_context(context);
85 void Analyzer::update_channel_name(Mixer::Output output, const string &name)
87 if (output >= Mixer::OUTPUT_INPUT0) {
88 int index = (output - Mixer::OUTPUT_INPUT0) + 2;
89 ui->input_box->setItemText(index, QString::fromStdString(name));
93 void Analyzer::mixer_shutting_down()
95 ui->display->shutdown();
97 if (!make_current(context, surface)) {
101 glDeleteBuffers(1, &pbo);
103 if (resource_pool != nullptr) {
104 resource_pool->clean_context();
108 void Analyzer::grab_clicked()
110 Mixer::Output channel = static_cast<Mixer::Output>(ui->input_box->currentData().value<int>());
112 if (!make_current(context, surface)) {
117 Mixer::DisplayFrame frame;
118 if (!global_mixer->get_display_frame(channel, &frame)) {
123 // Set up an FBO to render into.
124 if (resource_pool == nullptr) {
125 resource_pool = frame.chain->get_resource_pool();
127 assert(resource_pool == frame.chain->get_resource_pool());
129 GLuint fbo_tex = resource_pool->create_2d_texture(GL_RGBA8, global_flags.width, global_flags.height);
131 GLuint fbo = resource_pool->create_fbo(fbo_tex);
134 glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
138 glDisable(GL_FRAMEBUFFER_SRGB);
140 frame.chain->render_to_fbo(fbo, global_flags.width, global_flags.height);
143 // Read back to memory.
144 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
146 glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo);
148 glReadPixels(0, 0, global_flags.width, global_flags.height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
151 unsigned char *buf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
154 size_t pitch = global_flags.width * 4;
155 for (int y = 0; y < global_flags.height; ++y) {
156 memcpy(grabbed_image.scanLine(global_flags.height - y - 1), buf + y * pitch, pitch);
161 snprintf(buf, sizeof(buf), "Grabbed frame (%dx%d)", global_flags.width, global_flags.height);
162 ui->grabbed_frame_sublabel->setText(buf);
166 pixmap.convertFromImage(grabbed_image);
167 ui->grabbed_frame_label->setPixmap(pixmap);
169 int r_hist[256] = {0}, g_hist[256] = {0}, b_hist[256] = {0};
170 const unsigned char *ptr = buf;
171 for (int i = 0; i < global_flags.height * global_flags.width; ++i) {
182 glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
184 glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
186 glBindFramebuffer(GL_FRAMEBUFFER, 0);
189 QVector<double> r_vec(256), g_vec(256), b_vec(256), x_vec(256);
191 for (unsigned i = 0; i < 256; ++i) {
193 r_vec[i] = log(r_hist[i] + 1.0);
194 g_vec[i] = log(g_hist[i] + 1.0);
195 b_vec[i] = log(b_hist[i] + 1.0);
197 max = std::max(max, r_vec[i]);
198 max = std::max(max, g_vec[i]);
199 max = std::max(max, b_vec[i]);
202 ui->histogram->clearGraphs();
203 ui->histogram->addGraph();
204 ui->histogram->graph(0)->setData(x_vec, r_vec);
205 ui->histogram->graph(0)->setPen(QPen(Qt::red));
206 ui->histogram->graph(0)->setBrush(QBrush(QColor(255, 127, 127, 80)));
207 ui->histogram->addGraph();
208 ui->histogram->graph(1)->setData(x_vec, g_vec);
209 ui->histogram->graph(1)->setPen(QPen(Qt::green));
210 ui->histogram->graph(1)->setBrush(QBrush(QColor(127, 255, 127, 80)));
211 ui->histogram->addGraph();
212 ui->histogram->graph(2)->setData(x_vec, b_vec);
213 ui->histogram->graph(2)->setPen(QPen(Qt::blue));
214 ui->histogram->graph(2)->setBrush(QBrush(QColor(127, 127, 255, 80)));
216 ui->histogram->xAxis->setVisible(true);
217 ui->histogram->yAxis->setVisible(false);
218 ui->histogram->xAxis->setRange(0, 255);
219 ui->histogram->yAxis->setRange(0, max);
220 ui->histogram->replot();
222 resource_pool->release_2d_texture(fbo_tex);
224 resource_pool->release_fbo(fbo);
227 if (last_x >= 0 && last_y >= 0) {
228 grab_pixel(last_x, last_y);
234 // Set up the next autograb if configured.
235 int delay = ui->grab_frequency_box->currentData().toInt(nullptr);
237 grab_timer.start(delay);
242 void Analyzer::signal_changed()
244 Mixer::Output channel = static_cast<Mixer::Output>(ui->input_box->currentData().value<int>());
245 ui->display->set_output(channel);
249 bool Analyzer::eventFilter(QObject *watched, QEvent *event)
251 if (event->type() == QEvent::MouseMove && watched->isWidgetType()) {
252 const QMouseEvent *mouse_event = (QMouseEvent *)event;
253 last_x = mouse_event->x();
254 last_y = mouse_event->y();
255 grab_pixel(mouse_event->x(), mouse_event->y());
257 if (event->type() == QEvent::Leave && watched->isWidgetType()) {
258 last_x = last_y = -1;
259 ui->coord_label->setText("Selected coordinate (x,y): (none)");
260 ui->red_label->setText(u8"—");
261 ui->green_label->setText(u8"—");
262 ui->blue_label->setText(u8"—");
263 ui->hex_label->setText(u8"#—");
268 void Analyzer::grab_pixel(int x, int y)
270 const QPixmap *pixmap = ui->grabbed_frame_label->pixmap();
271 if (pixmap != nullptr) {
272 x = lrint(x * double(pixmap->width()) / ui->grabbed_frame_label->width());
273 y = lrint(y * double(pixmap->height()) / ui->grabbed_frame_label->height());
274 x = std::min(x, pixmap->width() - 1);
275 y = std::min(y, pixmap->height() - 1);
278 snprintf(buf, sizeof(buf), "Selected coordinate (x,y): (%d,%d)", x, y);
279 ui->coord_label->setText(buf);
281 QRgb pixel = grabbed_image.pixel(x, y);
282 ui->red_label->setText(QString::fromStdString(to_string(qRed(pixel))));
283 ui->green_label->setText(QString::fromStdString(to_string(qGreen(pixel))));
284 ui->blue_label->setText(QString::fromStdString(to_string(qBlue(pixel))));
286 snprintf(buf, sizeof(buf), "#%02x%02x%02x", qRed(pixel), qGreen(pixel), qBlue(pixel));
287 ui->hex_label->setText(buf);
291 void Analyzer::resizeEvent(QResizeEvent* event)
293 QMainWindow::resizeEvent(event);
295 // Ask for a relayout, but only after the event loop is done doing relayout
296 // on everything else.
297 QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
300 void Analyzer::showEvent(QShowEvent *event)
305 void Analyzer::relayout()
307 double aspect = double(global_flags.width) / global_flags.height;
309 // Left pane (2/5 of the width).
311 int width = ui->left_pane->geometry().width();
312 int height = ui->left_pane->geometry().height();
314 // Figure out how much space everything that's non-responsive needs.
315 int remaining_height = height - ui->left_pane->spacing() * (ui->left_pane->count() - 1);
317 remaining_height -= ui->input_box->geometry().height();
318 ui->left_pane->setStretch(2, ui->grab_btn->geometry().height());
320 remaining_height -= ui->grab_btn->geometry().height();
321 ui->left_pane->setStretch(3, ui->grab_btn->geometry().height());
323 remaining_height -= ui->histogram_label->geometry().height();
324 ui->left_pane->setStretch(5, ui->histogram_label->geometry().height());
326 // The histogram's minimumHeight returns 0, so let's set a reasonable minimum for it.
327 int min_histogram_height = 50;
328 remaining_height -= min_histogram_height;
330 // Allocate so that the display is 16:9, if possible.
331 unsigned wanted_display_height = width / aspect;
332 unsigned display_height;
334 if (remaining_height >= int(wanted_display_height)) {
335 display_height = wanted_display_height;
337 display_height = remaining_height;
338 int display_width = lrint(display_height * aspect);
339 margin = (width - display_width) / 2;
341 ui->left_pane->setStretch(1, display_height);
342 ui->display_left_spacer->changeSize(margin, 1);
343 ui->display_right_spacer->changeSize(margin, 1);
345 remaining_height -= display_height;
347 // Figure out if we can do the histogram at 16:9.
348 remaining_height += min_histogram_height;
349 unsigned histogram_height;
350 if (remaining_height >= int(wanted_display_height)) {
351 histogram_height = wanted_display_height;
353 histogram_height = remaining_height;
355 remaining_height -= histogram_height;
356 ui->left_pane->setStretch(4, histogram_height);
358 ui->left_pane->setStretch(0, remaining_height / 2);
359 ui->left_pane->setStretch(6, remaining_height / 2);
362 // Right pane (remaining 3/5 of the width).
364 int width = ui->right_pane->geometry().width();
365 int height = ui->right_pane->geometry().height();
367 // Figure out how much space everything that's non-responsive needs.
368 int remaining_height = height - ui->right_pane->spacing() * (ui->right_pane->count() - 1);
369 remaining_height -= ui->grabbed_frame_sublabel->geometry().height();
370 remaining_height -= ui->coord_label->geometry().height();
371 remaining_height -= ui->color_hbox->geometry().height();
373 // Allocate so that the display is 16:9, if possible.
374 unsigned wanted_display_height = width / aspect;
375 unsigned display_height;
377 if (remaining_height >= int(wanted_display_height)) {
378 display_height = wanted_display_height;
380 display_height = remaining_height;
381 int display_width = lrint(display_height * aspect);
382 margin = (width - display_width) / 2;
384 ui->right_pane->setStretch(1, display_height);
385 ui->grabbed_frame_left_spacer->changeSize(margin, 1);
386 ui->grabbed_frame_right_spacer->changeSize(margin, 1);
387 remaining_height -= display_height;
389 if (remaining_height < 0) remaining_height = 0;
391 ui->right_pane->setStretch(0, remaining_height / 2);
392 ui->right_pane->setStretch(5, remaining_height / 2);