Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/opengl/qglpixelbuffer.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 QtOpenGL 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 | /*! | - | ||||||||||||
35 | \class QGLPixelBuffer | - | ||||||||||||
36 | \inmodule QtOpenGL | - | ||||||||||||
37 | \brief The QGLPixelBuffer class encapsulates an OpenGL pbuffer. | - | ||||||||||||
38 | \since 4.1 | - | ||||||||||||
39 | \obsolete | - | ||||||||||||
40 | - | |||||||||||||
41 | \ingroup painting-3D | - | ||||||||||||
42 | - | |||||||||||||
43 | Rendering into a pbuffer is normally done using full hardware | - | ||||||||||||
44 | acceleration. This can be significantly faster than rendering | - | ||||||||||||
45 | into a QPixmap. | - | ||||||||||||
46 | - | |||||||||||||
47 | There are three approaches to using this class: | - | ||||||||||||
48 | - | |||||||||||||
49 | \list 1 | - | ||||||||||||
50 | \li \b{We can draw into the pbuffer and convert it to a QImage | - | ||||||||||||
51 | using toImage().} This is normally much faster than calling | - | ||||||||||||
52 | QGLWidget::renderPixmap(). | - | ||||||||||||
53 | - | |||||||||||||
54 | \li \b{We can draw into the pbuffer and copy the contents into | - | ||||||||||||
55 | an OpenGL texture using updateDynamicTexture().} This allows | - | ||||||||||||
56 | us to create dynamic textures and works on all systems | - | ||||||||||||
57 | with pbuffer support. | - | ||||||||||||
58 | - | |||||||||||||
59 | \li \b{On systems that support it, we can bind the pbuffer to | - | ||||||||||||
60 | an OpenGL texture.} The texture is then updated automatically | - | ||||||||||||
61 | when the pbuffer contents change, eliminating the need for | - | ||||||||||||
62 | additional copy operations. This is supported only on Windows | - | ||||||||||||
63 | and \macos systems that provide the \c render_texture | - | ||||||||||||
64 | extension. Note that under Windows, a multi-sampled pbuffer | - | ||||||||||||
65 | can't be used in conjunction with the \c render_texture | - | ||||||||||||
66 | extension. If a multi-sampled pbuffer is requested under | - | ||||||||||||
67 | Windows, the \c render_texture extension is turned off for that | - | ||||||||||||
68 | pbuffer. | - | ||||||||||||
69 | - | |||||||||||||
70 | - | |||||||||||||
71 | \endlist | - | ||||||||||||
72 | - | |||||||||||||
73 | \note This class has been deprecated, use QOpenGLFramebufferObject | - | ||||||||||||
74 | for offscreen rendering. | - | ||||||||||||
75 | - | |||||||||||||
76 | \section1 Threading | - | ||||||||||||
77 | - | |||||||||||||
78 | As of Qt 4.8, it's possible to render into a QGLPixelBuffer using | - | ||||||||||||
79 | a QPainter in a separate thread. Note that OpenGL 2.0 or OpenGL ES | - | ||||||||||||
80 | 2.0 is required for this to work. | - | ||||||||||||
81 | - | |||||||||||||
82 | Pbuffers are provided by the OpenGL \c pbuffer extension; call | - | ||||||||||||
83 | hasOpenGLPbuffer() to find out if the system provides pbuffers. | - | ||||||||||||
84 | */ | - | ||||||||||||
85 | - | |||||||||||||
86 | #include <private/qopenglextensions_p.h> | - | ||||||||||||
87 | - | |||||||||||||
88 | #include <QtCore/qglobal.h> | - | ||||||||||||
89 | #include <QtGui/qopenglframebufferobject.h> | - | ||||||||||||
90 | - | |||||||||||||
91 | #include "gl2paintengineex/qpaintengineex_opengl2_p.h" | - | ||||||||||||
92 | - | |||||||||||||
93 | #include <qglframebufferobject.h> | - | ||||||||||||
94 | #include <qglpixelbuffer.h> | - | ||||||||||||
95 | #include <private/qglpixelbuffer_p.h> | - | ||||||||||||
96 | #include <private/qfont_p.h> | - | ||||||||||||
97 | #include <qimage.h> | - | ||||||||||||
98 | - | |||||||||||||
99 | QT_BEGIN_NAMESPACE | - | ||||||||||||
100 | - | |||||||||||||
101 | extern QImage qt_gl_read_frame_buffer(const QSize&, bool, bool); | - | ||||||||||||
102 | - | |||||||||||||
103 | - | |||||||||||||
104 | QGLContext* QGLPBufferGLPaintDevice::context() const | - | ||||||||||||
105 | { | - | ||||||||||||
106 | return pbuf->d_func()->qctx; never executed: return pbuf->d_func()->qctx; | 0 | ||||||||||||
107 | } | - | ||||||||||||
108 | - | |||||||||||||
109 | void QGLPBufferGLPaintDevice::beginPaint() | - | ||||||||||||
110 | { | - | ||||||||||||
111 | pbuf->makeCurrent(); | - | ||||||||||||
112 | QGLPaintDevice::beginPaint(); | - | ||||||||||||
113 | } never executed: end of block | 0 | ||||||||||||
114 | - | |||||||||||||
115 | void QGLPBufferGLPaintDevice::endPaint() | - | ||||||||||||
116 | { | - | ||||||||||||
117 | QOpenGLContext::currentContext()->functions()->glFlush(); | - | ||||||||||||
118 | QGLPaintDevice::endPaint(); | - | ||||||||||||
119 | } never executed: end of block | 0 | ||||||||||||
120 | - | |||||||||||||
121 | void QGLPBufferGLPaintDevice::setFbo(GLuint fbo) | - | ||||||||||||
122 | { | - | ||||||||||||
123 | m_thisFBO = fbo; | - | ||||||||||||
124 | } never executed: end of block | 0 | ||||||||||||
125 | - | |||||||||||||
126 | void QGLPBufferGLPaintDevice::setPBuffer(QGLPixelBuffer* pb) | - | ||||||||||||
127 | { | - | ||||||||||||
128 | pbuf = pb; | - | ||||||||||||
129 | } never executed: end of block | 0 | ||||||||||||
130 | - | |||||||||||||
131 | void QGLPixelBufferPrivate::common_init(const QSize &size, const QGLFormat &format, QGLWidget *shareWidget) | - | ||||||||||||
132 | { | - | ||||||||||||
133 | Q_Q(QGLPixelBuffer); | - | ||||||||||||
134 | if(init(size, format, shareWidget)) {
| 0 | ||||||||||||
135 | req_size = size; | - | ||||||||||||
136 | req_format = format; | - | ||||||||||||
137 | req_shareWidget = shareWidget; | - | ||||||||||||
138 | invalid = false; | - | ||||||||||||
139 | glDevice.setPBuffer(q); | - | ||||||||||||
140 | } never executed: end of block | 0 | ||||||||||||
141 | } never executed: end of block | 0 | ||||||||||||
142 | - | |||||||||||||
143 | /*! | - | ||||||||||||
144 | Constructs an OpenGL pbuffer of the given \a size. If no \a | - | ||||||||||||
145 | format is specified, the \l{QGLFormat::defaultFormat()}{default | - | ||||||||||||
146 | format} is used. If the \a shareWidget parameter points to a | - | ||||||||||||
147 | valid QGLWidget, the pbuffer will share its context with \a | - | ||||||||||||
148 | shareWidget. | - | ||||||||||||
149 | - | |||||||||||||
150 | If you intend to bind this pbuffer as a dynamic texture, the width | - | ||||||||||||
151 | and height components of \c size must be powers of two (e.g., 512 | - | ||||||||||||
152 | x 128). | - | ||||||||||||
153 | - | |||||||||||||
154 | \sa size(), format() | - | ||||||||||||
155 | */ | - | ||||||||||||
156 | QGLPixelBuffer::QGLPixelBuffer(const QSize &size, const QGLFormat &format, QGLWidget *shareWidget) | - | ||||||||||||
157 | : d_ptr(new QGLPixelBufferPrivate(this)) | - | ||||||||||||
158 | { | - | ||||||||||||
159 | Q_D(QGLPixelBuffer); | - | ||||||||||||
160 | d->common_init(size, format, shareWidget); | - | ||||||||||||
161 | } never executed: end of block | 0 | ||||||||||||
162 | - | |||||||||||||
163 | - | |||||||||||||
164 | /*! \overload | - | ||||||||||||
165 | - | |||||||||||||
166 | Constructs an OpenGL pbuffer with the \a width and \a height. If | - | ||||||||||||
167 | no \a format is specified, the | - | ||||||||||||
168 | \l{QGLFormat::defaultFormat()}{default format} is used. If the \a | - | ||||||||||||
169 | shareWidget parameter points to a valid QGLWidget, the pbuffer | - | ||||||||||||
170 | will share its context with \a shareWidget. | - | ||||||||||||
171 | - | |||||||||||||
172 | If you intend to bind this pbuffer as a dynamic texture, the width | - | ||||||||||||
173 | and height components of \c size must be powers of two (e.g., 512 | - | ||||||||||||
174 | x 128). | - | ||||||||||||
175 | - | |||||||||||||
176 | \sa size(), format() | - | ||||||||||||
177 | */ | - | ||||||||||||
178 | QGLPixelBuffer::QGLPixelBuffer(int width, int height, const QGLFormat &format, QGLWidget *shareWidget) | - | ||||||||||||
179 | : d_ptr(new QGLPixelBufferPrivate(this)) | - | ||||||||||||
180 | { | - | ||||||||||||
181 | Q_D(QGLPixelBuffer); | - | ||||||||||||
182 | d->common_init(QSize(width, height), format, shareWidget); | - | ||||||||||||
183 | } never executed: end of block | 0 | ||||||||||||
184 | - | |||||||||||||
185 | - | |||||||||||||
186 | /*! \fn QGLPixelBuffer::~QGLPixelBuffer() | - | ||||||||||||
187 | - | |||||||||||||
188 | Destroys the pbuffer and frees any allocated resources. | - | ||||||||||||
189 | */ | - | ||||||||||||
190 | QGLPixelBuffer::~QGLPixelBuffer() | - | ||||||||||||
191 | { | - | ||||||||||||
192 | Q_D(QGLPixelBuffer); | - | ||||||||||||
193 | - | |||||||||||||
194 | // defined in qpaintengine_opengl.cpp | - | ||||||||||||
195 | QGLContext *current = const_cast<QGLContext *>(QGLContext::currentContext()); | - | ||||||||||||
196 | if (current != d->qctx)
| 0 | ||||||||||||
197 | makeCurrent(); never executed: makeCurrent(); | 0 | ||||||||||||
198 | d->cleanup(); | - | ||||||||||||
199 | if (current && current != d->qctx)
| 0 | ||||||||||||
200 | current->makeCurrent(); never executed: current->makeCurrent(); | 0 | ||||||||||||
201 | } never executed: end of block | 0 | ||||||||||||
202 | - | |||||||||||||
203 | /*! \fn bool QGLPixelBuffer::makeCurrent() | - | ||||||||||||
204 | - | |||||||||||||
205 | Makes this pbuffer the current OpenGL rendering context. Returns | - | ||||||||||||
206 | true on success; otherwise returns \c false. | - | ||||||||||||
207 | - | |||||||||||||
208 | \sa QGLContext::makeCurrent(), doneCurrent() | - | ||||||||||||
209 | */ | - | ||||||||||||
210 | - | |||||||||||||
211 | bool QGLPixelBuffer::makeCurrent() | - | ||||||||||||
212 | { | - | ||||||||||||
213 | Q_D(QGLPixelBuffer); | - | ||||||||||||
214 | if (d->invalid)
| 0 | ||||||||||||
215 | return false; never executed: return false; | 0 | ||||||||||||
216 | d->qctx->makeCurrent(); | - | ||||||||||||
217 | if (!d->fbo) {
| 0 | ||||||||||||
218 | QOpenGLFramebufferObjectFormat format; | - | ||||||||||||
219 | if (d->req_format.stencil())
| 0 | ||||||||||||
220 | format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil); never executed: format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil); | 0 | ||||||||||||
221 | else if (d->req_format.depth())
| 0 | ||||||||||||
222 | format.setAttachment(QOpenGLFramebufferObject::Depth); never executed: format.setAttachment(QOpenGLFramebufferObject::Depth); | 0 | ||||||||||||
223 | if (d->req_format.sampleBuffers())
| 0 | ||||||||||||
224 | format.setSamples(d->req_format.samples()); never executed: format.setSamples(d->req_format.samples()); | 0 | ||||||||||||
225 | d->fbo = new QOpenGLFramebufferObject(d->req_size, format); | - | ||||||||||||
226 | d->fbo->bind(); | - | ||||||||||||
227 | d->glDevice.setFbo(d->fbo->handle()); | - | ||||||||||||
228 | QOpenGLContext::currentContext()->functions()->glViewport(0, 0, d->req_size.width(), d->req_size.height()); | - | ||||||||||||
229 | } never executed: end of block | 0 | ||||||||||||
230 | return true; never executed: return true; | 0 | ||||||||||||
231 | } | - | ||||||||||||
232 | - | |||||||||||||
233 | /*! \fn bool QGLPixelBuffer::doneCurrent() | - | ||||||||||||
234 | - | |||||||||||||
235 | Makes no context the current OpenGL context. Returns \c true on | - | ||||||||||||
236 | success; otherwise returns \c false. | - | ||||||||||||
237 | */ | - | ||||||||||||
238 | - | |||||||||||||
239 | bool QGLPixelBuffer::doneCurrent() | - | ||||||||||||
240 | { | - | ||||||||||||
241 | Q_D(QGLPixelBuffer); | - | ||||||||||||
242 | if (d->invalid)
| 0 | ||||||||||||
243 | return false; never executed: return false; | 0 | ||||||||||||
244 | d->qctx->doneCurrent(); | - | ||||||||||||
245 | return true; never executed: return true; | 0 | ||||||||||||
246 | } | - | ||||||||||||
247 | - | |||||||||||||
248 | /*! | - | ||||||||||||
249 | Returns the context of this pixelbuffer. | - | ||||||||||||
250 | */ | - | ||||||||||||
251 | QGLContext *QGLPixelBuffer::context() const | - | ||||||||||||
252 | { | - | ||||||||||||
253 | Q_D(const QGLPixelBuffer); | - | ||||||||||||
254 | return d->qctx; never executed: return d->qctx; | 0 | ||||||||||||
255 | } | - | ||||||||||||
256 | - | |||||||||||||
257 | /*! | - | ||||||||||||
258 | \fn GLuint QGLPixelBuffer::generateDynamicTexture() const | - | ||||||||||||
259 | - | |||||||||||||
260 | Generates and binds a 2D GL texture that is the same size as the | - | ||||||||||||
261 | pbuffer, and returns the texture's ID. This can be used in | - | ||||||||||||
262 | conjunction with bindToDynamicTexture() and | - | ||||||||||||
263 | updateDynamicTexture(). | - | ||||||||||||
264 | - | |||||||||||||
265 | \sa size() | - | ||||||||||||
266 | */ | - | ||||||||||||
267 | - | |||||||||||||
268 | /*! \fn bool QGLPixelBuffer::bindToDynamicTexture(GLuint texture_id) | - | ||||||||||||
269 | - | |||||||||||||
270 | Binds the texture specified by \a texture_id to this pbuffer. | - | ||||||||||||
271 | Returns \c true on success; otherwise returns \c false. | - | ||||||||||||
272 | - | |||||||||||||
273 | The texture must be of the same size and format as the pbuffer. | - | ||||||||||||
274 | - | |||||||||||||
275 | To unbind the texture, call releaseFromDynamicTexture(). While | - | ||||||||||||
276 | the texture is bound, it is updated automatically when the | - | ||||||||||||
277 | pbuffer contents change, eliminating the need for additional copy | - | ||||||||||||
278 | operations. | - | ||||||||||||
279 | - | |||||||||||||
280 | Example: | - | ||||||||||||
281 | - | |||||||||||||
282 | \snippet code/src_opengl_qglpixelbuffer.cpp 0 | - | ||||||||||||
283 | - | |||||||||||||
284 | \warning This function uses the \c {render_texture} extension, | - | ||||||||||||
285 | which is currently not supported under X11. An alternative that | - | ||||||||||||
286 | works on all systems (including X11) is to manually copy the | - | ||||||||||||
287 | pbuffer contents to a texture using updateDynamicTexture(). | - | ||||||||||||
288 | - | |||||||||||||
289 | \warning For the bindToDynamicTexture() call to succeed on the | - | ||||||||||||
290 | \macos, the pbuffer needs a shared context, i.e. the | - | ||||||||||||
291 | QGLPixelBuffer must be created with a share widget. | - | ||||||||||||
292 | - | |||||||||||||
293 | \sa generateDynamicTexture(), releaseFromDynamicTexture() | - | ||||||||||||
294 | */ | - | ||||||||||||
295 | - | |||||||||||||
296 | /*! \fn void QGLPixelBuffer::releaseFromDynamicTexture() | - | ||||||||||||
297 | - | |||||||||||||
298 | Releases the pbuffer from any previously bound texture. | - | ||||||||||||
299 | - | |||||||||||||
300 | \sa bindToDynamicTexture() | - | ||||||||||||
301 | */ | - | ||||||||||||
302 | - | |||||||||||||
303 | /*! \fn bool QGLPixelBuffer::hasOpenGLPbuffers() | - | ||||||||||||
304 | - | |||||||||||||
305 | Returns \c true if the OpenGL \c pbuffer extension is present on | - | ||||||||||||
306 | this system; otherwise returns \c false. | - | ||||||||||||
307 | */ | - | ||||||||||||
308 | - | |||||||||||||
309 | /*! | - | ||||||||||||
310 | Copies the pbuffer contents into the texture specified with \a | - | ||||||||||||
311 | texture_id. | - | ||||||||||||
312 | - | |||||||||||||
313 | The texture must be of the same size and format as the pbuffer. | - | ||||||||||||
314 | - | |||||||||||||
315 | Example: | - | ||||||||||||
316 | - | |||||||||||||
317 | \snippet code/src_opengl_qglpixelbuffer.cpp 1 | - | ||||||||||||
318 | - | |||||||||||||
319 | An alternative on Windows and \macos systems that support the | - | ||||||||||||
320 | \c render_texture extension is to use bindToDynamicTexture() to | - | ||||||||||||
321 | get dynamic updates of the texture. | - | ||||||||||||
322 | - | |||||||||||||
323 | \sa generateDynamicTexture(), bindToDynamicTexture() | - | ||||||||||||
324 | */ | - | ||||||||||||
325 | void QGLPixelBuffer::updateDynamicTexture(GLuint texture_id) const | - | ||||||||||||
326 | { | - | ||||||||||||
327 | Q_D(const QGLPixelBuffer); | - | ||||||||||||
328 | if (d->invalid || !d->fbo)
| 0 | ||||||||||||
329 | return; never executed: return; | 0 | ||||||||||||
330 | - | |||||||||||||
331 | const QGLContext *ctx = QGLContext::currentContext(); | - | ||||||||||||
332 | if (!ctx)
| 0 | ||||||||||||
333 | return; never executed: return; | 0 | ||||||||||||
334 | - | |||||||||||||
335 | #undef glBindFramebuffer | - | ||||||||||||
336 | - | |||||||||||||
337 | #ifndef GL_READ_FRAMEBUFFER | - | ||||||||||||
338 | #define GL_READ_FRAMEBUFFER 0x8CA8 | - | ||||||||||||
339 | #endif | - | ||||||||||||
340 | - | |||||||||||||
341 | #ifndef GL_DRAW_FRAMEBUFFER | - | ||||||||||||
342 | #define GL_DRAW_FRAMEBUFFER 0x8CA9 | - | ||||||||||||
343 | #endif | - | ||||||||||||
344 | - | |||||||||||||
345 | QOpenGLExtensions extensions(ctx->contextHandle()); | - | ||||||||||||
346 | - | |||||||||||||
347 | ctx->d_ptr->refreshCurrentFbo(); | - | ||||||||||||
348 | - | |||||||||||||
349 | if (d->blit_fbo) {
| 0 | ||||||||||||
350 | QOpenGLFramebufferObject::blitFramebuffer(d->blit_fbo, d->fbo); | - | ||||||||||||
351 | extensions.glBindFramebuffer(GL_READ_FRAMEBUFFER, d->blit_fbo->handle()); | - | ||||||||||||
352 | } never executed: end of block | 0 | ||||||||||||
353 | - | |||||||||||||
354 | extensions.glBindTexture(GL_TEXTURE_2D, texture_id); | - | ||||||||||||
355 | #ifndef QT_OPENGL_ES | - | ||||||||||||
356 | GLenum format = ctx->contextHandle()->isOpenGLES() ? GL_RGBA : GL_RGBA8;
| 0 | ||||||||||||
357 | extensions.glCopyTexImage2D(GL_TEXTURE_2D, 0, format, 0, 0, d->req_size.width(), d->req_size.height(), 0); | - | ||||||||||||
358 | #else | - | ||||||||||||
359 | extensions.glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, d->req_size.width(), d->req_size.height(), 0); | - | ||||||||||||
360 | #endif | - | ||||||||||||
361 | - | |||||||||||||
362 | if (d->blit_fbo)
| 0 | ||||||||||||
363 | extensions.glBindFramebuffer(GL_READ_FRAMEBUFFER, ctx->d_func()->current_fbo); never executed: extensions.glBindFramebuffer(0x8CA8, ctx->d_func()->current_fbo); | 0 | ||||||||||||
364 | } never executed: end of block | 0 | ||||||||||||
365 | - | |||||||||||||
366 | /*! | - | ||||||||||||
367 | Returns the size of the pbuffer. | - | ||||||||||||
368 | */ | - | ||||||||||||
369 | QSize QGLPixelBuffer::size() const | - | ||||||||||||
370 | { | - | ||||||||||||
371 | Q_D(const QGLPixelBuffer); | - | ||||||||||||
372 | return d->req_size; never executed: return d->req_size; | 0 | ||||||||||||
373 | } | - | ||||||||||||
374 | - | |||||||||||||
375 | /*! | - | ||||||||||||
376 | Returns the contents of the pbuffer as a QImage. | - | ||||||||||||
377 | */ | - | ||||||||||||
378 | QImage QGLPixelBuffer::toImage() const | - | ||||||||||||
379 | { | - | ||||||||||||
380 | Q_D(const QGLPixelBuffer); | - | ||||||||||||
381 | if (d->invalid)
| 0 | ||||||||||||
382 | return QImage(); never executed: return QImage(); | 0 | ||||||||||||
383 | - | |||||||||||||
384 | const_cast<QGLPixelBuffer *>(this)->makeCurrent(); | - | ||||||||||||
385 | if (d->fbo)
| 0 | ||||||||||||
386 | d->fbo->bind(); never executed: d->fbo->bind(); | 0 | ||||||||||||
387 | return qt_gl_read_frame_buffer(d->req_size, d->format.alpha(), true); never executed: return qt_gl_read_frame_buffer(d->req_size, d->format.alpha(), true); | 0 | ||||||||||||
388 | } | - | ||||||||||||
389 | - | |||||||||||||
390 | /*! | - | ||||||||||||
391 | Returns the native pbuffer handle. | - | ||||||||||||
392 | */ | - | ||||||||||||
393 | Qt::HANDLE QGLPixelBuffer::handle() const | - | ||||||||||||
394 | { | - | ||||||||||||
395 | Q_D(const QGLPixelBuffer); | - | ||||||||||||
396 | if (d->invalid)
| 0 | ||||||||||||
397 | return 0; never executed: return 0; | 0 | ||||||||||||
398 | return (Qt::HANDLE) d->pbuf; never executed: return (Qt::HANDLE) d->pbuf; | 0 | ||||||||||||
399 | } | - | ||||||||||||
400 | - | |||||||||||||
401 | /*! | - | ||||||||||||
402 | Returns \c true if this pbuffer is valid; otherwise returns \c false. | - | ||||||||||||
403 | */ | - | ||||||||||||
404 | bool QGLPixelBuffer::isValid() const | - | ||||||||||||
405 | { | - | ||||||||||||
406 | Q_D(const QGLPixelBuffer); | - | ||||||||||||
407 | return !d->invalid; never executed: return !d->invalid; | 0 | ||||||||||||
408 | } | - | ||||||||||||
409 | - | |||||||||||||
410 | Q_GLOBAL_STATIC(QGLEngineThreadStorage<QGL2PaintEngineEx>, qt_buffer_2_engine) never executed: end of block never executed: guard.store(QtGlobalStatic::Destroyed); never executed: return &holder.value;
| 0 | ||||||||||||
411 | - | |||||||||||||
412 | /*! \reimp */ | - | ||||||||||||
413 | QPaintEngine *QGLPixelBuffer::paintEngine() const | - | ||||||||||||
414 | { | - | ||||||||||||
415 | return qt_buffer_2_engine()->engine(); never executed: return qt_buffer_2_engine()->engine(); | 0 | ||||||||||||
416 | } | - | ||||||||||||
417 | - | |||||||||||||
418 | /*! \reimp */ | - | ||||||||||||
419 | int QGLPixelBuffer::metric(PaintDeviceMetric metric) const | - | ||||||||||||
420 | { | - | ||||||||||||
421 | Q_D(const QGLPixelBuffer); | - | ||||||||||||
422 | - | |||||||||||||
423 | float dpmx = qt_defaultDpiX()*100./2.54; | - | ||||||||||||
424 | float dpmy = qt_defaultDpiY()*100./2.54; | - | ||||||||||||
425 | int w = d->req_size.width(); | - | ||||||||||||
426 | int h = d->req_size.height(); | - | ||||||||||||
427 | switch (metric) { | - | ||||||||||||
428 | case PdmWidth: never executed: case PdmWidth: | 0 | ||||||||||||
429 | return w; never executed: return w; | 0 | ||||||||||||
430 | - | |||||||||||||
431 | case PdmHeight: never executed: case PdmHeight: | 0 | ||||||||||||
432 | return h; never executed: return h; | 0 | ||||||||||||
433 | - | |||||||||||||
434 | case PdmWidthMM: never executed: case PdmWidthMM: | 0 | ||||||||||||
435 | return qRound(w * 1000 / dpmx); never executed: return qRound(w * 1000 / dpmx); | 0 | ||||||||||||
436 | - | |||||||||||||
437 | case PdmHeightMM: never executed: case PdmHeightMM: | 0 | ||||||||||||
438 | return qRound(h * 1000 / dpmy); never executed: return qRound(h * 1000 / dpmy); | 0 | ||||||||||||
439 | - | |||||||||||||
440 | case PdmNumColors: never executed: case PdmNumColors: | 0 | ||||||||||||
441 | return 0; never executed: return 0; | 0 | ||||||||||||
442 | - | |||||||||||||
443 | case PdmDepth: never executed: case PdmDepth: | 0 | ||||||||||||
444 | return 32;//d->depth; never executed: return 32; | 0 | ||||||||||||
445 | - | |||||||||||||
446 | case PdmDpiX: never executed: case PdmDpiX: | 0 | ||||||||||||
447 | return qRound(dpmx * 0.0254); never executed: return qRound(dpmx * 0.0254); | 0 | ||||||||||||
448 | - | |||||||||||||
449 | case PdmDpiY: never executed: case PdmDpiY: | 0 | ||||||||||||
450 | return qRound(dpmy * 0.0254); never executed: return qRound(dpmy * 0.0254); | 0 | ||||||||||||
451 | - | |||||||||||||
452 | case PdmPhysicalDpiX: never executed: case PdmPhysicalDpiX: | 0 | ||||||||||||
453 | return qRound(dpmx * 0.0254); never executed: return qRound(dpmx * 0.0254); | 0 | ||||||||||||
454 | - | |||||||||||||
455 | case PdmPhysicalDpiY: never executed: case PdmPhysicalDpiY: | 0 | ||||||||||||
456 | return qRound(dpmy * 0.0254); never executed: return qRound(dpmy * 0.0254); | 0 | ||||||||||||
457 | - | |||||||||||||
458 | case QPaintDevice::PdmDevicePixelRatio: never executed: case QPaintDevice::PdmDevicePixelRatio: | 0 | ||||||||||||
459 | return 1; never executed: return 1; | 0 | ||||||||||||
460 | - | |||||||||||||
461 | case QPaintDevice::PdmDevicePixelRatioScaled: never executed: case QPaintDevice::PdmDevicePixelRatioScaled: | 0 | ||||||||||||
462 | return QPaintDevice::devicePixelRatioFScale(); never executed: return QPaintDevice::devicePixelRatioFScale(); | 0 | ||||||||||||
463 | - | |||||||||||||
464 | default: never executed: default: | 0 | ||||||||||||
465 | qWarning("QGLPixelBuffer::metric(), Unhandled metric type: %d\n", metric); | - | ||||||||||||
466 | break; never executed: break; | 0 | ||||||||||||
467 | } | - | ||||||||||||
468 | return 0; never executed: return 0; | 0 | ||||||||||||
469 | } | - | ||||||||||||
470 | - | |||||||||||||
471 | /*! | - | ||||||||||||
472 | Generates and binds a 2D GL texture to the current context, based | - | ||||||||||||
473 | on \a image. The generated texture id is returned and can be used | - | ||||||||||||
474 | in later glBindTexture() calls. | - | ||||||||||||
475 | - | |||||||||||||
476 | The \a target parameter specifies the texture target. | - | ||||||||||||
477 | - | |||||||||||||
478 | Equivalent to calling QGLContext::bindTexture(). | - | ||||||||||||
479 | - | |||||||||||||
480 | \sa deleteTexture() | - | ||||||||||||
481 | */ | - | ||||||||||||
482 | GLuint QGLPixelBuffer::bindTexture(const QImage &image, GLenum target) | - | ||||||||||||
483 | { | - | ||||||||||||
484 | Q_D(QGLPixelBuffer); | - | ||||||||||||
485 | #ifndef QT_OPENGL_ES | - | ||||||||||||
486 | GLenum format = QOpenGLContext::currentContext()->isOpenGLES() ? GL_RGBA : GL_RGBA8;
| 0 | ||||||||||||
487 | return d->qctx->bindTexture(image, target, GLint(format)); never executed: return d->qctx->bindTexture(image, target, GLint(format)); | 0 | ||||||||||||
488 | #else | - | ||||||||||||
489 | return d->qctx->bindTexture(image, target, GL_RGBA); | - | ||||||||||||
490 | #endif | - | ||||||||||||
491 | } | - | ||||||||||||
492 | - | |||||||||||||
493 | /*! \overload | - | ||||||||||||
494 | - | |||||||||||||
495 | Generates and binds a 2D GL texture based on \a pixmap. | - | ||||||||||||
496 | - | |||||||||||||
497 | Equivalent to calling QGLContext::bindTexture(). | - | ||||||||||||
498 | - | |||||||||||||
499 | \sa deleteTexture() | - | ||||||||||||
500 | */ | - | ||||||||||||
501 | GLuint QGLPixelBuffer::bindTexture(const QPixmap &pixmap, GLenum target) | - | ||||||||||||
502 | { | - | ||||||||||||
503 | Q_D(QGLPixelBuffer); | - | ||||||||||||
504 | #ifndef QT_OPENGL_ES | - | ||||||||||||
505 | GLenum format = QOpenGLContext::currentContext()->isOpenGLES() ? GL_RGBA : GL_RGBA8;
| 0 | ||||||||||||
506 | return d->qctx->bindTexture(pixmap, target, GLint(format)); never executed: return d->qctx->bindTexture(pixmap, target, GLint(format)); | 0 | ||||||||||||
507 | #else | - | ||||||||||||
508 | return d->qctx->bindTexture(pixmap, target, GL_RGBA); | - | ||||||||||||
509 | #endif | - | ||||||||||||
510 | } | - | ||||||||||||
511 | - | |||||||||||||
512 | /*! \overload | - | ||||||||||||
513 | - | |||||||||||||
514 | Reads the DirectDrawSurface (DDS) compressed file \a fileName and | - | ||||||||||||
515 | generates a 2D GL texture from it. | - | ||||||||||||
516 | - | |||||||||||||
517 | Equivalent to calling QGLContext::bindTexture(). | - | ||||||||||||
518 | - | |||||||||||||
519 | \sa deleteTexture() | - | ||||||||||||
520 | */ | - | ||||||||||||
521 | GLuint QGLPixelBuffer::bindTexture(const QString &fileName) | - | ||||||||||||
522 | { | - | ||||||||||||
523 | Q_D(QGLPixelBuffer); | - | ||||||||||||
524 | return d->qctx->bindTexture(fileName); never executed: return d->qctx->bindTexture(fileName); | 0 | ||||||||||||
525 | } | - | ||||||||||||
526 | - | |||||||||||||
527 | /*! | - | ||||||||||||
528 | Removes the texture identified by \a texture_id from the texture cache. | - | ||||||||||||
529 | - | |||||||||||||
530 | Equivalent to calling QGLContext::deleteTexture(). | - | ||||||||||||
531 | */ | - | ||||||||||||
532 | void QGLPixelBuffer::deleteTexture(GLuint texture_id) | - | ||||||||||||
533 | { | - | ||||||||||||
534 | Q_D(QGLPixelBuffer); | - | ||||||||||||
535 | d->qctx->deleteTexture(texture_id); | - | ||||||||||||
536 | } never executed: end of block | 0 | ||||||||||||
537 | - | |||||||||||||
538 | /*! | - | ||||||||||||
539 | \since 4.4 | - | ||||||||||||
540 | - | |||||||||||||
541 | Draws the given texture, \a textureId, to the given target rectangle, | - | ||||||||||||
542 | \a target, in OpenGL model space. The \a textureTarget should be a 2D | - | ||||||||||||
543 | texture target. | - | ||||||||||||
544 | - | |||||||||||||
545 | Equivalent to the corresponding QGLContext::drawTexture(). | - | ||||||||||||
546 | */ | - | ||||||||||||
547 | void QGLPixelBuffer::drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget) | - | ||||||||||||
548 | { | - | ||||||||||||
549 | Q_D(QGLPixelBuffer); | - | ||||||||||||
550 | d->qctx->drawTexture(target, textureId, textureTarget); | - | ||||||||||||
551 | } never executed: end of block | 0 | ||||||||||||
552 | - | |||||||||||||
553 | /*! | - | ||||||||||||
554 | \since 4.4 | - | ||||||||||||
555 | - | |||||||||||||
556 | Draws the given texture, \a textureId, at the given \a point in OpenGL model | - | ||||||||||||
557 | space. The textureTarget parameter should be a 2D texture target. | - | ||||||||||||
558 | - | |||||||||||||
559 | Equivalent to the corresponding QGLContext::drawTexture(). | - | ||||||||||||
560 | */ | - | ||||||||||||
561 | void QGLPixelBuffer::drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget) | - | ||||||||||||
562 | { | - | ||||||||||||
563 | Q_D(QGLPixelBuffer); | - | ||||||||||||
564 | d->qctx->drawTexture(point, textureId, textureTarget); | - | ||||||||||||
565 | } never executed: end of block | 0 | ||||||||||||
566 | - | |||||||||||||
567 | /*! | - | ||||||||||||
568 | Returns the format of the pbuffer. The format may be different | - | ||||||||||||
569 | from the one that was requested. | - | ||||||||||||
570 | */ | - | ||||||||||||
571 | QGLFormat QGLPixelBuffer::format() const | - | ||||||||||||
572 | { | - | ||||||||||||
573 | Q_D(const QGLPixelBuffer); | - | ||||||||||||
574 | return d->format; never executed: return d->format; | 0 | ||||||||||||
575 | } | - | ||||||||||||
576 | - | |||||||||||||
577 | /*! \fn int QGLPixelBuffer::devType() const | - | ||||||||||||
578 | \internal | - | ||||||||||||
579 | */ | - | ||||||||||||
580 | - | |||||||||||||
581 | bool QGLPixelBufferPrivate::init(const QSize &, const QGLFormat &f, QGLWidget *shareWidget) | - | ||||||||||||
582 | { | - | ||||||||||||
583 | widget = new QGLWidget(f, 0, shareWidget); | - | ||||||||||||
584 | widget->resize(1, 1); | - | ||||||||||||
585 | qctx = const_cast<QGLContext *>(widget->context()); | - | ||||||||||||
586 | return widget->isValid(); never executed: return widget->isValid(); | 0 | ||||||||||||
587 | } | - | ||||||||||||
588 | - | |||||||||||||
589 | bool QGLPixelBufferPrivate::cleanup() | - | ||||||||||||
590 | { | - | ||||||||||||
591 | delete fbo; | - | ||||||||||||
592 | fbo = 0; | - | ||||||||||||
593 | delete blit_fbo; | - | ||||||||||||
594 | blit_fbo = 0; | - | ||||||||||||
595 | delete widget; | - | ||||||||||||
596 | widget = 0; | - | ||||||||||||
597 | return true; never executed: return true; | 0 | ||||||||||||
598 | } | - | ||||||||||||
599 | - | |||||||||||||
600 | bool QGLPixelBuffer::bindToDynamicTexture(GLuint texture_id) | - | ||||||||||||
601 | { | - | ||||||||||||
602 | Q_UNUSED(texture_id); | - | ||||||||||||
603 | return false; never executed: return false; | 0 | ||||||||||||
604 | } | - | ||||||||||||
605 | - | |||||||||||||
606 | void QGLPixelBuffer::releaseFromDynamicTexture() | - | ||||||||||||
607 | { | - | ||||||||||||
608 | } | - | ||||||||||||
609 | - | |||||||||||||
610 | GLuint QGLPixelBuffer::generateDynamicTexture() const | - | ||||||||||||
611 | { | - | ||||||||||||
612 | Q_D(const QGLPixelBuffer); | - | ||||||||||||
613 | if (!d->fbo)
| 0 | ||||||||||||
614 | return 0; never executed: return 0; | 0 | ||||||||||||
615 | - | |||||||||||||
616 | if (d->fbo->format().samples() > 0
| 0 | ||||||||||||
617 | && QOpenGLExtensions(QOpenGLContext::currentContext())
| 0 | ||||||||||||
618 | .hasOpenGLExtension(QOpenGLExtensions::FramebufferBlit))
| 0 | ||||||||||||
619 | { | - | ||||||||||||
620 | if (!d->blit_fbo)
| 0 | ||||||||||||
621 | const_cast<QOpenGLFramebufferObject *&>(d->blit_fbo) = new QOpenGLFramebufferObject(d->req_size); never executed: const_cast<QOpenGLFramebufferObject *&>(d->blit_fbo) = new QOpenGLFramebufferObject(d->req_size); | 0 | ||||||||||||
622 | } else { never executed: end of block | 0 | ||||||||||||
623 | return d->fbo->texture(); never executed: return d->fbo->texture(); | 0 | ||||||||||||
624 | } | - | ||||||||||||
625 | - | |||||||||||||
626 | GLuint texture; | - | ||||||||||||
627 | QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions(); | - | ||||||||||||
628 | - | |||||||||||||
629 | funcs->glGenTextures(1, &texture); | - | ||||||||||||
630 | funcs->glBindTexture(GL_TEXTURE_2D, texture); | - | ||||||||||||
631 | - | |||||||||||||
632 | funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | - | ||||||||||||
633 | funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | - | ||||||||||||
634 | funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | - | ||||||||||||
635 | funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | - | ||||||||||||
636 | - | |||||||||||||
637 | funcs->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, d->req_size.width(), d->req_size.height(), 0, | - | ||||||||||||
638 | GL_RGBA, GL_UNSIGNED_BYTE, 0); | - | ||||||||||||
639 | - | |||||||||||||
640 | return texture; never executed: return texture; | 0 | ||||||||||||
641 | } | - | ||||||||||||
642 | - | |||||||||||||
643 | bool QGLPixelBuffer::hasOpenGLPbuffers() | - | ||||||||||||
644 | { | - | ||||||||||||
645 | return QGLFramebufferObject::hasOpenGLFramebufferObjects(); never executed: return QGLFramebufferObject::hasOpenGLFramebufferObjects(); | 0 | ||||||||||||
646 | } | - | ||||||||||||
647 | - | |||||||||||||
648 | QT_END_NAMESPACE | - | ||||||||||||
Source code | Switch to Preprocessed file |