Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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 <d3d11_1.h>
10#include <d2d1_1.h>
11#include <d2d1_1helper.h>
12#include <dxgi1_2.h>
13#include <wrl.h>
14#include <dwrite.h>
15
17
19
21{
22public:
23 bool init()
24 {
25 HRESULT hr;
26
27 D3D_FEATURE_LEVEL level;
28
29 D3D_DRIVER_TYPE typeAttempts[] = {
30 D3D_DRIVER_TYPE_HARDWARE,
31 D3D_DRIVER_TYPE_WARP
32 };
33 const int ntypes = int(sizeof(typeAttempts) / sizeof(typeAttempts[0]));
34
35 for (int i = 0; i < ntypes; i++) {
36 hr = D3D11CreateDevice(nullptr,
37 typeAttempts[i],
38 nullptr,
39 D3D11_CREATE_DEVICE_SINGLETHREADED | D3D11_CREATE_DEVICE_BGRA_SUPPORT,
40 nullptr,
41 0,
42 D3D11_SDK_VERSION,
43 &d3dDevice,
44 &level,
46
47 if (SUCCEEDED(hr))
48 break;
49 }
50
51 if (FAILED(hr)) {
52 qWarning("%s: Could not create Direct3D Device: %#lx", __FUNCTION__, hr);
53 return false;
54 }
55
56 ComPtr<IDXGIDevice1> dxgiDevice;
57 ComPtr<IDXGIAdapter> dxgiAdapter;
58
59 hr = d3dDevice.As(&dxgiDevice);
60 if (FAILED(hr)) {
61 qWarning("%s: DXGI Device interface query failed on D3D Device: %#lx", __FUNCTION__, hr);
62 return false;
63 }
64
65 // Ensure that DXGI doesn't queue more than one frame at a time.
66 dxgiDevice->SetMaximumFrameLatency(1);
67
68 hr = dxgiDevice->GetAdapter(&dxgiAdapter);
69 if (FAILED(hr)) {
70 qWarning("%s: Failed to probe DXGI Device for parent DXGI Adapter: %#lx", __FUNCTION__, hr);
71 return false;
72 }
73
74 hr = dxgiAdapter->GetParent(IID_PPV_ARGS(&dxgiFactory));
75 if (FAILED(hr)) {
76 qWarning("%s: Failed to probe DXGI Adapter for parent DXGI Factory: %#lx", __FUNCTION__, hr);
77 return false;
78 }
79
80 D2D1_FACTORY_OPTIONS options = {};
81
82#ifdef QT_D2D_DEBUG_OUTPUT
83 qDebug("Turning on Direct2D debugging messages");
84 options.debugLevel = D2D1_DEBUG_LEVEL_INFORMATION;
85#endif // QT_D2D_DEBUG_OUTPUT
86
87 hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, options, d2dFactory.GetAddressOf());
88 if (FAILED(hr)) {
89 qWarning("%s: Could not create Direct2D Factory: %#lx", __FUNCTION__, hr);
90 return false;
91 }
92
93 hr = d2dFactory->CreateDevice(dxgiDevice.Get(), &d2dDevice);
94 if (FAILED(hr)) {
95 qWarning("%s: Could not create D2D Device: %#lx", __FUNCTION__, hr);
96 return false;
97 }
98
99 hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory),
100 static_cast<IUnknown **>(&directWriteFactory));
101 if (FAILED(hr)) {
102 qWarning("%s: Could not create DirectWrite factory: %#lx", __FUNCTION__, hr);
103 return false;
104 }
105
106 hr = directWriteFactory->GetGdiInterop(&directWriteGdiInterop);
107 if (FAILED(hr)) {
108 qWarning("%s: Could not create DirectWrite GDI Interop: %#lx", __FUNCTION__, hr);
109 return false;
110 }
111
112 return true;
113 }
114
115 ComPtr<ID3D11Device> d3dDevice;
116 ComPtr<ID2D1Factory1> d2dFactory;
117 ComPtr<ID2D1Device> d2dDevice;
118 ComPtr<IDXGIFactory2> dxgiFactory;
119 ComPtr<ID3D11DeviceContext> d3dDeviceContext;
120 ComPtr<IDWriteFactory> directWriteFactory;
121 ComPtr<IDWriteGdiInterop> directWriteGdiInterop;
122};
123
128
130
132{
134 return d->init();
135}
136
141
143{
144 Q_D(const QWindowsDirect2DContext);
145 return d->d3dDevice.Get();
146}
147
149{
150 Q_D(const QWindowsDirect2DContext);
151 return d->d2dDevice.Get();
152}
153
155{
156 Q_D(const QWindowsDirect2DContext);
157 return d->d2dFactory.Get();
158}
159
161{
162 Q_D(const QWindowsDirect2DContext);
163 return d->dxgiFactory.Get();
164}
165
167{
168 Q_D(const QWindowsDirect2DContext);
169 return d->d3dDeviceContext.Get();
170}
171
173{
174 Q_D(const QWindowsDirect2DContext);
175 return d->directWriteFactory.Get();
176}
177
179{
180 Q_D(const QWindowsDirect2DContext);
181 return d->directWriteGdiInterop.Get();
182}
183
ComPtr< IDWriteFactory > directWriteFactory
ComPtr< IDWriteGdiInterop > directWriteGdiInterop
ComPtr< ID3D11DeviceContext > d3dDeviceContext
ID2D1Factory1 * d2dFactory() const
IDXGIFactory2 * dxgiFactory() const
IDWriteGdiInterop * dwriteGdiInterop() const
ID3D11DeviceContext * d3dDeviceContext() const
IDWriteFactory * dwriteFactory() const
static QWindowsDirect2DContext * instance()
static QWindowsDirect2DIntegration * instance()
Combined button and popup list for selecting options.
#define qDebug
[1]
Definition qlogging.h:164
#define qWarning
Definition qlogging.h:166
GLenum GLuint GLint level
long HRESULT