src/opengl/qglpaintdevice.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 
       
    94 void QGLPaintDevice::ensureActiveTarget()
       
    95 {
       
    96     QGLContext* ctx = context();
       
    97     if (ctx != QGLContext::currentContext())
       
    98         ctx->makeCurrent();
       
    99 
       
   100     if (ctx->d_ptr->current_fbo != m_thisFBO) {
       
   101         ctx->d_ptr->current_fbo = m_thisFBO;
       
   102         glBindFramebuffer(GL_FRAMEBUFFER_EXT, m_thisFBO);
       
   103     }
       
   104 }
       
   105 
       
   106 void QGLPaintDevice::endPaint()
       
   107 {
       
   108     // Make sure the FBO bound at beginPaint is re-bound again here:
       
   109     QGLContext *ctx = context();
       
   110     if (m_previousFBO != ctx->d_func()->current_fbo) {
       
   111         ctx->d_ptr->current_fbo = m_previousFBO;
       
   112         glBindFramebuffer(GL_FRAMEBUFFER_EXT, m_previousFBO);
       
   113     }
       
   114 }
       
   115 
       
   116 QGLFormat QGLPaintDevice::format() const
       
   117 {
       
   118     return context()->format();
       
   119 }
       
   120 
       
   121 
       
   122 
       
   123 
       
   124 ////////////////// QGLWidgetGLPaintDevice //////////////////
       
   125 
       
   126 QGLWidgetGLPaintDevice::QGLWidgetGLPaintDevice()
       
   127 {
       
   128 }
       
   129 
       
   130 QPaintEngine* QGLWidgetGLPaintDevice::paintEngine() const
       
   131 {
       
   132     return glWidget->paintEngine();
       
   133 }
       
   134 
       
   135 void QGLWidgetGLPaintDevice::setWidget(QGLWidget* w)
       
   136 {
       
   137     glWidget = w;
       
   138 }
       
   139 
       
   140 void QGLWidgetGLPaintDevice::beginPaint()
       
   141 {
       
   142     QGLPaintDevice::beginPaint();
       
   143     if (!glWidget->d_func()->disable_clear_on_painter_begin && glWidget->autoFillBackground()) {
       
   144         if (glWidget->testAttribute(Qt::WA_TranslucentBackground))
       
   145             glClearColor(0.0, 0.0, 0.0, 0.0);
       
   146         else {
       
   147             const QColor &c = glWidget->palette().brush(glWidget->backgroundRole()).color();
       
   148             float alpha = c.alphaF();
       
   149             glClearColor(c.redF() * alpha, c.greenF() * alpha, c.blueF() * alpha, alpha);
       
   150         }
       
   151         glClear(GL_COLOR_BUFFER_BIT);
       
   152     }
       
   153 }
       
   154 
       
   155 void QGLWidgetGLPaintDevice::endPaint()
       
   156 {
       
   157     if (glWidget->autoBufferSwap())
       
   158         glWidget->swapBuffers();
       
   159     QGLPaintDevice::endPaint();
       
   160 }
       
   161 
       
   162 
       
   163 QSize QGLWidgetGLPaintDevice::size() const
       
   164 {
       
   165     return glWidget->size();
       
   166 }
       
   167 
       
   168 QGLContext* QGLWidgetGLPaintDevice::context() const
       
   169 {
       
   170     return const_cast<QGLContext*>(glWidget->context());
       
   171 }
       
   172 
       
   173 // returns the QGLPaintDevice for the given QPaintDevice
       
   174 QGLPaintDevice* QGLPaintDevice::getDevice(QPaintDevice* pd)
       
   175 {
       
   176     QGLPaintDevice* glpd = 0;
       
   177 
       
   178     switch(pd->devType()) {
       
   179         case QInternal::Widget:
       
   180             // Should not be called on a non-gl widget:
       
   181             Q_ASSERT(qobject_cast<QGLWidget*>(static_cast<QWidget*>(pd)));
       
   182             glpd = &(static_cast<QGLWidget*>(pd)->d_func()->glDevice);
       
   183             break;
       
   184         case QInternal::Pbuffer:
       
   185             glpd = &(static_cast<QGLPixelBuffer*>(pd)->d_func()->glDevice);
       
   186             break;
       
   187         case QInternal::FramebufferObject:
       
   188             glpd = &(static_cast<QGLFramebufferObject*>(pd)->d_func()->glDevice);
       
   189             break;
       
   190         case QInternal::Pixmap: {
       
   191 #if !defined(QT_OPENGL_ES_1) && !defined(QT_OPENGL_ES_1_CL)
       
   192             QPixmapData* pmd = static_cast<QPixmap*>(pd)->pixmapData();
       
   193             if (pmd->classId() == QPixmapData::OpenGLClass)
       
   194                 glpd = static_cast<QGLPixmapData*>(pmd)->glDevice();
       
   195 #ifdef Q_WS_X11
       
   196             else if (pmd->classId() == QPixmapData::X11Class)
       
   197                 glpd = static_cast<QX11GLPixmapData*>(pmd);
       
   198 #endif
       
   199             else
       
   200                 qWarning("Pixmap type not supported for GL rendering");
       
   201 #else
       
   202             qWarning("Pixmap render targets not supported on OpenGL ES 1.x");
       
   203 #endif
       
   204             break;
       
   205         }
       
   206         default:
       
   207             qWarning("QGLPaintDevice::getDevice() - Unknown device type %d", pd->devType());
       
   208             break;
       
   209     }
       
   210 
       
   211     return glpd;
       
   212 }
       
   213 
       
   214 QT_END_NAMESPACE