• Samuel Rødal's avatar
    Fix for current_fbo getting out of sync in QtOpenGL · a4428d48
    Samuel Rødal authored
    
    When using QGLWidget in combination with QOpenGLFramebufferObject from
    QtGui, instead of QGLFramebufferObject from QtOpenGL, the current_fbo
    variable doesn't get updated when framebuffer object bindings change.
    
    To ensure that the QGLWidget correctly releases the currently bound
    framebuffer object when using a QPainter, we keep track of whether
    QOpenGLFramebufferObject has modified the current FBO binding, and if
    that's the case we need to read the OpenGL state directly instead of
    relying on a cached value.
    
    Change-Id: If7e0bd936e202cad07365b5ce641ee01d2251930
    Reviewed-by: default avatarLaszlo Agocs <laszlo.agocs@digia.com>
    a4428d48
qglpaintdevice.cpp 7.18 KiB
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtOpenGL module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
** $QT_END_LICENSE$
****************************************************************************/
#include <private/qglpaintdevice_p.h>
#include <private/qgl_p.h>
#include <private/qglpixelbuffer_p.h>
#include <private/qglframebufferobject_p.h>
#include <qopenglfunctions.h>
#include <qwindow.h>
QT_BEGIN_NAMESPACE
QGLPaintDevice::QGLPaintDevice()
    : m_thisFBO(0)
QGLPaintDevice::~QGLPaintDevice()
int QGLPaintDevice::metric(QPaintDevice::PaintDeviceMetric metric) const
    switch(metric) {
    case PdmWidth:
        return size().width();
    case PdmHeight:
        return size().height();
    case PdmDepth: {
        const QGLFormat f = format();
        return f.redBufferSize() + f.greenBufferSize() + f.blueBufferSize() + f.alphaBufferSize();
    case PdmDevicePixelRatio:
        return 1;
    default:
        qWarning("QGLPaintDevice::metric() - metric %d not known", metric);
        return 0;
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
void QGLPaintDevice::beginPaint() { // Make sure our context is the current one: QGLContext *ctx = context(); ctx->makeCurrent(); ctx->d_func()->refreshCurrentFbo(); // Record the currently bound FBO so we can restore it again // in endPaint() and bind this device's FBO // // Note: m_thisFBO could be zero if the paint device is not // backed by an FBO (e.g. window back buffer). But there could // be a previous FBO bound to the context which we need to // explicitly unbind. Otherwise the painting will go into // the previous FBO instead of to the window. m_previousFBO = ctx->d_func()->current_fbo; if (m_previousFBO != m_thisFBO) { ctx->d_func()->setCurrentFbo(m_thisFBO); ctx->contextHandle()->functions()->glBindFramebuffer(GL_FRAMEBUFFER, m_thisFBO); } // Set the default fbo for the context to m_thisFBO so that // if some raw GL code between beginNativePainting() and // endNativePainting() calls QGLFramebufferObject::release(), // painting will revert to the window surface's fbo. ctx->d_ptr->default_fbo = m_thisFBO; } void QGLPaintDevice::ensureActiveTarget() { QGLContext* ctx = context(); if (ctx != QGLContext::currentContext()) ctx->makeCurrent(); ctx->d_func()->refreshCurrentFbo(); if (ctx->d_ptr->current_fbo != m_thisFBO) { ctx->d_func()->setCurrentFbo(m_thisFBO); ctx->contextHandle()->functions()->glBindFramebuffer(GL_FRAMEBUFFER, m_thisFBO); } ctx->d_ptr->default_fbo = m_thisFBO; } void QGLPaintDevice::endPaint() { // Make sure the FBO bound at beginPaint is re-bound again here: QGLContext *ctx = context(); ctx->d_func()->refreshCurrentFbo(); if (m_previousFBO != ctx->d_func()->current_fbo) { ctx->d_func()->setCurrentFbo(m_previousFBO); ctx->contextHandle()->functions()->glBindFramebuffer(GL_FRAMEBUFFER, m_previousFBO); } ctx->d_ptr->default_fbo = 0; } QGLFormat QGLPaintDevice::format() const { return context()->format(); } bool QGLPaintDevice::alphaRequested() const { return context()->d_func()->reqFormat.alpha(); }
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
bool QGLPaintDevice::isFlipped() const { return false; } ////////////////// QGLWidgetGLPaintDevice ////////////////// QGLWidgetGLPaintDevice::QGLWidgetGLPaintDevice() { } QPaintEngine* QGLWidgetGLPaintDevice::paintEngine() const { return glWidget->paintEngine(); } void QGLWidgetGLPaintDevice::setWidget(QGLWidget* w) { glWidget = w; } void QGLWidgetGLPaintDevice::beginPaint() { QGLPaintDevice::beginPaint(); QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions(); if (!glWidget->d_func()->disable_clear_on_painter_begin && glWidget->autoFillBackground()) { if (glWidget->testAttribute(Qt::WA_TranslucentBackground)) funcs->glClearColor(0.0, 0.0, 0.0, 0.0); else { const QColor &c = glWidget->palette().brush(glWidget->backgroundRole()).color(); float alpha = c.alphaF(); funcs->glClearColor(c.redF() * alpha, c.greenF() * alpha, c.blueF() * alpha, alpha); } if (context()->d_func()->workaround_needsFullClearOnEveryFrame) funcs->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); else funcs->glClear(GL_COLOR_BUFFER_BIT); } } void QGLWidgetGLPaintDevice::endPaint() { if (glWidget->autoBufferSwap()) glWidget->swapBuffers(); QGLPaintDevice::endPaint(); } QSize QGLWidgetGLPaintDevice::size() const { #ifdef Q_OS_MAC return glWidget->size() * (glWidget->windowHandle() ? glWidget->windowHandle()->devicePixelRatio() : qApp->devicePixelRatio()); #else return glWidget->size(); #endif } QGLContext* QGLWidgetGLPaintDevice::context() const { return const_cast<QGLContext*>(glWidget->context()); } // returns the QGLPaintDevice for the given QPaintDevice QGLPaintDevice* QGLPaintDevice::getDevice(QPaintDevice* pd) { QGLPaintDevice* glpd = 0; switch(pd->devType()) {
211212213214215216217218219220221222223224225226227228229230231232233234235
case QInternal::Widget: // Should not be called on a non-gl widget: Q_ASSERT(qobject_cast<QGLWidget*>(static_cast<QWidget*>(pd))); glpd = &(static_cast<QGLWidget*>(pd)->d_func()->glDevice); break; case QInternal::Pbuffer: glpd = &(static_cast<QGLPixelBuffer*>(pd)->d_func()->glDevice); break; case QInternal::FramebufferObject: glpd = &(static_cast<QGLFramebufferObject*>(pd)->d_func()->glDevice); break; case QInternal::Pixmap: { qWarning("Pixmap type not supported for GL rendering"); break; } default: qWarning("QGLPaintDevice::getDevice() - Unknown device type %d", pd->devType()); break; } return glpd; } QT_END_NAMESPACE