Chinaunix首页 | 论坛 | 博客
  • 博客访问: 533485
  • 博文数量: 174
  • 博客积分: 4177
  • 博客等级: 上校
  • 技术积分: 1827
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-15 14:12
文章分类

全部博文(174)

文章存档

2018年(1)

2017年(1)

2013年(3)

2012年(9)

2010年(12)

2009年(5)

2008年(106)

2007年(37)

我的朋友

分类: LINUX

2010-05-05 21:30:27

   要在虚拟机上测试frame buffer程序需要虚拟机上的linux进入字符界面,在图形界面是看不到相关的效果的。做法就是在grub.conf 中kernel 那一行加上参数vga=0x317这个可以参考documentation/fb/vesafb.txt中的相关描述

下面的代码是从网络上找到的:

 
 

 

#include<unistd.h>
#include<stdio.h>
#include<fcntl.h>
#include<linux/fb.h>
#include<sys/mman.h>


int main(int argc, char *argv[])
{
    int fbfd = 0;
    struct fb_var_screeninfo vinfo;
    struct fb_fix_screeninfo finfo;
    long int screensize = 0;
    char *fbp = NULL;
    int x = 0, y = 0;
    long int location = 0;

    fbfd = open("/dev/fb0",O_RDWR);
    if(fbfd <= 0)
    {
         printf("Error: cannot open framebuffer device\n");
         return -1;
    }

    printf("The framebuffer device was opened successfully\n");
    printf("The value of the file description is: %d\n",fbfd);

    if(ioctl(fbfd,FBIOGET_FSCREENINFO,&finfo))
    {
       printf("Error reading fixed information\n");
       return -1;
    }

   printf("The id of the framebuffer is: %s\nThe start of the fb is:%d\nThe length of the fb is:%d\nThe type of the fb is: %d\n",finfo.id,finfo.smem_start,finfo.smem_len,finfo.type);
    
   if(ioctl(fbfd,FBIOGET_VSCREENINFO,&vinfo))
   {
       printf("Error reading variable information\n");
       return -1;
   }
   printf("The visiual resolution is: %d * %d\nThe virtual resolution is: %d * %d\nThe offset of the point is:%d,%d\nThe bits per pixel value is: %d\n",vinfo.xres,vinfo.yres,vinfo.xres_virtual,vinfo.yres_virtual,vinfo.xoffset,vinfo.yoffset,vinfo.bits_per_pixel);

   screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
   fbp = (char *)mmap(NULL,screensize, PROT_READ|PROT_WRITE,MAP_SHARED,fbfd,0);
   if((int)fbp == -1)
   {
       printf("Error: failed to map framebuffer device to memory\n");
       return -4;
   }
   printf("The framebuffer device was mapped to memory successfully\n");

   x = 100;
   y = 100;

   for(y = 100; y < 200; y++)
   {
      for(x = 100; x< 200; 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;
             *(fbp + location + 1) = 15 + (x - 100) / 2;
             *(fbp + location + 2) = 200 -(y - 100) / 5;
             *(fbp + location + 3) = 0;
         }
         else
         {
             unsigned short b = 10;
             unsigned short g = (x - 100) / 6;
             unsigned short r = 31 -(y - 100) / 16;
             unsigned short t = r << 11 | g << 5 | b;
             *((unsigned short *)(fbp + location)) = t;
         }
    }
   }

   munmap(fbp, screensize);
   printf("The framebuffer device was munmapped to memory successfully\n");
   close(fbfd);
   
   printf("The framebuffer device was closed successfully\n");
   

   return 0;
}

 

下面这段代码也是从网络上找到用frame buffer画圆的程序,尚未测试

 


 

#include<stdio.h>
#include<unistd.h>
#include<sys/mman.h>
#include<sys/ioctl.h>
#include<fcntl.h>
#include<linux/fb.h>
#include<stdlib.h>
#include<string.h>

unsigned char *fbp;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;

