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