예제 #1
0
        private static GraphicsAdapter CreateAdapter(SharpDX.DXGI.Adapter1 device, SharpDX.DXGI.Output monitor)
        {
            var adapter = new GraphicsAdapter();

            adapter._adapter = device;

            adapter.DeviceName    = monitor.Description.DeviceName.TrimEnd(new char[] { '\0' });
            adapter.Description   = device.Description1.Description.TrimEnd(new char[] { '\0' });
            adapter.DeviceId      = device.Description1.DeviceId;
            adapter.Revision      = device.Description1.Revision;
            adapter.VendorId      = device.Description1.VendorId;
            adapter.SubSystemId   = device.Description1.SubsystemId;
            adapter.MonitorHandle = monitor.Description.MonitorHandle;

            var desktopWidth  = monitor.Description.DesktopBounds.Right - monitor.Description.DesktopBounds.Left;
            var desktopHeight = monitor.Description.DesktopBounds.Bottom - monitor.Description.DesktopBounds.Top;

            var modes = new List <DisplayMode>();

            foreach (var formatTranslation in FormatTranslations)
            {
                SharpDX.DXGI.ModeDescription[] displayModes;

                // This can fail on headless machines, so just assume the desktop size
                // is a valid mode and return that... so at least our unit tests work.
                try
                {
                    displayModes = monitor.GetDisplayModeList(formatTranslation.Key, 0);
                }
                catch (SharpDX.SharpDXException)
                {
                    var mode = new DisplayMode(desktopWidth, desktopHeight, SurfaceFormat.Color);
                    modes.Add(mode);
                    adapter._currentDisplayMode = mode;
                    break;
                }


                foreach (var displayMode in displayModes)
                {
                    var mode = new DisplayMode(displayMode.Width, displayMode.Height, formatTranslation.Value);

                    // Skip duplicate modes with the same width/height/formats.
                    if (modes.Contains(mode))
                    {
                        continue;
                    }

                    modes.Add(mode);

                    if (adapter._currentDisplayMode == null)
                    {
                        if (mode.Width == desktopWidth && mode.Height == desktopHeight && mode.Format == SurfaceFormat.Color)
                        {
                            adapter._currentDisplayMode = mode;
                        }
                    }
                }
            }

            adapter._supportedDisplayModes = new DisplayModeCollection(modes);

            if (adapter._currentDisplayMode == null) //(i.e. desktop mode wasn't found in the available modes)
            {
                adapter._currentDisplayMode = new DisplayMode(desktopWidth, desktopHeight, SurfaceFormat.Color);
            }

            return(adapter);
        }
예제 #2
0
        private void InitDx()
        {
            logger.Debug("GDICapture::InitDx()");

            SharpDX.DXGI.Factory1 dxgiFactory = null;
            SharpDX.DXGI.Adapter1 adapter     = null;
            SharpDX.DXGI.Output   output      = null;
            try
            {
                dxgiFactory = new SharpDX.DXGI.Factory1();

                logger.Info(MediaFoundation.DxTool.LogDxAdapters(dxgiFactory.Adapters1));

                //var hMonitor = NativeAPIs.User32.GetMonitorFromRect(this.srcRect);
                //if (hMonitor != IntPtr.Zero)
                //{
                //    foreach (var _adapter in dxgiFactory.Adapters1)
                //    {
                //        foreach (var _output in _adapter.Outputs)
                //        {
                //            var descr = _output.Description;
                //            if (descr.MonitorHandle == hMonitor)
                //            {
                //                adapter = _adapter;
                //                output = _output;

                //                break;
                //            }
                //        }
                //    }
                //}

                if (adapter == null)
                {// первым идет адаптер с которому подключен primary монитор
                    adapter = dxgiFactory.GetAdapter1(0);
                }

                AdapterId = adapter.Description.Luid;

                //logger.Info("Screen source info: " + adapter.Description.Description + " " + output.Description.DeviceName);

                var deviceCreationFlags =
                    //DeviceCreationFlags.Debug |
                    // DeviceCreationFlags.VideoSupport |
                    DeviceCreationFlags.BgraSupport;

                //var feature = FeatureLevel.Level_11_0;

                device = new Device(adapter, deviceCreationFlags);
                using (var multiThread = device.QueryInterface <SharpDX.Direct3D11.Multithread>())
                {
                    multiThread.SetMultithreadProtected(true);
                }

                SharedTexture = new Texture2D(device,
                                              new Texture2DDescription
                {
                    CpuAccessFlags = CpuAccessFlags.None,
                    BindFlags      = BindFlags.RenderTarget | BindFlags.ShaderResource,
                    Format         = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                    Width          = DestSize.Width,
                    Height         = DestSize.Height,

                    MipLevels         = 1,
                    ArraySize         = 1,
                    SampleDescription = { Count = 1, Quality = 0 },
                    Usage             = ResourceUsage.Default,
                    OptionFlags       = ResourceOptionFlags.Shared,
                });

                gdiTexture = new Texture2D(device,
                                           new Texture2DDescription
                {
                    CpuAccessFlags = CpuAccessFlags.None,
                    BindFlags      = BindFlags.RenderTarget | BindFlags.ShaderResource,
                    Format         = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                    Width          = SrcRect.Width,  //DestSize.Width,
                    Height         = SrcRect.Height, //DestSize.Height,

                    MipLevels         = 1,
                    ArraySize         = 1,
                    SampleDescription = { Count = 1, Quality = 0 },
                    Usage             = ResourceUsage.Default,
                    OptionFlags       = ResourceOptionFlags.GdiCompatible,
                });


                renderTexture = new Texture2D(device,
                                              new Texture2DDescription
                {
                    CpuAccessFlags = CpuAccessFlags.None,
                    BindFlags      = BindFlags.RenderTarget | BindFlags.ShaderResource,
                    Format         = SharpDX.DXGI.Format.B8G8R8A8_UNorm,

                    Width             = DestSize.Width,
                    Height            = DestSize.Height,
                    MipLevels         = 1,
                    ArraySize         = 1,
                    SampleDescription = { Count = 1, Quality = 0 },
                    Usage             = ResourceUsage.Default,
                    //OptionFlags = ResourceOptionFlags.GdiCompatible//ResourceOptionFlags.None,
                    OptionFlags = ResourceOptionFlags.Shared,
                });

                if (DestSize.Width != SrcRect.Width || DestSize.Height != SrcRect.Height)
                {
                    using (SharpDX.Direct2D1.Factory1 factory2D1 = new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.MultiThreaded))
                    {
                        using (var surf = renderTexture.QueryInterface <SharpDX.DXGI.Surface>())
                        {
                            //var pixelFormat = new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Ignore);

                            var pixelFormat       = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied);
                            var renderTargetProps = new SharpDX.Direct2D1.RenderTargetProperties(pixelFormat);

                            renderTarget = new SharpDX.Direct2D1.RenderTarget(factory2D1, surf, renderTargetProps);

                            //var d2dContext = new SharpDX.Direct2D1.DeviceContext(surface);
                        }
                    }
                }
            }
            finally
            {
                if (adapter != null)
                {
                    adapter.Dispose();
                    adapter = null;
                }

                if (output != null)
                {
                    output.Dispose();
                    output = null;
                }

                if (dxgiFactory != null)
                {
                    dxgiFactory.Dispose();
                    dxgiFactory = null;
                }
            }
        }