void drawpoint(unsigned int x,unsigned int y,unsigned int *color)
{
    unsigned char *fbp0;
    fbp0=fbp;
    fbp0+=y*vinfo.xres*vinfo.bits_per_pixel/8+x*vinfo.bits_per_pixel/8;
    memcpy(fbp0,color,vinfo.bits_per_pixel/8);
}
void drawline(unsigned int x0,unsigned int y0,unsigned int x1,unsigned int y1,unsigned int *color)
{
    unsigned int x=x0;
    unsigned int y=y0;
    int s0,s1,s2;
    while(x!=x1||y!=y1)
    {
        drawpoint(x,y,color);
        if(x0>=x1&&y0>=y1)
        {
            s0=abs((x-x0-1)*(y0-y1)-(y-y0)*(x0-x1));
            s1=abs((x-x0)*(y0-y1)-(y-y0-1)*(x0-x1));
            s2=abs((x-x0-1)*(y0-y1)-(y-y0-1)*(x0-x1));
            s0<=s1?(s0<=s2?x=x-1:(x=x-1,y=y-1)):(s1<=s2?y=y-1:(x=x-1,y=y-1));
        }
        else if(x0>=x1&&y0<=y1)
        {
            s0=abs((x-x0-1)*(y0-y1)-(y-y0)*(x0-x1));
            s1=abs((x-x0)*(y0-y1)-(y-y0+1)*(x0-x1));
            s2=abs((x-x0-1)*(y0-y1)-(y-y0+1)*(x0-x1));
            s0<=s1?(s0<=s2?x=x-1:(x=x-1,y=y+1)):(s1<=s2?y=y+1:(x=x-1,y=y+1));
        }
        else if(x0<=x1&&y0>=y1)
        {
            s0=abs((x-x0+1)*(y0-y1)-(y-y0)*(x0-x1));
            s1=abs((x-x0)*(y0-y1)-(y-y0-1)*(x0-x1));
            s2=abs((x-x0+1)*(y0-y1)-(y-y0-1)*(x0-x1));
            s0<=s1?(s0<=s2?x=x+1:(x=x+1,y=y-1)):(s1<=s2?y=y-1:(x=x+1,y=y-1));
        }
        else if(x0<=x1&&y0<=y1)
        {
            s0=abs((x-x0+1)*(y0-y1)-(y-y0)*(x0-x1));
            s1=abs((x-x0)*(y0-y1)-(y-y0+1)*(x0-x1));
            s2=abs((x-x0+1)*(y0-y1)-(y-y0+1)*(x0-x1));
            s0<=s1?(s0<=s2?x=x+1:(x=x+1,y=y+1)):(s1<=s2?y=y+1:(x=x+1,y=y+1));
        }
    }
    drawpoint(x,y,color);
}
void drawcircle(unsigned int x0,unsigned int y0,unsigned int radius,unsigned int *color)
{
    unsigned int x,y,y1=0;
    int s0,s1,s2;
    int i=0;
    x=x0+radius+1;
    y=y0;
    do
    {
        s0=abs((x-x0-1)*(x-x0-1)+(y-y0)*(y-y0)-radius*radius);
        s1=abs((x-x0)*(x-x0)+(y-y0-1)*(y-y0-1)-radius*radius);
        s2=abs((x-x0-1)*(x-x0-1)+(y-y0-1)*(y-y0-1)-radius*radius);
        s0<=s1?(s0<=s2?x=x-1:(x=x-1,y=y-1)):(s1<=s2?y=y-1:(x=x-1,y=y-1));
        if((x!=2*x0-x)&&(y1!=y))
        { drawpoint(2*x0-x,y,color);
                drawpoint(x,2*y0-y,color);
            drawline(x-1,y,2*x0-x+1,y,color);
        }
        if((y!=2*y0-y)&&(y1!=y))
        { drawpoint(x,y,color);
            drawpoint(2*x0-x,2*y0-y,color);
            drawline(2*x0-x+1,2*y0-y,x-1,2*y0-y,color);
        }
        y1=y;
    }while((x!=x0)&&(y!=(y0-radius)));
    
}

int main()
{
    int fd;
    unsigned int i=0;
    unsigned int color,bgcolor;
    long int screensize;
    bgcolor=0;
    color=0x0000ff00;
    if((fd=open("/dev/fb0",O_RDWR))<0)
    {
        perror("fail to open");
        return 0;
    }
     if (ioctl(fd,FBIOGET_FSCREENINFO, &finfo))
     {
        perror("fail to get");
        return 0;
     }
    if (ioctl(fd,FBIOGET_VSCREENINFO, &vinfo))
     {
        perror("fail to get");
        return 0;
     }
    screensize=vinfo.xres*vinfo.yres*vinfo.bits_per_pixel/8;
    fbp=(unsigned char *)mmap(0,screensize,PROT_READ | PROT_WRITE,MAP_SHARED,fd, 0);
    for(i=0;i<screensize;i+=vinfo.bits_per_pixel/8)
          memcpy(fbp+i,&bgcolor,vinfo.bits_per_pixel/8);
    usleep(10000);
    drawcircle(400,400,300,&color);
    munmap(fbp,screensize);
    close(fd);
    return 0;
}


阅读(1073) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~