view VBoxFrontend/Framebuffer.cs @ 2:f1deea9c97a0

Cleaned up the interop code generator, added wrapper application to initialize kernel driver and run mono
author Ivo Smits
date Wed, 04 May 2011 16:10:08 +0200
parents e1ec7bf71313
children
line wrap: on
line source

???using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Threading;
using VirtualBox;

namespace ConsoleApplication1 {
	public class FramebufferUpdateEventArgs : EventArgs {
		public Rectangle Rectangle { get; private set; }
		public FramebufferUpdateEventArgs(Rectangle rect) {
			Rectangle = rect;
		}
	}
	[ClassInterface((Int16)0)]
	[TypeLibType(2)]
	[ComVisible(true)]
	public class Framebuffer : IFramebuffer {
		Mutex mutex = new Mutex();

		public Framebuffer() {
			byte b = 0;
			RequestResize(0, (int)FramebufferPixelFormat.FramebufferPixelFormat_FOURCC_RGB, ref b, 32, 0, 640, 480);
		}

		public void Lock() {
			//Console.WriteLine("Lock");
			mutex.WaitOne();
		}
		public void Unlock() {
			//Console.WriteLine("Unlock");
			mutex.ReleaseMutex();
		}

		public Bitmap Bitmap { get; private set; }
		public event EventHandler BufferChanged;
		public event EventHandler<FramebufferUpdateEventArgs> Update;

		public IntPtr Address { get; private set; }
		public uint BitsPerPixel { get; private set; }
		public uint BytesPerLine { get; private set; }
		public uint Height { get; private set; }
		public uint HeightReduction { get { return 0; } }
		public IFramebufferOverlay Overlay { get { return null; } }
		public uint PixelFormat { get; private set; }
		public int UsesGuestVRAM { get { return 0; } }
		public uint Width { get; private set; }
		public long WinId { get; internal set; }

		public unsafe void NotifyUpdate(uint aX, uint aY, uint aWidth, uint aHeight) {
			//Console.WriteLine("NotifyUpdate X={0} Y={1} Width={2} Height={3}", aX, aY, aWidth, aHeight);
			if (Update != null) Update(this, new FramebufferUpdateEventArgs(new Rectangle((int)aX, (int)aY, (int)aWidth, (int)aHeight)));
		}
		public void ProcessVHWACommand(ref byte aCommand) {
			Console.WriteLine("ProcessVHWACommand");
		}
		public unsafe int RequestResize(uint aScreenId, uint aPixelFormat, ref byte aVRAM, uint aBitsPerPixel, uint aBytesPerLine, uint aWidth, uint aHeight) {
			//Note that aVRAM is a pointer, which may be NULL!
			//fixed (Byte* vrp = &aVRAM) Console.WriteLine("RequestResize Current={0} ScreenId={1} PixelFormat={2} VRAM={3} BitsPerPixel={4} BytesPerLine={5} Width={6} Height={7}", Address, aScreenId, aPixelFormat, (IntPtr)vrp, aBitsPerPixel, aBytesPerLine, aWidth, aHeight);
			Console.WriteLine("RequestResize Current={0} ScreenId={1} PixelFormat={2} VRAM={3} BitsPerPixel={4} BytesPerLine={5} Width={6} Height={7}", Address, aScreenId, aPixelFormat, 0, aBitsPerPixel, aBytesPerLine, aWidth, aHeight);
			Lock();
			try {
				IntPtr oldAddress = Address;

				Width = aWidth;
				Height = aHeight;

				BitsPerPixel = 32;
				PixelFormat = (int)FramebufferPixelFormat.FramebufferPixelFormat_FOURCC_RGB;
				BytesPerLine = BitsPerPixel * Width / 8; //aBytesPerLine;
				Address = Marshal.AllocHGlobal((int)(BytesPerLine * Height));
				if (Bitmap != null) Bitmap.Dispose();
				Bitmap = new Bitmap((int)Width, (int)Height, (int)BytesPerLine, System.Drawing.Imaging.PixelFormat.Format32bppRgb, Address);
				if (BufferChanged != null) BufferChanged(this, new EventArgs());
				if (oldAddress != IntPtr.Zero) Marshal.FreeHGlobal(oldAddress);
				//Console.WriteLine("RequestResizeDone Current={0} ScreenId={1} PixelFormat={2} VRAM={3} BitsPerPixel={4} BytesPerLine={5} Width={6} Height={7}", Address, aScreenId, PixelFormat, 0, BitsPerPixel, BytesPerLine, Width, Height);
			} finally {
				Unlock();
			}
			return 1; //Finished
		}

		~Framebuffer() {
			if (Bitmap != null) Bitmap.Dispose();
			if (Address != IntPtr.Zero) Marshal.FreeHGlobal(Address);
		}

		public void SetVisibleRegion(ref byte aRectangles, uint aCount) {
			Console.WriteLine("SetVisibleRegion");
		}
		public uint GetVisibleRegion(ref byte aRectangles, uint aCount) {
			Console.WriteLine("GetVisibleRegion");
			return 0;
		}
		public int VideoModeSupported(uint aWidth, uint aHeight, uint aBpp) {
			Console.WriteLine("VideoModeSupported Width={0} Height={1} Bpp={2}", aWidth, aHeight, aBpp);
			return 1; //Supported
		}
	}
}