Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qoffscreensurface.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qoffscreensurface.h"
5
6#include "qguiapplication_p.h"
7#include "qscreen.h"
10#include "qwindow.h"
11#include "qplatformwindow.h"
12
13#include <private/qwindow_p.h>
14
16
17using namespace Qt::StringLiterals;
18
71 : QObject(*new QOffscreenSurfacePrivate(), parent)
72 , QSurface(Offscreen)
73{
75 d->screen = targetScreen;
76 if (!d->screen)
78
79 //if your applications aborts here, then chances are your creating a QOffscreenSurface before
80 //the screen list is populated.
81 Q_ASSERT(d->screen);
82
83 connect(d->screen, SIGNAL(destroyed(QObject*)), this, SLOT(screenDestroyed(QObject*)));
84}
85
93
100{
101 Q_D(const QOffscreenSurface);
102 return d->surfaceType;
103}
104
118{
120 if (!d->platformOffscreenSurface && !d->offscreenWindow) {
121 d->platformOffscreenSurface = QGuiApplicationPrivate::platformIntegration()->createPlatformOffscreenSurface(this);
122 // No platform offscreen surface, fallback to an invisible window
123 if (!d->platformOffscreenSurface) {
124 if (QThread::currentThread() != qGuiApp->thread())
125 qWarning("Attempting to create QWindow-based QOffscreenSurface outside the gui thread. Expect failures.");
126 d->offscreenWindow = new QWindow(d->screen);
127 // Make the window frameless to prevent Windows from enlarging it, should it
128 // violate the minimum title bar width on the platform.
129 d->offscreenWindow->setFlags(d->offscreenWindow->flags()
131 d->offscreenWindow->setObjectName("QOffscreenSurface"_L1);
132 // Remove this window from the global list since we do not want it to be destroyed when closing the app.
133 // The QOffscreenSurface has to be usable even after exiting the event loop.
134 QGuiApplicationPrivate::window_list.removeOne(d->offscreenWindow);
135 d->offscreenWindow->setSurfaceType(QWindow::OpenGLSurface);
136 d->offscreenWindow->setFormat(d->requestedFormat);
137 // Prevent QPlatformWindow::initialGeometry() and platforms from setting a default geometry.
138 qt_window_private(d->offscreenWindow)->setAutomaticPositionAndResizeEnabled(false);
139 d->offscreenWindow->setGeometry(0, 0, d->size.width(), d->size.height());
140 d->offscreenWindow->create();
141 }
142
145 }
146}
147
154{
156
159
160 delete d->platformOffscreenSurface;
161 d->platformOffscreenSurface = nullptr;
162 if (d->offscreenWindow) {
163 d->offscreenWindow->destroy();
164 delete d->offscreenWindow;
165 d->offscreenWindow = nullptr;
166 }
167}
168
177{
178 Q_D(const QOffscreenSurface);
179 return (d->platformOffscreenSurface && d->platformOffscreenSurface->isValid())
180 || (d->offscreenWindow && d->offscreenWindow->handle());
181}
182
192{
194 d->requestedFormat = format;
195}
196
208{
209 Q_D(const QOffscreenSurface);
210 return d->requestedFormat;
211}
212
223{
224 Q_D(const QOffscreenSurface);
225 if (d->platformOffscreenSurface)
226 return d->platformOffscreenSurface->format();
227 if (d->offscreenWindow)
228 return d->offscreenWindow->format();
229 return d->requestedFormat;
230}
231
236{
237 Q_D(const QOffscreenSurface);
238 return d->size;
239}
240
247{
248 Q_D(const QOffscreenSurface);
249 return d->screen;
250}
251
260{
262 if (!newScreen)
264 if (newScreen != d->screen) {
265 const bool wasCreated = d->platformOffscreenSurface != nullptr || d->offscreenWindow != nullptr;
266 if (wasCreated)
267 destroy();
268 if (d->screen)
269 disconnect(d->screen, SIGNAL(destroyed(QObject*)), this, SLOT(screenDestroyed(QObject*)));
270 d->screen = newScreen;
271 if (newScreen) {
272 connect(d->screen, SIGNAL(destroyed(QObject*)), this, SLOT(screenDestroyed(QObject*)));
273 if (wasCreated)
274 create();
275 }
276 emit screenChanged(newScreen);
277 }
278}
279
285void QOffscreenSurface::screenDestroyed(QObject *object)
286{
288 if (object == static_cast<QObject *>(d->screen))
289 setScreen(nullptr);
290}
291
306{
307 Q_D(const QOffscreenSurface);
308 return d->platformOffscreenSurface;
309}
310
330{
331 Q_D(const QOffscreenSurface);
332 if (d->offscreenWindow)
333 return d->offscreenWindow->handle();
334
335 return d->platformOffscreenSurface;
336}
337
338using namespace QNativeInterface;
339
340void *QOffscreenSurface::resolveInterface(const char *name, int revision) const
341{
342 Q_UNUSED(name); Q_UNUSED(revision);
343
344 Q_D(const QOffscreenSurface);
345 Q_UNUSED(d);
346
347#if defined(Q_OS_ANDROID)
348 QT_NATIVE_INTERFACE_RETURN_IF(QAndroidOffscreenSurface, d->platformOffscreenSurface);
349#endif
350
351 return nullptr;
352}
353
355
356#include "moc_qoffscreensurface.cpp"
static bool sendEvent(QObject *receiver, QEvent *event)
Sends event event directly to receiver receiver, using the notify() function.
static QCoreApplication * instance() noexcept
Returns a pointer to the application's QCoreApplication (or QGuiApplication/QApplication) instance.
static QPlatformIntegration * platformIntegration()
static QWindowList window_list
QScreen * primaryScreen
the primary (or default) screen of the application.
Native interface to a offscreen surface on Android.
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
void destroyed(QObject *=nullptr)
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointe...
\inmodule QtGui
QPlatformOffscreenSurface * handle() const
Returns the platform offscreen surface corresponding to the offscreen surface.
SurfaceType surfaceType() const override
Returns the surface type of the offscreen surface.
QPlatformSurface * surfaceHandle() const override
Returns the platform surface corresponding to the offscreen surface.
~QOffscreenSurface()
Destroys the offscreen surface.
QOffscreenSurface(QScreen *screen=nullptr, QObject *parent=nullptr)
QSurfaceFormat format() const override
Returns the actual format of this offscreen surface.
void setScreen(QScreen *screen)
Sets the screen to which the offscreen surface is connected.
QScreen * screen() const
Returns the screen to which the offscreen surface is connected.
QSurfaceFormat requestedFormat() const
Returns the requested surfaceformat of this offscreen surface.
void create()
Allocates the platform resources associated with the offscreen surface.
bool isValid() const
Returns true if this offscreen surface is valid; otherwise returns false.
void destroy()
Releases the native platform resources associated with this offscreen surface.
QSize size() const override
Returns the size of the offscreen surface.
void setFormat(const QSurfaceFormat &format)
Sets the offscreen surface format.
void screenChanged(QScreen *screen)
This signal is emitted when an offscreen surface's screen changes, either by being set explicitly wit...
The QPlatformSurfaceEvent class is used to notify about native platform surface events....
Definition qevent.h:531
The QPlatformSurface class provides an abstraction for a surface.
The QScreen class is used to query screen properties. \inmodule QtGui.
Definition qscreen.h:32
\inmodule QtCore
Definition qsize.h:25
The QSurfaceFormat class represents the format of a QSurface. \inmodule QtGui.
\inmodule QtGui
Definition qsurface.h:21
SurfaceType
The SurfaceType enum describes what type of surface this is.
Definition qsurface.h:30
@ OpenGLSurface
Definition qsurface.h:32
static QThread * currentThread()
Definition qthread.cpp:1039
\inmodule QtGui
Definition qwindow.h:63
Combined button and popup list for selecting options.
@ CustomizeWindowHint
Definition qnamespace.h:239
@ FramelessWindowHint
Definition qnamespace.h:225
#define qGuiApp
#define qWarning
Definition qlogging.h:166
#define QT_NATIVE_INTERFACE_RETURN_IF(NativeInterface, baseType)
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLuint name
GLint GLsizei GLsizei GLenum format
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define emit
#define Q_UNUSED(x)
Q_GUI_EXPORT QWindowPrivate * qt_window_private(QWindow *window)
Definition qwindow.cpp:2950
myObject disconnect()
[26]