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