Doc: Fix some documentation issues.
[profile/ivi/qtbase.git] / src / gui / opengl / qopenglpaintdevice.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qopenglpaintdevice.h>
43 #include <qpaintengine.h>
44 #include <qthreadstorage.h>
45
46 #include <private/qobject_p.h>
47 #include <private/qopenglcontext_p.h>
48 #include <private/qopenglframebufferobject_p.h>
49 #include <private/qopenglpaintengine_p.h>
50
51 // for qt_defaultDpiX/Y
52 #include <private/qfont_p.h>
53
54 #include <qopenglfunctions.h>
55
56 QT_BEGIN_NAMESPACE
57
58 /*!
59     \class QOpenGLPaintDevice
60     \brief The QOpenGLPaintDevice class enables painting to an OpenGL context using QPainter.
61     \since 5.0
62     \inmodule QtGui
63
64     \ingroup painting-3D
65
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.
71 */
72
73 class QOpenGLPaintDevicePrivate
74 {
75 public:
76     QOpenGLPaintDevicePrivate(const QSize &size);
77
78     QSize size;
79     QOpenGLContext *ctx;
80
81     qreal dpmx;
82     qreal dpmy;
83
84     bool flipped;
85
86     QPaintEngine *engine;
87 };
88
89 /*!
90     Constructs a QOpenGLPaintDevice.
91
92     The QOpenGLPaintDevice is only valid for the current context.
93
94     \sa QOpenGLContext::currentContext()
95 */
96 QOpenGLPaintDevice::QOpenGLPaintDevice()
97     : d_ptr(new QOpenGLPaintDevicePrivate(QSize()))
98 {
99 }
100
101 /*!
102     Constructs a QOpenGLPaintDevice with the given \a size.
103
104     The QOpenGLPaintDevice is only valid for the current context.
105
106     \sa QOpenGLContext::currentContext()
107 */
108 QOpenGLPaintDevice::QOpenGLPaintDevice(const QSize &size)
109     : d_ptr(new QOpenGLPaintDevicePrivate(size))
110 {
111 }
112
113 /*!
114     Constructs a QOpenGLPaintDevice with the given \a width and \a height.
115
116     The QOpenGLPaintDevice is only valid for the current context.
117
118     \sa QOpenGLContext::currentContext()
119 */
120 QOpenGLPaintDevice::QOpenGLPaintDevice(int width, int height)
121     : d_ptr(new QOpenGLPaintDevicePrivate(QSize(width, height)))
122 {
123 }
124
125 /*!
126     Destroys the QOpenGLPaintDevice.
127 */
128
129 QOpenGLPaintDevice::~QOpenGLPaintDevice()
130 {
131     delete d_ptr->engine;
132 }
133
134 /*!
135     \fn int QOpenGLPaintDevice::devType() const
136     \internal
137     \reimp
138 */
139
140 QOpenGLPaintDevicePrivate::QOpenGLPaintDevicePrivate(const QSize &sz)
141     : size(sz)
142     , ctx(QOpenGLContext::currentContext())
143     , dpmx(qt_defaultDpiX() * 100. / 2.54)
144     , dpmy(qt_defaultDpiY() * 100. / 2.54)
145     , flipped(false)
146     , engine(0)
147 {
148 }
149
150 class QOpenGLEngineThreadStorage
151 {
152 public:
153     QPaintEngine *engine() {
154         QPaintEngine *&localEngine = storage.localData();
155         if (!localEngine)
156             localEngine = new QOpenGL2PaintEngineEx;
157         return localEngine;
158     }
159
160 private:
161     QThreadStorage<QPaintEngine *> storage;
162 };
163
164 Q_GLOBAL_STATIC(QOpenGLEngineThreadStorage, qt_opengl_engine)
165
166 /*!
167     \reimp
168 */
169
170 QPaintEngine *QOpenGLPaintDevice::paintEngine() const
171 {
172     if (d_ptr->engine)
173         return d_ptr->engine;
174
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;
179     }
180
181     return engine;
182 }
183
184 /*!
185     Returns the OpenGL context associated with the paint device.
186 */
187
188 QOpenGLContext *QOpenGLPaintDevice::context() const
189 {
190     return d_ptr->ctx;
191 }
192
193 /*!
194     Returns the pixel size of the paint device.
195
196     \sa setSize()
197 */
198
199 QSize QOpenGLPaintDevice::size() const
200 {
201     return d_ptr->size;
202 }
203
204 /*!
205     Sets the pixel size of the paint device to \a size.
206
207     \sa size()
208 */
209
210 void QOpenGLPaintDevice::setSize(const QSize &size)
211 {
212     d_ptr->size = size;
213 }
214
215 /*!
216     \reimp
217 */
218
219 int QOpenGLPaintDevice::metric(QPaintDevice::PaintDeviceMetric metric) const
220 {
221     switch (metric) {
222     case PdmWidth:
223         return d_ptr->size.width();
224     case PdmHeight:
225         return d_ptr->size.height();
226     case PdmDepth:
227         return 32;
228     case PdmWidthMM:
229         return qRound(d_ptr->size.width() * 1000 / d_ptr->dpmx);
230     case PdmHeightMM:
231         return qRound(d_ptr->size.height() * 1000 / d_ptr->dpmy);
232     case PdmNumColors:
233         return 0;
234     case PdmDpiX:
235         return qRound(d_ptr->dpmx * 0.0254);
236     case PdmDpiY:
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);
242     default:
243         qWarning("QOpenGLPaintDevice::metric() - metric %d not known", metric);
244         return 0;
245     }
246 }
247
248 /*!
249     Returns the number of pixels per meter horizontally.
250
251     \sa setDotsPerMeterX()
252 */
253
254 qreal QOpenGLPaintDevice::dotsPerMeterX() const
255 {
256     return d_ptr->dpmx;
257 }
258
259 /*!
260     Returns the number of pixels per meter vertically.
261
262     \sa setDotsPerMeterY()
263 */
264
265 qreal QOpenGLPaintDevice::dotsPerMeterY() const
266 {
267     return d_ptr->dpmy;
268 }
269
270 /*!
271     Sets the number of pixels per meter horizontally to \a dpmx.
272
273     \sa dotsPerMeterX()
274 */
275
276 void QOpenGLPaintDevice::setDotsPerMeterX(qreal dpmx)
277 {
278     d_ptr->dpmx = dpmx;
279 }
280
281 /*!
282     Sets the number of pixels per meter vertically to \a dpmy.
283
284     \sa dotsPerMeterY()
285 */
286
287 void QOpenGLPaintDevice::setDotsPerMeterY(qreal dpmy)
288 {
289     d_ptr->dpmx = dpmy;
290 }
291
292 /*!
293     Sets whether painting should be flipped around the Y-axis or not to \a flipped.
294
295     \sa paintFlipped()
296 */
297 void QOpenGLPaintDevice::setPaintFlipped(bool flipped)
298 {
299     d_ptr->flipped = flipped;
300 }
301
302 /*!
303     Returns true if painting is flipped around the Y-axis.
304
305     \sa setPaintFlipped()
306 */
307
308 bool QOpenGLPaintDevice::paintFlipped() const
309 {
310     return d_ptr->flipped;
311 }
312
313 /*!
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.
317
318     QPainter::beginNativePainting will also trigger this method.
319 */
320 void QOpenGLPaintDevice::ensureActiveTarget()
321 {
322 }
323
324 QT_END_NAMESPACE