| 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 | |
| 41 | QT_BEGIN_NAMESPACE |
| 42 | |
| 43 | class QOpenGLContext; |
| 44 | class QQuickAnimatorController; |
| 45 | class QQuickDragGrabber; |
| 46 | class QQuickItemPrivate; |
| 47 | class QPointingDevice; |
| 48 | class QQuickRenderControl; |
| 49 | class QQuickWindowIncubationController; |
| 50 | class QQuickWindowPrivate; |
| 51 | class QSGRenderLoop; |
| 52 | class QTouchEvent; |
| 53 | class QRhi; |
| 54 | class QRhiSwapChain; |
| 55 | class QRhiRenderBuffer; |
| 56 | class QRhiRenderPassDescriptor; |
| 57 | class QRhiTexture; |
| 58 | |
| 59 | Q_DECLARE_LOGGING_CATEGORY(lcQuickWindow) |
| 60 | |
| 61 | //Make it easy to identify and customize the root item if needed |
| 62 | class Q_QUICK_EXPORT QQuickRootItem : public QQuickItem |
| 63 | { |
| 64 | Q_OBJECT |
| 65 | QML_ANONYMOUS |
| 66 | QML_ADDED_IN_VERSION(2, 0) |
| 67 | public: |
| 68 | QQuickRootItem(); |
| 69 | |
| 70 | public Q_SLOTS: |
| 71 | void setWidth(int w) {QQuickItem::setWidth(qreal(w));} |
| 72 | void setHeight(int h) {QQuickItem::setHeight(qreal(h));} |
| 73 | }; |
| 74 | |
| 75 | struct 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 | |
| 105 | Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickWindowRenderTarget::ResetFlags) |
| 106 | |
| 107 | class Q_QUICK_EXPORT QQuickWindowPrivate |
| 108 | : public QWindowPrivate |
| 109 | , public QQuickPaletteProviderPrivateBase<QQuickWindow, QQuickWindowPrivate> |
| 110 | { |
| 111 | public: |
| 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 | QPair<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 dirtyItem(QQuickItem *); |
| 152 | void cleanup(QSGNode *); |
| 153 | |
| 154 | void ensureCustomRenderTarget(); |
| 155 | void setCustomCommandBuffer(QRhiCommandBuffer *cb); |
| 156 | |
| 157 | void polishItems(); |
| 158 | void forcePolish(); |
| 159 | void invalidateFontData(QQuickItem *item); |
| 160 | void syncSceneGraph(); |
| 161 | void renderSceneGraph(); |
| 162 | |
| 163 | bool isRenderable() const; |
| 164 | |
| 165 | bool emitError(QQuickWindow::SceneGraphError error, const QString &msg); |
| 166 | |
| 167 | enum TextureFromNativeTextureFlag { |
| 168 | NativeTextureIsExternalOES = 0x01 |
| 169 | }; |
| 170 | Q_DECLARE_FLAGS(TextureFromNativeTextureFlags, TextureFromNativeTextureFlag) |
| 171 | |
| 172 | QSGTexture *createTextureFromNativeTexture(quint64 nativeObjectHandle, |
| 173 | int nativeLayoutOrState, |
| 174 | uint nativeFormat, |
| 175 | const QSize &size, |
| 176 | QQuickWindow::CreateTextureOptions options, |
| 177 | TextureFromNativeTextureFlags flags = {}) const; |
| 178 | QSGTexture *createTextureFromNativeTexture(quint64 nativeObjectHandle, |
| 179 | int nativeLayoutOrState, |
| 180 | const QSize &size, |
| 181 | QQuickWindow::CreateTextureOptions options, |
| 182 | TextureFromNativeTextureFlags flags = {}) const { |
| 183 | return createTextureFromNativeTexture(nativeObjectHandle, nativeLayoutOrState, nativeFormat: 0, size, options, flags); |
| 184 | } |
| 185 | |
| 186 | QQuickItem::UpdatePaintNodeData updatePaintNodeData; |
| 187 | |
| 188 | QQuickItem *dirtyItemList; |
| 189 | QList<QSGNode *> cleanupNodeList; |
| 190 | |
| 191 | QVector<QQuickItem *> itemsToPolish; |
| 192 | |
| 193 | qreal lastReportedItemDevicePixelRatio; |
| 194 | QMetaObject::Connection physicalDpiChangedConnection; |
| 195 | |
| 196 | void updateDirtyNodes(); |
| 197 | void cleanupNodes(); |
| 198 | void cleanupNodesOnShutdown(); |
| 199 | bool updateEffectiveOpacity(QQuickItem *); |
| 200 | void updateEffectiveOpacityRoot(QQuickItem *, qreal); |
| 201 | void updateDirtyNode(QQuickItem *); |
| 202 | |
| 203 | void fireFrameSwapped() { Q_EMIT q_func()->frameSwapped(); } |
| 204 | void fireAboutToStop() { Q_EMIT q_func()->sceneGraphAboutToStop(); } |
| 205 | |
| 206 | bool needsChildWindowStackingOrderUpdate = false; |
| 207 | void updateChildWindowStackingOrder(QQuickItem *item = nullptr); |
| 208 | |
| 209 | int multiViewCount(); |
| 210 | QRhiRenderTarget *activeCustomRhiRenderTarget(); |
| 211 | |
| 212 | QSGRenderContext *context; |
| 213 | QSGRenderer *renderer; |
| 214 | QByteArray visualizationMode; // Default renderer supports "clip", "overdraw", "changes", "batches" and blank. |
| 215 | |
| 216 | QSGRenderLoop *windowManager; |
| 217 | QQuickRenderControl *renderControl; |
| 218 | QScopedPointer<QQuickAnimatorController> animationController; |
| 219 | |
| 220 | QColor clearColor; |
| 221 | |
| 222 | uint persistentGraphics : 1; |
| 223 | uint persistentSceneGraph : 1; |
| 224 | uint inDestructor : 1; |
| 225 | |
| 226 | // Storage for setRenderTarget(QQuickRenderTarget). |
| 227 | // Gets baked into redirect.renderTarget by ensureCustomRenderTarget() when rendering the next frame. |
| 228 | QQuickRenderTarget customRenderTarget; |
| 229 | |
| 230 | struct Redirect { |
| 231 | QRhiCommandBuffer *commandBuffer = nullptr; |
| 232 | QQuickWindowRenderTarget rt; |
| 233 | bool renderTargetDirty = false; |
| 234 | } redirect; |
| 235 | |
| 236 | QQuickGraphicsDevice customDeviceObjects; |
| 237 | |
| 238 | QQuickGraphicsConfiguration graphicsConfig; |
| 239 | |
| 240 | mutable QQuickWindowIncubationController *incubationController; |
| 241 | |
| 242 | static bool defaultAlphaBuffer; |
| 243 | static QQuickWindow::TextRenderType textRenderType; |
| 244 | |
| 245 | // vvv currently in use in Controls 2; TODO remove |
| 246 | static bool dragOverThreshold(qreal d, Qt::Axis axis, const QEventPoint *tp, int startDragThreshold = -1) |
| 247 | { return QQuickDeliveryAgentPrivate::dragOverThreshold(d, axis, tp: *tp, startDragThreshold); } |
| 248 | static bool dragOverThreshold(qreal d, Qt::Axis axis, QMouseEvent *event, int startDragThreshold = -1) |
| 249 | { return QQuickDeliveryAgentPrivate::dragOverThreshold(d, axis, event, startDragThreshold); } |
| 250 | void clearFocusInScope(QQuickItem *scope, QQuickItem *item, Qt::FocusReason reason) |
| 251 | { deliveryAgentPrivate()->clearFocusInScope(scope, item, reason); } |
| 252 | // ^^^ currently in use in Controls 2; TODO remove |
| 253 | |
| 254 | // data property |
| 255 | static void data_append(QQmlListProperty<QObject> *, QObject *); |
| 256 | static qsizetype data_count(QQmlListProperty<QObject> *); |
| 257 | static QObject *data_at(QQmlListProperty<QObject> *, qsizetype); |
| 258 | static void data_clear(QQmlListProperty<QObject> *); |
| 259 | static void data_removeLast(QQmlListProperty<QObject> *); |
| 260 | |
| 261 | static void rhiCreationFailureMessage(const QString &backendName, |
| 262 | QString *translatedMessage, |
| 263 | QString *untranslatedMessage); |
| 264 | |
| 265 | static void emitBeforeRenderPassRecording(void *ud); |
| 266 | static void emitAfterRenderPassRecording(void *ud); |
| 267 | |
| 268 | QMutex renderJobMutex; |
| 269 | QList<QRunnable *> beforeSynchronizingJobs; |
| 270 | QList<QRunnable *> afterSynchronizingJobs; |
| 271 | QList<QRunnable *> beforeRenderingJobs; |
| 272 | QList<QRunnable *> afterRenderingJobs; |
| 273 | QList<QRunnable *> afterSwapJobs; |
| 274 | |
| 275 | void runAndClearJobs(QList<QRunnable *> *jobs); |
| 276 | QOpenGLContext *openglContext(); |
| 277 | |
| 278 | QQuickWindow::GraphicsStateInfo rhiStateInfo; |
| 279 | QRhi *rhi = nullptr; |
| 280 | QRhiSwapChain *swapchain = nullptr; |
| 281 | QRhiRenderBuffer *depthStencilForSwapchain = nullptr; |
| 282 | QRhiRenderPassDescriptor *rpDescForSwapchain = nullptr; |
| 283 | uint hasActiveSwapchain : 1; |
| 284 | uint hasRenderableSwapchain : 1; |
| 285 | uint swapchainJustBecameRenderable : 1; |
| 286 | uint updatesEnabled : 1; |
| 287 | bool pendingFontUpdate = false; |
| 288 | bool windowEventDispatch = false; |
| 289 | QPointer<QQuickPalette> windowPaletteRef; |
| 290 | |
| 291 | private: |
| 292 | static void cleanupNodesOnShutdown(QQuickItem *); |
| 293 | }; |
| 294 | |
| 295 | class QQuickWindowQObjectCleanupJob : public QRunnable |
| 296 | { |
| 297 | public: |
| 298 | QQuickWindowQObjectCleanupJob(QObject *o) : object(o) { } |
| 299 | void run() override { delete object; } |
| 300 | QObject *object; |
| 301 | static void schedule(QQuickWindow *window, QObject *object) { |
| 302 | Q_ASSERT(window); |
| 303 | Q_ASSERT(object); |
| 304 | window->scheduleRenderJob(job: new QQuickWindowQObjectCleanupJob(object), schedule: QQuickWindow::AfterSynchronizingStage); |
| 305 | } |
| 306 | }; |
| 307 | |
| 308 | Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickWindowPrivate::TextureFromNativeTextureFlags) |
| 309 | |
| 310 | QT_END_NAMESPACE |
| 311 | |
| 312 | #endif // QQUICKWINDOW_P_H |
| 313 | |