1// Copyright (C) 2016 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 "qquickgraphicsinfo_p.h"
5#include <private/qquickitem_p.h>
6#include <qopenglcontext.h>
7
8QT_BEGIN_NAMESPACE
9
10/*!
11 \qmltype GraphicsInfo
12 \instantiates QQuickGraphicsInfo
13 \inqmlmodule QtQuick
14 \ingroup qtquick-visual
15 \since 5.8
16 \since QtQuick 2.8
17 \brief Provides information about the scenegraph backend and the graphics API used by Qt Quick.
18
19 The GraphicsInfo attached type provides information about the scenegraph
20 backend and the graphics API used to render the contents of the associated window.
21
22 If the item to which the properties are attached is not currently
23 associated with any window, the properties are set to default values. When
24 the associated window changes, the properties will update.
25 */
26
27QQuickGraphicsInfo::QQuickGraphicsInfo(QQuickItem *item)
28 : QObject(item)
29 , m_window(nullptr)
30 , m_api(Unknown)
31 , m_shaderType(UnknownShadingLanguage)
32 , m_shaderCompilationType(ShaderCompilationType(0))
33 , m_shaderSourceType(ShaderSourceType(0))
34 , m_majorVersion(2)
35 , m_minorVersion(0)
36 , m_profile(OpenGLNoProfile)
37 , m_renderableType(SurfaceFormatUnspecified)
38{
39 if (Q_LIKELY(item)) {
40 connect(sender: item, signal: &QQuickItem::windowChanged, context: this, slot: &QQuickGraphicsInfo::setWindow);
41 setWindow(item->window());
42 }
43}
44
45QQuickGraphicsInfo *QQuickGraphicsInfo::qmlAttachedProperties(QObject *object)
46{
47 if (QQuickItem *item = qobject_cast<QQuickItem *>(o: object))
48 return new QQuickGraphicsInfo(item);
49
50 return nullptr;
51}
52
53/*!
54 \qmlproperty enumeration QtQuick::GraphicsInfo::api
55
56 This property describes the graphics API that is currently in use.
57
58 The possible values are:
59
60 \value GraphicsInfo.Unknown the default value when no active scenegraph is associated with the item
61 \value GraphicsInfo.Software Qt Quick's software renderer based on QPainter with the raster paint engine
62 \value GraphicsInfo.OpenVG OpenVG
63 \value GraphicsInfo.OpenGL OpenGL or OpenGL ES on top of QRhi, a graphics abstraction layer
64 \value GraphicsInfo.Direct3D11 Direct3D 11 on top of QRhi, a graphics abstraction layer
65 \value GraphicsInfo.Direct3D12 Direct3D 12 on top of QRhi, a graphics abstraction layer
66 \value GraphicsInfo.Vulkan Vulkan on top of QRhi, a graphics abstraction layer
67 \value GraphicsInfo.Metal Metal on top of QRhi, a graphics abstraction layer
68 \value GraphicsInfo.Null Null (no output) on top of QRhi, a graphics abstraction layer
69 */
70
71/*!
72 \qmlproperty enumeration QtQuick::GraphicsInfo::shaderType
73
74 This property contains the shading language supported by the Qt Quick
75 backend the application is using.
76
77 \value GraphicsInfo.UnknownShadingLanguage Not yet known due to no window and scenegraph associated
78 \value GraphicsInfo.GLSL GLSL or GLSL ES
79 \value GraphicsInfo.HLSL HLSL
80 \value GraphicsInfo.RhiShader QShader
81
82 \note The value is only up-to-date once the item is associated with a
83 window. Bindings relying on the value have to keep this in mind since the
84 value may change from GraphicsInfo.UnknownShadingLanguage to the actual
85 value after component initialization is complete. This is particularly
86 relevant for ShaderEffect items inside ShaderEffectSource items set as
87 property values.
88
89 \since 5.8
90 \since QtQuick 2.8
91
92 \sa shaderCompilationType, shaderSourceType
93*/
94
95/*!
96 \qmlproperty enumeration QtQuick::GraphicsInfo::shaderCompilationType
97
98 This property contains a bitmask of the shader compilation approaches
99 supported by the Qt Quick backend the application is using.
100
101 \value GraphicsInfo.RuntimeCompilation
102 \value GraphicsInfo.OfflineCompilation
103
104 With OpenGL the value is GraphicsInfo.RuntimeCompilation, which corresponds
105 to the traditional way of using ShaderEffect. Non-OpenGL backends are
106 expected to focus more on GraphicsInfo.OfflineCompilation, however.
107
108 \note The value is only up-to-date once the item is associated with a
109 window. Bindings relying on the value have to keep this in mind since the
110 value may change from \c 0 to the actual bitmask after component
111 initialization is complete. This is particularly relevant for ShaderEffect
112 items inside ShaderEffectSource items set as property values.
113
114 \since 5.8
115 \since QtQuick 2.8
116
117 \sa shaderType, shaderSourceType
118*/
119
120/*!
121 \qmlproperty enumeration QtQuick::GraphicsInfo::shaderSourceType
122
123 This property contains a bitmask of the supported ways of providing shader
124 sources.
125
126 \value GraphicsInfo.ShaderSourceString
127 \value GraphicsInfo.ShaderSourceFile
128 \value GraphicsInfo.ShaderByteCode
129
130 With OpenGL the value is GraphicsInfo.ShaderSourceString, which corresponds
131 to the traditional way of inlining GLSL source code into QML. Other,
132 non-OpenGL Qt Quick backends may however decide not to support inlined
133 shader sources, or even shader sources at all. In this case shaders are
134 expected to be pre-compiled into formats like SPIR-V or D3D shader
135 bytecode.
136
137 \note The value is only up-to-date once the item is associated with a
138 window. Bindings relying on the value have to keep this in mind since the
139 value may change from \c 0 to the actual bitmask after component
140 initialization is complete. This is particularly relevant for ShaderEffect
141 items inside ShaderEffectSource items set as property values.
142
143 \since 5.8
144 \since QtQuick 2.8
145
146 \sa shaderType, shaderCompilationType
147*/
148
149/*!
150 \qmlproperty int QtQuick::GraphicsInfo::majorVersion
151
152 This property holds the major version of the graphics API in use.
153
154 With OpenGL the default version is \c 2.0.
155
156 \note This is applicable only to OpenGL.
157
158 \sa minorVersion, profile
159 */
160
161/*!
162 \qmlproperty int QtQuick::GraphicsInfo::minorVersion
163
164 This property holds the minor version of the graphics API in use.
165
166 With OpenGL the default version is \c 2.0.
167
168 \note This is applicable only to OpenGL.
169
170 \sa majorVersion, profile
171 */
172
173/*!
174 \qmlproperty enumeration QtQuick::GraphicsInfo::profile
175
176 This property holds the configured OpenGL context profile.
177
178 The possible values are:
179
180 \value GraphicsInfo.OpenGLNoProfile (default) OpenGL version is lower than 3.2 or OpenGL is not in use.
181 \value GraphicsInfo.OpenGLCoreProfile Functionality deprecated in OpenGL version 3.0 is not available.
182 \value GraphicsInfo.OpenGLCompatibilityProfile Functionality from earlier OpenGL versions is available.
183
184 Reusable QML components will typically use this property in bindings in order to
185 choose between core and non core profile compatible shader sources.
186
187 \note This is applicable only to OpenGL.
188
189 \sa majorVersion, minorVersion, QSurfaceFormat
190 */
191
192/*!
193 \qmlproperty enumeration QtQuick::GraphicsInfo::renderableType
194
195 This property holds the renderable type. The value has no meaning for APIs
196 other than OpenGL.
197
198 The possible values are:
199
200 \value GraphicsInfo.SurfaceFormatUnspecified (default) Unspecified rendering method
201 \value GraphicsInfo.SurfaceFormatOpenGL Desktop OpenGL or other graphics API
202 \value GraphicsInfo.SurfaceFormatOpenGLES OpenGL ES
203
204 \note This is applicable only to OpenGL.
205
206 \sa QSurfaceFormat
207 */
208
209void QQuickGraphicsInfo::updateInfo()
210{
211 // The queries via the RIF do not depend on isSceneGraphInitialized(), they only need a window.
212 if (m_window) {
213 QSGRendererInterface *rif = m_window->rendererInterface();
214 if (rif) {
215 GraphicsApi newAPI = GraphicsApi(rif->graphicsApi());
216 if (m_api != newAPI) {
217 m_api = newAPI;
218 emit apiChanged();
219 m_shaderType = ShaderType(rif->shaderType());
220 emit shaderTypeChanged();
221 m_shaderCompilationType = ShaderCompilationType(int(rif->shaderCompilationType()));
222 emit shaderCompilationTypeChanged();
223 m_shaderSourceType = ShaderSourceType(int(rif->shaderSourceType()));
224 emit shaderSourceTypeChanged();
225 }
226 }
227 }
228
229 QSurfaceFormat format = QSurfaceFormat::defaultFormat();
230#if QT_CONFIG(opengl)
231 if (m_window && m_window->isSceneGraphInitialized()) {
232 QOpenGLContext *context = QQuickWindowPrivate::get(c: m_window)->openglContext();
233 if (context)
234 format = context->format();
235 }
236#endif
237 if (m_majorVersion != format.majorVersion()) {
238 m_majorVersion = format.majorVersion();
239 emit majorVersionChanged();
240 }
241 if (m_minorVersion != format.minorVersion()) {
242 m_minorVersion = format.minorVersion();
243 emit minorVersionChanged();
244 }
245 OpenGLContextProfile profile = static_cast<OpenGLContextProfile>(format.profile());
246 if (m_profile != profile) {
247 m_profile = profile;
248 emit profileChanged();
249 }
250 RenderableType renderableType = static_cast<RenderableType>(format.renderableType());
251 if (m_renderableType != renderableType) {
252 m_renderableType = renderableType;
253 emit renderableTypeChanged();
254 }
255}
256
257void QQuickGraphicsInfo::setWindow(QQuickWindow *window)
258{
259 if (m_window != window) {
260 if (m_window) {
261 disconnect(sender: m_window, SIGNAL(sceneGraphInitialized()), receiver: this, SLOT(updateInfo()));
262 disconnect(sender: m_window, SIGNAL(sceneGraphInvalidated()), receiver: this, SLOT(updateInfo()));
263 }
264 if (window) {
265 connect(sender: window, SIGNAL(sceneGraphInitialized()), receiver: this, SLOT(updateInfo()));
266 connect(sender: window, SIGNAL(sceneGraphInvalidated()), receiver: this, SLOT(updateInfo()));
267 }
268 m_window = window;
269 }
270 updateInfo();
271}
272
273QT_END_NAMESPACE
274
275#include "moc_qquickgraphicsinfo_p.cpp"
276

source code of qtdeclarative/src/quick/items/qquickgraphicsinfo.cpp