Repository: AvaloniaUI/Avalonia
File: src/Windows/Avalonia.Win32/DirectX/DxgiRenderTarget.cs (C#)

Code before:
using Avalonia.OpenGL.Surfaces;
using Avalonia.Win32.OpenGl.Angle;
using MicroCom.Runtime;
using static Avalonia.Win32.Interop.UnmanagedMethods;

namespace Avalonia.Win32.DirectX
{
    internal unsafe class DxgiRenderTarget : EglPlatformSurfaceRenderTargetBase
    {
        // DXGI_FORMAT_B8G8R8A8_UNORM is target texture format as per ANGLE documentation 

        public const uint DXGI_USAGE_RENDER_TARGET_OUTPUT = 0x00000020U;
        private readonly Guid ID3D11Texture2DGuid = Guid.Parse("6F15AAF2-D208-4E89-9AB4-489535D34F9C");

        private readonly EglGlPlatformSurface.IEglWindowGlPlatformSurfaceInfo _window;
        private readonly DxgiConnection _connection;
        private readonly IDXGIDevice? _dxgiDevice;
        private readonly IDXGIFactory2? _dxgiFactory;
        private readonly IDXGISwapChain1? _swapChain;
        private readonly uint _flagsUsed;

        private IUnknown? _renderTexture;
        private RECT _clientRect;

        [Flags]
        enum AssociationFlags : ushort
        {
            DXGI_MWA_NO_WINDOW_CHANGES = 1,
            DXGI_MWA_NO_ALT_ENTER = 2,
            DXGI_MWA_NO_PRINT_SCREEN = 4
        }

        public DxgiRenderTarget(EglGlPlatformSurface.IEglWindowGlPlatformSurfaceInfo window, EglContext context, DxgiConnection connection) : base(context)
        {
            _window = window;
            _connection = connection;

            // the D3D device is expected to at least be an ID3D11Device 
            // but how do I wrap an IntPtr as a managed IUnknown now? Like this. 
            IUnknown pdevice = MicroComRuntime.CreateProxyFor<IUnknown>(((AngleWin32EglDisplay)context.Display).GetDirect3DDevice(), false);

            _dxgiDevice = pdevice.QueryInterface<IDXGIDevice>();

            // only needing the adapter pointer to ask it for the IDXGI Factory 
            using (var adapterPointer = _dxgiDevice.Adapter)
            {
                Guid factoryGuid = MicroComRuntime.GetGuidFor(typeof(IDXGIFactory2));
                _dxgiFactory = MicroComRuntime.CreateProxyFor<IDXGIFactory2>(adapterPointer.GetParent(&factoryGuid), true);
            }

            DXGI_SWAP_CHAIN_DESC1 dxgiSwapChainDesc = new DXGI_SWAP_CHAIN_DESC1();

Review comment (suggestion):
Please define this in `DirectXEnums.cs` for consistency (the name could be `DXGI_MWA`).
Also, the second parameter for `MakeWindowAssociation()` in `DirectX.idl` could probably now be typed.

Code after:
using Avalonia.OpenGL.Surfaces;
using Avalonia.Win32.OpenGl.Angle;
using MicroCom.Runtime;
using static Avalonia.Win32.Interop.UnmanagedMethods;

namespace Avalonia.Win32.DirectX
{
    internal unsafe class DxgiRenderTarget : EglPlatformSurfaceRenderTargetBase
    {
        // DXGI_FORMAT_B8G8R8A8_UNORM is target texture format as per ANGLE documentation 

        public const uint DXGI_USAGE_RENDER_TARGET_OUTPUT = 0x00000020U;
        private readonly Guid ID3D11Texture2DGuid = Guid.Parse("6F15AAF2-D208-4E89-9AB4-489535D34F9C");

        private readonly EglGlPlatformSurface.IEglWindowGlPlatformSurfaceInfo _window;
        private readonly DxgiConnection _connection;
        private readonly IDXGIDevice? _dxgiDevice;
        private readonly IDXGIFactory2? _dxgiFactory;
        private readonly IDXGISwapChain1? _swapChain;
        private readonly uint _flagsUsed;

        private IUnknown? _renderTexture;
        private RECT _clientRect;
        
        public DxgiRenderTarget(EglGlPlatformSurface.IEglWindowGlPlatformSurfaceInfo window, EglContext context, DxgiConnection connection) : base(context)
        {
            _window = window;
            _connection = connection;

            // the D3D device is expected to at least be an ID3D11Device 
            // but how do I wrap an IntPtr as a managed IUnknown now? Like this. 
            IUnknown pdevice = MicroComRuntime.CreateProxyFor<IUnknown>(((AngleWin32EglDisplay)context.Display).GetDirect3DDevice(), false);

            _dxgiDevice = pdevice.QueryInterface<IDXGIDevice>();

            // only needing the adapter pointer to ask it for the IDXGI Factory 
            using (var adapterPointer = _dxgiDevice.Adapter)
            {
                Guid factoryGuid = MicroComRuntime.GetGuidFor(typeof(IDXGIFactory2));
                _dxgiFactory = MicroComRuntime.CreateProxyFor<IDXGIFactory2>(adapterPointer.GetParent(&factoryGuid), true);
            }

            DXGI_SWAP_CHAIN_DESC1 dxgiSwapChainDesc = new DXGI_SWAP_CHAIN_DESC1();

            // standard swap chain really. 
            dxgiSwapChainDesc.Format = DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM;
            dxgiSwapChainDesc.SampleDesc.Count = 1U;
            dxgiSwapChainDesc.SampleDesc.Quality = 0U;
            dxgiSwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
            dxgiSwapChainDesc.AlphaMode = DXGI_ALPHA_MODE.DXGI_ALPHA_MODE_IGNORE;
            dxgiSwapChainDesc.Width = (uint)_window.Size.Width;