]> git.sesse.net Git - nageru/blob - nageru/glwidget.cpp
Make number of cards flexible at runtime.
[nageru] / nageru / glwidget.cpp
1 #include "glwidget.h"
2
3 #include <assert.h>
4 #include <bmusb/bmusb.h>
5 #include <movit/effect_chain.h>
6 #include <movit/resource_pool.h>
7 #include <stdbool.h>
8 #include <stdint.h>
9 #include <QAction>
10 #include <QActionGroup>
11 #include <QInputDialog>
12 #include <QList>
13 #include <QMenu>
14 #include <QPoint>
15 #include <QVariant>
16 #include <QWidget>
17 #include <functional>
18 #include <map>
19 #include <mutex>
20 #include <utility>
21
22 #include "audio_mixer.h"
23 #include "shared/context.h"
24 #include "context_menus.h"
25 #include "flags.h"
26 #include "mainwindow.h"
27 #include "mixer.h"
28 #include "shared/ref_counted_gl_sync.h"
29
30 class QMouseEvent;
31
32 #undef Success
33 #include <movit/util.h>
34 #include <string>
35
36 using namespace movit;
37 using namespace std;
38 using namespace std::placeholders;
39
40 namespace {
41
42 double srgb_to_linear(double x)
43 {
44         if (x < 0.04045) {
45                 return x / 12.92;
46         } else {
47                 return pow((x + 0.055) / 1.055, 2.4);
48         }
49 }
50
51 }  // namespace
52
53 GLWidget::GLWidget(QWidget *parent)
54     : QGLWidget(parent, global_share_widget)
55 {
56 }
57
58 GLWidget::~GLWidget()
59 {
60 }
61
62 void GLWidget::shutdown()
63 {
64         if (resource_pool != nullptr) {
65                 makeCurrent();
66                 resource_pool->clean_context();
67         }
68         global_mixer->remove_frame_ready_callback(output, this);
69 }
70
71 void GLWidget::grab_white_balance(unsigned channel, unsigned x, unsigned y)
72 {
73         // Set the white balance to neutral for the grab. It's probably going to
74         // flicker a bit, but hopefully this display is not live anyway.
75         global_mixer->set_wb(output, 1.0, 1.0, 1.0);
76         global_mixer->wait_for_next_frame();
77
78         // Mark that the next paintGL() should grab the given pixel.
79         grab_x = x;
80         grab_y = y;
81         grab_output = Mixer::Output(Mixer::OUTPUT_INPUT0 + channel);
82         should_grab = true;
83
84         updateGL();
85 }
86
87 void GLWidget::initializeGL()
88 {
89         static once_flag flag;
90         call_once(flag, [this]{
91                 global_mixer = new Mixer(QGLFormat::toSurfaceFormat(format()));
92                 global_audio_mixer = global_mixer->get_audio_mixer();
93                 global_mainwindow->mixer_created(global_mixer);
94                 global_mixer->start();
95         });
96         global_mixer->add_frame_ready_callback(output, this, [this]{
97                 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
98         });
99         global_mixer->set_name_updated_callback(output, [this](const string &name){
100                 emit name_updated(output, name);
101         });
102         if (output == Mixer::OUTPUT_LIVE) {
103                 global_mixer->set_transition_names_updated_callback(output, [this](const vector<string> &names){
104                         emit transition_names_updated(names);
105                 });
106         }
107         if (output >= Mixer::OUTPUT_INPUT0) {
108                 global_mixer->set_color_updated_callback(output, [this](const string &color){
109                         emit color_updated(output, color);
110                 });
111         }
112         setContextMenuPolicy(Qt::CustomContextMenu);
113         connect(this, &QWidget::customContextMenuRequested, bind(&GLWidget::show_context_menu, this, _1));
114
115         glDisable(GL_BLEND);
116         glDisable(GL_DEPTH_TEST);
117         glDepthMask(GL_FALSE);
118 }
119
120 void GLWidget::resizeGL(int width, int height)
121 {
122         current_width = width;
123         current_height = height;
124         glViewport(0, 0, width, height);
125 }
126
127 void GLWidget::paintGL()
128 {
129         Mixer::DisplayFrame frame;
130         if (!global_mixer->get_display_frame(output, &frame)) {
131                 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
132                 check_error();
133                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
134                 check_error();
135                 return;
136         }
137
138         check_error();
139         glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
140         check_error();
141         frame.setup_chain();
142         check_error();
143         glDisable(GL_FRAMEBUFFER_SRGB);
144         check_error();
145         frame.chain->render_to_fbo(0, current_width, current_height);
146         check_error();
147
148         if (resource_pool == nullptr) {
149                 resource_pool = frame.chain->get_resource_pool();
150         } else {
151                 assert(resource_pool == frame.chain->get_resource_pool());
152         }
153
154         if (should_grab) {
155                 GLfloat reference_color[4];
156                 glReadPixels(grab_x, current_height - grab_y - 1, 1, 1, GL_BGRA, GL_FLOAT, reference_color);
157
158                 double r = srgb_to_linear(reference_color[2]);
159                 double g = srgb_to_linear(reference_color[1]);
160                 double b = srgb_to_linear(reference_color[0]);
161                 global_mixer->set_wb(grab_output, r, g, b);
162                 should_grab = false;
163         }
164 }
165
166 void GLWidget::mousePressEvent(QMouseEvent *event)
167 {
168         emit clicked();
169 }
170
171 void GLWidget::show_context_menu(const QPoint &pos)
172 {
173         if (output == Mixer::OUTPUT_LIVE) {
174                 show_live_context_menu(pos);
175         }
176         if (output >= Mixer::OUTPUT_INPUT0) {
177                 int signal_num = global_mixer->map_channel_to_signal(output);
178                 if (signal_num != -1) {
179                         show_preview_context_menu(signal_num, pos);
180                 }
181         }
182 }
183
184 void GLWidget::show_live_context_menu(const QPoint &pos)
185 {
186         QPoint global_pos = mapToGlobal(pos);
187
188         QMenu menu;
189
190         // Add a submenu for selecting output card, with an action for each card.
191         QMenu card_submenu;
192         fill_hdmi_sdi_output_device_menu(&card_submenu);
193         card_submenu.setTitle("HDMI/SDI output device");
194         menu.addMenu(&card_submenu);
195
196         // Add a submenu for choosing the output resolution. Since this is
197         // card-dependent, it is disabled if we haven't chosen a card
198         // (but it's still there so that the user will know it exists).
199         QMenu resolution_submenu;
200         fill_hdmi_sdi_output_resolution_menu(&resolution_submenu);
201         resolution_submenu.setTitle("HDMI/SDI output resolution");
202         menu.addMenu(&resolution_submenu);
203
204         // Show the menu; if there's an action selected, it will deal with it itself.
205         menu.exec(global_pos);
206 }
207
208 void GLWidget::show_preview_context_menu(unsigned signal_num, const QPoint &pos)
209 {
210         QPoint global_pos = mapToGlobal(pos);
211
212         QMenu menu;
213
214         // Add a submenu for selecting input card, with an action for each card.
215         QMenu card_submenu;
216         QActionGroup card_group(&card_submenu);
217
218         QMenu interpretation_submenu;
219         QActionGroup interpretation_group(&interpretation_submenu);
220
221         QMenu video_input_submenu;
222         QActionGroup video_input_group(&video_input_submenu);
223
224         QMenu audio_input_submenu;
225         QActionGroup audio_input_group(&audio_input_submenu);
226
227         QMenu mode_submenu;
228         QActionGroup mode_group(&mode_submenu);
229
230         unsigned current_card = global_mixer->map_signal_to_card(signal_num);
231         bool is_ffmpeg = global_mixer->card_is_ffmpeg(current_card);
232
233         if (!is_ffmpeg) {  // FFmpeg inputs are not connected to any card; they're locked to a given input and have a given Y'CbCr interpretatio and have a given Y'CbCr interpretation.
234                 for (unsigned card_index = 0; card_index < MAX_VIDEO_CARDS; ++card_index) {
235                         if (!global_mixer->card_is_active(card_index)) continue;
236                         if (global_mixer->card_is_cef(card_index) || global_mixer->card_is_ffmpeg(card_index)) continue;
237                         QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
238                         QAction *action = new QAction(description, &card_group);
239                         action->setCheckable(true);
240                         if (current_card == card_index) {
241                                 action->setChecked(true);
242                         }
243                         action->setData(QList<QVariant>{"card", card_index});
244                         card_submenu.addAction(action);
245                 }
246
247                 card_submenu.setTitle("Input source");
248                 menu.addMenu(&card_submenu);
249
250                 // Note that this setting depends on which card is active.
251
252                 YCbCrInterpretation current_interpretation = global_mixer->get_input_ycbcr_interpretation(current_card);
253                 {
254                         QAction *action = new QAction("Auto", &interpretation_group);
255                         action->setCheckable(true);
256                         if (current_interpretation.ycbcr_coefficients_auto) {
257                                 action->setChecked(true);
258                         }
259                         action->setData(QList<QVariant>{"interpretation", true, YCBCR_REC_709, false});
260                         interpretation_submenu.addAction(action);
261                 }
262                 for (YCbCrLumaCoefficients ycbcr_coefficients : { YCBCR_REC_709, YCBCR_REC_601 }) {
263                         for (bool full_range : { false, true }) {
264                                 std::string description;
265                                 if (ycbcr_coefficients == YCBCR_REC_709) {
266                                         description = "Rec. 709 (HD)";
267                                 } else {
268                                         description = "Rec. 601 (SD)";
269                                 }
270                                 if (full_range) {
271                                         description += ", full range (nonstandard)";
272                                 }
273                                 QAction *action = new QAction(QString::fromStdString(description), &interpretation_group);
274                                 action->setCheckable(true);
275                                 if (!current_interpretation.ycbcr_coefficients_auto &&
276                                     ycbcr_coefficients == current_interpretation.ycbcr_coefficients &&
277                                     full_range == current_interpretation.full_range) {
278                                         action->setChecked(true);
279                                 }
280                                 action->setData(QList<QVariant>{"interpretation", false, ycbcr_coefficients, full_range});
281                                 interpretation_submenu.addAction(action);
282                         }
283                 }
284
285                 interpretation_submenu.setTitle("Input interpretation");
286                 menu.addMenu(&interpretation_submenu);
287         }
288
289         // --- The choices in the next few options depend a lot on which card is active ---
290
291         bool has_auto_mode = false;
292         QAction *change_url_action = nullptr;
293         if (is_ffmpeg) {
294                 // Add a menu to change the source URL if we're an FFmpeg card.
295                 // (The theme can still override.)
296                 change_url_action = new QAction("Change source filename/URL…", &menu);
297                 menu.addAction(change_url_action);
298         } else {
299                 // Add a submenu for selecting video input, with an action for each input.
300                 std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
301                 uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
302                 for (const auto &mode : video_inputs) {
303                         QString description(QString::fromStdString(mode.second));
304                         QAction *action = new QAction(description, &video_input_group);
305                         action->setCheckable(true);
306                         if (mode.first == current_video_input) {
307                                 action->setChecked(true);
308                         }
309                         action->setData(QList<QVariant>{"video_input", mode.first});
310                         video_input_submenu.addAction(action);
311                 }
312
313                 video_input_submenu.setTitle("Video input");
314                 menu.addMenu(&video_input_submenu);
315
316                 // The same for audio input.
317                 std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
318                 uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
319                 for (const auto &mode : audio_inputs) {
320                         QString description(QString::fromStdString(mode.second));
321                         QAction *action = new QAction(description, &audio_input_group);
322                         action->setCheckable(true);
323                         if (mode.first == current_audio_input) {
324                                 action->setChecked(true);
325                         }
326                         action->setData(QList<QVariant>{"audio_input", mode.first});
327                         audio_input_submenu.addAction(action);
328                 }
329
330                 audio_input_submenu.setTitle("Audio input");
331                 menu.addMenu(&audio_input_submenu);
332
333                 // The same for resolution.
334                 std::map<uint32_t, bmusb::VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
335                 uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
336                 for (const auto &mode : video_modes) {
337                         QString description(QString::fromStdString(mode.second.name));
338                         QAction *action = new QAction(description, &mode_group);
339                         action->setCheckable(true);
340                         if (mode.first == current_video_mode) {
341                                 action->setChecked(true);
342                         }
343                         action->setData(QList<QVariant>{"video_mode", mode.first});
344                         mode_submenu.addAction(action);
345
346                         // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
347                         if (mode.first == 0) {
348                                 has_auto_mode = true;
349                         }
350                 }
351
352                 // Add a “scan” menu if there's no “auto” mode.
353                 if (!has_auto_mode) {
354                         QAction *action = new QAction("Scan", &mode_group);
355                         action->setData(QList<QVariant>{"video_mode", 0});
356                         mode_submenu.addSeparator();
357                         mode_submenu.addAction(action);
358                 }
359
360                 mode_submenu.setTitle("Input mode");
361                 menu.addMenu(&mode_submenu);
362         }
363
364         // --- End of card-dependent choices ---
365
366         // Add an audio source selector.
367         QAction *audio_source_action = nullptr;
368         if (global_audio_mixer->get_mapping_mode() == AudioMixer::MappingMode::SIMPLE) {
369                 audio_source_action = new QAction("Use as audio source", &menu);
370                 audio_source_action->setCheckable(true);
371                 if (global_audio_mixer->get_simple_input() == current_card) {
372                         audio_source_action->setChecked(true);
373                         audio_source_action->setEnabled(false);
374                 }
375                 menu.addAction(audio_source_action);
376         }
377
378         // And a master clock selector.
379         QAction *master_clock_action = new QAction("Use as master clock", &menu);
380         master_clock_action->setCheckable(true);
381         if (global_mixer->get_output_card_index() != -1) {
382                 master_clock_action->setChecked(false);
383                 master_clock_action->setEnabled(false);
384         } else if (global_mixer->get_master_clock() == signal_num) {
385                 master_clock_action->setChecked(true);
386                 master_clock_action->setEnabled(false);
387         }
388         menu.addAction(master_clock_action);
389
390         // Show the menu and look at the result.
391         QAction *selected_item = menu.exec(global_pos);
392         if (audio_source_action != nullptr && selected_item == audio_source_action) {
393                 global_audio_mixer->set_simple_input(current_card);
394         } else if (change_url_action != nullptr && selected_item == change_url_action) {
395                 // NOTE: We can't use “this” as parent, since the dialog would inherit our style sheet.
396                 bool ok;
397                 const string url = global_mixer->get_ffmpeg_filename(current_card);
398                 QString new_url = QInputDialog::getText(window(), tr("Change URL"),
399                         tr("Enter new filename/URL for the given video input:"), QLineEdit::Normal,
400                                 QString::fromStdString(url), &ok);
401                 // FIXME prefill the input
402                 if (ok) {
403                         global_mixer->set_ffmpeg_filename(current_card, new_url.toStdString());
404                 }
405         } else if (selected_item == master_clock_action) {
406                 global_mixer->set_master_clock(signal_num);
407         } else if (selected_item != nullptr) {
408                 QList<QVariant> selected = selected_item->data().toList();
409                 if (selected[0].toString() == "video_mode") {
410                         uint32_t mode = selected[1].toUInt(nullptr);
411                         if (mode == 0 && !has_auto_mode) {
412                                 global_mixer->start_mode_scanning(current_card);
413                         } else {
414                                 global_mixer->set_video_mode(current_card, mode);
415                         }
416                 } else if (selected[0].toString() == "video_input") {
417                         uint32_t input = selected[1].toUInt(nullptr);
418                         global_mixer->set_video_input(current_card, input);
419                 } else if (selected[0].toString() == "audio_input") {
420                         uint32_t input = selected[1].toUInt(nullptr);
421                         global_mixer->set_audio_input(current_card, input);
422                 } else if (selected[0].toString() == "card") {
423                         unsigned card_index = selected[1].toUInt(nullptr);
424                         global_mixer->set_signal_mapping(signal_num, card_index);
425                 } else if (selected[0].toString() == "interpretation") {
426                         YCbCrInterpretation interpretation;
427                         interpretation.ycbcr_coefficients_auto = selected[1].toBool();
428                         interpretation.ycbcr_coefficients = YCbCrLumaCoefficients(selected[2].toUInt(nullptr));
429                         interpretation.full_range = selected[3].toBool();
430                         global_mixer->set_input_ycbcr_interpretation(current_card, interpretation);
431                 } else {
432                         assert(false);
433                 }
434         }
435 }