2013-11-14 12:23:35 +01:00
|
|
|
|
using System.Collections.Generic;
|
2013-11-14 12:06:50 +01:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using XenAdmin.Controls.Ballooning;
|
|
|
|
|
using XenAPI;
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Controls.GPU
|
|
|
|
|
{
|
|
|
|
|
public partial class GpuShinyBar : ShinyBar
|
|
|
|
|
{
|
|
|
|
|
public GpuShinyBar()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-14 12:28:43 +01:00
|
|
|
|
public PGPU PGPU { get; private set; }
|
2013-11-14 12:06:50 +01:00
|
|
|
|
|
|
|
|
|
private List<VGPU> vGPUs;
|
2013-11-14 12:23:35 +01:00
|
|
|
|
private Dictionary<VGPU, VM> vms;
|
2013-11-14 12:28:43 +01:00
|
|
|
|
private long capacity;
|
2013-11-14 12:06:50 +01:00
|
|
|
|
|
2013-11-14 12:23:35 +01:00
|
|
|
|
public void Initialize(PGPU pGPU)
|
2013-11-14 12:06:50 +01:00
|
|
|
|
{
|
2013-11-14 12:28:43 +01:00
|
|
|
|
this.PGPU = pGPU;
|
2013-11-14 12:06:50 +01:00
|
|
|
|
|
|
|
|
|
vGPUs = pGPU.Connection.ResolveAll(pGPU.resident_VGPUs);
|
|
|
|
|
|
|
|
|
|
vms = new Dictionary<VGPU, VM>();
|
|
|
|
|
foreach (VGPU vgpu in vGPUs)
|
|
|
|
|
vms[vgpu] = vgpu.Connection.Resolve(vgpu.VM);
|
2013-11-14 12:28:43 +01:00
|
|
|
|
|
|
|
|
|
capacity = vGPUs.Count > 0 ? pGPU.supported_VGPU_max_capacities[vGPUs[0].type] : 8;
|
2013-11-14 12:06:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnPaint(PaintEventArgs e)
|
|
|
|
|
{
|
2013-11-14 12:28:43 +01:00
|
|
|
|
if (PGPU == null || vGPUs == null)
|
2013-11-14 12:06:50 +01:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Graphics g = e.Graphics;
|
|
|
|
|
Rectangle barArea = barRect;
|
|
|
|
|
|
|
|
|
|
// Grid
|
2013-11-14 12:23:35 +01:00
|
|
|
|
DrawGrid(g, barArea, barArea.Width);
|
2013-11-14 12:06:50 +01:00
|
|
|
|
|
2013-11-14 12:23:35 +01:00
|
|
|
|
double left = barArea.Left;
|
2013-11-14 12:06:50 +01:00
|
|
|
|
|
|
|
|
|
// A bar for each vGPU
|
|
|
|
|
int i = 0;
|
|
|
|
|
vGPUs.Sort();
|
2013-11-14 12:28:43 +01:00
|
|
|
|
|
|
|
|
|
long segmentLength = barArea.Width / (capacity > 0 ? capacity : 8);
|
2013-11-14 12:06:50 +01:00
|
|
|
|
foreach (VGPU vgpu in vGPUs)
|
|
|
|
|
{
|
|
|
|
|
VM vm = vms[vgpu];
|
|
|
|
|
if (vm != null)
|
|
|
|
|
{
|
2013-11-14 12:28:43 +01:00
|
|
|
|
var vGpuType = PGPU.Connection.Resolve(vgpu.type);
|
2013-11-14 12:23:35 +01:00
|
|
|
|
|
2013-11-14 12:28:43 +01:00
|
|
|
|
DrawSegment(g, segmentLength, vm.Name, vGpuType != null ? vGpuType.model_name : "",
|
2013-11-14 12:23:35 +01:00
|
|
|
|
GpuShinyBarColors.GpuShinyBar_VMs[i++ % GpuShinyBarColors.GpuShinyBar_VMs.Length],
|
2013-11-14 12:06:50 +01:00
|
|
|
|
ref left);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// One final bar for free space
|
|
|
|
|
Rectangle rectFree = new Rectangle((int)left, barArea.Top, barArea.Right - (int)left, barArea.Height);
|
2013-11-14 12:23:35 +01:00
|
|
|
|
DrawToTarget(g, barArea, rectFree, GpuShinyBarColors.GpuShinyBar_Unused);
|
2013-11-14 12:06:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-14 12:23:35 +01:00
|
|
|
|
private void DrawGrid(Graphics g, Rectangle barArea, long max)
|
2013-11-14 12:06:50 +01:00
|
|
|
|
{
|
2013-11-14 12:23:35 +01:00
|
|
|
|
const int line_height = 12;
|
2013-11-14 12:06:50 +01:00
|
|
|
|
|
2013-11-14 12:23:35 +01:00
|
|
|
|
int line_bottom = barArea.Top + barArea.Height / 2;
|
|
|
|
|
int line_top = barArea.Top - line_height;
|
|
|
|
|
|
2013-11-14 12:28:43 +01:00
|
|
|
|
long incr = max / (capacity > 0 ? capacity : 8);
|
2013-11-14 12:23:35 +01:00
|
|
|
|
|
|
|
|
|
// Draw the grid
|
|
|
|
|
using (Pen pen = new Pen(GpuShinyBarColors.Grid))
|
2013-11-14 12:06:50 +01:00
|
|
|
|
{
|
2013-11-14 12:23:35 +01:00
|
|
|
|
for (long x = 0; x <= max; x += incr)
|
|
|
|
|
{
|
|
|
|
|
// Tick
|
|
|
|
|
int pos = barArea.Left + (int)((double)x);
|
|
|
|
|
g.DrawLine(pen, pos, line_top, pos, line_bottom);
|
|
|
|
|
}
|
2013-11-14 12:06:50 +01:00
|
|
|
|
}
|
2013-11-14 12:23:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DrawSegment(Graphics g, long width, string name, string description, Color color, ref double left)
|
|
|
|
|
{
|
|
|
|
|
if (width < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var rect = new Rectangle((int)left, barRect.Top,
|
|
|
|
|
(int)(left + width) - (int)left, // this is not necessarily the same as (int)width, which can leave a 1 pixel gap
|
|
|
|
|
barRect.Height);
|
|
|
|
|
var caption = name + "\n" + description;
|
|
|
|
|
DrawToTarget(g, barRect, rect, color, caption, GpuShinyBarColors.GpuShinyBar_Text, HorizontalAlignment.Center, caption);
|
2013-11-14 12:06:50 +01:00
|
|
|
|
left += width;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override int barHeight
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return 40;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-14 12:39:31 +01:00
|
|
|
|
|
|
|
|
|
protected override Rectangle barRect
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return new Rectangle(10, 20, this.Width - 25, barHeight);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-14 12:06:50 +01:00
|
|
|
|
}
|
2013-11-14 12:23:35 +01:00
|
|
|
|
|
|
|
|
|
public static class GpuShinyBarColors
|
|
|
|
|
{
|
|
|
|
|
public static Color[] GpuShinyBar_VMs = { Color.FromArgb(83, 39, 139), Color.FromArgb(157, 115, 215) };
|
|
|
|
|
public static Color GpuShinyBar_Unused = Color.Black;
|
|
|
|
|
public static Color GpuShinyBar_Text = Color.White;
|
|
|
|
|
|
|
|
|
|
public static Color Grid = Color.DarkGray;
|
|
|
|
|
}
|
2013-11-14 12:06:50 +01:00
|
|
|
|
}
|