Repository: files-community/Files
File: src/Files.App/ViewModels/UserControls/Previews/ShellPreviewViewModel.cs (C#)

Code before:
				wCls.ClassName,
				"Preview",
				WindowStyles.WS_CHILD | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_VISIBLE,
				0, 0, 0, 0,
				hWndParent: parent,
				hInstance: hInst);

			_ = ChildWindowToXaml(parent, presenter);
		}

		private unsafe bool ChildWindowToXaml(nint parent, UIElement presenter)
		{
			D3D_DRIVER_TYPE[] driverTypes =
			[
				D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_HARDWARE,
				D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_WARP,
			];

			ID3D11Device* pD3D11Device = default;
			ID3D11DeviceContext* pD3D11DeviceContext = default;

			foreach (var driveType in driverTypes)
			{
				var hr = PInvoke.D3D11CreateDevice(
					null,
					driveType,
					new(nint.Zero),
					D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_BGRA_SUPPORT,
					null,
					0,
					7,
					&pD3D11Device,
					null,
					&pD3D11DeviceContext);

				if (hr.Succeeded)
					break;
			}

			if (pD3D11Device is null)
				return false;

			IDXGIDevice* pDXGIDevice = (IDXGIDevice*)pD3D11Device;
			if (PInvoke.DCompositionCreateDevice(pDXGIDevice, typeof(IDCompositionDevice).GUID, out var compositionDevicePtr).Failed)
				return false;

			var pDCompositionDevice = (IDCompositionDevice*)compositionDevicePtr;
			IDCompositionVisual* pChildVisual = default;
			IUnknown* pControlSurface = default;

			pDCompositionDevice->CreateVisual(&pChildVisual);

Review comment (performance):
It seems that we never release it and would cause memory leaking. I think we should do something around this to free it eventually when user switched or closed the preview pane.

Code after:
				wCls.ClassName,
				"Preview",
				WindowStyles.WS_CHILD | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_VISIBLE,
				0, 0, 0, 0,
				hWndParent: parent,
				hInstance: hInst);

			_ = ChildWindowToXaml(parent, presenter);
		}

		private unsafe bool ChildWindowToXaml(nint parent, UIElement presenter)
		{
			D3D_DRIVER_TYPE[] driverTypes =
			[
				D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_HARDWARE,
				D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_WARP,
			];

			Windows.Win32.Foundation.HRESULT hr = default;
			Guid IID_IDCompositionDevice = typeof(IDCompositionDevice).GUID;
			using ComPtr<ID3D11Device> pD3D11Device = default;
			using ComPtr<ID3D11DeviceContext> pD3D11DeviceContext = default;
			using ComPtr<IDXGIDevice> pDXGIDevice = default;
			using ComPtr<IDCompositionDevice> pDCompositionDevice = default;
			using ComPtr<IUnknown> pControlSurface = default;
			ComPtr<IDCompositionVisual> pChildVisual = default; // Don't dispose this one, it's used by the compositor

			// Create the D3D11 device
			foreach (var driverType in driverTypes)
			{
				hr = PInvoke.D3D11CreateDevice(
					null, driverType, new(nint.Zero),
					D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_BGRA_SUPPORT,
					null, /* FeatureLevels */ 0, /* SDKVersion */ 7,
					pD3D11Device.GetAddressOf(), null,
					pD3D11DeviceContext.GetAddressOf());

				if (hr.Succeeded)
					break;
			}

			if (pD3D11Device.IsNull)
				return false;

			// Create the DComp device
			pDXGIDevice.Attach((IDXGIDevice*)pD3D11Device.Get());
			hr = PInvoke.DCompositionCreateDevice(
				pDXGIDevice.Get(),
				&IID_IDCompositionDevice,
				(void**)pDCompositionDevice.GetAddressOf());
			if (hr.Failed)