Android操作framebuffer
操作framebuffer的主要步骤如下:

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

4、退出时关闭framebuffer设备。



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

#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>

inline static unsigned short int make16color(unsigned char r, unsigned char g, unsigned char b)
{
return (
(((r >> 3) & 31) << 11) |
(((g >> 2) & 63) << 5) |
((b >> 3) & 31) );
}

int main() {
int fbfd = 0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
long int screensize = 0;
char *fbp = 0;
int x = 0, y = 0;
int guage_height = 20, step = 10;
long int location = 0;

// Open the file for reading and writing
fbfd = open("/dev/graphics/fb0", O_RDWR);
if (!fbfd) {
printf("Error: cannot open framebuffer device./n");
exit(1);
}
printf("The framebuffer device was opened successfully./n");

// Get fixed screen information
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
printf("Error reading fixed information./n");
exit(2);
}

// Get variable screen information
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
printf("Error reading variable information./n");
exit(3);
}

printf("sizeof(unsigned short) = %d/n", sizeof(unsigned short));
printf("%dx%d, %dbpp/n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel );
printf("xoffset:%d, yoffset:%d, line_length: %d/n", vinfo.xoffset, vinfo.yoffset, finfo.line_length );

// Figure out the size of the screen in bytes
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;;

// Map the device to memory
fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,
fbfd, 0);

if ((int)fbp == -1) {
printf("Error: failed to map framebuffer device to memory./n");
exit(4);
}
printf("The framebuffer device was mapped to memory successfully./n");

//set to black color first
memset(fbp, 0, screensize);
//draw rectangle
y = (vinfo.yres - guage_height) / 2 - 2; // Where we are going to put the pixel
for (x = step - 2; x < vinfo.xres - step + 2; x++) {
location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
(y+vinfo.yoffset) * finfo.line_length;

*((unsigned short int*)(fbp + location)) = 255;
}

y = (vinfo.yres + guage_height) / 2 + 2; // Where we are going to put the pixel
for (x = step - 2; x < vinfo.xres - step + 2; x++) {
location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
(y+vinfo.yoffset) * finfo.line_length;

*((unsigned short int*)(fbp + location)) = 255;
}

x = step - 2;
for (y = (vinfo.yres - guage_height) / 2 - 2; y < (vinfo.yres + guage_height) / 2 + 2; y++) {
location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
(y+vinfo.yoffset) * finfo.line_length;

*((unsigned short int*)(fbp + location)) = 255;
}

x = vinfo.xres - step + 2;
for (y = (vinfo.yres - guage_height) / 2 - 2; y < (vinfo.yres + guage_height) / 2 + 2; y++) {
location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
(y+vinfo.yoffset) * finfo.line_length;

*((unsigned short int*)(fbp + location)) = 255;
}

// Figure out where in memory to put the pixel
for ( x = step; x < vinfo.xres - step; x++ ) {
for ( y = (vinfo.yres - guage_height) / 2; y < (vinfo.yres + guage_height) / 2; y++ ) {
location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
(y+vinfo.yoffset) * finfo.line_length;

if ( vinfo.bits_per_pixel == 32 ) {
*(fbp + location) = 100; // Some blue
*(fbp + location + 1) = 15+(x-100)/2; // A little green
*(fbp + location + 2) = 200-(y-100)/5; // A lot of red
*(fbp + location + 3) = 0; // No transparency
} else { //assume 16bpp
unsigned char b = 255 * x / (vinfo.xres - step);
unsigned char g = 255; // (x - 100)/6 A little green
unsigned char r = 255; // A lot of red
unsigned short int t = make16color(r, g, b);
*((unsigned short int*)(fbp + location)) = t;
}
}
//printf("x = %d, temp = %d/n", x, temp);
//sleep to see it
usleep(200);
}
//clean framebuffer
munmap(fbp, screensize);
close(fbfd);

return 0;
}

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

fbfd = open("/dev/graphics/fb0", O_RDWR);

打开framebuffer设备,

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



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

最后别忘了关闭设备:

munmap(fbp, screensize);
close(fbfd);

如果您对此文章有任何疑问,欢迎到流媒体开发论坛提问。
本站遵循Creative Commons Attribution 3.0 License,所有文章欢迎任何形式的转载,但请注明作者及出处,尊重他人劳动成果!
文章转载自:罗索工作室 [http://www.rosoo.net]
本文标题:Android操作framebuffer
本文作者:evilcode 本文来源:blog.csdn.net/evilcode

本文地址:http://www.rosoo.net/a/201108/14882.html


更多相关文章

  1. Android(安卓)Display架构分析--侧重高通平台
  2. android usb host 读写USB设备
  3. Android(安卓)Shell 常用命令
  4. 在android tegra2中添加一个camera
  5. Android(安卓)数据存储ContentProvider(类容提供者)之对外提供数据
  6. Android如何判断当前字符串的组成情况(如:全由数字组成)
  7. Android(安卓)Studio 配置使用GreenDao3.0
  8. android横屏竖屏 实现
  9. Android(安卓)设备连接电脑显示offline

随机推荐

  1. android Inflater
  2. Android自定义组件系列【11】——实现3D
  3. [置顶] android仿最新版本微信相册--附源
  4. Android(安卓)获取、移除 View 的 OnClic
  5. Android中使用SerialPort读取红外温度传
  6. Gradle、 Gradle Android(安卓)Plugin 、
  7. Android(安卓)studio gradle系列二.
  8. ubuntu11.0.4下编译Android
  9. Android(安卓)SystemUI之启动流程(一)
  10. Webview缓存处理