Cosmos Graphic Subsystem


The Cosmos Graphic Subsystem (CGS from now on) is based on the abstraction of Canvas that is an empty space in which the user of CGS can draw its content.
CGS is not a widget toolkit as Winforms or Gnome / GTK but is thought to be at a lower level and it will be the basics in which widget toolkits will be implemented.
CGS hides the graphics driver (so far VGA, VBE and VMWare SVGA II) used and it is thought to be the universal way to draw on the screen in Cosmos.

While Canvas could be overwritten (for example to create subwindows) the user of CGS does not have to deal with it directly but it must use the static class FullScreenCanvas. Before using any of these following methods, be sure to include the Cosmos.System.Graphics namespace in your project’s using declarations!

Let’s give a look to its API methods.


  1. public static Canvas GetFullScreenCanvas(Mode mode) gets the instance of Canvas representing the complete screen (actually the instance of the running VGA driver) using the specified mode
  2. public static Canvas GetFullScreenCanvas() gets the instance of Canvas representing the complete screen (actually the instance of the running VGA driver) using the VGA driver’s preferred mode
    1. Both graphics drivers (VBE and SVGA II) have a default graphic mode of 1024×768@16.

To really draw into the screen we need to use the Canvas class. Let’s give a look to the API:


List of Properties of the Canvas class

  1. Mode: get / set the mode of the video card to mode. It throws if the selected mode is not supported by the video card
  2. DefaultGraphicMode: default graphic mode this will change based on the underlying hardware
  3. AvailableModes: list of the available modes supported this will change based on the underlying hardware
    1. VMWare doesn’t have a hard-coded list of supported modes, instead it supports arbitrary resolutions that change according to the VM window. This is not yet reflect in Cosmos however.

List of Methods of the Canvas class

  1. Clear(Color color: black) clear the entire Canvas using the specified color as background
  2. void DrawPoint(Pen pen, int x, int y) draws a point at the coordinates specified by x and y with the specified pen
  3. void DrawLine(Pen pen, int x_start, int y_start, int x_end, int y_end) draws a line at the coordinates specified by x_start, y_start and x_end, y_end with the specified pen
  4. void DrawRectangle(Pen pen, int x_start, int y_start,int width, int height) draws a rectangle specified by a coordinate pair, a width, and a height with the specified pen

Really simple right?

A working example

using System;
using Sys = Cosmos.System;
using Cosmos.System.Graphics;

namespace GraphicTest
    public class Kernel : Sys.Kernel
        Canvas canvas;

        protected override void BeforeRun()
            Console.WriteLine("Cosmos booted successfully. Let's go in Graphic Mode");
            canvas = FullScreenCanvas.GetFullScreenCanvas();

        protected override void Run()
                Pen pen = new Pen(Color.Red);
                canvas.DrawPoint(pen, 69, 69);

                pen.Color = Color.GreenYellow;
                canvas.DrawLine(pen, 250, 100, 400, 100);

                pen.Color = Color.IndianRed;
                canvas.DrawLine(pen, 350, 150, 350, 250);

                pen.Color = Color.MintCream;
                canvas.DrawLine(pen, 250, 150, 400, 250);

                pen.Color = Color.PaleVioletRed;
                canvas.DrawRectangle(pen, 350, 350, 80, 60);


                canvas.Mode = new Mode(800, 600, ColorDepth.ColorDepth32);

                pen.Color = Color.LimeGreen;
                canvas.DrawRectangle(pen, 450, 450, 80, 60);


            catch (Exception e)
                mDebugger.Send("Exception occurred: " + e.Message);

Limitation of the current implementation

  1. The Cosmos Graphics Subsystem now supports VMWare platforms using the SVGAII adapter, so you can now easily debug operating systems with a GUI at faster speeds compared to full emulation with Bochs. CGS does not make any assumption on the underlying hardware, now scanning to see if the SVGA II PCI device exists. This all happens under-the-hood so there’s no need to worry! No change to user code should be required to use another driver
  2. Only 32-bit color depth is actually supported, the API provides methods to set a resolution with 24, 16, 8 and 4 bit but the low-level Bochs driver has not yet implemented them
  3. CGS does not allow the user to do basic operations that would fulfill its promise to be the basic block from which a “Widget Toolkit” could be derived for example these methods should be added:
  4. void DrawImage(Image image, int x, int y) Canvas has already defined this method but it is not yet implemented
  5. void DrawString(String string, Font font, Brush brush, int x, int y) Canvas has already defined this method but it is not yet implemented
  6. .Net System.Drawing has overload of these methods taking a float to evaluate if they are really needed for CGS, they are defined but for now, no implementation is provided
  7. A double buffering strategy could be implemented to make it faster
  8. This is more a limitation of the Bochs driver when the Graphic Mode is set there is no more a way to return to text mode
  9. CGS / Graphics Mode interacts badly with the Kernel.Stop method: Bochs does not exit cleanly. You must use Sys.Power.Shutdown to shut down correctly your computer.