2020-06-04 03:39:16 +00:00
|
|
|
#include "scantargetwidget.h"
|
|
|
|
|
2020-06-05 02:39:32 +00:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QOpenGLContext>
|
2020-06-05 02:58:02 +00:00
|
|
|
#include <QTimer>
|
2020-06-05 02:39:32 +00:00
|
|
|
|
2020-06-06 23:47:35 +00:00
|
|
|
#include "../../ClockReceiver/TimeTypes.hpp"
|
|
|
|
|
2020-06-04 03:39:16 +00:00
|
|
|
ScanTargetWidget::ScanTargetWidget(QWidget *parent) : QOpenGLWidget(parent) {}
|
|
|
|
ScanTargetWidget::~ScanTargetWidget() {}
|
|
|
|
|
|
|
|
void ScanTargetWidget::initializeGL() {
|
2020-06-18 03:16:29 +00:00
|
|
|
// Retain the default background colour.
|
|
|
|
const QColor backgroundColour = palette().color(QWidget::backgroundRole());
|
|
|
|
glClearColor(backgroundColour.redF(), backgroundColour.greenF(), backgroundColour.blueF(), 1.0);
|
2020-06-05 02:39:32 +00:00
|
|
|
|
2020-06-06 23:47:35 +00:00
|
|
|
// Follow each swapped frame with an additional update.
|
2020-06-14 23:26:56 +00:00
|
|
|
connect(this, &QOpenGLWidget::frameSwapped, this, &ScanTargetWidget::vsync);
|
2020-06-04 03:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScanTargetWidget::paintGL() {
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2020-06-05 02:39:32 +00:00
|
|
|
if(scanTarget) {
|
2020-06-14 23:26:56 +00:00
|
|
|
vsync_predictor_.begin_redraw();
|
2020-06-05 02:39:32 +00:00
|
|
|
scanTarget->update(width(), height());
|
|
|
|
scanTarget->draw(width(), height());
|
2020-06-14 23:26:56 +00:00
|
|
|
vsync_predictor_.end_redraw();
|
2020-06-05 02:39:32 +00:00
|
|
|
}
|
2020-06-04 03:39:16 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 23:26:56 +00:00
|
|
|
void ScanTargetWidget::vsync() {
|
|
|
|
vsync_predictor_.announce_vsync();
|
|
|
|
|
|
|
|
const auto time_now = Time::nanos_now();
|
|
|
|
const auto delay_time = ((vsync_predictor_.suggested_draw_time() - time_now) / 1'000'000) - 5; // TODO: the extra 5 is a random guess.
|
|
|
|
if(delay_time > 0) {
|
|
|
|
QTimer::singleShot(delay_time, this, SLOT(repaint()));
|
|
|
|
} else {
|
|
|
|
repaint();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 03:39:16 +00:00
|
|
|
void ScanTargetWidget::resizeGL(int w, int h) {
|
2020-06-14 23:26:56 +00:00
|
|
|
glViewport(0,0,w,h);
|
2020-06-04 03:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Outputs::Display::OpenGL::ScanTarget *ScanTargetWidget::getScanTarget() {
|
2020-06-05 02:39:32 +00:00
|
|
|
makeCurrent();
|
|
|
|
if(!scanTarget) {
|
|
|
|
scanTarget = std::make_unique<Outputs::Display::OpenGL::ScanTarget>(defaultFramebufferObject());
|
|
|
|
}
|
2020-06-04 03:39:16 +00:00
|
|
|
return scanTarget.get();
|
|
|
|
}
|