Qt: Improve camera initialization

This commit is contained in:
Vicki Pfau 2019-03-05 17:34:02 -08:00
parent 7b59e620f1
commit 03aed12d28
4 changed files with 36 additions and 17 deletions

View File

@ -24,6 +24,7 @@ Misc:
- Debugger: Add breakpoint and watchpoint listing
- Qt: Add missing HEVC NVENC option (fixes mgba.io/i/1323)
- LR35902: Support PC-relative opcode decoding
- Qt: Improve camera initialization
0.7.1: (2019-02-24)
Bugfixes:

View File

@ -725,12 +725,23 @@ void InputController::setupCam() {
#ifdef BUILD_QT_MULTIMEDIA
if (!m_camera) {
m_camera = std::make_unique<QCamera>();
connect(m_camera.get(), &QCamera::statusChanged, this, &InputController::prepareCamSettings);
}
QVideoFrame::PixelFormat format(QVideoFrame::Format_RGB32);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0))
m_camera->setCaptureMode(QCamera::CaptureVideo);
m_camera->setViewfinder(&m_videoDumper);
m_camera->load();
#endif
}
#ifdef BUILD_QT_MULTIMEDIA
void InputController::prepareCamSettings(QCamera::Status status) {
if (status != QCamera::LoadedStatus || m_camera->state() == QCamera::ActiveState) {
return;
}
#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0))
QVideoFrame::PixelFormat format(QVideoFrame::Format_RGB32);
QCameraViewfinderSettings settings;
QSize size(1920, 1080);
QSize size(1280, 720);
auto cameraRes = m_camera->supportedViewfinderResolutions(settings);
for (auto& cameraSize : cameraRes) {
if (cameraSize.width() < m_image.w || cameraSize.height() < m_image.h) {
@ -741,10 +752,11 @@ void InputController::setupCam() {
}
}
settings.setResolution(size);
auto cameraFormats = m_camera->supportedViewfinderPixelFormats(settings);
auto goodFormats = m_videoDumper.supportedPixelFormats();
bool goodFormatFound = false;
for (auto& goodFormat : goodFormats) {
for (const auto& goodFormat : goodFormats) {
if (cameraFormats.contains(goodFormat)) {
settings.setPixelFormat(goodFormat);
format = goodFormat;
@ -754,20 +766,20 @@ void InputController::setupCam() {
}
if (!goodFormatFound) {
LOG(QT, WARN) << "Could not find a valid camera format!";
for (const auto& format : cameraFormats) {
LOG(QT, WARN) << "Camera supported format: " << QString::number(format);
}
}
m_camera->setViewfinderSettings(settings);
#endif
m_camera->setCaptureMode(QCamera::CaptureVideo);
m_camera->setViewfinder(&m_videoDumper);
m_camera->start();
#endif
}
#endif
void InputController::teardownCam() {
#ifdef BUILD_QT_MULTIMEDIA
if (m_camera) {
m_camera->stop();
m_camera.reset();
}
#endif
}

View File

@ -27,13 +27,12 @@
#ifdef BUILD_QT_MULTIMEDIA
#include "VideoDumper.h"
#include <QCamera>
#endif
struct mRotationSource;
struct mRumble;
class QCamera;
namespace QGBA {
class ConfigController;
@ -124,6 +123,9 @@ public slots:
void setCamImage(const QImage& image);
private slots:
#ifdef BUILD_QT_MULTIMEDIA
void prepareCamSettings(QCamera::Status);
#endif
void setupCam();
void teardownCam();

View File

@ -23,14 +23,18 @@ bool VideoDumper::present(const QVideoFrame& frame) {
QImage::Format format = QVideoFrame::imageFormatFromPixelFormat(vFormat);
bool swap = false;
if (format == QImage::Format_Invalid) {
vFormat = static_cast<QVideoFrame::PixelFormat>(vFormat - QVideoFrame::Format_BGRA32 + QVideoFrame::Format_ARGB32);
format = QVideoFrame::imageFormatFromPixelFormat(vFormat);
if (format == QImage::Format_ARGB32) {
format = QImage::Format_RGBA8888;
} else if (format == QImage::Format_ARGB32_Premultiplied) {
format = QImage::Format_RGBA8888_Premultiplied;
if (vFormat < QVideoFrame::Format_BGRA5658_Premultiplied) {
vFormat = static_cast<QVideoFrame::PixelFormat>(vFormat - QVideoFrame::Format_BGRA32 + QVideoFrame::Format_ARGB32);
format = QVideoFrame::imageFormatFromPixelFormat(vFormat);
if (format == QImage::Format_ARGB32) {
format = QImage::Format_RGBA8888;
} else if (format == QImage::Format_ARGB32_Premultiplied) {
format = QImage::Format_RGBA8888_Premultiplied;
}
swap = true;
} else {
return false;
}
swap = true;
}
uchar* bits = mappedFrame.bits();
QImage image(bits, mappedFrame.width(), mappedFrame.height(), mappedFrame.bytesPerLine(), format);