]> git.sesse.net Git - nageru/blob - glwidget.cpp
Make sure we do not ask for sRGB backbuffers when rendering to screen.
[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 <QList>
12 #include <QMenu>
13 #include <QPoint>
14 #include <QVariant>
15 #include <QWidget>
16 #include <functional>
17 #include <map>
18 #include <mutex>
19 #include <utility>
20
21 #include "audio_mixer.h"
22 #include "context.h"
23 #include "context_menus.h"
24 #include "flags.h"
25 #include "mainwindow.h"
26 #include "mixer.h"
27 #include "ref_counted_gl_sync.h"
28
29 class QMouseEvent;
30
31 #undef Success
32 #include <movit/util.h>
33 #include <string>
34
35 using namespace std;
36 using namespace std::placeholders;
37
38 GLWidget::GLWidget(QWidget *parent)
39     : QGLWidget(parent, global_share_widget)
40 {
41 }
42
43 void GLWidget::clean_context()
44 {
45         if (resource_pool != nullptr) {
46                 makeCurrent();
47                 resource_pool->clean_context();
48         }
49 }
50
51 void GLWidget::initializeGL()
52 {
53         static once_flag flag;
54         call_once(flag, [this]{
55                 global_mixer = new Mixer(QGLFormat::toSurfaceFormat(format()), global_flags.num_cards);
56                 global_audio_mixer = global_mixer->get_audio_mixer();
57                 global_mainwindow->mixer_created(global_mixer);
58                 global_mixer->start();
59         });
60         global_mixer->set_frame_ready_callback(output, [this]{
61                 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
62         });
63         if (output == Mixer::OUTPUT_LIVE) {
64                 global_mixer->set_transition_names_updated_callback(output, [this](const vector<string> &names){
65                         emit transition_names_updated(names);
66                 });
67                 setContextMenuPolicy(Qt::CustomContextMenu);
68                 connect(this, &QWidget::customContextMenuRequested,
69                         bind(&GLWidget::show_live_context_menu, this, _1));
70         }
71         if (output >= Mixer::OUTPUT_INPUT0) {
72                 global_mixer->set_name_updated_callback(output, [this](const string &name){
73                         emit name_updated(output, name);
74                 });
75                 global_mixer->set_color_updated_callback(output, [this](const string &color){
76                         emit color_updated(output, color);
77                 });
78
79                 int signal_num = global_mixer->get_channel_signal(output);
80                 if (signal_num != -1) {
81                         setContextMenuPolicy(Qt::CustomContextMenu);
82                         connect(this, &QWidget::customContextMenuRequested,
83                                 bind(&GLWidget::show_preview_context_menu, this, signal_num, _1));
84                 }
85         }
86
87         glDisable(GL_BLEND);
88         glDisable(GL_DEPTH_TEST);
89         glDepthMask(GL_FALSE);
90 }
91
92 void GLWidget::resizeGL(int width, int height)
93 {
94         glViewport(0, 0, width, height);
95 }
96
97 void GLWidget::paintGL()
98 {
99         Mixer::DisplayFrame frame;
100         if (!global_mixer->get_display_frame(output, &frame)) {
101                 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
102                 check_error();
103                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
104                 check_error();
105                 return;
106         }
107
108         check_error();
109         glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
110         check_error();
111         frame.setup_chain();
112         check_error();
113         glDisable(GL_FRAMEBUFFER_SRGB);
114         check_error();
115         frame.chain->render_to_screen();
116         check_error();
117
118         if (resource_pool == nullptr) {
119                 resource_pool = frame.chain->get_resource_pool();
120         } else {
121                 assert(resource_pool == frame.chain->get_resource_pool());
122         }
123 }
124
125 void GLWidget::mousePressEvent(QMouseEvent *event)
126 {
127         emit clicked();
128 }
129
130 void GLWidget::show_live_context_menu(const QPoint &pos)
131 {
132         QPoint global_pos = mapToGlobal(pos);
133
134         QMenu menu;
135
136         // Add a submenu for selecting output card, with an action for each card.
137         QMenu card_submenu;
138         fill_hdmi_sdi_output_device_menu(&card_submenu);
139         card_submenu.setTitle("HDMI/SDI output device");
140         menu.addMenu(&card_submenu);
141
142         // Add a submenu for choosing the output resolution. Since this is
143         // card-dependent, it is disabled if we haven't chosen a card
144         // (but it's still there so that the user will know it exists).
145         QMenu resolution_submenu;
146         fill_hdmi_sdi_output_resolution_menu(&resolution_submenu);
147         resolution_submenu.setTitle("HDMI/SDI output resolution");
148         menu.addMenu(&resolution_submenu);
149
150         // Show the menu; if there's an action selected, it will deal with it itself.
151         menu.exec(global_pos);
152 }
153
154 void GLWidget::show_preview_context_menu(unsigned signal_num, const QPoint &pos)
155 {
156         QPoint global_pos = mapToGlobal(pos);
157
158         QMenu menu;
159
160         // Add a submenu for selecting input card, with an action for each card.
161         QMenu card_submenu;
162         QActionGroup card_group(&card_submenu);
163
164         unsigned num_cards = global_mixer->get_num_cards();
165         unsigned current_card = global_mixer->map_signal(signal_num);
166         for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
167                 QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
168                 QAction *action = new QAction(description, &card_group);
169                 action->setCheckable(true);
170                 if (current_card == card_index) {
171                         action->setChecked(true);
172                 }
173                 action->setData(QList<QVariant>{"card", card_index});
174                 card_submenu.addAction(action);
175         }
176
177         card_submenu.setTitle("Input source");
178         menu.addMenu(&card_submenu);
179
180         // --- The choices in the next few options depend a lot on which card is active ---
181
182         // Add a submenu for selecting video input, with an action for each input.
183         QMenu video_input_submenu;
184         QActionGroup video_input_group(&video_input_submenu);
185         std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
186         uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
187         for (const auto &mode : video_inputs) {
188                 QString description(QString::fromStdString(mode.second));
189                 QAction *action = new QAction(description, &video_input_group);
190                 action->setCheckable(true);
191                 if (mode.first == current_video_input) {
192                         action->setChecked(true);
193                 }
194                 action->setData(QList<QVariant>{"video_input", mode.first});
195                 video_input_submenu.addAction(action);
196         }
197
198         video_input_submenu.setTitle("Video input");
199         menu.addMenu(&video_input_submenu);
200
201         // The same for audio input.
202         QMenu audio_input_submenu;
203         QActionGroup audio_input_group(&audio_input_submenu);
204         std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
205         uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
206         for (const auto &mode : audio_inputs) {
207                 QString description(QString::fromStdString(mode.second));
208                 QAction *action = new QAction(description, &audio_input_group);
209                 action->setCheckable(true);
210                 if (mode.first == current_audio_input) {
211                         action->setChecked(true);
212                 }
213                 action->setData(QList<QVariant>{"audio_input", mode.first});
214                 audio_input_submenu.addAction(action);
215         }
216
217         audio_input_submenu.setTitle("Audio input");
218         menu.addMenu(&audio_input_submenu);
219
220         // The same for resolution.
221         QMenu mode_submenu;
222         QActionGroup mode_group(&mode_submenu);
223         std::map<uint32_t, bmusb::VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
224         uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
225         bool has_auto_mode = false;
226         for (const auto &mode : video_modes) {
227                 QString description(QString::fromStdString(mode.second.name));
228                 QAction *action = new QAction(description, &mode_group);
229                 action->setCheckable(true);
230                 if (mode.first == current_video_mode) {
231                         action->setChecked(true);
232                 }
233                 action->setData(QList<QVariant>{"video_mode", mode.first});
234                 mode_submenu.addAction(action);
235
236                 // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
237                 if (mode.first == 0) {
238                         has_auto_mode = true;
239                 }
240         }
241
242         // Add a “scan” menu if there's no “auto” mode.
243         if (!has_auto_mode) {
244                 QAction *action = new QAction("Scan", &mode_group);
245                 action->setData(QList<QVariant>{"video_mode", 0});
246                 mode_submenu.addSeparator();
247                 mode_submenu.addAction(action);
248         }
249
250         mode_submenu.setTitle("Input mode");
251         menu.addMenu(&mode_submenu);
252
253         // --- End of card-dependent choices ---
254
255         // Add an audio source selector.
256         QAction *audio_source_action = nullptr;
257         if (global_audio_mixer->get_mapping_mode() == AudioMixer::MappingMode::SIMPLE) {
258                 audio_source_action = new QAction("Use as audio source", &menu);
259                 audio_source_action->setCheckable(true);
260                 if (global_audio_mixer->get_simple_input() == signal_num) {
261                         audio_source_action->setChecked(true);
262                         audio_source_action->setEnabled(false);
263                 }
264                 menu.addAction(audio_source_action);
265         }
266
267         // And a master clock selector.
268         QAction *master_clock_action = new QAction("Use as master clock", &menu);
269         master_clock_action->setCheckable(true);
270         if (global_mixer->get_output_card_index() != -1) {
271                 master_clock_action->setChecked(false);
272                 master_clock_action->setEnabled(false);
273         } else if (global_mixer->get_master_clock() == signal_num) {
274                 master_clock_action->setChecked(true);
275                 master_clock_action->setEnabled(false);
276         }
277         menu.addAction(master_clock_action);
278
279         // Show the menu and look at the result.
280         QAction *selected_item = menu.exec(global_pos);
281         if (audio_source_action != nullptr && selected_item == audio_source_action) {
282                 global_audio_mixer->set_simple_input(signal_num);
283         } else if (selected_item == master_clock_action) {
284                 global_mixer->set_master_clock(signal_num);
285         } else if (selected_item != nullptr) {
286                 QList<QVariant> selected = selected_item->data().toList();
287                 if (selected[0].toString() == "video_mode") {
288                         uint32_t mode = selected[1].toUInt(nullptr);
289                         if (mode == 0 && !has_auto_mode) {
290                                 global_mixer->start_mode_scanning(current_card);
291                         } else {
292                                 global_mixer->set_video_mode(current_card, mode);
293                         }
294                 } else if (selected[0].toString() == "video_input") {
295                         uint32_t input = selected[1].toUInt(nullptr);
296                         global_mixer->set_video_input(current_card, input);
297                 } else if (selected[0].toString() == "audio_input") {
298                         uint32_t input = selected[1].toUInt(nullptr);
299                         global_mixer->set_audio_input(current_card, input);
300                 } else if (selected[0].toString() == "card") {
301                         unsigned card_index = selected[1].toUInt(nullptr);
302                         global_mixer->set_signal_mapping(signal_num, card_index);
303                 } else {
304                         assert(false);
305                 }
306         }
307 }