Linux 应用基础 Framebuffer应用编程


前言

在 Linux应用基础中,Framebuffer 驱动程序主要用来控制 LCD。今天带大家来了解一下如何通过 Framebuffer 来编程 LCD。 在此之前,我们要先了解一下 Framebuffer 和 LCD。


一、了解Framebuffer

Frame是帧的意思,buffer是缓冲的意思,这意味着 Framebuffer 就是一块内存,里面保存着一帧图像。Framebuffer 中保存着一帧图像的每一个像素颜色值,假设LCD的分辨率是1024x768,每一个像素的颜色用32位来表示,那么Framebuffer的大小就是:1024x768x32/8=3145728字节。

二、了解LCD

1.LCD的操作原理

① 根据LCD分辨率、BPP分配Framebuffer ② APP使用ioctl获得LCD分辨率、BPP ③ APP通过mmap映射Framebuffer,在Framebuffer中写入数据

2.LCD坐标

Linux 应用基础 Framebuffer应用编程_打开文件

假设 fb_base 是APP执行 mmap 后得到的Framebuffer地址,可以用以下公式算出(x,y)坐标处像素对应的Framebuffer地址:

(x,y)像素起始地址=fb_base + (xres*bpp/8) * y + x * bpp / 8

三. Framebuffer 程序分析

1. 打开设备:(open)

open (const char pathname, int flags) ;pathname: 表示打开文件的路径。

73      fd_fb = open("/dev/fb0", O_RDWR);
74      if (fd_fb < 0)
75      {
76              printf("can't open /dev/fb0\n");
77              return -1;
78      }

fd_fb: 打开 LCD 设备返回的文件句柄。 /dev/fb0 :LCD的设备节点。 Flags表示打开文件的方式。

2. 获取LCD参数 : ( ioctl )

ioctl ( int fd, unsigned long request, ...);fd : 文件句柄。

12 static struct fb_var_screeninfo var; /* Current var */
……
79      if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &var))
80      {
81              printf("can't get var\n");
82              return -1;
83      }

FBIOGET_VSCREENINFO : 它表示get var screen info,获得屏幕的可变信息. 获取 lcd 屏幕的信息,并将其存放在 var 结构体中。以下是 var 结构体:

struct fb_var_screeninfo {
	__u32 xres;			/* visible resolution		*/
	__u32 yres;
	__u32 xres_virtual;		/* virtual resolution		*/
	__u32 yres_virtual;
	__u32 xoffset;			/* offset from virtual to visible */
	__u32 yoffset;			/* resolution			*/

	__u32 bits_per_pixel;		/* guess what			*/
	__u32 grayscale;		/* 0 = color, 1 = grayscale,	*/
					/* >1 = FOURCC			*/
	struct fb_bitfield red;		/* bitfield in fb mem if true color, */
	struct fb_bitfield green;	/* else only length is significant */
	struct fb_bitfield blue;
	struct fb_bitfield transp;	/* transparency			*/	

	__u32 nonstd;			/* != 0 Non standard pixel format */

	__u32 activate;			/* see FB_ACTIVATE_*		*/

	__u32 height;			/* height of picture in mm    */
	__u32 width;			/* width of picture in mm     */

	__u32 accel_flags;		/* (OBSOLETE) see fb_info.flags */

	/* Timing: All values in pixclocks, except pixclock (of course) */
	__u32 pixclock;			/* pixel clock in ps (pico seconds) */
	__u32 left_margin;		/* time from sync to picture	*/
	__u32 right_margin;		/* time from picture to sync	*/
	__u32 upper_margin;		/* time from sync to picture	*/
	__u32 lower_margin;
	__u32 hsync_len;		/* length of horizontal sync	*/
	__u32 vsync_len;		/* length of vertical sync	*/
	__u32 sync;			/* see FB_SYNC_*		*/
	__u32 vmode;			/* see FB_VMODE_*		*/
	__u32 rotate;			/* angle we rotate counter clockwise */
	__u32 colorspace;		/* colorspace for FOURCC-based modes */
	__u32 reserved[4];		/* Reserved for future compatibility */
};

3. 映射 framebuffer: ( mmap )

void *mmap ( void *addr, size_t length, int prot, int flags, int fd, off_t offset);

要映射一块内存,需要知道它的地址──这由驱动程序来设置,需要知道它的大小──这由应用程序决定。

85      line_width  = var.xres * var.bits_per_pixel / 8;
86      pixel_width = var.bits_per_pixel / 8;
87      screen_size = var.xres * var.yres * var.bits_per_pixel / 8;
88      fb_base = (unsigned char *)mmap(NULL , screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0);
89      if (fb_base == (unsigned char *)-1)
90      {
91              printf("can't mmap\n");
92              return -1;
93      }

screen_size : 代表整个 framebuffer 的大小。 fb_base : framebuffer 是存在于驱动程序中的,在应用程序中无法使用。则需要使用 mmap 将驱动程序的内存映射到应用程序中。


四. 描点函数: ( lcd_put_pixel )

大家在 lcd 上显示字符时,需要进行描点。pen_8,pen_16,pen_32都是起笔点。不同的只需要将pen_8变换一下即可。

void lcd_put_pixel(int x, int y, unsigned int color)
{
	unsigned char *pen_8 = fb_base+y*line_width+x*pixel_width;
	unsigned short *pen_16;	
	unsigned int *pen_32;	

	unsigned int red, green, blue;	

	pen_16 = (unsigned short *)pen_8;
	pen_32 = (unsigned int *)pen_8;

	switch (var.bits_per_pixel)
	{
		case 8:
		{
			*pen_8 = color;
			break;
		}
		case 16:
		{
			/* 565 */
			red   = (color >> 16) & 0xff;
			green = (color >> 8) & 0xff;
			blue  = (color >> 0) & 0xff;
			color = ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3);
			*pen_16 = color;
			break;
		}
		case 32:
		{
			*pen_32 = color;
			break;
		}
		default:
		{
			printf("can't surport %dbpp\n", var.bits_per_pixel);
			break;
		}
	}
}

总结

到此我们对 framebuffer 有了一定的了解。有不懂的欢迎留言,大家一起讨论。