@@ -161,61 +161,61 @@ private unsafe bool ChildWindowToXaml(nint parent, UIElement presenter)
161
161
D3D_DRIVER_TYPE . D3D_DRIVER_TYPE_WARP ,
162
162
] ;
163
163
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 )
168
175
{
169
- var hr = PInvoke . D3D11CreateDevice (
170
- null ,
171
- driveType ,
172
- new ( nint . Zero ) ,
176
+ hr = PInvoke . D3D11CreateDevice (
177
+ null , driverType , new ( nint . Zero ) ,
173
178
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 ( ) ) ;
180
182
181
183
if ( hr . Succeeded )
182
184
break ;
183
185
}
184
186
185
- if ( pD3D11Device is null )
187
+ if ( pD3D11Device . IsNull )
186
188
return false ;
187
189
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 )
190
197
return false ;
191
198
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 )
200
204
return false ;
201
205
206
+ // Get the compositor and set the visual on it
202
207
var compositor = ElementCompositionPreview . GetElementVisual ( presenter ) . Compositor ;
203
208
outputLink = ContentExternalOutputLink . Create ( compositor ) ;
204
209
205
210
var target = outputLink . As < IDCompositionTarget > ( ) ;
206
- target . SetRoot ( ( nint ) pChildVisual ) ;
211
+ target . SetRoot ( ( nint ) pChildVisual . Get ( ) ) ;
207
212
208
213
outputLink . PlacementVisual . Size = new ( 0 , 0 ) ;
209
214
outputLink . PlacementVisual . Scale = new ( 1 / ( float ) presenter . XamlRoot . RasterizationScale ) ;
210
215
ElementCompositionPreview . SetElementChildVisual ( presenter , outputLink . PlacementVisual ) ;
211
216
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 ( ) ;
219
219
220
220
var dwAttrib = Convert . ToUInt32 ( true ) ;
221
221
0 commit comments