From 520cccb2190b34edca8e5301119765be433827ba Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Thu, 24 Mar 2016 15:19:46 -0700 Subject: [PATCH] Make display plugin switching atomic from other threads --- interface/src/Application.cpp | 47 +++++++++++++++++++++-------------- interface/src/Application.h | 2 +- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index e7bccae892..c8860ffdaa 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -4677,13 +4677,18 @@ qreal Application::getDevicePixelRatio() { } DisplayPlugin* Application::getActiveDisplayPlugin() { - std::unique_lock lock(_displayPluginLock); - if (nullptr == _displayPlugin && QThread::currentThread() == thread()) { - updateDisplayMode(); - Q_ASSERT(_displayPlugin); + DisplayPlugin* result = nullptr; + if (QThread::currentThread() == thread()) { + if (nullptr == _displayPlugin) { + updateDisplayMode(); + Q_ASSERT(_displayPlugin); + } + result = _displayPlugin.get(); + } else { + std::unique_lock lock(_displayPluginLock); + result = _displayPlugin.get(); } - - return _displayPlugin.get(); + return result; } const DisplayPlugin* Application::getActiveDisplayPlugin() const { @@ -4801,20 +4806,26 @@ void Application::updateDisplayMode() { return; } - if (_displayPlugin) { - _displayPlugin->deactivate(); - } - auto offscreenUi = DependencyManager::get(); - // FIXME probably excessive and useless context switching - _offscreenContext->makeCurrent(); - newDisplayPlugin->activate(); - _offscreenContext->makeCurrent(); - offscreenUi->resize(fromGlm(newDisplayPlugin->getRecommendedUiSize())); - _offscreenContext->makeCurrent(); - getApplicationCompositor().setDisplayPlugin(newDisplayPlugin); - _displayPlugin = newDisplayPlugin; + // Make the switch atomic from the perspective of other threads + { + std::unique_lock lock(_displayPluginLock); + + if (_displayPlugin) { + _displayPlugin->deactivate(); + } + + // FIXME probably excessive and useless context switching + _offscreenContext->makeCurrent(); + newDisplayPlugin->activate(); + _offscreenContext->makeCurrent(); + offscreenUi->resize(fromGlm(newDisplayPlugin->getRecommendedUiSize())); + _offscreenContext->makeCurrent(); + getApplicationCompositor().setDisplayPlugin(newDisplayPlugin); + _displayPlugin = newDisplayPlugin; + } + emit activeDisplayPluginChanged(); diff --git a/interface/src/Application.h b/interface/src/Application.h index 695d30998a..d21e647bc7 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -383,7 +383,7 @@ private: OffscreenGLCanvas* _offscreenContext { nullptr }; DisplayPluginPointer _displayPlugin; - std::recursive_mutex _displayPluginLock; + std::mutex _displayPluginLock; InputPluginList _activeInputPlugins; bool _activatingDisplayPlugin { false };