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
qwindowsdirect2dcontext.cpp
Go to the documentation of this file.
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 <QtCore/qt_windows.h>
8
9#include <QtCore/private/qcomptr_p.h>
10
11#include <d3d11_1.h>
12#include <d2d1_1.h>
13#include <d2d1_1helper.h>
14#include <dxgi1_2.h>
15#include <dwrite.h>
16
18
20{
21public:
22 bool init()
23 {
24 HRESULT hr;
25
26 D3D_FEATURE_LEVEL level;
27
28 D3D_DRIVER_TYPE typeAttempts[] = {
29 D3D_DRIVER_TYPE_HARDWARE,
30 D3D_DRIVER_TYPE_WARP
31 };
32 const int ntypes = int(sizeof(typeAttempts) / sizeof(typeAttempts[0]));
33
34 for (int i = 0; i < ntypes; i++) {
35 hr = D3D11CreateDevice(nullptr,
36 typeAttempts[i],
37 nullptr,
38 D3D11_CREATE_DEVICE_SINGLETHREADED | D3D11_CREATE_DEVICE_BGRA_SUPPORT,
39 nullptr,
40 0,
41 D3D11_SDK_VERSION,
42 &d3dDevice,
43 &level,
44 &d3dDeviceContext);
45
46 if (SUCCEEDED(hr))
47 break;
48 }
49
50 if (FAILED(hr)) {
51 qWarning("%s: Could not create Direct3D Device: %#lx", __FUNCTION__, hr);
52 return false;
53 }
54
55 ComPtr<IDXGIDevice1> dxgiDevice;
56 ComPtr<IDXGIAdapter> dxgiAdapter;
57
58 hr = d3dDevice.As(&dxgiDevice);
59 if (FAILED(hr)) {
60 qWarning("%s: DXGI Device interface query failed on D3D Device: %#lx", __FUNCTION__, hr);
61 return false;
62 }
63
64 // Ensure that DXGI doesn't queue more than one frame at a time.
65 dxgiDevice->SetMaximumFrameLatency(1);
66
67 hr = dxgiDevice->GetAdapter(&dxgiAdapter);
68 if (FAILED(hr)) {
69 qWarning("%s: Failed to probe DXGI Device for parent DXGI Adapter: %#lx", __FUNCTION__, hr);
70 return false;
71 }
72
73 hr = dxgiAdapter->GetParent(IID_PPV_ARGS(&dxgiFactory));
74 if (FAILED(hr)) {
75 qWarning("%s: Failed to probe DXGI Adapter for parent DXGI Factory: %#lx", __FUNCTION__, hr);
76 return false;
77 }
78
79 D2D1_FACTORY_OPTIONS options = {};
80
81#ifdef QT_D2D_DEBUG_OUTPUT
82 qDebug("Turning on Direct2D debugging messages");
83 options.debugLevel = D2D1_DEBUG_LEVEL_INFORMATION;
84#endif // QT_D2D_DEBUG_OUTPUT
85
86 hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, options, d2dFactory.GetAddressOf());
87 if (FAILED(hr)) {
88 qWarning("%s: Could not create Direct2D Factory: %#lx", __FUNCTION__, hr);
89 return false;
90 }
91
92 hr = d2dFactory->CreateDevice(dxgiDevice.Get(), &d2dDevice);
93 if (FAILED(hr)) {
94 qWarning("%s: Could not create D2D Device: %#lx", __FUNCTION__, hr);
95 return false;
96 }
97
98 hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory),
99 static_cast<IUnknown **>(&directWriteFactory));
100 if (FAILED(hr)) {
101 qWarning("%s: Could not create DirectWrite factory: %#lx", __FUNCTION__, hr);
102 return false;
103 }
104
105 hr = directWriteFactory->GetGdiInterop(&directWriteGdiInterop);
106 if (FAILED(hr)) {
107 qWarning("%s: Could not create DirectWrite GDI Interop: %#lx", __FUNCTION__, hr);
108 return false;
109 }
110
111 return true;
112 }
113
121};
122
123QWindowsDirect2DContext::QWindowsDirect2DContext()
124 : d_ptr(new QWindowsDirect2DContextPrivate)
125{
126}
127
129
131{
132 Q_D(QWindowsDirect2DContext);
133 return d->init();
134}
135
140
141ID3D11Device *QWindowsDirect2DContext::d3dDevice() const
142{
143 Q_D(const QWindowsDirect2DContext);
144 return d->d3dDevice.Get();
145}
146
148{
149 Q_D(const QWindowsDirect2DContext);
150 return d->d2dDevice.Get();
151}
152
153ID2D1Factory1 *QWindowsDirect2DContext::d2dFactory() const
154{
155 Q_D(const QWindowsDirect2DContext);
156 return d->d2dFactory.Get();
157}
158
160{
161 Q_D(const QWindowsDirect2DContext);
162 return d->dxgiFactory.Get();
163}
164
165ID3D11DeviceContext *QWindowsDirect2DContext::d3dDeviceContext() const
166{
167 Q_D(const QWindowsDirect2DContext);
168 return d->d3dDeviceContext.Get();
169}
170
172{
173 Q_D(const QWindowsDirect2DContext);
174 return d->directWriteFactory.Get();
175}
176
177IDWriteGdiInterop *QWindowsDirect2DContext::dwriteGdiInterop() const
178{
179 Q_D(const QWindowsDirect2DContext);
180 return d->directWriteGdiInterop.Get();
181}
182
183QT_END_NAMESPACE
ComPtr< ID3D11DeviceContext > d3dDeviceContext
ComPtr< IDWriteFactory > directWriteFactory
ComPtr< IDWriteGdiInterop > directWriteGdiInterop
ID2D1Factory1 * d2dFactory() const
IDXGIFactory2 * dxgiFactory() const
IDWriteGdiInterop * dwriteGdiInterop() const
ID3D11DeviceContext * d3dDeviceContext() const
IDWriteFactory * dwriteFactory() const
static QWindowsDirect2DContext * instance()
QWindowsDirect2DContext * direct2DContext() const
static QWindowsDirect2DIntegration * instance()