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#ifdef Q_OS_DARWIN
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
85QT_BEGIN_NAMESPACE
86
87class QSGDefaultRenderContext;
88class QVulkanInstance;
89
90// Opting in/out of QRhi and choosing the default/requested backend is managed
91// by this singleton. This is because this information may be needed before
92// creating a render loop. A well-written render loop sets up its QRhi and
93// related machinery based on the settings queriable from here.
94//
95// cleanup() must be called to perform global (not per thread) cleanup, such
96// as, destroying the QVulkanInstance (if one was created in vulkanInstance()).
97//
98// In addition, the class provides handy conversion and query stuff for the
99// renderloop and the QSGRendererInterface implementations.
100//
101class QSGRhiSupport
102{
103public:
104 static void configure(QSGRendererInterface::GraphicsApi api);
105 static QSGRhiSupport *instance();
106 static QVulkanInstance *vulkanInstance();
107 void cleanup();
108
109 bool isRhiEnabled() const { return m_enableRhi; }
110 QRhi::Implementation rhiBackend() const { return m_rhiBackend; }
111 QSGRendererInterface::GraphicsApi graphicsApi() const;
112
113 bool isDebugLayerRequested() const { return m_debugLayer; }
114 bool isProfilingRequested() const { return m_profile; }
115 bool isShaderEffectDebuggingRequested() const { return m_shaderEffectDebug; }
116
117 QSurface::SurfaceType windowSurfaceType() const;
118
119 const void *rifResource(QSGRendererInterface::Resource res, const QSGDefaultRenderContext *rc);
120
121 int chooseSampleCountForWindowWithRhi(QWindow *window, QRhi *rhi);
122
123 QOffscreenSurface *maybeCreateOffscreenSurface(QWindow *window);
124 QRhi *createRhi(QWindow *window, QOffscreenSurface *offscreenSurface);
125
126 QImage grabAndBlockInCurrentFrame(QRhi *rhi, QRhiSwapChain *swapchain);
127
128private:
129 QSGRhiSupport();
130 void applySettings();
131 static QSGRhiSupport *staticInst();
132 struct {
133 bool valid = false;
134 QSGRendererInterface::GraphicsApi api;
135 uint rhi : 1;
136 } m_requested;
137 QRhi::Implementation m_rhiBackend = QRhi::Null;
138 uint m_set : 1;
139 uint m_enableRhi : 1;
140 uint m_debugLayer : 1;
141 uint m_profile : 1;
142 uint m_shaderEffectDebug : 1;
143};
144
145// Sends QRhi resource statistics over a QTcpSocket. To be initialized by the
146// renderloop when QSGRhiSupport::isProfilingRequested() is true. Will not do
147// anything unless extra env vars (QSG_RHI_PROFILE_HOST) are set.
148class QSGRhiProfileConnection
149{
150public:
151 static QSGRhiProfileConnection *instance();
152 void initialize(QRhi *rhi);
153 void cleanup();
154 void send(QRhi *rhi);
155
156private:
157#ifdef RHI_REMOTE_PROFILER
158 QScopedPointer<QTcpSocket> m_profConn;
159 QElapsedTimer m_lastMemStatWrite;
160#endif
161};
162
163QT_END_NAMESPACE
164
165#endif // QSGRHISUPPORT_P_H
166