Skip to content

Commit

Permalink
Merge pull request #821 from TomHarte/QtThreading
Browse files Browse the repository at this point in the history
Qt issue mega ticket
  • Loading branch information
TomHarte authored Jul 28, 2020
2 parents a2db6dd + 3db4a8c commit b1e0629
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
16 changes: 10 additions & 6 deletions ClockReceiver/VSyncPredictor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ class VSyncPredictor {
frame_duration_ = Nanos(1'000'000'000.0f / rate);
}

/*!
@returns The time this class currently believes a whole frame occupies.
*/
Time::Nanos frame_duration() {
return frame_duration_;
}

/*!
Adds a record of how much jitter was experienced in scheduling; these values will be
factored into the @c suggested_draw_time if supplied.
Expand All @@ -87,15 +94,13 @@ class VSyncPredictor {
(if those figures are being supplied).
*/
Nanos suggested_draw_time() {
const auto mean = redraw_period_.mean() - timer_jitter_.mean() - vsync_jitter_.mean();
const auto mean = redraw_period_.mean() + timer_jitter_.mean() + vsync_jitter_.mean();
const auto variance = redraw_period_.variance() + timer_jitter_.variance() + vsync_jitter_.variance();

// Permit three standard deviations from the mean, to cover 99.9% of cases.
const auto period = mean - Nanos(3.0f * sqrt(float(variance)));

assert(abs(period) < 10'000'000'000);
const auto period = mean + Nanos(3.0f * sqrt(float(variance)));

return last_vsync_ + period;
return last_vsync_ + frame_duration_ - period;
}

private:
Expand All @@ -109,7 +114,6 @@ class VSyncPredictor {
}

void post(Time::Nanos value) {
assert(abs(value) < 10'000'000'000); // 10 seconds is a very liberal maximum.
sum_ -= history_[write_pointer_];
sum_ += value;
history_[write_pointer_] = value;
Expand Down
37 changes: 30 additions & 7 deletions OSBindings/Qt/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {

MainWindow::MainWindow(const QString &fileName) {
init();
launchFile(fileName);
if(!launchFile(fileName)) {
setUIPhase(UIPhase::SelectingMachine);
}
}

void MainWindow::deleteMachine() {
Expand Down Expand Up @@ -210,11 +212,17 @@ void MainWindow::insertFile(const QString &fileName) {
mediaTarget->insert_media(media);
}

void MainWindow::launchFile(const QString &fileName) {
bool MainWindow::launchFile(const QString &fileName) {
targets = Analyser::Static::GetTargets(fileName.toStdString());
if(!targets.empty()) {
openFileName = QFileInfo(fileName).fileName();
launchMachine();
return true;
} else {
QMessageBox msgBox;
msgBox.setText("Unable to open file: " + fileName);
msgBox.exec();
return false;
}
}

Expand Down Expand Up @@ -707,6 +715,7 @@ void MainWindow::dropEvent(QDropEvent* event) {
bool foundROM = false;
const auto appDataLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation).toStdString();

QString unusedRoms;
for(const auto &url: event->mimeData()->urls()) {
const char *const name = url.toLocalFile().toUtf8();
FILE *const file = fopen(name, "rb");
Expand All @@ -716,6 +725,7 @@ void MainWindow::dropEvent(QDropEvent* event) {
CRC::CRC32 generator;
const uint32_t crc = generator.compute_crc(*contents);

bool wasUsed = false;
for(const auto &rom: missingRoms) {
if(std::find(rom.crc32s.begin(), rom.crc32s.end(), crc) != rom.crc32s.end()) {
foundROM = true;
Expand All @@ -731,10 +741,22 @@ void MainWindow::dropEvent(QDropEvent* event) {
FILE *const target = fopen(destination.c_str(), "wb");
fwrite(contents->data(), 1, contents->size(), target);
fclose(target);

wasUsed = true;
}
}

if(!wasUsed) {
if(!unusedRoms.isEmpty()) unusedRoms += ", ";
unusedRoms += url.fileName();
}
}

if(!unusedRoms.isEmpty()) {
QMessageBox msgBox;
msgBox.setText("Couldn't identify ROMs: " + unusedRoms);
msgBox.exec();
}
if(foundROM) launchMachine();
} break;
}
Expand Down Expand Up @@ -1338,24 +1360,25 @@ void MainWindow::addActivityObserver() {
}

void MainWindow::register_led(const std::string &name) {
std::lock_guard guard(ledStatusesLock);
ledStatuses[name] = false;
updateStatusBarText();
QMetaObject::invokeMethod(this, "updateStatusBarText");
}

void MainWindow::set_led_status(const std::string &name, bool isLit) {
std::lock_guard guard(ledStatusesLock);
ledStatuses[name] = isLit;
updateStatusBarText(); // Assumption here: Qt's attempt at automatic thread confinement will work here.
QMetaObject::invokeMethod(this, "updateStatusBarText");
}

void MainWindow::updateStatusBarText() {
QString fullText;
bool isFirst = true;
std::lock_guard guard(ledStatusesLock);
for(const auto &pair: ledStatuses) {
if(!isFirst) fullText += " | ";
if(!fullText.isEmpty()) fullText += " | ";
fullText += QString::fromStdString(pair.first);
fullText += " ";
fullText += pair.second ? "" : "";
isFirst = false;
}
statusBar()->showMessage(fullText);
}
8 changes: 6 additions & 2 deletions OSBindings/Qt/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QMainWindow>

#include <memory>
#include <mutex>
#include <optional>

#include "audiobuffer.h"
Expand Down Expand Up @@ -80,6 +81,7 @@ class MainWindow : public QMainWindow, public Outputs::Speaker::Speaker::Delegat

private slots:
void startMachine();
void updateStatusBarText();

private:
void start_appleII();
Expand All @@ -100,7 +102,7 @@ class MainWindow : public QMainWindow, public Outputs::Speaker::Speaker::Delegat
QAction *insertAction = nullptr;
void insertFile(const QString &fileName);

void launchFile(const QString &fileName);
bool launchFile(const QString &fileName);
void launchTarget(std::unique_ptr<Analyser::Static::Target> &&);

void restoreSelections();
Expand Down Expand Up @@ -144,9 +146,11 @@ class MainWindow : public QMainWindow, public Outputs::Speaker::Speaker::Delegat

void register_led(const std::string &) override;
void set_led_status(const std::string &, bool) override;

std::recursive_mutex ledStatusesLock;
std::map<std::string, bool> ledStatuses;

void addActivityObserver();
void updateStatusBarText();
};

#endif // MAINWINDOW_H
2 changes: 1 addition & 1 deletion OSBindings/Qt/scantargetwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void ScanTargetWidget::vsync() {
const auto time_now = Time::nanos_now();
requestedRedrawTime = vsyncPredictor.suggested_draw_time();
const auto delay_time = (requestedRedrawTime - time_now) / 1'000'000;
if(delay_time > 0) {
if(delay_time > 0 && delay_time < vsyncPredictor.frame_duration()) {
QTimer::singleShot(delay_time, this, SLOT(repaint()));
} else {
requestedRedrawTime = 0;
Expand Down

0 comments on commit b1e0629

Please sign in to comment.