]> git.sesse.net Git - nageru/blob - glwidget.cpp
Add red and green borders around channels to mark them as used for live and preview.
[nageru] / glwidget.cpp
1 #include <qmetatype.h>  // Needs to come before egl.h.
2 #include <qdatastream.h>  // Needs to come before egl.h.
3 #include <qtextstream.h>  // Needs to come before egl.h.
4 #include <qcursor.h>  // Needs to come before egl.h.
5 #include <qcoreevent.h>  // Needs to come before egl.h.
6 #include <qevent.h>  // Needs to come before egl.h.
7 #include <epoxy/gl.h>
8 #include <epoxy/egl.h>
9 #include <QAction>
10 #include <QMenu>
11 #include <QSurfaceFormat>
12
13 #include "glwidget.h"
14
15 #include <stdio.h>
16 #include <functional>
17 #include <mutex>
18 #include <movit/effect_chain.h>
19 #include <movit/resource_pool.h>
20
21 #include "context.h"
22 #include "flags.h"
23 #include "mainwindow.h"
24 #include "mixer.h"
25 #include "qnamespace.h"
26 #include "ref_counted_gl_sync.h"
27
28 class QMouseEvent;
29 class QWidget;
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_mainwindow->mixer_created(global_mixer);
57                 global_mixer->start();
58         });
59         global_mixer->set_frame_ready_callback(output, [this]{
60                 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
61                 emit transition_names_updated(global_mixer->get_transition_names());
62                 emit resolution_updated(output);
63                 emit color_updated(output);
64         });
65
66         if (output >= Mixer::OUTPUT_INPUT0) {
67                 int signal_num = global_mixer->get_channel_signal(output);
68                 if (signal_num != -1) {
69                         setContextMenuPolicy(Qt::CustomContextMenu);
70                         connect(this, &QWidget::customContextMenuRequested,
71                                 bind(&GLWidget::show_context_menu, this, signal_num, _1));
72                 }
73         }
74
75         glDisable(GL_BLEND);
76         glDisable(GL_DEPTH_TEST);
77         glDepthMask(GL_FALSE);
78 }
79
80 void GLWidget::resizeGL(int width, int height)
81 {
82         glViewport(0, 0, width, height);
83 }
84
85 void GLWidget::paintGL()
86 {
87         Mixer::DisplayFrame frame;
88         if (!global_mixer->get_display_frame(output, &frame)) {
89                 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
90                 check_error();
91                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
92                 check_error();
93                 return;
94         }
95
96         check_error();
97         glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
98         check_error();
99         frame.setup_chain();
100         check_error();
101         frame.chain->render_to_screen();
102         check_error();
103
104         if (resource_pool == nullptr) {
105                 resource_pool = frame.chain->get_resource_pool();
106         } else {
107                 assert(resource_pool == frame.chain->get_resource_pool());
108         }
109 }
110
111 void GLWidget::mousePressEvent(QMouseEvent *event)
112 {
113         emit clicked();
114 }
115
116 void GLWidget::show_context_menu(unsigned signal_num, const QPoint &pos)
117 {
118         QPoint global_pos = mapToGlobal(pos);
119
120         QMenu menu;
121
122         // Add a submenu for selecting input card, with an action for each card.
123         QMenu card_submenu;
124         QActionGroup card_group(&card_submenu);
125
126         unsigned num_cards = global_mixer->get_num_cards();
127         unsigned current_card = global_mixer->map_signal(signal_num);
128         for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
129                 QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
130                 QAction *action = new QAction(description, &card_group);
131                 action->setCheckable(true);
132                 if (current_card == card_index) {
133                         action->setChecked(true);
134                 }
135                 action->setData(QList<QVariant>{"card", card_index});
136                 card_submenu.addAction(action);
137         }
138
139         card_submenu.setTitle("Input source");
140         menu.addMenu(&card_submenu);
141
142         // --- The choices in the next few options depend a lot on which card is active ---
143
144         // Add a submenu for selecting video input, with an action for each input.
145         QMenu video_input_submenu;
146         QActionGroup video_input_group(&video_input_submenu);
147         std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
148         uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
149         for (const auto &mode : video_inputs) {
150                 QString description(QString::fromStdString(mode.second));
151                 QAction *action = new QAction(description, &video_input_group);
152                 action->setCheckable(true);
153                 if (mode.first == current_video_input) {
154                         action->setChecked(true);
155                 }
156                 action->setData(QList<QVariant>{"video_input", mode.first});
157                 video_input_submenu.addAction(action);
158         }
159
160         video_input_submenu.setTitle("Video input");
161         menu.addMenu(&video_input_submenu);
162
163         // The same for audio input.
164         QMenu audio_input_submenu;
165         QActionGroup audio_input_group(&audio_input_submenu);
166         std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
167         uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
168         for (const auto &mode : audio_inputs) {
169                 QString description(QString::fromStdString(mode.second));
170                 QAction *action = new QAction(description, &audio_input_group);
171                 action->setCheckable(true);
172                 if (mode.first == current_audio_input) {
173                         action->setChecked(true);
174                 }
175                 action->setData(QList<QVariant>{"audio_input", mode.first});
176                 audio_input_submenu.addAction(action);
177         }
178
179         audio_input_submenu.setTitle("Audio input");
180         menu.addMenu(&audio_input_submenu);
181
182         // The same for resolution.
183         QMenu mode_submenu;
184         QActionGroup mode_group(&mode_submenu);
185         std::map<uint32_t, VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
186         uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
187         bool has_auto_mode = false;
188         for (const auto &mode : video_modes) {
189                 QString description(QString::fromStdString(mode.second.name));
190                 QAction *action = new QAction(description, &mode_group);
191                 action->setCheckable(true);
192                 if (mode.first == current_video_mode) {
193                         action->setChecked(true);
194                 }
195                 action->setData(QList<QVariant>{"video_mode", mode.first});
196                 mode_submenu.addAction(action);
197
198                 // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
199                 if (mode.first == 0) {
200                         has_auto_mode = true;
201                 }
202         }
203
204         // Add a “scan” menu if there's no “auto” mode.
205         if (!has_auto_mode) {
206                 QAction *action = new QAction("Scan", &mode_group);
207                 action->setData(QList<QVariant>{"video_mode", 0});
208                 mode_submenu.addSeparator();
209                 mode_submenu.addAction(action);
210         }
211
212         mode_submenu.setTitle("Input mode");
213         menu.addMenu(&mode_submenu);
214
215         // --- End of card-dependent choices ---
216
217         // Add an audio source selector.
218         QAction *audio_source_action = new QAction("Use as audio source", &menu);
219         audio_source_action->setCheckable(true);
220         if (global_mixer->get_audio_source() == signal_num) {
221                 audio_source_action->setChecked(true);
222                 audio_source_action->setEnabled(false);
223         }
224         menu.addAction(audio_source_action);
225
226         // And a master clock selector.
227         QAction *master_clock_action = new QAction("Use as master clock", &menu);
228         master_clock_action->setCheckable(true);
229         if (global_mixer->get_master_clock() == signal_num) {
230                 master_clock_action->setChecked(true);
231                 master_clock_action->setEnabled(false);
232         }
233         menu.addAction(master_clock_action);
234
235         // Show the menu and look at the result.
236         QAction *selected_item = menu.exec(global_pos);
237         if (selected_item == audio_source_action) {
238                 global_mixer->set_audio_source(signal_num);
239         } else if (selected_item == master_clock_action) {
240                 global_mixer->set_master_clock(signal_num);
241         } else if (selected_item != nullptr) {
242                 QList<QVariant> selected = selected_item->data().toList();
243                 if (selected[0].toString() == "video_mode") {
244                         uint32_t mode = selected[1].toUInt(nullptr);
245                         if (mode == 0 && !has_auto_mode) {
246                                 global_mixer->start_mode_scanning(current_card);
247                         } else {
248                                 global_mixer->set_video_mode(current_card, mode);
249                         }
250                 } else if (selected[0].toString() == "video_input") {
251                         uint32_t input = selected[1].toUInt(nullptr);
252                         global_mixer->set_video_input(current_card, input);
253                 } else if (selected[0].toString() == "audio_input") {
254                         uint32_t input = selected[1].toUInt(nullptr);
255                         global_mixer->set_audio_input(current_card, input);
256                 } else if (selected[0].toString() == "card") {
257                         unsigned card_index = selected[1].toUInt(nullptr);
258                         global_mixer->set_signal_mapping(signal_num, card_index);
259                 } else {
260                         assert(false);
261                 }
262         }
263 }