3 #include <QDialogButtonBox>
6 #include <movit/resource_pool.h>
7 #include <movit/util.h>
12 #include "ui_analyzer.h"
17 : ui(new Ui::Analyzer)
21 //connect(ui->button_box, &QDialogButtonBox::accepted, [this]{ this->close(); });
23 ui->input_box->addItem("Live", Mixer::OUTPUT_LIVE);
24 ui->input_box->addItem("Preview", Mixer::OUTPUT_PREVIEW);
25 unsigned num_channels = global_mixer->get_num_channels();
26 for (unsigned channel_idx = 0; channel_idx < num_channels; ++channel_idx) {
27 Mixer::Output channel = static_cast<Mixer::Output>(Mixer::OUTPUT_INPUT0 + channel_idx);
28 string name = global_mixer->get_channel_name(channel);
29 ui->input_box->addItem(QString::fromStdString(name), channel);
32 connect(ui->grab_btn, &QPushButton::clicked, bind(&Analyzer::grab_clicked, this));
33 //ui->display->set_output(Mixer::OUTPUT_LIVE);
34 surface = create_surface(QSurfaceFormat::defaultFormat());
35 context = create_context(surface);
37 if (!make_current(context, surface)) {
42 glGenBuffers(1, &pbo);
43 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo);
44 glBufferData(GL_PIXEL_PACK_BUFFER_ARB, global_flags.width * global_flags.height * 4, NULL, GL_STREAM_READ);
49 if (!make_current(context, surface)) {
53 glDeleteBuffers(1, &pbo);
55 if (resource_pool != nullptr) {
56 resource_pool->clean_context();
58 delete_context(context);
59 delete surface; // TODO?
62 void Analyzer::grab_clicked()
64 Mixer::Output channel = static_cast<Mixer::Output>(ui->input_box->currentData().value<int>());
66 if (!make_current(context, surface)) {
71 Mixer::DisplayFrame frame;
72 if (!global_mixer->get_display_frame(channel, &frame)) {
73 printf("Not ready yet\n");
77 // Set up an FBO to render into.
78 if (resource_pool == nullptr) {
79 resource_pool = frame.chain->get_resource_pool();
81 assert(resource_pool == frame.chain->get_resource_pool());
83 GLuint fbo_tex = resource_pool->create_2d_texture(GL_RGBA8, global_flags.width, global_flags.height);
85 GLuint fbo = resource_pool->create_fbo(fbo_tex);
88 glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
92 glDisable(GL_FRAMEBUFFER_SRGB);
94 frame.chain->render_to_fbo(fbo, global_flags.width, global_flags.height);
97 // Read back to memory.
98 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
100 glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo);
102 glReadPixels(0, 0, global_flags.width, global_flags.height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
105 unsigned char *buf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
108 int r_hist[256] = {0}, g_hist[256] = {0}, b_hist[256] = {0};
109 const unsigned char *ptr = buf;
110 for (int y = 0; y < global_flags.width; ++y) {
111 for (int x = 0; x < global_flags.height; ++x) {
123 glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
125 glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
127 glBindFramebuffer(GL_FRAMEBUFFER, 0);
131 for (unsigned i = 0; i < 256; ++i) { printf(" %d", r_hist[i]); }
134 for (unsigned i = 0; i < 256; ++i) { printf(" %d", g_hist[i]); }
137 for (unsigned i = 0; i < 256; ++i) { printf(" %d", b_hist[i]); }
140 resource_pool->release_2d_texture(fbo_tex);
142 resource_pool->release_fbo(fbo);