From 7cdbd6c86e36223189e7a73db80ac0979101a7f0 Mon Sep 17 00:00:00 2001 From: Jake Petroules Date: Wed, 12 Jul 2017 10:53:15 -0700 Subject: Reduce the scope of the preprocessor branches with the help of a union MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I195589565920e795cdae99f837b51917b3831228 Reviewed-by: Thiago Macieira Reviewed-by: Miikka Heikkinen Reviewed-by: Tomi Korpipää --- src/datavisualizationqml2/abstractdeclarative.cpp | 72 ++++++++--------------- src/datavisualizationqml2/abstractdeclarative_p.h | 18 ++---- 2 files changed, 30 insertions(+), 60 deletions(-) (limited to 'src') diff --git a/src/datavisualizationqml2/abstractdeclarative.cpp b/src/datavisualizationqml2/abstractdeclarative.cpp index 9c7a7238..5a8f0535 100644 --- a/src/datavisualizationqml2/abstractdeclarative.cpp +++ b/src/datavisualizationqml2/abstractdeclarative.cpp @@ -38,6 +38,12 @@ #include #endif +#if !defined(Q_OS_MAC) && !defined(Q_OS_ANDROID) && !defined(Q_OS_WINRT) +#define USE_SHARED_CONTEXT +#else +#include "glstatestore_p.h" +#endif + QT_BEGIN_NAMESPACE_DATAVISUALIZATION static QList clearList; @@ -52,11 +58,7 @@ AbstractDeclarative::AbstractDeclarative(QQuickItem *parent) : m_samples(0), m_windowSamples(0), m_initialisedSize(0, 0), - #ifdef USE_SHARED_CONTEXT - m_context(0), - #else - m_stateStore(0), - #endif + m_contextOrStateStore(0), m_qtContext(0), m_mainThread(QThread::currentThread()), m_contextThread(0) @@ -352,60 +354,44 @@ void AbstractDeclarative::setSharedController(Abstract3DController *controller) void AbstractDeclarative::activateOpenGLContext(QQuickWindow *window) { -#ifdef USE_SHARED_CONTEXT // We can assume we are not in middle of AbstractDeclarative destructor when we are here, // since m_context creation is always done when this function is called from // synchDataToRenderer(), which blocks main thread -> no need to mutex. - if (!m_context || !m_qtContext || m_contextWindow != window) { + if (!m_contextOrStateStore || !m_qtContext || m_contextWindow != window) { QOpenGLContext *currentContext = QOpenGLContext::currentContext(); // Note: Changing graph to different window when using multithreaded renderer will break! - delete m_context; + delete m_contextOrStateStore; m_contextThread = QThread::currentThread(); m_contextWindow = window; m_qtContext = currentContext; + +#ifdef USE_SHARED_CONTEXT m_context = new QOpenGLContext(); m_context->setFormat(m_qtContext->format()); m_context->setShareContext(m_qtContext); m_context->create(); - m_context->makeCurrent(window); - m_controller->initializeOpenGL(); - - // Make sure context gets deleted. - QObject::connect(m_contextThread, &QThread::finished, this, - &AbstractDeclarative::destroyContext, Qt::DirectConnection); - } else { - m_context->makeCurrent(window); - } #else - // Shared contexts don't work properly in some platforms, so just store the - // context state on those - if (!m_stateStore || !m_qtContext || m_contextWindow != window) { - QOpenGLContext *currentContext = QOpenGLContext::currentContext(); - - // Note: Changing graph to different window when using multithreaded renderer will break! - - delete m_stateStore; - - m_contextThread = QThread::currentThread(); - m_contextWindow = window; - m_qtContext = currentContext; - + // Shared contexts don't work properly in some platforms, so just store the + // context state on those m_stateStore = new GLStateStore(QOpenGLContext::currentContext()); - m_stateStore->storeGLState(); +#endif m_controller->initializeOpenGL(); - // Make sure state store gets deleted. + // Make sure context / state store gets deleted. QObject::connect(m_contextThread, &QThread::finished, this, &AbstractDeclarative::destroyContext, Qt::DirectConnection); } else { +#ifdef USE_SHARED_CONTEXT + m_context->makeCurrent(window); +#else m_stateStore->storeGLState(); - } #endif + } } void AbstractDeclarative::doneOpenGLContext(QQuickWindow *window) @@ -854,24 +840,14 @@ void AbstractDeclarative::windowDestroyed(QObject *obj) void AbstractDeclarative::destroyContext() { -#ifdef USE_SHARED_CONTEXT - // Context can be in another thread, don't delete it directly in that case if (m_contextThread && m_contextThread != m_mainThread) { - if (m_context) - m_context->deleteLater(); + if (m_contextOrStateStore) + m_contextOrStateStore->deleteLater(); } else { - delete m_context; + delete m_contextOrStateStore; } - m_context = 0; -#else - if (m_contextThread && m_contextThread != m_mainThread) { - if (m_stateStore) - m_stateStore->deleteLater(); - } else { - delete m_stateStore; - } - m_stateStore = 0; -#endif + m_contextOrStateStore = 0; + if (m_contextThread) { QObject::disconnect(m_contextThread, &QThread::finished, this, &AbstractDeclarative::destroyContext); diff --git a/src/datavisualizationqml2/abstractdeclarative_p.h b/src/datavisualizationqml2/abstractdeclarative_p.h index f49da398..18cca6e2 100644 --- a/src/datavisualizationqml2/abstractdeclarative_p.h +++ b/src/datavisualizationqml2/abstractdeclarative_p.h @@ -50,13 +50,7 @@ #include #include -#if !defined(Q_OS_MAC) && !defined(Q_OS_ANDROID) && !defined(Q_OS_WINRT) -#define USE_SHARED_CONTEXT -#endif - -#ifndef USE_SHARED_CONTEXT -#include "glstatestore_p.h" -#endif +class GLStateStore; QT_BEGIN_NAMESPACE_DATAVISUALIZATION @@ -297,11 +291,11 @@ private: int m_samples; int m_windowSamples; QSize m_initialisedSize; -#ifdef USE_SHARED_CONTEXT - QOpenGLContext *m_context; -#else - GLStateStore *m_stateStore; -#endif + union { + QObject *m_contextOrStateStore; + QOpenGLContext *m_context; + GLStateStore *m_stateStore; + }; QPointer m_qtContext; QThread *m_mainThread; QThread *m_contextThread; -- cgit v1.2.3