Hi,
Ok, I thought it's only fair that I start things off by describing my video device drivers...
----8<----
First, everything in my OS uses asynchronous messaging, where different pieces of software need to use the same protocol (e.g. same message identifiers, etc) to be able to reliably talk to each other. One of these messaging protocols is called the "user interface messaging protocol", and it covers everything that is used to get input from users and output data for the user (video, sound, keyboard, mouse, joystick, touchpad, microphones, etc). There may be a hierarchical tree of processes. For example, messages from applications (using this "user interface messaging protocol") get sent and received to/from the application's parent (e.g. a GUI), and this parent (e.g. the GUI) may send/receive messages (in the exact same "user interface messaging protocol") to/from the parent's parent (e.g. a virtual terminal layer). Eventually, near the top of this hierarchical tree of processes there's a layer that talks directly to device drivers. This is important because this particular layer is responsible for splitting things up - the sound related stuff gets forwarded to the sound card driver, the keyboard related stuff gets forwarded to the keyboard driver, and the video related stuff gets forwarded to the video driver.
Basically, the video driver must support the subset of the "user interface messaging protocol" that relates to video.
This subset of the "user interface messaging protocol" mainly involves a "video script" that describes a complete frame of video using graphics primitives - what should be drawn by the video driver, and where (on the screen) the video driver should drawn them. For example, the script might say "draw a solid box from (x1, y1, z1) to (x2, y2, z2) using a specific colour". In all cases, all colours are specified in
CIE XYZ; and all co-ordinates are 3 dimensional virtual coordinates that range from 0x00000000 to 0xFFFFFFFF (for e.g. the middle of the screen would be at the co-ordinate (0x80000000,0x80000000,0x80000000). This means that the values used to represent colours and the values used to represent co-ordinates never have anything to do with the colour depth or resolution of the video mode currently being used by the video driver; and that the video driver is free to automatically change video modes for any reason. The video driver is responsible for choosing a video mode automatically (which must include negotiation with the monitor - DDC/EDID), and changing this video mode (and the quality of the primitives drawn) to ensure that it takes no more than 30 ms to drawn a complete video script.
In addition, the video driver may/should cache the results of drawing parts of a video script, so that these parts can be redrawn quickly. For example, a video script may describe something that is reused later, and the video driver can draw that thing once and cache the results, and then draw the cached results every time the any video script describes the same object. Video scripts will be subdivided to make this easier (for example, the video script from an application might be split into 4 separate zones, where the first zone describes the menus at the top, the second zone describes a toolbox on the left, the third zone describes the main working area, and the fourth zone describes a status bar; where the beginning of the script contains instructions on where these zones should be placed relative to each other). The video script may ask for these zones to be translated, rotated and scaled (in 3D space) by the video driver, so the video driver needs to support that to.
In addition the video script may include references to files that contain other video scripts, or bitmap data, etc. The video driver is responsible for loading and caching these files, and drawing their contents.
Video scripts may also include (Unicode, UTF-32) character data. It is the video driver's responsibility to communicate with the font engine to get any character data converted into suitable data of the appropriate size/shape and then draw the resulting data from the font engine.
It is recommended (but not necessarily required) that the video driver uses page flipping (or double buffering), and switches between frames using vertical synchronization (e.g. during a vertical retrace IRQ). It is also recommended (but not necessarily required) that the video driver is able to track performance metrics and sends warnings when certain conditions occur, including when its client is trying to update the video too frequently (e.g. if the frame rate from an application exceeds to vertical refresh rate of the current video mode), and when a new frame of video is exactly the same as the current/previous frame of video. When enabled, these warnings are sent by the video driver to a performance monitoring tool.
When the video driver is first started, the operating system will tell it which resources (I/O ports, IRQs and areas of the physical address space) correspond to the video card. The video driver must not attempt to use any other resources, except for RAM. Typically the operating system knows which resources correspond to the video card by examining the device's PCI configuration space (note: the video driver does not have access to the video card's PIC configuration space). In some cases (e.g. an ancient ISA video card) a separate piece of code is responsible for detecting the device and finding out which resources (I/O ports, IRQs and areas of the physical address space) the video card is using; but this has nothing to do with the video driver itself (it's beyond the scope of the video driver). These resource may be virtual resources, and may not be the actual resources used by the video card. For example, the video card might generate IRQ 0x09 when a vertical retrace occurs, and the operating system might tell the video device driver that it's using IRQ 0x00, and when IRQ 0x09 occurs the operating system tells the video driver that IRQ 0x00 occured.
Video drivers must also support virtualization. This is a separate interface to the video driver that uses a different messaging protocol (the video driver has 2 modes: normal and virtualized; and either the "user interface messaging protocol" or the "virtualized device messaging protocol" are used, never both at the same time). In the "virtualized" mode, something (e.g. an emulator) can connect to the video driver, and tell the video driver to read or write to/from certain I/O ports and memory mapped areas; and when an IRQ occurs the video driver forwards the IRQ to the client. In this case the video card must do what it's told by the emulator, except that anything that involves the device transferring data to/from RAM (e.g. DMA or bus mastering) must be virtualized by the device driver. For the purpose of testing, the operating system will allow one instance of the video driver (operating in "virtualized" mode, using the real hardware) to be used by another instance of the same video driver (operating in "normal" mode, using the first instance of the video driver instead of the real hardware *without* knowing it). All video drivers must operate correctly in this situation, and the operating system reserves the right to use this test as a compliance test (and refuse to start the device driver if it fails).
Also, all device drivers run as normal processes (e.g. at CPL=3). This means that a video driver can use multiple threads (and it's recommended that for any heavy processing the video driver does use multiple threads - e.g. one per CPU).
It's recommended (but not strictly required) that unused video card RAM be made available to the operating system in the form of swap space. The messaging protocol used for swap space is fairly simple (mostly it consists of "store page" and "fetch page"), but won't be covered here. For supporting "VRAM as swap space", the video driver should use a separate thread with a higher priority than all other threads used by the video driver.
----8<----
It's missing a lot of details, but it should provide a good enough idea of how video drivers for my OS should work.
I'm hoping that someone else's video drivers are designed in exactly the same way, so that this becomes the common/generic device driver interface for video cards...
Cheers,
Brendan