之前在mini2440对lcd的操作,比如在屏上显示红色,但是将代码直接copy过来,发现在android机器上不能直接显示,后来查阅资料,在android中framebuffer有双缓冲机制,一个缓冲区用来显示,另外一个缓冲区用来绘制,这样显示的那块缓冲区永远都是准备好的数据,就不会存在比如花屏等现象。
好了,直接来看代码(需要进入工厂模式)。
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>
#include <linux/fb.h>
void show(char *fb, int width, int height)
{
int x, y;
unsigned long *p = (unsigned long *)fb;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
*p++ = 0x000000ff;
}
}
}
int main(void)
{
int fd;
struct fb_var_screeninfo var;
struct fb_fix_screeninfo fix;
int size;
char *fb;
printf("framebuffer test\n");
fd = open("/dev/graphics/fb0", O_RDWR);
if (fd < 0) {
printf("can't open fb0!\n");
return -1;
}
memset(&var, 0, sizeof(struct fb_var_screeninfo));
ioctl(fd, FBIOGET_VSCREENINFO, &var);
printf("xres %d yres %d bits_per_pixel %d\n", var.xres, var.yres,
var.bits_per_pixel);
memset(&fix, 0, sizeof(struct fb_fix_screeninfo));
ioctl(fd, FBIOGET_FSCREENINFO, &fix);
printf("smem_start %lx smem_len %d line_length %d\n", fix.smem_start,
fix.smem_len, fix.line_length);
size = fix.smem_len;
fb = (char *)mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
var.yres_virtual = var.yres * 2;
var.yoffset = 0 * var.yres; /* 0: front 1: back */
ioctl(fd, FBIOPUT_VSCREENINFO, &var);
show(fb, var.xres, var.yres);
munmap(fb, size);
close(fd);
return 0;
}
需要注意fb_var_screeninfo中的xres_virtual、yres_virtual、yoffset、xoffset,xres_virtual、yres_virtual即framebuffer的虚拟分辨率,要大于或等于屏实际的分辨率,看代码,如果是双缓冲区,那么yres_virtual需要设置成yres的2倍,然后是xoffset、yoffset,即framebuffer中的偏移,如果yoffset为0,那就使用的第一个缓冲区数据,如果yoffset为yres,那就是用的第二个缓冲区数据。需要注意的是屏显示的第一个缓冲区中的数据,所以程序中将yoffset设置为了0,这样屏才会正确的显示出我们所写的数据。最后需要调用ioctl命令将fb_var_screeninfo数据回写到kernel中。
全文完。