From c7e65ff621b4bebfc41fb91fd378e034564b0880 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Thu, 27 Jul 2017 00:06:54 -0700 Subject: [PATCH] Qt: More camera threading fixes --- src/platform/qt/InputController.cpp | 17 +++++++++++++---- src/platform/qt/InputController.h | 3 +++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/platform/qt/InputController.cpp b/src/platform/qt/InputController.cpp index 8a86256ac..5e014adaa 100644 --- a/src/platform/qt/InputController.cpp +++ b/src/platform/qt/InputController.cpp @@ -111,12 +111,17 @@ InputController::InputController(int playerId, QWidget* topLevel, QObject* paren m_image.requestImage = [](mImageSource* context, const uint32_t** buffer, size_t* stride) { InputControllerImage* image = static_cast(context); - if (image->resizedImage.isNull()) { - image->resizedImage = image->image.scaled(image->w, image->h, Qt::KeepAspectRatioByExpanding); + QSize size; + { + QMutexLocker locker(&image->mutex); + if (image->outOfDate) { + image->resizedImage = image->image.scaled(image->w, image->h, Qt::KeepAspectRatioByExpanding); + image->resizedImage = image->resizedImage.convertToFormat(QImage::Format_RGB32); + image->outOfDate = false; + } } - image->resizedImage = image->resizedImage.convertToFormat(QImage::Format_RGB32); + size = image->resizedImage.size(); const uint32_t* bits = reinterpret_cast(image->resizedImage.constBits()); - QSize size = image->resizedImage.size(); if (size.width() > image->w) { bits += (size.width() - image->w) / 2; } @@ -671,13 +676,17 @@ void InputController::releaseFocus(QWidget* focus) { } void InputController::loadCamImage(const QString& path) { + QMutexLocker locker(&m_image.mutex); m_image.image.load(path); m_image.resizedImage = QImage(); + m_image.outOfDate = true; } void InputController::setCamImage(const QImage& image) { + QMutexLocker locker(&m_image.mutex); m_image.image = image; m_image.resizedImage = QImage(); + m_image.outOfDate = true; } void InputController::increaseLuminanceLevel() { diff --git a/src/platform/qt/InputController.h b/src/platform/qt/InputController.h index 3282c04cc..50d670fc2 100644 --- a/src/platform/qt/InputController.h +++ b/src/platform/qt/InputController.h @@ -10,6 +10,7 @@ #include "GamepadHatEvent.h" #include +#include #include #include #include @@ -144,6 +145,8 @@ private: InputController* p; QImage image; QImage resizedImage; + bool outOfDate; + QMutex mutex; unsigned w, h; } m_image;