科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件与afreez一起学习DirectFB之:一个linux下的framebuffer例子的学问

与afreez一起学习DirectFB之:一个linux下的framebuffer例子的学问

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

通过一个一个framebuffer例子,复习了内存分配的应用。

作者:聂飞 来源:CSDN 2008年3月27日

关键字: framebuffer Linux DirectFB 开源

  • 评论
  • 分享微博
  • 分享邮件

头文件

fbtools.h


#ifndef _FBTOOLS_H_

#define _FBTOOLS_H_

 

#include <linux/fb.h>

 

//a framebuffer device structure;

typedef struct fbdev{

               int fb;

               unsigned long fb_mem_offset;

               unsigned long fb_mem;

               struct fb_fix_screeninfo fb_fix;

               struct fb_var_screeninfo fb_var;

               char dev[20];

} FBDEV, *PFBDEV;

 

//open & init a frame buffer

//to use this function,

//you must set FBDEV.dev="/dev/fb0"

//or "/dev/fbX"

//it's your frame buffer.

int fb_open(PFBDEV pFbdev);

 

//close a frame buffer

int fb_close(PFBDEV pFbdev);

 

//get display depth

int get_display_depth(PFBDEV pFbdev);

 

 

//full screen clear

void fb_memset(void *addr, int c, size_t len);

 

#endif


 

测试文件,其中深颜色的注释部分为在我机器上测得的结果

fbtools.c

代码:


 

#include <stdio.h>

#include <stdlib.h>

#include <fcntl.h>

#include <unistd.h>

#include <string.h>

#include <sys/ioctl.h>

#include <sys/mman.h>

#include <asm/page.h>

 

#include "fbtools.h"

 

#define TRUE          1

#define FALSE         0

#define MAX(x,y)        ((x)>(y)?(x):(y))

#define MIN(x,y)        ((x)<(y)?(x):(y))

 

//open & init a frame buffer

int fb_open(PFBDEV pFbdev)

{

               pFbdev->fb = open(pFbdev->dev, O_RDWR);// pFbdev->fb==3

               if(pFbdev->fb < 0)

               {

                               printf("Error opening %s: %m. Check kernel config\n", pFbdev->dev);

                               return FALSE;

               }

               if (-1 == ioctl(pFbdev->fb,FBIOGET_VSCREENINFO,&(pFbdev->fb_var)))

               {

                               printf("ioctl FBIOGET_VSCREENINFO\n");

                               return FALSE;

               }

               if (-1 == ioctl(pFbdev->fb,FBIOGET_FSCREENINFO,&(pFbdev->fb_fix)))

               {

                               printf("ioctl FBIOGET_FSCREENINFO\n");

                               return FALSE;

               }

              

               //map physics address to virtual address

               // pFbdev->fb_fix.smem_start=f0000000

               pFbdev->fb_mem_offset = (unsigned long)(pFbdev->fb_fix.smem_start) & (~PAGE_MASK);

// pFbdev->fb_fix.smem_len=100 0000 pFbdev->fb_mem_offset=0

// pFbdev->fb_mem =0

               pFbdev->fb_mem = (unsigned long int)mmap(NULL, pFbdev->fb_fix.smem_len +

                                          pFbdev->fb_mem_offset,

                               PROT_READ | PROT_WRITE, MAP_SHARED, pFbdev->fb, 0);

               if (-1L == (long) pFbdev->fb_mem)

               {

                               printf("mmap error! mem:%d offset:%d\n", pFbdev->fb_mem,

                                                      pFbdev->fb_mem_offset);

                               return FALSE;

               }

              

               return TRUE;

}

 

//close frame buffer

int fb_close(PFBDEV pFbdev)

{

               close(pFbdev->fb);

               pFbdev->fb=-1;

}

 

//get display depth

int get_display_depth(PFBDEV pFbdev);

{

               if(pFbdev->fb<=0)

               {

                               printf("fb device not open, open it first\n");

                               return FALSE;

               }

               return pFbdev->fb_var.bits_per_pixel;

}

 

//full screen clear

void fb_memset (void *addr, int c, size_t len)

{

    memset(addr, c, len);

}

 

//use by test

#define DEBUG

#ifdef DEBUG

main()

{

               FBDEV fbdev;

               memset(&fbdev, 0, sizeof(FBDEV));

               strcpy(fbdev.dev, "/dev/fb0");

               if(fb_open(&fbdev)==FALSE)

               {

                               printf("open frame buffer error\n");

                               return;

               }

               //注意,下面一行有bug

               fb_memset(fbdev.fb_mem + fbdev.fb_mem_offset, 0, fbdev.fb_fix.smem_len);

              

               fb_close(&fbdev);

}

#endif


编译

如果对上述代码直接进行编译的话,是不能成功的,即会出现类似下面的编译错误

#gcc –o fbtools fbtools.c

fbtools.c: In function `main`

fbtools.c:89:warning:passing arg 1 of `fb_memset` makes pointer from integer without a cast

 

对有问题的fbtools.c中的第89行代码(即加粗的有注释的那一行)进行如下操作

修改为:

fb_memset((void *)(fbdev.fb_mem+fbdev.fb_mem_offset), 0, fbdev.fb_fix.smem_len);

或者

unsigned long temp;

temp= fbdev.fb_mem+fbdev.fb_mem_offset;

fb_memset((void *)temp, 0, fbdev.fb_fix.smem_len);

 

可以成功编译成功

 

而修改为:

fb_memset((&)(fbdev.fb_mem+fbdev.fb_mem_offset), 0, fbdev.fb_fix.smem_len);

或者

unsigned long temp;

temp= fbdev.fb_mem+fbdev.fb_mem_offset;

fb_memset((&)temp, 0, fbdev.fb_fix.smem_len);

 

会输出:段错误

 

分析

函数原形为:void fb_memset (void *addr, int c, size_t len)

fbtools.c:89调用时传递的参数为:

fb_memset(fbdev.fb_mem+fbdev.fb_mem_offset, 0, fbdev.fb_fix.smem_len);

fbdev.fb_memfbdev.fb_mem_offset都是unsigned long类型的变量,它们的计算结果保存在一个临时的栈空间,传递调用时,其临时的地址是不能够传递到被调用的函数的,所以编译是同不过的。具体的可以参考内存分配的相关知识,记得《effective c++》一书里讲的很详细,可以参考。

至于修改后的:

fb_memset((&)temp, 0, fbdev.fb_fix.smem_len);

编译出现段错误,也是很好理解的,因为(&)temp不等于(void *)temp,也不等于

(void *)(fbdev.fb_mem+fbdev.fb_mem_offset),具体原因读者可以对照《effective c++》思考。

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章