Debian Codegraph @

Source/Core/DolphinQt/FIFO/FIFOPlayerWindow.cpp

raw symbols SCIP
1 // Copyright 2017 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "DolphinQt/FIFO/FIFOPlayerWindow.h"
5
6 #include <QCheckBox>
7 #include <QDialogButtonBox>
8 #include <QEvent>
9 #include <QGroupBox>
10 #include <QHBoxLayout>
11 #include <QIcon>
12 #include <QKeyEvent>
13 #include <QKeySequence>
14 #include <QLabel>
15 #include <QPushButton>
16 #include <QSpinBox>
17 #include <QTabWidget>
18 #include <QVBoxLayout>
19
20 #include "Core/Core.h"
21 #include "Core/FifoPlayer/FifoDataFile.h"
22 #include "Core/FifoPlayer/FifoPlayer.h"
23 #include "Core/FifoPlayer/FifoRecorder.h"
24 #include "Core/System.h"
25
26 #include "DolphinQt/Config/ToolTipControls/ToolTipCheckBox.h"
27 #include "DolphinQt/FIFO/FIFOAnalyzer.h"
28 #include "DolphinQt/QtUtils/DolphinFileDialog.h"
29 #include "DolphinQt/QtUtils/ModalMessageBox.h"
30 #include "DolphinQt/QtUtils/QueueOnObject.h"
31 #include "DolphinQt/Resources.h"
32 #include "DolphinQt/Settings.h"
33
34 FIFOPlayerWindow::FIFOPlayerWindow(FifoPlayer& fifo_player, FifoRecorder& fifo_recorder,
35 QWidget* parent)
36 : QWidget(parent), m_fifo_player(fifo_player), m_fifo_recorder(fifo_recorder)
37 {
38 setWindowTitle(tr("FIFO Player"));
39 setWindowIcon(Resources::GetAppIcon());
40
41 CreateWidgets();
42 LoadSettings();
43 ConnectWidgets();
44 AddDescriptions();
45
46 UpdateInfo();
47
48 UpdateControls();
49
50 m_fifo_player.SetFileLoadedCallback(
51 [this] { QueueOnObject(this, &FIFOPlayerWindow::OnFIFOLoaded); });
52 m_fifo_player.SetFrameWrittenCallback([this] {
53 QueueOnObject(this, [this] {
54 UpdateInfo();
55 UpdateControls();
56 });
57 });
58
59 connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, [this](Core::State state) {
60 // We don't want to trigger OnEmulationStarted when going from Paused to Running,
61 // and nothing in UpdateControls treats Paused and Running differently
62 if (state == Core::State::Paused)
63 state = Core::State::Running;
64
65 // Skip redundant updates
66 if (state == m_emu_state)
67 return;
68
69 UpdateControls();
70
71 if (state == Core::State::Running)
72 OnEmulationStarted();
73 else if (state == Core::State::Uninitialized)
74 OnEmulationStopped();
75
76 m_emu_state = state;
77 });
78
79 installEventFilter(this);
80 }
81
82 FIFOPlayerWindow::~FIFOPlayerWindow()
83 {
84 Settings::GetQSettings().setValue(QStringLiteral("fifoplayerwindow/geometry"), saveGeometry());
85
86 m_fifo_player.SetFileLoadedCallback({});
87 m_fifo_player.SetFrameWrittenCallback({});
88 }
89
90 void FIFOPlayerWindow::CreateWidgets()
91 {
92 auto* layout = new QVBoxLayout;
93
94 // Info
95 auto* info_group = new QGroupBox(tr("File Info"));
96 auto* info_layout = new QHBoxLayout;
97
98 m_info_label = new QLabel;
99 info_layout->addWidget(m_info_label);
100 info_group->setLayout(info_layout);
101
102 m_info_label->setFixedHeight(QFontMetrics(font()).lineSpacing() * 3);
103
104 // Object Range
105 auto* object_range_group = new QGroupBox(tr("Object Range"));
106 auto* object_range_layout = new QHBoxLayout;
107
108 m_object_range_from = new QSpinBox;
109 m_object_range_from_label = new QLabel(tr("From:"));
110 m_object_range_to = new QSpinBox;
111 m_object_range_to_label = new QLabel(tr("To:"));
112
113 object_range_layout->addWidget(m_object_range_from_label);
114 object_range_layout->addWidget(m_object_range_from);
115 object_range_layout->addWidget(m_object_range_to_label);
116 object_range_layout->addWidget(m_object_range_to);
117 object_range_group->setLayout(object_range_layout);
118
119 // Frame Range
120 auto* frame_range_group = new QGroupBox(tr("Frame Range"));
121 auto* frame_range_layout = new QHBoxLayout;
122
123 m_frame_range_from = new QSpinBox;
124 m_frame_range_from_label = new QLabel(tr("From:"));
125 m_frame_range_to = new QSpinBox;
126 m_frame_range_to_label = new QLabel(tr("To:"));
127
128 frame_range_layout->addWidget(m_frame_range_from_label);
129 frame_range_layout->addWidget(m_frame_range_from);
130 frame_range_layout->addWidget(m_frame_range_to_label);
131 frame_range_layout->addWidget(m_frame_range_to);
132 frame_range_group->setLayout(frame_range_layout);
133
134 // Playback Options
135 auto* playback_group = new QGroupBox(tr("Playback Options"));
136 auto* playback_layout = new QGridLayout;
137 m_early_memory_updates = new ToolTipCheckBox(tr("Early Memory Updates"));
138 m_loop = new ToolTipCheckBox(tr("Loop"));
139
140 playback_layout->addWidget(object_range_group, 0, 0);
141 playback_layout->addWidget(frame_range_group, 0, 1);
142 playback_layout->addWidget(m_early_memory_updates, 1, 0);
143 playback_layout->addWidget(m_loop, 1, 1);
144 playback_group->setLayout(playback_layout);
145
146 // Recording Options
147 auto* recording_group = new QGroupBox(tr("Recording Options"));
148 auto* recording_layout = new QHBoxLayout;
149 m_frame_record_count = new QSpinBox;
150 m_frame_record_count_label = new QLabel(tr("Frames to Record:"));
151
152 m_frame_record_count->setMinimum(1);
153 m_frame_record_count->setMaximum(3600);
154 m_frame_record_count->setValue(3);
155
156 recording_layout->addWidget(m_frame_record_count_label);
157 recording_layout->addWidget(m_frame_record_count);
158 recording_group->setLayout(recording_layout);
159
160 m_button_box = new QDialogButtonBox(QDialogButtonBox::Close);
161
162 // Action Buttons
163 m_load = m_button_box->addButton(tr("Load..."), QDialogButtonBox::ActionRole);
164 m_save = m_button_box->addButton(tr("Save..."), QDialogButtonBox::ActionRole);
165 m_record = m_button_box->addButton(tr("Record"), QDialogButtonBox::ActionRole);
166 m_stop = m_button_box->addButton(tr("Stop"), QDialogButtonBox::ActionRole);
167
168 layout->addWidget(info_group);
169 layout->addWidget(playback_group);
170 layout->addWidget(recording_group);
171 layout->addStretch();
172 layout->addWidget(m_button_box);
173
174 m_main_widget = new QWidget(this);
175 m_main_widget->setLayout(layout);
176
177 m_tab_widget = new QTabWidget(this);
178
179 m_analyzer = new FIFOAnalyzer(m_fifo_player);
180
181 m_tab_widget->addTab(m_main_widget, tr("Play / Record"));
182 m_tab_widget->addTab(m_analyzer, tr("Analyze"));
183
184 auto* tab_layout = new QVBoxLayout;
185 tab_layout->addWidget(m_tab_widget);
186
187 setLayout(tab_layout);
188 }
189
190 void FIFOPlayerWindow::LoadSettings()
191 {
192 restoreGeometry(
193 Settings::GetQSettings().value(QStringLiteral("fifoplayerwindow/geometry")).toByteArray());
194
195 m_early_memory_updates->setChecked(Config::Get(Config::MAIN_FIFOPLAYER_EARLY_MEMORY_UPDATES));
196 m_loop->setChecked(Config::Get(Config::MAIN_FIFOPLAYER_LOOP_REPLAY));
197 }
198
199 void FIFOPlayerWindow::ConnectWidgets()
200 {
201 connect(m_load, &QPushButton::clicked, this, &FIFOPlayerWindow::LoadRecording);
202 connect(m_save, &QPushButton::clicked, this, &FIFOPlayerWindow::SaveRecording);
203 connect(m_record, &QPushButton::clicked, this, &FIFOPlayerWindow::StartRecording);
204 connect(m_stop, &QPushButton::clicked, this, &FIFOPlayerWindow::StopRecording);
205 connect(m_button_box, &QDialogButtonBox::rejected, this, &FIFOPlayerWindow::hide);
206 connect(m_early_memory_updates, &QCheckBox::toggled, this, &FIFOPlayerWindow::OnConfigChanged);
207 connect(m_loop, &QCheckBox::toggled, this, &FIFOPlayerWindow::OnConfigChanged);
208
209 connect(m_frame_range_from, &QSpinBox::valueChanged, this, &FIFOPlayerWindow::OnLimitsChanged);
210 connect(m_frame_range_to, &QSpinBox::valueChanged, this, &FIFOPlayerWindow::OnLimitsChanged);
211
212 connect(m_object_range_from, &QSpinBox::valueChanged, this, &FIFOPlayerWindow::OnLimitsChanged);
213 connect(m_object_range_to, &QSpinBox::valueChanged, this, &FIFOPlayerWindow::OnLimitsChanged);
214 }
215
216 void FIFOPlayerWindow::AddDescriptions()
217 {
218 static const char TR_MEMORY_UPDATES_DESCRIPTION[] = QT_TR_NOOP(
219 "If enabled, then all memory updates happen at once before the first frame.<br><br>"
220 "Causes issues with many fifologs, but can be useful for testing.<br><br>"
221 "<dolphin_emphasis>If unsure, leave this unchecked.</dolphin_emphasis>");
222 static const char TR_LOOP_DESCRIPTION[] =
223 QT_TR_NOOP("If unchecked, then playback of the fifolog stops after the final frame.<br><br>"
224 "This is generally only useful when a frame-dumping option is enabled.<br><br>"
225 "<dolphin_emphasis>If unsure, leave this checked.</dolphin_emphasis>");
226
227 m_early_memory_updates->SetDescription(tr(TR_MEMORY_UPDATES_DESCRIPTION));
228 m_loop->SetDescription(tr(TR_LOOP_DESCRIPTION));
229 }
230
231 void FIFOPlayerWindow::LoadRecording()
232 {
233 QString path = DolphinFileDialog::getOpenFileName(this, tr("Open FIFO Log"), QString(),
234 tr("Dolphin FIFO Log (*.dff)"));
235
236 if (path.isEmpty())
237 return;
238
239 emit LoadFIFORequested(path);
240 }
241
242 void FIFOPlayerWindow::SaveRecording()
243 {
244 QString path = DolphinFileDialog::getSaveFileName(this, tr("Save FIFO Log"), QString(),
245 tr("Dolphin FIFO Log (*.dff)"));
246
247 if (path.isEmpty())
248 return;
249
250 FifoDataFile* file = m_fifo_recorder.GetRecordedFile();
251
252 bool result = file->Save(path.toStdString());
253
254 if (!result)
255 {
256 ModalMessageBox::critical(this, tr("Error"), tr("Failed to save FIFO log."));
257 }
258 }
259
260 void FIFOPlayerWindow::StartRecording()
261 {
262 // Start recording
263 m_fifo_recorder.StartRecording(m_frame_record_count->value(),
264 [this] { QueueOnObject(this, [this] { OnRecordingDone(); }); });
265
266 UpdateControls();
267
268 UpdateInfo();
269 }
270
271 void FIFOPlayerWindow::StopRecording()
272 {
273 m_fifo_recorder.StopRecording();
274
275 UpdateControls();
276 UpdateInfo();
277 }
278
279 void FIFOPlayerWindow::OnEmulationStarted()
280 {
281 if (m_fifo_player.GetFile())
282 OnFIFOLoaded();
283 }
284
285 void FIFOPlayerWindow::OnEmulationStopped()
286 {
287 // If we have previously been recording, stop now.
288 if (m_fifo_recorder.IsRecording())
289 StopRecording();
290
291 // When emulation stops, switch away from the analyzer tab, as it no longer shows anything useful
292 m_tab_widget->setCurrentWidget(m_main_widget);
293 m_analyzer->Update();
294 }
295
296 void FIFOPlayerWindow::OnRecordingDone()
297 {
298 UpdateInfo();
299 UpdateControls();
300 }
301
302 void FIFOPlayerWindow::UpdateInfo()
303 {
304 if (m_fifo_player.IsPlaying())
305 {
306 FifoDataFile* file = m_fifo_player.GetFile();
307 m_info_label->setText(tr("%1 frame(s)\n%2 object(s)\nCurrent Frame: %3")
308 .arg(QString::number(file->GetFrameCount()),
309 QString::number(m_fifo_player.GetCurrentFrameObjectCount()),
310 QString::number(m_fifo_player.GetCurrentFrameNum())));
311 return;
312 }
313
314 if (m_fifo_recorder.IsRecordingDone())
315 {
316 FifoDataFile* file = m_fifo_recorder.GetRecordedFile();
317 size_t fifo_bytes = 0;
318 size_t mem_bytes = 0;
319
320 for (u32 i = 0; i < file->GetFrameCount(); ++i)
321 {
322 fifo_bytes += file->GetFrame(i).fifoData.size();
323 for (const auto& mem_update : file->GetFrame(i).memoryUpdates)
324 mem_bytes += mem_update.data.size();
325 }
326
327 m_info_label->setText(tr("%1 FIFO bytes\n%2 memory bytes\n%3 frames")
328 .arg(QString::number(fifo_bytes), QString::number(mem_bytes),
329 QString::number(file->GetFrameCount())));
330 return;
331 }
332
333 if (Core::IsRunning(Core::System::GetInstance()) && m_fifo_recorder.IsRecording())
334 {
335 m_info_label->setText(tr("Recording..."));
336 return;
337 }
338
339 m_info_label->setText(tr("No file loaded / recorded."));
340 }
341
342 void FIFOPlayerWindow::OnFIFOLoaded()
343 {
344 FifoDataFile* file = m_fifo_player.GetFile();
345
346 auto object_count = m_fifo_player.GetMaxObjectCount();
347 auto frame_count = file->GetFrameCount();
348
349 m_frame_range_to->setMaximum(frame_count - 1);
350 m_object_range_to->setMaximum(object_count - 1);
351
352 m_frame_range_from->setValue(0);
353 m_object_range_from->setValue(0);
354 m_frame_range_to->setValue(frame_count - 1);
355 m_object_range_to->setValue(object_count - 1);
356
357 UpdateInfo();
358 UpdateLimits();
359 UpdateControls();
360
361 m_analyzer->Update();
362 }
363
364 void FIFOPlayerWindow::OnConfigChanged()
365 {
366 Config::SetBase(Config::MAIN_FIFOPLAYER_EARLY_MEMORY_UPDATES,
367 m_early_memory_updates->isChecked());
368 Config::SetBase(Config::MAIN_FIFOPLAYER_LOOP_REPLAY, m_loop->isChecked());
369 }
370
371 void FIFOPlayerWindow::OnLimitsChanged()
372 {
373 FifoPlayer& player = m_fifo_player;
374
375 player.SetFrameRangeStart(m_frame_range_from->value());
376 player.SetFrameRangeEnd(m_frame_range_to->value());
377 player.SetObjectRangeStart(m_object_range_from->value());
378 player.SetObjectRangeEnd(m_object_range_to->value());
379 UpdateLimits();
380 }
381
382 void FIFOPlayerWindow::UpdateLimits()
383 {
384 m_frame_range_from->setMaximum(m_frame_range_to->value());
385 m_frame_range_to->setMinimum(m_frame_range_from->value());
386 m_object_range_from->setMaximum(m_object_range_to->value());
387 m_object_range_to->setMinimum(m_object_range_from->value());
388 }
389
390 void FIFOPlayerWindow::UpdateControls()
391 {
392 Core::System& system = Core::System::GetInstance();
393 const bool core_is_uninitialized = Core::IsUninitialized(system);
394 const bool core_is_running = Core::IsRunning(system);
395 const bool is_recording = m_fifo_recorder.IsRecording();
396 const bool is_playing = m_fifo_player.IsPlaying();
397
398 m_frame_range_from->setEnabled(is_playing);
399 m_frame_range_from_label->setEnabled(is_playing);
400 m_frame_range_to->setEnabled(is_playing);
401 m_frame_range_to_label->setEnabled(is_playing);
402 m_object_range_from->setEnabled(is_playing);
403 m_object_range_from_label->setEnabled(is_playing);
404 m_object_range_to->setEnabled(is_playing);
405 m_object_range_to_label->setEnabled(is_playing);
406
407 bool enable_frame_record_count = !is_playing && !is_recording;
408
409 m_frame_record_count_label->setEnabled(enable_frame_record_count);
410 m_frame_record_count->setEnabled(enable_frame_record_count);
411
412 m_load->setEnabled(core_is_uninitialized);
413 m_record->setEnabled(core_is_running && !is_playing);
414
415 m_stop->setVisible(core_is_running && is_recording);
416 m_record->setVisible(!m_stop->isVisible());
417
418 m_save->setEnabled(m_fifo_recorder.IsRecordingDone());
419 }
420
421 bool FIFOPlayerWindow::eventFilter(QObject* object, QEvent* event)
422 {
423 // Close when escape is pressed
424 if (event->type() == QEvent::KeyPress)
425 {
426 if (static_cast<QKeyEvent*>(event)->matches(QKeySequence::Cancel))
427 hide();
428 }
429
430 return false;
431 }