linux直接寫framebuffer linux 直接 對 Frame Buffer 操作,寫畫面快取例子,c語言讀寫framebuffer
程序實現直接寫屏。
在繼續下面的之前,先說明幾個背景知識:
1、FrameBuffer主要是根據VESA標準的實現的,所以只能實現最簡單的功能。
2、由於涉及核心的問題,FrameBuffer是不允許在系統起來後修改顯示模式等一系
列操作。(好象很多人都想要這樣幹,這是不被允許的,當然如果你自己與驅動
的話,是可以實現的)
3、對FrameBuffer的操作,會直接影響到本機的所有控制檯的輸出,包括XWIN的圖
形介面。
好,現在可以讓我們開始實現直接寫屏:
1、開啟一個FrameBuffer裝置
2、通過mmap呼叫把顯示卡的實體記憶體空間對映到使用者空間
3、直接寫記憶體。
//////////////////////////////////////////////////////////////////////////////////////
//這個程式是可以用的,在我的開發板上面編譯通過,顯示彩色
/////////////////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <time.h>
#include <sys/mman.h>
int main()
{
int fbfd = 0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
long int screensize = 0;
char *fbp = 0;
int x = 0, y = 0;
long int location = 0;
fbfd = open("/dev/fb0", O_RDWR);// Open the file for reading and writing
if (!fbfd)
{
printf("Error: cannot open framebuffer device.\n");
exit(0);
}
printf("The framebuffer device was opened successfully.\n");
// Get fixed screen information
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo))
{
printf("Error reading fixed information.\n");
exit(0);
}
// Get variable screen information
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo))
{
printf("Error reading variable information.\n");
exit(0);
}
printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
// Figure out the size of the screen in bytes
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
// Map the 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 to memory.\n");
exit(0);
}
printf("The framebuffer device was mapped to memory successfully.\n");
x = 100; y = 100;
// Where we are going to put the pixel
// Figure out where in memory to put the pixel
for (y = 100; y < 300; y++)
for (x = 100; x < 300; x++)
{
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
{
int b = 10;//assume 16bpp
int g = (x-100)/6;// A little green
int r = 31-(y-100)/16;// A lot of red
unsigned short int t = r<<11 | g << 5 | b;
*((unsigned short int*)(fbp + location)) = t;
}
}
munmap(fbp, screensize);
close(fbfd);
return 0;
}