145 lines
4.7 KiB
C#
145 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using Aiwaz.Contracts;
|
|
using SlimDX;
|
|
|
|
namespace Aiwaz.Core
|
|
{
|
|
public class DeviceEnumerator : IDisposable
|
|
{
|
|
private SlimDX.DXGI.Factory Factory;
|
|
internal SlimDX.Direct3D10.Device Device;
|
|
|
|
public bool HasEnumerated
|
|
{
|
|
get { return EnumeratedAdapters.Count > 0; }
|
|
}
|
|
|
|
public List<DisplayAdapter> EnumeratedAdapters { get; private set;}
|
|
|
|
public uint DesiredWidth = 0;
|
|
public uint DesiredHeight = 0;
|
|
public uint DesiredRefreshRate = 0;
|
|
public SlimDX.DXGI.Format DesiredFormat = SlimDX.DXGI.Format.Unknown;
|
|
|
|
public DeviceEnumerator(SlimDX.Direct3D10.Device argDevice)
|
|
{
|
|
Factory = new SlimDX.DXGI.Factory();
|
|
Device = argDevice;
|
|
EnumeratedAdapters = new List<DisplayAdapter>();
|
|
}
|
|
|
|
~DeviceEnumerator()
|
|
{
|
|
this.Dispose();
|
|
}
|
|
|
|
public bool TryEnumerate()
|
|
{
|
|
for (var i = 0; i < Factory.GetAdapterCount(); ++i)
|
|
{
|
|
SlimDX.DXGI.Adapter adapter = Factory.GetAdapter(i);
|
|
if (adapter.IsInterfaceSupported(typeof(SlimDX.Direct3D10.Device)))
|
|
{
|
|
DisplayAdapter displayAdapter = new DisplayAdapter(this, adapter);
|
|
if (displayAdapter.TryEnumerate())
|
|
EnumeratedAdapters.Add(displayAdapter);
|
|
else
|
|
adapter.Dispose();
|
|
}
|
|
else
|
|
adapter.Dispose();
|
|
}
|
|
|
|
return HasEnumerated;
|
|
}
|
|
|
|
public DisplayAdapter FindBestAdapter(bool argAllowToEnumerate)
|
|
{
|
|
if (argAllowToEnumerate && !this.TryEnumerate())
|
|
return null;
|
|
|
|
if (!HasEnumerated)
|
|
return null;
|
|
|
|
return EnumeratedAdapters.Last();
|
|
}
|
|
|
|
public OutputDevice FindBestOutput(bool argAllowToEnumerate)
|
|
{
|
|
if (argAllowToEnumerate && !this.TryEnumerate())
|
|
return null;
|
|
|
|
if (!HasEnumerated)
|
|
return null;
|
|
|
|
return EnumeratedAdapters.Last().FindBestOutput(argAllowToEnumerate);
|
|
}
|
|
|
|
public DisplayMode FindBestDisplayMode(bool argAllowToEnumerate)
|
|
{
|
|
if (argAllowToEnumerate && !this.TryEnumerate())
|
|
return null;
|
|
|
|
if (!HasEnumerated)
|
|
return null;
|
|
|
|
return EnumeratedAdapters.Last().FindBestDisplayMode(argAllowToEnumerate);
|
|
}
|
|
|
|
public virtual void Verbose()
|
|
{
|
|
if (!HasEnumerated)
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
Console.WriteLine("Enumeration was not executed or no results available.");
|
|
return;
|
|
}
|
|
|
|
DisplayAdapter displayAdapter = this.FindBestAdapter(false);
|
|
|
|
Console.ForegroundColor = ConsoleColor.White;
|
|
Console.WriteLine("Adapter: " + displayAdapter.Name);
|
|
Console.WriteLine("Dedicated System Memory: " + Math.Round(displayAdapter.DedicatedSystemMemory / 1024.0 / 1024.0, 0, MidpointRounding.AwayFromZero).ToString() + "MB");
|
|
Console.WriteLine("Dedicated Video Memory: " + Math.Round(displayAdapter.DedicatedVideoMemory / 1024.0 / 1024.0, 0, MidpointRounding.AwayFromZero).ToString() + "MB");
|
|
Console.WriteLine("Shared System Memory: " + Math.Round(displayAdapter.SharedSystemMemory / 1024.0 / 1024.0, 0, MidpointRounding.AwayFromZero).ToString() + "MB");
|
|
Console.WriteLine();
|
|
|
|
for (int i = 0; i < displayAdapter.OutputDevices.Count; ++i)
|
|
{
|
|
OutputDevice outputDevice = displayAdapter.OutputDevices[i];
|
|
if (i == 0)
|
|
Console.Write("Primary Output: ");
|
|
else
|
|
Console.Write("Secondary Output: ");
|
|
|
|
Console.WriteLine(outputDevice.Name);
|
|
|
|
DisplayMode displayMode = outputDevice.FindBestDisplayMode(false);
|
|
uint bpp = displayMode.Format.GetByteSize() * 8;
|
|
|
|
Console.WriteLine("Display: " + displayMode.Width.ToString() + "x" + displayMode.Height.ToString() + "x" + bpp.ToString() + " @" + displayMode.RefreshRate + "Hz");
|
|
Console.WriteLine();
|
|
}
|
|
}
|
|
|
|
#region IDisposable Members
|
|
|
|
public void Dispose()
|
|
{
|
|
if (Factory != null)
|
|
Factory.Dispose();
|
|
Factory = null;
|
|
|
|
if (EnumeratedAdapters != null)
|
|
foreach (var adapter in EnumeratedAdapters)
|
|
adapter.Dispose();
|
|
EnumeratedAdapters = null;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|