
帧缓冲(framebuffer)是Linux为显示设备提供的一个接口,把显存抽象后的一种设备,他允许上层应用程序在图形模式下直接对显示缓冲区进行读写操作。framebuffer是LCD对应的一中HAL(硬件抽象层),提供抽象的,统一的接口操作,用户不必关心硬件层是怎么实施的。这些都是由Framebuffer设备驱动来完成的。
帧缓冲设备对应的设备文件为/dev/fb*,如果系统有多个显示卡,Linux下还可支持多个帧缓冲设备,最多可达32个,分别为/dev/fb0到 /dev/fb31,而/dev/fb则为当前缺省的帧缓冲设备,通常指向/dev/fb0,在嵌入式系统中支持一个显示设备就够了。帧缓冲设备为标准字符设备,主设备号为29,次设备号则从0到31。分别对应/dev/fb0-/dev/fb31。
通过/dev/fb,应用程序的操作主要有这几种:
1. 读/写(read/write)/dev/fb:相当于读/写屏幕缓冲区。
2. 映射(map)操作:由于Linux工作在保护模式,每个应用程序都有自己的虚拟地址空间,在应用程序中是不能直接访问物理缓冲区地址的。而帧缓冲设备可以通过mmap()映射操作将屏幕缓冲区的物理地址映射到用户空间的一段虚拟地址上,然后用户就可以通过读写这段虚拟地址访问屏幕缓冲区,在屏幕上绘图了。
3. I/O控制:对于帧缓冲设备,对设备文件的ioctl操作可读取/设置显示设备及屏幕的参数,如分辨率,屏幕大小等相关参数。ioctl的操作是由底层的驱动程序来完成的。
在应用程序中,操作/dev/fb的一般步骤如下:
1. 打开/dev/fb设备文件。
2. 用ioctl操作取得当前显示屏幕的参数,根据屏幕参数可计算屏幕缓冲区的大小。
3. 将屏幕缓冲区映射到用户空间。
4. 映射后即可直接读写屏幕缓冲区,进行绘图和图片显示。
framebuffer相关数据结构介绍
1. fb_info结构体:帧缓冲设备中最重要的数据结构体,包括了帧缓冲设备属性和操作的完整性属性。
2. fb_ops结构体:fb_info结构体的成员变量,fb_ops为指向底层操作的函数的指针。
3.fb_var_screen和fb_fix_screen结构体:fb_var_screen记录用户可以修改的显示控制器参数,fb_fix_screen记录用户不能修改的显示控制器参数。
以下代码使用framebuffer显示一张图片:
[cpp] view plaincopy
1.#include 2.#include 3.#include 4.#include 5.#include 6.#include 7.#include 8.#include 9.#include 10. 11. 12.//14byte文件头 13.typedef struct 14.{ 15. char cfType[2];//文件类型,"BM"(0x4D42) 16. long cfSize;//文件大小(字节) 17. long cfReserved;//保留,值为0 18. long cfoffBits;//数据区相对于文件头的偏移量(字节) 19.}__attribute__((packed)) BITMAPFILEHEADER; 20.//__attribute__((packed))的作用是告诉编译器取消结构在编译过程中的优化对齐 21. 22.//40byte信息头 23.typedef struct 24.{ 25. char ciSize[4];//BITMAPFILEHEADER所占的字节数 26. long ciWidth;//宽度 27. long ciHeight;//高度 28. char ciPlanes[2];//目标设备的位平面数,值为1 29. int ciBitCount;//每个像素的位数 30. char ciCompress[4];//压缩说明 31. char ciSizeImage[4];//用字节表示的图像大小,该数据必须是4的倍数 32. char ciXPelsPerMeter[4];//目标设备的水平像素数/米 33. char ciYPelsPerMeter[4];//目标设备的垂直像素数/米 34. char ciClrUsed[4]; //位图使用调色板的颜色数 35. char ciClrImportant[4]; //指定重要的颜色数,当该域的值等于颜色数时(或者等于0时),表示所有颜色都一样重要 36.}__attribute__((packed)) BITMAPINFOHEADER; 37. 38.typedef struct 39.{ 40. unsigned short blue; 41. unsigned short green; 42. unsigned short red; 43. unsigned short reserved; 44.}__attribute__((packed)) PIXEL;//颜色模式RGB 45. 46.BITMAPFILEHEADER FileHead; 47.BITMAPINFOHEADER InfoHead; 48. 49.static char *fbp = 0; 50.static int xres = 0; 51.static int yres = 0; 52.static int bits_per_pixel = 0; 53. 54.int show_bmp(); 55. 56.int main ( int argc, char *argv[] ) 57.{ 58. int fbfd = 0; 59. struct fb_var_screeninfo vinfo; 60. struct fb_fix_screeninfo finfo; 61. long int screensize = 0; 62. struct fb_bitfield red; 63. struct fb_bitfield green; . struct fb_bitfield blue; 65. 66. //打开显示设备 67. fbfd = open("/dev/fb0", O_RDWR); 68. if (!fbfd) 69. { 70. printf("Error: cannot open framebuffer device.\\n"); 71. exit(1); 72. } 73. 74. if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) 75. { 76. printf("Error:reading fixed information.\\n"); 77. exit(2); 78. } 79. 80. if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) 81. { 82. printf("Error: reading variable information.\\n"); 83. exit(3); 84. } 85. 86. printf("R:%d,G:%d,B:%d \\n", vinfo.red, vinfo.green, vinfo.blue ); 87. 88. printf("%dx%d, %dbpp\\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel ); . xres = vinfo.xres; 90. yres = vinfo.yres; 91. bits_per_pixel = vinfo.bits_per_pixel; 92. 93. //计算屏幕的总大小(字节) 94. screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8; 95. printf("screensize=%d byte\\n",screensize); 96. 97. //对象映射 98. fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0); 99. if ((int)fbp == -1) 100. { 101. printf("Error: failed to map framebuffer device to memory.\\n"); 102. exit(4); 103. } 104. 105. printf("sizeof file header=%d\\n", sizeof(BITMAPFILEHEADER)); 106. 107. printf("into show_bmp function\\n"); 108. 109. //显示图像 110. show_bmp(); 111. 112. //删除对象映射 113. munmap(fbp, screensize); 114. close(fbfd); 115. return 0; 116.} 117. 118.int show_bmp() 119.{ 120. FILE *fp; 121. int rc; 122. int line_x, line_y; 123. long int location = 0, BytesPerLine = 0; 124. char tmp[1024*10]; 125. 126. fp = fopen( , "rb" ); 127. if (fp == NULL) 128. { 129. return( -1 ); 130. } 131. 132. rc = fread( &FileHead, sizeof(BITMAPFILEHEADER),1, fp ); 133. if ( rc != 1) 134. { 135. printf("read header error!\\n"); 136. fclose( fp ); 137. return( -2 ); 138. } 139. 140. //检测是否是bmp图像 141. if (memcmp(FileHead.cfType, "BM", 2) != 0) 142. { 143. printf("it's not a BMP file\\n"); 144. fclose( fp ); 145. return( -3 ); 146. } 147. 148. rc = fread( (char *)&InfoHead, sizeof(BITMAPINFOHEADER),1, fp ); 149. if ( rc != 1) 150. { 151. printf("read infoheader error!\\n"); 152. fclose( fp ); 153. return( -4 ); 154. } 155. 156. //跳转的数据区 157. fseek(fp, FileHead.cfoffBits, SEEK_SET); 158. //每行字节数 159. BytesPerLine = (InfoHead.ciWidth * InfoHead.ciBitCount + 31) / 32 * 4; 160. 161. line_x = line_y = 0; 162. //向framebuffer中写BMP图片 163. while(!feof(fp)) 1. { 165. PIXEL pix; 166. unsigned short int tmp; 167. rc = fread( (char *)&pix, 1, sizeof(PIXEL), fp); 168. if (rc != sizeof(PIXEL)) 169. break; 170. location = line_x * bits_per_pixel / 8 + (InfoHead.ciHeight - line_y - 1) * xres * bits_per_pixel / 8; 171. 172. //显示每一个像素 173. *(fbp + location + 0)=pix.blue; 174. *(fbp + location + 1)=pix.green; 175. *(fbp + location + 2)=pix.red; 176. *(fbp + location + 3)=pix.reserved; 177. 178. line_x++; 179. if (line_x == InfoHead.ciWidth ) 180. { 181. line_x = 0; 182. line_y++; 183. if(line_y == InfoHead.ciHeight) 184. break; 185. } 186. } 187. fclose( fp ); 188. return( 0 ); 1.} 注意:上面的程序只在framebuffer上显示图片,却没有删除刷新屏幕,可以使用下面的命令恢复屏幕 保存屏幕信息:dd if=/dev/fb0 of=fbfile 或: cp /dev/fb0 fbfile 恢复屏幕信息:dd if=fbfile of=/dev/fb0 或: cat fbfile > /dev/fb0
