Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4524375
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: 嵌入式

2011-05-03 15:23:14

本文的copyright归yuweixian4230@163.com 所有,使用GPL发布,可以自由拷贝,转载。但转载请保持文档的完整性,注明原作者及原链接,严禁用于任何商业用途。
作者:yuweixian4230@163.com
博客:
yuweixian4230.blog.chinaunix.net  



  网址 http://www.cnblogs.com/cute/archive/2011/04/20/2022114.html 有一程序关于 framebuffer,现在我对他进行测试,然后进行分析

代码附件: xx.rar  


现在 将 程序贴出来
  1. #include <stdlib.h>
  2.  #include <unistd.h>
  3.  #include <stdio.h>
  4.  #include <fcntl.h>
  5.  #include <linux/fb.h>
  6.  #include <linux/kd.h>
  7.  #include <sys/mman.h>
  8.  #include <sys/ioctl.h>
  9.  #include <sys/time.h>
  10.  #include <string.h>
  11.  #include <errno.h>


  12.  struct fb_var_screeninfo vinfo;
  13.  struct fb_fix_screeninfo finfo;
  14.  char *frameBuffer = 0;


  15.  //打印fb驱动中fix结构信息,注:在fb驱动加载后,fix结构不可被修改。
  16.  void printFixedInfo ()
  17.  {
  18.      printf ("Fixed screen info:\n"
  19.              "\tid: %s\n"
  20.              "\tsmem_start: 0x%lx\n"
  21.              "\tsmem_len: %d\n"
  22.              "\ttype: %d\n"
  23.              "\ttype_aux: %d\n"
  24.              "\tvisual: %d\n"
  25.              "\txpanstep: %d\n"
  26.              "\typanstep: %d\n"
  27.              "\tywrapstep: %d\n"
  28.              "\tline_length: %d\n"
  29.              "\tmmio_start: 0x%lx\n"
  30.              "\tmmio_len: %d\n"
  31.              "\taccel: %d\n"
  32.              "\n",
  33.              finfo.id, finfo.smem_start, finfo.smem_len, finfo.type,
  34.              finfo.type_aux, finfo.visual, finfo.xpanstep, finfo.ypanstep,
  35.              finfo.ywrapstep, finfo.line_length, finfo.mmio_start,
  36.              finfo.mmio_len, finfo.accel);
  37.  }


  38.  //打印fb驱动中var结构信息,注:fb驱动加载后,var结构可根据实际需要被重置
  39.  void printVariableInfo ()
  40.  {
  41.      printf ("Variable screen info:\n"
  42.              "\txres: %d\n"
  43.              "\tyres: %d\n"
  44.              "\txres_virtual: %d\n"
  45.              "\tyres_virtual: %d\n"
  46.              "\tyoffset: %d\n"
  47.              "\txoffset: %d\n"
  48.              "\tbits_per_pixel: %d\n"
  49.              "\tgrayscale: %d\n"
  50.              "\tred: offset: %2d, length: %2d, msb_right: %2d\n"
  51.              "\tgreen: offset: %2d, length: %2d, msb_right: %2d\n"
  52.              "\tblue: offset: %2d, length: %2d, msb_right: %2d\n"
  53.              "\ttransp: offset: %2d, length: %2d, msb_right: %2d\n"
  54.              "\tnonstd: %d\n"
  55.              "\tactivate: %d\n"
  56.              "\theight: %d\n"
  57.              "\twidth: %d\n"
  58.              "\taccel_flags: 0x%x\n"
  59.              "\tpixclock: %d\n"
  60.              "\tleft_margin: %d\n"
  61.              "\tright_margin: %d\n"
  62.              "\tupper_margin: %d\n"
  63.              "\tlower_margin: %d\n"
  64.              "\thsync_len: %d\n"
  65.              "\tvsync_len: %d\n"
  66.              "\tsync: %d\n"
  67.              "\tvmode: %d\n"
  68.              "\n",
  69.              vinfo.xres, vinfo.yres, vinfo.xres_virtual, vinfo.yres_virtual,
  70.              vinfo.xoffset, vinfo.yoffset, vinfo.bits_per_pixel,
  71.              vinfo.grayscale, vinfo.red.offset, vinfo.red.length,
  72.              vinfo.red.msb_right, vinfo.green.offset, vinfo.green.length,
  73.              vinfo.green.msb_right, vinfo.blue.offset, vinfo.blue.length,
  74.              vinfo.blue.msb_right, vinfo.transp.offset, vinfo.transp.length,
  75.              vinfo.transp.msb_right, vinfo.nonstd, vinfo.activate,
  76.              vinfo.height, vinfo.width, vinfo.accel_flags, vinfo.pixclock,
  77.              vinfo.left_margin, vinfo.right_margin, vinfo.upper_margin,
  78.              vinfo.lower_margin, vinfo.hsync_len, vinfo.vsync_len,
  79.              vinfo.sync, vinfo.vmode);
  80.  }


  81.  //画大小为width*height的同色矩阵,8alpha+8reds+8greens+8blues
  82.  void
  83.  drawRect_rgb32 (int x0, int y0, int width, int height, int color)
  84.  {
  85.      const int bytesPerPixel = 4;
  86.      const int stride = finfo.line_length / bytesPerPixel;


  87.      int *dest = (int *) (frameBuffer)
  88.          + (y0 + vinfo.yoffset) * stride + (x0 + vinfo.xoffset);


  89.      int x, y;
  90.      for (y = 0; y < height; ++y)
  91.      {
  92.          for (x = 0; x < width; ++x)
  93.          {
  94.              dest[x] = color;
  95.          }
  96.          dest += stride;
  97.      }
  98.  }


  99.  //画大小为width*height的同色矩阵,5reds+6greens+5blues
  100.  void
  101.  drawRect_rgb16 (int x0, int y0, int width, int height, int color)
  102.  {
  103.      const int bytesPerPixel = 2;
  104.      const int stride = finfo.line_length / bytesPerPixel;
  105.      const int red = (color & 0xff0000) >> (16 + 3);
  106.      const int green = (color & 0xff00) >> (8 + 2);
  107.      const int blue = (color & 0xff) >> 3;
  108.      const short color16 = blue | (green << 5) | (red << (5 + 6));


  109.      short *dest = (short *) (frameBuffer)
  110.          + (y0 + vinfo.yoffset) * stride + (x0 + vinfo.xoffset);


  111.      int x, y;
  112.      for (y = 0; y < height; ++y)
  113.      {
  114.          for (x = 0; x < width; ++x)
  115.          {
  116.              dest[x] = color16;
  117.          }
  118.          dest += stride;
  119.      }
  120.  }


  121.  //画大小为width*height的同色矩阵,5reds+5greens+5blues
  122.  void drawRect_rgb15 (int x0, int y0, int width, int height, int color)
  123.  {
  124.      const int bytesPerPixel = 2;
  125.      const int stride = finfo.line_length / bytesPerPixel;
  126.      const int red = (color & 0xff0000) >> (16 + 3);
  127.      const int green = (color & 0xff00) >> (8 + 3);
  128.      const int blue = (color & 0xff) >> 3;
  129.     const short color15 = blue | (green << 5) | (red << (5 + 5)) | 0x8000;


  130.      short *dest = (short *) (frameBuffer)
  131.          + (y0 + vinfo.yoffset) * stride + (x0 + vinfo.xoffset);


  132.      int x, y;
  133.      for (y = 0; y < height; ++y)
  134.      {
  135.          for (x = 0; x < width; ++x)
  136.          {
  137.              dest[x] = color15;
  138.          }
  139.          dest += stride;
  140.      }
  141.  }


  142.  void drawRect (int x0, int y0, int width, int height, int color)
  143.  {
  144.      switch (vinfo.bits_per_pixel)
  145.      {
  146.      case 32:
  147.          drawRect_rgb32 (x0, y0, width, height, color);
  148.          break;
  149.      case 16:
  150.          drawRect_rgb16 (x0, y0, width, height, color);
  151.          break;
  152.      case 15:
  153.          drawRect_rgb15 (x0, y0, width, height, color);
  154.          break;
  155.      default:
  156.          printf ("Warning: drawRect() not implemented for color depth %i\n",
  157.                  vinfo.bits_per_pixel);
  158.          break;
  159.      }
  160.  }


  161.  #define PERFORMANCE_RUN_COUNT 5
  162.  void performSpeedTest (void *fb, int fbSize)
  163.  {
  164.      int i, j, run;
  165.      struct timeval startTime, endTime;
  166.      unsigned long long results[PERFORMANCE_RUN_COUNT];
  167.      unsigned long long average;
  168.      unsigned int *testImage;

  169.      unsigned int randData[17] = {
  170.          0x3A428472, 0x724B84D3, 0x26B898AB, 0x7D980E3C, 0x5345A084,
  171.          0x6779B66B, 0x791EE4B4, 0x6E8EE3CC, 0x63AF504A, 0x18A21B33,
  172.          0x0E26EB73, 0x022F708E, 0x1740F3B0, 0x7E2C699D, 0x0E8A570B,
  173.          0x5F2C22FB, 0x6A742130
  174.      };


  175.      printf ("Frame Buffer Performance test...\n");
  176.      for (run = 0; run < PERFORMANCE_RUN_COUNT; ++run)
  177.      {
  178.          /* Generate test image with random(ish) data: */
  179.          testImage = (unsigned int *) malloc (fbSize);
  180.          j = run;
  181.          for (i = 0; i < (int) (fbSize / sizeof (int)); ++i)
  182.          {
  183.              testImage[i] = randData[j];
  184.              j++;
  185.              if (j >= 17)
  186.                  j = 0;
  187.          }


  188.          gettimeofday (&startTime, NULL);
  189.          memcpy (fb, testImage, fbSize);
  190.          gettimeofday (&endTime, NULL);


  191.          long secsDiff = endTime.tv_sec - startTime.tv_sec;
  192.          results[run] =
  193.              secsDiff * 1000000 + (endTime.tv_usec - startTime.tv_usec);


  194.          free (testImage);
  195.      }


  196.      average = 0;
  197.      for (i = 0; i < PERFORMANCE_RUN_COUNT; ++i)
  198.          average += results[i];
  199.      average = average / PERFORMANCE_RUN_COUNT;


  200.      printf (" Average: %llu usecs\n", average);
  201.      printf (" Bandwidth: %.03f MByte/Sec\n",
  202.              (fbSize / 1048576.0) / ((double) average / 1000000.0));
  203.      printf (" Max. FPS: %.03f fps\n\n",
  204.              1000000.0 / (double) average);


  205.      /* Clear the framebuffer back to black again: */
  206.      memset (fb, 0, fbSize);
  207.  }


  208.  int main (int argc, char **argv)
  209.  {
  210.      const char *devfile = "/dev/fb0";
  211.      long int screensize = 0;
  212.      int fbFd = 0;





  213.      /* Open the file for reading and writing */
  214.      fbFd = open (devfile, O_RDWR);
  215.      if (fbFd == -1)
  216.      {
  217.          perror ("Error: cannot open framebuffer device");
  218.          exit (1);
  219.      }


  220.      //获取finfo信息并显示
  221.      if (ioctl (fbFd, FBIOGET_FSCREENINFO, &finfo) == -1)
  222.      {
  223.          perror ("Error reading fixed information");
  224.          exit (2);
  225.      }
  226.      printFixedInfo ();
  227.      //获取vinfo信息并显示
  228.      if (ioctl (fbFd, FBIOGET_VSCREENINFO, &vinfo) == -1)
  229.      {
  230.          perror ("Error reading variable information");
  231.          exit (3);
  232.      }
  233.      printVariableInfo ();


  234.      /* Figure out the size of the screen in bytes */
  235.      screensize = finfo.smem_len;


  236.      /* Map the device to memory */
  237.      frameBuffer =
  238.          (char *) mmap (0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,
  239.                       fbFd, 0);
  240.      if (frameBuffer == MAP_FAILED)
  241.      {
  242.          perror ("Error: Failed to map framebuffer device to memory");
  243.          exit (4);
  244.      }


  245.      //测试virt fb的性能
  246.      performSpeedTest (frameBuffer, screensize);


  247.      printf ("Will draw 3 rectangles on the screen,\n"
  248.              "they should be colored red, green and blue (in that order).\n");
  249.      drawRect (vinfo.xres / 8, vinfo.yres / 8,
  250.               vinfo.xres / 4, vinfo.yres / 4, 0xffff0000);
  251.      drawRect (vinfo.xres * 3 / 8, vinfo.yres * 3 / 8,
  252.               vinfo.xres / 4, vinfo.yres / 4, 0xff00ff00);
  253.      drawRect (vinfo.xres * 5 / 8, vinfo.yres * 5 / 8,
  254.               vinfo.xres / 4, vinfo.yres / 4, 0xff0000ff);


  255.      sleep (5);
  256.      printf (" Done.\n");


  257.      munmap (frameBuffer, screensize); //解除内存映射,与mmap对应


  258.      close (fbFd);
  259.      return 0;
  260. }


显示效果



阅读(1427) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~