linux screen display

. Pixel (pixel) The basic unit of image display
2. Resolution (image resolution) Resolution refers to the number of display pixels per unit area
3. Color depth (BPP, bits per pixel) The number of different colors that a pixel can express depends on


bits per pixel.
Common values ​​are:
8bpp: 256 colors; 16 bpp: 2^16=65535 colors; 24 bpp: 2^24=16 million colors
A 16-bit depth is usually divided into 5-bit red, 5-bit blue, and 6-bit Green (eyes are more


sensitive ) 24-bit is generally 8 bits per component, and 32-bit color depth is also common, which means that


in addition to 24-bit pixels, there are 8 extra bits to describe transparency.
The greater the number of bytes corresponding to a pixel, the deeper the color depth and the more delicate the expressiveness.


        The device node file corresponding to the LCD display is /dev/fb0. Before writing the program, we also need to understand the framebuffer.    



The framebuffer mechanism imitates the function of the graphics card, and abstracts the hardware structure of the graphics card into a series of data structures, which can directly operate the video memory through the reading and writing of the framebuffer. Users can regard the framebuffer as an image of the video memory. After mapping it to the process space, you can directly perform read and write operations, and the write operations will be directly reflected on the screen.


Understand some structures of framebuffer

//struct fb_fix_screeninfo is used to describe the attributes of the graphics card
    struct fb_fix_screeninfo {
    char id[16]; /* identification string eg "TT Builtin" */
    unsigned long smem_start; /* video memory start address*/
    /* actual physical address*/
    __u32 smem_len; /* memory size*/
    __u32 type; /* see FB_TYPE_* */
    __u32 type_aux; /* Interleave for interleaved Planes */
    __u32 visual; /* see FB_VISUAL_* */
    __u16 xpanstep; /* zero if no hardware panning * /
    * zero if no hardware panning */
    __u16 ywrapstep; /* zero if no hardware ywrap */
    __u32 line_length; /* length of a line in bytes */
    unsigned long mmio_start; /* Start of Memory Mapped I/ O */
    /* (physical address) */
    __u32 mmio_len; /* Length of Memory Mapped I/O */
    __u32 accel; /* Type of acceleration available */
    __u16 reserved[3]; /* Reserved for future compatibility */
};
Above The structure is determined by the driver according to the hardware configuration and cannot be modified. We need to construct and operate the frame-buffer mapped memory according to the specific information provided by the
structure .
//Characteristics of graphics card
struct fb_var_screeninfo
{
    __u32 xres; /* visible resolution */
    __u32 yres;
    __u32 xres_virtual; /* virtual resolution */
    __u32 yres_virtual;
    __u32 xoffset; /* offset bits from virtual to visible resolution */
    __u32 yoffset;
    __u32_per_pixel ; /* guess what */
    __u32 grayscale; /* != 0 Gray levels instead of colors */
    struct fb_bitfield red; /* bitfield in fb mem if true color, */
    struct fb_bitfield green; /* else only length is significant */
    struct fb_bitfield blue;
    struct fb_bitfield transp; /* transparency */
    __u32 nonstd; /* != 0 Non standard pixel format */
    __u32 activate; /* see FB_ACTIVATE_* */
    __u32 height; /* height of picture in mm */
    __u32 width; /* width of picture in mm */
    __u32 accel_flags; /* acceleration flags (hints) */
    /* Timing: All values in pixclocks, except pixclock (of course) 


*/
    __u32 pixclock; /* pixel clock in ps (pico seconds) */
    __u32 left_margin; /* time from sync to picture */
    __u32 right_margin; /* time from picture to sync */
    __u32 upper_margin; /* time from sync to picture */
    __u32 lower_margin;
    __u32 hsync_len; /* length of horizontal sync */
    __u32 vsync_len; /* length of vertical sync */
    __u32 sync; /* see FB_SYNC_* */
    __u32 vmode; /* see FB_VMODE_* */
    __u32 reserved[6]; /* Reserved for future compatibility */
};
xres_virtual and yres_virtual Determines the size of the virtual area, and xres and yres are


the size, xoffset and yoffset are offsets, which are the offsets of xres and yres relative to the xres_virtual and


yres_virtual areas (generally xres_virtual and yres_virtual are larger than xres and


yres larger).

Below is the c routine for the screen to display monochrome.


#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <linux/fb.h>


#include <fcntl.h >
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
enum color{red,green,blue};


unsigned long * create_color(struct fb_var_screeninfo *pinfo,enum color c)
{
unsigned long *three_color = calloc(1,pinfo->bits_per_pixel/8);
unsigned long *mask = calloc(1,pinfo->bits_per_pixel/8);


int i;
*mask |= 1;
switch(c)
{
//Create red, according to the information provided by pinfo, write 1 to the position indicating red,
//others are 0, and similarly create two other colors
case red:
for(i=0;i<pinfo->red.length-1;i++)
{
*mask <<= 1;
*mask |= 0x1;
}*three_color |= *mask<<pinfo->red.offset;break;case green:for(i=0;i<pinfo->green.length-1;i++){*mask <<= 1;*mask |= 0x1;}*three_color |= *mask<<pinfo->green.offset;break;case blue:for(i=0;i<pinfo->blue.length-1;i++){*mask <<= 1;*mask |= 0x1;}*three_color |= *mask<<pinfo->blue.offset;break;}return three_color;}int main(void){int lcd = open("/dev/fb0",O_RDWR);if(lcd < 0){perror("lcd open failed:\n");exit(1);}































//Define two structures to store video memory information respectively
struct fb_fix_screeninfo finfo;
struct fb_var_screeninfo vinfo;


//Use the ioctl function to obtain the video memory information according to the command word FBIOGET_FSCREENINFO and
//FBIOGET_VSCREENINFO
ioctl(lcd,FBIOGET_FSCREENINFO,&finfo);
ioctl (lcd,FBIOGET_VSCREENINFO,&vinfo); //Set the offset to 0 vinfo.xoffset = 0; vinfo.xoffset = 0; ioctl(lcd,FBIOPAN_DISPLAY,&vinfo); //Because the position of the RGB three primary colors at each pixel is Fixed, so to display // these three monochrome colors, we must write 1 to the byte corresponding to each pixel, and the other // is 0, so that these three colors can be displayed, we need a function Come //create these three colors //use n to store how many bits each pixel is represented by, and use unsigned long n = vinfo.bits_per_pixel when applying for memory; //create three colors unsigned long *three_color [3] = {0}; three_color[0] = create_color(&vinfo,red);




















three_color[1] = create_color(&vinfo,green);
three_color[2] = create_color(&vinfo,blue);


//Map a piece of memory, when the first parameter is null, it means that the system automatically allocates
//The size of the mapped memory is the same as the screen Same size
char *show=mmap(NULL,vinfo.xres*vinfo.yres*n/8,
PROT_READ | PROT_WRITE,MAP_SHARED,lcd,0) ;
//Display a color every second
while(1)
{
int i ,k;
for(k=0;k<3;k++)
{


for(i=0;i<vinfo.xres*vinfo.yres;i++)
memcpy(show+i*n/8,three_color[k],n /8);
sleep(1);
}
}
return 0;

}

If there is no development board, you can also test it under ubuntu, press ctl+alt+f5 to enter the screen terminal, and then run the compiled file. Press ctl+alt+f7 to exit the black terminal.

This program simply displays monochrome. To display a picture such as jpg, some decoding libraries are required.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324603068&siteId=291194637