From 871bdaff6d7ae05571830ae87d9de6ef4e2cdb9f Mon Sep 17 00:00:00 2001 From: Rhys Weatherley Date: Thu, 1 Oct 2009 17:52:09 +1000 Subject: QGLContext::areSharing() to check for GL share relationships Reviewed-by: Samuel --- src/opengl/qgl.cpp | 21 +++++++++++++------ src/opengl/qgl.h | 2 ++ src/opengl/qgl_p.h | 5 ++--- src/opengl/qglshaderprogram.cpp | 8 ++++---- src/opengl/qpaintengine_opengl.cpp | 10 ++++----- src/opengl/qpixmapdata_gl.cpp | 2 +- tests/auto/qgl/tst_qgl.cpp | 42 +++++++++++++++++++------------------- 7 files changed, 50 insertions(+), 40 deletions(-) diff --git a/src/opengl/qgl.cpp b/src/opengl/qgl.cpp index 1276443..bfb004e 100644 --- a/src/opengl/qgl.cpp +++ b/src/opengl/qgl.cpp @@ -2254,7 +2254,7 @@ QGLTexture *QGLContextPrivate::textureCacheLookup(const qint64 key, GLenum targe Q_Q(QGLContext); QGLTexture *texture = QGLTextureCache::instance()->getTexture(key); if (texture && texture->target == target - && (texture->context == q || qgl_share_reg()->checkSharing(q, texture->context))) + && (texture->context == q || QGLContext::areSharing(q, texture->context))) { return texture; } @@ -2755,6 +2755,20 @@ void QGLContext::setDevice(QPaintDevice *pDev) */ /*! + Returns true if \a context1 and \a context2 are sharing their + GL resources such as textures, shader programs, etc; + otherwise returns false. + + \since 4.6 +*/ +bool QGLContext::areSharing(const QGLContext *context1, const QGLContext *context2) +{ + if (!context1 || !context2) + return false; + return context1->d_ptr->group == context2->d_ptr->group; +} + +/*! \fn bool QGLContext::deviceIsPixmap() const Returns true if the paint device of this context is a pixmap; @@ -4835,11 +4849,6 @@ Q_OPENGL_EXPORT const QString qt_gl_library_name() } #endif -bool QGLShareRegister::checkSharing(const QGLContext *context1, const QGLContext *context2) { - bool sharing = (context1 && context2 && context1->d_ptr->group == context2->d_ptr->group); - return sharing; -} - void QGLShareRegister::addShare(const QGLContext *context, const QGLContext *share) { Q_ASSERT(context && share); if (context->d_ptr->group == share->d_ptr->group) diff --git a/src/opengl/qgl.h b/src/opengl/qgl.h index 1776004..b1c1317 100644 --- a/src/opengl/qgl.h +++ b/src/opengl/qgl.h @@ -282,6 +282,8 @@ public: bool isSharing() const; void reset(); + static bool areSharing(const QGLContext *context1, const QGLContext *context2); + QGLFormat format() const; QGLFormat requestedFormat() const; void setFormat(const QGLFormat& format); diff --git a/src/opengl/qgl_p.h b/src/opengl/qgl_p.h index a113b0f..9b09c7c 100644 --- a/src/opengl/qgl_p.h +++ b/src/opengl/qgl_p.h @@ -405,7 +405,6 @@ public: QGLShareRegister() {} ~QGLShareRegister() { reg.clear(); } - bool checkSharing(const QGLContext *context1, const QGLContext *context2); void addShare(const QGLContext *context, const QGLContext *share); QList shares(const QGLContext *context); void removeShare(const QGLContext *context); @@ -436,7 +435,7 @@ public: QGLContext *current = const_cast(QGLContext::currentContext()); QGLContext *ctx = const_cast(context); Q_ASSERT(ctx); - bool switch_context = current != ctx && !qgl_share_reg()->checkSharing(current, ctx); + bool switch_context = current != ctx && !QGLContext::areSharing(current, ctx); if (switch_context) ctx->makeCurrent(); #if defined(Q_WS_X11) @@ -547,7 +546,7 @@ public: : m_oldContext(0) { QGLContext *currentContext = const_cast(QGLContext::currentContext()); - if (currentContext != ctx && !qgl_share_reg()->checkSharing(ctx, currentContext)) { + if (currentContext != ctx && !QGLContext::areSharing(ctx, currentContext)) { m_oldContext = currentContext; m_ctx = const_cast(ctx); m_ctx->makeCurrent(); diff --git a/src/opengl/qglshaderprogram.cpp b/src/opengl/qglshaderprogram.cpp index ebcd723..dfa6c40 100644 --- a/src/opengl/qglshaderprogram.cpp +++ b/src/opengl/qglshaderprogram.cpp @@ -349,7 +349,7 @@ QGLShader::QGLShader(QGLShader::ShaderType type, const QGLContext *context, QObj context = QGLContext::currentContext(); d = new QGLShaderPrivate(context, type); #ifndef QT_NO_DEBUG - if (context && !qgl_share_reg()->checkSharing(context, QGLContext::currentContext())) { + if (context && !QGLContext::areSharing(context, QGLContext::currentContext())) { qWarning("QGLShader::QGLShader: \'context\' must be the currect context or sharing with it."); return; } @@ -374,7 +374,7 @@ QGLShader::QGLShader context = QGLContext::currentContext(); d = new QGLShaderPrivate(context, type); #ifndef QT_NO_DEBUG - if (context && !qgl_share_reg()->checkSharing(context, QGLContext::currentContext())) { + if (context && !QGLContext::areSharing(context, QGLContext::currentContext())) { qWarning("QGLShader::QGLShader: \'context\' must be currect context or sharing with it."); return; } @@ -806,8 +806,8 @@ bool QGLShaderProgram::addShader(QGLShader *shader) if (d->shaders.contains(shader)) return true; // Already added to this shader program. if (d->programGuard.id() && shader) { - if (!qgl_share_reg()->checkSharing(shader->d->shaderGuard.context(), - d->programGuard.context())) { + if (!QGLContext::areSharing(shader->d->shaderGuard.context(), + d->programGuard.context())) { qWarning("QGLShaderProgram::addShader: Program and shader are not associated with same context."); return false; } diff --git a/src/opengl/qpaintengine_opengl.cpp b/src/opengl/qpaintengine_opengl.cpp index a904064..da490c0 100644 --- a/src/opengl/qpaintengine_opengl.cpp +++ b/src/opengl/qpaintengine_opengl.cpp @@ -338,7 +338,7 @@ void QGLOffscreen::initialize() int dim = qMax(2048, static_cast(qt_next_power_of_two(qMax(device->size().width(), device->size().height())))); - bool shared_context = qgl_share_reg()->checkSharing(device->context(), ctx); + bool shared_context = QGLContext::areSharing(device->context(), ctx); bool would_fail = last_failed_size.isValid() && (device->size().width() >= last_failed_size.width() || device->size().height() >= last_failed_size.height()); @@ -555,7 +555,7 @@ public: QList contexts = programs.uniqueKeys(); for (int i=0; icheckSharing(cx, ctx)) { + if (cx != ctx && QGLContext::areSharing(cx, ctx)) { QList progs = programs.values(cx); for (int k=0; kcheckSharing(buffer_ctx, ctx)) + if (buffer_ctx && !QGLContext::areSharing(buffer_ctx, ctx)) cleanCache(); buffer_ctx = ctx; @@ -1365,7 +1365,7 @@ bool QOpenGLPaintEngine::begin(QPaintDevice *pdev) #ifdef QT_OPENGL_ES d->max_texture_size = ctx->d_func()->maxTextureSize(); #else - bool shared_ctx = qgl_share_reg()->checkSharing(d->device->context(), d->shader_ctx); + bool shared_ctx = QGLContext::areSharing(d->device->context(), d->shader_ctx); if (shared_ctx) { d->max_texture_size = d->shader_ctx->d_func()->maxTextureSize(); @@ -4683,7 +4683,7 @@ void QGLGlyphCache::cacheGlyphs(QGLContext *context, const QTextItemInt &ti, QList contexts = qt_context_cache.keys(); for (int i=0; icheckSharing(context, ctx)) { + if (ctx != context && QGLContext::areSharing(context, ctx)) { context_key = ctx; dev_it = qt_context_cache.constFind(context_key); break; diff --git a/src/opengl/qpixmapdata_gl.cpp b/src/opengl/qpixmapdata_gl.cpp index 2331c6d..1ee3bbf 100644 --- a/src/opengl/qpixmapdata_gl.cpp +++ b/src/opengl/qpixmapdata_gl.cpp @@ -248,7 +248,7 @@ bool QGLPixmapData::isValidContext(const QGLContext *ctx) const return true; const QGLContext *share_ctx = qt_gl_share_widget()->context(); - return ctx == share_ctx || qgl_share_reg()->checkSharing(ctx, share_ctx); + return ctx == share_ctx || QGLContext::areSharing(ctx, share_ctx); } void QGLPixmapData::resize(int width, int height) diff --git a/tests/auto/qgl/tst_qgl.cpp b/tests/auto/qgl/tst_qgl.cpp index a49f543..f15b249 100644 --- a/tests/auto/qgl/tst_qgl.cpp +++ b/tests/auto/qgl/tst_qgl.cpp @@ -1799,13 +1799,13 @@ void tst_QGL::shareRegister() QVERIFY(list.contains(glw2->context())); // Check the sharing relationships. - QVERIFY(shareReg->checkSharing(glw1->context(), glw1->context())); - QVERIFY(shareReg->checkSharing(glw2->context(), glw2->context())); - QVERIFY(shareReg->checkSharing(glw1->context(), glw2->context())); - QVERIFY(shareReg->checkSharing(glw2->context(), glw1->context())); - QVERIFY(!shareReg->checkSharing(0, glw2->context())); - QVERIFY(!shareReg->checkSharing(glw1->context(), 0)); - QVERIFY(!shareReg->checkSharing(0, 0)); + QVERIFY(QGLContext::areSharing(glw1->context(), glw1->context())); + QVERIFY(QGLContext::areSharing(glw2->context(), glw2->context())); + QVERIFY(QGLContext::areSharing(glw1->context(), glw2->context())); + QVERIFY(QGLContext::areSharing(glw2->context(), glw1->context())); + QVERIFY(!QGLContext::areSharing(0, glw2->context())); + QVERIFY(!QGLContext::areSharing(glw1->context(), 0)); + QVERIFY(!QGLContext::areSharing(0, 0)); // Create a third context, not sharing with the others. QGLWidget *glw3 = new QGLWidget(); @@ -1827,20 +1827,20 @@ void tst_QGL::shareRegister() QCOMPARE(list.size(), 0); // Check the sharing relationships again. - QVERIFY(shareReg->checkSharing(glw1->context(), glw1->context())); - QVERIFY(shareReg->checkSharing(glw2->context(), glw2->context())); - QVERIFY(shareReg->checkSharing(glw1->context(), glw2->context())); - QVERIFY(shareReg->checkSharing(glw2->context(), glw1->context())); - QVERIFY(!shareReg->checkSharing(glw1->context(), glw3->context())); - QVERIFY(!shareReg->checkSharing(glw2->context(), glw3->context())); - QVERIFY(!shareReg->checkSharing(glw3->context(), glw1->context())); - QVERIFY(!shareReg->checkSharing(glw3->context(), glw2->context())); - QVERIFY(shareReg->checkSharing(glw3->context(), glw3->context())); - QVERIFY(!shareReg->checkSharing(0, glw2->context())); - QVERIFY(!shareReg->checkSharing(glw1->context(), 0)); - QVERIFY(!shareReg->checkSharing(0, glw3->context())); - QVERIFY(!shareReg->checkSharing(glw3->context(), 0)); - QVERIFY(!shareReg->checkSharing(0, 0)); + QVERIFY(QGLContext::areSharing(glw1->context(), glw1->context())); + QVERIFY(QGLContext::areSharing(glw2->context(), glw2->context())); + QVERIFY(QGLContext::areSharing(glw1->context(), glw2->context())); + QVERIFY(QGLContext::areSharing(glw2->context(), glw1->context())); + QVERIFY(!QGLContext::areSharing(glw1->context(), glw3->context())); + QVERIFY(!QGLContext::areSharing(glw2->context(), glw3->context())); + QVERIFY(!QGLContext::areSharing(glw3->context(), glw1->context())); + QVERIFY(!QGLContext::areSharing(glw3->context(), glw2->context())); + QVERIFY(QGLContext::areSharing(glw3->context(), glw3->context())); + QVERIFY(!QGLContext::areSharing(0, glw2->context())); + QVERIFY(!QGLContext::areSharing(glw1->context(), 0)); + QVERIFY(!QGLContext::areSharing(0, glw3->context())); + QVERIFY(!QGLContext::areSharing(glw3->context(), 0)); + QVERIFY(!QGLContext::areSharing(0, 0)); // Shared guard should still be the same. QVERIFY(guard.context() == glw1->context()); -- cgit v0.12