读取参数

涉及普通linuxfb应用层开发的,主要有2个参数:struct fb_var_screeninfostruct fb_fix_screeninfo。这2个结构体,以及其他一些相关结构体、宏定义,在/usr/include/linux/fb.h中定义。
可以通过如下程序,读取PC或者其他带fb设备的相关信息。

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


/*
 *
 */
int main (void) {
    int         fd_fb0;
    int         rtn;
    struct fb_fix_screeninfo fix;
    struct fb_var_screeninfo var;


    // open /dev/fb0
    fd_fb0 = open("/dev/fb0", O_RDWR, 0);
    if (fd_fb0 < 0) {
        printf("open err\n\r");
        return 1;
    }


    // get var screeninfo
    rtn = ioctl(fd_fb0, FBIOGET_VSCREENINFO, &var);
    if (rtn < 0) {
        printf("FBIOGET_VSCREENINFO err\n\r");
        close(fd_fb0);
        return 1;
    }


    // get fix screeninfo
    rtn = ioctl(fd_fb0, FBIOGET_FSCREENINFO, &fix);
    if (rtn < 0) {
        printf("FBIOGET_FSCREENINFO err\n\r");
        close(fd_fb0);
        return 1;
    }


    // close /dev/fb0
    close(fd_fb0);


    //
    printf("======== var screeninfo ========\n\r");
    printf("xres = %u\n\r", var.xres);
    printf("yres = %u\n\r", var.yres);
    printf("xres_virtual = %u\n\r", var.xres_virtual);
    printf("yres_virtual = %u\n\r", var.yres_virtual);
    printf("xoffset = %u\n\r", var.xoffset);
    printf("yoffset = %u\n\r\n\r", var.yoffset);

    printf("bits_per_pixel = %u\n\r", var.bits_per_pixel);
    printf("grayscale = %u\n\r\n\r", var.grayscale);

    printf("fb_bitfield red, offset = %u, length = %u, right = %u, \n\r", var.red.offset, var.red.length, var.red.msb_right);
    printf("fb_bitfield green, offset = %u, length = %u, right = %u, \n\r", var.green.offset, var.green.length, var.green.msb_right);
    printf("fb_bitfield blue, offset = %u, length = %u, right = %u, \n\r", var.blue.offset, var.blue.length, var.blue.msb_right);
    printf("fb_bitfield transp, offset = %u, length = %u, right = %u, \n\r\n\r", var.transp.offset, var.transp.length, var.transp.msb_right);

    printf("nonstd  = %u\n\r\n\r", var.nonstd);

    printf("activate  = %u\n\r\n\r", var.activate);

    printf("height = %u\n\r", var.height);
    printf("width = %u\n\r\n\r", var.width);

    printf("accel_flags = %u\n\r\n\r", var.accel_flags);

    printf("pixclock = %u\n\r", var.pixclock);
    printf("left_margin = %u\n\r", var.left_margin);
    printf("right_margin = %u\n\r", var.right_margin);
    printf("upper_margin = %u\n\r", var.upper_margin);
    printf("lower_margin = %u\n\r", var.lower_margin);
    printf("hsync_len = %u\n\r", var.hsync_len);
    printf("vsync_len = %u\n\r", var.vsync_len);
    printf("sync = %u\n\r", var.sync);
    printf("vmode = %u\n\r", var.vmode);
    printf("rotate = %u\n\r", var.rotate);
    printf("colorspace = %u\n\r\n\r", var.colorspace);


    //
    printf("======== fix screeninfo ========\n\r");
    printf("id = %s\n\r", fix.id);
    printf("smem_len = %u\n\r", fix.smem_len);
    printf("type = %u\n\r", fix.type);
    printf("type_aux = %u\n\r", fix.type_aux);
    printf("visual = %u\n\r\n\r", fix.visual);

    printf("xpanstep = %u\n\r", fix.xpanstep);
    printf("ypanstep = %u\n\r", fix.ypanstep);
    printf("ywrapstep = %u\n\r\n\r", fix.ywrapstep);

    printf("line_length = %u\n\r", fix.line_length);
    printf("mmio_len = %u\n\r", fix.mmio_len);


    return 0;
}

在Ubuntu 18系统中,编译执行结果如下:
编译:gcc -o fb_settings_read fb_settings_read.c
执行:

$ sudo ./fb_settings_read 
[sudo] password for user: 
======== var screeninfo ========
xres = 1920
yres = 1080
xres_virtual = 1920
yres_virtual = 1080
xoffset = 0
yoffset = 0

bits_per_pixel = 32
grayscale = 0

fb_bitfield red, offset = 16, length = 8, right = 0, 
fb_bitfield green, offset = 8, length = 8, right = 0, 
fb_bitfield blue, offset = 0, length = 8, right = 0, 
fb_bitfield transp, offset = 0, length = 0, right = 0, 

nonstd  = 0

activate  = 0

height = 170
width = 310

accel_flags = 1

pixclock = 0
left_margin = 0
right_margin = 0
upper_margin = 0
lower_margin = 0
hsync_len = 0
vsync_len = 0
sync = 0
vmode = 0
rotate = 0
colorspace = 0

======== fix screeninfo ========
id = inteldrmfb
smem_len = 8294400
type = 0
type_aux = 0
visual = 2

xpanstep = 1
ypanstep = 1
ywrapstep = 0

line_length = 7680
mmio_len = 0

参数简介

struct fb_var_screeninfo:

