Exemplo n.º 1
0
        public static DXGI_MODE_DESC1?FindClosestMatchingMode1(this IDXGIOutput1 output, DXGI_MODE_DESC1 modeToMatch, object concernedDevice)
        {
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            if (output.FindClosestMatchingMode1(ref modeToMatch, out var closest, ComObject.Unwrap(concernedDevice)).IsError)
            {
                return(null);
            }

            return(closest);
        }
Exemplo n.º 2
0
        public static ComObject <IDXGIOutputDuplication> DuplicateOutput(this IDXGIOutput1 output, object device, bool throwOnError = true)
        {
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            device = ComObject.Unwrap(device);

            // this wonderful magic (mess) is because of a horrible bug in DXGI
            // https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/0c28328a-100a-4d6f-9a08-44a452d3ca6a/idxgioutputduplication-implementation-bug?forum=windowssdk#0c28328a-100a-4d6f-9a08-44a452d3ca6a
            output.DuplicateOutput(device, out var unk).ThrowOnError(throwOnError);
            if (unk == IntPtr.Zero)
            {
                return(null);
            }

            // we copy the original COM's vtable to our process
            var vtablePtr = Marshal.ReadIntPtr(unk);
            var vtable    = new IDXGIOutputDuplicationVTable();

            Marshal.PtrToStructure(vtablePtr, vtable);

            // patch QueryInterface so it works (route to our code)
            var wrapper = new Wrapper(unk);

            vtable.QueryInterfacePtr = Marshal.GetFunctionPointerForDelegate <QueryInterfaceFn>(wrapper.QueryInterface);
            using (var newVtablePtr = new ComMemory(Marshal.SizeOf <IDXGIOutputDuplicationVTable>()))
            {
                Marshal.StructureToPtr(vtable, newVtablePtr.Pointer, false);

                // create a new vtable for the CLR
                using (var newUnk = new ComMemory(IntPtr.Size))
                {
                    Marshal.WriteIntPtr(newUnk.Pointer, newVtablePtr.Pointer);

                    var dup = (IDXGIOutputDuplication)Marshal.GetObjectForIUnknown(newUnk.Pointer);
                    return(new ComObject <IDXGIOutputDuplication>(dup));
                }
            }
        }
Exemplo n.º 3
0
        public static IComObject <T> CreateSwapChainForComposition <T>(this IDXGIFactory2 factory,
                                                                       IDXGIDevice1 device,
                                                                       DXGI_SWAP_CHAIN_DESC1 desc,
                                                                       IDXGIOutput1 restrictToOutput = null) where T : IDXGISwapChain1
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            factory.CreateSwapChainForComposition(device, ref desc, restrictToOutput, out var swapChain).ThrowOnError();
            return(new ComObject <T>((T)swapChain));
        }
Exemplo n.º 4
0
        public static IReadOnlyList <DXGI_MODE_DESC1> GetDisplayModeList1(this IDXGIOutput1 output, DXGI_FORMAT format, DXGI_ENUM_MODES modes)
        {
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            var  list = new List <DXGI_MODE_DESC1>();
            uint num  = 0;

            output.GetDisplayModeList(format, (uint)modes, ref num, null);
            if (num > 0)
            {
                var array = new DXGI_MODE_DESC1[num];
                output.GetDisplayModeList1(format, (uint)modes, ref num, array);
                list.AddRange(array);
            }
            return(list);
        }
Exemplo n.º 5
0
        public static ComObject <T> CreateSwapChainForHwnd <T>(this IDXGIFactory2 factory,
                                                               IDXGIDevice1 device,
                                                               IntPtr hwnd,
                                                               DXGI_SWAP_CHAIN_DESC1 desc,
                                                               DXGI_SWAP_CHAIN_FULLSCREEN_DESC?fullScreenDesc = null,
                                                               IDXGIOutput1 restrictToOutput = null) where T : IDXGISwapChain1
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            using (var mem = fullScreenDesc.StructureToMemory())
            {
                factory.CreateSwapChainForHwnd(device, hwnd, ref desc, mem.Pointer, restrictToOutput, out var swapChain).ThrowOnError();
                return(new ComObject <T>((T)swapChain));
            }
        }