1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtOpenGL module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 \brief The QGLPixelBuffer class encapsulates an OpenGL pbuffer.
49 Rendering into a pbuffer is normally done using full hardware
50 acceleration. This can be significantly faster than rendering
53 There are three approaches to using this class:
56 \o \bold{We can draw into the pbuffer and convert it to a QImage
57 using toImage().} This is normally much faster than calling
58 QGLWidget::renderPixmap().
60 \o \bold{We can draw into the pbuffer and copy the contents into
61 an OpenGL texture using updateDynamicTexture().} This allows
62 us to create dynamic textures and works on all systems
65 \o \bold{On systems that support it, we can bind the pbuffer to
66 an OpenGL texture.} The texture is then updated automatically
67 when the pbuffer contents change, eliminating the need for
68 additional copy operations. This is supported only on Windows
69 and Mac OS X systems that provide the \c render_texture
70 extension. Note that under Windows, a multi-sampled pbuffer
71 can't be used in conjunction with the \c render_texture
72 extension. If a multi-sampled pbuffer is requested under
73 Windows, the \c render_texture extension is turned off for that
82 As of Qt 4.8, it's possible to render into a QGLPixelBuffer using
83 a QPainter in a separate thread. Note that OpenGL 2.0 or OpenGL ES
84 2.0 is required for this to work. Also, under X11, it's necessary
85 to set the Qt::AA_X11InitThreads application attribute.
87 Pbuffers are provided by the OpenGL \c pbuffer extension; call
88 hasOpenGLPbuffer() to find out if the system provides pbuffers.
90 \sa {opengl/pbuffers}{Pbuffers Example}
93 #include <QtCore/qglobal.h>
95 #if !defined(QT_OPENGL_ES_1)
96 #include <private/qpaintengineex_opengl2_p.h>
99 #include <qglpixelbuffer.h>
100 #include <private/qglpixelbuffer_p.h>
101 #include <private/qfont_p.h>
104 #ifndef QT_OPENGL_ES_2
105 #include <private/qpaintengine_opengl_p.h>
110 #if !defined(QT_OPENGL_ES_2)
111 extern void qgl_cleanup_glyph_cache(QGLContext *);
113 void qgl_cleanup_glyph_cache(QGLContext *) {}
116 extern Q_OPENGL_EXPORT QImage qt_gl_read_framebuffer(const QSize&, bool, bool);
119 QGLContext* QGLPBufferGLPaintDevice::context() const
121 return pbuf->d_func()->qctx;
124 void QGLPBufferGLPaintDevice::endPaint() {
126 QGLPaintDevice::endPaint();
129 void QGLPBufferGLPaintDevice::setPBuffer(QGLPixelBuffer* pb)
134 void QGLPixelBufferPrivate::common_init(const QSize &size, const QGLFormat &format, QGLWidget *shareWidget)
137 if(init(size, format, shareWidget)) {
140 req_shareWidget = shareWidget;
142 qctx = new QGLContext(format);
143 qctx->d_func()->sharing = (shareWidget != 0);
144 if (shareWidget != 0 && shareWidget->d_func()->glcx) {
145 QGLContextGroup::addShare(qctx, shareWidget->d_func()->glcx);
146 shareWidget->d_func()->glcx->d_func()->sharing = true;
149 glDevice.setPBuffer(q);
150 qctx->d_func()->paintDevice = q;
151 qctx->d_func()->valid = true;
152 #if defined(Q_WS_WIN) && !defined(QT_OPENGL_ES)
153 qctx->d_func()->dc = dc;
154 qctx->d_func()->rc = ctx;
155 #elif (defined(Q_WS_X11) && defined(QT_NO_EGL))
156 qctx->d_func()->cx = ctx;
157 qctx->d_func()->pbuf = (void *) pbuf;
158 qctx->d_func()->vi = 0;
159 #elif defined(Q_WS_MAC)
160 qctx->d_func()->cx = ctx;
161 qctx->d_func()->vi = 0;
162 #elif !defined(QT_NO_EGL)
163 qctx->d_func()->eglContext = ctx;
164 qctx->d_func()->eglSurface = pbuf;
170 Constructs an OpenGL pbuffer of the given \a size. If no \a
171 format is specified, the \l{QGLFormat::defaultFormat()}{default
172 format} is used. If the \a shareWidget parameter points to a
173 valid QGLWidget, the pbuffer will share its context with \a
176 If you intend to bind this pbuffer as a dynamic texture, the width
177 and height components of \c size must be powers of two (e.g., 512
182 QGLPixelBuffer::QGLPixelBuffer(const QSize &size, const QGLFormat &format, QGLWidget *shareWidget)
183 : d_ptr(new QGLPixelBufferPrivate(this))
186 d->common_init(size, format, shareWidget);
192 Constructs an OpenGL pbuffer with the \a width and \a height. If
193 no \a format is specified, the
194 \l{QGLFormat::defaultFormat()}{default format} is used. If the \a
195 shareWidget parameter points to a valid QGLWidget, the pbuffer
196 will share its context with \a shareWidget.
198 If you intend to bind this pbuffer as a dynamic texture, the width
199 and height components of \c size must be powers of two (e.g., 512
204 QGLPixelBuffer::QGLPixelBuffer(int width, int height, const QGLFormat &format, QGLWidget *shareWidget)
205 : d_ptr(new QGLPixelBufferPrivate(this))
208 d->common_init(QSize(width, height), format, shareWidget);
212 /*! \fn QGLPixelBuffer::~QGLPixelBuffer()
214 Destroys the pbuffer and frees any allocated resources.
216 QGLPixelBuffer::~QGLPixelBuffer()
220 // defined in qpaintengine_opengl.cpp
221 QGLContext *current = const_cast<QGLContext *>(QGLContext::currentContext());
222 if (current != d->qctx)
224 qgl_cleanup_glyph_cache(d->qctx);
227 if (current && current != d->qctx)
228 current->makeCurrent();
231 /*! \fn bool QGLPixelBuffer::makeCurrent()
233 Makes this pbuffer the current OpenGL rendering context. Returns
234 true on success; otherwise returns false.
236 \sa QGLContext::makeCurrent(), doneCurrent()
239 bool QGLPixelBuffer::makeCurrent()
244 d->qctx->makeCurrent();
248 /*! \fn bool QGLPixelBuffer::doneCurrent()
250 Makes no context the current OpenGL context. Returns true on
251 success; otherwise returns false.
254 bool QGLPixelBuffer::doneCurrent()
259 d->qctx->doneCurrent();
264 Generates and binds a 2D GL texture that is the same size as the
265 pbuffer, and returns the texture's ID. This can be used in
266 conjunction with bindToDynamicTexture() and
267 updateDynamicTexture().
272 #if (defined(Q_WS_X11) || defined(Q_WS_WIN)) && defined(QT_NO_EGL)
273 GLuint QGLPixelBuffer::generateDynamicTexture() const
275 Q_D(const QGLPixelBuffer);
277 glGenTextures(1, &texture);
278 glBindTexture(GL_TEXTURE_2D, texture);
279 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, d->req_size.width(), d->req_size.height(), 0, GL_RGBA, GL_FLOAT, 0);
280 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
281 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
286 /*! \fn bool QGLPixelBuffer::bindToDynamicTexture(GLuint texture_id)
288 Binds the texture specified by \a texture_id to this pbuffer.
289 Returns true on success; otherwise returns false.
291 The texture must be of the same size and format as the pbuffer.
293 To unbind the texture, call releaseFromDynamicTexture(). While
294 the texture is bound, it is updated automatically when the
295 pbuffer contents change, eliminating the need for additional copy
300 \snippet doc/src/snippets/code/src_opengl_qglpixelbuffer.cpp 0
302 \warning This function uses the \c {render_texture} extension,
303 which is currently not supported under X11. An alternative that
304 works on all systems (including X11) is to manually copy the
305 pbuffer contents to a texture using updateDynamicTexture().
307 \warning For the bindToDynamicTexture() call to succeed on the
308 Mac OS X, the pbuffer needs a shared context, i.e. the
309 QGLPixelBuffer must be created with a share widget.
311 \sa generateDynamicTexture(), releaseFromDynamicTexture()
314 /*! \fn void QGLPixelBuffer::releaseFromDynamicTexture()
316 Releases the pbuffer from any previously bound texture.
318 \sa bindToDynamicTexture()
321 /*! \fn bool QGLPixelBuffer::hasOpenGLPbuffers()
323 Returns true if the OpenGL \c pbuffer extension is present on
324 this system; otherwise returns false.
328 Copies the pbuffer contents into the texture specified with \a
331 The texture must be of the same size and format as the pbuffer.
335 \snippet doc/src/snippets/code/src_opengl_qglpixelbuffer.cpp 1
337 An alternative on Windows and Mac OS X systems that support the
338 \c render_texture extension is to use bindToDynamicTexture() to
339 get dynamic updates of the texture.
341 \sa generateDynamicTexture(), bindToDynamicTexture()
343 void QGLPixelBuffer::updateDynamicTexture(GLuint texture_id) const
345 Q_D(const QGLPixelBuffer);
348 glBindTexture(GL_TEXTURE_2D, texture_id);
350 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 0, 0, d->req_size.width(), d->req_size.height(), 0);
352 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, d->req_size.width(), d->req_size.height(), 0);
356 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS
357 void QGLPixelBuffer::updateDynamicTexture(QMacCompatGLuint texture_id) const
359 updateDynamicTexture(GLuint(texture_id));
364 Returns the size of the pbuffer.
366 QSize QGLPixelBuffer::size() const
368 Q_D(const QGLPixelBuffer);
373 Returns the contents of the pbuffer as a QImage.
375 QImage QGLPixelBuffer::toImage() const
377 Q_D(const QGLPixelBuffer);
381 const_cast<QGLPixelBuffer *>(this)->makeCurrent();
382 return qt_gl_read_framebuffer(d->req_size, d->format.alpha(), true);
386 Returns the native pbuffer handle.
388 Qt::HANDLE QGLPixelBuffer::handle() const
390 Q_D(const QGLPixelBuffer);
393 return (Qt::HANDLE) d->pbuf;
397 Returns true if this pbuffer is valid; otherwise returns false.
399 bool QGLPixelBuffer::isValid() const
401 Q_D(const QGLPixelBuffer);
405 #if !defined(QT_OPENGL_ES_1)
406 Q_GLOBAL_STATIC(QGLEngineThreadStorage<QGL2PaintEngineEx>, qt_buffer_2_engine)
409 #ifndef QT_OPENGL_ES_2
410 Q_GLOBAL_STATIC(QGLEngineThreadStorage<QOpenGLPaintEngine>, qt_buffer_engine)
414 QPaintEngine *QGLPixelBuffer::paintEngine() const
416 #if defined(QT_OPENGL_ES_1)
417 return qt_buffer_engine()->engine();
418 #elif defined(QT_OPENGL_ES_2)
419 return qt_buffer_2_engine()->engine();
421 if (qt_gl_preferGL2Engine())
422 return qt_buffer_2_engine()->engine();
424 return qt_buffer_engine()->engine();
429 int QGLPixelBuffer::metric(PaintDeviceMetric metric) const
431 Q_D(const QGLPixelBuffer);
433 float dpmx = qt_defaultDpiX()*100./2.54;
434 float dpmy = qt_defaultDpiY()*100./2.54;
435 int w = d->req_size.width();
436 int h = d->req_size.height();
445 return qRound(w * 1000 / dpmx);
448 return qRound(h * 1000 / dpmy);
454 return 32;//d->depth;
457 return qRound(dpmx * 0.0254);
460 return qRound(dpmy * 0.0254);
462 case PdmPhysicalDpiX:
463 return qRound(dpmx * 0.0254);
465 case PdmPhysicalDpiY:
466 return qRound(dpmy * 0.0254);
469 qWarning("QGLPixelBuffer::metric(), Unhandled metric type: %d\n", metric);
476 Generates and binds a 2D GL texture to the current context, based
477 on \a image. The generated texture id is returned and can be used
478 in later glBindTexture() calls.
480 The \a target parameter specifies the texture target.
482 Equivalent to calling QGLContext::bindTexture().
486 GLuint QGLPixelBuffer::bindTexture(const QImage &image, GLenum target)
490 return d->qctx->bindTexture(image, target, GLint(GL_RGBA8));
492 return d->qctx->bindTexture(image, target, GL_RGBA);
496 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS
498 GLuint QGLPixelBuffer::bindTexture(const QImage &image, QMacCompatGLenum target)
501 return d->qctx->bindTexture(image, target, QMacCompatGLint(GL_RGBA8));
507 Generates and binds a 2D GL texture based on \a pixmap.
509 Equivalent to calling QGLContext::bindTexture().
513 GLuint QGLPixelBuffer::bindTexture(const QPixmap &pixmap, GLenum target)
517 return d->qctx->bindTexture(pixmap, target, GLint(GL_RGBA8));
519 return d->qctx->bindTexture(pixmap, target, GL_RGBA);
523 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS
525 GLuint QGLPixelBuffer::bindTexture(const QPixmap &pixmap, QMacCompatGLenum target)
528 return d->qctx->bindTexture(pixmap, target, QMacCompatGLint(GL_RGBA8));
534 Reads the DirectDrawSurface (DDS) compressed file \a fileName and
535 generates a 2D GL texture from it.
537 Equivalent to calling QGLContext::bindTexture().
541 GLuint QGLPixelBuffer::bindTexture(const QString &fileName)
544 return d->qctx->bindTexture(fileName);
548 Removes the texture identified by \a texture_id from the texture cache.
550 Equivalent to calling QGLContext::deleteTexture().
552 void QGLPixelBuffer::deleteTexture(GLuint texture_id)
555 d->qctx->deleteTexture(texture_id);
558 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS
560 void QGLPixelBuffer::deleteTexture(QMacCompatGLuint texture_id)
563 d->qctx->deleteTexture(texture_id);
570 Draws the given texture, \a textureId, to the given target rectangle,
571 \a target, in OpenGL model space. The \a textureTarget should be a 2D
574 Equivalent to the corresponding QGLContext::drawTexture().
576 void QGLPixelBuffer::drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget)
579 d->qctx->drawTexture(target, textureId, textureTarget);
582 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS
584 void QGLPixelBuffer::drawTexture(const QRectF &target, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget)
587 d->qctx->drawTexture(target, textureId, textureTarget);
594 Draws the given texture, \a textureId, at the given \a point in OpenGL model
595 space. The textureTarget parameter should be a 2D texture target.
597 Equivalent to the corresponding QGLContext::drawTexture().
599 void QGLPixelBuffer::drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget)
602 d->qctx->drawTexture(point, textureId, textureTarget);
605 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS
607 void QGLPixelBuffer::drawTexture(const QPointF &point, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget)
610 d->qctx->drawTexture(point, textureId, textureTarget);
615 Returns the format of the pbuffer. The format may be different
616 from the one that was requested.
618 QGLFormat QGLPixelBuffer::format() const
620 Q_D(const QGLPixelBuffer);
624 /*! \fn int QGLPixelBuffer::devType() const