util/src/opengl/qglpaintdevice.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtOpenGL module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <private/qglpaintdevice_p.h>
       
    43 #include <private/qgl_p.h>
       
    44 #include <private/qglpixelbuffer_p.h>
       
    45 #include <private/qglframebufferobject_p.h>
       
    46 #include <private/qwindowsurface_gl_p.h>
       
    47 #ifdef Q_WS_X11
       
    48 #include <private/qpixmapdata_x11gl_p.h>
       
    49 #endif
       
    50 
       
    51 #if !defined(QT_OPENGL_ES_1) && !defined(QT_OPENGL_ES_1_CL)
       
    52 #include <private/qpixmapdata_gl_p.h>
       
    53 #endif
       
    54 
       
    55 #if defined(QT_OPENGL_ES_1_CL)
       
    56 #include "qgl_cl_p.h"
       
    57 #endif
       
    58 
       
    59 QT_BEGIN_NAMESPACE
       
    60 
       
    61 QGLPaintDevice::QGLPaintDevice()
       
    62     : m_thisFBO(0)
       
    63 {
       
    64 }
       
    65 
       
    66 QGLPaintDevice::~QGLPaintDevice()
       
    67 {
       
    68 }
       
    69 
       
    70 
       
    71 void QGLPaintDevice::beginPaint()
       
    72 {
       
    73     // Make sure our context is the current one:
       
    74     QGLContext *ctx = context();
       
    75     if (ctx != QGLContext::currentContext())
       
    76         ctx->makeCurrent();
       
    77 
       
    78     // Record the currently bound FBO so we can restore it again
       
    79     // in endPaint() and bind this device's FBO
       
    80     //
       
    81     // Note: m_thisFBO could be zero if the paint device is not
       
    82     // backed by an FBO (e.g. window back buffer).  But there could
       
    83     // be a previous FBO bound to the context which we need to
       
    84     // explicitly unbind.  Otherwise the painting will go into
       
    85     // the previous FBO instead of to the window.
       
    86     m_previousFBO = ctx->d_func()->current_fbo;
       
    87 
       
    88     if (m_previousFBO != m_thisFBO) {
       
    89         ctx->d_ptr->current_fbo = m_thisFBO;
       
    90         glBindFramebuffer(GL_FRAMEBUFFER_EXT, m_thisFBO);
       
    91     }
       
    92 
       
    93     // Set the default fbo for the context to m_thisFBO so that
       
    94     // if some raw GL code between beginNativePainting() and
       
    95     // endNativePainting() calls QGLFramebufferObject::release(),
       
    96     // painting will revert to the window surface's fbo.
       
    97     ctx->d_ptr->default_fbo = m_thisFBO;
       
    98 }
       
    99 
       
   100 void QGLPaintDevice::ensureActiveTarget()
       
   101 {
       
   102     QGLContext* ctx = context();
       
   103     if (ctx != QGLContext::currentContext())
       
   104         ctx->makeCurrent();
       
   105 
       
   106     if (ctx->d_ptr->current_fbo != m_thisFBO) {
       
   107         ctx->d_ptr->current_fbo = m_thisFBO;
       
   108         glBindFramebuffer(GL_FRAMEBUFFER_EXT, m_thisFBO);
       
   109     }
       
   110 
       
   111     ctx->d_ptr->default_fbo = m_thisFBO;
       
   112 }
       
   113 
       
   114 void QGLPaintDevice::endPaint()
       
   115 {
       
   116     // Make sure the FBO bound at beginPaint is re-bound again here:
       
   117     QGLContext *ctx = context();
       
   118     if (m_previousFBO != ctx->d_func()->current_fbo) {
       
   119         ctx->d_ptr->current_fbo = m_previousFBO;
       
   120         glBindFramebuffer(GL_FRAMEBUFFER_EXT, m_previousFBO);
       
   121     }
       
   122 
       
   123     ctx->d_ptr->default_fbo = 0;
       
   124 }
       
   125 
       
   126 QGLFormat QGLPaintDevice::format() const
       
   127 {
       
   128     return context()->format();
       
   129 }
       
   130 
       
   131 bool QGLPaintDevice::alphaRequested() const
       
   132 {
       
   133     return context()->d_func()->reqFormat.alpha();
       
   134 }
       
   135 
       
   136 
       
   137 
       
   138 
       
   139 ////////////////// QGLWidgetGLPaintDevice //////////////////
       
   140 
       
   141 QGLWidgetGLPaintDevice::QGLWidgetGLPaintDevice()
       
   142 {
       
   143 }
       
   144 
       
   145 QPaintEngine* QGLWidgetGLPaintDevice::paintEngine() const
       
   146 {
       
   147     return glWidget->paintEngine();
       
   148 }
       
   149 
       
   150 void QGLWidgetGLPaintDevice::setWidget(QGLWidget* w)
       
   151 {
       
   152     glWidget = w;
       
   153 }
       
   154 
       
   155 void QGLWidgetGLPaintDevice::beginPaint()
       
   156 {
       
   157     QGLPaintDevice::beginPaint();
       
   158     if (!glWidget->d_func()->disable_clear_on_painter_begin && glWidget->autoFillBackground()) {
       
   159         if (glWidget->testAttribute(Qt::WA_TranslucentBackground))
       
   160             glClearColor(0.0, 0.0, 0.0, 0.0);
       
   161         else {
       
   162             const QColor &c = glWidget->palette().brush(glWidget->backgroundRole()).color();
       
   163             float alpha = c.alphaF();
       
   164             glClearColor(c.redF() * alpha, c.greenF() * alpha, c.blueF() * alpha, alpha);
       
   165         }
       
   166         glClear(GL_COLOR_BUFFER_BIT);
       
   167     }
       
   168 }
       
   169 
       
   170 void QGLWidgetGLPaintDevice::endPaint()
       
   171 {
       
   172     if (glWidget->autoBufferSwap())
       
   173         glWidget->swapBuffers();
       
   174     QGLPaintDevice::endPaint();
       
   175 }
       
   176 
       
   177 
       
   178 QSize QGLWidgetGLPaintDevice::size() const
       
   179 {
       
   180     return glWidget->size();
       
   181 }
       
   182 
       
   183 QGLContext* QGLWidgetGLPaintDevice::context() const
       
   184 {
       
   185     return const_cast<QGLContext*>(glWidget->context());
       
   186 }
       
   187 
       
   188 // returns the QGLPaintDevice for the given QPaintDevice
       
   189 QGLPaintDevice* QGLPaintDevice::getDevice(QPaintDevice* pd)
       
   190 {
       
   191     QGLPaintDevice* glpd = 0;
       
   192 
       
   193     switch(pd->devType()) {
       
   194         case QInternal::Widget:
       
   195             // Should not be called on a non-gl widget:
       
   196             Q_ASSERT(qobject_cast<QGLWidget*>(static_cast<QWidget*>(pd)));
       
   197             glpd = &(static_cast<QGLWidget*>(pd)->d_func()->glDevice);
       
   198             break;
       
   199         case QInternal::Pbuffer:
       
   200             glpd = &(static_cast<QGLPixelBuffer*>(pd)->d_func()->glDevice);
       
   201             break;
       
   202         case QInternal::FramebufferObject:
       
   203             glpd = &(static_cast<QGLFramebufferObject*>(pd)->d_func()->glDevice);
       
   204             break;
       
   205         case QInternal::Pixmap: {
       
   206 #if !defined(QT_OPENGL_ES_1) && !defined(QT_OPENGL_ES_1_CL)
       
   207             QPixmapData* pmd = static_cast<QPixmap*>(pd)->pixmapData();
       
   208             if (pmd->classId() == QPixmapData::OpenGLClass)
       
   209                 glpd = static_cast<QGLPixmapData*>(pmd)->glDevice();
       
   210 #ifdef Q_WS_X11
       
   211             else if (pmd->classId() == QPixmapData::X11Class)
       
   212                 glpd = static_cast<QX11GLPixmapData*>(pmd);
       
   213 #endif
       
   214             else
       
   215                 qWarning("Pixmap type not supported for GL rendering");
       
   216 #else
       
   217             qWarning("Pixmap render targets not supported on OpenGL ES 1.x");
       
   218 #endif
       
   219             break;
       
   220         }
       
   221         default:
       
   222             qWarning("QGLPaintDevice::getDevice() - Unknown device type %d", pd->devType());
       
   223             break;
       
   224     }
       
   225 
       
   226     return glpd;
       
   227 }
       
   228 
       
   229 QT_END_NAMESPACE