From b0b0a2bd58a8c7c22b1c93f8b817189928921a40 Mon Sep 17 00:00:00 2001 From: 0x5BFA <62196528+0x5bfa@users.noreply.github.com> Date: Sat, 8 Mar 2025 23:24:02 +0900 Subject: [PATCH 1/2] Use ComPtr for all D3D11 pointers --- src/Files.App.CsWin32/Windows.Win32.ComPtr.cs | 8 +++ .../Files.App.Launcher.exe.sha256 | 2 +- .../Previews/ShellPreviewViewModel.cs | 67 ++++++++++--------- 3 files changed, 43 insertions(+), 34 deletions(-) diff --git a/src/Files.App.CsWin32/Windows.Win32.ComPtr.cs b/src/Files.App.CsWin32/Windows.Win32.ComPtr.cs index cbfeaa39e5d5..2d18d76fdf4b 100644 --- a/src/Files.App.CsWin32/Windows.Win32.ComPtr.cs +++ b/src/Files.App.CsWin32/Windows.Win32.ComPtr.cs @@ -27,6 +27,14 @@ public ComPtr(T* ptr) ((IUnknown*)ptr)->AddRef(); } + public void Attach(T* other) + { + if (_ptr is not null) + ((IUnknown*)_ptr)->Release(); + + _ptr = other; + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public readonly T* Get() { diff --git a/src/Files.App/Assets/FilesOpenDialog/Files.App.Launcher.exe.sha256 b/src/Files.App/Assets/FilesOpenDialog/Files.App.Launcher.exe.sha256 index 3b4d7012842d..512a89ce27e7 100644 --- a/src/Files.App/Assets/FilesOpenDialog/Files.App.Launcher.exe.sha256 +++ b/src/Files.App/Assets/FilesOpenDialog/Files.App.Launcher.exe.sha256 @@ -1 +1 @@ -83f9dfee3c40c31ab2319cc0cf94c0f754aadf483ffe4aa6de16b1413ddb83c8 +2c50ef2c68ccdf705767f11a94caf940e0339e4bd2261961b6ff807fd4c12e44 diff --git a/src/Files.App/ViewModels/UserControls/Previews/ShellPreviewViewModel.cs b/src/Files.App/ViewModels/UserControls/Previews/ShellPreviewViewModel.cs index d164d6729c82..18df906073c6 100644 --- a/src/Files.App/ViewModels/UserControls/Previews/ShellPreviewViewModel.cs +++ b/src/Files.App/ViewModels/UserControls/Previews/ShellPreviewViewModel.cs @@ -161,61 +161,62 @@ private unsafe bool ChildWindowToXaml(nint parent, UIElement presenter) D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_WARP, ]; - ID3D11Device* pD3D11Device = default; - ID3D11DeviceContext* pD3D11DeviceContext = default; - - foreach (var driveType in driverTypes) + Windows.Win32.Foundation.HRESULT hr = default; + using ComPtr pD3D11Device = default; + using ComPtr pD3D11DeviceContext = default; + using ComPtr pDXGIDevice = default; + using ComPtr pDCompositionDevice = default; + using ComPtr pControlSurface = default; + ComPtr pChildVisual = default; // Don't dispose this one, it's used by the compositor + + // Create the D3D11 device + foreach (var driverType in driverTypes) { - var hr = PInvoke.D3D11CreateDevice( - null, - driveType, - new(nint.Zero), + hr = PInvoke.D3D11CreateDevice( + null, driverType, new(nint.Zero), D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_BGRA_SUPPORT, - null, - 0, - 7, - &pD3D11Device, - null, - &pD3D11DeviceContext); + null, /* FeatureLevels */ 0, /* SDKVersion */ 7, + pD3D11Device.GetAddressOf(), null, + pD3D11DeviceContext.GetAddressOf()); if (hr.Succeeded) break; } - if (pD3D11Device is null) + if (pD3D11Device.IsNull) return false; - IDXGIDevice* pDXGIDevice = (IDXGIDevice*)pD3D11Device; - if (PInvoke.DCompositionCreateDevice(pDXGIDevice, typeof(IDCompositionDevice).GUID, out var compositionDevicePtr).Failed) - return false; + Guid IID_IDCompositionDevice = typeof(IDCompositionDevice).GUID; - var pDCompositionDevice = (IDCompositionDevice*)compositionDevicePtr; - IDCompositionVisual* pChildVisual = default; - IUnknown* pControlSurface = default; + // Create the DComp device + pDXGIDevice.Attach((IDXGIDevice*)pD3D11Device.Get()); + hr = PInvoke.DCompositionCreateDevice( + pDXGIDevice.Get(), + &IID_IDCompositionDevice, + (void**)pDCompositionDevice.GetAddressOf()); + if (hr.Failed) + return false; - pDCompositionDevice->CreateVisual(&pChildVisual); - pDCompositionDevice->CreateSurfaceFromHwnd(new(hwnd.DangerousGetHandle()), &pControlSurface); - pChildVisual->SetContent(pControlSurface); - if (pChildVisual is null || pControlSurface is null) + // Create the visual + hr = pDCompositionDevice.Get()->CreateVisual(pChildVisual.GetAddressOf()); + hr = pDCompositionDevice.Get()->CreateSurfaceFromHwnd(new(hwnd.DangerousGetHandle()), pControlSurface.GetAddressOf()); + hr = pChildVisual.Get()->SetContent(pControlSurface.Get()); + if (pChildVisual.IsNull || pControlSurface.IsNull) return false; + // Get the compositor and set the visual on it var compositor = ElementCompositionPreview.GetElementVisual(presenter).Compositor; outputLink = ContentExternalOutputLink.Create(compositor); var target = outputLink.As(); - target.SetRoot((nint)pChildVisual); + target.SetRoot((nint)pChildVisual.Get()); outputLink.PlacementVisual.Size = new(0, 0); outputLink.PlacementVisual.Scale = new(1 / (float)presenter.XamlRoot.RasterizationScale); ElementCompositionPreview.SetElementChildVisual(presenter, outputLink.PlacementVisual); - pDCompositionDevice->Commit(); - - pControlSurface->Release(); - pDCompositionDevice->Release(); - pDXGIDevice->Release(); - pD3D11Device->Release(); - pD3D11DeviceContext->Release(); + // Commit the all pending DComp commands + pDCompositionDevice.Get()->Commit(); var dwAttrib = Convert.ToUInt32(true); From 21943e505aab06b4f3fbf4a2167dfd85ba33ac7a Mon Sep 17 00:00:00 2001 From: 0x5BFA <62196528+0x5bfa@users.noreply.github.com> Date: Thu, 13 Mar 2025 23:02:40 +0900 Subject: [PATCH 2/2] Revert --- .../Assets/FilesOpenDialog/Files.App.Launcher.exe.sha256 | 2 +- .../ViewModels/UserControls/Previews/ShellPreviewViewModel.cs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Files.App/Assets/FilesOpenDialog/Files.App.Launcher.exe.sha256 b/src/Files.App/Assets/FilesOpenDialog/Files.App.Launcher.exe.sha256 index 512a89ce27e7..3b4d7012842d 100644 --- a/src/Files.App/Assets/FilesOpenDialog/Files.App.Launcher.exe.sha256 +++ b/src/Files.App/Assets/FilesOpenDialog/Files.App.Launcher.exe.sha256 @@ -1 +1 @@ -2c50ef2c68ccdf705767f11a94caf940e0339e4bd2261961b6ff807fd4c12e44 +83f9dfee3c40c31ab2319cc0cf94c0f754aadf483ffe4aa6de16b1413ddb83c8 diff --git a/src/Files.App/ViewModels/UserControls/Previews/ShellPreviewViewModel.cs b/src/Files.App/ViewModels/UserControls/Previews/ShellPreviewViewModel.cs index 18df906073c6..16e2d1de4d3b 100644 --- a/src/Files.App/ViewModels/UserControls/Previews/ShellPreviewViewModel.cs +++ b/src/Files.App/ViewModels/UserControls/Previews/ShellPreviewViewModel.cs @@ -162,6 +162,7 @@ private unsafe bool ChildWindowToXaml(nint parent, UIElement presenter) ]; Windows.Win32.Foundation.HRESULT hr = default; + Guid IID_IDCompositionDevice = typeof(IDCompositionDevice).GUID; using ComPtr pD3D11Device = default; using ComPtr pD3D11DeviceContext = default; using ComPtr pDXGIDevice = default; @@ -186,8 +187,6 @@ private unsafe bool ChildWindowToXaml(nint parent, UIElement presenter) if (pD3D11Device.IsNull) return false; - Guid IID_IDCompositionDevice = typeof(IDCompositionDevice).GUID; - // Create the DComp device pDXGIDevice.Attach((IDXGIDevice*)pD3D11Device.Get()); hr = PInvoke.DCompositionCreateDevice(