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#ifndef QQUICKWINDOW_P_H
5#define QQUICKWINDOW_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtQuick/private/qquickdeliveryagent_p_p.h>
19#include <QtQuick/private/qquickevents_p_p.h>
20#include <QtQuick/private/qsgcontext_p.h>
21#include <QtQuick/private/qquickpaletteproviderprivatebase_p.h>
22#include <QtQuick/private/qquickrendertarget_p.h>
23#include <QtQuick/private/qquickgraphicsdevice_p.h>
24#include <QtQuick/private/qquickgraphicsconfiguration_p.h>
25#include <QtQuick/qquickitem.h>
26#include <QtQuick/qquickwindow.h>
27
28#include <QtCore/qthread.h>
29#include <QtCore/qmutex.h>
30#include <QtCore/qwaitcondition.h>
31#include <QtCore/qrunnable.h>
32#include <QtCore/qstack.h>
33
34#include <QtGui/private/qevent_p.h>
35#include <QtGui/private/qpointingdevice_p.h>
36#include <QtGui/private/qwindow_p.h>
37#include <QtGui/qevent.h>
38#include <QtGui/qstylehints.h>
39#include <QtGui/qguiapplication.h>
40
41QT_BEGIN_NAMESPACE
42
43class QOpenGLContext;
44class QQuickAnimatorController;
45class QQuickDragGrabber;
46class QQuickItemPrivate;
47class QPointingDevice;
48class QQuickRenderControl;
49class QQuickWindowIncubationController;
50class QQuickWindowPrivate;
51class QSGRenderLoop;
52class QTouchEvent;
53class QRhi;
54class QRhiSwapChain;
55class QRhiRenderBuffer;
56class QRhiRenderPassDescriptor;
57class QRhiTexture;
58
59Q_DECLARE_LOGGING_CATEGORY(lcQuickWindow)
60
61//Make it easy to identify and customize the root item if needed
62class Q_QUICK_EXPORT QQuickRootItem : public QQuickItem
63{
64 Q_OBJECT
65 QML_ANONYMOUS
66 QML_ADDED_IN_VERSION(2, 0)
67public:
68 QQuickRootItem();
69
70public Q_SLOTS:
71 void setWidth(int w) {QQuickItem::setWidth(qreal(w));}
72 void setHeight(int h) {QQuickItem::setHeight(qreal(h));}
73};
74
75struct QQuickWindowRenderTarget
76{
77 enum class ResetFlag {
78 KeepImplicitBuffers = 0x01
79 };
80 Q_DECLARE_FLAGS(ResetFlags, ResetFlag)
81 void reset(QRhi *rhi, ResetFlags flags = {});
82
83 struct {
84 QRhiRenderTarget *renderTarget = nullptr;
85 bool owns = false;
86 int multiViewCount = 1;
87 } rt;
88 struct {
89 QRhiTexture *texture = nullptr;
90 QRhiRenderBuffer *renderBuffer = nullptr;
91 QRhiRenderPassDescriptor *rpDesc = nullptr;
92 } res;
93 struct ImplicitBuffers {
94 QRhiRenderBuffer *depthStencil = nullptr;
95 QRhiTexture *depthStencilTexture = nullptr;
96 QRhiTexture *multisampleTexture = nullptr;
97 void reset(QRhi *rhi);
98 } implicitBuffers;
99 struct {
100 QPaintDevice *paintDevice = nullptr;
101 bool owns = false;
102 } sw;
103};
104
105Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickWindowRenderTarget::ResetFlags)
106
107class Q_QUICK_EXPORT QQuickWindowPrivate
108 : public QWindowPrivate
109 , public QQuickPaletteProviderPrivateBase<QQuickWindow, QQuickWindowPrivate>
110{
111public:
112 Q_DECLARE_PUBLIC(QQuickWindow)
113
114 enum CustomEvents {
115 FullUpdateRequest = QEvent::User + 1,
116 TriggerContextCreationFailure = QEvent::User + 2
117 };
118
119 static inline QQuickWindowPrivate *get(QQuickWindow *c) { return c->d_func(); }
120 static inline const QQuickWindowPrivate *get(const QQuickWindow *c) { return c->d_func(); }
121
122 QQuickWindowPrivate();
123 ~QQuickWindowPrivate() override;
124
125 void setPalette(QQuickPalette *p) override;
126 void updateWindowPalette();
127 void updateChildrenPalettes(const QPalette &parentPalette) override;
128
129 void init(QQuickWindow *, QQuickRenderControl *control = nullptr);
130
131 QQuickRootItem *contentItem;
132 QSet<QQuickItem *> parentlessItems;
133 QQmlListProperty<QObject> data();
134
135 // primary delivery agent for the whole scene, used by default for events that arrive in this window;
136 // but any subscene root can have a QQuickItemPrivate::ExtraData::subsceneDeliveryAgent
137 QQuickDeliveryAgent *deliveryAgent = nullptr;
138 QQuickDeliveryAgentPrivate *deliveryAgentPrivate() const
139 { return deliveryAgent ? static_cast<QQuickDeliveryAgentPrivate *>(QQuickDeliveryAgentPrivate::get(o: deliveryAgent)) : nullptr; }
140
141#if QT_CONFIG(cursor)
142 QQuickItem *cursorItem = nullptr;
143 QQuickPointerHandler *cursorHandler = nullptr;
144 void updateCursor(const QPointF &scenePos, QQuickItem *rootItem = nullptr);
145 std::pair<QQuickItem*, QQuickPointerHandler*> findCursorItemAndHandler(QQuickItem *item, const QPointF &scenePos) const;
146#endif
147
148 void clearFocusObject() override;
149 void setFocusToTarget(FocusTarget, Qt::FocusReason) override;
150
151 void maybeSynthesizeContextMenuEvent(QMouseEvent *event) override;
152
153 void dirtyItem(QQuickItem *);
154 void cleanup(QSGNode *);
155
156 void ensureCustomRenderTarget();
157 void setCustomCommandBuffer(QRhiCommandBuffer *cb);
158
159 void polishItems();
160 void forcePolish();
161 void invalidateFontData(QQuickItem *item);
162 void syncSceneGraph();
163 void renderSceneGraph();
164
165 bool isRenderable() const;
166
167 bool emitError(QQuickWindow::SceneGraphError error, const QString &msg);
168
169 enum TextureFromNativeTextureFlag {
170 NativeTextureIsExternalOES = 0x01
171 };
172 Q_DECLARE_FLAGS(TextureFromNativeTextureFlags, TextureFromNativeTextureFlag)
173
174 QSGTexture *createTextureFromNativeTexture(quint64 nativeObjectHandle,
175 int nativeLayoutOrState,
176 uint nativeFormat,
177 const QSize &size,
178 QQuickWindow::CreateTextureOptions options,
179 TextureFromNativeTextureFlags flags = {}) const;
180 QSGTexture *createTextureFromNativeTexture(quint64 nativeObjectHandle,
181 int nativeLayoutOrState,
182 const QSize &size,
183 QQuickWindow::CreateTextureOptions options,
184 TextureFromNativeTextureFlags flags = {}) const {
185 return createTextureFromNativeTexture(nativeObjectHandle, nativeLayoutOrState, nativeFormat: 0, size, options, flags);
186 }
187
188 QQuickItem::UpdatePaintNodeData updatePaintNodeData;
189
190 QQuickItem *dirtyItemList;
191 QList<QSGNode *> cleanupNodeList;
192
193 QVector<QQuickItem *> itemsToPolish;
194
195 qreal lastReportedItemDevicePixelRatio;
196 QMetaObject::Connection physicalDpiChangedConnection;
197 std::array<QMetaObject::Connection, 7> connections;
198
199 void updateDirtyNodes();
200 void cleanupNodes();
201 void cleanupNodesOnShutdown();
202 bool updateEffectiveOpacity(QQuickItem *);
203 void updateEffectiveOpacityRoot(QQuickItem *, qreal);
204 void updateDirtyNode(QQuickItem *);
205
206 void fireFrameSwapped() { Q_EMIT q_func()->frameSwapped(); }
207 void fireAboutToStop() { Q_EMIT q_func()->sceneGraphAboutToStop(); }
208
209 bool needsChildWindowStackingOrderUpdate = false;
210 void updateChildWindowStackingOrder(QQuickItem *item = nullptr);
211
212 int multiViewCount();
213 QRhiRenderTarget *activeCustomRhiRenderTarget();
214
215 QSGRenderContext *context;
216 QSGRenderer *renderer;
217 QByteArray visualizationMode; // Default renderer supports "clip", "overdraw", "changes", "batches" and blank.
218
219 QSGRenderLoop *windowManager;
220 QQuickRenderControl *renderControl;
221 QScopedPointer<QQuickAnimatorController> animationController;
222
223 QColor clearColor;
224
225 uint persistentGraphics : 1;
226 uint persistentSceneGraph : 1;
227 uint inDestructor : 1;
228
229 // Storage for setRenderTarget(QQuickRenderTarget).
230 // Gets baked into redirect.renderTarget by ensureCustomRenderTarget() when rendering the next frame.
231 QQuickRenderTarget customRenderTarget;
232
233 struct Redirect {
234 QRhiCommandBuffer *commandBuffer = nullptr;
235 QQuickWindowRenderTarget rt;
236 bool renderTargetDirty = false;
237 } redirect;
238
239 QQuickGraphicsDevice customDeviceObjects;
240
241 QQuickGraphicsConfiguration graphicsConfig;
242
243 mutable QQuickWindowIncubationController *incubationController;
244
245 static bool defaultAlphaBuffer;
246 static QQuickWindow::TextRenderType textRenderType;
247
248 // data property
249 static void data_append(QQmlListProperty<QObject> *, QObject *);
250 static qsizetype data_count(QQmlListProperty<QObject> *);
251 static QObject *data_at(QQmlListProperty<QObject> *, qsizetype);
252 static void data_clear(QQmlListProperty<QObject> *);
253 static void data_removeLast(QQmlListProperty<QObject> *);
254
255 static void rhiCreationFailureMessage(const QString &backendName,
256 QString *translatedMessage,
257 QString *untranslatedMessage);
258
259 static void emitBeforeRenderPassRecording(void *ud);
260 static void emitAfterRenderPassRecording(void *ud);
261
262 QMutex renderJobMutex;
263 QList<QRunnable *> beforeSynchronizingJobs;
264 QList<QRunnable *> afterSynchronizingJobs;
265 QList<QRunnable *> beforeRenderingJobs;
266 QList<QRunnable *> afterRenderingJobs;
267 QList<QRunnable *> afterSwapJobs;
268
269 void runAndClearJobs(QList<QRunnable *> *jobs);
270 QOpenGLContext *openglContext();
271
272 QQuickWindow::GraphicsStateInfo rhiStateInfo;
273 QRhi *rhi = nullptr;
274 QRhiSwapChain *swapchain = nullptr;
275 QRhiRenderBuffer *depthStencilForSwapchain = nullptr;
276 QRhiRenderPassDescriptor *rpDescForSwapchain = nullptr;
277 uint hasActiveSwapchain : 1;
278 uint hasRenderableSwapchain : 1;
279 uint swapchainJustBecameRenderable : 1;
280 uint updatesEnabled : 1;
281 bool pendingFontUpdate = false;
282 bool windowEventDispatch = false;
283 bool rmbContextMenuEventEnabled = false; // true after right-mouse press, false when menu opened
284 QPointer<QQuickPalette> windowPaletteRef;
285
286private:
287 static void cleanupNodesOnShutdown(QQuickItem *);
288};
289
290class QQuickWindowQObjectCleanupJob : public QRunnable
291{
292public:
293 QQuickWindowQObjectCleanupJob(QObject *o) : object(o) { }
294 void run() override { delete object; }
295 QObject *object;
296 static void schedule(QQuickWindow *window, QObject *object) {
297 Q_ASSERT(window);
298 Q_ASSERT(object);
299 window->scheduleRenderJob(job: new QQuickWindowQObjectCleanupJob(object), schedule: QQuickWindow::AfterSynchronizingStage);
300 }
301};
302
303Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickWindowPrivate::TextureFromNativeTextureFlags)
304
305QT_END_NAMESPACE
306
307#endif // QQUICKWINDOW_P_H
308

source code of qtdeclarative/src/quick/items/qquickwindow_p.h