comparison VBoxFrontend/Framebuffer.cs @ 0:e1ec7bf71313

Initial commit
author Ivo Smits
date Wed, 04 May 2011 00:59:43 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e1ec7bf71313
1 using System;
2 using System.Drawing;
3 using System.Drawing.Imaging;
4 using System.Runtime.InteropServices;
5 using System.Runtime.CompilerServices;
6 using System.Threading;
7 using VirtualBox;
8
9 namespace ConsoleApplication1 {
10 public class FramebufferUpdateEventArgs : EventArgs {
11 public Rectangle Rectangle { get; private set; }
12 public FramebufferUpdateEventArgs(Rectangle rect) {
13 Rectangle = rect;
14 }
15 }
16 [ClassInterface((Int16)0)]
17 [TypeLibType(2)]
18 [ComVisible(true)]
19 public class Framebuffer : IFramebuffer {
20 Mutex mutex = new Mutex();
21
22 public Framebuffer() {
23 byte b = 0;
24 RequestResize(0, (int)FramebufferPixelFormat.FramebufferPixelFormat_FOURCC_RGB, ref b, 32, 0, 640, 480);
25 }
26
27 public void Lock() {
28 //Console.WriteLine("Lock");
29 mutex.WaitOne();
30 }
31 public void Unlock() {
32 //Console.WriteLine("Unlock");
33 mutex.ReleaseMutex();
34 }
35
36 public Bitmap Bitmap { get; private set; }
37 public event EventHandler BufferChanged;
38 public event EventHandler<FramebufferUpdateEventArgs> Update;
39
40 public IntPtr Address { get; private set; }
41 public uint BitsPerPixel { get; private set; }
42 public uint BytesPerLine { get; private set; }
43 public uint Height { get; private set; }
44 public uint HeightReduction { get { return 0; } }
45 public IFramebufferOverlay Overlay { get { return null; } }
46 public uint PixelFormat { get; private set; }
47 public int UsesGuestVRAM { get { return 0; } }
48 public uint Width { get; private set; }
49 public long WinId { get; internal set; }
50
51 public unsafe void NotifyUpdate(uint aX, uint aY, uint aWidth, uint aHeight) {
52 //Console.WriteLine("NotifyUpdate X={0} Y={1} Width={2} Height={3}", aX, aY, aWidth, aHeight);
53 if (Update != null) Update(this, new FramebufferUpdateEventArgs(new Rectangle((int)aX, (int)aY, (int)aWidth, (int)aHeight)));
54 }
55 public void ProcessVHWACommand(ref byte aCommand) {
56 Console.WriteLine("ProcessVHWACommand");
57 }
58 public unsafe int RequestResize(uint aScreenId, uint aPixelFormat, ref byte aVRAM, uint aBitsPerPixel, uint aBytesPerLine, uint aWidth, uint aHeight) {
59 //Note that aVRAM is a pointer, which may be NULL!
60 //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);
61 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);
62 Lock();
63 try {
64 IntPtr oldAddress = Address;
65
66 Width = aWidth;
67 Height = aHeight;
68
69 BitsPerPixel = 32;
70 PixelFormat = (int)FramebufferPixelFormat.FramebufferPixelFormat_FOURCC_RGB;
71 BytesPerLine = BitsPerPixel * Width / 8; //aBytesPerLine;
72 Address = Marshal.AllocHGlobal((int)(BytesPerLine * Height));
73 if (Bitmap != null) Bitmap.Dispose();
74 Bitmap = new Bitmap((int)Width, (int)Height, (int)BytesPerLine, System.Drawing.Imaging.PixelFormat.Format32bppRgb, Address);
75 if (BufferChanged != null) BufferChanged(this, new EventArgs());
76 if (oldAddress != IntPtr.Zero) Marshal.FreeHGlobal(oldAddress);
77 //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);
78 } finally {
79 Unlock();
80 }
81 return 1; //Finished
82 }
83
84 ~Framebuffer() {
85 if (Bitmap != null) Bitmap.Dispose();
86 if (Address != IntPtr.Zero) Marshal.FreeHGlobal(Address);
87 }
88
89 public void SetVisibleRegion(ref byte aRectangles, uint aCount) {
90 Console.WriteLine("SetVisibleRegion");
91 }
92 public uint GetVisibleRegion(ref byte aRectangles, uint aCount) {
93 Console.WriteLine("GetVisibleRegion");
94 return 0;
95 }
96 public int VideoModeSupported(uint aWidth, uint aHeight, uint aBpp) {
97 Console.WriteLine("VideoModeSupported Width={0} Height={1} Bpp={2}", aWidth, aHeight, aBpp);
98 return 1; //Supported
99 }
100 }
101 }