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.
11 #include <QSurfaceFormat>
18 #include <movit/effect_chain.h>
19 #include <movit/resource_pool.h>
23 #include "mainwindow.h"
25 #include "qnamespace.h"
26 #include "ref_counted_gl_sync.h"
32 #include <movit/util.h>
36 using namespace std::placeholders;
38 GLWidget::GLWidget(QWidget *parent)
39 : QGLWidget(parent, global_share_widget)
43 void GLWidget::clean_context()
45 if (resource_pool != nullptr) {
47 resource_pool->clean_context();
51 void GLWidget::initializeGL()
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();
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);
65 if (output >= Mixer::OUTPUT_INPUT0) {
66 int signal_num = global_mixer->get_channel_signal(output);
67 if (signal_num != -1) {
68 setContextMenuPolicy(Qt::CustomContextMenu);
69 connect(this, &QWidget::customContextMenuRequested,
70 bind(&GLWidget::show_context_menu, this, signal_num, _1));
75 glDisable(GL_DEPTH_TEST);
76 glDepthMask(GL_FALSE);
79 void GLWidget::resizeGL(int width, int height)
81 glViewport(0, 0, width, height);
84 void GLWidget::paintGL()
86 Mixer::DisplayFrame frame;
87 if (!global_mixer->get_display_frame(output, &frame)) {
88 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
90 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
96 glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
100 frame.chain->render_to_screen();
103 if (resource_pool == nullptr) {
104 resource_pool = frame.chain->get_resource_pool();
106 assert(resource_pool == frame.chain->get_resource_pool());
110 void GLWidget::mousePressEvent(QMouseEvent *event)
115 void GLWidget::show_context_menu(unsigned signal_num, const QPoint &pos)
117 QPoint global_pos = mapToGlobal(pos);
121 // Add a submenu for selecting input card, with an action for each card.
123 QActionGroup card_group(&card_submenu);
125 unsigned num_cards = global_mixer->get_num_cards();
126 unsigned current_card = global_mixer->map_signal(signal_num);
127 for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
128 QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
129 QAction *action = new QAction(description, &card_group);
130 action->setCheckable(true);
131 if (current_card == card_index) {
132 action->setChecked(true);
134 action->setData(QList<QVariant>{"card", card_index});
135 card_submenu.addAction(action);
138 card_submenu.setTitle("Input source");
139 menu.addMenu(&card_submenu);
141 // Add a submenu for selecting resolution, with an action for each resolution.
142 // Note that the choice depends a lot on which card is active.
144 QActionGroup mode_group(&mode_submenu);
145 std::map<uint32_t, VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
146 uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
147 for (const auto &mode : video_modes) {
148 QString description(QString::fromStdString(mode.second.name));
149 QAction *action = new QAction(description, &mode_group);
150 action->setCheckable(true);
151 if (mode.first == current_video_mode) {
152 action->setChecked(true);
154 action->setData(QList<QVariant>{"video_mode", mode.first});
155 mode_submenu.addAction(action);
158 mode_submenu.setTitle("Input mode");
159 menu.addMenu(&mode_submenu);
161 // Add an audio source selector.
162 QAction *audio_source_action = new QAction("Use as audio source", &menu);
163 audio_source_action->setCheckable(true);
164 if (global_mixer->get_audio_source() == signal_num) {
165 audio_source_action->setChecked(true);
166 audio_source_action->setEnabled(false);
168 menu.addAction(audio_source_action);
170 // Show the menu and look at the result.
171 QAction *selected_item = menu.exec(global_pos);
172 if (selected_item == audio_source_action) {
173 global_mixer->set_audio_source(signal_num);
174 } else if (selected_item != nullptr) {
175 QList<QVariant> selected = selected_item->data().toList();
176 if (selected[0].toString() == "video_mode") {
177 uint32_t mode = selected[1].toUInt(nullptr);
178 global_mixer->set_video_mode(current_card, mode);
179 } else if (selected[0].toString() == "card") {
180 unsigned card_index = selected[1].toUInt(nullptr);
181 global_mixer->set_signal_mapping(signal_num, card_index);