Input abstraction layer and NativeEngine implementation 2

Source: Internet
Author: User
Article title: Implementation 2 of the input abstraction layer and NativeEngine. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
   3 Native graphics engine implementation
  
Native graphics engine graphics drivers have provided FrameBuffer-based drivers based on Linux kernel. Currently, they support linear 2 bpp, 4bpp, 8bpp, and 16bpp display modes. As we have seen above, most of the interface functions provided by GAL are related to graphics. they call graphics drivers to complete tasks. The graphic driver shields the details of the underlying driver and completes the underlying driver-related functions, instead of some hardware-related functions, such as some circle and line-drawn GDI functions.
  
The following describes implementation details based on the implemented FrameBuffer-based driver. First, the core data structure SCREENDEVICE is listed. This is mainly for convenience, so some secondary variables or functions are deleted.
  
Listing 5 core data structure of the Native graphics engine
  
Typedef struct _ screendevice {
Int xres;/* X screen res (real )*/
Int yres;/* Y screen res (real )*/
Int planes;/* # planes */
Int bpp;/* # bits per pixel */
Int linelen;/* line length in bytes for bpp 1, 2, 4, 8, line length in pixels for bpp 16, 24, 32 */
Int size;/* size of memory allocated */
Gfx_pixel gr_foreground;/* current foreground color */
Gfx_pixel gr_background;/* current background color */
Int gr_mode;
Int flags;/* device flags */
Void * addr;/* address of memory allocated (memdc or fb )*/
  
PSD (* Open) (PSD psd );
Void (* Close) (PSD psd );
Void (* SetPalette) (PSD psd, int first, int count, gfx_color * cmap );
Void (* GetPalette) (PSD psd, int first, int count, gfx_color * cmap );
PSD (* AllocateMemGC) (PSD psd );
BOOL (* MapMemGC) (PSD mempsd, int w, int h, int planes, int bpp, int linelen, int size, void * addr );
Void (* FreeMemGC) (PSD mempsd );
Void (* FillRect) (PSD, int x, int y, int w, int h, gfx_pixel c );
Void (* DrawPixel) (PSD psd, int x, int y, gfx_pixel c );
Gfx_pixel (* ReadPixel) (PSD psd, int x, int y );
Void (* DrawHLine) (PSD psd, int x, int y, int w, gfx_pixel c );
Void (* PutHLine) (GAL, int x, int y, int w, void * buf );
Void (* GetHLine) (GAL, int x, int y, int w, void * buf );
Void (* DrawVLine) (PSD, int x, int y, int w, gfx_pixel c );
Void (* PutVLine) (GAL, int x, int y, int w, void * buf );
Void (* GetVLine) (GAL, int x, int y, int w, void * buf );
Void (* bsty) (PSD dstpsd, int dstx, int dsty, int w, int h, PSD srcpsd, int srcx, int srcy );
Void (* PutBox) (GAL gal, int x, int y, int w, int h, void * buf );
Void (* GetBox) (GAL gal, int x, int y, int w, int h, void * buf );
Void (* PutBoxMask) (GAL gal, int x, int y, int w, int h, void * buf );
Void (* CopyBox) (PSD psd, int x1, int y1, int w, int h, int x2, int y2 );
} SCREENDEVICE;
  
The above PSD is the SCREENDEVICE pointer, and the GAL is the data structure of the GAL interface.
  
We know that graphic display has a concept of display mode. a pixel can be expressed in one bit, or in 2, 4, 8, 15, 16, 24, and 32 bits. In addition, the standard VGA16 mode uses the flat graphics mode, while VESA2.0 uses the linear graphics mode. So even if the Framebuffer-based driver is the same, different driver functions should be used in different modes: it is obviously different to draw a 1-bit monochrome point and a 24-bit true color point.
  
Therefore, graphics drivers use the concept of sub-drivers to support various display modes. In fact, they are the final functional functions. In order to keep the data structure at a level not complex, we use the initial function Open of the graphic driver to directly assign the function functions of the sub-driver to the interface function pointer of the graphic driver, after initialization, a simple graphic driver interface is used. Below is the sub-graphics driver interface (listing 6 ).
  
Listing 6 Native graphics engine sub-driver interfaces
  
