Framebuffer编程

来源:互联网 发布:sshd 知乎 编辑:程序博客网 时间:2024/05/22 06:05
         应用程序需要知道Framebuffer设备的相关参数,必须通过ioctl()系统调用来完成。
在头文件中定义了所有的ioctl命令字,不过,最常用的ioctl命令字是下面这两个:FBIOGET_FSCREENINFO和FBIOGET_VSCREENINFO。
前者返回与Framebuffer有关的固定的信息,比如图形硬件上实际的帧缓存空间的大小、能否硬件加速等信息。
而后者返回的是与Framebuffer有关的可变信息。可变的信息就是指Framebuffer的长度、宽度以及颜色深度等信息。

控制framebuffer设备的一般步骤如下:
1) 打开设备,映射framebuffer
2)依照硬件要求,准备好数据
3)把数据复制到framebuffer

打开设备,映射framebuffe
 
   static void*fbbuf;  
 
   intopenfb(char *devname)  
 
  
 
      int fd;  
 
      fd = open(devname, O_RDWR);  
 
      if (ioctl(fd, FBIOGET_VSCREENINFO, &fbvar)< 0)  
 
          return -1;  
 
      bpp = fbvar.bits_per_pixel;  
 
      screen_size = fbvar.xres * fbvar.yres * bpp / 8; 
 
      fbbuf = mmap(0, screen_size, PROT_READ | PROT_WRITE, MAP_SHARED,fd, 0);  
 
      return fd;  
 
     

数据准备,假设lcd控制器被初始化为565,16bit格式的
 
   staticinline int  make_pixel(unsigned int a, unsignedint r, unsigned int g, unsigned int b)  
 
  
 
      return (unsignedint)(((r>>3)<<11)|((g>>2)<<5|(b>>3))); 
 
   }

把想要显示的数据复制到framebuffer
 
   static voidfill_pixel(unsigned int pixel, int x0, int y0, int w, int h) 
 
  
 
      int i, j;  
 
      unsigned short *pbuf = (unsigned short *)fbbuf; 
 
      for (i = y0; i < h; i ++) {  
 
          for (j = x0; j < w; j ++) {  
 
              pbuf[i * screen_width + j] = pixel;  
 
          }  
 
      }  
 
   }
以上就是Linux Framebuffer编程的过程。

#include<asm/types.h> 
        
#include<fcntl.h> 
            
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <linux/fb.h>
#include <sys/time.h>
static void *fbbuf;
unsigned int bpp;
unsigned int screen_size;
struct fb_var_screeninfo fbvar;
struct fb_fix_screeninfo finfo;
int openfb(char *devname)
{
 
   intfd;
 
   fd =open(devname, O_RDWR);
 
   if(ioctl(fd, FBIOGET_VSCREENINFO, &fbvar)< 0)
 
      return -1;
 
   printf("theclock is %d\n", fbvar.pixclock);
 
   bpp =fbvar.bits_per_pixel;
 
   screen_size= fbvar.xres * fbvar.yres * bpp / 8;
 
   fbbuf =mmap(0, screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,0);
 
   returnfd;
}
static inline int 
make_pixel(unsigned int a,unsigned int r, unsigned int g, unsigned int b)
{
 
   return(unsignedint)(((r>>3)<<11)|((g>>2)<<5|(b>>3)));
}
static void fill_pixel(unsigned int pixel, int x0, int y0, int w,int h)
{
 
   int i,j;
 
   unsignedshort *pbuf = (unsigned short *)fbbuf;
 
   for (i = y0;i < h; i ++) {
 
      for (j = x0; j < w; j ++) {
 
          pbuf[i * 240 + j] = pixel;
 
      }
 
   }
}
int main () {
 
   unsigned intr, g, b;
 
   intfp=0;
 
   unsigned intcount;

 
   r = 0;
 
   g = 0;
 
   b = 0;
 
   fp = openfb("/dev/fb0");

 
   if (fp< 0){
 
      printf("Error : Can not open framebuffer device\n");
 
      exit(1);
 
   }

 
   while(1){
 
      for (count = 0; count < 100; count++) {
 
          r = 0xff;
 
          fill_pixel(make_pixel(0, r, g, b), 0, 0, 240, 320);
 
      }
 
     for (count = 0; count < 100; count++) {
 
          r = 0x00;
 
          fill_pixel(make_pixel(0, r, g, b), 0, 0, 240, 320);
 
     }
 
   }
 
   return0;
}
0 0
原创粉丝点击