diff options
Diffstat (limited to 'src/opengl')
-rw-r--r-- | src/opengl/gl2paintengineex/qglengineshadermanager.cpp | 6 | ||||
-rw-r--r-- | src/opengl/gl2paintengineex/qglgradientcache.cpp | 6 | ||||
-rw-r--r-- | src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp | 475 | ||||
-rw-r--r-- | src/opengl/gl2paintengineex/qpaintengineex_opengl2_p.h | 50 | ||||
-rw-r--r-- | src/opengl/qgl.cpp | 184 | ||||
-rw-r--r-- | src/opengl/qgl_p.h | 183 | ||||
-rw-r--r-- | src/opengl/qglpixmapfilter.cpp | 167 | ||||
-rw-r--r-- | src/opengl/qpaintengine_opengl.cpp | 2 |
8 files changed, 623 insertions, 450 deletions
diff --git a/src/opengl/gl2paintengineex/qglengineshadermanager.cpp b/src/opengl/gl2paintengineex/qglengineshadermanager.cpp index 59c50aa..e22303d 100644 --- a/src/opengl/gl2paintengineex/qglengineshadermanager.cpp +++ b/src/opengl/gl2paintengineex/qglengineshadermanager.cpp @@ -60,12 +60,8 @@ QGLEngineSharedShaders *QGLEngineSharedShaders::shadersForContext(const QGLConte { QGLEngineSharedShaders *p = reinterpret_cast<QGLEngineSharedShaders *>(qt_shared_shaders()->value(context)); if (!p) { - QGLContext *oldContext = const_cast<QGLContext *>(QGLContext::currentContext()); - if (oldContext != context) - const_cast<QGLContext *>(context)->makeCurrent(); + QGLShareContextScope scope(context); qt_shared_shaders()->insert(context, p = new QGLEngineSharedShaders(context)); - if (oldContext && oldContext != context) - oldContext->makeCurrent(); } return p; } diff --git a/src/opengl/gl2paintengineex/qglgradientcache.cpp b/src/opengl/gl2paintengineex/qglgradientcache.cpp index 4b2b2a0..e06f15d 100644 --- a/src/opengl/gl2paintengineex/qglgradientcache.cpp +++ b/src/opengl/gl2paintengineex/qglgradientcache.cpp @@ -57,13 +57,9 @@ QGL2GradientCache *QGL2GradientCache::cacheForContext(const QGLContext *context) { QGL2GradientCache *p = reinterpret_cast<QGL2GradientCache *>(qt_gradient_caches()->value(context)); if (!p) { - QGLContext *oldContext = const_cast<QGLContext *>(QGLContext::currentContext()); - if (oldContext != context) - const_cast<QGLContext *>(context)->makeCurrent(); + QGLShareContextScope scope(context); p = new QGL2GradientCache; qt_gradient_caches()->insert(context, p); - if (oldContext && oldContext != context) - oldContext->makeCurrent(); } return p; } diff --git a/src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp b/src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp index 992d47d..5875124 100644 --- a/src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp +++ b/src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp @@ -87,6 +87,7 @@ QT_BEGIN_NAMESPACE //#define QT_GL_NO_SCISSOR_TEST +static const GLuint GL_STENCIL_HIGH_BIT = 0x80; static const GLuint QT_BRUSH_TEXTURE_UNIT = 0; static const GLuint QT_IMAGE_TEXTURE_UNIT = 0; //Can be the same as brush texture unit static const GLuint QT_MASK_TEXTURE_UNIT = 1; @@ -162,15 +163,11 @@ QGLTextureGlyphCache::QGLTextureGlyphCache(QGLContext *context, QFontEngineGlyph QGLTextureGlyphCache::~QGLTextureGlyphCache() { if (ctx) { - QGLContext *oldContext = const_cast<QGLContext *>(QGLContext::currentContext()); - if (oldContext != ctx) - ctx->makeCurrent(); + QGLShareContextScope scope(ctx); glDeleteFramebuffers(1, &m_fbo); if (m_width || m_height) glDeleteTextures(1, &m_texture); - if (oldContext && oldContext != ctx) - oldContext->makeCurrent(); } } @@ -231,6 +228,7 @@ void QGLTextureGlyphCache::resizeTextureData(int width, int height) glPushAttrib(GL_ENABLE_BIT | GL_VIEWPORT_BIT | GL_SCISSOR_BIT); #endif + glDisable(GL_STENCIL_TEST); glDisable(GL_DEPTH_TEST); glDisable(GL_SCISSOR_TEST); glDisable(GL_BLEND); @@ -280,7 +278,7 @@ void QGLTextureGlyphCache::resizeTextureData(int width, int height) glBindFramebuffer(GL_FRAMEBUFFER_EXT, ctx->d_ptr->current_fbo); glViewport(0, 0, pex->width, pex->height); - pex->updateDepthScissorTest(); + pex->updateClipScissorTest(); #ifndef QT_OPENGL_ES_2 if (pex->inRenderText) @@ -406,11 +404,6 @@ void QGL2PaintEngineExPrivate::useSimpleShader() shaderManager->simpleProgram()->setUniformValue("pmvMatrix", pmvMatrix); simpleShaderMatrixUniformDirty = false; } - - if (simpleShaderDepthUniformDirty) { - shaderManager->simpleProgram()->setUniformValue("depth", normalizedDeviceDepth(q->state()->currentDepth)); - simpleShaderDepthUniformDirty = false; - } } void QGL2PaintEngineExPrivate::updateBrushTexture() @@ -766,6 +759,8 @@ void QGL2PaintEngineEx::beginNativePainting() d->dirtyStencilRegion = QRect(0, 0, d->width, d->height); d->resetGLState(); + d->shaderManager->setDirty(); + d->needsSync = true; } @@ -773,9 +768,11 @@ void QGL2PaintEngineExPrivate::resetGLState() { glDisable(GL_BLEND); glActiveTexture(GL_TEXTURE0); + glDisable(GL_STENCIL_TEST); glDisable(GL_DEPTH_TEST); glDisable(GL_SCISSOR_TEST); glDepthMask(true); + glDepthFunc(GL_LESS); glClearDepth(1); } @@ -879,16 +876,15 @@ void QGL2PaintEngineExPrivate::fill(const QVectorPath& path) if (path.shape() == QVectorPath::RectangleHint) { QGLRect rect(points[0].x(), points[0].y(), points[2].x(), points[2].y()); prepareForDraw(currentBrush->isOpaque()); - composite(rect); - } - else if (path.shape() == QVectorPath::EllipseHint) { + } else if (path.shape() == QVectorPath::EllipseHint + || path.shape() == QVectorPath::ConvexPolygonHint) + { vertexCoordinateArray.clear(); vertexCoordinateArray.addPath(path, inverseScale); prepareForDraw(currentBrush->isOpaque()); drawVertexArrays(vertexCoordinateArray, GL_TRIANGLE_FAN); - } - else { + } else { // The path is too complicated & needs the stencil technique vertexCoordinateArray.clear(); vertexCoordinateArray.addPath(path, inverseScale); @@ -896,20 +892,23 @@ void QGL2PaintEngineExPrivate::fill(const QVectorPath& path) fillStencilWithVertexArray(vertexCoordinateArray, path.hasWindingFill()); // Stencil the brush onto the dest buffer - glStencilFunc(GL_NOTEQUAL, 0, 0xFFFF); // Pass if stencil buff value != 0 - glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); + glStencilFunc(GL_EQUAL, GL_STENCIL_HIGH_BIT, GL_STENCIL_HIGH_BIT); // Pass if stencil buff value != 0 + glStencilOp(GL_KEEP, GL_ZERO, GL_ZERO); + glStencilMask(GL_STENCIL_HIGH_BIT); - glEnable(GL_STENCIL_TEST); prepareForDraw(currentBrush->isOpaque()); -#ifndef QT_OPENGL_ES_2 if (inRenderText) - shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::Depth), zValueForRenderText()); -#endif + prepareDepthRangeForRenderText(); + composite(vertexCoordinateArray.boundingRect()); - glDisable(GL_STENCIL_TEST); + + if (inRenderText) + restoreDepthRangeForRenderText(); glStencilMask(0); + + updateClipScissorTest(); } } @@ -917,31 +916,28 @@ void QGL2PaintEngineExPrivate::fill(const QVectorPath& path) void QGL2PaintEngineExPrivate::fillStencilWithVertexArray(QGL2PEXVertexArray& vertexArray, bool useWindingFill) { // qDebug("QGL2PaintEngineExPrivate::fillStencilWithVertexArray()"); - glStencilMask(0xFFFF); // Enable stencil writes + glStencilMask(0xffff); // Enable stencil writes if (dirtyStencilRegion.intersects(currentScissorBounds)) { - // Clear the stencil buffer to zeros - glDisable(GL_STENCIL_TEST); + QVector<QRect> clearRegion = dirtyStencilRegion.intersected(currentScissorBounds).rects(); glClearStencil(0); // Clear to zero - glClear(GL_STENCIL_BUFFER_BIT); + for (int i = 0; i < clearRegion.size(); ++i) { +#ifndef QT_GL_NO_SCISSOR_TEST + setScissor(clearRegion.at(i)); +#endif + glClear(GL_STENCIL_BUFFER_BIT); + } + dirtyStencilRegion -= currentScissorBounds; + +#ifndef QT_GL_NO_SCISSOR_TEST + updateClipScissorTest(); +#endif } glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); // Disable color writes - glStencilFunc(GL_ALWAYS, 0, 0xFFFF); // Always pass the stencil test - - // Setup the stencil op: - if (useWindingFill) { - glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_KEEP, GL_INCR_WRAP); // Inc. for front-facing triangle - glStencilOpSeparate(GL_BACK, GL_KEEP, GL_KEEP, GL_DECR_WRAP); //Dec. for back-facing "holes" - } else - glStencilOp(GL_KEEP, GL_KEEP, GL_INVERT); // Simply invert the stencil bit - - // No point in using a fancy gradient shader for writing into the stencil buffer! useSimpleShader(); - glEnable(GL_STENCIL_TEST); // For some reason, this has to happen _after_ the simple shader is use()'d - glDisable(GL_BLEND); #ifndef QT_OPENGL_ES_2 if (inRenderText) { @@ -950,15 +946,97 @@ void QGL2PaintEngineExPrivate::fillStencilWithVertexArray(QGL2PEXVertexArray& ve } #endif - // Draw the vertecies into the stencil buffer: - drawVertexArrays(vertexArray, GL_TRIANGLE_FAN); + if (useWindingFill) { + if (q->state()->clipTestEnabled) { + glStencilFunc(GL_LEQUAL, GL_STENCIL_HIGH_BIT | q->state()->currentClip, ~GL_STENCIL_HIGH_BIT); + glStencilOp(GL_KEEP, GL_REPLACE, GL_REPLACE); + composite(vertexArray.boundingRect()); + + glStencilFunc(GL_EQUAL, GL_STENCIL_HIGH_BIT, GL_STENCIL_HIGH_BIT); + } else { + glStencilFunc(GL_ALWAYS, 0, 0xffff); + glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO); + composite(vertexArray.boundingRect()); + } + + // Inc. for front-facing triangle + glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_INCR_WRAP, GL_INCR_WRAP); + //Dec. for back-facing "holes" + glStencilOpSeparate(GL_BACK, GL_KEEP, GL_DECR_WRAP, GL_DECR_WRAP); + glStencilMask(~GL_STENCIL_HIGH_BIT); + drawVertexArrays(vertexArray, GL_TRIANGLE_FAN); + + if (q->state()->clipTestEnabled) { + // clear high bit of stencil outside of path + glStencilFunc(GL_EQUAL, GL_STENCIL_HIGH_BIT | q->state()->currentClip, ~GL_STENCIL_HIGH_BIT); + glStencilOp(GL_KEEP, GL_INVERT, GL_INVERT); + glStencilMask(GL_STENCIL_HIGH_BIT); + composite(vertexArray.boundingRect()); + // reset lower bits of stencil inside path to current clip + glStencilFunc(GL_EQUAL, GL_STENCIL_HIGH_BIT | q->state()->currentClip, GL_STENCIL_HIGH_BIT); + glStencilOp(GL_KEEP, GL_REPLACE, GL_REPLACE); + glStencilMask(~GL_STENCIL_HIGH_BIT); + composite(vertexArray.boundingRect()); + } else { + // set high bit of stencil inside path + glStencilFunc(GL_NOTEQUAL, 0, 0xffff); + glStencilOp(GL_KEEP, GL_INVERT, GL_INVERT); + glStencilMask(GL_STENCIL_HIGH_BIT); + composite(vertexArray.boundingRect()); + } + } else { + glStencilMask(GL_STENCIL_HIGH_BIT); + glStencilOp(GL_KEEP, GL_KEEP, GL_INVERT); // Simply invert the stencil bit + drawVertexArrays(vertexArray, GL_TRIANGLE_FAN); + } + + // Enable color writes & disable stencil writes + glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); #ifndef QT_OPENGL_ES_2 if (inRenderText) glPopAttrib(); #endif - // Enable color writes & disable stencil writes +} + +/* + If the maximum value in the stencil buffer is GL_STENCIL_HIGH_BIT - 1, + restore the stencil buffer to a pristine state. The current clip region + is set to 1, and the rest to 0. +*/ +void QGL2PaintEngineExPrivate::resetClipIfNeeded() +{ + if (maxClip != (GL_STENCIL_HIGH_BIT - 1)) + return; + + Q_Q(QGL2PaintEngineEx); + + useSimpleShader(); + glEnable(GL_STENCIL_TEST); + glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); + + QRectF bounds = q->state()->matrix.inverted().mapRect(QRectF(0, 0, width, height)); + QGLRect rect(bounds.left(), bounds.top(), bounds.right(), bounds.bottom()); + + // Set high bit on clip region + glStencilFunc(GL_LEQUAL, q->state()->currentClip, 0xffff); + glStencilOp(GL_KEEP, GL_INVERT, GL_INVERT); + glStencilMask(GL_STENCIL_HIGH_BIT); + composite(rect); + + // Reset clipping to 1 and everything else to zero + glStencilFunc(GL_NOTEQUAL, 0x01, GL_STENCIL_HIGH_BIT); + glStencilOp(GL_ZERO, GL_REPLACE, GL_REPLACE); + glStencilMask(0xFFFF); + composite(rect); + + q->state()->currentClip = 1; + q->state()->canRestoreClip = false; + + maxClip = 1; + + glStencilMask(0x0); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); } @@ -1006,7 +1084,6 @@ bool QGL2PaintEngineExPrivate::prepareForDraw(bool srcPixelsAreOpaque) // The shader program has changed so mark all uniforms as dirty: brushUniformsDirty = true; shaderMatrixUniformDirty = true; - depthUniformDirty = true; opacityUniformDirty = true; } @@ -1018,11 +1095,6 @@ bool QGL2PaintEngineExPrivate::prepareForDraw(bool srcPixelsAreOpaque) shaderMatrixUniformDirty = false; } - if (depthUniformDirty) { - shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::Depth), normalizedDeviceDepth(q->state()->currentDepth)); - depthUniformDirty = false; - } - if (opacityMode == QGLEngineShaderManager::UniformOpacity && opacityUniformDirty) { shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::GlobalOpacity), (GLfloat)q->state()->opacity); opacityUniformDirty = false; @@ -1069,7 +1141,7 @@ void QGL2PaintEngineExPrivate::drawVertexArrays(QGL2PEXVertexArray& vertexArray, glDisableVertexAttribArray(QT_VERTEX_COORDS_ATTR); } -float QGL2PaintEngineExPrivate::zValueForRenderText() const +void QGL2PaintEngineExPrivate::prepareDepthRangeForRenderText() { #ifndef QT_OPENGL_ES_2 // Get the z translation value from the model view matrix and @@ -1077,9 +1149,19 @@ float QGL2PaintEngineExPrivate::zValueForRenderText() const // and z-far = 1, which is used in QGLWidget::renderText() GLdouble model[4][4]; glGetDoublev(GL_MODELVIEW_MATRIX, &model[0][0]); - return -2 * model[3][2] - 1; -#else - return 0; + float deviceZ = -2 * model[3][2] - 1; + + glGetFloatv(GL_DEPTH_RANGE, depthRange); + float windowZ = depthRange[0] + (deviceZ + 1) * 0.5 * (depthRange[1] - depthRange[0]); + + glDepthRange(windowZ, windowZ); +#endif +} + +void QGL2PaintEngineExPrivate::restoreDepthRangeForRenderText() +{ +#ifndef QT_OPENGL_ES_2 + glDepthRange(depthRange[0], depthRange[1]); #endif } @@ -1142,6 +1224,7 @@ void QGL2PaintEngineEx::opacityChanged() { // qDebug("QGL2PaintEngineEx::opacityChanged()"); Q_D(QGL2PaintEngineEx); + state()->opacityChanged = true; Q_ASSERT(d->shaderManager); d->brushUniformsDirty = true; @@ -1152,11 +1235,14 @@ void QGL2PaintEngineEx::compositionModeChanged() { // qDebug("QGL2PaintEngineEx::compositionModeChanged()"); Q_D(QGL2PaintEngineEx); + state()->compositionModeChanged = true; d->compositionModeDirty = true; } void QGL2PaintEngineEx::renderHintsChanged() { + state()->renderHintsChanged = true; + #if !defined(QT_OPENGL_ES_2) if ((state()->renderHints & QPainter::Antialiasing) || (state()->renderHints & QPainter::HighQualityAntialiasing)) @@ -1175,6 +1261,7 @@ void QGL2PaintEngineEx::transformChanged() { Q_D(QGL2PaintEngineEx); d->matrixDirty = true; + state()->matrixChanged = true; } @@ -1262,6 +1349,9 @@ void QGL2PaintEngineEx::drawTextItem(const QPointF &p, const QTextItem &textItem ? QFontEngineGlyphCache::Type(ti.fontEngine->glyphFormat) : d->glyphCacheType; + if (d->inRenderText) + glyphType = QFontEngineGlyphCache::Raster_A8; + if (glyphType == QFontEngineGlyphCache::Raster_RGBMask && state()->composition_mode != QPainter::CompositionMode_Source && state()->composition_mode != QPainter::CompositionMode_SourceOver) @@ -1334,6 +1424,9 @@ void QGL2PaintEngineExPrivate::drawCachedGlyphs(const QPointF &p, QFontEngineGly QBrush pensBrush = q->state()->pen.brush(); setBrush(&pensBrush); + if (inRenderText) + prepareDepthRangeForRenderText(); + if (glyphType == QFontEngineGlyphCache::Raster_RGBMask) { // Subpixel antialiasing without gamma correction @@ -1386,10 +1479,6 @@ void QGL2PaintEngineExPrivate::drawCachedGlyphs(const QPointF &p, QFontEngineGly glBindTexture(GL_TEXTURE_2D, cache->texture()); updateTextureFilter(GL_TEXTURE_2D, GL_REPEAT, false); -#ifndef QT_OPENGL_ES_2 - if (inRenderText) - shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::Depth), zValueForRenderText()); -#endif shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::MaskTexture), QT_MASK_TEXTURE_UNIT); glDrawArrays(GL_TRIANGLES, 0, 6 * glyphs.size()); @@ -1420,12 +1509,11 @@ void QGL2PaintEngineExPrivate::drawCachedGlyphs(const QPointF &p, QFontEngineGly glBindTexture(GL_TEXTURE_2D, cache->texture()); updateTextureFilter(GL_TEXTURE_2D, GL_REPEAT, false); -#ifndef QT_OPENGL_ES_2 - if (inRenderText) - shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::Depth), zValueForRenderText()); -#endif shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::MaskTexture), QT_MASK_TEXTURE_UNIT); glDrawArrays(GL_TRIANGLES, 0, 6 * glyphs.size()); + + if (inRenderText) + restoreDepthRangeForRenderText(); } void QGL2PaintEngineEx::drawPixmaps(const QDrawPixmaps::Data *drawingData, int dataCount, const QPixmap &pixmap, QDrawPixmaps::DrawingHints hints) @@ -1543,8 +1631,6 @@ bool QGL2PaintEngineEx::begin(QPaintDevice *pdev) d->brushUniformsDirty = true; d->matrixDirty = true; d->compositionModeDirty = true; - d->simpleShaderDepthUniformDirty = true; - d->depthUniformDirty = true; d->opacityUniformDirty = true; d->needsSync = true; d->use_system_clip = !systemClip().isEmpty(); @@ -1565,10 +1651,9 @@ bool QGL2PaintEngineEx::begin(QPaintDevice *pdev) d->shaderManager = new QGLEngineShaderManager(d->ctx); if (!d->inRenderText) { + glDisable(GL_STENCIL_TEST); glDisable(GL_DEPTH_TEST); glDisable(GL_SCISSOR_TEST); - glDepthFunc(GL_LESS); - glDepthMask(false); } #if !defined(QT_OPENGL_ES_2) @@ -1632,22 +1717,26 @@ void QGL2PaintEngineEx::ensureActive() if (d->needsSync) { d->transferMode(BrushDrawingMode); glViewport(0, 0, d->width, d->height); - glDepthMask(false); - glDepthFunc(GL_LESS); d->needsSync = false; + d->shaderManager->setDirty(); setState(state()); } } -void QGL2PaintEngineExPrivate::updateDepthScissorTest() +void QGL2PaintEngineExPrivate::updateClipScissorTest() { Q_Q(QGL2PaintEngineEx); - if (q->state()->depthTestEnabled) - glEnable(GL_DEPTH_TEST); - else - glDisable(GL_DEPTH_TEST); + if (q->state()->clipTestEnabled) { + glEnable(GL_STENCIL_TEST); + glStencilFunc(GL_LEQUAL, q->state()->currentClip, ~GL_STENCIL_HIGH_BIT); + } else { + glDisable(GL_STENCIL_TEST); + glStencilFunc(GL_ALWAYS, 0, 0xffff); + } -#ifndef QT_GL_NO_SCISSOR_TEST +#ifdef QT_GL_NO_SCISSOR_TEST + currentScissorBounds = QRect(0, 0, width, height); +#else QRect bounds = q->state()->rectangleClip; if (!q->state()->clipEnabled) { if (use_system_clip) @@ -1686,69 +1775,83 @@ void QGL2PaintEngineEx::clipEnabledChanged() { Q_D(QGL2PaintEngineEx); - d->simpleShaderDepthUniformDirty = true; - d->depthUniformDirty = true; + state()->clipChanged = true; - if (painter()->hasClipping()) { - d->regenerateDepthClip(); - } else { - if (d->use_system_clip) { - state()->currentDepth = 0; - } else { - state()->depthTestEnabled = false; - } + if (painter()->hasClipping()) + d->regenerateClip(); + else + d->systemStateChanged(); +} - d->updateDepthScissorTest(); - } +void QGL2PaintEngineExPrivate::clearClip(uint value) +{ + dirtyStencilRegion -= currentScissorBounds; + + glStencilMask(0xffff); + glClearStencil(value); + glClear(GL_STENCIL_BUFFER_BIT); + glStencilMask(0x0); + + q->state()->needsClipBufferClear = false; } -void QGL2PaintEngineExPrivate::writeClip(const QVectorPath &path, uint depth) +void QGL2PaintEngineExPrivate::writeClip(const QVectorPath &path, uint value) { transferMode(BrushDrawingMode); if (matrixDirty) updateMatrix(); - if (q->state()->needsDepthBufferClear) { - glDepthMask(true); - glClearDepth(rawDepth(2)); - glClear(GL_DEPTH_BUFFER_BIT); - q->state()->needsDepthBufferClear = false; - glDepthMask(false); - } - if (path.isEmpty()) + const bool singlePass = !path.hasWindingFill() + && (((q->state()->currentClip == maxClip - 1) && q->state()->clipTestEnabled) + || q->state()->needsClipBufferClear); + const uint referenceClipValue = q->state()->needsClipBufferClear ? 1 : q->state()->currentClip; + + if (q->state()->needsClipBufferClear) + clearClip(1); + + if (path.isEmpty()) { + glEnable(GL_STENCIL_TEST); + glStencilFunc(GL_LEQUAL, value, ~GL_STENCIL_HIGH_BIT); return; + } - glDisable(GL_BLEND); - glDepthMask(false); + if (q->state()->clipTestEnabled) + glStencilFunc(GL_LEQUAL, q->state()->currentClip, ~GL_STENCIL_HIGH_BIT); + else + glStencilFunc(GL_ALWAYS, 0, 0xffff); vertexCoordinateArray.clear(); vertexCoordinateArray.addPath(path, inverseScale); - glDepthMask(GL_FALSE); - fillStencilWithVertexArray(vertexCoordinateArray, path.hasWindingFill()); + if (!singlePass) + fillStencilWithVertexArray(vertexCoordinateArray, path.hasWindingFill()); - // Stencil the clip onto the clip buffer glColorMask(false, false, false, false); - glDepthMask(true); + glEnable(GL_STENCIL_TEST); + useSimpleShader(); - shaderManager->simpleProgram()->setUniformValue("depth", normalizedDeviceDepth(depth)); - simpleShaderDepthUniformDirty = true; + if (singlePass) { + // Under these conditions we can set the new stencil value in a single + // pass, by using the current value and the "new value" as the toggles - glEnable(GL_DEPTH_TEST); - glDepthFunc(GL_ALWAYS); + glStencilFunc(GL_LEQUAL, referenceClipValue, ~GL_STENCIL_HIGH_BIT); + glStencilOp(GL_KEEP, GL_INVERT, GL_INVERT); + glStencilMask(value ^ referenceClipValue); - glStencilFunc(GL_NOTEQUAL, 0, 0xFFFF); // Pass if stencil buff value != 0 - glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); + drawVertexArrays(vertexCoordinateArray, GL_TRIANGLE_FAN); + } else { + glStencilFunc(GL_NOTEQUAL, value, GL_STENCIL_HIGH_BIT); + glStencilOp(GL_KEEP, GL_REPLACE, GL_REPLACE); + glStencilMask(0xffff); - glEnable(GL_STENCIL_TEST); - composite(vertexCoordinateArray.boundingRect()); - glDisable(GL_STENCIL_TEST); + composite(vertexCoordinateArray.boundingRect()); + } + glStencilFunc(GL_LEQUAL, value, ~GL_STENCIL_HIGH_BIT); glStencilMask(0); glColorMask(true, true, true, true); - glDepthMask(false); } void QGL2PaintEngineEx::clip(const QVectorPath &path, Qt::ClipOperation op) @@ -1756,6 +1859,8 @@ void QGL2PaintEngineEx::clip(const QVectorPath &path, Qt::ClipOperation op) // qDebug("QGL2PaintEngineEx::clip()"); Q_D(QGL2PaintEngineEx); + state()->clipChanged = true; + ensureActive(); if (op == Qt::ReplaceClip) { @@ -1773,7 +1878,7 @@ void QGL2PaintEngineEx::clip(const QVectorPath &path, Qt::ClipOperation op) if (state()->matrix.type() <= QTransform::TxScale) { state()->rectangleClip = state()->rectangleClip.intersected(state()->matrix.mapRect(rect).toRect()); - d->updateDepthScissorTest(); + d->updateClipScissorTest(); return; } } @@ -1784,43 +1889,44 @@ void QGL2PaintEngineEx::clip(const QVectorPath &path, Qt::ClipOperation op) switch (op) { case Qt::NoClip: if (d->use_system_clip) { - state()->depthTestEnabled = true; - state()->currentDepth = 0; + state()->clipTestEnabled = true; + state()->currentClip = 1; } else { - state()->depthTestEnabled = false; + state()->clipTestEnabled = false; } state()->rectangleClip = QRect(0, 0, d->width, d->height); state()->canRestoreClip = false; - d->updateDepthScissorTest(); + d->updateClipScissorTest(); break; case Qt::IntersectClip: state()->rectangleClip = state()->rectangleClip.intersected(pathRect); - d->updateDepthScissorTest(); - ++state()->maxDepth; - d->writeClip(path, state()->maxDepth); - state()->currentDepth = state()->maxDepth - 1; - state()->depthTestEnabled = true; + d->updateClipScissorTest(); + d->resetClipIfNeeded(); + ++d->maxClip; + d->writeClip(path, d->maxClip); + state()->currentClip = d->maxClip; + state()->clipTestEnabled = true; break; case Qt::UniteClip: { -#ifndef QT_GL_NO_SCISSOR_TEST + d->resetClipIfNeeded(); + ++d->maxClip; if (state()->rectangleClip.isValid()) { - ++state()->maxDepth; - QPainterPath path; path.addRect(state()->rectangleClip); // flush the existing clip rectangle to the depth buffer - d->writeClip(qtVectorPathForPath(state()->matrix.inverted().map(path)), state()->maxDepth); + d->writeClip(qtVectorPathForPath(state()->matrix.inverted().map(path)), d->maxClip); } + state()->clipTestEnabled = false; +#ifndef QT_GL_NO_SCISSOR_TEST QRect oldRectangleClip = state()->rectangleClip; state()->rectangleClip = state()->rectangleClip.united(pathRect); - d->updateDepthScissorTest(); + d->updateClipScissorTest(); QRegion extendRegion = QRegion(state()->rectangleClip) - oldRectangleClip; - glDepthFunc(GL_ALWAYS); if (!extendRegion.isEmpty()) { QPainterPath extendPath; extendPath.addRegion(extendRegion); @@ -1829,27 +1935,19 @@ void QGL2PaintEngineEx::clip(const QVectorPath &path, Qt::ClipOperation op) d->writeClip(qtVectorPathForPath(state()->matrix.inverted().map(extendPath)), 0); } #endif - glDepthFunc(GL_ALWAYS); // now write the clip path - d->writeClip(path, state()->maxDepth); + d->writeClip(path, d->maxClip); state()->canRestoreClip = false; - state()->currentDepth = state()->maxDepth - 1; - state()->depthTestEnabled = true; + state()->currentClip = d->maxClip; + state()->clipTestEnabled = true; break; } default: break; } - - glDepthFunc(GL_LESS); - if (state()->depthTestEnabled) { - glEnable(GL_DEPTH_TEST); - d->simpleShaderDepthUniformDirty = true; - d->depthUniformDirty = true; - } } -void QGL2PaintEngineExPrivate::regenerateDepthClip() +void QGL2PaintEngineExPrivate::regenerateClip() { systemStateChanged(); replayClipOperations(); @@ -1859,6 +1957,8 @@ void QGL2PaintEngineExPrivate::systemStateChanged() { Q_Q(QGL2PaintEngineEx); + q->state()->clipChanged = true; + if (systemClip.isEmpty()) { use_system_clip = false; } else { @@ -1870,46 +1970,34 @@ void QGL2PaintEngineExPrivate::systemStateChanged() } } - q->state()->depthTestEnabled = false; - q->state()->needsDepthBufferClear = true; + q->state()->clipTestEnabled = false; + q->state()->needsClipBufferClear = true; - q->state()->currentDepth = 1; - q->state()->maxDepth = 4; + q->state()->currentClip = 1; + maxClip = 1; q->state()->rectangleClip = use_system_clip ? systemClip.boundingRect() : QRect(0, 0, width, height); - updateDepthScissorTest(); + updateClipScissorTest(); - if (use_system_clip) { + if (systemClip.numRects() == 1) { + if (systemClip.boundingRect() == QRect(0, 0, width, height)) + use_system_clip = false; #ifndef QT_GL_NO_SCISSOR_TEST - if (systemClip.numRects() == 1) { - if (q->state()->rectangleClip == QRect(0, 0, width, height)) { - use_system_clip = false; - } else { - simpleShaderDepthUniformDirty = true; - depthUniformDirty = true; - } - return; - } + // scissoring takes care of the system clip + return; #endif - q->state()->needsDepthBufferClear = false; - - glDepthMask(true); + } - glClearDepth(0); - glClear(GL_DEPTH_BUFFER_BIT); + if (use_system_clip) { + clearClip(0); QPainterPath path; path.addRegion(systemClip); - glDepthFunc(GL_ALWAYS); - writeClip(qtVectorPathForPath(q->state()->matrix.inverted().map(path)), 2); - glDepthFunc(GL_LESS); - - glEnable(GL_DEPTH_TEST); - q->state()->depthTestEnabled = true; - - simpleShaderDepthUniformDirty = true; - depthUniformDirty = true; + q->state()->currentClip = 0; + writeClip(qtVectorPathForPath(q->state()->matrix.inverted().map(path)), 1); + q->state()->currentClip = 1; + q->state()->clipTestEnabled = true; } } @@ -1929,25 +2017,28 @@ void QGL2PaintEngineEx::setState(QPainterState *new_state) return; } - renderHintsChanged(); + if (old_state == s || s->renderHintsChanged) + renderHintsChanged(); - d->matrixDirty = true; - d->compositionModeDirty = true; - d->simpleShaderDepthUniformDirty = true; - d->depthUniformDirty = true; - d->simpleShaderMatrixUniformDirty = true; - d->shaderMatrixUniformDirty = true; - d->opacityUniformDirty = true; + if (old_state == s || s->matrixChanged) { + d->matrixDirty = true; + d->simpleShaderMatrixUniformDirty = true; + d->shaderMatrixUniformDirty = true; + } - d->shaderManager->setDirty(); + if (old_state == s || s->compositionModeChanged) + d->compositionModeDirty = true; - if (old_state && old_state != s && old_state->canRestoreClip) { - d->updateDepthScissorTest(); - glDepthMask(false); - glDepthFunc(GL_LESS); - s->maxDepth = old_state->maxDepth; - } else { - d->regenerateDepthClip(); + if (old_state == s || s->opacityChanged) + d->opacityUniformDirty = true; + + if (old_state == s || s->clipChanged) { + if (old_state && old_state != s && old_state->canRestoreClip) { + d->updateClipScissorTest(); + glDepthFunc(GL_LEQUAL); + } else { + d->regenerateClip(); + } } } @@ -1964,6 +2055,12 @@ QPainterState *QGL2PaintEngineEx::createState(QPainterState *orig) const else s = new QOpenGL2PaintEngineState(*static_cast<QOpenGL2PaintEngineState *>(orig)); + s->matrixChanged = false; + s->compositionModeChanged = false; + s->opacityChanged = false; + s->renderHintsChanged = false; + s->clipChanged = false; + d->last_created_state = s; return s; } @@ -1977,21 +2074,17 @@ void QGL2PaintEngineEx::setRenderTextActive(bool active) QOpenGL2PaintEngineState::QOpenGL2PaintEngineState(QOpenGL2PaintEngineState &other) : QPainterState(other) { - needsDepthBufferClear = other.needsDepthBufferClear; - depthTestEnabled = other.depthTestEnabled; - scissorTestEnabled = other.scissorTestEnabled; - currentDepth = other.currentDepth; - maxDepth = other.maxDepth; + needsClipBufferClear = other.needsClipBufferClear; + clipTestEnabled = other.clipTestEnabled; + currentClip = other.currentClip; canRestoreClip = other.canRestoreClip; rectangleClip = other.rectangleClip; } QOpenGL2PaintEngineState::QOpenGL2PaintEngineState() { - needsDepthBufferClear = true; - depthTestEnabled = false; - currentDepth = 1; - maxDepth = 4; + needsClipBufferClear = true; + clipTestEnabled = false; canRestoreClip = true; } diff --git a/src/opengl/gl2paintengineex/qpaintengineex_opengl2_p.h b/src/opengl/gl2paintengineex/qpaintengineex_opengl2_p.h index 4f42082..28c972e 100644 --- a/src/opengl/gl2paintengineex/qpaintengineex_opengl2_p.h +++ b/src/opengl/gl2paintengineex/qpaintengineex_opengl2_p.h @@ -82,17 +82,17 @@ public: QOpenGL2PaintEngineState(); ~QOpenGL2PaintEngineState(); - bool needsDepthBufferClear; - qreal depthBufferClearValue; + uint needsClipBufferClear : 1; + uint clipTestEnabled : 1; + uint canRestoreClip : 1; + uint matrixChanged : 1; + uint compositionModeChanged : 1; + uint opacityChanged : 1; + uint renderHintsChanged : 1; + uint clipChanged : 1; + uint currentClip : 8; - bool depthTestEnabled; - bool scissorTestEnabled; - uint maxDepth; - uint currentDepth; - - bool canRestoreClip; QRect rectangleClip; - bool hasRectangleClip; }; class Q_OPENGL_EXPORT QGL2PaintEngineEx : public QPaintEngineEx @@ -200,7 +200,8 @@ public: inline void useSimpleShader(); - float zValueForRenderText() const; + void prepareDepthRangeForRenderText(); + void restoreDepthRangeForRenderText(); static QGLEngineShaderManager* shaderManagerForEngine(QGL2PaintEngineEx *engine) { return engine->d_func()->shaderManager; } @@ -220,12 +221,11 @@ public: bool brushUniformsDirty; bool simpleShaderMatrixUniformDirty; bool shaderMatrixUniformDirty; - bool depthUniformDirty; - bool simpleShaderDepthUniformDirty; bool opacityUniformDirty; QRegion dirtyStencilRegion; QRect currentScissorBounds; + uint maxClip; const QBrush* currentBrush; // May not be the state's brush! @@ -242,26 +242,16 @@ public: QGLEngineShaderManager* shaderManager; - void writeClip(const QVectorPath &path, uint depth); - void updateDepthScissorTest(); + void clearClip(uint value); + void writeClip(const QVectorPath &path, uint value); + void resetClipIfNeeded(); + + void updateClipScissorTest(); void setScissor(const QRect &rect); - void regenerateDepthClip(); + void regenerateClip(); void systemStateChanged(); uint use_system_clip : 1; - static inline GLfloat rawDepth(uint depth) - { - // assume at least 16 bits in the depth buffer, and - // use 2^15 depth levels to be safe with regard to - // rounding issues etc - return depth * (1.0f / GLfloat((1 << 15) - 1)); - } - - static inline GLfloat normalizedDeviceDepth(uint depth) - { - return 2.0f * rawDepth(depth) - 1.0f; - } - uint location(QGLEngineShaderManager::Uniform uniform) { return shaderManager->getUniformLocation(uniform); @@ -272,12 +262,16 @@ public: bool needsSync; bool inRenderText; + GLfloat depthRange[2]; + float textureInvertedY; QScopedPointer<QPixmapFilter> convolutionFilter; QScopedPointer<QPixmapFilter> colorizeFilter; QScopedPointer<QPixmapFilter> blurFilter; QScopedPointer<QPixmapFilter> fastBlurFilter; + QScopedPointer<QPixmapFilter> dropShadowFilter; + QScopedPointer<QPixmapFilter> fastDropShadowFilter; }; QT_END_NAMESPACE diff --git a/src/opengl/qgl.cpp b/src/opengl/qgl.cpp index 2327d7a..3f96d1c 100644 --- a/src/opengl/qgl.cpp +++ b/src/opengl/qgl.cpp @@ -1393,11 +1393,39 @@ bool operator!=(const QGLFormat& a, const QGLFormat& b) /***************************************************************************** QGLContext implementation *****************************************************************************/ -QGLContextPrivate::~QGLContextPrivate() + +QGLContextGroup::~QGLContextGroup() +{ + // Clear any remaining QGLSharedResourceGuard objects on the group. + QGLSharedResourceGuard *guard = m_guards; + while (guard != 0) { + guard->m_group = 0; + guard->m_id = 0; + guard = guard->m_next; + } +} + +void QGLContextGroup::addGuard(QGLSharedResourceGuard *guard) +{ + if (m_guards) + m_guards->m_prev = guard; + guard->m_next = m_guards; + guard->m_prev = 0; + m_guards = guard; +} + +void QGLContextGroup::removeGuard(QGLSharedResourceGuard *guard) { - if (!reference->deref()) - delete reference; + if (guard->m_next) + guard->m_next->m_prev = guard->m_prev; + if (guard->m_prev) + guard->m_prev->m_next = guard->m_next; + else + m_guards = guard->m_next; +} +QGLContextPrivate::~QGLContextPrivate() +{ if (!group->m_refs.deref()) { Q_ASSERT(group->context() == q_ptr); delete group; @@ -1626,8 +1654,10 @@ void QGLTextureCache::pixmapCleanupHook(QPixmap* pixmap) } #if defined(Q_WS_X11) QPixmapData *pd = pixmap->data_ptr().data(); - Q_ASSERT(pd->ref == 1); // Make sure reference counting isn't broken - QGLContextPrivate::destroyGlSurfaceForPixmap(pd); + if (pd->classId() == QPixmapData::X11Class) { + Q_ASSERT(pd->ref == 1); // Make sure reference counting isn't broken + QGLContextPrivate::destroyGlSurfaceForPixmap(pd); + } #endif } @@ -1806,6 +1836,8 @@ QGLContext::~QGLContext() QGLTextureCache::instance()->removeContextTextures(this); QGLTextureCache::deleteIfEmpty(); // ### thread safety + d_ptr->group->cleanupResources(this); + QGLSignalProxy::instance()->emitAboutToDestroyContext(this); reset(); } @@ -3751,6 +3783,11 @@ bool QGLWidget::event(QEvent *e) glFinish(); doneCurrent(); } else if (e->type() == QEvent::ParentChange) { + // if we've reparented a window that has the current context + // bound, we need to rebind that context to the new window id + if (d->glcx == QGLContext::currentContext()) + makeCurrent(); + if (d->glcx->d_func()->screen != d->xinfo.screen() || testAttribute(Qt::WA_TranslucentBackground)) { setContext(new QGLContext(d->glcx->requestedFormat(), this)); // ### recreating the overlay isn't supported atm @@ -4908,16 +4945,15 @@ void QGLShareRegister::removeShare(const QGLContext *context) { group->m_shares.clear(); } -QGLContextResource::QGLContextResource(FreeFunc f, QObject *parent) - : QObject(parent), free(f) +QGLContextResource::QGLContextResource(FreeFunc f) + : free(f), active(0) { - connect(QGLSignalProxy::instance(), SIGNAL(aboutToDestroyContext(const QGLContext *)), this, SLOT(removeOne(const QGLContext *))); } QGLContextResource::~QGLContextResource() { #ifndef QT_NO_DEBUG - if (m_resources.size()) { + if (active != 0) { qWarning("QtOpenGL: Resources are still available at program shutdown.\n" " This is possibly caused by a leaked QGLWidget, \n" " QGLFramebufferObject or QGLPixelBuffer."); @@ -4927,121 +4963,53 @@ QGLContextResource::~QGLContextResource() void QGLContextResource::insert(const QGLContext *key, void *value) { - QList<const QGLContext *> shares = qgl_share_reg()->shares(key); - if (shares.size() == 0) - shares.append(key); - void *oldValue = 0; - for (int i = 0; i < shares.size(); ++i) { - ResourceHash::iterator it = m_resources.find(shares.at(i)); - if (it != m_resources.end()) { - Q_ASSERT(oldValue == 0 || oldValue == it.value()); - oldValue = it.value(); - it.value() = value; - } else { - m_resources.insert(shares.at(i), value); - } - } - if (oldValue != 0 && oldValue != value) { - QGLContext *oldContext = const_cast<QGLContext *>(QGLContext::currentContext()); - if (oldContext != key) - const_cast<QGLContext *>(key)->makeCurrent(); - free(oldValue); - if (oldContext && oldContext != key) - oldContext->makeCurrent(); - } + QGLContextGroup *group = QGLContextPrivate::contextGroup(key); + Q_ASSERT(!group->m_resources.contains(this)); + group->m_resources.insert(this, value); + active.ref(); } void *QGLContextResource::value(const QGLContext *key) { - ResourceHash::const_iterator it = m_resources.find(key); - // Check if there is a value associated with 'key'. - if (it != m_resources.end()) - return it.value(); - // Check if there is a value associated with sharing contexts. - QList<const QGLContext *> shares = qgl_share_reg()->shares(key); - for (int i = 0; i < shares.size() && it == m_resources.end(); ++i) - it = m_resources.find(shares.at(i)); - if (it == m_resources.end()) - return 0; // Didn't find anything. - - // Found something! Share this info with all the buddies. - for (int i = 0; i < shares.size(); ++i) - m_resources.insert(shares.at(i), it.value()); - return it.value(); -} - -void QGLContextResource::removeGroup(const QGLContext *key) -{ - QList<const QGLContext *> shares = qgl_share_reg()->shares(key); - if (shares.size() == 0) - shares.append(key); - void *oldValue = 0; - for (int i = 0; i < shares.size(); ++i) { - ResourceHash::iterator it = m_resources.find(shares.at(i)); - if (it != m_resources.end()) { - Q_ASSERT(oldValue == 0 || oldValue == it.value()); - oldValue = it.value(); - m_resources.erase(it); - } - } - if (oldValue != 0) { - QGLContext *oldContext = const_cast<QGLContext *>(QGLContext::currentContext()); - if (oldContext != key) - const_cast<QGLContext *>(key)->makeCurrent(); - free(oldValue); - if (oldContext && oldContext != key) - oldContext->makeCurrent(); - } + QGLContextGroup *group = QGLContextPrivate::contextGroup(key); + return group->m_resources.value(this, 0); } -void QGLContextResource::removeOne(const QGLContext *key) +void QGLContextResource::cleanup(const QGLContext *ctx, void *value) { - ResourceHash::iterator it = m_resources.find(key); - if (it == m_resources.end()) + QGLShareContextScope scope(ctx); + free(value); + active.deref(); +} + +void QGLContextGroup::cleanupResources(const QGLContext *ctx) +{ + // If there are still shares, then no cleanup to be done yet. + if (m_shares.size() > 1) return; - QList<const QGLContext *> shares = qgl_share_reg()->shares(key); - if (shares.size() > 1) { - Q_ASSERT(key->isSharing()); - // At least one of the shared contexts must stay in the cache. - // Otherwise, the value pointer is lost. - for (int i = 0; i < 2/*shares.size()*/; ++i) - m_resources.insert(shares.at(i), it.value()); - } else { - QGLContext *oldContext = const_cast<QGLContext *>(QGLContext::currentContext()); - if (oldContext != key) - const_cast<QGLContext *>(key)->makeCurrent(); - free(it.value()); - if (oldContext && oldContext != key) - oldContext->makeCurrent(); - } - m_resources.erase(it); + // Iterate over all resources and free each in turn. + QHash<QGLContextResource *, void *>::ConstIterator it; + for (it = m_resources.begin(); it != m_resources.end(); ++it) + it.key()->cleanup(ctx, it.value()); } -QGLContextReference::QGLContextReference(const QGLContext *ctx) - : m_ref(1), m_ctx(ctx) +QGLSharedResourceGuard::~QGLSharedResourceGuard() { - connect(QGLSignalProxy::instance(), - SIGNAL(aboutToDestroyContext(const QGLContext *)), - this, SLOT(aboutToDestroyContext(const QGLContext *))); + if (m_group) + m_group->removeGuard(this); } -void QGLContextReference::aboutToDestroyContext(const QGLContext *ctx) +void QGLSharedResourceGuard::setContext(const QGLContext *context) { - // Bail out if our context is not being destroyed. - if (ctx != m_ctx || !m_ctx) - return; - - // Find some other context that this one is shared with to take over. - QList<const QGLContext *> shares = qgl_share_reg()->shares(m_ctx); - shares.removeAll(m_ctx); - if (!shares.isEmpty()) { - m_ctx = shares[0]; - return; + if (m_group) + m_group->removeGuard(this); + if (context) { + m_group = QGLContextPrivate::contextGroup(context); + m_group->addGuard(this); + } else { + m_group = 0; } - - // No more contexts sharing with this one, so the reference is now invalid. - m_ctx = 0; } QT_END_NAMESPACE diff --git a/src/opengl/qgl_p.h b/src/opengl/qgl_p.h index 1957429..8d4f673 100644 --- a/src/opengl/qgl_p.h +++ b/src/opengl/qgl_p.h @@ -219,6 +219,9 @@ public: #endif }; +class QGLContextResource; +class QGLSharedResourceGuard; + // QGLContextPrivate has the responsibility of creating context groups. // QGLContextPrivate and QGLShareRegister will both maintain the reference counter and destroy // context groups when needed. @@ -226,42 +229,29 @@ public: class QGLContextGroup { public: + ~QGLContextGroup(); + QGLExtensionFuncs &extensionFuncs() {return m_extensionFuncs;} const QGLContext *context() const {return m_context;} + + void addGuard(QGLSharedResourceGuard *guard); + void removeGuard(QGLSharedResourceGuard *guard); private: - QGLContextGroup(const QGLContext *context) : m_context(context), m_refs(1) { } + QGLContextGroup(const QGLContext *context) : m_context(context), m_guards(0), m_refs(1) { } QGLExtensionFuncs m_extensionFuncs; const QGLContext *m_context; // context group's representative QList<const QGLContext *> m_shares; + QHash<QGLContextResource *, void *> m_resources; + QGLSharedResourceGuard *m_guards; // double-linked list of active guards. QAtomicInt m_refs; + void cleanupResources(const QGLContext *ctx); + friend class QGLShareRegister; + friend class QGLContext; friend class QGLContextPrivate; -}; - -// Reference to a QGLContext which automatically switches to another -// shared context when the main one is destroyed. If there is no -// shared context to switch to, the context pointer is set to null. -// Note: should be merged into QGLContextGroup at some point. -class QGLContextReference : public QObject -{ - Q_OBJECT -public: - QGLContextReference(const QGLContext *ctx); - ~QGLContextReference() {} - - const QGLContext *context() const { return m_ctx; } - - void ref() { m_ref.ref(); } - bool deref() { return m_ref.deref(); } - -private slots: - void aboutToDestroyContext(const QGLContext *ctx); - -private: - QAtomicInt m_ref; - const QGLContext *m_ctx; + friend class QGLContextResource; }; class QGLTexture; @@ -270,7 +260,7 @@ class QGLContextPrivate { Q_DECLARE_PUBLIC(QGLContext) public: - explicit QGLContextPrivate(QGLContext *context) : internal_context(false), q_ptr(context) {reference = new QGLContextReference(context); group = new QGLContextGroup(context);} + explicit QGLContextPrivate(QGLContext *context) : internal_context(false), q_ptr(context) {group = new QGLContextGroup(context);} ~QGLContextPrivate(); QGLTexture *bindTexture(const QImage &image, GLenum target, GLint format, QGLContext::BindOptions options); @@ -333,7 +323,6 @@ public: QGLContext *q_ptr; QGLFormat::OpenGLVersionFlags version_flags; - QGLContextReference *reference; QGLContextGroup *group; GLint max_texture_size; @@ -411,6 +400,46 @@ public: extern Q_OPENGL_EXPORT QGLShareRegister* qgl_share_reg(); +// Temporarily make a context current if not already current or +// shared with the current contex. The previous context is made +// current when the object goes out of scope. +class Q_OPENGL_EXPORT QGLShareContextScope +{ +public: + QGLShareContextScope(const QGLContext *ctx) + : m_oldContext(0) + { + QGLContext *currentContext = const_cast<QGLContext *>(QGLContext::currentContext()); + if (currentContext != ctx && !QGLContext::areSharing(ctx, currentContext)) { + m_oldContext = currentContext; + m_ctx = const_cast<QGLContext *>(ctx); + m_ctx->makeCurrent(); + } else { + m_ctx = currentContext; + } + } + + operator QGLContext *() + { + return m_ctx; + } + + QGLContext *operator->() + { + return m_ctx; + } + + ~QGLShareContextScope() + { + if (m_oldContext) + m_oldContext->makeCurrent(); + } + +private: + QGLContext *m_oldContext; + QGLContext *m_ctx; +}; + class QGLTexture { public: QGLTexture(QGLContext *ctx = 0, GLuint tx_id = 0, GLenum tx_target = GL_TEXTURE_2D, @@ -426,12 +455,8 @@ public: ~QGLTexture() { if (options & QGLContext::MemoryManagedBindOption) { - QGLContext *current = const_cast<QGLContext *>(QGLContext::currentContext()); - QGLContext *ctx = const_cast<QGLContext *>(context); - Q_ASSERT(ctx); - bool switch_context = current != ctx && !QGLContext::areSharing(current, ctx); - if (switch_context) - ctx->makeCurrent(); + Q_ASSERT(context); + QGLShareContextScope scope(context); #if defined(Q_WS_X11) // Although glXReleaseTexImage is a glX call, it must be called while there // is a current context - the context the pixmap was bound to a texture in. @@ -441,8 +466,6 @@ public: QGLContextPrivate::unbindPixmapFromTexture(boundPixmap); #endif glDeleteTextures(1, &id); - if (switch_context && current) - current->makeCurrent(); } } @@ -506,66 +529,21 @@ inline GLenum qt_gl_preferredTextureTarget() } // One resource per group of shared contexts. -class QGLContextResource : public QObject +class Q_AUTOTEST_EXPORT QGLContextResource { - Q_OBJECT public: typedef void (*FreeFunc)(void *); - QGLContextResource(FreeFunc f, QObject *parent = 0); + QGLContextResource(FreeFunc f); ~QGLContextResource(); // Set resource 'value' for 'key' and all its shared contexts. void insert(const QGLContext *key, void *value); // Return resource for 'key' or a shared context. void *value(const QGLContext *key); - // Free resource for 'key' and all its shared contexts. - void removeGroup(const QGLContext *key); -private slots: - // Remove entry 'key' from cache and delete resource if there are no shared contexts. - void removeOne(const QGLContext *key); + // Cleanup 'value' in response to a context group being destroyed. + void cleanup(const QGLContext *ctx, void *value); private: - typedef QHash<const QGLContext *, void *> ResourceHash; - ResourceHash m_resources; FreeFunc free; -}; - -// Temporarily make a context current if not already current or -// shared with the current contex. The previous context is made -// current when the object goes out of scope. -class Q_OPENGL_EXPORT QGLShareContextScope -{ -public: - QGLShareContextScope(const QGLContext *ctx) - : m_oldContext(0) - { - QGLContext *currentContext = const_cast<QGLContext *>(QGLContext::currentContext()); - if (currentContext != ctx && !QGLContext::areSharing(ctx, currentContext)) { - m_oldContext = currentContext; - m_ctx = const_cast<QGLContext *>(ctx); - m_ctx->makeCurrent(); - } else { - m_ctx = currentContext; - } - } - - operator QGLContext *() - { - return m_ctx; - } - - QGLContext *operator->() - { - return m_ctx; - } - - ~QGLShareContextScope() - { - if (m_oldContext) - m_oldContext->makeCurrent(); - } - -private: - QGLContext *m_oldContext; - QGLContext *m_ctx; + QAtomicInt active; }; // Put a guard around a GL object identifier and its context. @@ -577,44 +555,27 @@ class Q_OPENGL_EXPORT QGLSharedResourceGuard { public: QGLSharedResourceGuard(const QGLContext *context) - : m_ctxref(0), m_id(0) + : m_group(0), m_id(0), m_next(0), m_prev(0) { setContext(context); } QGLSharedResourceGuard(const QGLContext *context, GLuint id) - : m_ctxref(0), m_id(id) + : m_group(0), m_id(id), m_next(0), m_prev(0) { setContext(context); } - ~QGLSharedResourceGuard() - { - if (m_ctxref && !m_ctxref->deref()) - delete m_ctxref; - } + ~QGLSharedResourceGuard(); const QGLContext *context() const { - return m_ctxref ? m_ctxref->context() : 0; + return m_group ? m_group->context() : 0; } - void setContext(const QGLContext *context) - { - if (m_ctxref && !m_ctxref->deref()) - delete m_ctxref; - if (context) { - m_ctxref = context->d_ptr->reference; - m_ctxref->ref(); - } else { - m_ctxref = 0; - } - } + void setContext(const QGLContext *context); GLuint id() const { - if (m_ctxref && m_ctxref->context()) - return m_id; - else - return 0; + return m_id; } void setId(GLuint id) @@ -623,8 +584,12 @@ public: } private: - QGLContextReference *m_ctxref; + QGLContextGroup *m_group; GLuint m_id; + QGLSharedResourceGuard *m_next; + QGLSharedResourceGuard *m_prev; + + friend class QGLContextGroup; }; QT_END_NAMESPACE diff --git a/src/opengl/qglpixmapfilter.cpp b/src/opengl/qglpixmapfilter.cpp index 1ae3866..0603369 100644 --- a/src/opengl/qglpixmapfilter.cpp +++ b/src/opengl/qglpixmapfilter.cpp @@ -104,11 +104,12 @@ public: void setUniforms(QGLShaderProgram *program); + static QByteArray generateGaussianShader(int radius, bool dropShadow = false); + protected: bool processGL(QPainter *painter, const QPointF &pos, const QPixmap &src, const QRectF &srcRect) const; private: - static QByteArray generateGaussianShader(int radius); mutable QSize m_textureSize; mutable bool m_horizontalBlur; @@ -118,6 +119,25 @@ private: mutable Qt::RenderHint m_hint; }; +class QGLPixmapDropShadowFilter : public QGLCustomShaderStage, public QGLPixmapFilter<QPixmapDropShadowFilter> +{ +public: + QGLPixmapDropShadowFilter(Qt::RenderHint hint); + + void setUniforms(QGLShaderProgram *program); + +protected: + bool processGL(QPainter *painter, const QPointF &pos, const QPixmap &src, const QRectF &srcRect) const; + +private: + mutable QSize m_textureSize; + mutable bool m_horizontalBlur; + + mutable bool m_haveCached; + mutable int m_cachedRadius; + mutable Qt::RenderHint m_hint; +}; + extern QGLWidget *qt_gl_share_widget(); QPixmapFilter *QGL2PaintEngineEx::pixmapFilter(int type, const QPixmapFilter *prototype) @@ -141,6 +161,18 @@ QPixmapFilter *QGL2PaintEngineEx::pixmapFilter(int type, const QPixmapFilter *pr return d->blurFilter.data(); } + case QPixmapFilter::DropShadowFilter: { + const QPixmapDropShadowFilter *proto = static_cast<const QPixmapDropShadowFilter *>(prototype); + if (proto->blurRadius() <= 5) { + if (!d->fastDropShadowFilter) + d->fastDropShadowFilter.reset(new QGLPixmapDropShadowFilter(Qt::PerformanceHint)); + return d->fastDropShadowFilter.data(); + } + if (!d->dropShadowFilter) + d->dropShadowFilter.reset(new QGLPixmapDropShadowFilter(Qt::QualityHint)); + return d->dropShadowFilter.data(); + } + case QPixmapFilter::ConvolutionFilter: if (!d->convolutionFilter) d->convolutionFilter.reset(new QGLPixmapConvolutionFilter); @@ -279,6 +311,20 @@ static const char *qt_gl_blur_filter_fast = " return color * (1.0 / float(samples));" "}"; +static const char *qt_gl_drop_shadow_filter_fast = + "const int samples = 9;" + "uniform mediump vec2 delta;" + "uniform mediump vec4 shadowColor;" + "lowp vec4 customShader(lowp sampler2D src, highp vec2 srcCoords) {" + " mediump vec4 color = vec4(0.0, 0.0, 0.0, 0.0);" + " mediump float offset = (float(samples) - 1.0) / 2.0;" + " for (int i = 0; i < samples; i++) {" + " mediump vec2 coord = srcCoords + delta * (offset - float(i)) / offset;" + " color += texture2D(src, coord).a * shadowColor;" + " }" + " return color * (1.0 / float(samples));" + "}"; + QGLPixmapBlurFilter::QGLPixmapBlurFilter(Qt::RenderHint hint) : m_haveCached(false) , m_cachedRadius(5) @@ -380,7 +426,7 @@ static inline qreal gaussian(qreal dx, qreal sigma) return exp(-dx * dx / (2 * sigma * sigma)) / (Q_2PI * sigma * sigma); } -QByteArray QGLPixmapBlurFilter::generateGaussianShader(int radius) +QByteArray QGLPixmapBlurFilter::generateGaussianShader(int radius, bool dropShadow) { Q_ASSERT(radius >= 1); @@ -388,6 +434,8 @@ QByteArray QGLPixmapBlurFilter::generateGaussianShader(int radius) source.reserve(1000); source.append("uniform highp vec2 delta;\n"); + if (dropShadow) + source.append("uniform mediump vec4 shadowColor;\n"); source.append("lowp vec4 customShader(lowp sampler2D src, highp vec2 srcCoords) {\n"); QVector<qreal> sampleOffsets; @@ -444,7 +492,10 @@ QByteArray QGLPixmapBlurFilter::generateGaussianShader(int radius) source.append(coordinate); source.append(";\n"); - source.append(" sample += texture2D(src, coord)"); + if (dropShadow) + source.append(" sample += texture2D(src, coord).a * shadowColor"); + else + source.append(" sample += texture2D(src, coord)"); weightSum += weights.at(i); if (weights.at(i) != qreal(1)) { @@ -463,4 +514,114 @@ QByteArray QGLPixmapBlurFilter::generateGaussianShader(int radius) return source; } +QGLPixmapDropShadowFilter::QGLPixmapDropShadowFilter(Qt::RenderHint hint) + : m_haveCached(false) + , m_cachedRadius(5) + , m_hint(hint) +{ + if (hint == Qt::PerformanceHint) { + QGLPixmapDropShadowFilter *filter = const_cast<QGLPixmapDropShadowFilter *>(this); + filter->setSource(qt_gl_drop_shadow_filter_fast); + m_haveCached = true; + } +} + +bool QGLPixmapDropShadowFilter::processGL(QPainter *painter, const QPointF &pos, const QPixmap &src, const QRectF &srcRect) const +{ + QGLPixmapDropShadowFilter *filter = const_cast<QGLPixmapDropShadowFilter *>(this); + + int radius = this->blurRadius(); + if (!m_haveCached || (m_hint == Qt::QualityHint && radius != m_cachedRadius)) { + // Only regenerate the shader from source if parameters have changed. + m_haveCached = true; + m_cachedRadius = radius; + filter->setSource(QGLPixmapBlurFilter::generateGaussianShader(radius, true)); + } + + QGLFramebufferObjectFormat format; + format.setInternalTextureFormat(GLenum(src.hasAlphaChannel() ? GL_RGBA : GL_RGB)); + QGLFramebufferObject *fbo = qgl_fbo_pool()->acquire(src.size(), format); + + if (!fbo) + return false; + + glBindTexture(GL_TEXTURE_2D, fbo->texture()); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glBindTexture(GL_TEXTURE_2D, 0); + + // prepare for updateUniforms + m_textureSize = src.size(); + + // horizontal pass, to pixmap + m_horizontalBlur = true; + + QPainter fboPainter(fbo); + + if (src.hasAlphaChannel()) { + glClearColor(0, 0, 0, 0); + glClear(GL_COLOR_BUFFER_BIT); + } + + // ensure GL_LINEAR filtering is used + fboPainter.setRenderHint(QPainter::SmoothPixmapTransform); + filter->setOnPainter(&fboPainter); + fboPainter.drawPixmap(0, 0, src); + filter->removeFromPainter(&fboPainter); + fboPainter.end(); + + QGL2PaintEngineEx *engine = static_cast<QGL2PaintEngineEx *>(painter->paintEngine()); + + // vertical pass, to painter + m_horizontalBlur = false; + + painter->save(); + // ensure GL_LINEAR filtering is used + painter->setRenderHint(QPainter::SmoothPixmapTransform); + filter->setOnPainter(painter); + QPointF ofs = offset(); + engine->drawTexture(src.rect().translated(pos.x() + ofs.x(), pos.y() + ofs.y()), fbo->texture(), fbo->size(), src.rect().translated(0, fbo->height() - src.height())); + filter->removeFromPainter(painter); + painter->restore(); + + qgl_fbo_pool()->release(fbo); + + // Now draw the actual pixmap over the top. + painter->drawPixmap(pos, src, srcRect); + + return true; +} + +void QGLPixmapDropShadowFilter::setUniforms(QGLShaderProgram *program) +{ + QColor col = color(); + if (m_horizontalBlur) { + program->setUniformValue("shadowColor", 1.0f, 1.0f, 1.0f, 1.0f); + } else { + qreal alpha = col.alphaF(); + program->setUniformValue("shadowColor", col.redF() * alpha, + col.greenF() * alpha, + col.blueF() * alpha, + alpha); + } + if (m_hint == Qt::QualityHint) { + if (m_horizontalBlur) + program->setUniformValue("delta", 1.0 / m_textureSize.width(), 0.0); + else + program->setUniformValue("delta", 0.0, 1.0 / m_textureSize.height()); + } else { + // 1.4 is chosen to most closely match the blurriness of the gaussian blur + // at low radii + qreal blur = blurRadius() / 1.4f; + + if (m_horizontalBlur) + program->setUniformValue("delta", blur / m_textureSize.width(), 0.0); + else + program->setUniformValue("delta", 0.0, blur / m_textureSize.height()); + } +} + QT_END_NAMESPACE diff --git a/src/opengl/qpaintengine_opengl.cpp b/src/opengl/qpaintengine_opengl.cpp index da490c0..3e4a8e7 100644 --- a/src/opengl/qpaintengine_opengl.cpp +++ b/src/opengl/qpaintengine_opengl.cpp @@ -2311,7 +2311,7 @@ void QOpenGLPaintEnginePrivate::updateDepthClip() void QOpenGLPaintEnginePrivate::systemStateChanged() { Q_Q(QOpenGLPaintEngine); - if (q->state()->hasClipping) + if (q->painter()->hasClipping()) q->updateClipRegion(q->painter()->clipRegion(), Qt::ReplaceClip); else q->updateClipRegion(QRegion(), Qt::NoClip); |