qoffscreensurface.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/gui/kernel/qoffscreensurface.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
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 "qoffscreensurface.h"-
35-
36#include "qguiapplication_p.h"-
37#include "qscreen.h"-
38#include "qplatformintegration.h"-
39#include "qplatformoffscreensurface.h"-
40#include "qwindow.h"-
41#include "qplatformwindow.h"-
42-
43QT_BEGIN_NAMESPACE-
44-
45/*!-
46 \class QOffscreenSurface-
47 \inmodule QtGui-
48 \since 5.1-
49 \brief The QOffscreenSurface class represents an offscreen surface in the underlying platform.-
50-
51 QOffscreenSurface is intended to be used with QOpenGLContext to allow rendering with OpenGL in-
52 an arbitrary thread without the need to create a QWindow.-
53-
54 Even though the surface is typically renderable, the surface's pixels are not accessible.-
55 QOffscreenSurface should only be used to create OpenGL resources such as textures-
56 or framebuffer objects.-
57-
58 An application will typically use QOffscreenSurface to perform some time-consuming tasks in a-
59 separate thread in order to avoid stalling the main rendering thread. Resources created in the-
60 QOffscreenSurface's context can be shared with the main OpenGL context. Some common use cases-
61 are asynchronous texture uploads or rendering into a QOpenGLFramebufferObject.-
62-
63 How the offscreen surface is implemented depends on the underlying platform, but it will-
64 typically use a pixel buffer (pbuffer). If the platform doesn't implement or support-
65 offscreen surfaces, QOffscreenSurface will use an invisible QWindow internally.-
66-
67 \note Due to the fact that QOffscreenSurface is backed by a QWindow on some platforms,-
68 cross-platform applications must ensure that create() is only called on the main (GUI)-
69 thread. The QOffscreenSurface is then safe to be used with-
70 \l{QOpenGLContext::makeCurrent()}{makeCurrent()} on other threads, but the-
71 initialization and destruction must always happen on the main (GUI) thread.-
72-
73 \note In order to create an offscreen surface that is guaranteed to be compatible with-
74 a given context and window, make sure to set the format to the context's or the-
75 window's actual format, that is, the QSurfaceFormat returned from-
76 QOpenGLContext::format() or QWindow::format() \e{after the context or window has been-
77 created}. Passing the format returned from QWindow::requestedFormat() to setFormat()-
78 may result in an incompatible offscreen surface since the underlying windowing system-
79 interface may offer a different set of configurations for window and pbuffer surfaces.-
80-
81 \note Some platforms may utilize a surfaceless context extension (for example-
82 EGL_KHR_surfaceless_context) when available. In this case there will be no underlying-
83 native surface. For the use cases of QOffscreenSurface (rendering to FBOs, texture-
84 upload) this is not a problem.-
85*/-
86class Q_GUI_EXPORT QOffscreenSurfacePrivate : public QObjectPrivate-
87{-
88 Q_DECLARE_PUBLIC(QOffscreenSurface)-
89-
90public:-
91 QOffscreenSurfacePrivate()-
92 : QObjectPrivate()-
93 , surfaceType(QSurface::OpenGLSurface)-
94 , platformOffscreenSurface(0)-
95 , offscreenWindow(0)-
96 , requestedFormat(QSurfaceFormat::defaultFormat())-
97 , screen(0)-
98 , size(1, 1)-
99 {-
100 }
never executed: end of block
0
101-
102 ~QOffscreenSurfacePrivate()-
103 {-
104 }-
105-
106 QSurface::SurfaceType surfaceType;-
107 QPlatformOffscreenSurface *platformOffscreenSurface;-
108 QWindow *offscreenWindow;-
109 QSurfaceFormat requestedFormat;-
110 QScreen *screen;-
111 QSize size;-
112};-
113-
114-
115/*!-
116 Creates an offscreen surface for the \a targetScreen.-
117-
118 The underlying platform surface is not created until create() is called.-
119-
120 \sa setScreen(), create()-
121*/-
122QOffscreenSurface::QOffscreenSurface(QScreen *targetScreen)-
123 : QObject(*new QOffscreenSurfacePrivate(), 0)-
124 , QSurface(Offscreen)-
125{-
126 Q_D(QOffscreenSurface);-
127 d->screen = targetScreen;-
128 if (!d->screen)
!d->screenDescription
TRUEnever evaluated
FALSEnever evaluated
0
129 d->screen = QGuiApplication::primaryScreen();
never executed: d->screen = QGuiApplication::primaryScreen();
0
130-
131 //if your applications aborts here, then chances are your creating a QOffscreenSurface before-
132 //the screen list is populated.-
133 Q_ASSERT(d->screen);-
134-
135 connect(d->screen, SIGNAL(destroyed(QObject*)), this, SLOT(screenDestroyed(QObject*)));-
136}
never executed: end of block
0
137-
138-
139/*!-
140 Destroys the offscreen surface.-
141*/-
142QOffscreenSurface::~QOffscreenSurface()-
143{-
144 destroy();-
145}
never executed: end of block
0
146-
147/*!-
148 Returns the surface type of the offscreen surface.-
149-
150 The surface type of an offscreen surface is always QSurface::OpenGLSurface.-
151*/-
152QOffscreenSurface::SurfaceType QOffscreenSurface::surfaceType() const-
153{-
154 Q_D(const QOffscreenSurface);-
155 return d->surfaceType;
never executed: return d->surfaceType;
0
156}-
157-
158/*!-
159 Allocates the platform resources associated with the offscreen surface.-
160-
161 It is at this point that the surface format set using setFormat() gets resolved-
162 into an actual native surface.-
163-
164 Call destroy() to free the platform resources if necessary.-
165-
166 \note Some platforms require this function to be called on the main (GUI) thread.-
167-
168 \sa destroy()-
169*/-
170void QOffscreenSurface::create()-
171{-
172 Q_D(QOffscreenSurface);-
173 if (!d->platformOffscreenSurface && !d->offscreenWindow) {
!d->platformOffscreenSurfaceDescription
TRUEnever evaluated
FALSEnever evaluated
!d->offscreenWindowDescription
TRUEnever evaluated
FALSEnever evaluated
0
174 d->platformOffscreenSurface = QGuiApplicationPrivate::platformIntegration()->createPlatformOffscreenSurface(this);-
175 // No platform offscreen surface, fallback to an invisible window-
176 if (!d->platformOffscreenSurface) {
!d->platformOffscreenSurfaceDescription
TRUEnever evaluated
FALSEnever evaluated
0
177 if (QThread::currentThread() != qGuiApp->thread())
QThread::curre...()))->thread()Description
TRUEnever evaluated
FALSEnever evaluated
0
178 qWarning("Attempting to create QWindow-based QOffscreenSurface outside the gui thread. Expect failures.");
never executed: QMessageLogger(__FILE__, 178, __PRETTY_FUNCTION__).warning("Attempting to create QWindow-based QOffscreenSurface outside the gui thread. Expect failures.");
0
179 d->offscreenWindow = new QWindow(d->screen);-
180 d->offscreenWindow->setObjectName(QLatin1String("QOffscreenSurface"));-
181 // Remove this window from the global list since we do not want it to be destroyed when closing the app.-
182 // The QOffscreenSurface has to be usable even after exiting the event loop.-
183 QGuiApplicationPrivate::window_list.removeOne(d->offscreenWindow);-
184 d->offscreenWindow->setSurfaceType(QWindow::OpenGLSurface);-
185 d->offscreenWindow->setFormat(d->requestedFormat);-
186 d->offscreenWindow->setGeometry(0, 0, d->size.width(), d->size.height());-
187 d->offscreenWindow->create();-
188 }
never executed: end of block
0
189-
190 QPlatformSurfaceEvent e(QPlatformSurfaceEvent::SurfaceCreated);-
191 QGuiApplication::sendEvent(this, &e);-
192 }
never executed: end of block
0
193}
never executed: end of block
0
194-
195/*!-
196 Releases the native platform resources associated with this offscreen surface.-
197-
198 \sa create()-
199*/-
200void QOffscreenSurface::destroy()-
201{-
202 Q_D(QOffscreenSurface);-
203-
204 QPlatformSurfaceEvent e(QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed);-
205 QGuiApplication::sendEvent(this, &e);-
206-
207 delete d->platformOffscreenSurface;-
208 d->platformOffscreenSurface = 0;-
209 if (d->offscreenWindow) {
d->offscreenWindowDescription
TRUEnever evaluated
FALSEnever evaluated
0
210 d->offscreenWindow->destroy();-
211 delete d->offscreenWindow;-
212 d->offscreenWindow = 0;-
213 }
never executed: end of block
0
214}
never executed: end of block
0
215-
216/*!-
217 Returns \c true if this offscreen surface is valid; otherwise returns \c false.-
218-
219 The offscreen surface is valid if the platform resources have been successfuly allocated.-
220-
221 \sa create()-
222*/-
223bool QOffscreenSurface::isValid() const-
224{-
225 Q_D(const QOffscreenSurface);-
226 return (d->platformOffscreenSurface && d->platformOffscreenSurface->isValid())
never executed: return (d->platformOffscreenSurface && d->platformOffscreenSurface->isValid()) || (d->offscreenWindow && d->offscreenWindow->handle());
d->platformOffscreenSurfaceDescription
TRUEnever evaluated
FALSEnever evaluated
d->platformOff...ace->isValid()Description
TRUEnever evaluated
FALSEnever evaluated
0
227 || (d->offscreenWindow && d->offscreenWindow->handle());
never executed: return (d->platformOffscreenSurface && d->platformOffscreenSurface->isValid()) || (d->offscreenWindow && d->offscreenWindow->handle());
d->offscreenWindowDescription
TRUEnever evaluated
FALSEnever evaluated
d->offscreenWindow->handle()Description
TRUEnever evaluated
FALSEnever evaluated
0
228}-
229-
230/*!-
231 Sets the offscreen surface \a format.-
232-
233 The surface format will be resolved in the create() function. Calling-
234 this function after create() will not re-resolve the surface format of the native surface.-
235-
236 \sa create(), destroy()-
237*/-
238void QOffscreenSurface::setFormat(const QSurfaceFormat &format)-
239{-
240 Q_D(QOffscreenSurface);-
241 d->requestedFormat = format;-
242}
never executed: end of block
0
243-
244/*!-
245 Returns the requested surfaceformat of this offscreen surface.-
246-
247 If the requested format was not supported by the platform implementation,-
248 the requestedFormat will differ from the actual offscreen surface format.-
249-
250 This is the value set with setFormat().-
251-
252 \sa setFormat(), format()-
253 */-
254QSurfaceFormat QOffscreenSurface::requestedFormat() const-
255{-
256 Q_D(const QOffscreenSurface);-
257 return d->requestedFormat;
never executed: return d->requestedFormat;
0
258}-
259-
260/*!-
261 Returns the actual format of this offscreen surface.-
262-
263 After the offscreen surface has been created, this function will return the actual-
264 surface format of the surface. It might differ from the requested format if the requested-
265 format could not be fulfilled by the platform.-
266-
267 \sa create(), requestedFormat()-
268*/-
269QSurfaceFormat QOffscreenSurface::format() const-
270{-
271 Q_D(const QOffscreenSurface);-
272 if (d->platformOffscreenSurface)
d->platformOffscreenSurfaceDescription
TRUEnever evaluated
FALSEnever evaluated
0
273 return d->platformOffscreenSurface->format();
never executed: return d->platformOffscreenSurface->format();
0
274 if (d->offscreenWindow)
d->offscreenWindowDescription
TRUEnever evaluated
FALSEnever evaluated
0
275 return d->offscreenWindow->format();
never executed: return d->offscreenWindow->format();
0
276 return d->requestedFormat;
never executed: return d->requestedFormat;
0
277}-
278-
279/*!-
280 Returns the size of the offscreen surface.-
281*/-
282QSize QOffscreenSurface::size() const-
283{-
284 Q_D(const QOffscreenSurface);-
285 return d->size;
never executed: return d->size;
0
286}-
287-
288/*!-
289 Returns the screen to which the offscreen surface is connected.-
290-
291 \sa setScreen()-
292*/-
293QScreen *QOffscreenSurface::screen() const-
294{-
295 Q_D(const QOffscreenSurface);-
296 return d->screen;
never executed: return d->screen;
0
297}-
298-
299/*!-
300 Sets the screen to which the offscreen surface is connected.-
301-
302 If the offscreen surface has been created, it will be recreated on the \a newScreen.-
303-
304 \sa screen()-
305*/-
306void QOffscreenSurface::setScreen(QScreen *newScreen)-
307{-
308 Q_D(QOffscreenSurface);-
309 if (!newScreen)
!newScreenDescription
TRUEnever evaluated
FALSEnever evaluated
0
310 newScreen = QGuiApplication::primaryScreen();
never executed: newScreen = QGuiApplication::primaryScreen();
0
311 if (newScreen != d->screen) {
newScreen != d->screenDescription
TRUEnever evaluated
FALSEnever evaluated
0
312 const bool wasCreated = d->platformOffscreenSurface != 0 || d->offscreenWindow != 0;
d->platformOff...enSurface != 0Description
TRUEnever evaluated
FALSEnever evaluated
d->offscreenWindow != 0Description
TRUEnever evaluated
FALSEnever evaluated
0
313 if (wasCreated)
wasCreatedDescription
TRUEnever evaluated
FALSEnever evaluated
0
314 destroy();
never executed: destroy();
0
315 if (d->screen)
d->screenDescription
TRUEnever evaluated
FALSEnever evaluated
0
316 disconnect(d->screen, SIGNAL(destroyed(QObject*)), this, SLOT(screenDestroyed(QObject*)));
never executed: disconnect(d->screen, qFlagLocation("2""destroyed(QObject*)" "\0" __FILE__ ":" "316"), this, qFlagLocation("1""screenDestroyed(QObject*)" "\0" __FILE__ ":" "316"));
0
317 d->screen = newScreen;-
318 if (newScreen) {
newScreenDescription
TRUEnever evaluated
FALSEnever evaluated
0
319 connect(d->screen, SIGNAL(destroyed(QObject*)), this, SLOT(screenDestroyed(QObject*)));-
320 if (wasCreated)
wasCreatedDescription
TRUEnever evaluated
FALSEnever evaluated
0
321 create();
never executed: create();
0
322 }
never executed: end of block
0
323 emit screenChanged(newScreen);-
324 }
never executed: end of block
0
325}
never executed: end of block
0
326-
327/*!-
328 Called when the offscreen surface's screen is destroyed.-
329-
330 \internal-
331*/-
332void QOffscreenSurface::screenDestroyed(QObject *object)-
333{-
334 Q_D(QOffscreenSurface);-
335 if (object == static_cast<QObject *>(d->screen))
object == stat... *>(d->screen)Description
TRUEnever evaluated
FALSEnever evaluated
0
336 setScreen(0);
never executed: setScreen(0);
0
337}
never executed: end of block
0
338-
339/*!-
340 \fn QOffscreenSurface::screenChanged(QScreen *screen)-
341-
342 This signal is emitted when an offscreen surface's \a screen changes, either-
343 by being set explicitly with setScreen(), or automatically when-
344 the window's screen is removed.-
345*/-
346-
347/*!-
348 Returns the platform offscreen surface corresponding to the offscreen surface.-
349-
350 \internal-
351*/-
352QPlatformOffscreenSurface *QOffscreenSurface::handle() const-
353{-
354 Q_D(const QOffscreenSurface);-
355 return d->platformOffscreenSurface;
never executed: return d->platformOffscreenSurface;
0
356}-
357-
358/*!-
359 Returns the platform surface corresponding to the offscreen surface.-
360-
361 \internal-
362*/-
363QPlatformSurface *QOffscreenSurface::surfaceHandle() const-
364{-
365 Q_D(const QOffscreenSurface);-
366 if (d->offscreenWindow)
d->offscreenWindowDescription
TRUEnever evaluated
FALSEnever evaluated
0
367 return d->offscreenWindow->handle();
never executed: return d->offscreenWindow->handle();
0
368-
369 return d->platformOffscreenSurface;
never executed: return d->platformOffscreenSurface;
0
370}-
371-
372QT_END_NAMESPACE-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9