Cosmos/source/Cosmos.System2/Graphics/FullScreenCanvas.cs
2020-07-04 14:28:56 +01:00

143 lines
4.5 KiB
C#

//#define COSMOSDEBUG
using Cosmos.HAL;
using Cosmos.HAL.Drivers;
namespace Cosmos.System.Graphics
{
/// <summary>
/// FullScreenCanvas class. Used to set and get full screen canvas.
/// </summary>
public static class FullScreenCanvas
{
/// <summary>
/// Boolean value whether CGS is in use or not
/// </summary>
public static bool IsInUse = false;
/// <summary>
/// Disables the specified Graphics Driver used and returns to VGA text mode 80x25
/// </summary>
public static void Disable()
{
if (IsInUse)
{
_VideoDriver.Disable();
VGAScreen.SetTextMode(VGADriver.TextSize.Size80x25);
}
}
/// <summary>
/// List of all video drivers (BGA,
/// </summary>
private enum VideoDriver
{
VMWareSVGAIIDriver,
VBEDriver,
VGADriver
}
/// <summary>
/// SVGA 2 device.
/// </summary>
private static PCIDevice _SVGAIIDevice = PCI.GetDevice(VendorID.VMWare, DeviceID.SVGAIIAdapter);
/// <summary>
/// Checks whether the Bochs Graphics Adapter exists (not limited to Bochs)
/// </summary>
/// <returns></returns>
public static bool BGAExists()
{
return VBEDriver.Available();
}
/// <summary>
/// Video driver.
/// </summary>
private static Canvas _VideoDriver = null;
/// <summary>
/// Get video driver.
/// </summary>
/// <returns>Canvas value.</returns>
/// <exception cref="sys.ArgumentOutOfRangeException">Thrown if default graphics mode is not suppoted.</exception>
private static Canvas GetVideoDriver()
{
if (_SVGAIIDevice != null && PCI.Exists(_SVGAIIDevice))
{
return new SVGAIICanvas();
}
else if (BGAExists() || PCI.GetDevice((VendorID)0x80EE, (DeviceID)0xBEEF) != null)
{
return new VBECanvas();
}
else
{
return new VGACanvas();
}
}
/// <summary>
/// Get video driver.
/// </summary>
/// <param name="mode">Mode.</param>
/// <returns>Canvas value.</returns>
/// <exception cref="sys.ArgumentOutOfRangeException">Thrown if graphics mode is not suppoted.</exception>
private static Canvas GetVideoDriver(Mode mode)
{
if (_SVGAIIDevice != null && PCI.Exists(_SVGAIIDevice))
{
return new SVGAIICanvas(mode);
}
else if (BGAExists() || PCI.GetDevice((VendorID)0x80EE, (DeviceID)0xBEEF) != null)
{
return new VBECanvas(mode);
}
else
{
return new VGACanvas(mode);
}
}
/// <summary>
/// Get full screen canvas.
/// </summary>
/// <returns>Canvas value.</returns>
/// <exception cref="sys.ArgumentOutOfRangeException">Thrown if default graphics mode is not suppoted.</exception>
public static Canvas GetFullScreenCanvas()
{
Global.mDebugger.SendInternal($"GetFullScreenCanvas() with default mode");
if (_VideoDriver == null)
{
Global.mDebugger.SendInternal($"_VideoDriver is null creating new object");
_VideoDriver = GetVideoDriver();
}
else
{
Global.mDebugger.SendInternal($"_VideoDriver is NOT null using the old one changing mode to DefaultMode");
_VideoDriver.Mode = _VideoDriver.DefaultGraphicMode;
}
return _VideoDriver;
}
/// <summary>
/// Get full screen canvas.
/// </summary>
/// <param name="mode">Mode.</param>
/// <returns>Canvas value.</returns>
/// <exception cref="sys.ArgumentOutOfRangeException">Thrown if graphics mode is not suppoted.</exception>
public static Canvas GetFullScreenCanvas(Mode mode)
{
Global.mDebugger.SendInternal($"GetFullScreenCanvas() with mode" + mode);
if (_VideoDriver == null)
{
_VideoDriver = GetVideoDriver(mode);
}
else
{
_VideoDriver.Mode = mode;
}
return _VideoDriver;
}
}
}