织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>嵌入式开发>Android>

Android原生(Native)C开发之二 framebuffer篇

落鹤生 发布于 2011-05-12 09:21 点击:次 
虽然现在能通过交叉环境编译程序,并push到Android上执行,但那只是console台程序,是不是有些单调呢?下面就要看如何通过 Linux的 framebuffer 技术在Android上画图形,关于Linux的framebuffer技术,这里就不再详细讲解了,请大家google一下。
TAG:

虽然现在能通过交叉环境编译程序,并push到Android上执行,但那只是console台程序,是不是有些单调呢?下面就要看如何通过 Linux的 framebuffer 技术在Android上画图形,关于Linux的framebuffer技术,这里就不再详细讲解了,请大家google一下。

操作framebuffer的主要步骤如下:

1、打开一个可用的FrameBuffer设备;
2、通过mmap调用把显卡的物理内存空间映射到用户空间;
3、更改内存空间里的像素数据并显示;

4、退出时关闭framebuffer设备。

下面的这个例子简单地用framebuffer画了一个渐变的进度条,代码 framebuf.c 如下:

  1. #include <unistd.h> 
  2. #include <stdio.h> 
  3. #include <fcntl.h> 
  4. #include <linux/fb.h> 
  5. #include <sys/mman.h> 
  6.  
  7. inline static unsigned short int make16color(unsigned char r
  8. , unsigned char g, unsigned char b) 
  9. return ( 
  10. (((r >> 3) & 31) << 11) | 
  11. (((g >> 2) & 63) << 5)  | 
  12. ((b >> 3) & 31)        ); 
  13.  
  14. int main() { 
  15. int fbfd = 0; 
  16. struct fb_var_screeninfo vinfo; 
  17. struct fb_fix_screeninfo finfo; 
  18. long int screensize = 0; 
  19. char *fbp = 0; 
  20. int x = 0, y = 0; 
  21. int guage_height = 20, step = 10; 
  22. long int location = 0; 
  23.  
  24. // Open the file for reading and writing 
  25. fbfd = open(”/dev/graphics/fb0″, O_RDWR); 
  26. if (!fbfd) { 
  27. printf(”Error: cannot open framebuffer device.\n”); 
  28. exit(1); 
  29. printf(”The framebuffer device was opened successfully.\n”); 
  30.  
  31. // Get fixed screen information 
  32. if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) { 
  33. printf(”Error reading fixed information.\n”); 
  34. exit(2); 
  35.  
  36. // Get variable screen information 
  37. if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) { 
  38. printf(”Error reading variable information.\n”); 
  39. exit(3); 
  40.  
  41. printf(”sizeof(unsigned short) = %d\n”, sizeof(unsigned short)); 
  42. printf(”%dx%d, %dbpp\n”, vinfo.xres, vinfo.yres, vinfo.bits_per_pixel ); 
  43. printf(”xoffset:%d, yoffset:%d, line_length: %d\n”
  44. , vinfo.xoffset, vinfo.yoffset, finfo.line_length ); 
  45.  
  46. // Figure out the size of the screen in bytes 
  47. screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;; 
  48.  
  49. // Map the device to memory 
  50. fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, 
  51. fbfd, 0); 
  52.  
  53. if ((int)fbp == -1) { 
  54. printf(”Error: failed to map framebuffer device to memory.\n”); 
  55. exit(4); 
  56. printf(”The framebuffer device was mapped to memory successfully.\n”); 
  57.  
  58. //set to black color first 
  59. memset(fbp, 0, screensize); 
  60. //draw rectangle 
  61. y = (vinfo.yres - guage_height) / 2 - 2;
  62. // Where we are going to put the pixel 
  63. for (x = step - 2; x < vinfo.xres - step + 2; x++) { 
  64. location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) + 
  65. (y+vinfo.yoffset) * finfo.line_length; 
  66.  
  67. *((unsigned short int*)(fbp + location)) = 255; 
  68.  
  69. y = (vinfo.yres + guage_height) / 2 + 2;
  70. // Where we are going to put the pixel 
  71. for (x = step - 2; x < vinfo.xres - step + 2; x++) { 
  72. location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) + 
  73. (y+vinfo.yoffset) * finfo.line_length; 
  74.  
  75. *((unsigned short int*)(fbp + location)) = 255; 
  76.  
  77. x = step - 2; 
  78. for (y = (vinfo.yres - guage_height) / 2 - 2;
  79.  y < (vinfo.yres + guage_height) / 2 + 2; y++) { 
  80. location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) + 
  81. (y+vinfo.yoffset) * finfo.line_length; 
  82.  
  83. *((unsigned short int*)(fbp + location)) = 255; 
  84.  
  85. x = vinfo.xres - step + 2; 
  86. for (y = (vinfo.yres - guage_height) / 2 - 2;
  87.  y < (vinfo.yres + guage_height) / 2 + 2; y++) { 
  88. location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) + 
  89. (y+vinfo.yoffset) * finfo.line_length; 
  90.  
  91. *((unsigned short int*)(fbp + location)) = 255; 
  92.  
  93. // Figure out where in memory to put the pixel 
  94. for ( x = step; x < vinfo.xres - step; x++ ) { 
  95. for ( y = (vinfo.yres - guage_height) / 2;
  96.  y < (vinfo.yres + guage_height) / 2; y++ ) { 
  97. location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) + 
  98. (y+vinfo.yoffset) * finfo.line_length; 
  99.  
  100. if ( vinfo.bits_per_pixel == 32 ) { 
  101. *(fbp + location) = 100;        // Some blue 
  102. *(fbp + location + 1) = 15+(x-100)/2;     // A little green 
  103. *(fbp + location + 2) = 200-(y-100)/5;    // A lot of red 
  104. *(fbp + location + 3) = 0;      // No transparency 
  105. else { //assume 16bpp 
  106. unsigned char b = 255 * x / (vinfo.xres - step); 
  107. unsigned char g = 255;     // (x - 100)/6 A little green 
  108. unsigned char r = 255;    // A lot of red 
  109. unsigned short int t = make16color(r, g, b); 
  110. *((unsigned short int*)(fbp + location)) = t; 
  111. //printf(”x = %d, temp = %d\n”, x, temp); 
  112. //sleep to see it 
  113. usleep(200); 
  114. //clean framebuffer 
  115. munmap(fbp, screensize); 
  116. close(fbfd); 
  117.  
  118. return 0; 

注意,在Android环境,framebuffer设备不是象linux一样的 /dev/fb0,而是 /dev/graphics/fb0 ,

  1. fbfd = open(”/dev/graphics/fb0″, O_RDWR);  

打开framebuffer设备,

  1. fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,fbfd, 0);  

将设备map到一块内存,然后就可以操作这块内存空间来显示你想画的图形了。

最后别忘了关闭设备: 

  1. munmap(fbp, screensize); 
  2. close(fbfd);  

效果图如下:

(emck)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201104/11307.html]
本文出处:emck.avaw.com 作者:emck
顶一下
(2)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容