From 41c08151f3815af843491d1659f9770c4c115e6d Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Sun, 23 Sep 2018 19:41:36 -0700 Subject: [PATCH] Qt: Fix tearing issues --- src/platform/qt/CoreController.cpp | 12 +++++------- src/platform/qt/CoreController.h | 4 ++-- src/platform/qt/DisplayQt.cpp | 2 +- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/platform/qt/CoreController.cpp b/src/platform/qt/CoreController.cpp index f81eb6c9b..b8ac73de5 100644 --- a/src/platform/qt/CoreController.cpp +++ b/src/platform/qt/CoreController.cpp @@ -46,6 +46,7 @@ CoreController::CoreController(mCore* core, QObject* parent) m_buffers[0].fill(0xFF); m_buffers[1].fill(0xFF); m_activeBuffer = &m_buffers[0]; + m_completeBuffer = m_buffers[0]; m_threadContext.core->setVideoBuffer(m_threadContext.core, reinterpret_cast(m_activeBuffer->data()), size.width()); @@ -209,12 +210,9 @@ CoreController::~CoreController() { m_threadContext.core->deinit(m_threadContext.core); } -color_t* CoreController::drawContext() { +const color_t* CoreController::drawContext() { QMutexLocker locker(&m_mutex); - if (!m_completeBuffer) { - return nullptr; - } - return reinterpret_cast(m_completeBuffer->data()); + return reinterpret_cast(m_completeBuffer.constData()); } bool CoreController::isPaused() { @@ -790,7 +788,7 @@ int CoreController::updateAutofire() { void CoreController::finishFrame() { QMutexLocker locker(&m_mutex); - m_completeBuffer = m_activeBuffer; + memcpy(m_completeBuffer.data(), m_activeBuffer->constData(), m_activeBuffer->size()); // TODO: Generalize this to triple buffering? m_activeBuffer = &m_buffers[0]; @@ -798,7 +796,7 @@ void CoreController::finishFrame() { m_activeBuffer = &m_buffers[1]; } // Copy contents to avoid issues when doing frameskip - memcpy(m_activeBuffer->data(), m_completeBuffer->data(), m_activeBuffer->size()); + memcpy(m_activeBuffer->data(), m_completeBuffer.constData(), m_activeBuffer->size()); m_threadContext.core->setVideoBuffer(m_threadContext.core, reinterpret_cast(m_activeBuffer->data()), screenDimensions().width()); for (auto& action : m_frameActions) { diff --git a/src/platform/qt/CoreController.h b/src/platform/qt/CoreController.h index 4ea647402..ec6b7d998 100644 --- a/src/platform/qt/CoreController.h +++ b/src/platform/qt/CoreController.h @@ -58,7 +58,7 @@ public: mCoreThread* thread() { return &m_threadContext; } - color_t* drawContext(); + const color_t* drawContext(); bool isPaused(); bool hasStarted(); @@ -174,7 +174,7 @@ private: QByteArray m_buffers[2]; QByteArray* m_activeBuffer; - QByteArray* m_completeBuffer = nullptr; + QByteArray m_completeBuffer; std::unique_ptr m_cacheSet; std::unique_ptr m_override; diff --git a/src/platform/qt/DisplayQt.cpp b/src/platform/qt/DisplayQt.cpp index 846382f3a..6043a7399 100644 --- a/src/platform/qt/DisplayQt.cpp +++ b/src/platform/qt/DisplayQt.cpp @@ -51,7 +51,7 @@ void DisplayQt::filter(bool filter) { void DisplayQt::framePosted() { update(); - color_t* buffer = m_context->drawContext(); + const color_t* buffer = m_context->drawContext(); if (const_cast(m_backing).bits() == reinterpret_cast(buffer)) { return; }