Skip to content

Commit 79b7621

Browse files
authored
Code Quality: Use ComPtr for all D3D11 pointers (#16908)
1 parent 383d82c commit 79b7621

File tree

2 files changed

+41
-33
lines changed

2 files changed

+41
-33
lines changed

src/Files.App.CsWin32/Windows.Win32.ComPtr.cs

+8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ public ComPtr(T* ptr)
2727
((IUnknown*)ptr)->AddRef();
2828
}
2929

30+
public void Attach(T* other)
31+
{
32+
if (_ptr is not null)
33+
((IUnknown*)_ptr)->Release();
34+
35+
_ptr = other;
36+
}
37+
3038
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3139
public readonly T* Get()
3240
{

src/Files.App/ViewModels/UserControls/Previews/ShellPreviewViewModel.cs

+33-33
Original file line numberDiff line numberDiff line change
@@ -161,61 +161,61 @@ private unsafe bool ChildWindowToXaml(nint parent, UIElement presenter)
161161
D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_WARP,
162162
];
163163

164-
ID3D11Device* pD3D11Device = default;
165-
ID3D11DeviceContext* pD3D11DeviceContext = default;
166-
167-
foreach (var driveType in driverTypes)
164+
Windows.Win32.Foundation.HRESULT hr = default;
165+
Guid IID_IDCompositionDevice = typeof(IDCompositionDevice).GUID;
166+
using ComPtr<ID3D11Device> pD3D11Device = default;
167+
using ComPtr<ID3D11DeviceContext> pD3D11DeviceContext = default;
168+
using ComPtr<IDXGIDevice> pDXGIDevice = default;
169+
using ComPtr<IDCompositionDevice> pDCompositionDevice = default;
170+
using ComPtr<IUnknown> pControlSurface = default;
171+
ComPtr<IDCompositionVisual> pChildVisual = default; // Don't dispose this one, it's used by the compositor
172+
173+
// Create the D3D11 device
174+
foreach (var driverType in driverTypes)
168175
{
169-
var hr = PInvoke.D3D11CreateDevice(
170-
null,
171-
driveType,
172-
new(nint.Zero),
176+
hr = PInvoke.D3D11CreateDevice(
177+
null, driverType, new(nint.Zero),
173178
D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_BGRA_SUPPORT,
174-
null,
175-
0,
176-
7,
177-
&pD3D11Device,
178-
null,
179-
&pD3D11DeviceContext);
179+
null, /* FeatureLevels */ 0, /* SDKVersion */ 7,
180+
pD3D11Device.GetAddressOf(), null,
181+
pD3D11DeviceContext.GetAddressOf());
180182

181183
if (hr.Succeeded)
182184
break;
183185
}
184186

185-
if (pD3D11Device is null)
187+
if (pD3D11Device.IsNull)
186188
return false;
187189

188-
IDXGIDevice* pDXGIDevice = (IDXGIDevice*)pD3D11Device;
189-
if (PInvoke.DCompositionCreateDevice(pDXGIDevice, typeof(IDCompositionDevice).GUID, out var compositionDevicePtr).Failed)
190+
// Create the DComp device
191+
pDXGIDevice.Attach((IDXGIDevice*)pD3D11Device.Get());
192+
hr = PInvoke.DCompositionCreateDevice(
193+
pDXGIDevice.Get(),
194+
&IID_IDCompositionDevice,
195+
(void**)pDCompositionDevice.GetAddressOf());
196+
if (hr.Failed)
190197
return false;
191198

192-
var pDCompositionDevice = (IDCompositionDevice*)compositionDevicePtr;
193-
IDCompositionVisual* pChildVisual = default;
194-
IUnknown* pControlSurface = default;
195-
196-
pDCompositionDevice->CreateVisual(&pChildVisual);
197-
pDCompositionDevice->CreateSurfaceFromHwnd(new(hwnd.DangerousGetHandle()), &pControlSurface);
198-
pChildVisual->SetContent(pControlSurface);
199-
if (pChildVisual is null || pControlSurface is null)
199+
// Create the visual
200+
hr = pDCompositionDevice.Get()->CreateVisual(pChildVisual.GetAddressOf());
201+
hr = pDCompositionDevice.Get()->CreateSurfaceFromHwnd(new(hwnd.DangerousGetHandle()), pControlSurface.GetAddressOf());
202+
hr = pChildVisual.Get()->SetContent(pControlSurface.Get());
203+
if (pChildVisual.IsNull || pControlSurface.IsNull)
200204
return false;
201205

206+
// Get the compositor and set the visual on it
202207
var compositor = ElementCompositionPreview.GetElementVisual(presenter).Compositor;
203208
outputLink = ContentExternalOutputLink.Create(compositor);
204209

205210
var target = outputLink.As<IDCompositionTarget>();
206-
target.SetRoot((nint)pChildVisual);
211+
target.SetRoot((nint)pChildVisual.Get());
207212

208213
outputLink.PlacementVisual.Size = new(0, 0);
209214
outputLink.PlacementVisual.Scale = new(1 / (float)presenter.XamlRoot.RasterizationScale);
210215
ElementCompositionPreview.SetElementChildVisual(presenter, outputLink.PlacementVisual);
211216

212-
pDCompositionDevice->Commit();
213-
214-
pControlSurface->Release();
215-
pDCompositionDevice->Release();
216-
pDXGIDevice->Release();
217-
pD3D11Device->Release();
218-
pD3D11DeviceContext->Release();
217+
// Commit the all pending DComp commands
218+
pDCompositionDevice.Get()->Commit();
219219

220220
var dwAttrib = Convert.ToUInt32(true);
221221

0 commit comments

Comments
 (0)