Typedef struct {
Int (* Init) (PSD psd );
Void (* DrawPixel) (PSD psd, int x, int y, gfx_pixel c );
Gfx_pixel (* ReadPixel) (PSD psd, int x, int y );
Void (* DrawHLine) (PSD psd, int x, int y, int w, gfx_pixel c );
Void (* PutHLine) (GAL, int x, int y, int w, void * buf );
Void (* GetHLine) (GAL, int x, int y, int w, void * buf );
Void (* DrawVLine) (PSD, int x, int y, int w, gfx_pixel c );
Void (* PutVLine) (GAL, int x, int y, int w, void * buf );
Void (* GetVLine) (GAL, int x, int y, int w, void * buf );
Void (* bsty) (PSD dstpsd, int dstx, int dsty, int w, int h, PSD srcpsd, int srcx, int srcy );
Void (* PutBox) (GAL gal, int x, int y, int w, int h, void * buf );
Void (* GetBox) (GAL gal, int x, int y, int w, int h, void * buf );
Void (* PutBoxMask) (GAL gal, int x, int y, int w, int h, void * buf );
Void (* CopyBox) (PSD psd, int x1, int y1, int w, int h, int x2, int y2 );
} SUBDRIVER, * PSUBDRIVER;
  
As you can see, in addition to the Init function pointer, other function pointers in this interface are the same as the function pointer in the graphic driver interface. The Init function is used to initialize the display mode of the graphic driver.
  
The following describes the data structure of SCREENDEVICE, so that you can understand the graphics engine.
  
A screendevice represents a screen device, which can correspond to a physical screen device or a memory screen device. the existence of memory screen devices mainly aims to improve the quality of GDI, for example, we first store a bitmap in the memory and then draw it on the screen. This gives the user a better visual effect.
  
First, we will introduce several variables.
  
Xres indicates the screen width (in pixels );
Yres indicates the screen height (in pixels );
Planes: when it is in the flat display mode, planes is used to record the number of flat planes used, such as the linear mode relative to the flat mode. at this time, this variable is meaningless. Usually set it to 0.
Bpp: the number of bits used by each pixel, which can be 1, 2, 4, 8, 15, 16, 24, and 32.
Linelen: for each pixel mode of 1, 2, 4, and 8 bits, it indicates the number of bytes used by a row of pixels. for a mode larger than 8 bits per pixel, it indicates the total number of pixels in a row.
Size: The number of memories used by the device in this display mode. Linelen and size are mainly used to allocate memory for memory screen devices.
Gr_foreground and gr_background: the foreground color and background color of the memory screen, which are mainly used by some GDI functions.
Gr_mode: describes how to draw a notebook to the screen. the optional values are MODE_SET MODE_XOR MODE_OR MODE_AND MODE_MAX. MODE_SET and MODE_XOR are commonly used.
Flags: some options for the screen device. the more important is the PSF_MEMORY mark, indicating that the screen device represents the physical screen device or a memory screen device.
Addr: each screen device has a memory space for storing pixels. The addr variable records the starting address of the space.
The following describes the interface functions:
  
Open, Close
Basic initialization and termination functions. As mentioned above, you need to select a sub-graphics driver in the Open function and assign the implemented function to the function pointer in the PSD structure. Here I will talk about the initialization of the Frambebuffer-based graphics engine.
  
Fb_open first open the device file/dev/fb0 of Framebuffer, and then use ioctl to read various information about the current Framebuffer. Fill in the PSD structure. And select the sub-driver based on the information. The program currently supports fbvga16, fblin16, and fblin8, namely, the standard VGA16 mode, the VESA linear 16-bit mode, and the VESA linear 8-bit mode. Then, place the current terminal mode in the graphic mode. And save some current system information, such as the palette information. Finally, the system uses mmap to map/dev/fb0 to the memory address. Later, program access to/dev/fb0 will be as simple as accessing an array. Of course, this is for the linear mode. if it is for the plane mode, the problem is much more complicated. From the code point of view, the code in the plane mode is nearly twice the implementation of the linear mode. This problem will be explained in the subsequent difficulties analysis.
  
SetPalette, GetPalette
The system color palette is used when the 8-bit or below graph mode is used. The color palette processing functions are similar to those in Windows APIs. linux uses ioctl to provide an interface for processing the color palette.
  
AllocateMemGC, MapMemGC, freemgc
I have mentioned the concept of memory screen many times before. the memory screen is a pseudo screen. during operations on the screen image, such as moving the window, we have a memory screen, copy an area of the physical screen to the memory screen and then to the new location of the physical screen. this reduces the latency of direct screen copy. Allocatemgc is used to allocate space to the memory screen. MapMemGC performs initialization, while freemgc releases the memory screen.
  
DrawPixel, ReadPixel, DrawHLine, DrawVLine, FillRect
These are underlying graphic functions. They are painting points, reading points, draw horizontal lines, draw vertical lines, and draw a solid rectangle. So many functions are implemented at the underlying layer to improve efficiency. Graphic functions support multiple drawing modes, which are commonly used for direct setting or Alpha hybrid mode, so as to support various graphic effects.
  
PutHLine, GetHLine, PutVLine, GetVLine, PutBox, GetBox

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.