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