Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickgraphicsdevice.cpp
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
6
8
9/*!
10 \class QQuickGraphicsDevice
11 \since 6.0
12 \inmodule QtQuick
13
14 \brief The QQuickGraphicsDevice class provides an opaque container for
15 native graphics objects representing graphics devices or contexts.
16
17 \sa QQuickWindow::setGraphicsDevice(), QQuickRenderTarget
18*/
19
20/*!
21 Constructs a default QQuickGraphicsDevice that does not reference any native
22 objects.
23 */
24QQuickGraphicsDevice::QQuickGraphicsDevice()
25 : d(new QQuickGraphicsDevicePrivate)
26{
27}
28
29/*!
30 \internal
31 */
32void QQuickGraphicsDevice::detach()
33{
34 qAtomicDetach(d);
35}
36
37/*!
38 \internal
39 */
40QQuickGraphicsDevice::QQuickGraphicsDevice(const QQuickGraphicsDevice &other)
41 : d(other.d)
42{
43 d->ref.ref();
44}
45
46/*!
47 \internal
48 */
49QQuickGraphicsDevice &QQuickGraphicsDevice::operator=(const QQuickGraphicsDevice &other)
50{
51 qAtomicAssign(d, other.d);
52 return *this;
53}
54
55/*!
56 Destructor.
57 */
58QQuickGraphicsDevice::~QQuickGraphicsDevice()
59{
60 if (!d->ref.deref())
61 delete d;
62}
63
64/*!
65 \return true if this is a default constructed graphics device that
66 does not reference any native objects.
67 */
68bool QQuickGraphicsDevice::isNull() const
69{
70 return d->type == QQuickGraphicsDevicePrivate::Type::Null;
71}
72
73/*!
74 \return a new QQuickGraphicsDevice referencing an existing OpenGL \a context.
75
76 This factory function is suitable for OpenGL.
77
78 \note It is up the caller to ensure that \a context is going to be
79 compatible and usable with the QQuickWindow. Platform-specific mismatches in
80 the associated QSurfaceFormat, or threading issues due to attempting to use
81 \a context on multiple threads are up to the caller to avoid.
82 */
83#if QT_CONFIG(opengl) || defined(Q_QDOC)
84QQuickGraphicsDevice QQuickGraphicsDevice::fromOpenGLContext(QOpenGLContext *context)
85{
86 QQuickGraphicsDevice dev;
87 QQuickGraphicsDevicePrivate *d = QQuickGraphicsDevicePrivate::get(&dev);
88 d->type = QQuickGraphicsDevicePrivate::Type::OpenGLContext;
89 d->u.context = context;
90 return dev;
91}
92#endif
93
94/*!
95 \return a new QQuickGraphicsDevice describing a DXGI adapter and D3D feature level.
96
97 This factory function is suitable for Direct3D 11 and 12, particularly in
98 combination with OpenXR. \a adapterLuidLow and \a adapterLuidHigh together
99 specify a LUID, while a featureLevel specifies a \c{D3D_FEATURE_LEVEL_}
100 value. \a featureLevel can be set to 0 if it is not intended to be
101 specified, in which case the scene graph's defaults will be used.
102
103 \note With Direct 3D 12 \a featureLevel specifies the \c minimum feature
104 level passed on to D3D12CreateDevice().
105 */
106#if defined(Q_OS_WIN) || defined(Q_QDOC)
107QQuickGraphicsDevice QQuickGraphicsDevice::fromAdapter(quint32 adapterLuidLow,
108 qint32 adapterLuidHigh,
109 int featureLevel)
110{
111 QQuickGraphicsDevice dev;
112 QQuickGraphicsDevicePrivate *d = QQuickGraphicsDevicePrivate::get(&dev);
113 d->type = QQuickGraphicsDevicePrivate::Type::Adapter;
114 d->u.adapter = { adapterLuidLow, adapterLuidHigh, featureLevel };
115 return dev;
116}
117#endif
118
119/*!
120 \return a new QQuickGraphicsDevice referencing a native device and context
121 object.
122
123 This factory function is suitable for Direct3D 11. \a device is expected to
124 be a \c{ID3D11Device*}, \a context is expected to be a
125 \c{ID3D11DeviceContext*}.
126
127 It also supports Direct 3D 12, if that is the 3D API used at run time. With
128 D3D12 \a context is unused and can be set to null. \a device is expected to
129 be a \c{ID3D12Device*}.
130
131 \note the resulting QQuickGraphicsDevice does not own any native resources,
132 it merely contains references. It is the caller's responsibility to ensure
133 that the native resource exists as long as necessary.
134 */
135#if defined(Q_OS_WIN) || defined(Q_QDOC)
136QQuickGraphicsDevice QQuickGraphicsDevice::fromDeviceAndContext(void *device, void *context)
137{
138 QQuickGraphicsDevice dev;
139 QQuickGraphicsDevicePrivate *d = QQuickGraphicsDevicePrivate::get(&dev);
140 d->type = QQuickGraphicsDevicePrivate::Type::DeviceAndContext;
141 d->u.deviceAndContext = { device, context };
142 return dev;
143}
144#endif
145
146/*!
147 \return a new QQuickGraphicsDevice referencing an existing \a device and
148 \a commandQueue object.
149
150 This factory function is suitable for Metal.
151
152 \note the resulting QQuickGraphicsDevice does not own any native resources,
153 it merely contains references. It is the caller's responsibility to ensure
154 that the native resource exists as long as necessary.
155
156 */
157#if QT_CONFIG(metal) || defined(Q_QDOC)
158QQuickGraphicsDevice QQuickGraphicsDevice::fromDeviceAndCommandQueue(MTLDevice *device,
159 MTLCommandQueue *commandQueue)
160{
161 QQuickGraphicsDevice dev;
162 QQuickGraphicsDevicePrivate *d = QQuickGraphicsDevicePrivate::get(&dev);
163 d->type = QQuickGraphicsDevicePrivate::Type::DeviceAndCommandQueue;
164 d->u.deviceAndCommandQueue = { device, commandQueue };
165 return dev;
166}
167#endif
168
169/*!
170 \return a new QQuickGraphicsDevice referencing an existing \a physicalDevice.
171
172 This factory function is suitable for Vulkan, particularly in combination
173 with OpenXR.
174
175 \note the resulting QQuickGraphicsDevice does not own any native resources,
176 it merely contains references. It is the caller's responsibility to ensure
177 that the native resource exists as long as necessary.
178 */
179#if QT_CONFIG(vulkan) || defined(Q_QDOC)
180QQuickGraphicsDevice QQuickGraphicsDevice::fromPhysicalDevice(VkPhysicalDevice physicalDevice)
181{
182 QQuickGraphicsDevice dev;
183 QQuickGraphicsDevicePrivate *d = QQuickGraphicsDevicePrivate::get(&dev);
184 d->type = QQuickGraphicsDevicePrivate::Type::PhysicalDevice;
185 d->u.physicalDevice = { physicalDevice };
186 return dev;
187}
188#endif
189
190/*!
191 \return a new QQuickGraphicsDevice referencing an existing \a device object.
192
193 This factory function is suitable for Vulkan. \a physicalDevice, \a device
194 and \a queueFamilyIndex must always be provided. \a queueIndex is optional
195 since the default value of 0 is often suitable.
196
197 \note the resulting QQuickGraphicsDevice does not own any native resources,
198 it merely contains references. It is the caller's responsibility to ensure
199 that the native resource exists as long as necessary.
200 */
201#if QT_CONFIG(vulkan) || defined(Q_QDOC)
202QQuickGraphicsDevice QQuickGraphicsDevice::fromDeviceObjects(VkPhysicalDevice physicalDevice,
203 VkDevice device,
204 int queueFamilyIndex,
205 int queueIndex)
206{
207 QQuickGraphicsDevice dev;
208 QQuickGraphicsDevicePrivate *d = QQuickGraphicsDevicePrivate::get(&dev);
209 d->type = QQuickGraphicsDevicePrivate::Type::DeviceObjects;
210 d->u.deviceObjects = { physicalDevice, device, queueFamilyIndex, queueIndex };
211 return dev;
212}
213#endif
214
215/*!
216 \return a new QQuickGraphicsDevice referencing an existing \a rhi object.
217
218 \note Similarly to fromOpenGLContext(), the caller must be careful to only
219 share a QRhi (and so the underlying graphics context or device) between
220 QQuickWindows that are known to be compatible, not breaking the underlying
221 graphics API's rules when it comes to threading, pixel formats, etc.
222
223 \since 6.6
224*/
225QQuickGraphicsDevice QQuickGraphicsDevice::fromRhi(QRhi *rhi)
226{
227 QQuickGraphicsDevice dev;
228 QQuickGraphicsDevicePrivate *d = QQuickGraphicsDevicePrivate::get(&dev);
229 d->type = QQuickGraphicsDevicePrivate::Type::Rhi;
230 d->u.rhi = rhi;
231 return dev;
232}
233
234/*!
235 \return a new QQuickGraphicsDevice referencing an existing \a adapter QRhiAdapter object.
236
237 Not applicable to QRhi backends and graphics APIs where QRhiAdapter does not
238 have a real implementation.
239
240 Equivalent to fromAdapter() for Direct 3D, and fromPhysicalDevice() for Vulkan.
241
242 \note Ownership is not taken for \a adapter, and it must stay valid at
243 minimum until the scene graph intializes, which most likely happens when the
244 associated QQuickWindow becomes exposed.
245
246 \since 6.10
247 \sa QRhi::enumerateAdapters()
248*/
249QQuickGraphicsDevice QQuickGraphicsDevice::fromRhiAdapter(QRhiAdapter *adapter)
250{
251 QQuickGraphicsDevice dev;
252 QQuickGraphicsDevicePrivate *d = QQuickGraphicsDevicePrivate::get(&dev);
253 d->type = QQuickGraphicsDevicePrivate::Type::RhiAdapter;
254 d->u.rhiAdapter = adapter;
255 return dev;
256}
257
258QQuickGraphicsDevicePrivate::QQuickGraphicsDevicePrivate()
259 : ref(1)
260{
261}
262
263QQuickGraphicsDevicePrivate::QQuickGraphicsDevicePrivate(const QQuickGraphicsDevicePrivate &other)
264 : ref(1),
265 type(other.type),
266 u(other.u)
267{
268}
269
270QT_END_NAMESPACE