struct fb_var_screeninfo {
    // 实际分辨率,每行、每列,多少个像素点
	__u32 xres;			/* visible resolution		*/
	__u32 yres;
	// 虚拟分辨率
	__u32 xres_virtual;		/* virtual resolution		*/
	__u32 yres_virtual;
	// 虚拟分辨率到实际分辨率的偏移
	__u32 xoffset;			/* offset from virtual to visible */
	__u32 yoffset;			/* resolution			*/

    // 每个像素点,多少bit,例如ARGB一共32bit,或者一共16bit等
	__u32 bits_per_pixel;		/* guess what			*/
	__u32 grayscale;		/* 0 = color, 1 = grayscale,	*/
					/* >1 = FOURCC			*/
	// ARGB3个颜色及透明度的起始位置、占多少bit
	// struct fb_bitfield结构体说明见下
	struct fb_bitfield red;		/* bitfield in fb mem if true color, */
	struct fb_bitfield green;	/* else only length is significant */
	struct fb_bitfield blue;
	struct fb_bitfield transp;	/* transparency			*/	

    // 非标准像素标记,0-标准;非0-非标准
	__u32 nonstd;			/* != 0 Non standard pixel format */

    // 本配置生效的方式,见下说明
	__u32 activate;			/* see FB_ACTIVATE_*		*/

    // 图片的高度和宽度,单位毫米
	__u32 height;			/* height of picture in mm    */
	__u32 width;			/* width of picture in mm     */

	__u32 accel_flags;		/* (OBSOLETE) see fb_info.flags */

	/* Timing: All values in pixclocks, except pixclock (of course) */
	__u32 pixclock;			/* pixel clock in ps (pico seconds) */
	__u32 left_margin;		/* time from sync to picture	*/
	__u32 right_margin;		/* time from picture to sync	*/
	__u32 upper_margin;		/* time from sync to picture	*/
	__u32 lower_margin;
	__u32 hsync_len;		/* length of horizontal sync	*/
	__u32 vsync_len;		/* length of vertical sync	*/
	__u32 sync;			/* see FB_SYNC_*		*/
	__u32 vmode;			/* see FB_VMODE_*		*/
	__u32 rotate;			/* angle we rotate counter clockwise */
	__u32 colorspace;		/* colorspace for FOURCC-based modes */
	__u32 reserved[4];		/* Reserved for future compatibility */
};

fb_var_screeninfo.activate相关的宏定义,说明当前配置是立即生效、下次生效,或者其他:

#define FB_ACTIVATE_NOW		0	/* set values immediately (or vbl)*/
#define FB_ACTIVATE_NXTOPEN	1	/* activate on next open	*/
#define FB_ACTIVATE_TEST	2	/* don't set, round up impossible */
#define FB_ACTIVATE_MASK       15
					/* values			*/
#define FB_ACTIVATE_VBL	       16	/* activate values on next vbl  */
#define FB_CHANGE_CMAP_VBL     32	/* change colormap on vbl	*/
#define FB_ACTIVATE_ALL	       64	/* change all VCs on this fb	*/
#define FB_ACTIVATE_FORCE     128	/* force apply even when no change*/
#define FB_ACTIVATE_INV_MODE  256       /* invalidate videomode */

struct fb_bitfield:

struct fb_bitfield {
	__u32 offset;			/* beginning of bitfield	*/
	__u32 length;			/* length of bitfield		*/
	__u32 msb_right;		/* != 0 : Most significant bit is */ 
					/* right */ 
};

每个像素点ARGB的信息,A是透明度,0是透明,最大值是完全不透明。
在fb_var_screeninfo.bits_per_pixel定义了1个像素点多少bit,一般:

  • bits_per_pixel = 32bit,ARGB各8bit;
  • bits_per_pixel = 16bit,ARGB中,RGB各5bit,A 1bit;

上面打印的PC信息中:

fb_bitfield red, offset = 16, length = 8, right = 0,    // R分量,偏移从高16bit开始,长度8bit
fb_bitfield green, offset = 8, length = 8, right = 0,   // G分量,偏移从高8bit开始,长度8bit
fb_bitfield blue, offset = 0, length = 8, right = 0,    // B分量,无偏移,长度8bit
fb_bitfield transp, offset = 0, length = 0, right = 0,  // A分量,PC无A分量。LCD有A分量时,这里会有对应值

struct fb_fix_screeninfo,里面主要有用的是smem_lenline_length

struct fb_fix_screeninfo {
	char id[16];			/* identification string eg "TT Builtin" */
	unsigned long smem_start;	/* Start of frame buffer mem */
					/* (physical address) */
    // fb的长度,用于映射到用户空间
	__u32 smem_len;			/* Length of frame buffer mem */
	__u32 type;			/* see FB_TYPE_*		*/
	__u32 type_aux;			/* Interleave for interleaved Planes */
	__u32 visual;			/* see FB_VISUAL_*		*/ 
	__u16 xpanstep;			/* zero if no hardware panning  */
	__u16 ypanstep;			/* zero if no hardware panning  */
	__u16 ywrapstep;		/* zero if no hardware ywrap    */
   // 一行的字节数
	__u32 line_length;		/* length of a line in bytes    */
	unsigned long mmio_start;	/* Start of Memory Mapped I/O   */
					/* (physical address) */
	__u32 mmio_len;			/* Length of Memory Mapped I/O  */
	__u32 accel;			/* Indicate to driver which	*/
					/*  specific chip/card we have	*/
	__u16 capabilities;		/* see FB_CAP_*			*/
	__u16 reserved[2];		/* Reserved for future compatibility */
};
Logo

更多推荐