1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtQuick module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QSGRHISUPPORT_P_H
41 #define QSGRHISUPPORT_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists purely as an
48 // implementation detail.  This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include "qsgrenderloop_p.h"
55 #include "qsgrendererinterface.h"
56 
57 #include <QtGui/private/qrhi_p.h>
58 
59 #include <QtGui/private/qrhinull_p.h>
60 
61 #if QT_CONFIG(opengl)
62 #include <QtGui/private/qrhigles2_p.h>
63 #endif
64 
65 #if QT_CONFIG(vulkan)
66 #include <QtGui/private/qrhivulkan_p.h>
67 #endif
68 
69 #ifdef Q_OS_WIN
70 #include <QtGui/private/qrhid3d11_p.h>
71 #endif
72 
73 #if defined(Q_OS_MACOS) || defined(Q_OS_IOS)
74 #include <QtGui/private/qrhimetal_p.h>
75 #endif
76 
77 #if QT_CONFIG(qml_network)
78 #define RHI_REMOTE_PROFILER
79 #include <QtCore/qelapsedtimer.h>
80 #include <QtCore/qscopedpointer.h>
81 #include <QtNetwork/qtcpsocket.h>
82 #include <QtGui/private/qrhiprofiler_p.h>
83 #endif
84 
85 QT_BEGIN_NAMESPACE
86 
87 class QSGDefaultRenderContext;
88 class QVulkanInstance;
89 class QOffscreenSurface;
90 
91 // Opting in/out of QRhi and choosing the default/requested backend is managed
92 // by this singleton. This is because this information may be needed before
93 // creating a render loop. A well-written render loop sets up its QRhi and
94 // related machinery based on the settings queriable from here.
95 //
96 // cleanup() must be called to perform global (not per thread) cleanup, such
97 // as, destroying the QVulkanInstance (if one was created in vulkanInstance()).
98 //
99 // In addition, the class provides handy conversion and query stuff for the
100 // renderloop and the QSGRendererInterface implementations.
101 //
102 class QSGRhiSupport
103 {
104 public:
105     static void configure(QSGRendererInterface::GraphicsApi api);
106     static QSGRhiSupport *instance();
107     static QVulkanInstance *vulkanInstance();
108     void cleanup();
109 
isRhiEnabled()110     bool isRhiEnabled() const { return m_enableRhi; }
rhiBackend()111     QRhi::Implementation rhiBackend() const { return m_rhiBackend; }
112     QString rhiBackendName() const;
113     QSGRendererInterface::GraphicsApi graphicsApi() const;
114 
isDebugLayerRequested()115     bool isDebugLayerRequested() const { return m_debugLayer; }
isProfilingRequested()116     bool isProfilingRequested() const { return m_profile; }
isShaderEffectDebuggingRequested()117     bool isShaderEffectDebuggingRequested() const { return m_shaderEffectDebug; }
isSoftwareRendererRequested()118     bool isSoftwareRendererRequested() const { return m_preferSoftwareRenderer; }
119 
120     QSurface::SurfaceType windowSurfaceType() const;
121 
122     const void *rifResource(QSGRendererInterface::Resource res, const QSGDefaultRenderContext *rc);
123 
124     int chooseSampleCountForWindowWithRhi(QWindow *window, QRhi *rhi);
125 
126     QOffscreenSurface *maybeCreateOffscreenSurface(QWindow *window);
127     QRhi *createRhi(QWindow *window, QOffscreenSurface *offscreenSurface);
128 
129     QImage grabAndBlockInCurrentFrame(QRhi *rhi, QRhiSwapChain *swapchain);
130 
131     static void checkEnvQSgInfo();
132 
133 private:
134     QSGRhiSupport();
135     void applySettings();
136     static QSGRhiSupport *staticInst();
137     struct {
138         bool valid = false;
139         QSGRendererInterface::GraphicsApi api;
140         uint rhi : 1;
141     } m_requested;
142     QRhi::Implementation m_rhiBackend = QRhi::Null;
143     int m_killDeviceFrameCount;
144     uint m_set : 1;
145     uint m_enableRhi : 1;
146     uint m_debugLayer : 1;
147     uint m_profile : 1;
148     uint m_shaderEffectDebug : 1;
149     uint m_preferSoftwareRenderer : 1;
150 };
151 
152 // Sends QRhi resource statistics over a QTcpSocket. To be initialized by the
153 // renderloop when QSGRhiSupport::isProfilingRequested() is true. Will not do
154 // anything unless extra env vars (QSG_RHI_PROFILE_HOST) are set.
155 class QSGRhiProfileConnection
156 {
157 public:
158     static QSGRhiProfileConnection *instance();
159     void initialize(QRhi *rhi);
160     void cleanup();
161     void send(QRhi *rhi);
162 
163 private:
164 #ifdef RHI_REMOTE_PROFILER
165     QScopedPointer<QTcpSocket> m_profConn;
166     QElapsedTimer m_lastMemStatWrite;
167 #endif
168 };
169 
170 QT_END_NAMESPACE
171 
172 #endif // QSGRHISUPPORT_P_H
173