Line | Source Code | Coverage |
---|
1 | /**************************************************************************** | - |
2 | ** | - |
3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). | - |
4 | ** Contact: http://www.qt-project.org/legal | - |
5 | ** | - |
6 | ** This file is part of the QtGui module of the Qt Toolkit. | - |
7 | ** | - |
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - |
9 | ** Commercial License Usage | - |
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - |
11 | ** accordance with the commercial license agreement provided with the | - |
12 | ** Software or, alternatively, in accordance with the terms contained in | - |
13 | ** a written agreement between you and Digia. For licensing terms and | - |
14 | ** conditions see http://qt.digia.com/licensing. For further information | - |
15 | ** use the contact form at http://qt.digia.com/contact-us. | - |
16 | ** | - |
17 | ** GNU Lesser General Public License Usage | - |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - |
19 | ** General Public License version 2.1 as published by the Free Software | - |
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the | - |
21 | ** packaging of this file. Please review the following information to | - |
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements | - |
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - |
24 | ** | - |
25 | ** In addition, as a special exception, Digia gives you certain additional | - |
26 | ** rights. These rights are described in the Digia Qt LGPL Exception | - |
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - |
28 | ** | - |
29 | ** GNU General Public License Usage | - |
30 | ** Alternatively, this file may be used under the terms of the GNU | - |
31 | ** General Public License version 3.0 as published by the Free Software | - |
32 | ** Foundation and appearing in the file LICENSE.GPL included in the | - |
33 | ** packaging of this file. Please review the following information to | - |
34 | ** ensure the GNU General Public License version 3.0 requirements will be | - |
35 | ** met: http://www.gnu.org/copyleft/gpl.html. | - |
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 | The QOpenGLPaintDevice uses the current QOpenGL context to render | - |
67 | QPainter draw commands. It requires OpenGL (ES) 2.0 support or | - |
68 | higher. | - |
69 | | - |
70 | \section1 Performance | - |
71 | | - |
72 | The QOpenGLPaintDevice is almost always hardware accelerated and | - |
73 | has the potential of being much faster than software | - |
74 | rasterization. However, it is more sensitive to state changes, and | - |
75 | therefore requires the drawing commands to be carefully ordered to | - |
76 | achieve optimal performance. | - |
77 | | - |
78 | \section1 Antialiasing and Quality | - |
79 | | - |
80 | Antialiasing in the OpenGL paint engine is done using | - |
81 | multisampling. Most hardware require significantly more memory to | - |
82 | do multisampling and the resulting quality is not on par with the | - |
83 | quality of the software paint engine. The OpenGL paint engine's | - |
84 | strenght lies in its performance, not its visual rendering | - |
85 | quality. | - |
86 | | - |
87 | \section1 State Changes | - |
88 | | - |
89 | When painting to a QOpenGLPaintDevice using QPainter, the state of | - |
90 | the current OpenGL context will be altered by the paint engine to | - |
91 | reflect its needs. Applications should not rely upon the OpenGL | - |
92 | state being reset to its original conditions, particularly the | - |
93 | current shader program, OpenGL viewport, texture units, and | - |
94 | drawing modes. | - |
95 | | - |
96 | \section1 Mixing QPainter and OpenGL | - |
97 | | - |
98 | When intermixing QPainter and OpenGL, it is important to notify | - |
99 | QPainter that the OpenGL state may have been cluttered so it can | - |
100 | restore its internal state. This is acheived by calling \l | - |
101 | QPainter::beginNativePainting() before starting the OpenGL | - |
102 | rendering and calling \l QPainter::endNativePainting() after | - |
103 | finishing. | - |
104 | | - |
105 | \sa {OpenGL Window Example} | - |
106 | | - |
107 | */ | - |
108 | | - |
109 | class QOpenGLPaintDevicePrivate | - |
110 | { | - |
111 | public: | - |
112 | QOpenGLPaintDevicePrivate(const QSize &size); | - |
113 | | - |
114 | QSize size; | - |
115 | QOpenGLContext *ctx; | - |
116 | | - |
117 | qreal dpmx; | - |
118 | qreal dpmy; | - |
119 | | - |
120 | bool flipped; | - |
121 | | - |
122 | QPaintEngine *engine; | - |
123 | }; | - |
124 | | - |
125 | /*! | - |
126 | Constructs a QOpenGLPaintDevice. | - |
127 | | - |
128 | The QOpenGLPaintDevice is only valid for the current context. | - |
129 | | - |
130 | \sa QOpenGLContext::currentContext() | - |
131 | */ | - |
132 | QOpenGLPaintDevice::QOpenGLPaintDevice() | - |
133 | : d_ptr(new QOpenGLPaintDevicePrivate(QSize())) | - |
134 | { | - |
135 | } | 0 |
136 | | - |
137 | /*! | - |
138 | Constructs a QOpenGLPaintDevice with the given \a size. | - |
139 | | - |
140 | The QOpenGLPaintDevice is only valid for the current context. | - |
141 | | - |
142 | \sa QOpenGLContext::currentContext() | - |
143 | */ | - |
144 | QOpenGLPaintDevice::QOpenGLPaintDevice(const QSize &size) | - |
145 | : d_ptr(new QOpenGLPaintDevicePrivate(size)) | - |
146 | { | - |
147 | } | 0 |
148 | | - |
149 | /*! | - |
150 | Constructs a QOpenGLPaintDevice with the given \a width and \a height. | - |
151 | | - |
152 | The QOpenGLPaintDevice is only valid for the current context. | - |
153 | | - |
154 | \sa QOpenGLContext::currentContext() | - |
155 | */ | - |
156 | QOpenGLPaintDevice::QOpenGLPaintDevice(int width, int height) | - |
157 | : d_ptr(new QOpenGLPaintDevicePrivate(QSize(width, height))) | - |
158 | { | - |
159 | } | 0 |
160 | | - |
161 | /*! | - |
162 | Destroys the QOpenGLPaintDevice. | - |
163 | */ | - |
164 | | - |
165 | QOpenGLPaintDevice::~QOpenGLPaintDevice() | - |
166 | { | - |
167 | delete d_ptr->engine; never executed (the execution status of this line is deduced): delete d_ptr->engine; | - |
168 | } | 0 |
169 | | - |
170 | /*! | - |
171 | \fn int QOpenGLPaintDevice::devType() const | - |
172 | \internal | - |
173 | \reimp | - |
174 | */ | - |
175 | | - |
176 | QOpenGLPaintDevicePrivate::QOpenGLPaintDevicePrivate(const QSize &sz) | - |
177 | : size(sz) | - |
178 | , ctx(QOpenGLContext::currentContext()) | - |
179 | , dpmx(qt_defaultDpiX() * 100. / 2.54) | - |
180 | , dpmy(qt_defaultDpiY() * 100. / 2.54) | - |
181 | , flipped(false) | - |
182 | , engine(0) | - |
183 | { | - |
184 | } | 0 |
185 | | - |
186 | class QOpenGLEngineThreadStorage | - |
187 | { | - |
188 | public: | - |
189 | QPaintEngine *engine() { | - |
190 | QPaintEngine *&localEngine = storage.localData(); never executed (the execution status of this line is deduced): QPaintEngine *&localEngine = storage.localData(); | - |
191 | if (!localEngine) never evaluated: !localEngine | 0 |
192 | localEngine = new QOpenGL2PaintEngineEx; never executed: localEngine = new QOpenGL2PaintEngineEx; | 0 |
193 | return localEngine; never executed: return localEngine; | 0 |
194 | } | - |
195 | | - |
196 | private: | - |
197 | QThreadStorage<QPaintEngine *> storage; | - |
198 | }; | - |
199 | | - |
200 | Q_GLOBAL_STATIC(QOpenGLEngineThreadStorage, qt_opengl_engine) never executed: delete x; never executed: return thisGlobalStatic.pointer.load(); never evaluated: !thisGlobalStatic.pointer.testAndSetOrdered(0, x) never evaluated: !thisGlobalStatic.pointer.load() never evaluated: !thisGlobalStatic.destroyed | 0 |
201 | | - |
202 | /*! | - |
203 | \reimp | - |
204 | */ | - |
205 | | - |
206 | QPaintEngine *QOpenGLPaintDevice::paintEngine() const | - |
207 | { | - |
208 | if (d_ptr->engine) never evaluated: d_ptr->engine | 0 |
209 | return d_ptr->engine; never executed: return d_ptr->engine; | 0 |
210 | | - |
211 | QPaintEngine *engine = qt_opengl_engine()->engine(); never executed (the execution status of this line is deduced): QPaintEngine *engine = qt_opengl_engine()->engine(); | - |
212 | if (engine->isActive() && engine->paintDevice() != this) { never evaluated: engine->isActive() never evaluated: engine->paintDevice() != this | 0 |
213 | d_ptr->engine = new QOpenGL2PaintEngineEx; never executed (the execution status of this line is deduced): d_ptr->engine = new QOpenGL2PaintEngineEx; | - |
214 | return d_ptr->engine; never executed: return d_ptr->engine; | 0 |
215 | } | - |
216 | | - |
217 | return engine; never executed: return engine; | 0 |
218 | } | - |
219 | | - |
220 | /*! | - |
221 | Returns the OpenGL context associated with the paint device. | - |
222 | */ | - |
223 | | - |
224 | QOpenGLContext *QOpenGLPaintDevice::context() const | - |
225 | { | - |
226 | return d_ptr->ctx; never executed: return d_ptr->ctx; | 0 |
227 | } | - |
228 | | - |
229 | /*! | - |
230 | Returns the pixel size of the paint device. | - |
231 | | - |
232 | \sa setSize() | - |
233 | */ | - |
234 | | - |
235 | QSize QOpenGLPaintDevice::size() const | - |
236 | { | - |
237 | return d_ptr->size; never executed: return d_ptr->size; | 0 |
238 | } | - |
239 | | - |
240 | /*! | - |
241 | Sets the pixel size of the paint device to \a size. | - |
242 | | - |
243 | \sa size() | - |
244 | */ | - |
245 | | - |
246 | void QOpenGLPaintDevice::setSize(const QSize &size) | - |
247 | { | - |
248 | d_ptr->size = size; never executed (the execution status of this line is deduced): d_ptr->size = size; | - |
249 | } | 0 |
250 | | - |
251 | /*! | - |
252 | \reimp | - |
253 | */ | - |
254 | | - |
255 | int QOpenGLPaintDevice::metric(QPaintDevice::PaintDeviceMetric metric) const | - |
256 | { | - |
257 | switch (metric) { | - |
258 | case PdmWidth: | - |
259 | return d_ptr->size.width(); never executed: return d_ptr->size.width(); | 0 |
260 | case PdmHeight: | - |
261 | return d_ptr->size.height(); never executed: return d_ptr->size.height(); | 0 |
262 | case PdmDepth: | - |
263 | return 32; never executed: return 32; | 0 |
264 | case PdmWidthMM: | - |
265 | return qRound(d_ptr->size.width() * 1000 / d_ptr->dpmx); never executed: return qRound(d_ptr->size.width() * 1000 / d_ptr->dpmx); | 0 |
266 | case PdmHeightMM: | - |
267 | return qRound(d_ptr->size.height() * 1000 / d_ptr->dpmy); never executed: return qRound(d_ptr->size.height() * 1000 / d_ptr->dpmy); | 0 |
268 | case PdmNumColors: | - |
269 | return 0; never executed: return 0; | 0 |
270 | case PdmDpiX: | - |
271 | return qRound(d_ptr->dpmx * 0.0254); never executed: return qRound(d_ptr->dpmx * 0.0254); | 0 |
272 | case PdmDpiY: | - |
273 | return qRound(d_ptr->dpmy * 0.0254); never executed: return qRound(d_ptr->dpmy * 0.0254); | 0 |
274 | case PdmPhysicalDpiX: | - |
275 | return qRound(d_ptr->dpmx * 0.0254); never executed: return qRound(d_ptr->dpmx * 0.0254); | 0 |
276 | case PdmPhysicalDpiY: | - |
277 | return qRound(d_ptr->dpmy * 0.0254); never executed: return qRound(d_ptr->dpmy * 0.0254); | 0 |
278 | default: | - |
279 | qWarning("QOpenGLPaintDevice::metric() - metric %d not known", metric); never executed (the execution status of this line is deduced): QMessageLogger("opengl/qopenglpaintdevice.cpp", 279, __PRETTY_FUNCTION__).warning("QOpenGLPaintDevice::metric() - metric %d not known", metric); | - |
280 | return 0; never executed: return 0; | 0 |
281 | } | - |
282 | } | 0 |
283 | | - |
284 | /*! | - |
285 | Returns the number of pixels per meter horizontally. | - |
286 | | - |
287 | \sa setDotsPerMeterX() | - |
288 | */ | - |
289 | | - |
290 | qreal QOpenGLPaintDevice::dotsPerMeterX() const | - |
291 | { | - |
292 | return d_ptr->dpmx; never executed: return d_ptr->dpmx; | 0 |
293 | } | - |
294 | | - |
295 | /*! | - |
296 | Returns the number of pixels per meter vertically. | - |
297 | | - |
298 | \sa setDotsPerMeterY() | - |
299 | */ | - |
300 | | - |
301 | qreal QOpenGLPaintDevice::dotsPerMeterY() const | - |
302 | { | - |
303 | return d_ptr->dpmy; never executed: return d_ptr->dpmy; | 0 |
304 | } | - |
305 | | - |
306 | /*! | - |
307 | Sets the number of pixels per meter horizontally to \a dpmx. | - |
308 | | - |
309 | \sa dotsPerMeterX() | - |
310 | */ | - |
311 | | - |
312 | void QOpenGLPaintDevice::setDotsPerMeterX(qreal dpmx) | - |
313 | { | - |
314 | d_ptr->dpmx = dpmx; never executed (the execution status of this line is deduced): d_ptr->dpmx = dpmx; | - |
315 | } | 0 |
316 | | - |
317 | /*! | - |
318 | Sets the number of pixels per meter vertically to \a dpmy. | - |
319 | | - |
320 | \sa dotsPerMeterY() | - |
321 | */ | - |
322 | | - |
323 | void QOpenGLPaintDevice::setDotsPerMeterY(qreal dpmy) | - |
324 | { | - |
325 | d_ptr->dpmx = dpmy; never executed (the execution status of this line is deduced): d_ptr->dpmx = dpmy; | - |
326 | } | 0 |
327 | | - |
328 | /*! | - |
329 | Sets whether painting should be flipped around the Y-axis or not to \a flipped. | - |
330 | | - |
331 | \sa paintFlipped() | - |
332 | */ | - |
333 | void QOpenGLPaintDevice::setPaintFlipped(bool flipped) | - |
334 | { | - |
335 | d_ptr->flipped = flipped; never executed (the execution status of this line is deduced): d_ptr->flipped = flipped; | - |
336 | } | 0 |
337 | | - |
338 | /*! | - |
339 | Returns true if painting is flipped around the Y-axis. | - |
340 | | - |
341 | \sa setPaintFlipped() | - |
342 | */ | - |
343 | | - |
344 | bool QOpenGLPaintDevice::paintFlipped() const | - |
345 | { | - |
346 | return d_ptr->flipped; never executed: return d_ptr->flipped; | 0 |
347 | } | - |
348 | | - |
349 | /*! | - |
350 | This virtual method is provided as a callback to allow re-binding a | - |
351 | target frame buffer object when different QOpenGLPaintDevice instances | - |
352 | are issuing draw calls alternately on the same OpenGL context. | - |
353 | | - |
354 | QPainter::beginNativePainting will also trigger this method. | - |
355 | */ | - |
356 | void QOpenGLPaintDevice::ensureActiveTarget() | - |
357 | { | - |
358 | } | - |
359 | | - |
360 | QT_END_NAMESPACE | - |
361 | | - |
| | |