1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtGui module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <qopenglpaintdevice.h>
43 #include <qpaintengine.h>
44 #include <qthreadstorage.h>
46 #include <private/qobject_p.h>
47 #include <private/qopenglcontext_p.h>
48 #include <private/qopenglframebufferobject_p.h>
49 #include <private/qopenglpaintengine_p.h>
51 // for qt_defaultDpiX/Y
52 #include <private/qfont_p.h>
54 #include <qopenglfunctions.h>
59 \class QOpenGLPaintDevice
60 \brief The QOpenGLPaintDevice class enables painting to an OpenGL context using QPainter.
66 When painting to a QOpenGLPaintDevice using QPainter, the state of
67 the current OpenGL context will be altered by the paint engine to reflect
68 its needs. Applications should not rely upon the OpenGL state being reset
69 to its original conditions, particularly the current shader program,
70 OpenGL viewport, texture units, and drawing modes.
73 class QOpenGLPaintDevicePrivate
76 QOpenGLPaintDevicePrivate(const QSize &size);
90 Constructs a QOpenGLPaintDevice.
92 The QOpenGLPaintDevice is only valid for the current context.
94 \sa QOpenGLContext::currentContext()
96 QOpenGLPaintDevice::QOpenGLPaintDevice()
97 : d_ptr(new QOpenGLPaintDevicePrivate(QSize()))
102 Constructs a QOpenGLPaintDevice with the given \a size.
104 The QOpenGLPaintDevice is only valid for the current context.
106 \sa QOpenGLContext::currentContext()
108 QOpenGLPaintDevice::QOpenGLPaintDevice(const QSize &size)
109 : d_ptr(new QOpenGLPaintDevicePrivate(size))
114 Constructs a QOpenGLPaintDevice with the given \a width and \a height.
116 The QOpenGLPaintDevice is only valid for the current context.
118 \sa QOpenGLContext::currentContext()
120 QOpenGLPaintDevice::QOpenGLPaintDevice(int width, int height)
121 : d_ptr(new QOpenGLPaintDevicePrivate(QSize(width, height)))
126 Destroys the QOpenGLPaintDevice.
129 QOpenGLPaintDevice::~QOpenGLPaintDevice()
131 delete d_ptr->engine;
135 \fn int QOpenGLPaintDevice::devType() const
140 QOpenGLPaintDevicePrivate::QOpenGLPaintDevicePrivate(const QSize &sz)
142 , ctx(QOpenGLContext::currentContext())
143 , dpmx(qt_defaultDpiX() * 100. / 2.54)
144 , dpmy(qt_defaultDpiY() * 100. / 2.54)
150 class QOpenGLEngineThreadStorage
153 QPaintEngine *engine() {
154 QPaintEngine *&localEngine = storage.localData();
156 localEngine = new QOpenGL2PaintEngineEx;
161 QThreadStorage<QPaintEngine *> storage;
164 Q_GLOBAL_STATIC(QOpenGLEngineThreadStorage, qt_opengl_engine)
170 QPaintEngine *QOpenGLPaintDevice::paintEngine() const
173 return d_ptr->engine;
175 QPaintEngine *engine = qt_opengl_engine()->engine();
176 if (engine->isActive() && engine->paintDevice() != this) {
177 d_ptr->engine = new QOpenGL2PaintEngineEx;
178 return d_ptr->engine;
185 Returns the OpenGL context associated with the paint device.
188 QOpenGLContext *QOpenGLPaintDevice::context() const
194 Returns the pixel size of the paint device.
199 QSize QOpenGLPaintDevice::size() const
205 Sets the pixel size of the paint device to \a size.
210 void QOpenGLPaintDevice::setSize(const QSize &size)
219 int QOpenGLPaintDevice::metric(QPaintDevice::PaintDeviceMetric metric) const
223 return d_ptr->size.width();
225 return d_ptr->size.height();
229 return qRound(d_ptr->size.width() * 1000 / d_ptr->dpmx);
231 return qRound(d_ptr->size.height() * 1000 / d_ptr->dpmy);
235 return qRound(d_ptr->dpmx * 0.0254);
237 return qRound(d_ptr->dpmy * 0.0254);
238 case PdmPhysicalDpiX:
239 return qRound(d_ptr->dpmx * 0.0254);
240 case PdmPhysicalDpiY:
241 return qRound(d_ptr->dpmy * 0.0254);
243 qWarning("QOpenGLPaintDevice::metric() - metric %d not known", metric);
249 Returns the number of pixels per meter horizontally.
251 \sa setDotsPerMeterX()
254 qreal QOpenGLPaintDevice::dotsPerMeterX() const
260 Returns the number of pixels per meter vertically.
262 \sa setDotsPerMeterY()
265 qreal QOpenGLPaintDevice::dotsPerMeterY() const
271 Sets the number of pixels per meter horizontally to \a dpmx.
276 void QOpenGLPaintDevice::setDotsPerMeterX(qreal dpmx)
282 Sets the number of pixels per meter vertically to \a dpmy.
287 void QOpenGLPaintDevice::setDotsPerMeterY(qreal dpmy)
293 Sets whether painting should be flipped around the Y-axis or not to \a flipped.
297 void QOpenGLPaintDevice::setPaintFlipped(bool flipped)
299 d_ptr->flipped = flipped;
303 Returns true if painting is flipped around the Y-axis.
305 \sa setPaintFlipped()
308 bool QOpenGLPaintDevice::paintFlipped() const
310 return d_ptr->flipped;
314 This virtual method is provided as a callback to allow re-binding a
315 target frame buffer object when different QOpenGLPaintDevice instances
316 are issuing draw calls alternately on the same OpenGL context.
318 QPainter::beginNativePainting will also trigger this method.
320 void QOpenGLPaintDevice::ensureActiveTarget()