android清空frame,Android操作framebuffer

操作framebuffer的主要步骤如下:

1、打开一个可用的FrameBuffer设备;

2、通过mmap调用把显卡的物理内存空间映射到用户空间;

3、更改内存空间里的像素数据并显示;

4、退出时关闭framebuffer设备。

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

#include

#include

#include

#include

#include

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;

structfb_var_screeninfo vinfo;

structfb_fix_screeninfo finfo;

long intscreensize = 0;

char *fbp = 0;

int x = 0, y= 0;

intguage_height = 20, step = 10;

long intlocation = 0;

// Openthe file for reading and writing

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

if (!fbfd){

printf("Error: cannot open framebuffer device./n");

exit(1);

}

printf("Theframebuffer device was opened successfully./n");

// Getfixed screen information

if(ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {

printf("Error reading fixed information./n");

exit(2);

}

// Getvariable 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 );

// Figureout the size of the screen in bytes

screensize =vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;;

// Mapthe 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 tomemory./n");

exit(4);

}

printf("Theframebuffer device was mapped to memory successfully./n");

//set to black color first

memset(fbp, 0, screensize);

//drawrectangle

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;

}

// Figureout where in memory to put the pixel

for ( x =step; x < vinfo.xres - step; x++ ) {

for ( 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 lotof 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);

}

//cleanframebuffer

munmap(fbp,screensize);

close(fbfd);

return0;

}

注意,在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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值