Linux framebuffer Analysis Writing application based on Framebuffer interface

Source: Internet
Author: User

Liu Hao

Blog: Http://blog.csdn.net/liuhaoyutz

Test environment: Ubuntu 12.04 Terminal mode

On the internet to find a very good introduction framebuffer related knowledge of posts, the original post URL is as follows: http://bbs.chinaunix.net/ thread-1932291-1-1.html, the application code of which the Test framebuffer is reprint to facilitate analysis and study:

#include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <fcntl.h> #include < linux/fb.h> #include <linux/kd.h> #include <sys/mman.h> #include <sys/ioctl.h> #include <sys/ time.h> #include <string.h> #include <errno.h> struct fb_var_screeninfo vinfo;struct fb_fix_screeninfo Finfo;char *framebuffer = 0; To print the fix structure information in FB driver, note: After the FB driver is loaded, the fix structure cannot be modified. Voidprintfixedinfo () {printf ("Fixed screen info:\n" "\tid:%s\n" "\tsmem _start:0x%lx\n "" \tsmem_len:%d\n "" \ttype:%d\n "" \tty pe_aux:%d\n "" \tvisual:%d\n "" \txpanstep:%d\n "" \typa                        nstep:%d\n "" \tywrapstep:%d\n "" \tline_length:%d\n "   "\tmmio_start:0x%lx\n" "\tmmio_len:%d\n" "\taccel:%d\n"        "\ n", Finfo.id, Finfo.smem_start, Finfo.smem_len, Finfo.type, Finfo.type_aux, finfo.visual, fi Nfo.xpanstep, Finfo.ypanstep, Finfo.ywrapstep, Finfo.line_length, Finfo.mmio_start, Finfo.mmio_len, fi Nfo.accel);}                        Print FB drive VAR structure information, note: After the FB driver is loaded, the VAR structure can be reset according to the actual need Voidprintvariableinfo () {printf ("Variable screen info:\n"                        "\txres:%d\n" "\tyres:%d\n" "\txres_virtual:%d\n"                        "\tyres_virtual:%d\n" "\tyoffset:%d\n" "\txoffset:%d\n" "\tbits_per_pixel:%d\n" "\tgrayscale:%d\n" "\tred:offset:%2d, Length:                        %2d, Msb_right:%2d\n "" \tgreen:offset:%2d, Length:%2d, Msb_right:%2d\n " "\tblue:offset:%2d, Length:%2d, Msb_right:%2d\n" "\ttransp:offset:%2d, Length:%2d, Msb_right:     %2d\n "                   "\tnonstd:%d\n" "\tactivate:%d\n" "\theight:%d\n"                        "\twidth:%d\n" "\taccel_flags:0x%x\n" "\tpixclock:%d\n" "\tleft_margin:%d\n" "\tright_margin:%d\n" "\tupper_marg in:%d\n "" \tlower_margin:%d\n "" \thsync_len:%d\n "" \ T vsync_len:%d\n "\tsync:%d\n" "\tvmode:%d\n" "\ n", Vinfo.           Xres, Vinfo.yres, Vinfo.xres_virtual, Vinfo.yres_virtual, Vinfo.xoffset, Vinfo.yoffset, Vinfo.bits_per_pixel, Vinfo.grayscale, Vinfo.red.offset, Vinfo.red.length, Vinfo.red.msb_right,vinfo.green.offset, Vinfo.gree N.length, Vinfo.green.msb_right, Vinfo.blue.offset, Vinfo.blue.length, Vinfo.blue.msb_right, Vinfo.tra Nsp.offset, VINFO.TRANSP. length, Vinfo.transp.msb_right, VINFO.NONSTD, Vinfo.activate, Vinfo.height, Vinfo.width, Vinfo.accel_ Flags, Vinfo.pixclock, Vinfo.left_margin, Vinfo.right_margin, Vinfo.upper_margin, Vinfo.lower_margin, Vinfo.hsync_len, Vinfo.vsync_len, Vinfo.sync, Vinfo.vmode);} Draw the same color matrix with size width*height, 8alpha+8reds+8greens+8bluesvoiddrawrect_rgb32 (int x0, int y0, int width,int height, int color)   {Const int bytesperpixel = 4;    const int stride = Finfo.line_length/bytesperpixel;    int *dest = (int *) (FrameBuffer) + (y0 + vinfo.yoffset) * Stride + (x0 + vinfo.xoffset);   int x, y;        for (y = 0, y < height; ++y) {for (x = 0; x < width; ++x) {dest[x] = color;    } dest + = stride; }}//Draw the same color matrix with size width*height, 5reds+6greens+5bluesvoiddrawrect_rgb16 (int x0, int y0, int width,int height, int color) {con   St int bytesperpixel = 2;   const int stride = Finfo.line_length/bytesperpixel; const INT RED= (color & 0xff0000) >> (16 + 3);   const int green = (color & 0xff00) >> (8 + 2);   const int blue = (color & 0xff) >> 3; Const short COLOR16 = Blue | (Green << 5) |    (Red << (5 +6));    Short *dest = (short *) (FrameBuffer) + (y0 + vinfo.yoffset) * Stride + (x0 +vinfo.xoffset);   int x, y;       for (y = 0, y < height; ++y) {for (x = 0; x < width; ++x) {dest[x] = color16;    } dest + = stride; }}//Draw the same color matrix with size width*height, 5reds+5greens+5bluesvoiddrawrect_rgb15 (int x0, int y0, int width,int height, int color) {con   St int bytesperpixel = 2;   const int stride = Finfo.line_length/bytesperpixel;   const int red = (color & 0xff0000) >> (16 + 3);   const int green = (color & 0xff00) >> (8 + 3);   const int blue = (color & 0xff) >> 3; Const short COLOR15 = Blue | (Green << 5) | (Red << (5 + 5)) |    0x8000 Short *dest = (short *) (FrameBuffer) + (y0+ vinfo.yoffset) * Stride + (x0 + vinfo.xoffset);   int x, y;       for (y = 0, y < height; ++y) {for (x = 0; x < width; ++x) {dest[x] = Color15;    } dest + = stride; }} voiddrawrect (int x0, int y0, int width, intheight, int color) {switch (vinfo.bits_per_pixel) {case 32:d       Rawrect_rgb32 (x0, y0, width, height, color);   Break       Case 16:drawrect_rgb16 (x0, y0, width, height, color);   Break       Case 15:drawrect_rgb15 (x0, y0, width, height, color);   Break       default:printf ("Warning:drawrect () not implemented for color depth%i\n", vinfo.bits_per_pixel);    Break   }} #define Performance_run_count 5voidperformSpeedTest (void *fb, int fbsize) {int I, j, RUN;   struct Timeval startTime, endTime;   unsigned long long results[performance_run_count];   unsigned long long average;    unsigned int *testimage; unsigned int randdata[17] = {0x3a428472, 0x724b84d3, 0x26b898ab,0X7D980E3C, 0x5345a084, 0x6779b66b, 0x791ee4b4, 0x6e8ee3cc, 0x63af504a, 0x18a21b33, 0x0e26eb73, 0x022F708E, 0x    1740f3b0, 0x7e2c699d, 0x0e8a570b, 0X5F2C22FB, 0x6a742130};   printf ("Frame Buffer performance test...\n"); for (run = 0; run < Performance_run_count; ++run) {/* Generate Test image with random (ish) data: */tes       Timage = (unsigned int *) malloc (fbsize);       j = run;           for (i = 0; i < (int) (fbsize/sizeof (int)); ++i) {testimage[i] = randdata[j];           j + +;       if (j >=) j = 0;       } gettimeofday (&starttime, NULL);       memcpy (FB, Testimage, fbsize);        Gettimeofday (&endtime,null);                Long Secsdiff = endtime.tv_sec-starttime.tv_sec;                 Results[run] = Secsdiff * 1000000 + (ENDTIME.TV_USEC-STARTTIME.TV_USEC);    Free (testimage);   } average = 0; for (i = 0; i < performance_run_count; ++i) avErage + = Results[i];    Average = Average/performance_run_count;   printf ("Average:%llu usecs\n", Average);   printf ("Bandwidth:%.03f mbyte/sec\n", (fbsize/1048576.0)/((double) average/1000000.0)); printf ("Max.    FPS:%.03f fps\n\n ", 1000000.0/(double) average); /* Clear The framebuffer back to black again: */memset (FB, 0, fbsize);}   Intmain (int argc, char **argv) {const char *devfile = "/dev/fb0";   long int screensize = 0;     int FBFD = 0;   /* Open the file for reading and writing */FBFD = open (Devfile, O_RDWR);       if (FBFD = =-1) {perror ("Error:cannot Open framebuffer Device");    Exit (1); }//Get finfo information and show if (IOCTL (FBFD, fbioget_fscreeninfo, &finfo) = =-1) {perror ("Error reading fixed inf       Ormation ");    Exit (2);   } printfixedinfo (); Get vinfo information and show if (IOCTL (FBFD, fbioget_vscreeninfo, &vinfo) = =-1) {perror ("Error reading variable infor       Mation ");    Exit (3);} printvariableinfo ();    /* figure out the size of the bytes */screensize = Finfo.smem_len; /* Map the device to memory */FrameBuffer = (char *) mmap (0, screensize, Prot_read |       Prot_write, map_shared, FBFD, 0);           if (FrameBuffer = = map_failed) {perror ("error:failed to MAP FrameBuffer device to Memory");       Exit (4);        }//Test Virt FB Performance performspeedtest (FrameBuffer, screensize); printf ("would draw 3 rectangles on the screen,\n" "They should is coloredred, green and blue (in that order       ). \ n ");       DrawRect (VINFO.XRES/8, VINFO.YRES/8, VINFO.XRES/4, VINFO.YRES/4, 0xffff0000);       DrawRect (Vinfo.xres * 3/8, Vinfo.yres * 3/8, VINFO.XRES/4, vinfo.yres/4,0xff00ff00);        DrawRect (Vinfo.xres * 5/8, Vinfo.yres * 5/8, VINFO.XRES/4, VINFO.YRES/4, 0XFF0000FF);       Sleep (5); PrinTF ("done.\n");   Munmap (FrameBuffer, screensize);       De-memory mapping, corresponding to mmap close (FBFD); return 0;}


