linux直接写framebuffer linux 直接 对 Frame Buffer 操作,写画面缓存例子,c语言读写framebuffer

提供的中断调用来实现直接写屏,故Linux抽象出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;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值