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

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