The program needs to run in the terminal mode of open framebuffer, I use Ubuntu 12.04, press CTRL + ALT + F1 directly, switch to Terminal 1 to run the program. The program works as shown in the following:

To illustrate, in terminal mode, the software I use is fbgrab.

On my computer, the program prints the information on the terminal as follows:

Fixed screen INFO:ID:INTELDRMFB smem_start:0xd0064000 smem_len:8294400 type:0 type_aux:       0 visual:2 xpanstep:1 ypanstep:1 ywrapstep:0 line_length:7680 mmio_start:0x0 mmio_len:0 accel:0 Variable screen info:xres:1920 yres:1080 xres_virtual:1920 yres _virtual:1080 yoffset:0 xoffset:0 bits_per_pixel:32 grayscale:0 red:offset:16, length       : 8,msb_right:0 Green:offset:8, Length:8, msb_right:0 blue:offset:0, Length:8, msb_right:0 transp:offset:0, length:0, msb_right:0 nonstd:0 activate:0 Height:-1 width: 1 ac       cel_flags:0x1 pixclock:0 left_margin:0 right_margin:0 upper_margin:0 lower_margin:0 hsync_len:0 vsync_len:0 sync:0 vmode:0 Frame Buffer performance Test ... average:1489 usecs bandwidth:5312.395 Mbyte/seC max.fps:671.592 FPS would draw 3 rectangles on the screen,they should is colored red, green and blue (in that order). Done.


With this test program, we can understand how to draw the desired graph in the Linux user space, based on the Framebuffer interface.

Linux framebuffer Analysis Writing application based on Framebuffer interface

Related Article

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.