| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 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 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 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 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 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) | 
|---|
| 52 | #include <private/qpixmapdata_gl_p.h> | 
|---|
| 53 | #endif | 
|---|
| 54 |  | 
|---|
| 55 | QT_BEGIN_NAMESPACE | 
|---|
| 56 |  | 
|---|
| 57 | QGLPaintDevice::QGLPaintDevice() | 
|---|
| 58 | : m_thisFBO(0) | 
|---|
| 59 | { | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | QGLPaintDevice::~QGLPaintDevice() | 
|---|
| 63 | { | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | int QGLPaintDevice::metric(QPaintDevice::PaintDeviceMetric metric) const | 
|---|
| 67 | { | 
|---|
| 68 | switch(metric) { | 
|---|
| 69 | case PdmWidth: | 
|---|
| 70 | return size().width(); | 
|---|
| 71 | case PdmHeight: | 
|---|
| 72 | return size().height(); | 
|---|
| 73 | case PdmDepth: { | 
|---|
| 74 | const QGLFormat f = format(); | 
|---|
| 75 | return f.redBufferSize() + f.greenBufferSize() + f.blueBufferSize() + f.alphaBufferSize(); | 
|---|
| 76 | } | 
|---|
| 77 | default: | 
|---|
| 78 | qWarning("QGLPaintDevice::metric() - metric %d not known", metric); | 
|---|
| 79 | return 0; | 
|---|
| 80 | } | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | void QGLPaintDevice::beginPaint() | 
|---|
| 84 | { | 
|---|
| 85 | // Make sure our context is the current one: | 
|---|
| 86 | QGLContext *ctx = context(); | 
|---|
| 87 | if (ctx != QGLContext::currentContext()) | 
|---|
| 88 | ctx->makeCurrent(); | 
|---|
| 89 |  | 
|---|
| 90 | // Record the currently bound FBO so we can restore it again | 
|---|
| 91 | // in endPaint() and bind this device's FBO | 
|---|
| 92 | // | 
|---|
| 93 | // Note: m_thisFBO could be zero if the paint device is not | 
|---|
| 94 | // backed by an FBO (e.g. window back buffer).  But there could | 
|---|
| 95 | // be a previous FBO bound to the context which we need to | 
|---|
| 96 | // explicitly unbind.  Otherwise the painting will go into | 
|---|
| 97 | // the previous FBO instead of to the window. | 
|---|
| 98 | m_previousFBO = ctx->d_func()->current_fbo; | 
|---|
| 99 |  | 
|---|
| 100 | if (m_previousFBO != m_thisFBO) { | 
|---|
| 101 | ctx->d_ptr->current_fbo = m_thisFBO; | 
|---|
| 102 | glBindFramebuffer(GL_FRAMEBUFFER_EXT, m_thisFBO); | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | // Set the default fbo for the context to m_thisFBO so that | 
|---|
| 106 | // if some raw GL code between beginNativePainting() and | 
|---|
| 107 | // endNativePainting() calls QGLFramebufferObject::release(), | 
|---|
| 108 | // painting will revert to the window surface's fbo. | 
|---|
| 109 | ctx->d_ptr->default_fbo = m_thisFBO; | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | void QGLPaintDevice::ensureActiveTarget() | 
|---|
| 113 | { | 
|---|
| 114 | QGLContext* ctx = context(); | 
|---|
| 115 | if (ctx != QGLContext::currentContext()) | 
|---|
| 116 | ctx->makeCurrent(); | 
|---|
| 117 |  | 
|---|
| 118 | if (ctx->d_ptr->current_fbo != m_thisFBO) { | 
|---|
| 119 | ctx->d_ptr->current_fbo = m_thisFBO; | 
|---|
| 120 | glBindFramebuffer(GL_FRAMEBUFFER_EXT, m_thisFBO); | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | ctx->d_ptr->default_fbo = m_thisFBO; | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | void QGLPaintDevice::endPaint() | 
|---|
| 127 | { | 
|---|
| 128 | // Make sure the FBO bound at beginPaint is re-bound again here: | 
|---|
| 129 | QGLContext *ctx = context(); | 
|---|
| 130 | if (m_previousFBO != ctx->d_func()->current_fbo) { | 
|---|
| 131 | ctx->d_ptr->current_fbo = m_previousFBO; | 
|---|
| 132 | glBindFramebuffer(GL_FRAMEBUFFER_EXT, m_previousFBO); | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | ctx->d_ptr->default_fbo = 0; | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | QGLFormat QGLPaintDevice::format() const | 
|---|
| 139 | { | 
|---|
| 140 | return context()->format(); | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | bool QGLPaintDevice::alphaRequested() const | 
|---|
| 144 | { | 
|---|
| 145 | return context()->d_func()->reqFormat.alpha(); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 |  | 
|---|
| 149 |  | 
|---|
| 150 |  | 
|---|
| 151 | ////////////////// QGLWidgetGLPaintDevice ////////////////// | 
|---|
| 152 |  | 
|---|
| 153 | QGLWidgetGLPaintDevice::QGLWidgetGLPaintDevice() | 
|---|
| 154 | { | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | QPaintEngine* QGLWidgetGLPaintDevice::paintEngine() const | 
|---|
| 158 | { | 
|---|
| 159 | return glWidget->paintEngine(); | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | void QGLWidgetGLPaintDevice::setWidget(QGLWidget* w) | 
|---|
| 163 | { | 
|---|
| 164 | glWidget = w; | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | void QGLWidgetGLPaintDevice::beginPaint() | 
|---|
| 168 | { | 
|---|
| 169 | QGLPaintDevice::beginPaint(); | 
|---|
| 170 | if (!glWidget->d_func()->disable_clear_on_painter_begin && glWidget->autoFillBackground()) { | 
|---|
| 171 | if (glWidget->testAttribute(Qt::WA_TranslucentBackground)) | 
|---|
| 172 | glClearColor(0.0, 0.0, 0.0, 0.0); | 
|---|
| 173 | else { | 
|---|
| 174 | const QColor &c = glWidget->palette().brush(glWidget->backgroundRole()).color(); | 
|---|
| 175 | float alpha = c.alphaF(); | 
|---|
| 176 | glClearColor(c.redF() * alpha, c.greenF() * alpha, c.blueF() * alpha, alpha); | 
|---|
| 177 | } | 
|---|
| 178 | if (context()->d_func()->workaround_needsFullClearOnEveryFrame) | 
|---|
| 179 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); | 
|---|
| 180 | else | 
|---|
| 181 | glClear(GL_COLOR_BUFFER_BIT); | 
|---|
| 182 | } | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | void QGLWidgetGLPaintDevice::endPaint() | 
|---|
| 186 | { | 
|---|
| 187 | if (glWidget->autoBufferSwap()) | 
|---|
| 188 | glWidget->swapBuffers(); | 
|---|
| 189 | QGLPaintDevice::endPaint(); | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 |  | 
|---|
| 193 | QSize QGLWidgetGLPaintDevice::size() const | 
|---|
| 194 | { | 
|---|
| 195 | return glWidget->size(); | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | QGLContext* QGLWidgetGLPaintDevice::context() const | 
|---|
| 199 | { | 
|---|
| 200 | return const_cast<QGLContext*>(glWidget->context()); | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | // returns the QGLPaintDevice for the given QPaintDevice | 
|---|
| 204 | QGLPaintDevice* QGLPaintDevice::getDevice(QPaintDevice* pd) | 
|---|
| 205 | { | 
|---|
| 206 | QGLPaintDevice* glpd = 0; | 
|---|
| 207 |  | 
|---|
| 208 | switch(pd->devType()) { | 
|---|
| 209 | case QInternal::Widget: | 
|---|
| 210 | // Should not be called on a non-gl widget: | 
|---|
| 211 | Q_ASSERT(qobject_cast<QGLWidget*>(static_cast<QWidget*>(pd))); | 
|---|
| 212 | glpd = &(static_cast<QGLWidget*>(pd)->d_func()->glDevice); | 
|---|
| 213 | break; | 
|---|
| 214 | case QInternal::Pbuffer: | 
|---|
| 215 | glpd = &(static_cast<QGLPixelBuffer*>(pd)->d_func()->glDevice); | 
|---|
| 216 | break; | 
|---|
| 217 | case QInternal::FramebufferObject: | 
|---|
| 218 | glpd = &(static_cast<QGLFramebufferObject*>(pd)->d_func()->glDevice); | 
|---|
| 219 | break; | 
|---|
| 220 | case QInternal::Pixmap: { | 
|---|
| 221 | #if !defined(QT_OPENGL_ES_1) | 
|---|
| 222 | QPixmapData* pmd = static_cast<QPixmap*>(pd)->pixmapData(); | 
|---|
| 223 | if (pmd->classId() == QPixmapData::OpenGLClass) | 
|---|
| 224 | glpd = static_cast<QGLPixmapData*>(pmd)->glDevice(); | 
|---|
| 225 | #ifdef Q_WS_X11 | 
|---|
| 226 | else if (pmd->classId() == QPixmapData::X11Class) | 
|---|
| 227 | glpd = static_cast<QX11GLPixmapData*>(pmd); | 
|---|
| 228 | #endif | 
|---|
| 229 | else | 
|---|
| 230 | qWarning("Pixmap type not supported for GL rendering"); | 
|---|
| 231 | #else | 
|---|
| 232 | qWarning("Pixmap render targets not supported on OpenGL ES 1.x"); | 
|---|
| 233 | #endif | 
|---|
| 234 | break; | 
|---|
| 235 | } | 
|---|
| 236 | default: | 
|---|
| 237 | qWarning("QGLPaintDevice::getDevice() - Unknown device type %d", pd->devType()); | 
|---|
| 238 | break; | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 | return glpd; | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | QT_END_NAMESPACE | 
|---|