framebuffer应用编程实践

时间:2023-03-09 07:50:06
framebuffer应用编程实践

framebuffer的使用主要包括4个部分:

(1):首先需要打开设备文件 /dev/fb0。

(2):获取设备的信息。包括可变信息和不可变信息,分别使用两个结构体来进行封装,这两个结构体在 <linux/fb.h> 头文件中定义,所以需要先包含这个头文件。

(3):如果有需要可以对可变的参数进行修改。

(4):做mmap映射。我们需要将驱动中给LCD分配的显存空间映射到我们的应用层来,这样才能在应用层对显存进行操作。

(5):填充framebuffer。也就是上面说的操作显存。

数据结构: (include\linux\fb.h)

struct  fb_fix_screeninfo:(不可变信息)

 struct fb_fix_screeninfo {
char id[]; // 标识字符串
unsigned long smem_start; // FB显存的起始地址(物理地址) __u32 smem_len; // FB显存的长度
__u32 type; /* see FB_TYPE_* */
__u32 type_aux;
__u32 visual; /* see FB_VISUAL_* */
__u16 xpanstep; /* zero if no hardware panning */
__u16 ypanstep; /* zero if no hardware panning */
__u16 ywrapstep; /* zero if no hardware ywrap */
__u32 line_length; // 一行的长度(以字节为单位)
unsigned long mmio_start; /* Start of Memory Mapped I/O */
/* (physical address) */
__u32 mmio_len; /* Length of Memory Mapped I/O */
__u32 accel; /* Indicate to driver which */
/* specific chip/card we have */
__u16 reserved[]; /* Reserved for future compatibility */
};

struct  fb_var_screeninfo:(可变信息)

 struct fb_var_screeninfo {
__u32 xres; // LCD的水平像素大小
__u32 yres; // LCD的垂直像素大小
__u32 xres_virtual; // LCD的虚拟水平像素大小
__u32 yres_virtual; // LCD的虚拟垂直像素大小
__u32 xoffset; // 水平像素偏移量
__u32 yoffset; // 垂直像素偏移量 __u32 bits_per_pixel; // 像素深度bpp
__u32 grayscale; /* != 0 Graylevels instead of colors */ 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; // LCD的物理高度 mm
__u32 width; // LCD的物理宽度 mm __u32 accel_flags; /* (OBSOLETE) see fb_info.flags */ __u32 pixclock; // 像素时钟 /* 下面是六个时序参数 */
__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 reserved[]; /* Reserved for future compatibility */
};

相关宏定义: (include\linux\fb.h)

#define FBIOGET_VSCREENINFO 0x4600      // 传给ioctl函数,用来获取可变参数信息,返回的是一个fb_var_screeninfo结构体
#define FBIOPUT_VSCREENINFO 0x4601      // 传给ioctl函数,用来设置可变参数,需要传入一个fb_var_screeninfo结构体,
#define FBIOGET_FSCREENINFO 0x4602      // 传给ioctl函数,用来获取不可变信息,返回的是一个fb_fix_screeninfo结构体

/*************************************************************************************************************/

测试代码:

平台:s5pv210

内核版本:2.6.35.7

/*************************************************************************************************************/

 #include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/ioctl.h>
#include <sys/mman.h> #define FILE "/dev/fb0" // 设备文件 /*计算机颜色16进制表示符*/
#define WHITE 0xFFFFFF //白色
#define RED 0xFF0000 //红色
#define GREEN 0x00FF00 //绿色
#define BLUE 0x0000FF //蓝色
#define YELLOW 0xFFFF00 //黄色
#define BLACK 0x000000 //黑色
#define AQNA 0xAFDFE4 //水色
#define NAVE 0x23238E //海军蓝
#define ORANGE 0xFF7F00 //橙色
#define PURPLE 0x871F78 //紫色
#define Qioke 0x6B4226 //巧克力色 struct fb_fix_screeninfo finfo = {}; // 不可变信息结构体
struct fb_var_screeninfo vinfo = {}; // 可变信息结构体
static volatile unsigned int *pMap = NULL; // 用来指向mmap映射得到的虚拟地址 static inline void lcd_draw_pixel(unsigned int x, unsigned int y, unsigned int color);
static void lcd_draw_background(unsigned int color);
void lcd_draw_lline(const unsigned int x, const unsigned int y, const unsigned int length, // 注意这个函数参数太多了,不应该这样设计,我们应该把这些参数放在一个结构体中,把结构体变量的指针传进来即可,这样效率高
const unsigned int width, const unsigned int color); static void lcd_draw_image(const unsigned char *pData); // pData是一个图片的数据数组: 这里是 1024*600 像素24位真彩色格式的图片,,数组的元素个数: 1024*600*3 我们需要将连续的3个1个字节长度的数据合成一个24位数据 int main(void)
{
int fd = ; /* 打开文件得到文件描述符 */
fd = open(FILE, O_RDWR);
if ( > fd) {
perror("open error");
return -;
}
printf("%s 打开成功\n", FILE); /* 操作文件 */
if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo)) {
perror("ioctl error");
close(fd);
return -;
} if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo)) {
perror("ioctl error");
close(fd);
return -;
} // 打印信息
printf("不可变信息smem_start = 0x%x\n", finfo.smem_start);
printf("不可变信息smem_len = %ld\n", finfo.smem_len);
printf("可变信息xres = %d, yres = %d\n", vinfo.xres, vinfo.yres);
printf("可变信息xres_virtual = %d, yres_virtual = %d\n", vinfo.xres_virtual, vinfo.yres_virtual);
printf("可变信息xoffset = %d, yoffset = %d\n", vinfo.xoffset, vinfo.yoffset); /* 进行mmap映射 */
pMap = mmap(NULL, finfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, );
if (NULL == pMap) {
perror("mmap error");
return -;
} /* 背景填充 */
lcd_draw_background(WHITE); /* 关闭文件 */
close(fd); return ;
} /*填充像素点*/
static inline void lcd_draw_pixel(unsigned int x, unsigned int y, unsigned int color)
{
*(unsigned int *)((unsigned int)pMap + (vinfo.xres*x + y)*) = color;
} /*填充LCD背景*/
static void lcd_draw_background(unsigned int color)
{
unsigned int i = ;
unsigned int j = ; for (i = ; i <= vinfo.yres; ++i)
{
for (j = ; j <= vinfo.xres; ++j)
lcd_draw_pixel(i, j, color);
}
} // 画线函数
void lcd_draw_lline(const unsigned int x, const unsigned int y, const unsigned int length,
const unsigned int width, const unsigned int color)
{
volatile unsigned int i = ;
volatile unsigned int j = ; for (i = x; i < width+x; i++)
{
for (j = y; j < length+y; j++)
{
lcd_draw_pixel(i, j, color);
}
}
} // 24位真彩色图片显示函数
static void lcd_draw_image(const unsigned char *pData)
{
unsigned int i = ;
unsigned int j = ;
unsigned int color = ;
unsigned int p = ; for (i = ; i < vinfo.yres; i++)
{
for (j = ; j < vinfo.xres; j++)
{
color = (pData[p+] << ) | (pData[p+] << ) | (pData[p+] << );
lcd_draw_pixel(i, j, color);
p = p+;
}
}
}